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 | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 77 | : Line(new UnwrappedLine), RootTokenInitialized(false), |
| 78 | LastInCurrentLine(NULL), MustBreakBeforeNextToken(false), Style(Style), |
| 79 | Tokens(&Tokens), Callback(Callback) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 82 | bool UnwrappedLineParser::parse() { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 83 | readToken(); |
| 84 | return parseFile(); |
| 85 | } |
| 86 | |
| 87 | bool UnwrappedLineParser::parseFile() { |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 88 | bool Error = parseLevel(/*HasOpeningBrace=*/false); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 89 | // Make sure to format the remaining tokens. |
| 90 | addUnwrappedLine(); |
| 91 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 94 | bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 95 | bool Error = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 96 | do { |
| 97 | switch (FormatTok.Tok.getKind()) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 98 | case tok::comment: |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 99 | nextToken(); |
| 100 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 101 | break; |
| 102 | case tok::l_brace: |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 103 | Error |= parseBlock(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 104 | addUnwrappedLine(); |
| 105 | break; |
| 106 | case tok::r_brace: |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 107 | if (HasOpeningBrace) { |
| 108 | return false; |
| 109 | } else { |
| 110 | // Stray '}' is an error. |
| 111 | Error = true; |
| 112 | nextToken(); |
| 113 | addUnwrappedLine(); |
| 114 | } |
| 115 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 116 | default: |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 117 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 118 | break; |
| 119 | } |
| 120 | } while (!eof()); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 121 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 124 | bool UnwrappedLineParser::parseBlock(unsigned AddLevels) { |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 125 | assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 126 | nextToken(); |
| 127 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 128 | addUnwrappedLine(); |
| 129 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 130 | Line->Level += AddLevels; |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 131 | parseLevel(/*HasOpeningBrace=*/true); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 132 | Line->Level -= AddLevels; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 133 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 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 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 137 | nextToken(); // Munch the closing brace. |
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 | 526ed11 | 2013-01-09 15:25:02 +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(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 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 | |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 198 | void UnwrappedLineParser::parseStructuralElement() { |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 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()) { |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 203 | case tok::at: |
| 204 | nextToken(); |
| 205 | switch (FormatTok.Tok.getObjCKeywordID()) { |
| 206 | case tok::objc_public: |
| 207 | case tok::objc_protected: |
| 208 | case tok::objc_package: |
| 209 | case tok::objc_private: |
| 210 | return parseAccessSpecifier(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 211 | case tok::objc_interface: |
| 212 | return parseObjCInterface(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 213 | case tok::objc_protocol: |
| 214 | return parseObjCProtocol(); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame^] | 215 | case tok::objc_end: |
| 216 | return; // Handled by the caller. |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 217 | default: |
| 218 | break; |
| 219 | } |
| 220 | break; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 221 | case tok::kw_namespace: |
| 222 | parseNamespace(); |
| 223 | return; |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 224 | case tok::kw_inline: |
| 225 | nextToken(); |
| 226 | TokenNumber++; |
| 227 | if (FormatTok.Tok.is(tok::kw_namespace)) { |
| 228 | parseNamespace(); |
| 229 | return; |
| 230 | } |
| 231 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 232 | case tok::kw_public: |
| 233 | case tok::kw_protected: |
| 234 | case tok::kw_private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 235 | parseAccessSpecifier(); |
| 236 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 237 | case tok::kw_if: |
| 238 | parseIfThenElse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 239 | return; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 240 | case tok::kw_for: |
| 241 | case tok::kw_while: |
| 242 | parseForOrWhileLoop(); |
| 243 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 244 | case tok::kw_do: |
| 245 | parseDoWhile(); |
| 246 | return; |
| 247 | case tok::kw_switch: |
| 248 | parseSwitch(); |
| 249 | return; |
| 250 | case tok::kw_default: |
| 251 | nextToken(); |
| 252 | parseLabel(); |
| 253 | return; |
| 254 | case tok::kw_case: |
| 255 | parseCaseLabel(); |
| 256 | return; |
| 257 | default: |
| 258 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 259 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 260 | do { |
| 261 | ++TokenNumber; |
| 262 | switch (FormatTok.Tok.getKind()) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 263 | case tok::kw_enum: |
| 264 | parseEnum(); |
| 265 | return; |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 266 | case tok::kw_struct: // fallthrough |
| 267 | case tok::kw_class: |
| 268 | parseStructOrClass(); |
| 269 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 270 | case tok::semi: |
| 271 | nextToken(); |
| 272 | addUnwrappedLine(); |
| 273 | return; |
| 274 | case tok::l_paren: |
| 275 | parseParens(); |
| 276 | break; |
| 277 | case tok::l_brace: |
| 278 | parseBlock(); |
| 279 | addUnwrappedLine(); |
| 280 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 281 | case tok::identifier: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 282 | nextToken(); |
| 283 | if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) { |
| 284 | parseLabel(); |
| 285 | return; |
| 286 | } |
| 287 | break; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 288 | case tok::equal: |
| 289 | nextToken(); |
| 290 | // Skip initializers as they will be formatted by a later step. |
| 291 | if (FormatTok.Tok.is(tok::l_brace)) |
| 292 | nextToken(); |
| 293 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 294 | default: |
| 295 | nextToken(); |
| 296 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 297 | } |
| 298 | } while (!eof()); |
| 299 | } |
| 300 | |
| 301 | void UnwrappedLineParser::parseParens() { |
| 302 | assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected."); |
| 303 | nextToken(); |
| 304 | do { |
| 305 | switch (FormatTok.Tok.getKind()) { |
| 306 | case tok::l_paren: |
| 307 | parseParens(); |
| 308 | break; |
| 309 | case tok::r_paren: |
| 310 | nextToken(); |
| 311 | return; |
| 312 | default: |
| 313 | nextToken(); |
| 314 | break; |
| 315 | } |
| 316 | } while (!eof()); |
| 317 | } |
| 318 | |
| 319 | void UnwrappedLineParser::parseIfThenElse() { |
| 320 | assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected"); |
| 321 | nextToken(); |
| 322 | parseParens(); |
| 323 | bool NeedsUnwrappedLine = false; |
| 324 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 325 | parseBlock(); |
| 326 | NeedsUnwrappedLine = true; |
| 327 | } else { |
| 328 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 329 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 330 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 331 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 332 | } |
| 333 | if (FormatTok.Tok.is(tok::kw_else)) { |
| 334 | nextToken(); |
| 335 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 336 | parseBlock(); |
| 337 | addUnwrappedLine(); |
| 338 | } else if (FormatTok.Tok.is(tok::kw_if)) { |
| 339 | parseIfThenElse(); |
| 340 | } else { |
| 341 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 342 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 343 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 344 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 345 | } |
| 346 | } else if (NeedsUnwrappedLine) { |
| 347 | addUnwrappedLine(); |
| 348 | } |
| 349 | } |
| 350 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 351 | void UnwrappedLineParser::parseNamespace() { |
| 352 | assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); |
| 353 | nextToken(); |
| 354 | if (FormatTok.Tok.is(tok::identifier)) |
| 355 | nextToken(); |
| 356 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 357 | parseBlock(0); |
| 358 | addUnwrappedLine(); |
| 359 | } |
| 360 | // FIXME: Add error handling. |
| 361 | } |
| 362 | |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 363 | void UnwrappedLineParser::parseForOrWhileLoop() { |
| 364 | assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && |
| 365 | "'for' or 'while' expected"); |
| 366 | nextToken(); |
| 367 | parseParens(); |
| 368 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 369 | parseBlock(); |
| 370 | addUnwrappedLine(); |
| 371 | } else { |
| 372 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 373 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 374 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 375 | --Line->Level; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 379 | void UnwrappedLineParser::parseDoWhile() { |
| 380 | assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected"); |
| 381 | nextToken(); |
| 382 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 383 | parseBlock(); |
| 384 | } else { |
| 385 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 386 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 387 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 388 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 391 | // FIXME: Add error handling. |
| 392 | if (!FormatTok.Tok.is(tok::kw_while)) { |
| 393 | addUnwrappedLine(); |
| 394 | return; |
| 395 | } |
| 396 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 397 | nextToken(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 398 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void UnwrappedLineParser::parseLabel() { |
| 402 | // FIXME: remove all asserts. |
| 403 | assert(FormatTok.Tok.is(tok::colon) && "':' expected"); |
| 404 | nextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 405 | unsigned OldLineLevel = Line->Level; |
| 406 | if (Line->Level > 0) |
| 407 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 408 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 409 | parseBlock(); |
| 410 | } |
| 411 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 412 | Line->Level = OldLineLevel; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void UnwrappedLineParser::parseCaseLabel() { |
| 416 | assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected"); |
| 417 | // FIXME: fix handling of complex expressions here. |
| 418 | do { |
| 419 | nextToken(); |
| 420 | } while (!eof() && !FormatTok.Tok.is(tok::colon)); |
| 421 | parseLabel(); |
| 422 | } |
| 423 | |
| 424 | void UnwrappedLineParser::parseSwitch() { |
| 425 | assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected"); |
| 426 | nextToken(); |
| 427 | parseParens(); |
| 428 | if (FormatTok.Tok.is(tok::l_brace)) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 429 | parseBlock(Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 430 | addUnwrappedLine(); |
| 431 | } else { |
| 432 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 433 | Line->Level += (Style.IndentCaseLabels ? 2 : 1); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 434 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 435 | Line->Level -= (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | |
| 439 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 440 | nextToken(); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 441 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
| 442 | if (FormatTok.Tok.is(tok::colon)) |
| 443 | nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 444 | addUnwrappedLine(); |
| 445 | } |
| 446 | |
| 447 | void UnwrappedLineParser::parseEnum() { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 448 | bool HasContents = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 449 | do { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 450 | switch (FormatTok.Tok.getKind()) { |
| 451 | case tok::l_brace: |
| 452 | nextToken(); |
| 453 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 454 | ++Line->Level; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 455 | parseComments(); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 456 | break; |
| 457 | case tok::l_paren: |
| 458 | parseParens(); |
| 459 | break; |
| 460 | case tok::comma: |
| 461 | nextToken(); |
| 462 | addUnwrappedLine(); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 463 | parseComments(); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 464 | break; |
| 465 | case tok::r_brace: |
| 466 | if (HasContents) |
| 467 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 468 | --Line->Level; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 469 | nextToken(); |
| 470 | break; |
| 471 | case tok::semi: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 472 | nextToken(); |
| 473 | addUnwrappedLine(); |
| 474 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 475 | default: |
| 476 | HasContents = true; |
| 477 | nextToken(); |
| 478 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 479 | } |
| 480 | } while (!eof()); |
| 481 | } |
| 482 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 483 | void UnwrappedLineParser::parseStructOrClass() { |
| 484 | nextToken(); |
| 485 | do { |
| 486 | switch (FormatTok.Tok.getKind()) { |
| 487 | case tok::l_brace: |
| 488 | // FIXME: Think about how to resolve the error handling here. |
| 489 | parseBlock(); |
| 490 | parseStructuralElement(); |
| 491 | return; |
| 492 | case tok::semi: |
| 493 | nextToken(); |
| 494 | addUnwrappedLine(); |
| 495 | return; |
| 496 | default: |
| 497 | nextToken(); |
| 498 | break; |
| 499 | } |
| 500 | } while (!eof()); |
| 501 | } |
| 502 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 503 | void UnwrappedLineParser::parseObjCProtocolList() { |
| 504 | assert(FormatTok.Tok.is(tok::less) && "'<' expected."); |
| 505 | do |
| 506 | nextToken(); |
| 507 | while (!eof() && FormatTok.Tok.isNot(tok::greater)); |
| 508 | nextToken(); // Skip '>'. |
| 509 | } |
| 510 | |
| 511 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 512 | do { |
| 513 | if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) { |
| 514 | nextToken(); |
| 515 | addUnwrappedLine(); |
| 516 | break; |
| 517 | } |
| 518 | parseStructuralElement(); |
| 519 | } while (!eof()); |
| 520 | } |
| 521 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 522 | void UnwrappedLineParser::parseObjCInterface() { |
| 523 | nextToken(); |
| 524 | nextToken(); // interface name |
| 525 | |
| 526 | // @interface can be followed by either a base class, or a category. |
| 527 | if (FormatTok.Tok.is(tok::colon)) { |
| 528 | nextToken(); |
| 529 | nextToken(); // base class name |
| 530 | } else if (FormatTok.Tok.is(tok::l_paren)) |
| 531 | // Skip category, if present. |
| 532 | parseParens(); |
| 533 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 534 | if (FormatTok.Tok.is(tok::less)) |
| 535 | parseObjCProtocolList(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 536 | |
| 537 | // If instance variables are present, keep the '{' on the first line too. |
| 538 | if (FormatTok.Tok.is(tok::l_brace)) |
| 539 | parseBlock(); |
| 540 | |
| 541 | // With instance variables, this puts '}' on its own line. Without instance |
| 542 | // variables, this ends the @interface line. |
| 543 | addUnwrappedLine(); |
| 544 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 545 | parseObjCUntilAtEnd(); |
| 546 | } |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 547 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 548 | void UnwrappedLineParser::parseObjCProtocol() { |
| 549 | nextToken(); |
| 550 | nextToken(); // protocol name |
| 551 | |
| 552 | if (FormatTok.Tok.is(tok::less)) |
| 553 | parseObjCProtocolList(); |
| 554 | |
| 555 | // Check for protocol declaration. |
| 556 | if (FormatTok.Tok.is(tok::semi)) { |
| 557 | nextToken(); |
| 558 | return addUnwrappedLine(); |
| 559 | } |
| 560 | |
| 561 | addUnwrappedLine(); |
| 562 | parseObjCUntilAtEnd(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 565 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 566 | if (!RootTokenInitialized) |
| 567 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 568 | // Consume trailing comments. |
| 569 | while (!eof() && FormatTok.NewlinesBefore == 0 && |
| 570 | FormatTok.Tok.is(tok::comment)) { |
| 571 | nextToken(); |
| 572 | } |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 573 | Callback.consumeUnwrappedLine(*Line); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 574 | RootTokenInitialized = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 575 | LastInCurrentLine = NULL; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | bool UnwrappedLineParser::eof() const { |
| 579 | return FormatTok.Tok.is(tok::eof); |
| 580 | } |
| 581 | |
| 582 | void UnwrappedLineParser::nextToken() { |
| 583 | if (eof()) |
| 584 | return; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 585 | if (RootTokenInitialized) { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 586 | assert(LastInCurrentLine->Children.empty()); |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 587 | LastInCurrentLine->Children.push_back(FormatTok); |
| 588 | LastInCurrentLine = &LastInCurrentLine->Children.back(); |
| 589 | } else { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 590 | Line->RootToken = FormatTok; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 591 | RootTokenInitialized = true; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 592 | LastInCurrentLine = &Line->RootToken; |
| 593 | } |
| 594 | if (MustBreakBeforeNextToken) { |
| 595 | LastInCurrentLine->MustBreakBefore = true; |
| 596 | MustBreakBeforeNextToken = false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 597 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 598 | readToken(); |
| 599 | } |
| 600 | |
| 601 | void UnwrappedLineParser::readToken() { |
| 602 | FormatTok = Tokens->getNextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 603 | while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) && |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 604 | ((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) || |
| 605 | FormatTok.IsFirst)) { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 606 | UnwrappedLine* StoredLine = Line.take(); |
| 607 | Line.reset(new UnwrappedLine(*StoredLine)); |
| 608 | assert(LastInCurrentLine == NULL || LastInCurrentLine->Children.empty()); |
| 609 | FormatToken *StoredLastInCurrentLine = LastInCurrentLine; |
| 610 | bool PreviousInitialized = RootTokenInitialized; |
| 611 | RootTokenInitialized = false; |
| 612 | LastInCurrentLine = NULL; |
| 613 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 614 | parsePPDirective(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 615 | |
| 616 | assert(!RootTokenInitialized); |
| 617 | Line.reset(StoredLine); |
| 618 | RootTokenInitialized = PreviousInitialized; |
| 619 | LastInCurrentLine = StoredLastInCurrentLine; |
| 620 | assert(LastInCurrentLine == NULL || LastInCurrentLine->Children.empty()); |
| 621 | MustBreakBeforeNextToken = true; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 622 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 625 | } // end namespace format |
| 626 | } // end namespace clang |