Daniel Jasper | f793511 | 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 | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 16 | #define DEBUG_TYPE "format-parser" |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 17 | |
Chandler Carruth | 4b41745 | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 18 | #include "UnwrappedLineParser.h" |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 20 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 21 | namespace clang { |
| 22 | namespace format { |
| 23 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 24 | class FormatTokenSource { |
| 25 | public: |
| 26 | virtual ~FormatTokenSource() {} |
| 27 | virtual FormatToken *getNextToken() = 0; |
| 28 | |
| 29 | virtual unsigned getPosition() = 0; |
| 30 | virtual FormatToken *setPosition(unsigned Position) = 0; |
| 31 | }; |
| 32 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 35 | class ScopedDeclarationState { |
| 36 | public: |
| 37 | ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack, |
| 38 | bool MustBeDeclaration) |
| 39 | : Line(Line), Stack(Stack) { |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 40 | Line.MustBeDeclaration = MustBeDeclaration; |
Manuel Klimek | 3908057 | 2013-01-23 11:03:04 +0000 | [diff] [blame] | 41 | Stack.push_back(MustBeDeclaration); |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 42 | } |
| 43 | ~ScopedDeclarationState() { |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 44 | Stack.pop_back(); |
Manuel Klimek | c1237a8 | 2013-01-23 14:08:21 +0000 | [diff] [blame] | 45 | if (!Stack.empty()) |
| 46 | Line.MustBeDeclaration = Stack.back(); |
| 47 | else |
| 48 | Line.MustBeDeclaration = true; |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 49 | } |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 50 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 51 | private: |
| 52 | UnwrappedLine &Line; |
| 53 | std::vector<bool> &Stack; |
| 54 | }; |
| 55 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 56 | class ScopedMacroState : public FormatTokenSource { |
| 57 | public: |
| 58 | ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource, |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 59 | FormatToken *&ResetToken, bool &StructuralError) |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 60 | : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken), |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 61 | PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource), |
| 62 | StructuralError(StructuralError), |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 63 | PreviousStructuralError(StructuralError), Token(NULL) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 64 | TokenSource = this; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 65 | Line.Level = 0; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 66 | Line.InPPDirective = true; |
| 67 | } |
| 68 | |
| 69 | ~ScopedMacroState() { |
| 70 | TokenSource = PreviousTokenSource; |
| 71 | ResetToken = Token; |
| 72 | Line.InPPDirective = false; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 73 | Line.Level = PreviousLineLevel; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 74 | StructuralError = PreviousStructuralError; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 77 | virtual FormatToken *getNextToken() { |
Manuel Klimek | 7872571 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 78 | // The \c UnwrappedLineParser guards against this by never calling |
| 79 | // \c getNextToken() after it has encountered the first eof token. |
| 80 | assert(!eof()); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 81 | Token = PreviousTokenSource->getNextToken(); |
| 82 | if (eof()) |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 83 | return getFakeEOF(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 84 | return Token; |
| 85 | } |
| 86 | |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 87 | virtual unsigned getPosition() { return PreviousTokenSource->getPosition(); } |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 88 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 89 | virtual FormatToken *setPosition(unsigned Position) { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 90 | Token = PreviousTokenSource->setPosition(Position); |
| 91 | return Token; |
| 92 | } |
| 93 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 94 | private: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 95 | bool eof() { return Token && Token->HasUnescapedNewline; } |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 96 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 97 | FormatToken *getFakeEOF() { |
| 98 | static bool EOFInitialized = false; |
| 99 | static FormatToken FormatTok; |
| 100 | if (!EOFInitialized) { |
| 101 | FormatTok.Tok.startToken(); |
| 102 | FormatTok.Tok.setKind(tok::eof); |
| 103 | EOFInitialized = true; |
| 104 | } |
| 105 | return &FormatTok; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | UnwrappedLine &Line; |
| 109 | FormatTokenSource *&TokenSource; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 110 | FormatToken *&ResetToken; |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 111 | unsigned PreviousLineLevel; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 112 | FormatTokenSource *PreviousTokenSource; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 113 | bool &StructuralError; |
| 114 | bool PreviousStructuralError; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 115 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 116 | FormatToken *Token; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 119 | } // end anonymous namespace |
| 120 | |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 121 | class ScopedLineState { |
| 122 | public: |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 123 | ScopedLineState(UnwrappedLineParser &Parser, |
| 124 | bool SwitchToPreprocessorLines = false) |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 125 | : Parser(Parser) { |
| 126 | OriginalLines = Parser.CurrentLines; |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 127 | if (SwitchToPreprocessorLines) |
| 128 | Parser.CurrentLines = &Parser.PreprocessorDirectives; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 129 | else if (!Parser.Line->Tokens.empty()) |
| 130 | Parser.CurrentLines = &Parser.Line->Tokens.back().Children; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 131 | PreBlockLine = Parser.Line.take(); |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 132 | Parser.Line.reset(new UnwrappedLine()); |
| 133 | Parser.Line->Level = PreBlockLine->Level; |
| 134 | Parser.Line->InPPDirective = PreBlockLine->InPPDirective; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | ~ScopedLineState() { |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 138 | if (!Parser.Line->Tokens.empty()) { |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 139 | Parser.addUnwrappedLine(); |
| 140 | } |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 141 | assert(Parser.Line->Tokens.empty()); |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 142 | Parser.Line.reset(PreBlockLine); |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 143 | if (Parser.CurrentLines == &Parser.PreprocessorDirectives) |
| 144 | Parser.MustBreakBeforeNextToken = true; |
| 145 | Parser.CurrentLines = OriginalLines; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | private: |
| 149 | UnwrappedLineParser &Parser; |
| 150 | |
| 151 | UnwrappedLine *PreBlockLine; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 152 | SmallVectorImpl<UnwrappedLine> *OriginalLines; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 155 | namespace { |
| 156 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 157 | class IndexedTokenSource : public FormatTokenSource { |
| 158 | public: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 159 | IndexedTokenSource(ArrayRef<FormatToken *> Tokens) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 160 | : Tokens(Tokens), Position(-1) {} |
| 161 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 162 | virtual FormatToken *getNextToken() { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 163 | ++Position; |
| 164 | return Tokens[Position]; |
| 165 | } |
| 166 | |
| 167 | virtual unsigned getPosition() { |
| 168 | assert(Position >= 0); |
| 169 | return Position; |
| 170 | } |
| 171 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 172 | virtual FormatToken *setPosition(unsigned P) { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 173 | Position = P; |
| 174 | return Tokens[Position]; |
| 175 | } |
| 176 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 177 | void reset() { Position = -1; } |
| 178 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 179 | private: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 180 | ArrayRef<FormatToken *> Tokens; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 181 | int Position; |
| 182 | }; |
| 183 | |
Craig Topper | 69665e1 | 2013-07-01 04:21:54 +0000 | [diff] [blame] | 184 | } // end anonymous namespace |
| 185 | |
Daniel Jasper | d2ae41a | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 186 | UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 187 | ArrayRef<FormatToken *> Tokens, |
Daniel Jasper | d2ae41a | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 188 | UnwrappedLineConsumer &Callback) |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 189 | : Line(new UnwrappedLine), MustBreakBeforeNextToken(false), |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 190 | CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL), |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 191 | Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {} |
| 192 | |
| 193 | void UnwrappedLineParser::reset() { |
| 194 | PPBranchLevel = -1; |
| 195 | Line.reset(new UnwrappedLine); |
| 196 | CommentsBeforeNextToken.clear(); |
| 197 | FormatTok = NULL; |
| 198 | MustBreakBeforeNextToken = false; |
| 199 | PreprocessorDirectives.clear(); |
| 200 | CurrentLines = &Lines; |
| 201 | DeclarationScopeStack.clear(); |
| 202 | StructuralError = false; |
| 203 | PPStack.clear(); |
| 204 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 205 | |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 206 | bool UnwrappedLineParser::parse() { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 207 | IndexedTokenSource TokenSource(AllTokens); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 208 | do { |
| 209 | DEBUG(llvm::dbgs() << "----\n"); |
| 210 | reset(); |
| 211 | Tokens = &TokenSource; |
| 212 | TokenSource.reset(); |
Daniel Jasper | a79064a | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 213 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 214 | readToken(); |
| 215 | parseFile(); |
| 216 | // Create line with eof token. |
| 217 | pushToken(FormatTok); |
| 218 | addUnwrappedLine(); |
| 219 | |
| 220 | for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(), |
| 221 | E = Lines.end(); |
| 222 | I != E; ++I) { |
| 223 | Callback.consumeUnwrappedLine(*I); |
| 224 | } |
| 225 | Callback.finishRun(); |
| 226 | Lines.clear(); |
| 227 | while (!PPLevelBranchIndex.empty() && |
Daniel Jasper | 53bd167 | 2013-10-12 13:32:56 +0000 | [diff] [blame] | 228 | PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 229 | PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1); |
| 230 | PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1); |
| 231 | } |
| 232 | if (!PPLevelBranchIndex.empty()) { |
| 233 | ++PPLevelBranchIndex.back(); |
| 234 | assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size()); |
| 235 | assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back()); |
| 236 | } |
| 237 | } while (!PPLevelBranchIndex.empty()); |
| 238 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 239 | return StructuralError; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 242 | void UnwrappedLineParser::parseFile() { |
Daniel Jasper | dd9276e | 2013-03-22 16:55:40 +0000 | [diff] [blame] | 243 | ScopedDeclarationState DeclarationState( |
| 244 | *Line, DeclarationScopeStack, |
| 245 | /*MustBeDeclaration=*/ !Line->InPPDirective); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 246 | parseLevel(/*HasOpeningBrace=*/false); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 247 | // Make sure to format the remaining tokens. |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 248 | flushComments(true); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 249 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 252 | void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 253 | bool SwitchLabelEncountered = false; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 254 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 255 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 256 | case tok::comment: |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 257 | nextToken(); |
| 258 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 259 | break; |
| 260 | case tok::l_brace: |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 261 | // FIXME: Add parameter whether this can happen - if this happens, we must |
| 262 | // be in a non-declaration context. |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 263 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 264 | addUnwrappedLine(); |
| 265 | break; |
| 266 | case tok::r_brace: |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 267 | if (HasOpeningBrace) |
| 268 | return; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 269 | StructuralError = true; |
| 270 | nextToken(); |
| 271 | addUnwrappedLine(); |
Manuel Klimek | 1058d98 | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 272 | break; |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 273 | case tok::kw_default: |
| 274 | case tok::kw_case: |
Daniel Jasper | 7240762 | 2013-09-02 08:26:29 +0000 | [diff] [blame] | 275 | if (!SwitchLabelEncountered && |
| 276 | (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1))) |
| 277 | ++Line->Level; |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 278 | SwitchLabelEncountered = true; |
| 279 | parseStructuralElement(); |
| 280 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 281 | default: |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 282 | parseStructuralElement(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 283 | break; |
| 284 | } |
| 285 | } while (!eof()); |
| 286 | } |
| 287 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 288 | void UnwrappedLineParser::calculateBraceTypes() { |
| 289 | // We'll parse forward through the tokens until we hit |
| 290 | // a closing brace or eof - note that getNextToken() will |
| 291 | // parse macros, so this will magically work inside macro |
| 292 | // definitions, too. |
| 293 | unsigned StoredPosition = Tokens->getPosition(); |
| 294 | unsigned Position = StoredPosition; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 295 | FormatToken *Tok = FormatTok; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 296 | // Keep a stack of positions of lbrace tokens. We will |
| 297 | // update information about whether an lbrace starts a |
| 298 | // braced init list or a different block during the loop. |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 299 | SmallVector<FormatToken *, 8> LBraceStack; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 300 | assert(Tok->Tok.is(tok::l_brace)); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 301 | do { |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 302 | // Get next none-comment token. |
| 303 | FormatToken *NextTok; |
Daniel Jasper | ca7bd72 | 2013-07-01 16:43:38 +0000 | [diff] [blame] | 304 | unsigned ReadTokens = 0; |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 305 | do { |
| 306 | NextTok = Tokens->getNextToken(); |
Daniel Jasper | ca7bd72 | 2013-07-01 16:43:38 +0000 | [diff] [blame] | 307 | ++ReadTokens; |
Daniel Jasper | 7f5d53e | 2013-07-01 09:15:46 +0000 | [diff] [blame] | 308 | } while (NextTok->is(tok::comment)); |
| 309 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 310 | switch (Tok->Tok.getKind()) { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 311 | case tok::l_brace: |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 312 | LBraceStack.push_back(Tok); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 313 | break; |
| 314 | case tok::r_brace: |
| 315 | if (!LBraceStack.empty()) { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 316 | if (LBraceStack.back()->BlockKind == BK_Unknown) { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 317 | // If there is a comma, semicolon or right paren after the closing |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 318 | // brace, we assume this is a braced initializer list. Note that |
| 319 | // regardless how we mark inner braces here, we will overwrite the |
| 320 | // BlockKind later if we parse a braced list (where all blocks inside |
| 321 | // are by default braced lists), or when we explicitly detect blocks |
| 322 | // (for example while parsing lambdas). |
Daniel Jasper | 65b7982 | 2013-08-28 07:50:37 +0000 | [diff] [blame] | 323 | // |
| 324 | // We exclude + and - as they can be ObjC visibility modifiers. |
Daniel Jasper | b631d5e | 2013-11-22 07:26:53 +0000 | [diff] [blame] | 325 | if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren, tok::period, |
Daniel Jasper | f3d9779 | 2013-09-13 10:55:31 +0000 | [diff] [blame] | 326 | tok::r_square, tok::l_brace, tok::colon) || |
Daniel Jasper | 65b7982 | 2013-08-28 07:50:37 +0000 | [diff] [blame] | 327 | (NextTok->isBinaryOperator() && |
| 328 | !NextTok->isOneOf(tok::plus, tok::minus))) { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 329 | Tok->BlockKind = BK_BracedInit; |
| 330 | LBraceStack.back()->BlockKind = BK_BracedInit; |
| 331 | } else { |
| 332 | Tok->BlockKind = BK_Block; |
| 333 | LBraceStack.back()->BlockKind = BK_Block; |
| 334 | } |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 335 | } |
| 336 | LBraceStack.pop_back(); |
| 337 | } |
| 338 | break; |
| 339 | case tok::semi: |
| 340 | case tok::kw_if: |
| 341 | case tok::kw_while: |
| 342 | case tok::kw_for: |
| 343 | case tok::kw_switch: |
| 344 | case tok::kw_try: |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 345 | if (!LBraceStack.empty()) |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 346 | LBraceStack.back()->BlockKind = BK_Block; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 347 | break; |
| 348 | default: |
| 349 | break; |
| 350 | } |
| 351 | Tok = NextTok; |
Daniel Jasper | ca7bd72 | 2013-07-01 16:43:38 +0000 | [diff] [blame] | 352 | Position += ReadTokens; |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 353 | } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty()); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 354 | // Assume other blocks for all unclosed opening braces. |
| 355 | for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 356 | if (LBraceStack[i]->BlockKind == BK_Unknown) |
| 357 | LBraceStack[i]->BlockKind = BK_Block; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 358 | } |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 359 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 360 | FormatTok = Tokens->setPosition(StoredPosition); |
| 361 | } |
| 362 | |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 363 | void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel, |
| 364 | bool MunchSemi) { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 365 | assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected"); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 366 | unsigned InitialLevel = Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 367 | nextToken(); |
| 368 | |
Manuel Klimek | a4fe1c1 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 369 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 370 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 371 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 372 | MustBeDeclaration); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 373 | if (AddLevel) |
| 374 | ++Line->Level; |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 375 | parseLevel(/*HasOpeningBrace=*/true); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 376 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 377 | if (!FormatTok->Tok.is(tok::r_brace)) { |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 378 | Line->Level = InitialLevel; |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 379 | StructuralError = true; |
| 380 | return; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 381 | } |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 382 | |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 383 | nextToken(); // Munch the closing brace. |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 384 | if (MunchSemi && FormatTok->Tok.is(tok::semi)) |
| 385 | nextToken(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 386 | Line->Level = InitialLevel; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 389 | void UnwrappedLineParser::parseChildBlock() { |
| 390 | FormatTok->BlockKind = BK_Block; |
| 391 | nextToken(); |
| 392 | { |
| 393 | ScopedLineState LineState(*this); |
| 394 | ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack, |
| 395 | /*MustBeDeclaration=*/false); |
| 396 | Line->Level += 1; |
| 397 | parseLevel(/*HasOpeningBrace=*/true); |
| 398 | Line->Level -= 1; |
| 399 | } |
| 400 | nextToken(); |
| 401 | } |
| 402 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 403 | void UnwrappedLineParser::parsePPDirective() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 404 | assert(FormatTok->Tok.is(tok::hash) && "'#' expected"); |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 405 | ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError); |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 406 | nextToken(); |
| 407 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 408 | if (FormatTok->Tok.getIdentifierInfo() == NULL) { |
Manuel Klimek | 591b580 | 2013-01-31 15:58:48 +0000 | [diff] [blame] | 409 | parsePPUnknown(); |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 410 | return; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 411 | } |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 412 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 413 | switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 414 | case tok::pp_define: |
| 415 | parsePPDefine(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 416 | return; |
| 417 | case tok::pp_if: |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 418 | parsePPIf(/*IfDef=*/false); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 419 | break; |
| 420 | case tok::pp_ifdef: |
| 421 | case tok::pp_ifndef: |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 422 | parsePPIf(/*IfDef=*/true); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 423 | break; |
| 424 | case tok::pp_else: |
| 425 | parsePPElse(); |
| 426 | break; |
| 427 | case tok::pp_elif: |
| 428 | parsePPElIf(); |
| 429 | break; |
| 430 | case tok::pp_endif: |
| 431 | parsePPEndIf(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 432 | break; |
| 433 | default: |
| 434 | parsePPUnknown(); |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 439 | void UnwrappedLineParser::pushPPConditional() { |
| 440 | if (!PPStack.empty() && PPStack.back() == PP_Unreachable) |
| 441 | PPStack.push_back(PP_Unreachable); |
| 442 | else |
| 443 | PPStack.push_back(PP_Conditional); |
| 444 | } |
| 445 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 446 | void UnwrappedLineParser::parsePPIf(bool IfDef) { |
| 447 | ++PPBranchLevel; |
| 448 | assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size()); |
| 449 | if (PPBranchLevel == (int)PPLevelBranchIndex.size()) { |
| 450 | PPLevelBranchIndex.push_back(0); |
| 451 | PPLevelBranchCount.push_back(0); |
| 452 | } |
| 453 | PPChainBranchIndex.push(0); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 454 | nextToken(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 455 | bool IsLiteralFalse = (FormatTok->Tok.isLiteral() && |
| 456 | StringRef(FormatTok->Tok.getLiteralData(), |
| 457 | FormatTok->Tok.getLength()) == "0") || |
| 458 | FormatTok->Tok.is(tok::kw_false); |
| 459 | if ((!IfDef && IsLiteralFalse) || PPLevelBranchIndex[PPBranchLevel] > 0) { |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 460 | PPStack.push_back(PP_Unreachable); |
| 461 | } else { |
| 462 | pushPPConditional(); |
| 463 | } |
| 464 | parsePPUnknown(); |
| 465 | } |
| 466 | |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 467 | void UnwrappedLineParser::parsePPElse() { |
| 468 | if (!PPStack.empty()) |
| 469 | PPStack.pop_back(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 470 | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
| 471 | if (!PPChainBranchIndex.empty()) |
| 472 | ++PPChainBranchIndex.top(); |
| 473 | if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty() && |
| 474 | PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()) { |
| 475 | PPStack.push_back(PP_Unreachable); |
| 476 | } else { |
| 477 | pushPPConditional(); |
| 478 | } |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 479 | parsePPUnknown(); |
| 480 | } |
| 481 | |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 482 | void UnwrappedLineParser::parsePPElIf() { parsePPElse(); } |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 483 | |
| 484 | void UnwrappedLineParser::parsePPEndIf() { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 485 | assert(PPBranchLevel < (int)PPLevelBranchIndex.size()); |
| 486 | if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) { |
| 487 | if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) { |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 488 | PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1; |
| 489 | } |
| 490 | } |
| 491 | --PPBranchLevel; |
| 492 | if (!PPChainBranchIndex.empty()) |
| 493 | PPChainBranchIndex.pop(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 494 | if (!PPStack.empty()) |
| 495 | PPStack.pop_back(); |
| 496 | parsePPUnknown(); |
| 497 | } |
| 498 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 499 | void UnwrappedLineParser::parsePPDefine() { |
| 500 | nextToken(); |
| 501 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 502 | if (FormatTok->Tok.getKind() != tok::identifier) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 503 | parsePPUnknown(); |
| 504 | return; |
| 505 | } |
| 506 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 507 | if (FormatTok->Tok.getKind() == tok::l_paren && |
| 508 | FormatTok->WhitespaceRange.getBegin() == |
| 509 | FormatTok->WhitespaceRange.getEnd()) { |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 510 | parseParens(); |
| 511 | } |
| 512 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 513 | Line->Level = 1; |
Manuel Klimek | 1b89629 | 2013-01-07 09:34:28 +0000 | [diff] [blame] | 514 | |
| 515 | // Errors during a preprocessor directive can only affect the layout of the |
| 516 | // preprocessor directive, and thus we ignore them. An alternative approach |
| 517 | // would be to use the same approach we use on the file level (no |
| 518 | // re-indentation if there was a structural error) within the macro |
| 519 | // definition. |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 520 | parseFile(); |
| 521 | } |
| 522 | |
| 523 | void UnwrappedLineParser::parsePPUnknown() { |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 524 | do { |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 525 | nextToken(); |
| 526 | } while (!eof()); |
| 527 | addUnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 530 | // Here we blacklist certain tokens that are not usually the first token in an |
| 531 | // unwrapped line. This is used in attempt to distinguish macro calls without |
| 532 | // trailing semicolons from other constructs split to several lines. |
| 533 | bool tokenCanStartNewLine(clang::Token Tok) { |
| 534 | // Semicolon can be a null-statement, l_square can be a start of a macro or |
| 535 | // a C++11 attribute, but this doesn't seem to be common. |
| 536 | return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) && |
| 537 | Tok.isNot(tok::l_square) && |
| 538 | // Tokens that can only be used as binary operators and a part of |
| 539 | // overloaded operator names. |
| 540 | Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) && |
| 541 | Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) && |
| 542 | Tok.isNot(tok::less) && Tok.isNot(tok::greater) && |
| 543 | Tok.isNot(tok::slash) && Tok.isNot(tok::percent) && |
| 544 | Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) && |
| 545 | Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) && |
| 546 | Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) && |
| 547 | Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) && |
| 548 | Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) && |
| 549 | Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) && |
| 550 | Tok.isNot(tok::lesslessequal) && |
| 551 | // Colon is used in labels, base class lists, initializer lists, |
| 552 | // range-based for loops, ternary operator, but should never be the |
| 553 | // first token in an unwrapped line. |
| 554 | Tok.isNot(tok::colon); |
| 555 | } |
| 556 | |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 557 | void UnwrappedLineParser::parseStructuralElement() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 558 | assert(!FormatTok->Tok.is(tok::l_brace)); |
| 559 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | 04e9f1a | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 560 | case tok::at: |
| 561 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 562 | if (FormatTok->Tok.is(tok::l_brace)) { |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 563 | parseBracedList(); |
| 564 | break; |
| 565 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 566 | switch (FormatTok->Tok.getObjCKeywordID()) { |
Nico Weber | 04e9f1a | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 567 | case tok::objc_public: |
| 568 | case tok::objc_protected: |
| 569 | case tok::objc_package: |
| 570 | case tok::objc_private: |
| 571 | return parseAccessSpecifier(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 572 | case tok::objc_interface: |
Nico Weber | 2ce0ac5 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 573 | case tok::objc_implementation: |
| 574 | return parseObjCInterfaceOrImplementation(); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 575 | case tok::objc_protocol: |
| 576 | return parseObjCProtocol(); |
Nico Weber | d8ffe75 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 577 | case tok::objc_end: |
| 578 | return; // Handled by the caller. |
Nico Weber | 51306d2 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 579 | case tok::objc_optional: |
| 580 | case tok::objc_required: |
| 581 | nextToken(); |
| 582 | addUnwrappedLine(); |
| 583 | return; |
Nico Weber | 04e9f1a | 2013-01-07 19:05:19 +0000 | [diff] [blame] | 584 | default: |
| 585 | break; |
| 586 | } |
| 587 | break; |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 588 | case tok::kw_namespace: |
| 589 | parseNamespace(); |
| 590 | return; |
Dmitri Gribenko | 58d64e2 | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 591 | case tok::kw_inline: |
| 592 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 593 | if (FormatTok->Tok.is(tok::kw_namespace)) { |
Dmitri Gribenko | 58d64e2 | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 594 | parseNamespace(); |
| 595 | return; |
| 596 | } |
| 597 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 598 | case tok::kw_public: |
| 599 | case tok::kw_protected: |
| 600 | case tok::kw_private: |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 601 | parseAccessSpecifier(); |
| 602 | return; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 603 | case tok::kw_if: |
| 604 | parseIfThenElse(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 605 | return; |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 606 | case tok::kw_for: |
| 607 | case tok::kw_while: |
| 608 | parseForOrWhileLoop(); |
| 609 | return; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 610 | case tok::kw_do: |
| 611 | parseDoWhile(); |
| 612 | return; |
| 613 | case tok::kw_switch: |
| 614 | parseSwitch(); |
| 615 | return; |
| 616 | case tok::kw_default: |
| 617 | nextToken(); |
| 618 | parseLabel(); |
| 619 | return; |
| 620 | case tok::kw_case: |
| 621 | parseCaseLabel(); |
| 622 | return; |
Manuel Klimek | 762dd18 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 623 | case tok::kw_return: |
| 624 | parseReturn(); |
| 625 | return; |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 626 | case tok::kw_extern: |
| 627 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 628 | if (FormatTok->Tok.is(tok::string_literal)) { |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 629 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 630 | if (FormatTok->Tok.is(tok::l_brace)) { |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 631 | parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false); |
Manuel Klimek | ae610d1 | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 632 | addUnwrappedLine(); |
| 633 | return; |
| 634 | } |
| 635 | } |
| 636 | // In all other cases, parse the declaration. |
| 637 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 638 | default: |
| 639 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 640 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 641 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 642 | switch (FormatTok->Tok.getKind()) { |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 643 | case tok::at: |
| 644 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 645 | if (FormatTok->Tok.is(tok::l_brace)) |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 646 | parseBracedList(); |
| 647 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 648 | case tok::kw_enum: |
| 649 | parseEnum(); |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 650 | break; |
Alexander Kornienko | 1231e06 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 651 | case tok::kw_struct: |
| 652 | case tok::kw_union: |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 653 | case tok::kw_class: |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 654 | parseRecord(); |
| 655 | // A record declaration or definition is always the start of a structural |
| 656 | // element. |
| 657 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 658 | case tok::semi: |
| 659 | nextToken(); |
| 660 | addUnwrappedLine(); |
| 661 | return; |
Alexander Kornienko | 1231e06 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 662 | case tok::r_brace: |
| 663 | addUnwrappedLine(); |
| 664 | return; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 665 | case tok::l_paren: |
| 666 | parseParens(); |
| 667 | break; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 668 | case tok::caret: |
| 669 | nextToken(); |
| 670 | if (FormatTok->is(tok::l_brace)) { |
| 671 | parseChildBlock(); |
| 672 | } |
| 673 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 674 | case tok::l_brace: |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 675 | if (!tryToParseBracedList()) { |
| 676 | // A block outside of parentheses must be the last part of a |
| 677 | // structural element. |
| 678 | // FIXME: Figure out cases where this is not true, and add projections |
| 679 | // for them (the one we know is missing are lambdas). |
| 680 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 681 | Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup || |
| 682 | Style.BreakBeforeBraces == FormatStyle::BS_Allman) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 683 | addUnwrappedLine(); |
Alexander Kornienko | 3cfa973 | 2013-11-20 16:33:05 +0000 | [diff] [blame] | 684 | FormatTok->Type = TT_FunctionLBrace; |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 685 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 686 | addUnwrappedLine(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 687 | return; |
| 688 | } |
| 689 | // Otherwise this was a braced init list, and the structural |
| 690 | // element continues. |
| 691 | break; |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 692 | case tok::identifier: { |
| 693 | StringRef Text = FormatTok->TokenText; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 694 | nextToken(); |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 695 | if (Line->Tokens.size() == 1) { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 696 | if (FormatTok->Tok.is(tok::colon)) { |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 697 | parseLabel(); |
| 698 | return; |
| 699 | } |
Alexander Kornienko | a04e5e2 | 2013-04-09 16:15:19 +0000 | [diff] [blame] | 700 | // Recognize function-like macro usages without trailing semicolon. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 701 | if (FormatTok->Tok.is(tok::l_paren)) { |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 702 | parseParens(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 703 | if (FormatTok->HasUnescapedNewline && |
| 704 | tokenCanStartNewLine(FormatTok->Tok)) { |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 705 | addUnwrappedLine(); |
| 706 | return; |
| 707 | } |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 708 | } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 && |
| 709 | Text == Text.upper()) { |
| 710 | // Recognize free-standing macros like Q_OBJECT. |
| 711 | addUnwrappedLine(); |
Daniel Jasper | 41a0f78 | 2013-05-29 14:09:17 +0000 | [diff] [blame] | 712 | return; |
Alexander Kornienko | de64427 | 2013-04-08 22:16:06 +0000 | [diff] [blame] | 713 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 714 | } |
| 715 | break; |
Daniel Jasper | 40e1921 | 2013-05-29 13:16:10 +0000 | [diff] [blame] | 716 | } |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 717 | case tok::equal: |
| 718 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 719 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 720 | parseBracedList(); |
| 721 | } |
Daniel Jasper | e25509f | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 722 | break; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 723 | case tok::l_square: |
| 724 | tryToParseLambda(); |
| 725 | break; |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 726 | default: |
| 727 | nextToken(); |
| 728 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 729 | } |
| 730 | } while (!eof()); |
| 731 | } |
| 732 | |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 733 | void UnwrappedLineParser::tryToParseLambda() { |
Daniel Jasper | bf02b2c1 | 2013-09-05 11:49:39 +0000 | [diff] [blame] | 734 | // FIXME: This is a dirty way to access the previous token. Find a better |
| 735 | // solution. |
Daniel Jasper | b4b9998 | 2013-09-06 21:25:51 +0000 | [diff] [blame] | 736 | if (!Line->Tokens.empty() && |
| 737 | Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator)) { |
Daniel Jasper | bf02b2c1 | 2013-09-05 11:49:39 +0000 | [diff] [blame] | 738 | nextToken(); |
| 739 | return; |
| 740 | } |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 741 | assert(FormatTok->is(tok::l_square)); |
| 742 | FormatToken &LSquare = *FormatTok; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 743 | if (!tryToParseLambdaIntroducer()) |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 744 | return; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 745 | |
| 746 | while (FormatTok->isNot(tok::l_brace)) { |
| 747 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 748 | case tok::l_brace: |
| 749 | break; |
| 750 | case tok::l_paren: |
| 751 | parseParens(); |
| 752 | break; |
| 753 | case tok::identifier: |
| 754 | case tok::kw_mutable: |
| 755 | nextToken(); |
| 756 | break; |
| 757 | default: |
| 758 | return; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 761 | LSquare.Type = TT_LambdaLSquare; |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 762 | parseChildBlock(); |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | bool UnwrappedLineParser::tryToParseLambdaIntroducer() { |
| 766 | nextToken(); |
| 767 | if (FormatTok->is(tok::equal)) { |
| 768 | nextToken(); |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 769 | if (FormatTok->is(tok::r_square)) { |
| 770 | nextToken(); |
| 771 | return true; |
| 772 | } |
| 773 | if (FormatTok->isNot(tok::comma)) |
| 774 | return false; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 775 | nextToken(); |
| 776 | } else if (FormatTok->is(tok::amp)) { |
| 777 | nextToken(); |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 778 | if (FormatTok->is(tok::r_square)) { |
| 779 | nextToken(); |
| 780 | return true; |
| 781 | } |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 782 | if (!FormatTok->isOneOf(tok::comma, tok::identifier)) { |
| 783 | return false; |
| 784 | } |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 785 | if (FormatTok->is(tok::comma)) |
| 786 | nextToken(); |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 787 | } else if (FormatTok->is(tok::r_square)) { |
| 788 | nextToken(); |
| 789 | return true; |
| 790 | } |
| 791 | do { |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 792 | if (FormatTok->is(tok::amp)) |
| 793 | nextToken(); |
| 794 | if (!FormatTok->isOneOf(tok::identifier, tok::kw_this)) |
| 795 | return false; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 796 | nextToken(); |
| 797 | if (FormatTok->is(tok::comma)) { |
| 798 | nextToken(); |
| 799 | } else if (FormatTok->is(tok::r_square)) { |
| 800 | nextToken(); |
| 801 | return true; |
| 802 | } else { |
| 803 | return false; |
| 804 | } |
| 805 | } while (!eof()); |
| 806 | return false; |
| 807 | } |
| 808 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 809 | bool UnwrappedLineParser::tryToParseBracedList() { |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 810 | if (FormatTok->BlockKind == BK_Unknown) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 811 | calculateBraceTypes(); |
Daniel Jasper | b1f74a8 | 2013-07-09 09:06:29 +0000 | [diff] [blame] | 812 | assert(FormatTok->BlockKind != BK_Unknown); |
| 813 | if (FormatTok->BlockKind == BK_Block) |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 814 | return false; |
| 815 | parseBracedList(); |
| 816 | return true; |
| 817 | } |
| 818 | |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 819 | bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) { |
| 820 | bool HasError = false; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 821 | nextToken(); |
| 822 | |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 823 | // FIXME: Once we have an expression parser in the UnwrappedLineParser, |
| 824 | // replace this by using parseAssigmentExpression() inside. |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 825 | do { |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 826 | // FIXME: When we start to support lambdas, we'll want to parse them away |
| 827 | // here, otherwise our bail-out scenarios below break. The better solution |
| 828 | // might be to just implement a more or less complete expression parser. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 829 | switch (FormatTok->Tok.getKind()) { |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 830 | case tok::caret: |
| 831 | nextToken(); |
| 832 | if (FormatTok->is(tok::l_brace)) { |
| 833 | parseChildBlock(); |
| 834 | } |
| 835 | break; |
| 836 | case tok::l_square: |
| 837 | tryToParseLambda(); |
| 838 | break; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 839 | case tok::l_brace: |
Manuel Klimek | bab25fd | 2013-09-04 08:20:47 +0000 | [diff] [blame] | 840 | // Assume there are no blocks inside a braced init list apart |
| 841 | // from the ones we explicitly parse out (like lambdas). |
| 842 | FormatTok->BlockKind = BK_BracedInit; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 843 | parseBracedList(); |
| 844 | break; |
| 845 | case tok::r_brace: |
| 846 | nextToken(); |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 847 | return !HasError; |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 848 | case tok::semi: |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 849 | HasError = true; |
| 850 | if (!ContinueOnSemicolons) |
| 851 | return !HasError; |
| 852 | nextToken(); |
| 853 | break; |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 854 | case tok::comma: |
| 855 | nextToken(); |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 856 | break; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 857 | default: |
| 858 | nextToken(); |
| 859 | break; |
| 860 | } |
| 861 | } while (!eof()); |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 862 | return false; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Manuel Klimek | 762dd18 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 865 | void UnwrappedLineParser::parseReturn() { |
| 866 | nextToken(); |
| 867 | |
| 868 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 869 | switch (FormatTok->Tok.getKind()) { |
Manuel Klimek | 762dd18 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 870 | case tok::l_brace: |
| 871 | parseBracedList(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 872 | if (FormatTok->Tok.isNot(tok::semi)) { |
Manuel Klimek | a3ff45e | 2013-04-10 09:52:05 +0000 | [diff] [blame] | 873 | // Assume missing ';'. |
| 874 | addUnwrappedLine(); |
| 875 | return; |
| 876 | } |
Manuel Klimek | 762dd18 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 877 | break; |
| 878 | case tok::l_paren: |
| 879 | parseParens(); |
| 880 | break; |
| 881 | case tok::r_brace: |
| 882 | // Assume missing ';'. |
| 883 | addUnwrappedLine(); |
| 884 | return; |
| 885 | case tok::semi: |
| 886 | nextToken(); |
| 887 | addUnwrappedLine(); |
| 888 | return; |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 889 | case tok::l_square: |
| 890 | tryToParseLambda(); |
| 891 | break; |
Manuel Klimek | 762dd18 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 892 | default: |
| 893 | nextToken(); |
| 894 | break; |
| 895 | } |
| 896 | } while (!eof()); |
| 897 | } |
| 898 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 899 | void UnwrappedLineParser::parseParens() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 900 | assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected."); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 901 | nextToken(); |
| 902 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 903 | switch (FormatTok->Tok.getKind()) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 904 | case tok::l_paren: |
| 905 | parseParens(); |
| 906 | break; |
| 907 | case tok::r_paren: |
| 908 | nextToken(); |
| 909 | return; |
Daniel Jasper | 393564f | 2013-05-31 14:56:29 +0000 | [diff] [blame] | 910 | case tok::r_brace: |
| 911 | // A "}" inside parenthesis is an error if there wasn't a matching "{". |
| 912 | return; |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 913 | case tok::l_square: |
| 914 | tryToParseLambda(); |
| 915 | break; |
Nico Weber | 67ffb33 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 916 | case tok::l_brace: { |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 917 | if (!tryToParseBracedList()) { |
Manuel Klimek | f017dc0 | 2013-09-04 13:34:14 +0000 | [diff] [blame] | 918 | parseChildBlock(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 919 | } |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 920 | break; |
Nico Weber | 67ffb33 | 2013-02-10 04:38:23 +0000 | [diff] [blame] | 921 | } |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 922 | case tok::at: |
| 923 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 924 | if (FormatTok->Tok.is(tok::l_brace)) |
Nico Weber | 372d8dc | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 925 | parseBracedList(); |
| 926 | break; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 927 | default: |
| 928 | nextToken(); |
| 929 | break; |
| 930 | } |
| 931 | } while (!eof()); |
| 932 | } |
| 933 | |
| 934 | void UnwrappedLineParser::parseIfThenElse() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 935 | assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 936 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 937 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | adededf | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 938 | parseParens(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 939 | bool NeedsUnwrappedLine = false; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 940 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 941 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) |
| 942 | addUnwrappedLine(); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 943 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 944 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) |
| 945 | addUnwrappedLine(); |
| 946 | else |
| 947 | NeedsUnwrappedLine = true; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 948 | } else { |
| 949 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 950 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 951 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 952 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 953 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 954 | if (FormatTok->Tok.is(tok::kw_else)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 955 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 956 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 957 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) |
| 958 | addUnwrappedLine(); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 959 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 960 | addUnwrappedLine(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 961 | } else if (FormatTok->Tok.is(tok::kw_if)) { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 962 | parseIfThenElse(); |
| 963 | } else { |
| 964 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 965 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 966 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 967 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 968 | } |
| 969 | } else if (NeedsUnwrappedLine) { |
| 970 | addUnwrappedLine(); |
| 971 | } |
| 972 | } |
| 973 | |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 974 | void UnwrappedLineParser::parseNamespace() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 975 | assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected"); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 976 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 977 | if (FormatTok->Tok.is(tok::identifier)) |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 978 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 979 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 980 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || |
| 981 | Style.BreakBeforeBraces == FormatStyle::BS_Allman) |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 982 | addUnwrappedLine(); |
| 983 | |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 984 | bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All || |
| 985 | (Style.NamespaceIndentation == FormatStyle::NI_Inner && |
| 986 | DeclarationScopeStack.size() > 1); |
| 987 | parseBlock(/*MustBeDeclaration=*/true, AddLevel); |
Manuel Klimek | 046b930 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 988 | // Munch the semicolon after a namespace. This is more common than one would |
| 989 | // think. Puttin the semicolon into its own line is very ugly. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 990 | if (FormatTok->Tok.is(tok::semi)) |
Manuel Klimek | 046b930 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 991 | nextToken(); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 992 | addUnwrappedLine(); |
| 993 | } |
| 994 | // FIXME: Add error handling. |
| 995 | } |
| 996 | |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 997 | void UnwrappedLineParser::parseForOrWhileLoop() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 998 | assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) && |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 999 | "'for' or 'while' expected"); |
| 1000 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1001 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 9fa8d55 | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1002 | parseParens(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1003 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1004 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) |
| 1005 | addUnwrappedLine(); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1006 | parseBlock(/*MustBeDeclaration=*/false); |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1007 | addUnwrappedLine(); |
| 1008 | } else { |
| 1009 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1010 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1011 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1012 | --Line->Level; |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 1013 | } |
| 1014 | } |
| 1015 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1016 | void UnwrappedLineParser::parseDoWhile() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1017 | assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1018 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1019 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1020 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) |
| 1021 | addUnwrappedLine(); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1022 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1023 | } else { |
| 1024 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1025 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1026 | parseStructuralElement(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1027 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1030 | // FIXME: Add error handling. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1031 | if (!FormatTok->Tok.is(tok::kw_while)) { |
Alexander Kornienko | 0ea8e10 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1032 | addUnwrappedLine(); |
| 1033 | return; |
| 1034 | } |
| 1035 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1036 | nextToken(); |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1037 | parseStructuralElement(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | void UnwrappedLineParser::parseLabel() { |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1041 | nextToken(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1042 | unsigned OldLineLevel = Line->Level; |
Daniel Jasper | a127512 | 2013-03-20 10:23:53 +0000 | [diff] [blame] | 1043 | if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0)) |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1044 | --Line->Level; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1045 | if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1046 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) |
| 1047 | addUnwrappedLine(); |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1048 | parseBlock(/*MustBeDeclaration=*/false); |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1049 | if (FormatTok->Tok.is(tok::kw_break)) { |
| 1050 | // "break;" after "}" on its own line only for BS_Allman |
| 1051 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) |
| 1052 | addUnwrappedLine(); |
| 1053 | parseStructuralElement(); |
| 1054 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1055 | } |
| 1056 | addUnwrappedLine(); |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1057 | Line->Level = OldLineLevel; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | void UnwrappedLineParser::parseCaseLabel() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1061 | assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1062 | // FIXME: fix handling of complex expressions here. |
| 1063 | do { |
| 1064 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1065 | } while (!eof() && !FormatTok->Tok.is(tok::colon)); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1066 | parseLabel(); |
| 1067 | } |
| 1068 | |
| 1069 | void UnwrappedLineParser::parseSwitch() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1070 | assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected"); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1071 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1072 | if (FormatTok->Tok.is(tok::l_paren)) |
Manuel Klimek | 9fa8d55 | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1073 | parseParens(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1074 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1075 | if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) |
| 1076 | addUnwrappedLine(); |
Daniel Jasper | 65ee347 | 2013-07-31 23:16:02 +0000 | [diff] [blame] | 1077 | parseBlock(/*MustBeDeclaration=*/false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1078 | addUnwrappedLine(); |
| 1079 | } else { |
| 1080 | addUnwrappedLine(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 1081 | ++Line->Level; |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 1082 | parseStructuralElement(); |
Daniel Jasper | 516d797 | 2013-07-25 11:31:57 +0000 | [diff] [blame] | 1083 | --Line->Level; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | void UnwrappedLineParser::parseAccessSpecifier() { |
| 1088 | nextToken(); |
Daniel Jasper | 84c47a1 | 2013-11-23 17:53:41 +0000 | [diff] [blame^] | 1089 | // Understand Qt's slots. |
| 1090 | if (FormatTok->is(tok::identifier) && FormatTok->TokenText == "slots") |
| 1091 | nextToken(); |
Alexander Kornienko | 2ca766f | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1092 | // Otherwise, we don't know what it is, and we'd better keep the next token. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1093 | if (FormatTok->Tok.is(tok::colon)) |
Alexander Kornienko | 2ca766f | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1094 | nextToken(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1095 | addUnwrappedLine(); |
| 1096 | } |
| 1097 | |
| 1098 | void UnwrappedLineParser::parseEnum() { |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1099 | nextToken(); |
Daniel Jasper | 2b41a82 | 2013-08-20 12:42:50 +0000 | [diff] [blame] | 1100 | // Eat up enum class ... |
| 1101 | if (FormatTok->Tok.is(tok::kw_class) || |
| 1102 | FormatTok->Tok.is(tok::kw_struct)) |
| 1103 | nextToken(); |
Daniel Jasper | 786a550 | 2013-09-06 21:32:35 +0000 | [diff] [blame] | 1104 | while (FormatTok->Tok.getIdentifierInfo() || |
| 1105 | FormatTok->isOneOf(tok::colon, tok::coloncolon)) { |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1106 | nextToken(); |
| 1107 | // We can have macros or attributes in between 'enum' and the enum name. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1108 | if (FormatTok->Tok.is(tok::l_paren)) { |
Alexander Kornienko | b7076a2 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 1109 | parseParens(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1110 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1111 | if (FormatTok->Tok.is(tok::identifier)) |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1112 | nextToken(); |
| 1113 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1114 | if (FormatTok->Tok.is(tok::l_brace)) { |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1115 | FormatTok->BlockKind = BK_Block; |
| 1116 | bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true); |
| 1117 | if (HasError) { |
| 1118 | if (FormatTok->is(tok::semi)) |
| 1119 | nextToken(); |
Manuel Klimek | a027f30 | 2013-08-07 19:20:45 +0000 | [diff] [blame] | 1120 | addUnwrappedLine(); |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 1121 | } |
Manuel Klimek | 2cec019 | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 1122 | } |
| 1123 | // We fall through to parsing a structural element afterwards, so that in |
| 1124 | // enum A {} n, m; |
| 1125 | // "} n, m;" will end up in one unwrapped line. |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1128 | void UnwrappedLineParser::parseRecord() { |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1129 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1130 | if (FormatTok->Tok.is(tok::identifier) || |
| 1131 | FormatTok->Tok.is(tok::kw___attribute) || |
Manuel Klimek | 91e4858 | 2013-10-07 09:15:41 +0000 | [diff] [blame] | 1132 | FormatTok->Tok.is(tok::kw___declspec) || |
| 1133 | FormatTok->Tok.is(tok::kw_alignas)) { |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1134 | nextToken(); |
| 1135 | // We can have macros or attributes in between 'class' and the class name. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1136 | if (FormatTok->Tok.is(tok::l_paren)) { |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1137 | parseParens(); |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1138 | } |
Manuel Klimek | d265090 | 2013-02-06 15:57:54 +0000 | [diff] [blame] | 1139 | // The actual identifier can be a nested name specifier, and in macros |
| 1140 | // it is often token-pasted. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1141 | while (FormatTok->Tok.is(tok::identifier) || |
| 1142 | FormatTok->Tok.is(tok::coloncolon) || |
| 1143 | FormatTok->Tok.is(tok::hashhash)) |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1144 | nextToken(); |
| 1145 | |
Manuel Klimek | cdee74d | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1146 | // Note that parsing away template declarations here leads to incorrectly |
| 1147 | // accepting function declarations as record declarations. |
| 1148 | // In general, we cannot solve this problem. Consider: |
| 1149 | // class A<int> B() {} |
| 1150 | // which can be a function definition or a class definition when B() is a |
| 1151 | // macro. If we find enough real-world cases where this is a problem, we |
| 1152 | // can parse for the 'template' keyword in the beginning of the statement, |
| 1153 | // and thus rule out the record production in case there is no template |
| 1154 | // (this would still leave us with an ambiguity between template function |
| 1155 | // and class declarations). |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1156 | if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) { |
| 1157 | while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) { |
| 1158 | if (FormatTok->Tok.is(tok::semi)) |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1159 | return; |
| 1160 | nextToken(); |
| 1161 | } |
| 1162 | } |
| 1163 | } |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1164 | if (FormatTok->Tok.is(tok::l_brace)) { |
Manuel Klimek | d3ed59a | 2013-08-02 21:31:59 +0000 | [diff] [blame] | 1165 | if (Style.BreakBeforeBraces == FormatStyle::BS_Linux || |
| 1166 | Style.BreakBeforeBraces == FormatStyle::BS_Allman) |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1167 | addUnwrappedLine(); |
| 1168 | |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 1169 | parseBlock(/*MustBeDeclaration=*/true, /*Addlevel=*/true, |
| 1170 | /*MunchSemi=*/false); |
Manuel Klimek | a8eb914 | 2013-05-13 12:51:40 +0000 | [diff] [blame] | 1171 | } |
Manuel Klimek | cdee74d | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1172 | // We fall through to parsing a structural element afterwards, so |
| 1173 | // class A {} n, m; |
| 1174 | // will end up in one unwrapped line. |
Manuel Klimek | 28cacc7 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1177 | void UnwrappedLineParser::parseObjCProtocolList() { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1178 | assert(FormatTok->Tok.is(tok::less) && "'<' expected."); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1179 | do |
| 1180 | nextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1181 | while (!eof() && FormatTok->Tok.isNot(tok::greater)); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1182 | nextToken(); // Skip '>'. |
| 1183 | } |
| 1184 | |
| 1185 | void UnwrappedLineParser::parseObjCUntilAtEnd() { |
| 1186 | do { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1187 | if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) { |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1188 | nextToken(); |
| 1189 | addUnwrappedLine(); |
| 1190 | break; |
| 1191 | } |
Daniel Jasper | a15da30 | 2013-08-28 08:04:23 +0000 | [diff] [blame] | 1192 | if (FormatTok->is(tok::l_brace)) { |
| 1193 | parseBlock(/*MustBeDeclaration=*/false); |
| 1194 | // In ObjC interfaces, nothing should be following the "}". |
| 1195 | addUnwrappedLine(); |
| 1196 | } else { |
| 1197 | parseStructuralElement(); |
| 1198 | } |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1199 | } while (!eof()); |
| 1200 | } |
| 1201 | |
Nico Weber | 2ce0ac5 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1202 | void UnwrappedLineParser::parseObjCInterfaceOrImplementation() { |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1203 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1204 | nextToken(); // interface name |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1205 | |
| 1206 | // @interface can be followed by either a base class, or a category. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1207 | if (FormatTok->Tok.is(tok::colon)) { |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1208 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1209 | nextToken(); // base class name |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1210 | } else if (FormatTok->Tok.is(tok::l_paren)) |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1211 | // Skip category, if present. |
| 1212 | parseParens(); |
| 1213 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1214 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1215 | parseObjCProtocolList(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1216 | |
| 1217 | // If instance variables are present, keep the '{' on the first line too. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1218 | if (FormatTok->Tok.is(tok::l_brace)) |
Nico Weber | 9096fc0 | 2013-06-26 00:30:14 +0000 | [diff] [blame] | 1219 | parseBlock(/*MustBeDeclaration=*/true); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1220 | |
| 1221 | // With instance variables, this puts '}' on its own line. Without instance |
| 1222 | // variables, this ends the @interface line. |
| 1223 | addUnwrappedLine(); |
| 1224 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1225 | parseObjCUntilAtEnd(); |
| 1226 | } |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1227 | |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1228 | void UnwrappedLineParser::parseObjCProtocol() { |
| 1229 | nextToken(); |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1230 | nextToken(); // protocol name |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1231 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1232 | if (FormatTok->Tok.is(tok::less)) |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1233 | parseObjCProtocolList(); |
| 1234 | |
| 1235 | // Check for protocol declaration. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1236 | if (FormatTok->Tok.is(tok::semi)) { |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1237 | nextToken(); |
| 1238 | return addUnwrappedLine(); |
| 1239 | } |
| 1240 | |
| 1241 | addUnwrappedLine(); |
| 1242 | parseObjCUntilAtEnd(); |
Nico Weber | 7eecf4b | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Daniel Jasper | 3b203a6 | 2013-09-05 16:05:56 +0000 | [diff] [blame] | 1245 | LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line, |
| 1246 | StringRef Prefix = "") { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1247 | llvm::dbgs() << Prefix << "Line(" << Line.Level << ")" |
| 1248 | << (Line.InPPDirective ? " MACRO" : "") << ": "; |
| 1249 | for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), |
| 1250 | E = Line.Tokens.end(); |
| 1251 | I != E; ++I) { |
Daniel Jasper | 9a8d48b | 2013-09-05 10:04:31 +0000 | [diff] [blame] | 1252 | llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] "; |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1253 | } |
| 1254 | for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(), |
| 1255 | E = Line.Tokens.end(); |
| 1256 | I != E; ++I) { |
| 1257 | const UnwrappedLineNode &Node = *I; |
| 1258 | for (SmallVectorImpl<UnwrappedLine>::const_iterator |
| 1259 | I = Node.Children.begin(), |
| 1260 | E = Node.Children.end(); |
| 1261 | I != E; ++I) { |
| 1262 | printDebugInfo(*I, "\nChild: "); |
| 1263 | } |
| 1264 | } |
| 1265 | llvm::dbgs() << "\n"; |
| 1266 | } |
| 1267 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1268 | void UnwrappedLineParser::addUnwrappedLine() { |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1269 | if (Line->Tokens.empty()) |
Daniel Jasper | 7c85fde | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1270 | return; |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1271 | DEBUG({ |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1272 | if (CurrentLines == &Lines) |
| 1273 | printDebugInfo(*Line); |
Manuel Klimek | ab3dc00 | 2013-01-16 12:31:12 +0000 | [diff] [blame] | 1274 | }); |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1275 | CurrentLines->push_back(*Line); |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 1276 | Line->Tokens.clear(); |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1277 | if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1278 | for (SmallVectorImpl<UnwrappedLine>::iterator |
Daniel Jasper | a79064a | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 1279 | I = PreprocessorDirectives.begin(), |
| 1280 | E = PreprocessorDirectives.end(); |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1281 | I != E; ++I) { |
| 1282 | CurrentLines->push_back(*I); |
| 1283 | } |
| 1284 | PreprocessorDirectives.clear(); |
| 1285 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1288 | bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1289 | |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1290 | void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) { |
| 1291 | bool JustComments = Line->Tokens.empty(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1292 | for (SmallVectorImpl<FormatToken *>::const_iterator |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1293 | I = CommentsBeforeNextToken.begin(), |
| 1294 | E = CommentsBeforeNextToken.end(); |
| 1295 | I != E; ++I) { |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1296 | if ((*I)->NewlinesBefore && JustComments) { |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1297 | addUnwrappedLine(); |
| 1298 | } |
| 1299 | pushToken(*I); |
| 1300 | } |
| 1301 | if (NewlineBeforeNext && JustComments) { |
| 1302 | addUnwrappedLine(); |
| 1303 | } |
| 1304 | CommentsBeforeNextToken.clear(); |
| 1305 | } |
| 1306 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1307 | void UnwrappedLineParser::nextToken() { |
| 1308 | if (eof()) |
| 1309 | return; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1310 | flushComments(FormatTok->NewlinesBefore > 0); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1311 | pushToken(FormatTok); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1312 | readToken(); |
| 1313 | } |
| 1314 | |
| 1315 | void UnwrappedLineParser::readToken() { |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1316 | bool CommentsInCurrentLine = true; |
| 1317 | do { |
| 1318 | FormatTok = Tokens->getNextToken(); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1319 | while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) && |
| 1320 | (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) { |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1321 | // If there is an unfinished unwrapped line, we flush the preprocessor |
| 1322 | // directives only after that unwrapped line was finished later. |
Daniel Jasper | d1ae358 | 2013-03-20 12:37:50 +0000 | [diff] [blame] | 1323 | bool SwitchToPreprocessorLines = |
| 1324 | !Line->Tokens.empty() && CurrentLines == &Lines; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1325 | ScopedLineState BlockState(*this, SwitchToPreprocessorLines); |
Alexander Kornienko | b1be9d6 | 2013-04-03 12:38:53 +0000 | [diff] [blame] | 1326 | // Comments stored before the preprocessor directive need to be output |
| 1327 | // before the preprocessor directive, at the same level as the |
| 1328 | // preprocessor directive, as we consider them to apply to the directive. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1329 | flushComments(FormatTok->NewlinesBefore > 0); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1330 | parsePPDirective(); |
| 1331 | } |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 1332 | |
| 1333 | if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) && |
| 1334 | !Line->InPPDirective) { |
| 1335 | continue; |
| 1336 | } |
| 1337 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1338 | if (!FormatTok->Tok.is(tok::comment)) |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1339 | return; |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1340 | if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) { |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1341 | CommentsInCurrentLine = false; |
| 1342 | } |
| 1343 | if (CommentsInCurrentLine) { |
| 1344 | pushToken(FormatTok); |
| 1345 | } else { |
| 1346 | CommentsBeforeNextToken.push_back(FormatTok); |
| 1347 | } |
| 1348 | } while (!eof()); |
| 1349 | } |
| 1350 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 1351 | void UnwrappedLineParser::pushToken(FormatToken *Tok) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1352 | Line->Tokens.push_back(UnwrappedLineNode(Tok)); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1353 | if (MustBreakBeforeNextToken) { |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 1354 | Line->Tokens.back().Tok->MustBreakBefore = true; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 1355 | MustBreakBeforeNextToken = false; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1356 | } |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Daniel Jasper | 8d1832e | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1359 | } // end namespace format |
| 1360 | } // end namespace clang |