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