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