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 | /// |
| 14 | /// This is EXPERIMENTAL code under heavy development. It is not in a state yet, |
| 15 | /// where it can be used to format real code. |
| 16 | /// |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "format-parser" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 20 | |
Chandler Carruth | b1ba0ef | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 21 | #include "UnwrappedLineParser.h" |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
| 23 | #include "llvm/Support/Debug.h" |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 24 | |
| 25 | // Uncomment to get debug output from tests: |
| 26 | // #define DEBUG_WITH_TYPE(T, X) do { X; } while(0) |
Manuel Klimek | 4c60fc6 | 2013-01-10 10:05:08 +0000 | [diff] [blame] | 27 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 28 | namespace clang { |
| 29 | namespace format { |
| 30 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 31 | class ScopedDeclarationState { |
| 32 | public: |
| 33 | ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack, |
| 34 | bool MustBeDeclaration) |
| 35 | : Line(Line), Stack(Stack) { |
| 36 | Stack.push_back(MustBeDeclaration); |
| 37 | Line.MustBeDeclaration = MustBeDeclaration; |
| 38 | } |
| 39 | ~ScopedDeclarationState() { |
| 40 | Line.MustBeDeclaration = Stack.back(); |
| 41 | Stack.pop_back(); |
| 42 | } |
| 43 | private: |
| 44 | UnwrappedLine &Line; |
| 45 | std::vector<bool> &Stack; |
| 46 | }; |
| 47 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 48 | class ScopedMacroState : public FormatTokenSource { |
| 49 | public: |
| 50 | ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, |
| 51 | FormatToken &ResetToken) |
| 52 | : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 53 | PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 54 | TokenSource = this; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 55 | Line.Level = 0; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 56 | Line.InPPDirective = true; |
| 57 | } |
| 58 | |
| 59 | ~ScopedMacroState() { |
| 60 | TokenSource = PreviousTokenSource; |
| 61 | ResetToken = Token; |
| 62 | Line.InPPDirective = false; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 63 | Line.Level = PreviousLineLevel; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | virtual FormatToken getNextToken() { |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 67 | // The \c UnwrappedLineParser guards against this by never calling |
| 68 | // \c getNextToken() after it has encountered the first eof token. |
| 69 | assert(!eof()); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 70 | Token = PreviousTokenSource->getNextToken(); |
| 71 | if (eof()) |
| 72 | return createEOF(); |
| 73 | return Token; |
| 74 | } |
| 75 | |
| 76 | private: |
| 77 | bool eof() { |
| 78 | return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline; |
| 79 | } |
| 80 | |
| 81 | FormatToken createEOF() { |
| 82 | FormatToken FormatTok; |
| 83 | FormatTok.Tok.startToken(); |
| 84 | FormatTok.Tok.setKind(tok::eof); |
| 85 | return FormatTok; |
| 86 | } |
| 87 | |
| 88 | UnwrappedLine &Line; |
| 89 | FormatTokenSource *&TokenSource; |
| 90 | FormatToken &ResetToken; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 91 | unsigned PreviousLineLevel; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 92 | FormatTokenSource *PreviousTokenSource; |
| 93 | |
| 94 | FormatToken Token; |
| 95 | }; |
| 96 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 97 | class ScopedLineState { |
| 98 | public: |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 99 | ScopedLineState(UnwrappedLineParser &Parser, |
| 100 | bool SwitchToPreprocessorLines = false) |
| 101 | : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) { |
| 102 | if (SwitchToPreprocessorLines) |
| 103 | Parser.CurrentLines = &Parser.PreprocessorDirectives; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 104 | PreBlockLine = Parser.Line.take(); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 105 | Parser.Line.reset(new UnwrappedLine()); |
| 106 | Parser.Line->Level = PreBlockLine->Level; |
| 107 | Parser.Line->InPPDirective = PreBlockLine->InPPDirective; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | ~ScopedLineState() { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 111 | if (!Parser.Line->Tokens.empty()) { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 112 | Parser.addUnwrappedLine(); |
| 113 | } |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 114 | assert(Parser.Line->Tokens.empty()); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 115 | Parser.Line.reset(PreBlockLine); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 116 | Parser.MustBreakBeforeNextToken = true; |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 117 | if (SwitchToPreprocessorLines) |
| 118 | Parser.CurrentLines = &Parser.Lines; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | private: |
| 122 | UnwrappedLineParser &Parser; |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 123 | const bool SwitchToPreprocessorLines; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 124 | |
| 125 | UnwrappedLine *PreBlockLine; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 128 | UnwrappedLineParser::UnwrappedLineParser( |
| 129 | clang::DiagnosticsEngine &Diag, const FormatStyle &Style, |
| 130 | FormatTokenSource &Tokens, UnwrappedLineConsumer &Callback) |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 131 | : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), |
| 132 | CurrentLines(&Lines), Diag(Diag), Style(Style), Tokens(&Tokens), |
| 133 | Callback(Callback) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 134 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 135 | bool UnwrappedLineParser::parse() { |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 136 | DEBUG(llvm::dbgs() << "----\n"); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 137 | readToken(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 138 | bool Error = parseFile(); |
| 139 | for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), |
| 140 | E = Lines.end(); |
| 141 | I != E; ++I) { |
| 142 | Callback.consumeUnwrappedLine(*I); |
| 143 | } |
| 144 | return Error; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | bool UnwrappedLineParser::parseFile() { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 148 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 149 | /*MustBeDeclaration=*/ true); |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 150 | bool Error = parseLevel(/*HasOpeningBrace=*/false); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 151 | // Make sure to format the remaining tokens. |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 152 | flushComments(true); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 153 | addUnwrappedLine(); |
| 154 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 157 | bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 158 | bool Error = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 159 | do { |
| 160 | switch (FormatTok.Tok.getKind()) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 161 | case tok::comment: |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 162 | nextToken(); |
| 163 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 164 | break; |
| 165 | case tok::l_brace: |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 166 | // FIXME: Add parameter whether this can happen - if this happens, we must |
| 167 | // be in a non-declaration context. |
| 168 | Error |= parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 169 | addUnwrappedLine(); |
| 170 | break; |
| 171 | case tok::r_brace: |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 172 | if (HasOpeningBrace) { |
| 173 | return false; |
| 174 | } else { |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 175 | Diag.Report(FormatTok.Tok.getLocation(), |
| 176 | Diag.getCustomDiagID(clang::DiagnosticsEngine::Error, |
Alexander Kornienko | 276a209 | 2013-01-11 16:03:45 +0000 | [diff] [blame] | 177 | "unexpected '}'")); |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 178 | Error = true; |
| 179 | nextToken(); |
| 180 | addUnwrappedLine(); |
| 181 | } |
| 182 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 183 | default: |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 184 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 185 | break; |
| 186 | } |
| 187 | } while (!eof()); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 188 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 191 | bool UnwrappedLineParser::parseBlock(bool MustBeDeclaration, unsigned AddLevels) { |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 192 | assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 193 | nextToken(); |
| 194 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 195 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 196 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 197 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 198 | MustBeDeclaration); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 199 | Line->Level += AddLevels; |
| 200 | parseLevel(/*HasOpeningBrace=*/true); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 201 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 202 | if (!FormatTok.Tok.is(tok::r_brace)) { |
| 203 | Line->Level -= AddLevels; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 204 | return true; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 205 | } |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 206 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 207 | nextToken(); // Munch the closing brace. |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 208 | Line->Level -= AddLevels; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 209 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 213 | assert(FormatTok.Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 214 | ScopedMacroState MacroState(*Line, Tokens, FormatTok); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 215 | nextToken(); |
| 216 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 217 | if (FormatTok.Tok.getIdentifierInfo() == NULL) { |
| 218 | addUnwrappedLine(); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 219 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 220 | } |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 221 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 222 | switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) { |
| 223 | case tok::pp_define: |
| 224 | parsePPDefine(); |
| 225 | break; |
| 226 | default: |
| 227 | parsePPUnknown(); |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void UnwrappedLineParser::parsePPDefine() { |
| 233 | nextToken(); |
| 234 | |
| 235 | if (FormatTok.Tok.getKind() != tok::identifier) { |
| 236 | parsePPUnknown(); |
| 237 | return; |
| 238 | } |
| 239 | nextToken(); |
| 240 | if (FormatTok.Tok.getKind() == tok::l_paren) { |
| 241 | parseParens(); |
| 242 | } |
| 243 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 244 | Line->Level = 1; |
Manuel Klimek | c3d0c82 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 245 | |
| 246 | // Errors during a preprocessor directive can only affect the layout of the |
| 247 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 248 | // would be to use the same approach we use on the file level (no |
| 249 | // re-indentation if there was a structural error) within the macro |
| 250 | // definition. |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 251 | parseFile(); |
| 252 | } |
| 253 | |
| 254 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 255 | do { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 256 | nextToken(); |
| 257 | } while (!eof()); |
| 258 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 261 | void UnwrappedLineParser::parseStructuralElement() { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 262 | assert(!FormatTok.Tok.is(tok::l_brace)); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 263 | int TokenNumber = 0; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 264 | switch (FormatTok.Tok.getKind()) { |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 265 | case tok::at: |
| 266 | nextToken(); |
| 267 | switch (FormatTok.Tok.getObjCKeywordID()) { |
| 268 | case tok::objc_public: |
| 269 | case tok::objc_protected: |
| 270 | case tok::objc_package: |
| 271 | case tok::objc_private: |
| 272 | return parseAccessSpecifier(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 273 | case tok::objc_interface: |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 274 | case tok::objc_implementation: |
| 275 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 276 | case tok::objc_protocol: |
| 277 | return parseObjCProtocol(); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 278 | case tok::objc_end: |
| 279 | return; // Handled by the caller. |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 280 | case tok::objc_optional: |
| 281 | case tok::objc_required: |
| 282 | nextToken(); |
| 283 | addUnwrappedLine(); |
| 284 | return; |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 285 | default: |
| 286 | break; |
| 287 | } |
| 288 | break; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 289 | case tok::kw_namespace: |
| 290 | parseNamespace(); |
| 291 | return; |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 292 | case tok::kw_inline: |
| 293 | nextToken(); |
| 294 | TokenNumber++; |
| 295 | if (FormatTok.Tok.is(tok::kw_namespace)) { |
| 296 | parseNamespace(); |
| 297 | return; |
| 298 | } |
| 299 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 300 | case tok::kw_public: |
| 301 | case tok::kw_protected: |
| 302 | case tok::kw_private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 303 | parseAccessSpecifier(); |
| 304 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 305 | case tok::kw_if: |
| 306 | parseIfThenElse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 307 | return; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 308 | case tok::kw_for: |
| 309 | case tok::kw_while: |
| 310 | parseForOrWhileLoop(); |
| 311 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 312 | case tok::kw_do: |
| 313 | parseDoWhile(); |
| 314 | return; |
| 315 | case tok::kw_switch: |
| 316 | parseSwitch(); |
| 317 | return; |
| 318 | case tok::kw_default: |
| 319 | nextToken(); |
| 320 | parseLabel(); |
| 321 | return; |
| 322 | case tok::kw_case: |
| 323 | parseCaseLabel(); |
| 324 | return; |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 325 | case tok::kw_return: |
| 326 | parseReturn(); |
| 327 | return; |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 328 | case tok::kw_extern: |
| 329 | nextToken(); |
| 330 | if (FormatTok.Tok.is(tok::string_literal)) { |
| 331 | nextToken(); |
| 332 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 333 | parseBlock(/*MustBeDeclaration=*/ true, 0); |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 334 | addUnwrappedLine(); |
| 335 | return; |
| 336 | } |
| 337 | } |
| 338 | // In all other cases, parse the declaration. |
| 339 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 340 | default: |
| 341 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 342 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 343 | do { |
| 344 | ++TokenNumber; |
| 345 | switch (FormatTok.Tok.getKind()) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 346 | case tok::kw_enum: |
| 347 | parseEnum(); |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 348 | break; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 349 | case tok::kw_struct: |
| 350 | case tok::kw_union: |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 351 | case tok::kw_class: |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 352 | parseRecord(); |
| 353 | // A record declaration or definition is always the start of a structural |
| 354 | // element. |
| 355 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 356 | case tok::semi: |
| 357 | nextToken(); |
| 358 | addUnwrappedLine(); |
| 359 | return; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 360 | case tok::r_brace: |
| 361 | addUnwrappedLine(); |
| 362 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 363 | case tok::l_paren: |
| 364 | parseParens(); |
| 365 | break; |
| 366 | case tok::l_brace: |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 367 | // A block outside of parentheses must be the last part of a |
| 368 | // structural element. |
| 369 | // FIXME: Figure out cases where this is not true, and add projections for |
| 370 | // them (the one we know is missing are lambdas). |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 371 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 372 | addUnwrappedLine(); |
| 373 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 374 | case tok::identifier: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 375 | nextToken(); |
| 376 | if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) { |
| 377 | parseLabel(); |
| 378 | return; |
| 379 | } |
| 380 | break; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 381 | case tok::equal: |
| 382 | nextToken(); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 383 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 384 | parseBracedList(); |
| 385 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 386 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 387 | default: |
| 388 | nextToken(); |
| 389 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 390 | } |
| 391 | } while (!eof()); |
| 392 | } |
| 393 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 394 | void UnwrappedLineParser::parseBracedList() { |
| 395 | nextToken(); |
| 396 | |
| 397 | do { |
| 398 | switch (FormatTok.Tok.getKind()) { |
| 399 | case tok::l_brace: |
| 400 | parseBracedList(); |
| 401 | break; |
| 402 | case tok::r_brace: |
| 403 | nextToken(); |
| 404 | return; |
| 405 | default: |
| 406 | nextToken(); |
| 407 | break; |
| 408 | } |
| 409 | } while (!eof()); |
| 410 | } |
| 411 | |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 412 | void UnwrappedLineParser::parseReturn() { |
| 413 | nextToken(); |
| 414 | |
| 415 | do { |
| 416 | switch (FormatTok.Tok.getKind()) { |
| 417 | case tok::l_brace: |
| 418 | parseBracedList(); |
| 419 | break; |
| 420 | case tok::l_paren: |
| 421 | parseParens(); |
| 422 | break; |
| 423 | case tok::r_brace: |
| 424 | // Assume missing ';'. |
| 425 | addUnwrappedLine(); |
| 426 | return; |
| 427 | case tok::semi: |
| 428 | nextToken(); |
| 429 | addUnwrappedLine(); |
| 430 | return; |
| 431 | default: |
| 432 | nextToken(); |
| 433 | break; |
| 434 | } |
| 435 | } while (!eof()); |
| 436 | } |
| 437 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 438 | void UnwrappedLineParser::parseParens() { |
| 439 | assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected."); |
| 440 | nextToken(); |
| 441 | do { |
| 442 | switch (FormatTok.Tok.getKind()) { |
| 443 | case tok::l_paren: |
| 444 | parseParens(); |
| 445 | break; |
| 446 | case tok::r_paren: |
| 447 | nextToken(); |
| 448 | return; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 449 | case tok::l_brace: |
| 450 | { |
| 451 | nextToken(); |
| 452 | ScopedLineState LineState(*this); |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 453 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 454 | /*MustBeDeclaration=*/ false); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 455 | Line->Level += 1; |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 456 | parseLevel(/*HasOpeningBrace=*/ true); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 457 | Line->Level -= 1; |
| 458 | } |
| 459 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 460 | default: |
| 461 | nextToken(); |
| 462 | break; |
| 463 | } |
| 464 | } while (!eof()); |
| 465 | } |
| 466 | |
| 467 | void UnwrappedLineParser::parseIfThenElse() { |
| 468 | assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected"); |
| 469 | nextToken(); |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 470 | if (FormatTok.Tok.is(tok::l_paren)) |
| 471 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 472 | bool NeedsUnwrappedLine = false; |
| 473 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 474 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 475 | NeedsUnwrappedLine = true; |
| 476 | } else { |
| 477 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 478 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 479 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 480 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 481 | } |
| 482 | if (FormatTok.Tok.is(tok::kw_else)) { |
| 483 | nextToken(); |
| 484 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 485 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 486 | addUnwrappedLine(); |
| 487 | } else if (FormatTok.Tok.is(tok::kw_if)) { |
| 488 | parseIfThenElse(); |
| 489 | } else { |
| 490 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 491 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 492 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 493 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 494 | } |
| 495 | } else if (NeedsUnwrappedLine) { |
| 496 | addUnwrappedLine(); |
| 497 | } |
| 498 | } |
| 499 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 500 | void UnwrappedLineParser::parseNamespace() { |
| 501 | assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); |
| 502 | nextToken(); |
| 503 | if (FormatTok.Tok.is(tok::identifier)) |
| 504 | nextToken(); |
| 505 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 506 | parseBlock(/*MustBeDeclaration=*/ true, 0); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 507 | addUnwrappedLine(); |
| 508 | } |
| 509 | // FIXME: Add error handling. |
| 510 | } |
| 511 | |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 512 | void UnwrappedLineParser::parseForOrWhileLoop() { |
| 513 | assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && |
| 514 | "'for' or 'while' expected"); |
| 515 | nextToken(); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 516 | if (FormatTok.Tok.is(tok::l_paren)) |
| 517 | parseParens(); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 518 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 519 | parseBlock(/*MustBeDeclaration=*/ false); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 520 | addUnwrappedLine(); |
| 521 | } else { |
| 522 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 523 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 524 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 525 | --Line->Level; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 529 | void UnwrappedLineParser::parseDoWhile() { |
| 530 | assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected"); |
| 531 | nextToken(); |
| 532 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 533 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 534 | } else { |
| 535 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 536 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 537 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 538 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 541 | // FIXME: Add error handling. |
| 542 | if (!FormatTok.Tok.is(tok::kw_while)) { |
| 543 | addUnwrappedLine(); |
| 544 | return; |
| 545 | } |
| 546 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 547 | nextToken(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 548 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | void UnwrappedLineParser::parseLabel() { |
| 552 | // FIXME: remove all asserts. |
| 553 | assert(FormatTok.Tok.is(tok::colon) && "':' expected"); |
| 554 | nextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 555 | unsigned OldLineLevel = Line->Level; |
| 556 | if (Line->Level > 0) |
| 557 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 558 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 559 | parseBlock(/*MustBeDeclaration=*/ false); |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 560 | if (FormatTok.Tok.is(tok::kw_break)) |
| 561 | parseStructuralElement(); // "break;" after "}" goes on the same line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 562 | } |
| 563 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 564 | Line->Level = OldLineLevel; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | void UnwrappedLineParser::parseCaseLabel() { |
| 568 | assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected"); |
| 569 | // FIXME: fix handling of complex expressions here. |
| 570 | do { |
| 571 | nextToken(); |
| 572 | } while (!eof() && !FormatTok.Tok.is(tok::colon)); |
| 573 | parseLabel(); |
| 574 | } |
| 575 | |
| 576 | void UnwrappedLineParser::parseSwitch() { |
| 577 | assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected"); |
| 578 | nextToken(); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 579 | if (FormatTok.Tok.is(tok::l_paren)) |
| 580 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 581 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 582 | parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 583 | addUnwrappedLine(); |
| 584 | } else { |
| 585 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 586 | Line->Level += (Style.IndentCaseLabels ? 2 : 1); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 587 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 588 | Line->Level -= (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 593 | nextToken(); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 594 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
| 595 | if (FormatTok.Tok.is(tok::colon)) |
| 596 | nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 597 | addUnwrappedLine(); |
| 598 | } |
| 599 | |
| 600 | void UnwrappedLineParser::parseEnum() { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 601 | nextToken(); |
| 602 | if (FormatTok.Tok.is(tok::identifier) || |
| 603 | FormatTok.Tok.is(tok::kw___attribute) || |
| 604 | FormatTok.Tok.is(tok::kw___declspec)) { |
| 605 | nextToken(); |
| 606 | // We can have macros or attributes in between 'enum' and the enum name. |
| 607 | if (FormatTok.Tok.is(tok::l_paren)) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 608 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 609 | } |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 610 | if (FormatTok.Tok.is(tok::identifier)) |
| 611 | nextToken(); |
| 612 | } |
| 613 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 614 | nextToken(); |
| 615 | addUnwrappedLine(); |
| 616 | ++Line->Level; |
| 617 | do { |
| 618 | switch (FormatTok.Tok.getKind()) { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 619 | case tok::l_paren: |
| 620 | parseParens(); |
| 621 | break; |
| 622 | case tok::r_brace: |
| 623 | addUnwrappedLine(); |
| 624 | nextToken(); |
| 625 | --Line->Level; |
| 626 | return; |
| 627 | case tok::comma: |
| 628 | nextToken(); |
| 629 | addUnwrappedLine(); |
| 630 | break; |
| 631 | default: |
| 632 | nextToken(); |
| 633 | break; |
| 634 | } |
| 635 | } while (!eof()); |
| 636 | } |
| 637 | // We fall through to parsing a structural element afterwards, so that in |
| 638 | // enum A {} n, m; |
| 639 | // "} n, m;" will end up in one unwrapped line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 642 | void UnwrappedLineParser::parseRecord() { |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 643 | nextToken(); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 644 | if (FormatTok.Tok.is(tok::identifier) || |
| 645 | FormatTok.Tok.is(tok::kw___attribute) || |
| 646 | FormatTok.Tok.is(tok::kw___declspec)) { |
| 647 | nextToken(); |
| 648 | // We can have macros or attributes in between 'class' and the class name. |
| 649 | if (FormatTok.Tok.is(tok::l_paren)) { |
| 650 | parseParens(); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 651 | } |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 652 | // The actual identifier can be a nested name specifier. |
| 653 | while (FormatTok.Tok.is(tok::identifier) || |
| 654 | FormatTok.Tok.is(tok::coloncolon)) |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 655 | nextToken(); |
| 656 | |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 657 | // Note that parsing away template declarations here leads to incorrectly |
| 658 | // accepting function declarations as record declarations. |
| 659 | // In general, we cannot solve this problem. Consider: |
| 660 | // class A<int> B() {} |
| 661 | // which can be a function definition or a class definition when B() is a |
| 662 | // macro. If we find enough real-world cases where this is a problem, we |
| 663 | // can parse for the 'template' keyword in the beginning of the statement, |
| 664 | // and thus rule out the record production in case there is no template |
| 665 | // (this would still leave us with an ambiguity between template function |
| 666 | // and class declarations). |
| 667 | if (FormatTok.Tok.is(tok::colon) || FormatTok.Tok.is(tok::less)) { |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 668 | while (FormatTok.Tok.isNot(tok::l_brace)) { |
| 669 | if (FormatTok.Tok.is(tok::semi)) |
| 670 | return; |
| 671 | nextToken(); |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | if (FormatTok.Tok.is(tok::l_brace)) |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 676 | parseBlock(/*MustBeDeclaration=*/ true); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 677 | // We fall through to parsing a structural element afterwards, so |
| 678 | // class A {} n, m; |
| 679 | // will end up in one unwrapped line. |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 682 | void UnwrappedLineParser::parseObjCProtocolList() { |
| 683 | assert(FormatTok.Tok.is(tok::less) && "'<' expected."); |
| 684 | do |
| 685 | nextToken(); |
| 686 | while (!eof() && FormatTok.Tok.isNot(tok::greater)); |
| 687 | nextToken(); // Skip '>'. |
| 688 | } |
| 689 | |
| 690 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 691 | do { |
| 692 | if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) { |
| 693 | nextToken(); |
| 694 | addUnwrappedLine(); |
| 695 | break; |
| 696 | } |
| 697 | parseStructuralElement(); |
| 698 | } while (!eof()); |
| 699 | } |
| 700 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 701 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 702 | nextToken(); |
| 703 | nextToken(); // interface name |
| 704 | |
| 705 | // @interface can be followed by either a base class, or a category. |
| 706 | if (FormatTok.Tok.is(tok::colon)) { |
| 707 | nextToken(); |
| 708 | nextToken(); // base class name |
| 709 | } else if (FormatTok.Tok.is(tok::l_paren)) |
| 710 | // Skip category, if present. |
| 711 | parseParens(); |
| 712 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 713 | if (FormatTok.Tok.is(tok::less)) |
| 714 | parseObjCProtocolList(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 715 | |
| 716 | // If instance variables are present, keep the '{' on the first line too. |
| 717 | if (FormatTok.Tok.is(tok::l_brace)) |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 718 | parseBlock(/*MustBeDeclaration=*/ true); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 719 | |
| 720 | // With instance variables, this puts '}' on its own line. Without instance |
| 721 | // variables, this ends the @interface line. |
| 722 | addUnwrappedLine(); |
| 723 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 724 | parseObjCUntilAtEnd(); |
| 725 | } |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 726 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 727 | void UnwrappedLineParser::parseObjCProtocol() { |
| 728 | nextToken(); |
| 729 | nextToken(); // protocol name |
| 730 | |
| 731 | if (FormatTok.Tok.is(tok::less)) |
| 732 | parseObjCProtocolList(); |
| 733 | |
| 734 | // Check for protocol declaration. |
| 735 | if (FormatTok.Tok.is(tok::semi)) { |
| 736 | nextToken(); |
| 737 | return addUnwrappedLine(); |
| 738 | } |
| 739 | |
| 740 | addUnwrappedLine(); |
| 741 | parseObjCUntilAtEnd(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 744 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 745 | if (Line->Tokens.empty()) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 746 | return; |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 747 | DEBUG({ |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 748 | llvm::dbgs() << "Line(" << Line->Level << "): "; |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 749 | for (std::list<FormatToken>::iterator I = Line->Tokens.begin(), |
| 750 | E = Line->Tokens.end(); |
| 751 | I != E; ++I) { |
| 752 | llvm::dbgs() << I->Tok.getName() << " "; |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 753 | |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 754 | } |
| 755 | llvm::dbgs() << "\n"; |
| 756 | }); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 757 | CurrentLines->push_back(*Line); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 758 | Line->Tokens.clear(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 759 | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { |
| 760 | for (std::vector<UnwrappedLine>::iterator I = PreprocessorDirectives |
| 761 | .begin(), E = PreprocessorDirectives.end(); |
| 762 | I != E; ++I) { |
| 763 | CurrentLines->push_back(*I); |
| 764 | } |
| 765 | PreprocessorDirectives.clear(); |
| 766 | } |
| 767 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | bool UnwrappedLineParser::eof() const { |
| 771 | return FormatTok.Tok.is(tok::eof); |
| 772 | } |
| 773 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 774 | void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { |
| 775 | bool JustComments = Line->Tokens.empty(); |
| 776 | for (SmallVectorImpl<FormatToken>::const_iterator |
| 777 | I = CommentsBeforeNextToken.begin(), |
| 778 | E = CommentsBeforeNextToken.end(); |
| 779 | I != E; ++I) { |
| 780 | if (I->HasUnescapedNewline && JustComments) { |
| 781 | addUnwrappedLine(); |
| 782 | } |
| 783 | pushToken(*I); |
| 784 | } |
| 785 | if (NewlineBeforeNext && JustComments) { |
| 786 | addUnwrappedLine(); |
| 787 | } |
| 788 | CommentsBeforeNextToken.clear(); |
| 789 | } |
| 790 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 791 | void UnwrappedLineParser::nextToken() { |
| 792 | if (eof()) |
| 793 | return; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 794 | flushComments(FormatTok.HasUnescapedNewline); |
| 795 | pushToken(FormatTok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 796 | readToken(); |
| 797 | } |
| 798 | |
| 799 | void UnwrappedLineParser::readToken() { |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 800 | bool CommentsInCurrentLine = true; |
| 801 | do { |
| 802 | FormatTok = Tokens->getNextToken(); |
| 803 | while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) && |
| 804 | ((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) || |
| 805 | FormatTok.IsFirst)) { |
| 806 | // If there is an unfinished unwrapped line, we flush the preprocessor |
| 807 | // directives only after that unwrapped line was finished later. |
| 808 | bool SwitchToPreprocessorLines = !Line->Tokens.empty() && |
| 809 | CurrentLines == &Lines; |
| 810 | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
| 811 | parsePPDirective(); |
| 812 | } |
| 813 | if (!FormatTok.Tok.is(tok::comment)) |
| 814 | return; |
| 815 | if (FormatTok.HasUnescapedNewline || FormatTok.IsFirst) { |
| 816 | CommentsInCurrentLine = false; |
| 817 | } |
| 818 | if (CommentsInCurrentLine) { |
| 819 | pushToken(FormatTok); |
| 820 | } else { |
| 821 | CommentsBeforeNextToken.push_back(FormatTok); |
| 822 | } |
| 823 | } while (!eof()); |
| 824 | } |
| 825 | |
| 826 | void UnwrappedLineParser::pushToken(const FormatToken &Tok) { |
| 827 | Line->Tokens.push_back(Tok); |
| 828 | if (MustBreakBeforeNextToken) { |
| 829 | Line->Tokens.back().MustBreakBefore = true; |
| 830 | MustBreakBeforeNextToken = false; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 831 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 834 | } // end namespace format |
| 835 | } // end namespace clang |