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