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