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