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