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) || |
Daniel Jasper | de0d61f | 2013-05-23 18:29:16 +0000 | [diff] [blame] | 260 | NextTok.Tok.is(tok::r_paren) || NextTok.Tok.is(tok::l_brace)) |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 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(); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 326 | return; |
| 327 | case tok::pp_if: |
| 328 | parsePPIf(); |
| 329 | break; |
| 330 | case tok::pp_ifdef: |
| 331 | case tok::pp_ifndef: |
| 332 | parsePPIfdef(); |
| 333 | break; |
| 334 | case tok::pp_else: |
| 335 | parsePPElse(); |
| 336 | break; |
| 337 | case tok::pp_elif: |
| 338 | parsePPElIf(); |
| 339 | break; |
| 340 | case tok::pp_endif: |
| 341 | parsePPEndIf(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 342 | break; |
| 343 | default: |
| 344 | parsePPUnknown(); |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 349 | void UnwrappedLineParser::pushPPConditional() { |
| 350 | if (!PPStack.empty() && PPStack.back() == PP_Unreachable) |
| 351 | PPStack.push_back(PP_Unreachable); |
| 352 | else |
| 353 | PPStack.push_back(PP_Conditional); |
| 354 | } |
| 355 | |
| 356 | void UnwrappedLineParser::parsePPIf() { |
| 357 | nextToken(); |
| 358 | if ((FormatTok.Tok.isLiteral() && |
| 359 | StringRef(FormatTok.Tok.getLiteralData(), FormatTok.Tok.getLength()) == |
| 360 | "0") || |
| 361 | FormatTok.Tok.is(tok::kw_false)) { |
| 362 | PPStack.push_back(PP_Unreachable); |
| 363 | } else { |
| 364 | pushPPConditional(); |
| 365 | } |
| 366 | parsePPUnknown(); |
| 367 | } |
| 368 | |
| 369 | void UnwrappedLineParser::parsePPIfdef() { |
| 370 | pushPPConditional(); |
| 371 | parsePPUnknown(); |
| 372 | } |
| 373 | |
| 374 | void UnwrappedLineParser::parsePPElse() { |
| 375 | if (!PPStack.empty()) |
| 376 | PPStack.pop_back(); |
| 377 | pushPPConditional(); |
| 378 | parsePPUnknown(); |
| 379 | } |
| 380 | |
| 381 | void UnwrappedLineParser::parsePPElIf() { |
| 382 | parsePPElse(); |
| 383 | } |
| 384 | |
| 385 | void UnwrappedLineParser::parsePPEndIf() { |
| 386 | if (!PPStack.empty()) |
| 387 | PPStack.pop_back(); |
| 388 | parsePPUnknown(); |
| 389 | } |
| 390 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 391 | void UnwrappedLineParser::parsePPDefine() { |
| 392 | nextToken(); |
| 393 | |
| 394 | if (FormatTok.Tok.getKind() != tok::identifier) { |
| 395 | parsePPUnknown(); |
| 396 | return; |
| 397 | } |
| 398 | nextToken(); |
Manuel Klimek | 7ccbc21 | 2013-01-23 14:37:36 +0000 | [diff] [blame] | 399 | if (FormatTok.Tok.getKind() == tok::l_paren && |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 400 | FormatTok.WhitespaceRange.getBegin() == |
| 401 | FormatTok.WhitespaceRange.getEnd()) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 402 | parseParens(); |
| 403 | } |
| 404 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 405 | Line->Level = 1; |
Manuel Klimek | c3d0c82 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 406 | |
| 407 | // Errors during a preprocessor directive can only affect the layout of the |
| 408 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 409 | // would be to use the same approach we use on the file level (no |
| 410 | // re-indentation if there was a structural error) within the macro |
| 411 | // definition. |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 412 | parseFile(); |
| 413 | } |
| 414 | |
| 415 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 416 | do { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 417 | nextToken(); |
| 418 | } while (!eof()); |
| 419 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Alexander Kornienko | 99b0e14 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 422 | // Here we blacklist certain tokens that are not usually the first token in an |
| 423 | // unwrapped line. This is used in attempt to distinguish macro calls without |
| 424 | // trailing semicolons from other constructs split to several lines. |
| 425 | bool tokenCanStartNewLine(clang::Token Tok) { |
| 426 | // Semicolon can be a null-statement, l_square can be a start of a macro or |
| 427 | // a C++11 attribute, but this doesn't seem to be common. |
| 428 | return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && |
| 429 | Tok.isNot(tok::l_square) && |
| 430 | // Tokens that can only be used as binary operators and a part of |
| 431 | // overloaded operator names. |
| 432 | Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && |
| 433 | Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && |
| 434 | Tok.isNot(tok::less) && Tok.isNot(tok::greater) && |
| 435 | Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && |
| 436 | Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && |
| 437 | Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && |
| 438 | Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && |
| 439 | Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && |
| 440 | Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && |
| 441 | Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && |
| 442 | Tok.isNot(tok::lesslessequal) && |
| 443 | // Colon is used in labels, base class lists, initializer lists, |
| 444 | // range-based for loops, ternary operator, but should never be the |
| 445 | // first token in an unwrapped line. |
| 446 | Tok.isNot(tok::colon); |
| 447 | } |
| 448 | |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 449 | void UnwrappedLineParser::parseStructuralElement() { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 450 | assert(!FormatTok.Tok.is(tok::l_brace)); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 451 | switch (FormatTok.Tok.getKind()) { |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 452 | case tok::at: |
| 453 | nextToken(); |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 454 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 455 | parseBracedList(); |
| 456 | break; |
| 457 | } |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 458 | switch (FormatTok.Tok.getObjCKeywordID()) { |
| 459 | case tok::objc_public: |
| 460 | case tok::objc_protected: |
| 461 | case tok::objc_package: |
| 462 | case tok::objc_private: |
| 463 | return parseAccessSpecifier(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 464 | case tok::objc_interface: |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 465 | case tok::objc_implementation: |
| 466 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 467 | case tok::objc_protocol: |
| 468 | return parseObjCProtocol(); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 469 | case tok::objc_end: |
| 470 | return; // Handled by the caller. |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 471 | case tok::objc_optional: |
| 472 | case tok::objc_required: |
| 473 | nextToken(); |
| 474 | addUnwrappedLine(); |
| 475 | return; |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 476 | default: |
| 477 | break; |
| 478 | } |
| 479 | break; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 480 | case tok::kw_namespace: |
| 481 | parseNamespace(); |
| 482 | return; |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 483 | case tok::kw_inline: |
| 484 | nextToken(); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 485 | if (FormatTok.Tok.is(tok::kw_namespace)) { |
| 486 | parseNamespace(); |
| 487 | return; |
| 488 | } |
| 489 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 490 | case tok::kw_public: |
| 491 | case tok::kw_protected: |
| 492 | case tok::kw_private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 493 | parseAccessSpecifier(); |
| 494 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 495 | case tok::kw_if: |
| 496 | parseIfThenElse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 497 | return; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 498 | case tok::kw_for: |
| 499 | case tok::kw_while: |
| 500 | parseForOrWhileLoop(); |
| 501 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 502 | case tok::kw_do: |
| 503 | parseDoWhile(); |
| 504 | return; |
| 505 | case tok::kw_switch: |
| 506 | parseSwitch(); |
| 507 | return; |
| 508 | case tok::kw_default: |
| 509 | nextToken(); |
| 510 | parseLabel(); |
| 511 | return; |
| 512 | case tok::kw_case: |
| 513 | parseCaseLabel(); |
| 514 | return; |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 515 | case tok::kw_return: |
| 516 | parseReturn(); |
| 517 | return; |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 518 | case tok::kw_extern: |
| 519 | nextToken(); |
| 520 | if (FormatTok.Tok.is(tok::string_literal)) { |
| 521 | nextToken(); |
| 522 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 523 | parseBlock(/*MustBeDeclaration=*/ true, 0); |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 524 | addUnwrappedLine(); |
| 525 | return; |
| 526 | } |
| 527 | } |
| 528 | // In all other cases, parse the declaration. |
| 529 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 530 | default: |
| 531 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 532 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 533 | do { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 534 | switch (FormatTok.Tok.getKind()) { |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 535 | case tok::at: |
| 536 | nextToken(); |
| 537 | if (FormatTok.Tok.is(tok::l_brace)) |
| 538 | parseBracedList(); |
| 539 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 540 | case tok::kw_enum: |
| 541 | parseEnum(); |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 542 | break; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 543 | case tok::kw_struct: |
| 544 | case tok::kw_union: |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 545 | case tok::kw_class: |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 546 | parseRecord(); |
| 547 | // A record declaration or definition is always the start of a structural |
| 548 | // element. |
| 549 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 550 | case tok::semi: |
| 551 | nextToken(); |
| 552 | addUnwrappedLine(); |
| 553 | return; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 554 | case tok::r_brace: |
| 555 | addUnwrappedLine(); |
| 556 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 557 | case tok::l_paren: |
| 558 | parseParens(); |
| 559 | break; |
| 560 | case tok::l_brace: |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 561 | if (!tryToParseBracedList()) { |
| 562 | // A block outside of parentheses must be the last part of a |
| 563 | // structural element. |
| 564 | // FIXME: Figure out cases where this is not true, and add projections |
| 565 | // for them (the one we know is missing are lambdas). |
| 566 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || |
| 567 | Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) |
| 568 | addUnwrappedLine(); |
| 569 | parseBlock(/*MustBeDeclaration=*/ false); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 570 | addUnwrappedLine(); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | // Otherwise this was a braced init list, and the structural |
| 574 | // element continues. |
| 575 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 576 | case tok::identifier: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 577 | nextToken(); |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 578 | if (Line->Tokens.size() == 1) { |
| 579 | if (FormatTok.Tok.is(tok::colon)) { |
| 580 | parseLabel(); |
| 581 | return; |
| 582 | } |
Alexander Kornienko | 99b0e14 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 583 | // Recognize function-like macro usages without trailing semicolon. |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 584 | if (FormatTok.Tok.is(tok::l_paren)) { |
| 585 | parseParens(); |
Alexander Kornienko | 99b0e14 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 586 | if (FormatTok.HasUnescapedNewline && |
| 587 | tokenCanStartNewLine(FormatTok.Tok)) { |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 588 | addUnwrappedLine(); |
| 589 | return; |
| 590 | } |
| 591 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 592 | } |
| 593 | break; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 594 | case tok::equal: |
| 595 | nextToken(); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 596 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 597 | parseBracedList(); |
| 598 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 599 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 600 | default: |
| 601 | nextToken(); |
| 602 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 603 | } |
| 604 | } while (!eof()); |
| 605 | } |
| 606 | |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 607 | bool UnwrappedLineParser::tryToParseBracedList() { |
| 608 | if (LBraces[Tokens->getPosition()] == BS_Unknown) |
| 609 | calculateBraceTypes(); |
| 610 | assert(LBraces[Tokens->getPosition()] != BS_Unknown); |
| 611 | if (LBraces[Tokens->getPosition()] == BS_Block) |
| 612 | return false; |
| 613 | parseBracedList(); |
| 614 | return true; |
| 615 | } |
| 616 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 617 | void UnwrappedLineParser::parseBracedList() { |
| 618 | nextToken(); |
| 619 | |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 620 | // FIXME: Once we have an expression parser in the UnwrappedLineParser, |
| 621 | // replace this by using parseAssigmentExpression() inside. |
| 622 | bool StartOfExpression = true; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 623 | do { |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 624 | // FIXME: When we start to support lambdas, we'll want to parse them away |
| 625 | // here, otherwise our bail-out scenarios below break. The better solution |
| 626 | // might be to just implement a more or less complete expression parser. |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 627 | switch (FormatTok.Tok.getKind()) { |
| 628 | case tok::l_brace: |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 629 | if (!StartOfExpression) { |
| 630 | // Probably a missing closing brace. Bail out. |
| 631 | addUnwrappedLine(); |
| 632 | return; |
| 633 | } |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 634 | parseBracedList(); |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 635 | StartOfExpression = false; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 636 | break; |
| 637 | case tok::r_brace: |
| 638 | nextToken(); |
| 639 | return; |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 640 | case tok::semi: |
| 641 | // Probably a missing closing brace. Bail out. |
| 642 | return; |
| 643 | case tok::comma: |
| 644 | nextToken(); |
| 645 | StartOfExpression = true; |
| 646 | break; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 647 | default: |
| 648 | nextToken(); |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 649 | StartOfExpression = false; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 650 | break; |
| 651 | } |
| 652 | } while (!eof()); |
| 653 | } |
| 654 | |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 655 | void UnwrappedLineParser::parseReturn() { |
| 656 | nextToken(); |
| 657 | |
| 658 | do { |
| 659 | switch (FormatTok.Tok.getKind()) { |
| 660 | case tok::l_brace: |
| 661 | parseBracedList(); |
Manuel Klimek | 423dd93 | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 662 | if (FormatTok.Tok.isNot(tok::semi)) { |
| 663 | // Assume missing ';'. |
| 664 | addUnwrappedLine(); |
| 665 | return; |
| 666 | } |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 667 | break; |
| 668 | case tok::l_paren: |
| 669 | parseParens(); |
| 670 | break; |
| 671 | case tok::r_brace: |
| 672 | // Assume missing ';'. |
| 673 | addUnwrappedLine(); |
| 674 | return; |
| 675 | case tok::semi: |
| 676 | nextToken(); |
| 677 | addUnwrappedLine(); |
| 678 | return; |
| 679 | default: |
| 680 | nextToken(); |
| 681 | break; |
| 682 | } |
| 683 | } while (!eof()); |
| 684 | } |
| 685 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 686 | void UnwrappedLineParser::parseParens() { |
| 687 | assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected."); |
| 688 | nextToken(); |
| 689 | do { |
| 690 | switch (FormatTok.Tok.getKind()) { |
| 691 | case tok::l_paren: |
| 692 | parseParens(); |
| 693 | break; |
| 694 | case tok::r_paren: |
| 695 | nextToken(); |
| 696 | return; |
Nico Weber | 2afbe52 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 697 | case tok::l_brace: { |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 698 | if (!tryToParseBracedList()) { |
| 699 | nextToken(); |
| 700 | ScopedLineState LineState(*this); |
| 701 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 702 | /*MustBeDeclaration=*/ false); |
| 703 | Line->Level += 1; |
| 704 | parseLevel(/*HasOpeningBrace=*/ true); |
| 705 | Line->Level -= 1; |
| 706 | } |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 707 | break; |
Nico Weber | 2afbe52 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 708 | } |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 709 | case tok::at: |
| 710 | nextToken(); |
| 711 | if (FormatTok.Tok.is(tok::l_brace)) |
| 712 | parseBracedList(); |
| 713 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 714 | default: |
| 715 | nextToken(); |
| 716 | break; |
| 717 | } |
| 718 | } while (!eof()); |
| 719 | } |
| 720 | |
| 721 | void UnwrappedLineParser::parseIfThenElse() { |
| 722 | assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected"); |
| 723 | nextToken(); |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 724 | if (FormatTok.Tok.is(tok::l_paren)) |
| 725 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 726 | bool NeedsUnwrappedLine = false; |
| 727 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 728 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 729 | NeedsUnwrappedLine = true; |
| 730 | } else { |
| 731 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 732 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 733 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 734 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 735 | } |
| 736 | if (FormatTok.Tok.is(tok::kw_else)) { |
| 737 | nextToken(); |
| 738 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 739 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 740 | addUnwrappedLine(); |
| 741 | } else if (FormatTok.Tok.is(tok::kw_if)) { |
| 742 | parseIfThenElse(); |
| 743 | } else { |
| 744 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 745 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 746 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 747 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 748 | } |
| 749 | } else if (NeedsUnwrappedLine) { |
| 750 | addUnwrappedLine(); |
| 751 | } |
| 752 | } |
| 753 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 754 | void UnwrappedLineParser::parseNamespace() { |
| 755 | assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); |
| 756 | nextToken(); |
| 757 | if (FormatTok.Tok.is(tok::identifier)) |
| 758 | nextToken(); |
| 759 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 760 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux) |
| 761 | addUnwrappedLine(); |
| 762 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 763 | parseBlock(/*MustBeDeclaration=*/ true, 0); |
Manuel Klimek | 7fc2db0 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 764 | // Munch the semicolon after a namespace. This is more common than one would |
| 765 | // think. Puttin the semicolon into its own line is very ugly. |
| 766 | if (FormatTok.Tok.is(tok::semi)) |
| 767 | nextToken(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 768 | addUnwrappedLine(); |
| 769 | } |
| 770 | // FIXME: Add error handling. |
| 771 | } |
| 772 | |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 773 | void UnwrappedLineParser::parseForOrWhileLoop() { |
| 774 | assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && |
| 775 | "'for' or 'while' expected"); |
| 776 | nextToken(); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 777 | if (FormatTok.Tok.is(tok::l_paren)) |
| 778 | parseParens(); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 779 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 780 | parseBlock(/*MustBeDeclaration=*/ false); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 781 | addUnwrappedLine(); |
| 782 | } else { |
| 783 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 784 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 785 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 786 | --Line->Level; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 790 | void UnwrappedLineParser::parseDoWhile() { |
| 791 | assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected"); |
| 792 | nextToken(); |
| 793 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 794 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 795 | } else { |
| 796 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 797 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 798 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 799 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 802 | // FIXME: Add error handling. |
| 803 | if (!FormatTok.Tok.is(tok::kw_while)) { |
| 804 | addUnwrappedLine(); |
| 805 | return; |
| 806 | } |
| 807 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 808 | nextToken(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 809 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | void UnwrappedLineParser::parseLabel() { |
Daniel Jasper | 89a0daa | 2013-02-12 20:17:17 +0000 | [diff] [blame] | 813 | if (FormatTok.Tok.isNot(tok::colon)) |
| 814 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 815 | nextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 816 | unsigned OldLineLevel = Line->Level; |
Daniel Jasper | bcca7e4 | 2013-03-20 10:23:53 +0000 | [diff] [blame] | 817 | if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 818 | --Line->Level; |
Daniel Jasper | c30eb51 | 2013-03-19 18:33:58 +0000 | [diff] [blame] | 819 | if (CommentsBeforeNextToken.empty() && FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 820 | parseBlock(/*MustBeDeclaration=*/ false); |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 821 | if (FormatTok.Tok.is(tok::kw_break)) |
| 822 | parseStructuralElement(); // "break;" after "}" goes on the same line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 823 | } |
| 824 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 825 | Line->Level = OldLineLevel; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | void UnwrappedLineParser::parseCaseLabel() { |
| 829 | assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected"); |
| 830 | // FIXME: fix handling of complex expressions here. |
| 831 | do { |
| 832 | nextToken(); |
| 833 | } while (!eof() && !FormatTok.Tok.is(tok::colon)); |
| 834 | parseLabel(); |
| 835 | } |
| 836 | |
| 837 | void UnwrappedLineParser::parseSwitch() { |
| 838 | assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected"); |
| 839 | nextToken(); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 840 | if (FormatTok.Tok.is(tok::l_paren)) |
| 841 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 842 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 843 | parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 844 | addUnwrappedLine(); |
| 845 | } else { |
| 846 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 847 | Line->Level += (Style.IndentCaseLabels ? 2 : 1); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 848 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 849 | Line->Level -= (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
| 853 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 854 | nextToken(); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 855 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
| 856 | if (FormatTok.Tok.is(tok::colon)) |
| 857 | nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 858 | addUnwrappedLine(); |
| 859 | } |
| 860 | |
| 861 | void UnwrappedLineParser::parseEnum() { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 862 | nextToken(); |
| 863 | if (FormatTok.Tok.is(tok::identifier) || |
| 864 | FormatTok.Tok.is(tok::kw___attribute) || |
| 865 | FormatTok.Tok.is(tok::kw___declspec)) { |
| 866 | nextToken(); |
| 867 | // We can have macros or attributes in between 'enum' and the enum name. |
| 868 | if (FormatTok.Tok.is(tok::l_paren)) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 869 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 870 | } |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 871 | if (FormatTok.Tok.is(tok::identifier)) |
| 872 | nextToken(); |
| 873 | } |
| 874 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 875 | nextToken(); |
| 876 | addUnwrappedLine(); |
| 877 | ++Line->Level; |
| 878 | do { |
| 879 | switch (FormatTok.Tok.getKind()) { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 880 | case tok::l_paren: |
| 881 | parseParens(); |
| 882 | break; |
| 883 | case tok::r_brace: |
| 884 | addUnwrappedLine(); |
| 885 | nextToken(); |
| 886 | --Line->Level; |
| 887 | return; |
| 888 | case tok::comma: |
| 889 | nextToken(); |
| 890 | addUnwrappedLine(); |
| 891 | break; |
| 892 | default: |
| 893 | nextToken(); |
| 894 | break; |
| 895 | } |
| 896 | } while (!eof()); |
| 897 | } |
| 898 | // We fall through to parsing a structural element afterwards, so that in |
| 899 | // enum A {} n, m; |
| 900 | // "} n, m;" will end up in one unwrapped line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 903 | void UnwrappedLineParser::parseRecord() { |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 904 | nextToken(); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 905 | if (FormatTok.Tok.is(tok::identifier) || |
| 906 | FormatTok.Tok.is(tok::kw___attribute) || |
| 907 | FormatTok.Tok.is(tok::kw___declspec)) { |
| 908 | nextToken(); |
| 909 | // We can have macros or attributes in between 'class' and the class name. |
| 910 | if (FormatTok.Tok.is(tok::l_paren)) { |
| 911 | parseParens(); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 912 | } |
Manuel Klimek | b8b1ce1 | 2013-02-06 15:57:54 +0000 | [diff] [blame] | 913 | // The actual identifier can be a nested name specifier, and in macros |
| 914 | // it is often token-pasted. |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 915 | while (FormatTok.Tok.is(tok::identifier) || |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 916 | FormatTok.Tok.is(tok::coloncolon) || FormatTok.Tok.is(tok::hashhash)) |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 917 | nextToken(); |
| 918 | |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 919 | // Note that parsing away template declarations here leads to incorrectly |
| 920 | // accepting function declarations as record declarations. |
| 921 | // In general, we cannot solve this problem. Consider: |
| 922 | // class A<int> B() {} |
| 923 | // which can be a function definition or a class definition when B() is a |
| 924 | // macro. If we find enough real-world cases where this is a problem, we |
| 925 | // can parse for the 'template' keyword in the beginning of the statement, |
| 926 | // and thus rule out the record production in case there is no template |
| 927 | // (this would still leave us with an ambiguity between template function |
| 928 | // and class declarations). |
| 929 | if (FormatTok.Tok.is(tok::colon) || FormatTok.Tok.is(tok::less)) { |
Daniel Jasper | 6fe554e | 2013-03-20 15:12:38 +0000 | [diff] [blame] | 930 | while (!eof() && FormatTok.Tok.isNot(tok::l_brace)) { |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 931 | if (FormatTok.Tok.is(tok::semi)) |
| 932 | return; |
| 933 | nextToken(); |
| 934 | } |
| 935 | } |
| 936 | } |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 937 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 938 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux) |
| 939 | addUnwrappedLine(); |
| 940 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 941 | parseBlock(/*MustBeDeclaration=*/ true); |
Manuel Klimek | 44135b8 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 942 | } |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 943 | // We fall through to parsing a structural element afterwards, so |
| 944 | // class A {} n, m; |
| 945 | // will end up in one unwrapped line. |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 948 | void UnwrappedLineParser::parseObjCProtocolList() { |
| 949 | assert(FormatTok.Tok.is(tok::less) && "'<' expected."); |
| 950 | do |
| 951 | nextToken(); |
| 952 | while (!eof() && FormatTok.Tok.isNot(tok::greater)); |
| 953 | nextToken(); // Skip '>'. |
| 954 | } |
| 955 | |
| 956 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 957 | do { |
| 958 | if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) { |
| 959 | nextToken(); |
| 960 | addUnwrappedLine(); |
| 961 | break; |
| 962 | } |
| 963 | parseStructuralElement(); |
| 964 | } while (!eof()); |
| 965 | } |
| 966 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 967 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 968 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 969 | nextToken(); // interface name |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 970 | |
| 971 | // @interface can be followed by either a base class, or a category. |
| 972 | if (FormatTok.Tok.is(tok::colon)) { |
| 973 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 974 | nextToken(); // base class name |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 975 | } else if (FormatTok.Tok.is(tok::l_paren)) |
| 976 | // Skip category, if present. |
| 977 | parseParens(); |
| 978 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 979 | if (FormatTok.Tok.is(tok::less)) |
| 980 | parseObjCProtocolList(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 981 | |
| 982 | // If instance variables are present, keep the '{' on the first line too. |
| 983 | if (FormatTok.Tok.is(tok::l_brace)) |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 984 | parseBlock(/*MustBeDeclaration=*/ true); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 985 | |
| 986 | // With instance variables, this puts '}' on its own line. Without instance |
| 987 | // variables, this ends the @interface line. |
| 988 | addUnwrappedLine(); |
| 989 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 990 | parseObjCUntilAtEnd(); |
| 991 | } |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 992 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 993 | void UnwrappedLineParser::parseObjCProtocol() { |
| 994 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 995 | nextToken(); // protocol name |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 996 | |
| 997 | if (FormatTok.Tok.is(tok::less)) |
| 998 | parseObjCProtocolList(); |
| 999 | |
| 1000 | // Check for protocol declaration. |
| 1001 | if (FormatTok.Tok.is(tok::semi)) { |
| 1002 | nextToken(); |
| 1003 | return addUnwrappedLine(); |
| 1004 | } |
| 1005 | |
| 1006 | addUnwrappedLine(); |
| 1007 | parseObjCUntilAtEnd(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1010 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1011 | if (Line->Tokens.empty()) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1012 | return; |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1013 | DEBUG({ |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1014 | llvm::dbgs() << "Line(" << Line->Level << ")" |
| 1015 | << (Line->InPPDirective ? " MACRO" : "") << ": "; |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1016 | for (std::list<FormatToken>::iterator I = Line->Tokens.begin(), |
| 1017 | E = Line->Tokens.end(); |
| 1018 | I != E; ++I) { |
| 1019 | llvm::dbgs() << I->Tok.getName() << " "; |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1020 | |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1021 | } |
| 1022 | llvm::dbgs() << "\n"; |
| 1023 | }); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1024 | CurrentLines->push_back(*Line); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1025 | Line->Tokens.clear(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1026 | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { |
Daniel Jasper | 516fb31 | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 1027 | for (std::vector<UnwrappedLine>::iterator |
| 1028 | I = PreprocessorDirectives.begin(), |
| 1029 | E = PreprocessorDirectives.end(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1030 | I != E; ++I) { |
| 1031 | CurrentLines->push_back(*I); |
| 1032 | } |
| 1033 | PreprocessorDirectives.clear(); |
| 1034 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1037 | bool UnwrappedLineParser::eof() const { return FormatTok.Tok.is(tok::eof); } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1038 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1039 | void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { |
| 1040 | bool JustComments = Line->Tokens.empty(); |
| 1041 | for (SmallVectorImpl<FormatToken>::const_iterator |
| 1042 | I = CommentsBeforeNextToken.begin(), |
| 1043 | E = CommentsBeforeNextToken.end(); |
| 1044 | I != E; ++I) { |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 1045 | if (I->NewlinesBefore && JustComments) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1046 | addUnwrappedLine(); |
| 1047 | } |
| 1048 | pushToken(*I); |
| 1049 | } |
| 1050 | if (NewlineBeforeNext && JustComments) { |
| 1051 | addUnwrappedLine(); |
| 1052 | } |
| 1053 | CommentsBeforeNextToken.clear(); |
| 1054 | } |
| 1055 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1056 | void UnwrappedLineParser::nextToken() { |
| 1057 | if (eof()) |
| 1058 | return; |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 1059 | flushComments(FormatTok.NewlinesBefore > 0); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1060 | pushToken(FormatTok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1061 | readToken(); |
| 1062 | } |
| 1063 | |
| 1064 | void UnwrappedLineParser::readToken() { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1065 | bool CommentsInCurrentLine = true; |
| 1066 | do { |
| 1067 | FormatTok = Tokens->getNextToken(); |
| 1068 | while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) && |
Alexander Kornienko | 3d713a7 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 1069 | (FormatTok.HasUnescapedNewline || FormatTok.IsFirst)) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1070 | // If there is an unfinished unwrapped line, we flush the preprocessor |
| 1071 | // directives only after that unwrapped line was finished later. |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1072 | bool SwitchToPreprocessorLines = |
| 1073 | !Line->Tokens.empty() && CurrentLines == &Lines; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1074 | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
Alexander Kornienko | 4128e19 | 2013-04-03 12:38:53 +0000 | [diff] [blame] | 1075 | // Comments stored before the preprocessor directive need to be output |
| 1076 | // before the preprocessor directive, at the same level as the |
| 1077 | // preprocessor directive, as we consider them to apply to the directive. |
| 1078 | flushComments(FormatTok.NewlinesBefore > 0); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1079 | parsePPDirective(); |
| 1080 | } |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 1081 | |
| 1082 | if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) && |
| 1083 | !Line->InPPDirective) { |
| 1084 | continue; |
| 1085 | } |
| 1086 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1087 | if (!FormatTok.Tok.is(tok::comment)) |
| 1088 | return; |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 1089 | if (FormatTok.NewlinesBefore > 0 || FormatTok.IsFirst) { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1090 | CommentsInCurrentLine = false; |
| 1091 | } |
| 1092 | if (CommentsInCurrentLine) { |
| 1093 | pushToken(FormatTok); |
| 1094 | } else { |
| 1095 | CommentsBeforeNextToken.push_back(FormatTok); |
| 1096 | } |
| 1097 | } while (!eof()); |
| 1098 | } |
| 1099 | |
| 1100 | void UnwrappedLineParser::pushToken(const FormatToken &Tok) { |
| 1101 | Line->Tokens.push_back(Tok); |
| 1102 | if (MustBreakBeforeNextToken) { |
| 1103 | Line->Tokens.back().MustBreakBefore = true; |
| 1104 | MustBreakBeforeNextToken = false; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1105 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1108 | } // end namespace format |
| 1109 | } // end namespace clang |