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 | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame^] | 173 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 174 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame^] | 175 | Line->Level += AddLevels; |
| 176 | parseLevel(/*HasOpeningBrace=*/true); |
| 177 | Line->Level -= AddLevels; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 178 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame^] | 179 | if (!FormatTok.Tok.is(tok::r_brace)) |
| 180 | return true; |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 181 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 182 | nextToken(); // Munch the closing brace. |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 183 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 187 | assert(FormatTok.Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 188 | ScopedMacroState MacroState(*Line, Tokens, FormatTok); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 189 | nextToken(); |
| 190 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 191 | if (FormatTok.Tok.getIdentifierInfo() == NULL) { |
| 192 | addUnwrappedLine(); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 193 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 194 | } |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 195 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 196 | switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) { |
| 197 | case tok::pp_define: |
| 198 | parsePPDefine(); |
| 199 | break; |
| 200 | default: |
| 201 | parsePPUnknown(); |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void UnwrappedLineParser::parsePPDefine() { |
| 207 | nextToken(); |
| 208 | |
| 209 | if (FormatTok.Tok.getKind() != tok::identifier) { |
| 210 | parsePPUnknown(); |
| 211 | return; |
| 212 | } |
| 213 | nextToken(); |
| 214 | if (FormatTok.Tok.getKind() == tok::l_paren) { |
| 215 | parseParens(); |
| 216 | } |
| 217 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 218 | Line->Level = 1; |
Manuel Klimek | c3d0c82 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 219 | |
| 220 | // Errors during a preprocessor directive can only affect the layout of the |
| 221 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 222 | // would be to use the same approach we use on the file level (no |
| 223 | // re-indentation if there was a structural error) within the macro |
| 224 | // definition. |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 225 | parseFile(); |
| 226 | } |
| 227 | |
| 228 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 229 | do { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 230 | nextToken(); |
| 231 | } while (!eof()); |
| 232 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 235 | void UnwrappedLineParser::parseComments() { |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 236 | // Consume leading line comments, e.g. for branches without compounds. |
| 237 | while (FormatTok.Tok.is(tok::comment)) { |
| 238 | nextToken(); |
| 239 | addUnwrappedLine(); |
| 240 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 243 | void UnwrappedLineParser::parseStructuralElement() { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 244 | assert(!FormatTok.Tok.is(tok::l_brace)); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 245 | parseComments(); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 246 | |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 247 | int TokenNumber = 0; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 248 | switch (FormatTok.Tok.getKind()) { |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 249 | case tok::at: |
| 250 | nextToken(); |
| 251 | switch (FormatTok.Tok.getObjCKeywordID()) { |
| 252 | case tok::objc_public: |
| 253 | case tok::objc_protected: |
| 254 | case tok::objc_package: |
| 255 | case tok::objc_private: |
| 256 | return parseAccessSpecifier(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 257 | case tok::objc_interface: |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 258 | case tok::objc_implementation: |
| 259 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 260 | case tok::objc_protocol: |
| 261 | return parseObjCProtocol(); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 262 | case tok::objc_end: |
| 263 | return; // Handled by the caller. |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 264 | case tok::objc_optional: |
| 265 | case tok::objc_required: |
| 266 | nextToken(); |
| 267 | addUnwrappedLine(); |
| 268 | return; |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 269 | default: |
| 270 | break; |
| 271 | } |
| 272 | break; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 273 | case tok::kw_namespace: |
| 274 | parseNamespace(); |
| 275 | return; |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 276 | case tok::kw_inline: |
| 277 | nextToken(); |
| 278 | TokenNumber++; |
| 279 | if (FormatTok.Tok.is(tok::kw_namespace)) { |
| 280 | parseNamespace(); |
| 281 | return; |
| 282 | } |
| 283 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 284 | case tok::kw_public: |
| 285 | case tok::kw_protected: |
| 286 | case tok::kw_private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 287 | parseAccessSpecifier(); |
| 288 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 289 | case tok::kw_if: |
| 290 | parseIfThenElse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 291 | return; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 292 | case tok::kw_for: |
| 293 | case tok::kw_while: |
| 294 | parseForOrWhileLoop(); |
| 295 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 296 | case tok::kw_do: |
| 297 | parseDoWhile(); |
| 298 | return; |
| 299 | case tok::kw_switch: |
| 300 | parseSwitch(); |
| 301 | return; |
| 302 | case tok::kw_default: |
| 303 | nextToken(); |
| 304 | parseLabel(); |
| 305 | return; |
| 306 | case tok::kw_case: |
| 307 | parseCaseLabel(); |
| 308 | return; |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 309 | case tok::kw_return: |
| 310 | parseReturn(); |
| 311 | return; |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 312 | case tok::kw_extern: |
| 313 | nextToken(); |
| 314 | if (FormatTok.Tok.is(tok::string_literal)) { |
| 315 | nextToken(); |
| 316 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 317 | parseBlock(0); |
| 318 | addUnwrappedLine(); |
| 319 | return; |
| 320 | } |
| 321 | } |
| 322 | // In all other cases, parse the declaration. |
| 323 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 324 | default: |
| 325 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 326 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 327 | do { |
| 328 | ++TokenNumber; |
| 329 | switch (FormatTok.Tok.getKind()) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 330 | case tok::kw_enum: |
| 331 | parseEnum(); |
| 332 | return; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 333 | case tok::kw_struct: |
| 334 | case tok::kw_union: |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 335 | case tok::kw_class: |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 336 | parseRecord(); |
| 337 | // A record declaration or definition is always the start of a structural |
| 338 | // element. |
| 339 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 340 | case tok::semi: |
| 341 | nextToken(); |
| 342 | addUnwrappedLine(); |
| 343 | return; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 344 | case tok::r_brace: |
| 345 | addUnwrappedLine(); |
| 346 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 347 | case tok::l_paren: |
| 348 | parseParens(); |
| 349 | break; |
| 350 | case tok::l_brace: |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 351 | // A block outside of parentheses must be the last part of a |
| 352 | // structural element. |
| 353 | // FIXME: Figure out cases where this is not true, and add projections for |
| 354 | // them (the one we know is missing are lambdas). |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 355 | parseBlock(); |
| 356 | addUnwrappedLine(); |
| 357 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 358 | case tok::identifier: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 359 | nextToken(); |
| 360 | if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) { |
| 361 | parseLabel(); |
| 362 | return; |
| 363 | } |
| 364 | break; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 365 | case tok::equal: |
| 366 | nextToken(); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 367 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 368 | parseBracedList(); |
| 369 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 370 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 371 | default: |
| 372 | nextToken(); |
| 373 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 374 | } |
| 375 | } while (!eof()); |
| 376 | } |
| 377 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 378 | void UnwrappedLineParser::parseBracedList() { |
| 379 | nextToken(); |
| 380 | |
| 381 | do { |
| 382 | switch (FormatTok.Tok.getKind()) { |
| 383 | case tok::l_brace: |
| 384 | parseBracedList(); |
| 385 | break; |
| 386 | case tok::r_brace: |
| 387 | nextToken(); |
| 388 | return; |
| 389 | default: |
| 390 | nextToken(); |
| 391 | break; |
| 392 | } |
| 393 | } while (!eof()); |
| 394 | } |
| 395 | |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 396 | void UnwrappedLineParser::parseReturn() { |
| 397 | nextToken(); |
| 398 | |
| 399 | do { |
| 400 | switch (FormatTok.Tok.getKind()) { |
| 401 | case tok::l_brace: |
| 402 | parseBracedList(); |
| 403 | break; |
| 404 | case tok::l_paren: |
| 405 | parseParens(); |
| 406 | break; |
| 407 | case tok::r_brace: |
| 408 | // Assume missing ';'. |
| 409 | addUnwrappedLine(); |
| 410 | return; |
| 411 | case tok::semi: |
| 412 | nextToken(); |
| 413 | addUnwrappedLine(); |
| 414 | return; |
| 415 | default: |
| 416 | nextToken(); |
| 417 | break; |
| 418 | } |
| 419 | } while (!eof()); |
| 420 | } |
| 421 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 422 | void UnwrappedLineParser::parseParens() { |
| 423 | assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected."); |
| 424 | nextToken(); |
| 425 | do { |
| 426 | switch (FormatTok.Tok.getKind()) { |
| 427 | case tok::l_paren: |
| 428 | parseParens(); |
| 429 | break; |
| 430 | case tok::r_paren: |
| 431 | nextToken(); |
| 432 | return; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 433 | case tok::l_brace: |
| 434 | { |
| 435 | nextToken(); |
| 436 | ScopedLineState LineState(*this); |
| 437 | Line->Level += 1; |
| 438 | parseLevel(/*HasOpeningBrace=*/true); |
| 439 | Line->Level -= 1; |
| 440 | } |
| 441 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 442 | default: |
| 443 | nextToken(); |
| 444 | break; |
| 445 | } |
| 446 | } while (!eof()); |
| 447 | } |
| 448 | |
| 449 | void UnwrappedLineParser::parseIfThenElse() { |
| 450 | assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected"); |
| 451 | nextToken(); |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 452 | if (FormatTok.Tok.is(tok::l_paren)) |
| 453 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 454 | bool NeedsUnwrappedLine = false; |
| 455 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 456 | parseBlock(); |
| 457 | NeedsUnwrappedLine = true; |
| 458 | } else { |
| 459 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 460 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 461 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 462 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 463 | } |
| 464 | if (FormatTok.Tok.is(tok::kw_else)) { |
| 465 | nextToken(); |
| 466 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 467 | parseBlock(); |
| 468 | addUnwrappedLine(); |
| 469 | } else if (FormatTok.Tok.is(tok::kw_if)) { |
| 470 | parseIfThenElse(); |
| 471 | } else { |
| 472 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 473 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 474 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 475 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 476 | } |
| 477 | } else if (NeedsUnwrappedLine) { |
| 478 | addUnwrappedLine(); |
| 479 | } |
| 480 | } |
| 481 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 482 | void UnwrappedLineParser::parseNamespace() { |
| 483 | assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); |
| 484 | nextToken(); |
| 485 | if (FormatTok.Tok.is(tok::identifier)) |
| 486 | nextToken(); |
| 487 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 488 | parseBlock(0); |
| 489 | addUnwrappedLine(); |
| 490 | } |
| 491 | // FIXME: Add error handling. |
| 492 | } |
| 493 | |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 494 | void UnwrappedLineParser::parseForOrWhileLoop() { |
| 495 | assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && |
| 496 | "'for' or 'while' expected"); |
| 497 | nextToken(); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 498 | if (FormatTok.Tok.is(tok::l_paren)) |
| 499 | parseParens(); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 500 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 501 | parseBlock(); |
| 502 | addUnwrappedLine(); |
| 503 | } else { |
| 504 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 505 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 506 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 507 | --Line->Level; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 511 | void UnwrappedLineParser::parseDoWhile() { |
| 512 | assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected"); |
| 513 | nextToken(); |
| 514 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 515 | parseBlock(); |
| 516 | } else { |
| 517 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 518 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 519 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 520 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 523 | // FIXME: Add error handling. |
| 524 | if (!FormatTok.Tok.is(tok::kw_while)) { |
| 525 | addUnwrappedLine(); |
| 526 | return; |
| 527 | } |
| 528 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 529 | nextToken(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 530 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void UnwrappedLineParser::parseLabel() { |
| 534 | // FIXME: remove all asserts. |
| 535 | assert(FormatTok.Tok.is(tok::colon) && "':' expected"); |
| 536 | nextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 537 | unsigned OldLineLevel = Line->Level; |
| 538 | if (Line->Level > 0) |
| 539 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 540 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 541 | parseBlock(); |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 542 | if (FormatTok.Tok.is(tok::kw_break)) |
| 543 | parseStructuralElement(); // "break;" after "}" goes on the same line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 544 | } |
| 545 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 546 | Line->Level = OldLineLevel; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | void UnwrappedLineParser::parseCaseLabel() { |
| 550 | assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected"); |
| 551 | // FIXME: fix handling of complex expressions here. |
| 552 | do { |
| 553 | nextToken(); |
| 554 | } while (!eof() && !FormatTok.Tok.is(tok::colon)); |
| 555 | parseLabel(); |
| 556 | } |
| 557 | |
| 558 | void UnwrappedLineParser::parseSwitch() { |
| 559 | assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected"); |
| 560 | nextToken(); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 561 | if (FormatTok.Tok.is(tok::l_paren)) |
| 562 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 563 | if (FormatTok.Tok.is(tok::l_brace)) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 564 | parseBlock(Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 565 | addUnwrappedLine(); |
| 566 | } else { |
| 567 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 568 | Line->Level += (Style.IndentCaseLabels ? 2 : 1); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 569 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 570 | Line->Level -= (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| 574 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 575 | nextToken(); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 576 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
| 577 | if (FormatTok.Tok.is(tok::colon)) |
| 578 | nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 579 | addUnwrappedLine(); |
| 580 | } |
| 581 | |
| 582 | void UnwrappedLineParser::parseEnum() { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 583 | bool HasContents = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 584 | do { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 585 | switch (FormatTok.Tok.getKind()) { |
| 586 | case tok::l_brace: |
| 587 | nextToken(); |
| 588 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 589 | ++Line->Level; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 590 | parseComments(); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 591 | break; |
| 592 | case tok::l_paren: |
| 593 | parseParens(); |
| 594 | break; |
| 595 | case tok::comma: |
| 596 | nextToken(); |
| 597 | addUnwrappedLine(); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 598 | parseComments(); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 599 | break; |
| 600 | case tok::r_brace: |
| 601 | if (HasContents) |
| 602 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 603 | --Line->Level; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 604 | nextToken(); |
| 605 | break; |
| 606 | case tok::semi: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 607 | nextToken(); |
| 608 | addUnwrappedLine(); |
| 609 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 610 | default: |
| 611 | HasContents = true; |
| 612 | nextToken(); |
| 613 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 614 | } |
| 615 | } while (!eof()); |
| 616 | } |
| 617 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 618 | void UnwrappedLineParser::parseRecord() { |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 619 | nextToken(); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 620 | if (FormatTok.Tok.is(tok::identifier) || |
| 621 | FormatTok.Tok.is(tok::kw___attribute) || |
| 622 | FormatTok.Tok.is(tok::kw___declspec)) { |
| 623 | nextToken(); |
| 624 | // We can have macros or attributes in between 'class' and the class name. |
| 625 | if (FormatTok.Tok.is(tok::l_paren)) { |
| 626 | parseParens(); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 627 | } |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 628 | // The actual identifier can be a nested name specifier. |
| 629 | while (FormatTok.Tok.is(tok::identifier) || |
| 630 | FormatTok.Tok.is(tok::coloncolon)) |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 631 | nextToken(); |
| 632 | |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 633 | // Note that parsing away template declarations here leads to incorrectly |
| 634 | // accepting function declarations as record declarations. |
| 635 | // In general, we cannot solve this problem. Consider: |
| 636 | // class A<int> B() {} |
| 637 | // which can be a function definition or a class definition when B() is a |
| 638 | // macro. If we find enough real-world cases where this is a problem, we |
| 639 | // can parse for the 'template' keyword in the beginning of the statement, |
| 640 | // and thus rule out the record production in case there is no template |
| 641 | // (this would still leave us with an ambiguity between template function |
| 642 | // and class declarations). |
| 643 | if (FormatTok.Tok.is(tok::colon) || FormatTok.Tok.is(tok::less)) { |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 644 | while (FormatTok.Tok.isNot(tok::l_brace)) { |
| 645 | if (FormatTok.Tok.is(tok::semi)) |
| 646 | return; |
| 647 | nextToken(); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | if (FormatTok.Tok.is(tok::l_brace)) |
| 652 | parseBlock(); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 653 | // We fall through to parsing a structural element afterwards, so |
| 654 | // class A {} n, m; |
| 655 | // will end up in one unwrapped line. |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 658 | void UnwrappedLineParser::parseObjCProtocolList() { |
| 659 | assert(FormatTok.Tok.is(tok::less) && "'<' expected."); |
| 660 | do |
| 661 | nextToken(); |
| 662 | while (!eof() && FormatTok.Tok.isNot(tok::greater)); |
| 663 | nextToken(); // Skip '>'. |
| 664 | } |
| 665 | |
| 666 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 667 | do { |
| 668 | if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) { |
| 669 | nextToken(); |
| 670 | addUnwrappedLine(); |
| 671 | break; |
| 672 | } |
| 673 | parseStructuralElement(); |
| 674 | } while (!eof()); |
| 675 | } |
| 676 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 677 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 678 | nextToken(); |
| 679 | nextToken(); // interface name |
| 680 | |
| 681 | // @interface can be followed by either a base class, or a category. |
| 682 | if (FormatTok.Tok.is(tok::colon)) { |
| 683 | nextToken(); |
| 684 | nextToken(); // base class name |
| 685 | } else if (FormatTok.Tok.is(tok::l_paren)) |
| 686 | // Skip category, if present. |
| 687 | parseParens(); |
| 688 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 689 | if (FormatTok.Tok.is(tok::less)) |
| 690 | parseObjCProtocolList(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 691 | |
| 692 | // If instance variables are present, keep the '{' on the first line too. |
| 693 | if (FormatTok.Tok.is(tok::l_brace)) |
| 694 | parseBlock(); |
| 695 | |
| 696 | // With instance variables, this puts '}' on its own line. Without instance |
| 697 | // variables, this ends the @interface line. |
| 698 | addUnwrappedLine(); |
| 699 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 700 | parseObjCUntilAtEnd(); |
| 701 | } |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 702 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 703 | void UnwrappedLineParser::parseObjCProtocol() { |
| 704 | nextToken(); |
| 705 | nextToken(); // protocol name |
| 706 | |
| 707 | if (FormatTok.Tok.is(tok::less)) |
| 708 | parseObjCProtocolList(); |
| 709 | |
| 710 | // Check for protocol declaration. |
| 711 | if (FormatTok.Tok.is(tok::semi)) { |
| 712 | nextToken(); |
| 713 | return addUnwrappedLine(); |
| 714 | } |
| 715 | |
| 716 | addUnwrappedLine(); |
| 717 | parseObjCUntilAtEnd(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 720 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 721 | if (Line->Tokens.empty()) |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 722 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 723 | // Consume trailing comments. |
| 724 | while (!eof() && FormatTok.NewlinesBefore == 0 && |
| 725 | FormatTok.Tok.is(tok::comment)) { |
| 726 | nextToken(); |
| 727 | } |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 728 | DEBUG({ |
| 729 | llvm::dbgs() << "Line: "; |
| 730 | for (std::list<FormatToken>::iterator I = Line->Tokens.begin(), |
| 731 | E = Line->Tokens.end(); |
| 732 | I != E; ++I) { |
| 733 | llvm::dbgs() << I->Tok.getName() << " "; |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 734 | |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 735 | } |
| 736 | llvm::dbgs() << "\n"; |
| 737 | }); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 738 | CurrentLines->push_back(*Line); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 739 | Line->Tokens.clear(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 740 | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { |
| 741 | for (std::vector<UnwrappedLine>::iterator I = PreprocessorDirectives |
| 742 | .begin(), E = PreprocessorDirectives.end(); |
| 743 | I != E; ++I) { |
| 744 | CurrentLines->push_back(*I); |
| 745 | } |
| 746 | PreprocessorDirectives.clear(); |
| 747 | } |
| 748 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | bool UnwrappedLineParser::eof() const { |
| 752 | return FormatTok.Tok.is(tok::eof); |
| 753 | } |
| 754 | |
| 755 | void UnwrappedLineParser::nextToken() { |
| 756 | if (eof()) |
| 757 | return; |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 758 | Line->Tokens.push_back(FormatTok); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 759 | if (MustBreakBeforeNextToken) { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 760 | Line->Tokens.back().MustBreakBefore = true; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 761 | MustBreakBeforeNextToken = false; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 762 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 763 | readToken(); |
| 764 | } |
| 765 | |
| 766 | void UnwrappedLineParser::readToken() { |
| 767 | FormatTok = Tokens->getNextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 768 | while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) && |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 769 | ((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) || |
| 770 | FormatTok.IsFirst)) { |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 771 | // If there is an unfinished unwrapped line, we flush the preprocessor |
| 772 | // directives only after that unwrapped line was finished later. |
| 773 | bool SwitchToPreprocessorLines = !Line->Tokens.empty() && |
| 774 | CurrentLines == &Lines; |
| 775 | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 776 | parsePPDirective(); |
| 777 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 780 | } // end namespace format |
| 781 | } // end namespace clang |