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