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