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 | |
Manuel Klimek | 4c60fc6 | 2013-01-10 10:05:08 +0000 | [diff] [blame^] | 22 | // Uncomment to get debug output from the UnwrappedLineParser. |
| 23 | // Use in combination with --gtest_filter=*TestName* to limit the output to a |
| 24 | // single test. |
| 25 | // #define UNWRAPPED_LINE_PARSER_DEBUG_OUTPUT |
| 26 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 27 | namespace clang { |
| 28 | namespace format { |
| 29 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 30 | class ScopedMacroState : public FormatTokenSource { |
| 31 | public: |
| 32 | ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, |
| 33 | FormatToken &ResetToken) |
| 34 | : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 35 | PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 36 | TokenSource = this; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 37 | Line.Level = 0; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 38 | Line.InPPDirective = true; |
| 39 | } |
| 40 | |
| 41 | ~ScopedMacroState() { |
| 42 | TokenSource = PreviousTokenSource; |
| 43 | ResetToken = Token; |
| 44 | Line.InPPDirective = false; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 45 | Line.Level = PreviousLineLevel; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | virtual FormatToken getNextToken() { |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 49 | // The \c UnwrappedLineParser guards against this by never calling |
| 50 | // \c getNextToken() after it has encountered the first eof token. |
| 51 | assert(!eof()); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 52 | Token = PreviousTokenSource->getNextToken(); |
| 53 | if (eof()) |
| 54 | return createEOF(); |
| 55 | return Token; |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | bool eof() { |
| 60 | return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline; |
| 61 | } |
| 62 | |
| 63 | FormatToken createEOF() { |
| 64 | FormatToken FormatTok; |
| 65 | FormatTok.Tok.startToken(); |
| 66 | FormatTok.Tok.setKind(tok::eof); |
| 67 | return FormatTok; |
| 68 | } |
| 69 | |
| 70 | UnwrappedLine &Line; |
| 71 | FormatTokenSource *&TokenSource; |
| 72 | FormatToken &ResetToken; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 73 | unsigned PreviousLineLevel; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 74 | FormatTokenSource *PreviousTokenSource; |
| 75 | |
| 76 | FormatToken Token; |
| 77 | }; |
| 78 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 79 | UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, |
| 80 | FormatTokenSource &Tokens, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 81 | UnwrappedLineConsumer &Callback) |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 82 | : Line(new UnwrappedLine), RootTokenInitialized(false), |
| 83 | LastInCurrentLine(NULL), MustBreakBeforeNextToken(false), Style(Style), |
| 84 | Tokens(&Tokens), Callback(Callback) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 87 | bool UnwrappedLineParser::parse() { |
Manuel Klimek | 4c60fc6 | 2013-01-10 10:05:08 +0000 | [diff] [blame^] | 88 | #ifdef UNWRAPPED_LINE_PARSER_DEBUG_OUTPUT |
| 89 | llvm::errs() << "----\n"; |
| 90 | #endif |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 91 | readToken(); |
| 92 | return parseFile(); |
| 93 | } |
| 94 | |
| 95 | bool UnwrappedLineParser::parseFile() { |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 96 | bool Error = parseLevel(/*HasOpeningBrace=*/false); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 97 | // Make sure to format the remaining tokens. |
| 98 | addUnwrappedLine(); |
| 99 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 102 | bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 103 | bool Error = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 104 | do { |
| 105 | switch (FormatTok.Tok.getKind()) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 106 | case tok::comment: |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 107 | nextToken(); |
| 108 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 109 | break; |
| 110 | case tok::l_brace: |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 111 | Error |= parseBlock(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 112 | addUnwrappedLine(); |
| 113 | break; |
| 114 | case tok::r_brace: |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 115 | if (HasOpeningBrace) { |
| 116 | return false; |
| 117 | } else { |
| 118 | // Stray '}' is an error. |
| 119 | Error = true; |
| 120 | nextToken(); |
| 121 | addUnwrappedLine(); |
| 122 | } |
| 123 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 124 | default: |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 125 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 126 | break; |
| 127 | } |
| 128 | } while (!eof()); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 129 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 132 | bool UnwrappedLineParser::parseBlock(unsigned AddLevels) { |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 133 | assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 134 | nextToken(); |
| 135 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 136 | addUnwrappedLine(); |
| 137 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 138 | Line->Level += AddLevels; |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 139 | parseLevel(/*HasOpeningBrace=*/true); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 140 | Line->Level -= AddLevels; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 141 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 142 | if (!FormatTok.Tok.is(tok::r_brace)) |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 143 | return true; |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 144 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 145 | nextToken(); // Munch the closing brace. |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 146 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 150 | assert(FormatTok.Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 151 | ScopedMacroState MacroState(*Line, Tokens, FormatTok); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 152 | nextToken(); |
| 153 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 154 | if (FormatTok.Tok.getIdentifierInfo() == NULL) { |
| 155 | addUnwrappedLine(); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 156 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 157 | } |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 158 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 159 | switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) { |
| 160 | case tok::pp_define: |
| 161 | parsePPDefine(); |
| 162 | break; |
| 163 | default: |
| 164 | parsePPUnknown(); |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void UnwrappedLineParser::parsePPDefine() { |
| 170 | nextToken(); |
| 171 | |
| 172 | if (FormatTok.Tok.getKind() != tok::identifier) { |
| 173 | parsePPUnknown(); |
| 174 | return; |
| 175 | } |
| 176 | nextToken(); |
| 177 | if (FormatTok.Tok.getKind() == tok::l_paren) { |
| 178 | parseParens(); |
| 179 | } |
| 180 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 181 | Line->Level = 1; |
Manuel Klimek | c3d0c82 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 182 | |
| 183 | // Errors during a preprocessor directive can only affect the layout of the |
| 184 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 185 | // would be to use the same approach we use on the file level (no |
| 186 | // re-indentation if there was a structural error) within the macro |
| 187 | // definition. |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 188 | parseFile(); |
| 189 | } |
| 190 | |
| 191 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 192 | do { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 193 | nextToken(); |
| 194 | } while (!eof()); |
| 195 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 198 | void UnwrappedLineParser::parseComments() { |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 199 | // Consume leading line comments, e.g. for branches without compounds. |
| 200 | while (FormatTok.Tok.is(tok::comment)) { |
| 201 | nextToken(); |
| 202 | addUnwrappedLine(); |
| 203 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 206 | void UnwrappedLineParser::parseStructuralElement() { |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 207 | parseComments(); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 208 | |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 209 | int TokenNumber = 0; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 210 | switch (FormatTok.Tok.getKind()) { |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 211 | case tok::at: |
| 212 | nextToken(); |
| 213 | switch (FormatTok.Tok.getObjCKeywordID()) { |
| 214 | case tok::objc_public: |
| 215 | case tok::objc_protected: |
| 216 | case tok::objc_package: |
| 217 | case tok::objc_private: |
| 218 | return parseAccessSpecifier(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 219 | case tok::objc_interface: |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 220 | case tok::objc_implementation: |
| 221 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 222 | case tok::objc_protocol: |
| 223 | return parseObjCProtocol(); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 224 | case tok::objc_end: |
| 225 | return; // Handled by the caller. |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 226 | case tok::objc_optional: |
| 227 | case tok::objc_required: |
| 228 | nextToken(); |
| 229 | addUnwrappedLine(); |
| 230 | return; |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 231 | default: |
| 232 | break; |
| 233 | } |
| 234 | break; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 235 | case tok::kw_namespace: |
| 236 | parseNamespace(); |
| 237 | return; |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 238 | case tok::kw_inline: |
| 239 | nextToken(); |
| 240 | TokenNumber++; |
| 241 | if (FormatTok.Tok.is(tok::kw_namespace)) { |
| 242 | parseNamespace(); |
| 243 | return; |
| 244 | } |
| 245 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 246 | case tok::kw_public: |
| 247 | case tok::kw_protected: |
| 248 | case tok::kw_private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 249 | parseAccessSpecifier(); |
| 250 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 251 | case tok::kw_if: |
| 252 | parseIfThenElse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 253 | return; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 254 | case tok::kw_for: |
| 255 | case tok::kw_while: |
| 256 | parseForOrWhileLoop(); |
| 257 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 258 | case tok::kw_do: |
| 259 | parseDoWhile(); |
| 260 | return; |
| 261 | case tok::kw_switch: |
| 262 | parseSwitch(); |
| 263 | return; |
| 264 | case tok::kw_default: |
| 265 | nextToken(); |
| 266 | parseLabel(); |
| 267 | return; |
| 268 | case tok::kw_case: |
| 269 | parseCaseLabel(); |
| 270 | return; |
| 271 | default: |
| 272 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 273 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 274 | do { |
| 275 | ++TokenNumber; |
| 276 | switch (FormatTok.Tok.getKind()) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 277 | case tok::kw_enum: |
| 278 | parseEnum(); |
| 279 | return; |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 280 | case tok::kw_struct: // fallthrough |
| 281 | case tok::kw_class: |
| 282 | parseStructOrClass(); |
| 283 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 284 | case tok::semi: |
| 285 | nextToken(); |
| 286 | addUnwrappedLine(); |
| 287 | return; |
| 288 | case tok::l_paren: |
| 289 | parseParens(); |
| 290 | break; |
| 291 | case tok::l_brace: |
| 292 | parseBlock(); |
| 293 | addUnwrappedLine(); |
| 294 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 295 | case tok::identifier: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 296 | nextToken(); |
| 297 | if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) { |
| 298 | parseLabel(); |
| 299 | return; |
| 300 | } |
| 301 | break; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 302 | case tok::equal: |
| 303 | nextToken(); |
| 304 | // Skip initializers as they will be formatted by a later step. |
| 305 | if (FormatTok.Tok.is(tok::l_brace)) |
| 306 | nextToken(); |
| 307 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 308 | default: |
| 309 | nextToken(); |
| 310 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 311 | } |
| 312 | } while (!eof()); |
| 313 | } |
| 314 | |
| 315 | void UnwrappedLineParser::parseParens() { |
| 316 | assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected."); |
| 317 | nextToken(); |
| 318 | do { |
| 319 | switch (FormatTok.Tok.getKind()) { |
| 320 | case tok::l_paren: |
| 321 | parseParens(); |
| 322 | break; |
| 323 | case tok::r_paren: |
| 324 | nextToken(); |
| 325 | return; |
| 326 | default: |
| 327 | nextToken(); |
| 328 | break; |
| 329 | } |
| 330 | } while (!eof()); |
| 331 | } |
| 332 | |
| 333 | void UnwrappedLineParser::parseIfThenElse() { |
| 334 | assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected"); |
| 335 | nextToken(); |
| 336 | parseParens(); |
| 337 | bool NeedsUnwrappedLine = false; |
| 338 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 339 | parseBlock(); |
| 340 | NeedsUnwrappedLine = true; |
| 341 | } else { |
| 342 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 343 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 344 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 345 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 346 | } |
| 347 | if (FormatTok.Tok.is(tok::kw_else)) { |
| 348 | nextToken(); |
| 349 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 350 | parseBlock(); |
| 351 | addUnwrappedLine(); |
| 352 | } else if (FormatTok.Tok.is(tok::kw_if)) { |
| 353 | parseIfThenElse(); |
| 354 | } else { |
| 355 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 356 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 357 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 358 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 359 | } |
| 360 | } else if (NeedsUnwrappedLine) { |
| 361 | addUnwrappedLine(); |
| 362 | } |
| 363 | } |
| 364 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 365 | void UnwrappedLineParser::parseNamespace() { |
| 366 | assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); |
| 367 | nextToken(); |
| 368 | if (FormatTok.Tok.is(tok::identifier)) |
| 369 | nextToken(); |
| 370 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 371 | parseBlock(0); |
| 372 | addUnwrappedLine(); |
| 373 | } |
| 374 | // FIXME: Add error handling. |
| 375 | } |
| 376 | |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 377 | void UnwrappedLineParser::parseForOrWhileLoop() { |
| 378 | assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && |
| 379 | "'for' or 'while' expected"); |
| 380 | nextToken(); |
| 381 | parseParens(); |
| 382 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 383 | parseBlock(); |
| 384 | addUnwrappedLine(); |
| 385 | } else { |
| 386 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 387 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 388 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 389 | --Line->Level; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 393 | void UnwrappedLineParser::parseDoWhile() { |
| 394 | assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected"); |
| 395 | nextToken(); |
| 396 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 397 | parseBlock(); |
| 398 | } else { |
| 399 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 400 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 401 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 402 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 405 | // FIXME: Add error handling. |
| 406 | if (!FormatTok.Tok.is(tok::kw_while)) { |
| 407 | addUnwrappedLine(); |
| 408 | return; |
| 409 | } |
| 410 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 411 | nextToken(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 412 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void UnwrappedLineParser::parseLabel() { |
| 416 | // FIXME: remove all asserts. |
| 417 | assert(FormatTok.Tok.is(tok::colon) && "':' expected"); |
| 418 | nextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 419 | unsigned OldLineLevel = Line->Level; |
| 420 | if (Line->Level > 0) |
| 421 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 422 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 423 | parseBlock(); |
| 424 | } |
| 425 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 426 | Line->Level = OldLineLevel; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | void UnwrappedLineParser::parseCaseLabel() { |
| 430 | assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected"); |
| 431 | // FIXME: fix handling of complex expressions here. |
| 432 | do { |
| 433 | nextToken(); |
| 434 | } while (!eof() && !FormatTok.Tok.is(tok::colon)); |
| 435 | parseLabel(); |
| 436 | } |
| 437 | |
| 438 | void UnwrappedLineParser::parseSwitch() { |
| 439 | assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected"); |
| 440 | nextToken(); |
| 441 | parseParens(); |
| 442 | if (FormatTok.Tok.is(tok::l_brace)) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 443 | parseBlock(Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 444 | addUnwrappedLine(); |
| 445 | } else { |
| 446 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 447 | Line->Level += (Style.IndentCaseLabels ? 2 : 1); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 448 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 449 | Line->Level -= (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 454 | nextToken(); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 455 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
| 456 | if (FormatTok.Tok.is(tok::colon)) |
| 457 | nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 458 | addUnwrappedLine(); |
| 459 | } |
| 460 | |
| 461 | void UnwrappedLineParser::parseEnum() { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 462 | bool HasContents = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 463 | do { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 464 | switch (FormatTok.Tok.getKind()) { |
| 465 | case tok::l_brace: |
| 466 | nextToken(); |
| 467 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 468 | ++Line->Level; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 469 | parseComments(); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 470 | break; |
| 471 | case tok::l_paren: |
| 472 | parseParens(); |
| 473 | break; |
| 474 | case tok::comma: |
| 475 | nextToken(); |
| 476 | addUnwrappedLine(); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 477 | parseComments(); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 478 | break; |
| 479 | case tok::r_brace: |
| 480 | if (HasContents) |
| 481 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 482 | --Line->Level; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 483 | nextToken(); |
| 484 | break; |
| 485 | case tok::semi: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 486 | nextToken(); |
| 487 | addUnwrappedLine(); |
| 488 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 489 | default: |
| 490 | HasContents = true; |
| 491 | nextToken(); |
| 492 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 493 | } |
| 494 | } while (!eof()); |
| 495 | } |
| 496 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 497 | void UnwrappedLineParser::parseStructOrClass() { |
| 498 | nextToken(); |
| 499 | do { |
| 500 | switch (FormatTok.Tok.getKind()) { |
| 501 | case tok::l_brace: |
| 502 | // FIXME: Think about how to resolve the error handling here. |
| 503 | parseBlock(); |
| 504 | parseStructuralElement(); |
| 505 | return; |
| 506 | case tok::semi: |
| 507 | nextToken(); |
| 508 | addUnwrappedLine(); |
| 509 | return; |
| 510 | default: |
| 511 | nextToken(); |
| 512 | break; |
| 513 | } |
| 514 | } while (!eof()); |
| 515 | } |
| 516 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 517 | void UnwrappedLineParser::parseObjCProtocolList() { |
| 518 | assert(FormatTok.Tok.is(tok::less) && "'<' expected."); |
| 519 | do |
| 520 | nextToken(); |
| 521 | while (!eof() && FormatTok.Tok.isNot(tok::greater)); |
| 522 | nextToken(); // Skip '>'. |
| 523 | } |
| 524 | |
| 525 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 526 | do { |
| 527 | if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) { |
| 528 | nextToken(); |
| 529 | addUnwrappedLine(); |
| 530 | break; |
| 531 | } |
| 532 | parseStructuralElement(); |
| 533 | } while (!eof()); |
| 534 | } |
| 535 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 536 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 537 | nextToken(); |
| 538 | nextToken(); // interface name |
| 539 | |
| 540 | // @interface can be followed by either a base class, or a category. |
| 541 | if (FormatTok.Tok.is(tok::colon)) { |
| 542 | nextToken(); |
| 543 | nextToken(); // base class name |
| 544 | } else if (FormatTok.Tok.is(tok::l_paren)) |
| 545 | // Skip category, if present. |
| 546 | parseParens(); |
| 547 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 548 | if (FormatTok.Tok.is(tok::less)) |
| 549 | parseObjCProtocolList(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 550 | |
| 551 | // If instance variables are present, keep the '{' on the first line too. |
| 552 | if (FormatTok.Tok.is(tok::l_brace)) |
| 553 | parseBlock(); |
| 554 | |
| 555 | // With instance variables, this puts '}' on its own line. Without instance |
| 556 | // variables, this ends the @interface line. |
| 557 | addUnwrappedLine(); |
| 558 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 559 | parseObjCUntilAtEnd(); |
| 560 | } |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 561 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 562 | void UnwrappedLineParser::parseObjCProtocol() { |
| 563 | nextToken(); |
| 564 | nextToken(); // protocol name |
| 565 | |
| 566 | if (FormatTok.Tok.is(tok::less)) |
| 567 | parseObjCProtocolList(); |
| 568 | |
| 569 | // Check for protocol declaration. |
| 570 | if (FormatTok.Tok.is(tok::semi)) { |
| 571 | nextToken(); |
| 572 | return addUnwrappedLine(); |
| 573 | } |
| 574 | |
| 575 | addUnwrappedLine(); |
| 576 | parseObjCUntilAtEnd(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 579 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 580 | if (!RootTokenInitialized) |
| 581 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 582 | // Consume trailing comments. |
| 583 | while (!eof() && FormatTok.NewlinesBefore == 0 && |
| 584 | FormatTok.Tok.is(tok::comment)) { |
| 585 | nextToken(); |
| 586 | } |
Manuel Klimek | 4c60fc6 | 2013-01-10 10:05:08 +0000 | [diff] [blame^] | 587 | #ifdef UNWRAPPED_LINE_PARSER_DEBUG_OUTPUT |
| 588 | FormatToken* NextToken = &Line->RootToken; |
| 589 | llvm::errs() << "Line: "; |
| 590 | while (NextToken) { |
| 591 | llvm::errs() << NextToken->Tok.getName() << " "; |
| 592 | NextToken = NextToken->Children.empty() ? NULL : &NextToken->Children[0]; |
| 593 | } |
| 594 | llvm::errs() << "\n"; |
| 595 | #endif |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 596 | Callback.consumeUnwrappedLine(*Line); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 597 | RootTokenInitialized = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 598 | LastInCurrentLine = NULL; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | bool UnwrappedLineParser::eof() const { |
| 602 | return FormatTok.Tok.is(tok::eof); |
| 603 | } |
| 604 | |
| 605 | void UnwrappedLineParser::nextToken() { |
| 606 | if (eof()) |
| 607 | return; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 608 | if (RootTokenInitialized) { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 609 | assert(LastInCurrentLine->Children.empty()); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 610 | LastInCurrentLine->Children.push_back(FormatTok); |
| 611 | LastInCurrentLine = &LastInCurrentLine->Children.back(); |
| 612 | } else { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 613 | Line->RootToken = FormatTok; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 614 | RootTokenInitialized = true; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 615 | LastInCurrentLine = &Line->RootToken; |
| 616 | } |
| 617 | if (MustBreakBeforeNextToken) { |
| 618 | LastInCurrentLine->MustBreakBefore = true; |
| 619 | MustBreakBeforeNextToken = false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 620 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 621 | readToken(); |
| 622 | } |
| 623 | |
| 624 | void UnwrappedLineParser::readToken() { |
| 625 | FormatTok = Tokens->getNextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 626 | while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) && |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 627 | ((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) || |
| 628 | FormatTok.IsFirst)) { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 629 | UnwrappedLine* StoredLine = Line.take(); |
| 630 | Line.reset(new UnwrappedLine(*StoredLine)); |
| 631 | assert(LastInCurrentLine == NULL || LastInCurrentLine->Children.empty()); |
| 632 | FormatToken *StoredLastInCurrentLine = LastInCurrentLine; |
| 633 | bool PreviousInitialized = RootTokenInitialized; |
| 634 | RootTokenInitialized = false; |
| 635 | LastInCurrentLine = NULL; |
| 636 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 637 | parsePPDirective(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 638 | |
| 639 | assert(!RootTokenInitialized); |
| 640 | Line.reset(StoredLine); |
| 641 | RootTokenInitialized = PreviousInitialized; |
| 642 | LastInCurrentLine = StoredLastInCurrentLine; |
| 643 | assert(LastInCurrentLine == NULL || LastInCurrentLine->Children.empty()); |
| 644 | MustBreakBeforeNextToken = true; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 645 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 648 | } // end namespace format |
| 649 | } // end namespace clang |