Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file contains the implementation of the UnwrappedLineParser, |
| 12 | /// which turns a stream of tokens into UnwrappedLines. |
| 13 | /// |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "format-parser" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 17 | |
Chandler Carruth | b1ba0ef | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 18 | #include "UnwrappedLineParser.h" |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 20 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 21 | namespace clang { |
| 22 | namespace format { |
| 23 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 24 | class ScopedDeclarationState { |
| 25 | public: |
| 26 | ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack, |
| 27 | bool MustBeDeclaration) |
| 28 | : Line(Line), Stack(Stack) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 29 | Line.MustBeDeclaration = MustBeDeclaration; |
Manuel Klimek | 836b58f | 2013-01-23 11:03:04 +0000 | [diff] [blame] | 30 | Stack.push_back(MustBeDeclaration); |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 31 | } |
| 32 | ~ScopedDeclarationState() { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 33 | Stack.pop_back(); |
Manuel Klimek | a32a7fd | 2013-01-23 14:08:21 +0000 | [diff] [blame] | 34 | if (!Stack.empty()) |
| 35 | Line.MustBeDeclaration = Stack.back(); |
| 36 | else |
| 37 | Line.MustBeDeclaration = true; |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 38 | } |
| 39 | private: |
| 40 | UnwrappedLine &Line; |
| 41 | std::vector<bool> &Stack; |
| 42 | }; |
| 43 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 44 | class ScopedMacroState : public FormatTokenSource { |
| 45 | public: |
| 46 | ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 47 | FormatToken &ResetToken, bool &StructuralError) |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 48 | : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 49 | PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), |
| 50 | StructuralError(StructuralError), |
| 51 | PreviousStructuralError(StructuralError) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 52 | TokenSource = this; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 53 | Line.Level = 0; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 54 | Line.InPPDirective = true; |
| 55 | } |
| 56 | |
| 57 | ~ScopedMacroState() { |
| 58 | TokenSource = PreviousTokenSource; |
| 59 | ResetToken = Token; |
| 60 | Line.InPPDirective = false; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 61 | Line.Level = PreviousLineLevel; |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 62 | StructuralError = PreviousStructuralError; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | virtual FormatToken getNextToken() { |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 66 | // The \c UnwrappedLineParser guards against this by never calling |
| 67 | // \c getNextToken() after it has encountered the first eof token. |
| 68 | assert(!eof()); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 69 | Token = PreviousTokenSource->getNextToken(); |
| 70 | if (eof()) |
| 71 | return createEOF(); |
| 72 | return Token; |
| 73 | } |
| 74 | |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 75 | virtual unsigned getPosition() { |
| 76 | return PreviousTokenSource->getPosition(); |
| 77 | } |
| 78 | |
| 79 | virtual FormatToken setPosition(unsigned Position) { |
| 80 | Token = PreviousTokenSource->setPosition(Position); |
| 81 | return Token; |
| 82 | } |
| 83 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 84 | private: |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 85 | bool eof() { return Token.HasUnescapedNewline; } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 86 | |
| 87 | FormatToken createEOF() { |
| 88 | FormatToken FormatTok; |
| 89 | FormatTok.Tok.startToken(); |
| 90 | FormatTok.Tok.setKind(tok::eof); |
| 91 | return FormatTok; |
| 92 | } |
| 93 | |
| 94 | UnwrappedLine &Line; |
| 95 | FormatTokenSource *&TokenSource; |
| 96 | FormatToken &ResetToken; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 97 | unsigned PreviousLineLevel; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 98 | FormatTokenSource *PreviousTokenSource; |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 99 | bool &StructuralError; |
| 100 | bool PreviousStructuralError; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 101 | |
| 102 | FormatToken Token; |
| 103 | }; |
| 104 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 105 | class ScopedLineState { |
| 106 | public: |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 107 | ScopedLineState(UnwrappedLineParser &Parser, |
| 108 | bool SwitchToPreprocessorLines = false) |
| 109 | : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) { |
| 110 | if (SwitchToPreprocessorLines) |
| 111 | Parser.CurrentLines = &Parser.PreprocessorDirectives; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 112 | PreBlockLine = Parser.Line.take(); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 113 | Parser.Line.reset(new UnwrappedLine()); |
| 114 | Parser.Line->Level = PreBlockLine->Level; |
| 115 | Parser.Line->InPPDirective = PreBlockLine->InPPDirective; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | ~ScopedLineState() { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 119 | if (!Parser.Line->Tokens.empty()) { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 120 | Parser.addUnwrappedLine(); |
| 121 | } |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 122 | assert(Parser.Line->Tokens.empty()); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 123 | Parser.Line.reset(PreBlockLine); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 124 | Parser.MustBreakBeforeNextToken = true; |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 125 | if (SwitchToPreprocessorLines) |
| 126 | Parser.CurrentLines = &Parser.Lines; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private: |
| 130 | UnwrappedLineParser &Parser; |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 131 | const bool SwitchToPreprocessorLines; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 132 | |
| 133 | UnwrappedLine *PreBlockLine; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 134 | }; |
| 135 | |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 136 | class IndexedTokenSource : public FormatTokenSource { |
| 137 | public: |
| 138 | IndexedTokenSource(ArrayRef<FormatToken> Tokens) |
| 139 | : Tokens(Tokens), Position(-1) {} |
| 140 | |
| 141 | virtual FormatToken getNextToken() { |
| 142 | ++Position; |
| 143 | return Tokens[Position]; |
| 144 | } |
| 145 | |
| 146 | virtual unsigned getPosition() { |
| 147 | assert(Position >= 0); |
| 148 | return Position; |
| 149 | } |
| 150 | |
| 151 | virtual FormatToken setPosition(unsigned P) { |
| 152 | Position = P; |
| 153 | return Tokens[Position]; |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | ArrayRef<FormatToken> Tokens; |
| 158 | int Position; |
| 159 | }; |
| 160 | |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 161 | UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, |
| 162 | FormatTokenSource &Tokens, |
| 163 | UnwrappedLineConsumer &Callback) |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 164 | : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 165 | CurrentLines(&Lines), StructuralError(false), Style(Style), |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 166 | Tokens(NULL), Callback(Callback) { |
| 167 | FormatToken Tok; |
| 168 | do { |
| 169 | Tok = Tokens.getNextToken(); |
| 170 | AllTokens.push_back(Tok); |
| 171 | } while (Tok.Tok.isNot(tok::eof)); |
| 172 | LBraces.resize(AllTokens.size(), BS_Unknown); |
| 173 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 174 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 175 | bool UnwrappedLineParser::parse() { |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 176 | DEBUG(llvm::dbgs() << "----\n"); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 177 | IndexedTokenSource TokenSource(AllTokens); |
| 178 | Tokens = &TokenSource; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 179 | readToken(); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 180 | parseFile(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 181 | for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), E = Lines.end(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 182 | I != E; ++I) { |
| 183 | Callback.consumeUnwrappedLine(*I); |
| 184 | } |
Daniel Jasper | 516fb31 | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 185 | |
| 186 | // Create line with eof token. |
| 187 | pushToken(FormatTok); |
| 188 | Callback.consumeUnwrappedLine(*Line); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 189 | return StructuralError; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 192 | void UnwrappedLineParser::parseFile() { |
Daniel Jasper | 627707b | 2013-03-22 16:55:40 +0000 | [diff] [blame] | 193 | ScopedDeclarationState DeclarationState( |
| 194 | *Line, DeclarationScopeStack, |
| 195 | /*MustBeDeclaration=*/ !Line->InPPDirective); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 196 | parseLevel(/*HasOpeningBrace=*/ false); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 197 | // Make sure to format the remaining tokens. |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 198 | flushComments(true); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 199 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 202 | void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 203 | do { |
| 204 | switch (FormatTok.Tok.getKind()) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 205 | case tok::comment: |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 206 | nextToken(); |
| 207 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 208 | break; |
| 209 | case tok::l_brace: |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 210 | // FIXME: Add parameter whether this can happen - if this happens, we must |
| 211 | // be in a non-declaration context. |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 212 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 213 | addUnwrappedLine(); |
| 214 | break; |
| 215 | case tok::r_brace: |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 216 | if (HasOpeningBrace) |
| 217 | return; |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 218 | StructuralError = true; |
| 219 | nextToken(); |
| 220 | addUnwrappedLine(); |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 221 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 222 | default: |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 223 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | } while (!eof()); |
| 227 | } |
| 228 | |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 229 | void UnwrappedLineParser::calculateBraceTypes() { |
| 230 | // We'll parse forward through the tokens until we hit |
| 231 | // a closing brace or eof - note that getNextToken() will |
| 232 | // parse macros, so this will magically work inside macro |
| 233 | // definitions, too. |
| 234 | unsigned StoredPosition = Tokens->getPosition(); |
| 235 | unsigned Position = StoredPosition; |
| 236 | FormatToken Tok = FormatTok; |
| 237 | // Keep a stack of positions of lbrace tokens. We will |
| 238 | // update information about whether an lbrace starts a |
| 239 | // braced init list or a different block during the loop. |
| 240 | SmallVector<unsigned, 8> LBraceStack; |
| 241 | assert(Tok.Tok.is(tok::l_brace)); |
| 242 | do { |
| 243 | FormatToken NextTok = Tokens->getNextToken(); |
| 244 | switch (Tok.Tok.getKind()) { |
| 245 | case tok::l_brace: |
| 246 | LBraceStack.push_back(Position); |
| 247 | break; |
| 248 | case tok::r_brace: |
| 249 | if (!LBraceStack.empty()) { |
| 250 | if (LBraces[LBraceStack.back()] == BS_Unknown) { |
| 251 | // If there is a comma, semicolon or right paren after the closing |
| 252 | // brace, we assume this is a braced initializer list. |
| 253 | |
| 254 | // FIXME: Note that this currently works only because we do not |
| 255 | // use the brace information while inside a braced init list. |
| 256 | // Thus, if the parent is a braced init list, we consider all |
| 257 | // brace blocks inside it braced init list. That works good enough |
| 258 | // for now, but we will need to fix it to correctly handle lambdas. |
| 259 | if (NextTok.Tok.is(tok::comma) || NextTok.Tok.is(tok::semi) || |
| 260 | NextTok.Tok.is(tok::r_paren)) |
| 261 | LBraces[LBraceStack.back()] = BS_BracedInit; |
| 262 | else |
| 263 | LBraces[LBraceStack.back()] = BS_Block; |
| 264 | } |
| 265 | LBraceStack.pop_back(); |
| 266 | } |
| 267 | break; |
| 268 | case tok::semi: |
| 269 | case tok::kw_if: |
| 270 | case tok::kw_while: |
| 271 | case tok::kw_for: |
| 272 | case tok::kw_switch: |
| 273 | case tok::kw_try: |
| 274 | if (!LBraceStack.empty()) |
| 275 | LBraces[LBraceStack.back()] = BS_Block; |
| 276 | break; |
| 277 | default: |
| 278 | break; |
| 279 | } |
| 280 | Tok = NextTok; |
| 281 | ++Position; |
| 282 | } while (Tok.Tok.isNot(tok::eof)); |
| 283 | // Assume other blocks for all unclosed opening braces. |
| 284 | for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { |
| 285 | if (LBraces[LBraceStack[i]] == BS_Unknown) |
| 286 | LBraces[LBraceStack[i]] = BS_Block; |
| 287 | } |
| 288 | FormatTok = Tokens->setPosition(StoredPosition); |
| 289 | } |
| 290 | |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 291 | void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 292 | unsigned AddLevels) { |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 293 | assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 294 | nextToken(); |
| 295 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 296 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 297 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 298 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 299 | MustBeDeclaration); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 300 | Line->Level += AddLevels; |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 301 | parseLevel(/*HasOpeningBrace=*/ true); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 302 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 303 | if (!FormatTok.Tok.is(tok::r_brace)) { |
| 304 | Line->Level -= AddLevels; |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 305 | StructuralError = true; |
| 306 | return; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 307 | } |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 308 | |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 309 | nextToken(); // Munch the closing brace. |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 310 | Line->Level -= AddLevels; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 314 | assert(FormatTok.Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 315 | ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 316 | nextToken(); |
| 317 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 318 | if (FormatTok.Tok.getIdentifierInfo() == NULL) { |
Manuel Klimek | bd04f2a | 2013-01-31 15:58:48 +0000 | [diff] [blame] | 319 | parsePPUnknown(); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 320 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 321 | } |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 322 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 323 | switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) { |
| 324 | case tok::pp_define: |
| 325 | parsePPDefine(); |
| 326 | break; |
| 327 | default: |
| 328 | parsePPUnknown(); |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void UnwrappedLineParser::parsePPDefine() { |
| 334 | nextToken(); |
| 335 | |
| 336 | if (FormatTok.Tok.getKind() != tok::identifier) { |
| 337 | parsePPUnknown(); |
| 338 | return; |
| 339 | } |
| 340 | nextToken(); |
Manuel Klimek | 7ccbc21 | 2013-01-23 14:37:36 +0000 | [diff] [blame] | 341 | if (FormatTok.Tok.getKind() == tok::l_paren && |
| 342 | FormatTok.WhiteSpaceLength == 0) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 343 | parseParens(); |
| 344 | } |
| 345 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 346 | Line->Level = 1; |
Manuel Klimek | c3d0c82 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 347 | |
| 348 | // Errors during a preprocessor directive can only affect the layout of the |
| 349 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 350 | // would be to use the same approach we use on the file level (no |
| 351 | // re-indentation if there was a structural error) within the macro |
| 352 | // definition. |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 353 | parseFile(); |
| 354 | } |
| 355 | |
| 356 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 357 | do { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 358 | nextToken(); |
| 359 | } while (!eof()); |
| 360 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Alexander Kornienko | 99b0e14 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 363 | // Here we blacklist certain tokens that are not usually the first token in an |
| 364 | // unwrapped line. This is used in attempt to distinguish macro calls without |
| 365 | // trailing semicolons from other constructs split to several lines. |
| 366 | bool tokenCanStartNewLine(clang::Token Tok) { |
| 367 | // Semicolon can be a null-statement, l_square can be a start of a macro or |
| 368 | // a C++11 attribute, but this doesn't seem to be common. |
| 369 | return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && |
| 370 | Tok.isNot(tok::l_square) && |
| 371 | // Tokens that can only be used as binary operators and a part of |
| 372 | // overloaded operator names. |
| 373 | Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && |
| 374 | Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && |
| 375 | Tok.isNot(tok::less) && Tok.isNot(tok::greater) && |
| 376 | Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && |
| 377 | Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && |
| 378 | Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && |
| 379 | Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && |
| 380 | Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && |
| 381 | Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && |
| 382 | Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && |
| 383 | Tok.isNot(tok::lesslessequal) && |
| 384 | // Colon is used in labels, base class lists, initializer lists, |
| 385 | // range-based for loops, ternary operator, but should never be the |
| 386 | // first token in an unwrapped line. |
| 387 | Tok.isNot(tok::colon); |
| 388 | } |
| 389 | |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 390 | void UnwrappedLineParser::parseStructuralElement() { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 391 | assert(!FormatTok.Tok.is(tok::l_brace)); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 392 | switch (FormatTok.Tok.getKind()) { |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 393 | case tok::at: |
| 394 | nextToken(); |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 395 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 396 | parseBracedList(); |
| 397 | break; |
| 398 | } |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 399 | switch (FormatTok.Tok.getObjCKeywordID()) { |
| 400 | case tok::objc_public: |
| 401 | case tok::objc_protected: |
| 402 | case tok::objc_package: |
| 403 | case tok::objc_private: |
| 404 | return parseAccessSpecifier(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 405 | case tok::objc_interface: |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 406 | case tok::objc_implementation: |
| 407 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 408 | case tok::objc_protocol: |
| 409 | return parseObjCProtocol(); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 410 | case tok::objc_end: |
| 411 | return; // Handled by the caller. |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 412 | case tok::objc_optional: |
| 413 | case tok::objc_required: |
| 414 | nextToken(); |
| 415 | addUnwrappedLine(); |
| 416 | return; |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 417 | default: |
| 418 | break; |
| 419 | } |
| 420 | break; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 421 | case tok::kw_namespace: |
| 422 | parseNamespace(); |
| 423 | return; |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 424 | case tok::kw_inline: |
| 425 | nextToken(); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 426 | if (FormatTok.Tok.is(tok::kw_namespace)) { |
| 427 | parseNamespace(); |
| 428 | return; |
| 429 | } |
| 430 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 431 | case tok::kw_public: |
| 432 | case tok::kw_protected: |
| 433 | case tok::kw_private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 434 | parseAccessSpecifier(); |
| 435 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 436 | case tok::kw_if: |
| 437 | parseIfThenElse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 438 | return; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 439 | case tok::kw_for: |
| 440 | case tok::kw_while: |
| 441 | parseForOrWhileLoop(); |
| 442 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 443 | case tok::kw_do: |
| 444 | parseDoWhile(); |
| 445 | return; |
| 446 | case tok::kw_switch: |
| 447 | parseSwitch(); |
| 448 | return; |
| 449 | case tok::kw_default: |
| 450 | nextToken(); |
| 451 | parseLabel(); |
| 452 | return; |
| 453 | case tok::kw_case: |
| 454 | parseCaseLabel(); |
| 455 | return; |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 456 | case tok::kw_return: |
| 457 | parseReturn(); |
| 458 | return; |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 459 | case tok::kw_extern: |
| 460 | nextToken(); |
| 461 | if (FormatTok.Tok.is(tok::string_literal)) { |
| 462 | nextToken(); |
| 463 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 464 | parseBlock(/*MustBeDeclaration=*/ true, 0); |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 465 | addUnwrappedLine(); |
| 466 | return; |
| 467 | } |
| 468 | } |
| 469 | // In all other cases, parse the declaration. |
| 470 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 471 | default: |
| 472 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 473 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 474 | do { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 475 | switch (FormatTok.Tok.getKind()) { |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 476 | case tok::at: |
| 477 | nextToken(); |
| 478 | if (FormatTok.Tok.is(tok::l_brace)) |
| 479 | parseBracedList(); |
| 480 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 481 | case tok::kw_enum: |
| 482 | parseEnum(); |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 483 | break; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 484 | case tok::kw_struct: |
| 485 | case tok::kw_union: |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 486 | case tok::kw_class: |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 487 | parseRecord(); |
| 488 | // A record declaration or definition is always the start of a structural |
| 489 | // element. |
| 490 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 491 | case tok::semi: |
| 492 | nextToken(); |
| 493 | addUnwrappedLine(); |
| 494 | return; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 495 | case tok::r_brace: |
| 496 | addUnwrappedLine(); |
| 497 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 498 | case tok::l_paren: |
| 499 | parseParens(); |
| 500 | break; |
| 501 | case tok::l_brace: |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 502 | if (!tryToParseBracedList()) { |
| 503 | // A block outside of parentheses must be the last part of a |
| 504 | // structural element. |
| 505 | // FIXME: Figure out cases where this is not true, and add projections |
| 506 | // for them (the one we know is missing are lambdas). |
| 507 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || |
| 508 | Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) |
| 509 | addUnwrappedLine(); |
| 510 | parseBlock(/*MustBeDeclaration=*/ false); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 511 | addUnwrappedLine(); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 512 | return; |
| 513 | } |
| 514 | // Otherwise this was a braced init list, and the structural |
| 515 | // element continues. |
| 516 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 517 | case tok::identifier: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 518 | nextToken(); |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 519 | if (Line->Tokens.size() == 1) { |
| 520 | if (FormatTok.Tok.is(tok::colon)) { |
| 521 | parseLabel(); |
| 522 | return; |
| 523 | } |
Alexander Kornienko | 99b0e14 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 524 | // Recognize function-like macro usages without trailing semicolon. |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 525 | if (FormatTok.Tok.is(tok::l_paren)) { |
| 526 | parseParens(); |
Alexander Kornienko | 99b0e14 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 527 | if (FormatTok.HasUnescapedNewline && |
| 528 | tokenCanStartNewLine(FormatTok.Tok)) { |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 529 | addUnwrappedLine(); |
| 530 | return; |
| 531 | } |
| 532 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 533 | } |
| 534 | break; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 535 | case tok::equal: |
| 536 | nextToken(); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 537 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 538 | parseBracedList(); |
| 539 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 540 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 541 | default: |
| 542 | nextToken(); |
| 543 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 544 | } |
| 545 | } while (!eof()); |
| 546 | } |
| 547 | |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 548 | bool UnwrappedLineParser::tryToParseBracedList() { |
| 549 | if (LBraces[Tokens->getPosition()] == BS_Unknown) |
| 550 | calculateBraceTypes(); |
| 551 | assert(LBraces[Tokens->getPosition()] != BS_Unknown); |
| 552 | if (LBraces[Tokens->getPosition()] == BS_Block) |
| 553 | return false; |
| 554 | parseBracedList(); |
| 555 | return true; |
| 556 | } |
| 557 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 558 | void UnwrappedLineParser::parseBracedList() { |
| 559 | nextToken(); |
| 560 | |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 561 | // FIXME: Once we have an expression parser in the UnwrappedLineParser, |
| 562 | // replace this by using parseAssigmentExpression() inside. |
| 563 | bool StartOfExpression = true; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 564 | do { |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 565 | // FIXME: When we start to support lambdas, we'll want to parse them away |
| 566 | // here, otherwise our bail-out scenarios below break. The better solution |
| 567 | // might be to just implement a more or less complete expression parser. |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 568 | switch (FormatTok.Tok.getKind()) { |
| 569 | case tok::l_brace: |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 570 | if (!StartOfExpression) { |
| 571 | // Probably a missing closing brace. Bail out. |
| 572 | addUnwrappedLine(); |
| 573 | return; |
| 574 | } |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 575 | parseBracedList(); |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 576 | StartOfExpression = false; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 577 | break; |
| 578 | case tok::r_brace: |
| 579 | nextToken(); |
| 580 | return; |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 581 | case tok::semi: |
| 582 | // Probably a missing closing brace. Bail out. |
| 583 | return; |
| 584 | case tok::comma: |
| 585 | nextToken(); |
| 586 | StartOfExpression = true; |
| 587 | break; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 588 | default: |
| 589 | nextToken(); |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 590 | StartOfExpression = false; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | } while (!eof()); |
| 594 | } |
| 595 | |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 596 | void UnwrappedLineParser::parseReturn() { |
| 597 | nextToken(); |
| 598 | |
| 599 | do { |
| 600 | switch (FormatTok.Tok.getKind()) { |
| 601 | case tok::l_brace: |
| 602 | parseBracedList(); |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 603 | if (FormatTok.Tok.isNot(tok::semi)) { |
| 604 | // Assume missing ';'. |
| 605 | addUnwrappedLine(); |
| 606 | return; |
| 607 | } |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 608 | break; |
| 609 | case tok::l_paren: |
| 610 | parseParens(); |
| 611 | break; |
| 612 | case tok::r_brace: |
| 613 | // Assume missing ';'. |
| 614 | addUnwrappedLine(); |
| 615 | return; |
| 616 | case tok::semi: |
| 617 | nextToken(); |
| 618 | addUnwrappedLine(); |
| 619 | return; |
| 620 | default: |
| 621 | nextToken(); |
| 622 | break; |
| 623 | } |
| 624 | } while (!eof()); |
| 625 | } |
| 626 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 627 | void UnwrappedLineParser::parseParens() { |
| 628 | assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected."); |
| 629 | nextToken(); |
| 630 | do { |
| 631 | switch (FormatTok.Tok.getKind()) { |
| 632 | case tok::l_paren: |
| 633 | parseParens(); |
| 634 | break; |
| 635 | case tok::r_paren: |
| 636 | nextToken(); |
| 637 | return; |
Nico Weber | 2afbe52 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 638 | case tok::l_brace: { |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 639 | if (!tryToParseBracedList()) { |
| 640 | nextToken(); |
| 641 | ScopedLineState LineState(*this); |
| 642 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 643 | /*MustBeDeclaration=*/ false); |
| 644 | Line->Level += 1; |
| 645 | parseLevel(/*HasOpeningBrace=*/ true); |
| 646 | Line->Level -= 1; |
| 647 | } |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 648 | break; |
Nico Weber | 2afbe52 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 649 | } |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 650 | case tok::at: |
| 651 | nextToken(); |
| 652 | if (FormatTok.Tok.is(tok::l_brace)) |
| 653 | parseBracedList(); |
| 654 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 655 | default: |
| 656 | nextToken(); |
| 657 | break; |
| 658 | } |
| 659 | } while (!eof()); |
| 660 | } |
| 661 | |
| 662 | void UnwrappedLineParser::parseIfThenElse() { |
| 663 | assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected"); |
| 664 | nextToken(); |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 665 | if (FormatTok.Tok.is(tok::l_paren)) |
| 666 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 667 | bool NeedsUnwrappedLine = false; |
| 668 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 669 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 670 | NeedsUnwrappedLine = true; |
| 671 | } else { |
| 672 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 673 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 674 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 675 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 676 | } |
| 677 | if (FormatTok.Tok.is(tok::kw_else)) { |
| 678 | nextToken(); |
| 679 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 680 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 681 | addUnwrappedLine(); |
| 682 | } else if (FormatTok.Tok.is(tok::kw_if)) { |
| 683 | parseIfThenElse(); |
| 684 | } else { |
| 685 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 686 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 687 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 688 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 689 | } |
| 690 | } else if (NeedsUnwrappedLine) { |
| 691 | addUnwrappedLine(); |
| 692 | } |
| 693 | } |
| 694 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 695 | void UnwrappedLineParser::parseNamespace() { |
| 696 | assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); |
| 697 | nextToken(); |
| 698 | if (FormatTok.Tok.is(tok::identifier)) |
| 699 | nextToken(); |
| 700 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 701 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux) |
| 702 | addUnwrappedLine(); |
| 703 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 704 | parseBlock(/*MustBeDeclaration=*/ true, 0); |
Manuel Klimek | 7fc2db0 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 705 | // Munch the semicolon after a namespace. This is more common than one would |
| 706 | // think. Puttin the semicolon into its own line is very ugly. |
| 707 | if (FormatTok.Tok.is(tok::semi)) |
| 708 | nextToken(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 709 | addUnwrappedLine(); |
| 710 | } |
| 711 | // FIXME: Add error handling. |
| 712 | } |
| 713 | |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 714 | void UnwrappedLineParser::parseForOrWhileLoop() { |
| 715 | assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && |
| 716 | "'for' or 'while' expected"); |
| 717 | nextToken(); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 718 | if (FormatTok.Tok.is(tok::l_paren)) |
| 719 | parseParens(); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 720 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 721 | parseBlock(/*MustBeDeclaration=*/ false); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 722 | addUnwrappedLine(); |
| 723 | } else { |
| 724 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 725 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 726 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 727 | --Line->Level; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 731 | void UnwrappedLineParser::parseDoWhile() { |
| 732 | assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected"); |
| 733 | nextToken(); |
| 734 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 735 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 736 | } else { |
| 737 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 738 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 739 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 740 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 743 | // FIXME: Add error handling. |
| 744 | if (!FormatTok.Tok.is(tok::kw_while)) { |
| 745 | addUnwrappedLine(); |
| 746 | return; |
| 747 | } |
| 748 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 749 | nextToken(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 750 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | void UnwrappedLineParser::parseLabel() { |
Daniel Jasper | 89a0daa | 2013-02-12 20:17:17 +0000 | [diff] [blame] | 754 | if (FormatTok.Tok.isNot(tok::colon)) |
| 755 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 756 | nextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 757 | unsigned OldLineLevel = Line->Level; |
Daniel Jasper | bcca7e4 | 2013-03-20 10:23:53 +0000 | [diff] [blame] | 758 | if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 759 | --Line->Level; |
Daniel Jasper | c30eb51 | 2013-03-19 18:33:58 +0000 | [diff] [blame] | 760 | if (CommentsBeforeNextToken.empty() && FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 761 | parseBlock(/*MustBeDeclaration=*/ false); |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 762 | if (FormatTok.Tok.is(tok::kw_break)) |
| 763 | parseStructuralElement(); // "break;" after "}" goes on the same line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 764 | } |
| 765 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 766 | Line->Level = OldLineLevel; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | void UnwrappedLineParser::parseCaseLabel() { |
| 770 | assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected"); |
| 771 | // FIXME: fix handling of complex expressions here. |
| 772 | do { |
| 773 | nextToken(); |
| 774 | } while (!eof() && !FormatTok.Tok.is(tok::colon)); |
| 775 | parseLabel(); |
| 776 | } |
| 777 | |
| 778 | void UnwrappedLineParser::parseSwitch() { |
| 779 | assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected"); |
| 780 | nextToken(); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 781 | if (FormatTok.Tok.is(tok::l_paren)) |
| 782 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 783 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 784 | parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 785 | addUnwrappedLine(); |
| 786 | } else { |
| 787 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 788 | Line->Level += (Style.IndentCaseLabels ? 2 : 1); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 789 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 790 | Line->Level -= (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
| 794 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 795 | nextToken(); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 796 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
| 797 | if (FormatTok.Tok.is(tok::colon)) |
| 798 | nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 799 | addUnwrappedLine(); |
| 800 | } |
| 801 | |
| 802 | void UnwrappedLineParser::parseEnum() { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 803 | nextToken(); |
| 804 | if (FormatTok.Tok.is(tok::identifier) || |
| 805 | FormatTok.Tok.is(tok::kw___attribute) || |
| 806 | FormatTok.Tok.is(tok::kw___declspec)) { |
| 807 | nextToken(); |
| 808 | // We can have macros or attributes in between 'enum' and the enum name. |
| 809 | if (FormatTok.Tok.is(tok::l_paren)) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 810 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 811 | } |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 812 | if (FormatTok.Tok.is(tok::identifier)) |
| 813 | nextToken(); |
| 814 | } |
| 815 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 816 | nextToken(); |
| 817 | addUnwrappedLine(); |
| 818 | ++Line->Level; |
| 819 | do { |
| 820 | switch (FormatTok.Tok.getKind()) { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 821 | case tok::l_paren: |
| 822 | parseParens(); |
| 823 | break; |
| 824 | case tok::r_brace: |
| 825 | addUnwrappedLine(); |
| 826 | nextToken(); |
| 827 | --Line->Level; |
| 828 | return; |
| 829 | case tok::comma: |
| 830 | nextToken(); |
| 831 | addUnwrappedLine(); |
| 832 | break; |
| 833 | default: |
| 834 | nextToken(); |
| 835 | break; |
| 836 | } |
| 837 | } while (!eof()); |
| 838 | } |
| 839 | // We fall through to parsing a structural element afterwards, so that in |
| 840 | // enum A {} n, m; |
| 841 | // "} n, m;" will end up in one unwrapped line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 842 | } |
| 843 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 844 | void UnwrappedLineParser::parseRecord() { |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 845 | nextToken(); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 846 | if (FormatTok.Tok.is(tok::identifier) || |
| 847 | FormatTok.Tok.is(tok::kw___attribute) || |
| 848 | FormatTok.Tok.is(tok::kw___declspec)) { |
| 849 | nextToken(); |
| 850 | // We can have macros or attributes in between 'class' and the class name. |
| 851 | if (FormatTok.Tok.is(tok::l_paren)) { |
| 852 | parseParens(); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 853 | } |
Manuel Klimek | b8b1ce1 | 2013-02-06 15:57:54 +0000 | [diff] [blame] | 854 | // The actual identifier can be a nested name specifier, and in macros |
| 855 | // it is often token-pasted. |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 856 | while (FormatTok.Tok.is(tok::identifier) || |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 857 | FormatTok.Tok.is(tok::coloncolon) || FormatTok.Tok.is(tok::hashhash)) |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 858 | nextToken(); |
| 859 | |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 860 | // Note that parsing away template declarations here leads to incorrectly |
| 861 | // accepting function declarations as record declarations. |
| 862 | // In general, we cannot solve this problem. Consider: |
| 863 | // class A<int> B() {} |
| 864 | // which can be a function definition or a class definition when B() is a |
| 865 | // macro. If we find enough real-world cases where this is a problem, we |
| 866 | // can parse for the 'template' keyword in the beginning of the statement, |
| 867 | // and thus rule out the record production in case there is no template |
| 868 | // (this would still leave us with an ambiguity between template function |
| 869 | // and class declarations). |
| 870 | if (FormatTok.Tok.is(tok::colon) || FormatTok.Tok.is(tok::less)) { |
Daniel Jasper | 6fe554e | 2013-03-20 15:12:38 +0000 | [diff] [blame] | 871 | while (!eof() && FormatTok.Tok.isNot(tok::l_brace)) { |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 872 | if (FormatTok.Tok.is(tok::semi)) |
| 873 | return; |
| 874 | nextToken(); |
| 875 | } |
| 876 | } |
| 877 | } |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 878 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 879 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux) |
| 880 | addUnwrappedLine(); |
| 881 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 882 | parseBlock(/*MustBeDeclaration=*/ true); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 883 | } |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 884 | // We fall through to parsing a structural element afterwards, so |
| 885 | // class A {} n, m; |
| 886 | // will end up in one unwrapped line. |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 889 | void UnwrappedLineParser::parseObjCProtocolList() { |
| 890 | assert(FormatTok.Tok.is(tok::less) && "'<' expected."); |
| 891 | do |
| 892 | nextToken(); |
| 893 | while (!eof() && FormatTok.Tok.isNot(tok::greater)); |
| 894 | nextToken(); // Skip '>'. |
| 895 | } |
| 896 | |
| 897 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 898 | do { |
| 899 | if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) { |
| 900 | nextToken(); |
| 901 | addUnwrappedLine(); |
| 902 | break; |
| 903 | } |
| 904 | parseStructuralElement(); |
| 905 | } while (!eof()); |
| 906 | } |
| 907 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 908 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 909 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 910 | nextToken(); // interface name |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 911 | |
| 912 | // @interface can be followed by either a base class, or a category. |
| 913 | if (FormatTok.Tok.is(tok::colon)) { |
| 914 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 915 | nextToken(); // base class name |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 916 | } else if (FormatTok.Tok.is(tok::l_paren)) |
| 917 | // Skip category, if present. |
| 918 | parseParens(); |
| 919 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 920 | if (FormatTok.Tok.is(tok::less)) |
| 921 | parseObjCProtocolList(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 922 | |
| 923 | // If instance variables are present, keep the '{' on the first line too. |
| 924 | if (FormatTok.Tok.is(tok::l_brace)) |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 925 | parseBlock(/*MustBeDeclaration=*/ true); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 926 | |
| 927 | // With instance variables, this puts '}' on its own line. Without instance |
| 928 | // variables, this ends the @interface line. |
| 929 | addUnwrappedLine(); |
| 930 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 931 | parseObjCUntilAtEnd(); |
| 932 | } |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 933 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 934 | void UnwrappedLineParser::parseObjCProtocol() { |
| 935 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 936 | nextToken(); // protocol name |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 937 | |
| 938 | if (FormatTok.Tok.is(tok::less)) |
| 939 | parseObjCProtocolList(); |
| 940 | |
| 941 | // Check for protocol declaration. |
| 942 | if (FormatTok.Tok.is(tok::semi)) { |
| 943 | nextToken(); |
| 944 | return addUnwrappedLine(); |
| 945 | } |
| 946 | |
| 947 | addUnwrappedLine(); |
| 948 | parseObjCUntilAtEnd(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 951 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 952 | if (Line->Tokens.empty()) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 953 | return; |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 954 | DEBUG({ |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 955 | llvm::dbgs() << "Line(" << Line->Level << ")" |
| 956 | << (Line->InPPDirective ? " MACRO" : "") << ": "; |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 957 | for (std::list<FormatToken>::iterator I = Line->Tokens.begin(), |
| 958 | E = Line->Tokens.end(); |
| 959 | I != E; ++I) { |
| 960 | llvm::dbgs() << I->Tok.getName() << " "; |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 961 | |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 962 | } |
| 963 | llvm::dbgs() << "\n"; |
| 964 | }); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 965 | CurrentLines->push_back(*Line); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 966 | Line->Tokens.clear(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 967 | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { |
Daniel Jasper | 516fb31 | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 968 | for (std::vector<UnwrappedLine>::iterator |
| 969 | I = PreprocessorDirectives.begin(), |
| 970 | E = PreprocessorDirectives.end(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 971 | I != E; ++I) { |
| 972 | CurrentLines->push_back(*I); |
| 973 | } |
| 974 | PreprocessorDirectives.clear(); |
| 975 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 978 | bool UnwrappedLineParser::eof() const { return FormatTok.Tok.is(tok::eof); } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 979 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 980 | void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { |
| 981 | bool JustComments = Line->Tokens.empty(); |
| 982 | for (SmallVectorImpl<FormatToken>::const_iterator |
| 983 | I = CommentsBeforeNextToken.begin(), |
| 984 | E = CommentsBeforeNextToken.end(); |
| 985 | I != E; ++I) { |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 986 | if (I->NewlinesBefore && JustComments) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 987 | addUnwrappedLine(); |
| 988 | } |
| 989 | pushToken(*I); |
| 990 | } |
| 991 | if (NewlineBeforeNext && JustComments) { |
| 992 | addUnwrappedLine(); |
| 993 | } |
| 994 | CommentsBeforeNextToken.clear(); |
| 995 | } |
| 996 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 997 | void UnwrappedLineParser::nextToken() { |
| 998 | if (eof()) |
| 999 | return; |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 1000 | flushComments(FormatTok.NewlinesBefore > 0); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1001 | pushToken(FormatTok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1002 | readToken(); |
| 1003 | } |
| 1004 | |
| 1005 | void UnwrappedLineParser::readToken() { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1006 | bool CommentsInCurrentLine = true; |
| 1007 | do { |
| 1008 | FormatTok = Tokens->getNextToken(); |
| 1009 | while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) && |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 1010 | (FormatTok.HasUnescapedNewline || FormatTok.IsFirst)) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1011 | // If there is an unfinished unwrapped line, we flush the preprocessor |
| 1012 | // directives only after that unwrapped line was finished later. |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1013 | bool SwitchToPreprocessorLines = |
| 1014 | !Line->Tokens.empty() && CurrentLines == &Lines; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1015 | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
Alexander Kornienko | 4128e19 | 2013-04-03 12:38:53 +0000 | [diff] [blame] | 1016 | // Comments stored before the preprocessor directive need to be output |
| 1017 | // before the preprocessor directive, at the same level as the |
| 1018 | // preprocessor directive, as we consider them to apply to the directive. |
| 1019 | flushComments(FormatTok.NewlinesBefore > 0); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1020 | parsePPDirective(); |
| 1021 | } |
| 1022 | if (!FormatTok.Tok.is(tok::comment)) |
| 1023 | return; |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 1024 | if (FormatTok.NewlinesBefore > 0 || FormatTok.IsFirst) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1025 | CommentsInCurrentLine = false; |
| 1026 | } |
| 1027 | if (CommentsInCurrentLine) { |
| 1028 | pushToken(FormatTok); |
| 1029 | } else { |
| 1030 | CommentsBeforeNextToken.push_back(FormatTok); |
| 1031 | } |
| 1032 | } while (!eof()); |
| 1033 | } |
| 1034 | |
| 1035 | void UnwrappedLineParser::pushToken(const FormatToken &Tok) { |
| 1036 | Line->Tokens.push_back(Tok); |
| 1037 | if (MustBreakBeforeNextToken) { |
| 1038 | Line->Tokens.back().MustBreakBefore = true; |
| 1039 | MustBreakBeforeNextToken = false; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1040 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1043 | } // end namespace format |
| 1044 | } // end namespace clang |