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: |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 74 | bool eof() { return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline; } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 75 | |
| 76 | FormatToken createEOF() { |
| 77 | FormatToken FormatTok; |
| 78 | FormatTok.Tok.startToken(); |
| 79 | FormatTok.Tok.setKind(tok::eof); |
| 80 | return FormatTok; |
| 81 | } |
| 82 | |
| 83 | UnwrappedLine &Line; |
| 84 | FormatTokenSource *&TokenSource; |
| 85 | FormatToken &ResetToken; |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 86 | unsigned PreviousLineLevel; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 87 | FormatTokenSource *PreviousTokenSource; |
| 88 | |
| 89 | FormatToken Token; |
| 90 | }; |
| 91 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 92 | class ScopedLineState { |
| 93 | public: |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 94 | ScopedLineState(UnwrappedLineParser &Parser, |
| 95 | bool SwitchToPreprocessorLines = false) |
| 96 | : Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) { |
| 97 | if (SwitchToPreprocessorLines) |
| 98 | Parser.CurrentLines = &Parser.PreprocessorDirectives; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 99 | PreBlockLine = Parser.Line.take(); |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 100 | Parser.Line.reset(new UnwrappedLine()); |
| 101 | Parser.Line->Level = PreBlockLine->Level; |
| 102 | Parser.Line->InPPDirective = PreBlockLine->InPPDirective; |
Alexander Kornienko | f52d527 | 2013-04-02 13:04:06 +0000 | [diff] [blame] | 103 | Parser.CommentsBeforeNextToken.swap(CommentsBeforeNextToken); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | ~ScopedLineState() { |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 107 | if (!Parser.Line->Tokens.empty()) { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 108 | Parser.addUnwrappedLine(); |
| 109 | } |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 110 | assert(Parser.Line->Tokens.empty()); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 111 | Parser.Line.reset(PreBlockLine); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 112 | Parser.MustBreakBeforeNextToken = true; |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 113 | if (SwitchToPreprocessorLines) |
| 114 | Parser.CurrentLines = &Parser.Lines; |
Alexander Kornienko | f52d527 | 2013-04-02 13:04:06 +0000 | [diff] [blame] | 115 | Parser.CommentsBeforeNextToken.swap(CommentsBeforeNextToken); |
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; |
Alexander Kornienko | f52d527 | 2013-04-02 13:04:06 +0000 | [diff] [blame] | 123 | SmallVector<FormatToken, 1> CommentsBeforeNextToken; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 126 | UnwrappedLineParser::UnwrappedLineParser( |
| 127 | clang::DiagnosticsEngine &Diag, const FormatStyle &Style, |
| 128 | FormatTokenSource &Tokens, UnwrappedLineConsumer &Callback) |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 129 | : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), |
| 130 | CurrentLines(&Lines), Diag(Diag), Style(Style), Tokens(&Tokens), |
| 131 | Callback(Callback) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 132 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 133 | bool UnwrappedLineParser::parse() { |
Manuel Klimek | 8fa3799 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 134 | DEBUG(llvm::dbgs() << "----\n"); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 135 | readToken(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 136 | bool Error = parseFile(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 137 | for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), E = Lines.end(); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 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() { |
Daniel Jasper | 627707b | 2013-03-22 16:55:40 +0000 | [diff] [blame] | 150 | ScopedDeclarationState DeclarationState( |
| 151 | *Line, DeclarationScopeStack, |
| 152 | /*MustBeDeclaration=*/ !Line->InPPDirective); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 153 | bool Error = parseLevel(/*HasOpeningBrace=*/ false); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 154 | // Make sure to format the remaining tokens. |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 155 | flushComments(true); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 156 | addUnwrappedLine(); |
| 157 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 160 | bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 161 | bool Error = false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 162 | do { |
| 163 | switch (FormatTok.Tok.getKind()) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 164 | case tok::comment: |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 165 | nextToken(); |
| 166 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 167 | break; |
| 168 | case tok::l_brace: |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 169 | // FIXME: Add parameter whether this can happen - if this happens, we must |
| 170 | // be in a non-declaration context. |
| 171 | Error |= parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 172 | addUnwrappedLine(); |
| 173 | break; |
| 174 | case tok::r_brace: |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 175 | if (HasOpeningBrace) { |
| 176 | return false; |
| 177 | } else { |
Alexander Kornienko | 3048aea | 2013-01-10 15:05:09 +0000 | [diff] [blame] | 178 | Diag.Report(FormatTok.Tok.getLocation(), |
| 179 | Diag.getCustomDiagID(clang::DiagnosticsEngine::Error, |
Alexander Kornienko | 276a209 | 2013-01-11 16:03:45 +0000 | [diff] [blame] | 180 | "unexpected '}'")); |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 181 | Error = true; |
| 182 | nextToken(); |
| 183 | addUnwrappedLine(); |
| 184 | } |
| 185 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 186 | default: |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 187 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | } while (!eof()); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 191 | return Error; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 194 | bool UnwrappedLineParser::parseBlock(bool MustBeDeclaration, |
| 195 | unsigned AddLevels) { |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 196 | assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 197 | nextToken(); |
| 198 | |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 199 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 200 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 201 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 202 | MustBeDeclaration); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 203 | Line->Level += AddLevels; |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 204 | parseLevel(/*HasOpeningBrace=*/ true); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 205 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 206 | if (!FormatTok.Tok.is(tok::r_brace)) { |
| 207 | Line->Level -= AddLevels; |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 208 | return true; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 209 | } |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 210 | |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 211 | nextToken(); // Munch the closing brace. |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 212 | Line->Level -= AddLevels; |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 213 | return false; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 217 | assert(FormatTok.Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 218 | ScopedMacroState MacroState(*Line, Tokens, FormatTok); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 219 | nextToken(); |
| 220 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 221 | if (FormatTok.Tok.getIdentifierInfo() == NULL) { |
Manuel Klimek | bd04f2a | 2013-01-31 15:58:48 +0000 | [diff] [blame] | 222 | parsePPUnknown(); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 223 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 224 | } |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 225 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 226 | switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) { |
| 227 | case tok::pp_define: |
| 228 | parsePPDefine(); |
| 229 | break; |
| 230 | default: |
| 231 | parsePPUnknown(); |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void UnwrappedLineParser::parsePPDefine() { |
| 237 | nextToken(); |
| 238 | |
| 239 | if (FormatTok.Tok.getKind() != tok::identifier) { |
| 240 | parsePPUnknown(); |
| 241 | return; |
| 242 | } |
| 243 | nextToken(); |
Manuel Klimek | 7ccbc21 | 2013-01-23 14:37:36 +0000 | [diff] [blame] | 244 | if (FormatTok.Tok.getKind() == tok::l_paren && |
| 245 | FormatTok.WhiteSpaceLength == 0) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 246 | parseParens(); |
| 247 | } |
| 248 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 249 | Line->Level = 1; |
Manuel Klimek | c3d0c82 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 250 | |
| 251 | // Errors during a preprocessor directive can only affect the layout of the |
| 252 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 253 | // would be to use the same approach we use on the file level (no |
| 254 | // re-indentation if there was a structural error) within the macro |
| 255 | // definition. |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 256 | parseFile(); |
| 257 | } |
| 258 | |
| 259 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 260 | do { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 261 | nextToken(); |
| 262 | } while (!eof()); |
| 263 | addUnwrappedLine(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 266 | void UnwrappedLineParser::parseStructuralElement() { |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 267 | assert(!FormatTok.Tok.is(tok::l_brace)); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 268 | int TokenNumber = 0; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 269 | switch (FormatTok.Tok.getKind()) { |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 270 | case tok::at: |
| 271 | nextToken(); |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 272 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 273 | parseBracedList(); |
| 274 | break; |
| 275 | } |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 276 | switch (FormatTok.Tok.getObjCKeywordID()) { |
| 277 | case tok::objc_public: |
| 278 | case tok::objc_protected: |
| 279 | case tok::objc_package: |
| 280 | case tok::objc_private: |
| 281 | return parseAccessSpecifier(); |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 282 | case tok::objc_interface: |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 283 | case tok::objc_implementation: |
| 284 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 285 | case tok::objc_protocol: |
| 286 | return parseObjCProtocol(); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 287 | case tok::objc_end: |
| 288 | return; // Handled by the caller. |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 289 | case tok::objc_optional: |
| 290 | case tok::objc_required: |
| 291 | nextToken(); |
| 292 | addUnwrappedLine(); |
| 293 | return; |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 294 | default: |
| 295 | break; |
| 296 | } |
| 297 | break; |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 298 | case tok::kw_namespace: |
| 299 | parseNamespace(); |
| 300 | return; |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 301 | case tok::kw_inline: |
| 302 | nextToken(); |
| 303 | TokenNumber++; |
| 304 | if (FormatTok.Tok.is(tok::kw_namespace)) { |
| 305 | parseNamespace(); |
| 306 | return; |
| 307 | } |
| 308 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 309 | case tok::kw_public: |
| 310 | case tok::kw_protected: |
| 311 | case tok::kw_private: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 312 | parseAccessSpecifier(); |
| 313 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 314 | case tok::kw_if: |
| 315 | parseIfThenElse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 316 | return; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 317 | case tok::kw_for: |
| 318 | case tok::kw_while: |
| 319 | parseForOrWhileLoop(); |
| 320 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 321 | case tok::kw_do: |
| 322 | parseDoWhile(); |
| 323 | return; |
| 324 | case tok::kw_switch: |
| 325 | parseSwitch(); |
| 326 | return; |
| 327 | case tok::kw_default: |
| 328 | nextToken(); |
| 329 | parseLabel(); |
| 330 | return; |
| 331 | case tok::kw_case: |
| 332 | parseCaseLabel(); |
| 333 | return; |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 334 | case tok::kw_return: |
| 335 | parseReturn(); |
| 336 | return; |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 337 | case tok::kw_extern: |
| 338 | nextToken(); |
| 339 | if (FormatTok.Tok.is(tok::string_literal)) { |
| 340 | nextToken(); |
| 341 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 342 | parseBlock(/*MustBeDeclaration=*/ true, 0); |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 343 | addUnwrappedLine(); |
| 344 | return; |
| 345 | } |
| 346 | } |
| 347 | // In all other cases, parse the declaration. |
| 348 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 349 | default: |
| 350 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 351 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 352 | do { |
| 353 | ++TokenNumber; |
| 354 | switch (FormatTok.Tok.getKind()) { |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 355 | case tok::at: |
| 356 | nextToken(); |
| 357 | if (FormatTok.Tok.is(tok::l_brace)) |
| 358 | parseBracedList(); |
| 359 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 360 | case tok::kw_enum: |
| 361 | parseEnum(); |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 362 | break; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 363 | case tok::kw_struct: |
| 364 | case tok::kw_union: |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 365 | case tok::kw_class: |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 366 | parseRecord(); |
| 367 | // A record declaration or definition is always the start of a structural |
| 368 | // element. |
| 369 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 370 | case tok::semi: |
| 371 | nextToken(); |
| 372 | addUnwrappedLine(); |
| 373 | return; |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 374 | case tok::r_brace: |
| 375 | addUnwrappedLine(); |
| 376 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 377 | case tok::l_paren: |
| 378 | parseParens(); |
| 379 | break; |
| 380 | case tok::l_brace: |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 381 | // A block outside of parentheses must be the last part of a |
| 382 | // structural element. |
| 383 | // FIXME: Figure out cases where this is not true, and add projections for |
| 384 | // them (the one we know is missing are lambdas). |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 385 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 386 | addUnwrappedLine(); |
| 387 | return; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 388 | case tok::identifier: |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 389 | nextToken(); |
| 390 | if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) { |
| 391 | parseLabel(); |
| 392 | return; |
| 393 | } |
| 394 | break; |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 395 | case tok::equal: |
| 396 | nextToken(); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 397 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 398 | parseBracedList(); |
| 399 | } |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 400 | break; |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 401 | default: |
| 402 | nextToken(); |
| 403 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 404 | } |
| 405 | } while (!eof()); |
| 406 | } |
| 407 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 408 | void UnwrappedLineParser::parseBracedList() { |
| 409 | nextToken(); |
| 410 | |
| 411 | do { |
| 412 | switch (FormatTok.Tok.getKind()) { |
| 413 | case tok::l_brace: |
| 414 | parseBracedList(); |
| 415 | break; |
| 416 | case tok::r_brace: |
| 417 | nextToken(); |
| 418 | return; |
| 419 | default: |
| 420 | nextToken(); |
| 421 | break; |
| 422 | } |
| 423 | } while (!eof()); |
| 424 | } |
| 425 | |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 426 | void UnwrappedLineParser::parseReturn() { |
| 427 | nextToken(); |
| 428 | |
| 429 | do { |
| 430 | switch (FormatTok.Tok.getKind()) { |
| 431 | case tok::l_brace: |
| 432 | parseBracedList(); |
| 433 | break; |
| 434 | case tok::l_paren: |
| 435 | parseParens(); |
| 436 | break; |
| 437 | case tok::r_brace: |
| 438 | // Assume missing ';'. |
| 439 | addUnwrappedLine(); |
| 440 | return; |
| 441 | case tok::semi: |
| 442 | nextToken(); |
| 443 | addUnwrappedLine(); |
| 444 | return; |
| 445 | default: |
| 446 | nextToken(); |
| 447 | break; |
| 448 | } |
| 449 | } while (!eof()); |
| 450 | } |
| 451 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 452 | void UnwrappedLineParser::parseParens() { |
| 453 | assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected."); |
| 454 | nextToken(); |
| 455 | do { |
| 456 | switch (FormatTok.Tok.getKind()) { |
| 457 | case tok::l_paren: |
| 458 | parseParens(); |
| 459 | break; |
| 460 | case tok::r_paren: |
| 461 | nextToken(); |
| 462 | return; |
Nico Weber | 2afbe52 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 463 | case tok::l_brace: { |
| 464 | nextToken(); |
| 465 | ScopedLineState LineState(*this); |
| 466 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 467 | /*MustBeDeclaration=*/ false); |
| 468 | Line->Level += 1; |
| 469 | parseLevel(/*HasOpeningBrace=*/ true); |
| 470 | Line->Level -= 1; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 471 | break; |
Nico Weber | 2afbe52 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 472 | } |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 473 | case tok::at: |
| 474 | nextToken(); |
| 475 | if (FormatTok.Tok.is(tok::l_brace)) |
| 476 | parseBracedList(); |
| 477 | break; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 478 | default: |
| 479 | nextToken(); |
| 480 | break; |
| 481 | } |
| 482 | } while (!eof()); |
| 483 | } |
| 484 | |
| 485 | void UnwrappedLineParser::parseIfThenElse() { |
| 486 | assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected"); |
| 487 | nextToken(); |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 488 | if (FormatTok.Tok.is(tok::l_paren)) |
| 489 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 490 | bool NeedsUnwrappedLine = false; |
| 491 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 492 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 493 | NeedsUnwrappedLine = true; |
| 494 | } else { |
| 495 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 496 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 497 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 498 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 499 | } |
| 500 | if (FormatTok.Tok.is(tok::kw_else)) { |
| 501 | nextToken(); |
| 502 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 503 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 504 | addUnwrappedLine(); |
| 505 | } else if (FormatTok.Tok.is(tok::kw_if)) { |
| 506 | parseIfThenElse(); |
| 507 | } else { |
| 508 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 509 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 510 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 511 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 512 | } |
| 513 | } else if (NeedsUnwrappedLine) { |
| 514 | addUnwrappedLine(); |
| 515 | } |
| 516 | } |
| 517 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 518 | void UnwrappedLineParser::parseNamespace() { |
| 519 | assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected"); |
| 520 | nextToken(); |
| 521 | if (FormatTok.Tok.is(tok::identifier)) |
| 522 | nextToken(); |
| 523 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 524 | parseBlock(/*MustBeDeclaration=*/ true, 0); |
Manuel Klimek | 7fc2db0 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 525 | // Munch the semicolon after a namespace. This is more common than one would |
| 526 | // think. Puttin the semicolon into its own line is very ugly. |
| 527 | if (FormatTok.Tok.is(tok::semi)) |
| 528 | nextToken(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 529 | addUnwrappedLine(); |
| 530 | } |
| 531 | // FIXME: Add error handling. |
| 532 | } |
| 533 | |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 534 | void UnwrappedLineParser::parseForOrWhileLoop() { |
| 535 | assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) && |
| 536 | "'for' or 'while' expected"); |
| 537 | nextToken(); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 538 | if (FormatTok.Tok.is(tok::l_paren)) |
| 539 | parseParens(); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 540 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 541 | parseBlock(/*MustBeDeclaration=*/ false); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 542 | addUnwrappedLine(); |
| 543 | } else { |
| 544 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 545 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 546 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 547 | --Line->Level; |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 551 | void UnwrappedLineParser::parseDoWhile() { |
| 552 | assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected"); |
| 553 | nextToken(); |
| 554 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 555 | parseBlock(/*MustBeDeclaration=*/ false); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 556 | } else { |
| 557 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 558 | ++Line->Level; |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 559 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 560 | --Line->Level; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 563 | // FIXME: Add error handling. |
| 564 | if (!FormatTok.Tok.is(tok::kw_while)) { |
| 565 | addUnwrappedLine(); |
| 566 | return; |
| 567 | } |
| 568 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 569 | nextToken(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 570 | parseStructuralElement(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | void UnwrappedLineParser::parseLabel() { |
Daniel Jasper | 89a0daa | 2013-02-12 20:17:17 +0000 | [diff] [blame] | 574 | if (FormatTok.Tok.isNot(tok::colon)) |
| 575 | return; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 576 | nextToken(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 577 | unsigned OldLineLevel = Line->Level; |
Daniel Jasper | bcca7e4 | 2013-03-20 10:23:53 +0000 | [diff] [blame] | 578 | if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 579 | --Line->Level; |
Daniel Jasper | c30eb51 | 2013-03-19 18:33:58 +0000 | [diff] [blame] | 580 | if (CommentsBeforeNextToken.empty() && FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 581 | parseBlock(/*MustBeDeclaration=*/ false); |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 582 | if (FormatTok.Tok.is(tok::kw_break)) |
| 583 | parseStructuralElement(); // "break;" after "}" goes on the same line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 584 | } |
| 585 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 586 | Line->Level = OldLineLevel; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | void UnwrappedLineParser::parseCaseLabel() { |
| 590 | assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected"); |
| 591 | // FIXME: fix handling of complex expressions here. |
| 592 | do { |
| 593 | nextToken(); |
| 594 | } while (!eof() && !FormatTok.Tok.is(tok::colon)); |
| 595 | parseLabel(); |
| 596 | } |
| 597 | |
| 598 | void UnwrappedLineParser::parseSwitch() { |
| 599 | assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected"); |
| 600 | nextToken(); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 601 | if (FormatTok.Tok.is(tok::l_paren)) |
| 602 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 603 | if (FormatTok.Tok.is(tok::l_brace)) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 604 | parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 605 | addUnwrappedLine(); |
| 606 | } else { |
| 607 | addUnwrappedLine(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 608 | Line->Level += (Style.IndentCaseLabels ? 2 : 1); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 609 | parseStructuralElement(); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 610 | Line->Level -= (Style.IndentCaseLabels ? 2 : 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
| 614 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 615 | nextToken(); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 616 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
| 617 | if (FormatTok.Tok.is(tok::colon)) |
| 618 | nextToken(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 619 | addUnwrappedLine(); |
| 620 | } |
| 621 | |
| 622 | void UnwrappedLineParser::parseEnum() { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 623 | nextToken(); |
| 624 | if (FormatTok.Tok.is(tok::identifier) || |
| 625 | FormatTok.Tok.is(tok::kw___attribute) || |
| 626 | FormatTok.Tok.is(tok::kw___declspec)) { |
| 627 | nextToken(); |
| 628 | // We can have macros or attributes in between 'enum' and the enum name. |
| 629 | if (FormatTok.Tok.is(tok::l_paren)) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 630 | parseParens(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 631 | } |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 632 | if (FormatTok.Tok.is(tok::identifier)) |
| 633 | nextToken(); |
| 634 | } |
| 635 | if (FormatTok.Tok.is(tok::l_brace)) { |
| 636 | nextToken(); |
| 637 | addUnwrappedLine(); |
| 638 | ++Line->Level; |
| 639 | do { |
| 640 | switch (FormatTok.Tok.getKind()) { |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 641 | case tok::l_paren: |
| 642 | parseParens(); |
| 643 | break; |
| 644 | case tok::r_brace: |
| 645 | addUnwrappedLine(); |
| 646 | nextToken(); |
| 647 | --Line->Level; |
| 648 | return; |
| 649 | case tok::comma: |
| 650 | nextToken(); |
| 651 | addUnwrappedLine(); |
| 652 | break; |
| 653 | default: |
| 654 | nextToken(); |
| 655 | break; |
| 656 | } |
| 657 | } while (!eof()); |
| 658 | } |
| 659 | // We fall through to parsing a structural element afterwards, so that in |
| 660 | // enum A {} n, m; |
| 661 | // "} n, m;" will end up in one unwrapped line. |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 664 | void UnwrappedLineParser::parseRecord() { |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 665 | nextToken(); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 666 | if (FormatTok.Tok.is(tok::identifier) || |
| 667 | FormatTok.Tok.is(tok::kw___attribute) || |
| 668 | FormatTok.Tok.is(tok::kw___declspec)) { |
| 669 | nextToken(); |
| 670 | // We can have macros or attributes in between 'class' and the class name. |
| 671 | if (FormatTok.Tok.is(tok::l_paren)) { |
| 672 | parseParens(); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 673 | } |
Manuel Klimek | b8b1ce1 | 2013-02-06 15:57:54 +0000 | [diff] [blame] | 674 | // The actual identifier can be a nested name specifier, and in macros |
| 675 | // it is often token-pasted. |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 676 | while (FormatTok.Tok.is(tok::identifier) || |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 677 | FormatTok.Tok.is(tok::coloncolon) || 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)) { |
Daniel Jasper | 6fe554e | 2013-03-20 15:12:38 +0000 | [diff] [blame] | 691 | while (!eof() && FormatTok.Tok.isNot(tok::l_brace)) { |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 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(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 726 | nextToken(); // interface name |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 727 | |
| 728 | // @interface can be followed by either a base class, or a category. |
| 729 | if (FormatTok.Tok.is(tok::colon)) { |
| 730 | nextToken(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 731 | nextToken(); // base class name |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 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(); |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 752 | nextToken(); // protocol name |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 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 | |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 794 | bool UnwrappedLineParser::eof() const { return FormatTok.Tok.is(tok::eof); } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 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. |
Daniel Jasper | f9955d3 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 830 | bool SwitchToPreprocessorLines = |
| 831 | !Line->Tokens.empty() && CurrentLines == &Lines; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 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 |