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