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 | |
| 19 | #include "UnwrappedLineParser.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | |
| 22 | namespace clang { |
| 23 | namespace format { |
| 24 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 25 | class ScopedMacroState : public FormatTokenSource { |
| 26 | public: |
| 27 | ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, |
| 28 | FormatToken &ResetToken) |
| 29 | : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), |
| 30 | PreviousTokenSource(TokenSource) { |
| 31 | TokenSource = this; |
| 32 | // FIXME: Back up all other state (errors, line indent, etc) and reset after |
| 33 | // parsing the macro. |
| 34 | Line.InPPDirective = true; |
| 35 | } |
| 36 | |
| 37 | ~ScopedMacroState() { |
| 38 | TokenSource = PreviousTokenSource; |
| 39 | ResetToken = Token; |
| 40 | Line.InPPDirective = false; |
| 41 | Line.Level = 0; // FIXME: Test + this is obviously incorrect |
| 42 | } |
| 43 | |
| 44 | virtual FormatToken getNextToken() { |
| 45 | // FIXME: Write test that breaks due to a missing |
| 46 | // if (eof()) return createEOF(); |
| 47 | Token = PreviousTokenSource->getNextToken(); |
| 48 | if (eof()) |
| 49 | return createEOF(); |
| 50 | return Token; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | bool eof() { |
| 55 | return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline; |
| 56 | } |
| 57 | |
| 58 | FormatToken createEOF() { |
| 59 | FormatToken FormatTok; |
| 60 | FormatTok.Tok.startToken(); |
| 61 | FormatTok.Tok.setKind(tok::eof); |
| 62 | return FormatTok; |
| 63 | } |
| 64 | |
| 65 | UnwrappedLine &Line; |
| 66 | FormatTokenSource *&TokenSource; |
| 67 | FormatToken &ResetToken; |
| 68 | |
| 69 | FormatTokenSource *PreviousTokenSource; |
| 70 | |
| 71 | FormatToken Token; |
| 72 | }; |
| 73 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 74 | UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, |
| 75 | FormatTokenSource &Tokens, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 76 | UnwrappedLineConsumer &Callback) |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 77 | : Style(Style), Tokens(&Tokens), Callback(Callback) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 80 | bool UnwrappedLineParser::parse() { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 81 | readToken(); |
| 82 | return parseFile(); |
| 83 | } |
| 84 | |
| 85 | bool UnwrappedLineParser::parseFile() { |
| 86 | bool Error = parseLevel(); |
| 87 | // Make sure to format the remaining tokens. |
| 88 | addUnwrappedLine(); |
| 89 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 92 | bool UnwrappedLineParser::parseLevel() { |
| 93 | bool Error = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 94 | do { |
| 95 | switch (FormatTok.Tok.getKind()) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 96 | case tok::comment: |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 97 | nextToken(); |
| 98 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 99 | break; |
| 100 | case tok::l_brace: |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 101 | Error |= parseBlock(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 102 | addUnwrappedLine(); |
| 103 | break; |
| 104 | case tok::r_brace: |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 105 | // Stray '}' is an error. |
| 106 | return true; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 107 | default: |
| 108 | parseStatement(); |
| 109 | break; |
| 110 | } |
| 111 | } while (!eof()); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 112 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 115 | bool UnwrappedLineParser::parseBlock(unsigned AddLevels) { |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 116 | assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 117 | nextToken(); |
| 118 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 119 | addUnwrappedLine(); |
| 120 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 121 | Line.Level += AddLevels; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 122 | parseLevel(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 123 | Line.Level -= AddLevels; |
| 124 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 125 | // FIXME: Add error handling. |
| 126 | if (!FormatTok.Tok.is(tok::r_brace)) |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 127 | return true; |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 128 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 129 | nextToken(); |
| 130 | if (FormatTok.Tok.is(tok::semi)) |
| 131 | nextToken(); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 132 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 136 | assert(FormatTok.Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 137 | ScopedMacroState MacroState(Line, Tokens, FormatTok); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 138 | nextToken(); |
| 139 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 140 | if (FormatTok.Tok.getIdentifierInfo() == NULL) { |
| 141 | addUnwrappedLine(); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 142 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 143 | } |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 144 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 145 | switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) { |
| 146 | case tok::pp_define: |
| 147 | parsePPDefine(); |
| 148 | break; |
| 149 | default: |
| 150 | parsePPUnknown(); |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void UnwrappedLineParser::parsePPDefine() { |
| 156 | nextToken(); |
| 157 | |
| 158 | if (FormatTok.Tok.getKind() != tok::identifier) { |
| 159 | parsePPUnknown(); |
| 160 | return; |
| 161 | } |
| 162 | nextToken(); |
| 163 | if (FormatTok.Tok.getKind() == tok::l_paren) { |
| 164 | parseParens(); |
| 165 | } |
| 166 | addUnwrappedLine(); |
| 167 | Line.Level = 1; |
| 168 | parseFile(); |
| 169 | } |
| 170 | |
| 171 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 172 | do { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 173 | nextToken(); |
| 174 | } while (!eof()); |
| 175 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 178 | void UnwrappedLineParser::parseComments() { |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 179 | // Consume leading line comments, e.g. for branches without compounds. |
| 180 | while (FormatTok.Tok.is(tok::comment)) { |
| 181 | nextToken(); |
| 182 | addUnwrappedLine(); |
| 183 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void UnwrappedLineParser::parseStatement() { |
| 187 | parseComments(); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 188 | |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 189 | int TokenNumber = 0; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 190 | switch (FormatTok.Tok.getKind()) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 191 | case tok::kw_namespace: |
| 192 | parseNamespace(); |
| 193 | return; |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 194 | case tok::kw_inline: |
| 195 | nextToken(); |
| 196 | TokenNumber++; |
| 197 | if (FormatTok.Tok.is(tok::kw_namespace)) { |
| 198 | parseNamespace(); |
| 199 | return; |
| 200 | } |
| 201 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 202 | case tok::kw_public: |
| 203 | case tok::kw_protected: |
| 204 | case tok::kw_private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 205 | parseAccessSpecifier(); |
| 206 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 207 | case tok::kw_if: |
| 208 | parseIfThenElse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 209 | return; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 210 | case tok::kw_for: |
| 211 | case tok::kw_while: |
| 212 | parseForOrWhileLoop(); |
| 213 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 214 | case tok::kw_do: |
| 215 | parseDoWhile(); |
| 216 | return; |
| 217 | case tok::kw_switch: |
| 218 | parseSwitch(); |
| 219 | return; |
| 220 | case tok::kw_default: |
| 221 | nextToken(); |
| 222 | parseLabel(); |
| 223 | return; |
| 224 | case tok::kw_case: |
| 225 | parseCaseLabel(); |
| 226 | return; |
| 227 | default: |
| 228 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 229 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 230 | do { |
| 231 | ++TokenNumber; |
| 232 | switch (FormatTok.Tok.getKind()) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 233 | case tok::kw_enum: |
| 234 | parseEnum(); |
| 235 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 236 | case tok::semi: |
| 237 | nextToken(); |
| 238 | addUnwrappedLine(); |
| 239 | return; |
| 240 | case tok::l_paren: |
| 241 | parseParens(); |
| 242 | break; |
| 243 | case tok::l_brace: |
| 244 | parseBlock(); |
| 245 | addUnwrappedLine(); |
| 246 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 247 | case tok::identifier: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 248 | nextToken(); |
| 249 | if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) { |
| 250 | parseLabel(); |
| 251 | return; |
| 252 | } |
| 253 | break; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 254 | case tok::equal: |
| 255 | nextToken(); |
| 256 | // Skip initializers as they will be formatted by a later step. |
| 257 | if (FormatTok.Tok.is(tok::l_brace)) |
| 258 | nextToken(); |
| 259 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 260 | default: |
| 261 | nextToken(); |
| 262 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 263 | } |
| 264 | } while (!eof()); |
| 265 | } |
| 266 | |
| 267 | void UnwrappedLineParser::parseParens() { |
| 268 | assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected."); |
| 269 | nextToken(); |
| 270 | do { |
| 271 | switch (FormatTok.Tok.getKind()) { |
| 272 | case tok::l_paren: |
| 273 | parseParens(); |
| 274 | break; |
| 275 | case tok::r_paren: |
| 276 | nextToken(); |
| 277 | return; |
| 278 | default: |
| 279 | nextToken(); |
| 280 | break; |
| 281 | } |
| 282 | } while (!eof()); |
| 283 | } |
| 284 | |
| 285 | void UnwrappedLineParser::parseIfThenElse() { |
| 286 | assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected"); |
| 287 | nextToken(); |
| 288 | parseParens(); |
| 289 | bool NeedsUnwrappedLine = false; |
| 290 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 291 | parseBlock(); |
| 292 | NeedsUnwrappedLine = true; |
| 293 | } else { |
| 294 | addUnwrappedLine(); |
| 295 | ++Line.Level; |
| 296 | parseStatement(); |
| 297 | --Line.Level; |
| 298 | } |
| 299 | if (FormatTok.Tok.is(tok::kw_else)) { |
| 300 | nextToken(); |
| 301 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 302 | parseBlock(); |
| 303 | addUnwrappedLine(); |
| 304 | } else if (FormatTok.Tok.is(tok::kw_if)) { |
| 305 | parseIfThenElse(); |
| 306 | } else { |
| 307 | addUnwrappedLine(); |
| 308 | ++Line.Level; |
| 309 | parseStatement(); |
| 310 | --Line.Level; |
| 311 | } |
| 312 | } else if (NeedsUnwrappedLine) { |
| 313 | addUnwrappedLine(); |
| 314 | } |
| 315 | } |
| 316 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 317 | void UnwrappedLineParser::parseNamespace() { |
| 318 | assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); |
| 319 | nextToken(); |
| 320 | if (FormatTok.Tok.is(tok::identifier)) |
| 321 | nextToken(); |
| 322 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 323 | parseBlock(0); |
| 324 | addUnwrappedLine(); |
| 325 | } |
| 326 | // FIXME: Add error handling. |
| 327 | } |
| 328 | |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 329 | void UnwrappedLineParser::parseForOrWhileLoop() { |
| 330 | assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && |
| 331 | "'for' or 'while' expected"); |
| 332 | nextToken(); |
| 333 | parseParens(); |
| 334 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 335 | parseBlock(); |
| 336 | addUnwrappedLine(); |
| 337 | } else { |
| 338 | addUnwrappedLine(); |
| 339 | ++Line.Level; |
| 340 | parseStatement(); |
| 341 | --Line.Level; |
| 342 | } |
| 343 | } |
| 344 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 345 | void UnwrappedLineParser::parseDoWhile() { |
| 346 | assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected"); |
| 347 | nextToken(); |
| 348 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 349 | parseBlock(); |
| 350 | } else { |
| 351 | addUnwrappedLine(); |
| 352 | ++Line.Level; |
| 353 | parseStatement(); |
| 354 | --Line.Level; |
| 355 | } |
| 356 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 357 | // FIXME: Add error handling. |
| 358 | if (!FormatTok.Tok.is(tok::kw_while)) { |
| 359 | addUnwrappedLine(); |
| 360 | return; |
| 361 | } |
| 362 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 363 | nextToken(); |
| 364 | parseStatement(); |
| 365 | } |
| 366 | |
| 367 | void UnwrappedLineParser::parseLabel() { |
| 368 | // FIXME: remove all asserts. |
| 369 | assert(FormatTok.Tok.is(tok::colon) && "':' expected"); |
| 370 | nextToken(); |
| 371 | unsigned OldLineLevel = Line.Level; |
| 372 | if (Line.Level > 0) |
| 373 | --Line.Level; |
| 374 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 375 | parseBlock(); |
| 376 | } |
| 377 | addUnwrappedLine(); |
| 378 | Line.Level = OldLineLevel; |
| 379 | } |
| 380 | |
| 381 | void UnwrappedLineParser::parseCaseLabel() { |
| 382 | assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected"); |
| 383 | // FIXME: fix handling of complex expressions here. |
| 384 | do { |
| 385 | nextToken(); |
| 386 | } while (!eof() && !FormatTok.Tok.is(tok::colon)); |
| 387 | parseLabel(); |
| 388 | } |
| 389 | |
| 390 | void UnwrappedLineParser::parseSwitch() { |
| 391 | assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected"); |
| 392 | nextToken(); |
| 393 | parseParens(); |
| 394 | if (FormatTok.Tok.is(tok::l_brace)) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 395 | parseBlock(Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 396 | addUnwrappedLine(); |
| 397 | } else { |
| 398 | addUnwrappedLine(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 399 | Line.Level += (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 400 | parseStatement(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 401 | Line.Level -= (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 406 | nextToken(); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 407 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
| 408 | if (FormatTok.Tok.is(tok::colon)) |
| 409 | nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 410 | addUnwrappedLine(); |
| 411 | } |
| 412 | |
| 413 | void UnwrappedLineParser::parseEnum() { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 414 | bool HasContents = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 415 | do { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 416 | switch (FormatTok.Tok.getKind()) { |
| 417 | case tok::l_brace: |
| 418 | nextToken(); |
| 419 | addUnwrappedLine(); |
| 420 | ++Line.Level; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 421 | parseComments(); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 422 | break; |
| 423 | case tok::l_paren: |
| 424 | parseParens(); |
| 425 | break; |
| 426 | case tok::comma: |
| 427 | nextToken(); |
| 428 | addUnwrappedLine(); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 429 | parseComments(); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 430 | break; |
| 431 | case tok::r_brace: |
| 432 | if (HasContents) |
| 433 | addUnwrappedLine(); |
| 434 | --Line.Level; |
| 435 | nextToken(); |
| 436 | break; |
| 437 | case tok::semi: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 438 | nextToken(); |
| 439 | addUnwrappedLine(); |
| 440 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 441 | default: |
| 442 | HasContents = true; |
| 443 | nextToken(); |
| 444 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 445 | } |
| 446 | } while (!eof()); |
| 447 | } |
| 448 | |
| 449 | void UnwrappedLineParser::addUnwrappedLine() { |
| 450 | // Consume trailing comments. |
| 451 | while (!eof() && FormatTok.NewlinesBefore == 0 && |
| 452 | FormatTok.Tok.is(tok::comment)) { |
| 453 | nextToken(); |
| 454 | } |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 455 | Callback.consumeUnwrappedLine(Line); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 456 | Line.Tokens.clear(); |
| 457 | } |
| 458 | |
| 459 | bool UnwrappedLineParser::eof() const { |
| 460 | return FormatTok.Tok.is(tok::eof); |
| 461 | } |
| 462 | |
| 463 | void UnwrappedLineParser::nextToken() { |
| 464 | if (eof()) |
| 465 | return; |
| 466 | Line.Tokens.push_back(FormatTok); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 467 | readToken(); |
| 468 | } |
| 469 | |
| 470 | void UnwrappedLineParser::readToken() { |
| 471 | FormatTok = Tokens->getNextToken(); |
| 472 | while (FormatTok.Tok.is(tok::hash)) { |
| 473 | // FIXME: This is incorrect - the correct way is to create a |
| 474 | // data structure that will construct the parts around the preprocessor |
| 475 | // directive as a structured \c UnwrappedLine. |
| 476 | addUnwrappedLine(); |
| 477 | parsePPDirective(); |
| 478 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | } // end namespace format |
| 482 | } // end namespace clang |