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