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