blob: b9b071bca8d394926333f53c5a2fc010ac7b241a [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- 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 Jasperf7935112012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Chandler Carruth4b417452013-01-19 08:09:44 +000016#include "UnwrappedLineParser.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000017#include "llvm/Support/Debug.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000018
Chandler Carruth10346662014-04-22 03:17:02 +000019#define DEBUG_TYPE "format-parser"
20
Daniel Jasperf7935112012-12-03 18:12:45 +000021namespace clang {
22namespace format {
23
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000024class FormatTokenSource {
25public:
26 virtual ~FormatTokenSource() {}
27 virtual FormatToken *getNextToken() = 0;
28
29 virtual unsigned getPosition() = 0;
30 virtual FormatToken *setPosition(unsigned Position) = 0;
31};
32
Craig Topper69665e12013-07-01 04:21:54 +000033namespace {
34
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000035class ScopedDeclarationState {
36public:
37 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
38 bool MustBeDeclaration)
39 : Line(Line), Stack(Stack) {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000040 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek39080572013-01-23 11:03:04 +000041 Stack.push_back(MustBeDeclaration);
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000042 }
43 ~ScopedDeclarationState() {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000044 Stack.pop_back();
Manuel Klimekc1237a82013-01-23 14:08:21 +000045 if (!Stack.empty())
46 Line.MustBeDeclaration = Stack.back();
47 else
48 Line.MustBeDeclaration = true;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000049 }
Daniel Jasper393564f2013-05-31 14:56:29 +000050
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000051private:
52 UnwrappedLine &Line;
53 std::vector<bool> &Stack;
54};
55
Manuel Klimek1abf7892013-01-04 23:34:14 +000056class ScopedMacroState : public FormatTokenSource {
57public:
58 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000059 FormatToken *&ResetToken, bool &StructuralError)
Manuel Klimek1abf7892013-01-04 23:34:14 +000060 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000061 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
62 StructuralError(StructuralError),
Craig Topper2145bc02014-05-09 08:15:10 +000063 PreviousStructuralError(StructuralError), Token(nullptr) {
Manuel Klimek1abf7892013-01-04 23:34:14 +000064 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000065 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000066 Line.InPPDirective = true;
67 }
68
69 ~ScopedMacroState() {
70 TokenSource = PreviousTokenSource;
71 ResetToken = Token;
72 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000073 Line.Level = PreviousLineLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +000074 StructuralError = PreviousStructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +000075 }
76
Craig Topperfb6b25b2014-03-15 04:29:04 +000077 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +000078 // The \c UnwrappedLineParser guards against this by never calling
79 // \c getNextToken() after it has encountered the first eof token.
80 assert(!eof());
Manuel Klimek1abf7892013-01-04 23:34:14 +000081 Token = PreviousTokenSource->getNextToken();
82 if (eof())
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000083 return getFakeEOF();
Manuel Klimek1abf7892013-01-04 23:34:14 +000084 return Token;
85 }
86
Craig Topperfb6b25b2014-03-15 04:29:04 +000087 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +000088
Craig Topperfb6b25b2014-03-15 04:29:04 +000089 FormatToken *setPosition(unsigned Position) override {
Manuel Klimekab419912013-05-23 09:41:43 +000090 Token = PreviousTokenSource->setPosition(Position);
91 return Token;
92 }
93
Manuel Klimek1abf7892013-01-04 23:34:14 +000094private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000095 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimek1abf7892013-01-04 23:34:14 +000096
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000097 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 Klimek1abf7892013-01-04 23:34:14 +0000106 }
107
108 UnwrappedLine &Line;
109 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000110 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000111 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000112 FormatTokenSource *PreviousTokenSource;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000113 bool &StructuralError;
114 bool PreviousStructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000115
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000116 FormatToken *Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000117};
118
Craig Topper69665e12013-07-01 04:21:54 +0000119} // end anonymous namespace
120
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000121class ScopedLineState {
122public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000123 ScopedLineState(UnwrappedLineParser &Parser,
124 bool SwitchToPreprocessorLines = false)
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000125 : Parser(Parser) {
126 OriginalLines = Parser.CurrentLines;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000127 if (SwitchToPreprocessorLines)
128 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000129 else if (!Parser.Line->Tokens.empty())
130 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000131 PreBlockLine = Parser.Line.release();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000132 Parser.Line.reset(new UnwrappedLine());
133 Parser.Line->Level = PreBlockLine->Level;
134 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000135 }
136
137 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000138 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000139 Parser.addUnwrappedLine();
140 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000141 assert(Parser.Line->Tokens.empty());
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000142 Parser.Line.reset(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000143 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
144 Parser.MustBreakBeforeNextToken = true;
145 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000146 }
147
148private:
149 UnwrappedLineParser &Parser;
150
151 UnwrappedLine *PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000152 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000153};
154
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000155class CompoundStatementIndenter {
156public:
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
171private:
172 unsigned &LineLevel;
173 unsigned OldLineLevel;
174};
175
Craig Topper69665e12013-07-01 04:21:54 +0000176namespace {
177
Manuel Klimekab419912013-05-23 09:41:43 +0000178class IndexedTokenSource : public FormatTokenSource {
179public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000180 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000181 : Tokens(Tokens), Position(-1) {}
182
Craig Topperfb6b25b2014-03-15 04:29:04 +0000183 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000184 ++Position;
185 return Tokens[Position];
186 }
187
Craig Topperfb6b25b2014-03-15 04:29:04 +0000188 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000189 assert(Position >= 0);
190 return Position;
191 }
192
Craig Topperfb6b25b2014-03-15 04:29:04 +0000193 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000194 Position = P;
195 return Tokens[Position];
196 }
197
Manuel Klimek71814b42013-10-11 21:25:45 +0000198 void reset() { Position = -1; }
199
Manuel Klimekab419912013-05-23 09:41:43 +0000200private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000201 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000202 int Position;
203};
204
Craig Topper69665e12013-07-01 04:21:54 +0000205} // end anonymous namespace
206
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000207UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000208 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000209 UnwrappedLineConsumer &Callback)
Craig Topper2145bc02014-05-09 08:15:10 +0000210 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
211 CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(nullptr),
212 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000213
214void UnwrappedLineParser::reset() {
215 PPBranchLevel = -1;
216 Line.reset(new UnwrappedLine);
217 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000218 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000219 MustBreakBeforeNextToken = false;
220 PreprocessorDirectives.clear();
221 CurrentLines = &Lines;
222 DeclarationScopeStack.clear();
223 StructuralError = false;
224 PPStack.clear();
225}
Daniel Jasperf7935112012-12-03 18:12:45 +0000226
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000227bool UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000228 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimek71814b42013-10-11 21:25:45 +0000229 do {
230 DEBUG(llvm::dbgs() << "----\n");
231 reset();
232 Tokens = &TokenSource;
233 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000234
Manuel Klimek71814b42013-10-11 21:25:45 +0000235 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 Jasper53bd1672013-10-12 13:32:56 +0000249 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000250 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 Klimek1a18c402013-04-12 14:13:36 +0000260 return StructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000261}
262
Manuel Klimek1a18c402013-04-12 14:13:36 +0000263void UnwrappedLineParser::parseFile() {
Daniel Jasperdd9276e2013-03-22 16:55:40 +0000264 ScopedDeclarationState DeclarationState(
265 *Line, DeclarationScopeStack,
266 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber9096fc02013-06-26 00:30:14 +0000267 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000268 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000269 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000270 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000271}
272
Manuel Klimek1a18c402013-04-12 14:13:36 +0000273void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000274 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000275 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000276 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000277 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000278 nextToken();
279 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000280 break;
281 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000282 // FIXME: Add parameter whether this can happen - if this happens, we must
283 // be in a non-declaration context.
Nico Weber9096fc02013-06-26 00:30:14 +0000284 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000285 addUnwrappedLine();
286 break;
287 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000288 if (HasOpeningBrace)
289 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000290 StructuralError = true;
291 nextToken();
292 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000293 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000294 case tok::kw_default:
295 case tok::kw_case:
Daniel Jasper72407622013-09-02 08:26:29 +0000296 if (!SwitchLabelEncountered &&
297 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
298 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000299 SwitchLabelEncountered = true;
300 parseStructuralElement();
301 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000302 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000303 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000304 break;
305 }
306 } while (!eof());
307}
308
Manuel Klimekab419912013-05-23 09:41:43 +0000309void 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 Klimek15dfe7a2013-05-28 11:55:06 +0000316 FormatToken *Tok = FormatTok;
Manuel Klimekab419912013-05-23 09:41:43 +0000317 // 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 Jasperb1f74a82013-07-09 09:06:29 +0000320 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000321 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000322 do {
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000323 // Get next none-comment token.
324 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000325 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000326 do {
327 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000328 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000329 } while (NextTok->is(tok::comment));
330
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000331 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000332 case tok::l_brace:
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000333 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000334 break;
335 case tok::r_brace:
336 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000337 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Daniel Jasper220c0d12014-04-10 07:27:12 +0000338 bool ProbablyBracedList = false;
339 if (Style.Language == FormatStyle::LK_Proto) {
340 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
341 } else {
342 // If there is a comma, semicolon or right paren after the closing
343 // brace, we assume this is a braced initializer list. Note that
344 // regardless how we mark inner braces here, we will overwrite the
345 // BlockKind later if we parse a braced list (where all blocks
346 // inside are by default braced lists), or when we explicitly detect
347 // blocks (for example while parsing lambdas).
348 //
349 // We exclude + and - as they can be ObjC visibility modifiers.
350 ProbablyBracedList =
351 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
352 tok::r_paren, tok::r_square, tok::l_brace) ||
353 (NextTok->isBinaryOperator() &&
354 !NextTok->isOneOf(tok::plus, tok::minus));
355 }
356 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000357 Tok->BlockKind = BK_BracedInit;
358 LBraceStack.back()->BlockKind = BK_BracedInit;
359 } else {
360 Tok->BlockKind = BK_Block;
361 LBraceStack.back()->BlockKind = BK_Block;
362 }
Manuel Klimekab419912013-05-23 09:41:43 +0000363 }
364 LBraceStack.pop_back();
365 }
366 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000367 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000368 case tok::semi:
369 case tok::kw_if:
370 case tok::kw_while:
371 case tok::kw_for:
372 case tok::kw_switch:
373 case tok::kw_try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000374 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000375 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000376 break;
377 default:
378 break;
379 }
380 Tok = NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000381 Position += ReadTokens;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000382 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000383 // Assume other blocks for all unclosed opening braces.
384 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000385 if (LBraceStack[i]->BlockKind == BK_Unknown)
386 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000387 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000388
Manuel Klimekab419912013-05-23 09:41:43 +0000389 FormatTok = Tokens->setPosition(StoredPosition);
390}
391
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000392void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
393 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000394 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000395 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000396 nextToken();
397
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000398 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000399
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000400 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
401 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000402 if (AddLevel)
403 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000404 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000405
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000406 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000407 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000408 StructuralError = true;
409 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000410 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000411
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000412 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000413 if (MunchSemi && FormatTok->Tok.is(tok::semi))
414 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000415 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000416}
417
Daniel Jasper4a39c842014-05-06 13:54:10 +0000418static bool IsGoogScope(const UnwrappedLine &Line) {
419 if (Line.Tokens.size() < 4)
420 return false;
421 auto I = Line.Tokens.begin();
422 if (I->Tok->TokenText != "goog")
423 return false;
424 ++I;
425 if (I->Tok->isNot(tok::period))
426 return false;
427 ++I;
428 if (I->Tok->TokenText != "scope")
429 return false;
430 ++I;
431 return I->Tok->is(tok::l_paren);
432}
433
Manuel Klimek516e0542013-09-04 13:25:30 +0000434void UnwrappedLineParser::parseChildBlock() {
435 FormatTok->BlockKind = BK_Block;
436 nextToken();
437 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000438 bool GoogScope =
439 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000440 ScopedLineState LineState(*this);
441 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
442 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000443 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000444 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000445 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000446 }
447 nextToken();
448}
449
Daniel Jasperf7935112012-12-03 18:12:45 +0000450void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000451 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000452 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000453 nextToken();
454
Craig Topper2145bc02014-05-09 08:15:10 +0000455 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000456 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000457 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000458 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000459
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000460 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000461 case tok::pp_define:
462 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000463 return;
464 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000465 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000466 break;
467 case tok::pp_ifdef:
468 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000469 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000470 break;
471 case tok::pp_else:
472 parsePPElse();
473 break;
474 case tok::pp_elif:
475 parsePPElIf();
476 break;
477 case tok::pp_endif:
478 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000479 break;
480 default:
481 parsePPUnknown();
482 break;
483 }
484}
485
Manuel Klimek68b03042014-04-14 09:14:11 +0000486void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
487 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000488 PPStack.push_back(PP_Unreachable);
489 else
490 PPStack.push_back(PP_Conditional);
491}
492
Manuel Klimek68b03042014-04-14 09:14:11 +0000493void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000494 ++PPBranchLevel;
495 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
496 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
497 PPLevelBranchIndex.push_back(0);
498 PPLevelBranchCount.push_back(0);
499 }
500 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000501 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
502 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000503}
504
Manuel Klimek68b03042014-04-14 09:14:11 +0000505void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000506 if (!PPStack.empty())
507 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000508 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
509 if (!PPChainBranchIndex.empty())
510 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000511 conditionalCompilationCondition(
512 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
513 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000514}
515
Manuel Klimek68b03042014-04-14 09:14:11 +0000516void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000517 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
518 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
519 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000520 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
521 }
522 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000523 // Guard against #endif's without #if.
524 if (PPBranchLevel > 0)
525 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000526 if (!PPChainBranchIndex.empty())
527 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000528 if (!PPStack.empty())
529 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000530}
531
532void UnwrappedLineParser::parsePPIf(bool IfDef) {
533 nextToken();
534 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
535 StringRef(FormatTok->Tok.getLiteralData(),
536 FormatTok->Tok.getLength()) == "0") ||
537 FormatTok->Tok.is(tok::kw_false);
538 conditionalCompilationStart(!IfDef && IsLiteralFalse);
539 parsePPUnknown();
540}
541
542void UnwrappedLineParser::parsePPElse() {
543 conditionalCompilationAlternative();
544 parsePPUnknown();
545}
546
547void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
548
549void UnwrappedLineParser::parsePPEndIf() {
550 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000551 parsePPUnknown();
552}
553
Manuel Klimek1abf7892013-01-04 23:34:14 +0000554void UnwrappedLineParser::parsePPDefine() {
555 nextToken();
556
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000557 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000558 parsePPUnknown();
559 return;
560 }
561 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000562 if (FormatTok->Tok.getKind() == tok::l_paren &&
563 FormatTok->WhitespaceRange.getBegin() ==
564 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000565 parseParens();
566 }
567 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000568 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000569
570 // Errors during a preprocessor directive can only affect the layout of the
571 // preprocessor directive, and thus we ignore them. An alternative approach
572 // would be to use the same approach we use on the file level (no
573 // re-indentation if there was a structural error) within the macro
574 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000575 parseFile();
576}
577
578void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000579 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000580 nextToken();
581 } while (!eof());
582 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000583}
584
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000585// Here we blacklist certain tokens that are not usually the first token in an
586// unwrapped line. This is used in attempt to distinguish macro calls without
587// trailing semicolons from other constructs split to several lines.
588bool tokenCanStartNewLine(clang::Token Tok) {
589 // Semicolon can be a null-statement, l_square can be a start of a macro or
590 // a C++11 attribute, but this doesn't seem to be common.
591 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
592 Tok.isNot(tok::l_square) &&
593 // Tokens that can only be used as binary operators and a part of
594 // overloaded operator names.
595 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
596 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
597 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
598 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
599 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
600 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
601 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
602 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
603 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
604 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
605 Tok.isNot(tok::lesslessequal) &&
606 // Colon is used in labels, base class lists, initializer lists,
607 // range-based for loops, ternary operator, but should never be the
608 // first token in an unwrapped line.
609 Tok.isNot(tok::colon);
610}
611
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000612void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000613 assert(!FormatTok->Tok.is(tok::l_brace));
614 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000615 case tok::at:
616 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000617 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000618 parseBracedList();
619 break;
620 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000621 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000622 case tok::objc_public:
623 case tok::objc_protected:
624 case tok::objc_package:
625 case tok::objc_private:
626 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000627 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000628 case tok::objc_implementation:
629 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000630 case tok::objc_protocol:
631 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000632 case tok::objc_end:
633 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000634 case tok::objc_optional:
635 case tok::objc_required:
636 nextToken();
637 addUnwrappedLine();
638 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000639 default:
640 break;
641 }
642 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000643 case tok::kw_namespace:
644 parseNamespace();
645 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000646 case tok::kw_inline:
647 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000648 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000649 parseNamespace();
650 return;
651 }
652 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000653 case tok::kw_public:
654 case tok::kw_protected:
655 case tok::kw_private:
Daniel Jasperf7935112012-12-03 18:12:45 +0000656 parseAccessSpecifier();
657 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000658 case tok::kw_if:
659 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000660 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000661 case tok::kw_for:
662 case tok::kw_while:
663 parseForOrWhileLoop();
664 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000665 case tok::kw_do:
666 parseDoWhile();
667 return;
668 case tok::kw_switch:
669 parseSwitch();
670 return;
671 case tok::kw_default:
672 nextToken();
673 parseLabel();
674 return;
675 case tok::kw_case:
676 parseCaseLabel();
677 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000678 case tok::kw_try:
679 parseTryCatch();
680 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000681 case tok::kw_extern:
682 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000683 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000684 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000685 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000686 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000687 addUnwrappedLine();
688 return;
689 }
690 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000691 break;
692 case tok::identifier:
693 if (FormatTok->IsForEachMacro) {
694 parseForOrWhileLoop();
695 return;
696 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000697 // In all other cases, parse the declaration.
698 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000699 default:
700 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000701 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000702 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000703 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000704 case tok::at:
705 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000706 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000707 parseBracedList();
708 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000709 case tok::kw_enum:
710 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000711 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000712 case tok::kw_typedef:
713 nextToken();
714 // FIXME: Use the IdentifierTable instead.
715 if (FormatTok->TokenText == "NS_ENUM")
716 parseEnum();
717 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000718 case tok::kw_struct:
719 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000720 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000721 parseRecord();
722 // A record declaration or definition is always the start of a structural
723 // element.
724 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000725 case tok::semi:
726 nextToken();
727 addUnwrappedLine();
728 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000729 case tok::r_brace:
730 addUnwrappedLine();
731 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000732 case tok::l_paren:
733 parseParens();
734 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000735 case tok::caret:
736 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000737 if (FormatTok->Tok.isAnyIdentifier() ||
738 FormatTok->isSimpleTypeSpecifier())
739 nextToken();
740 if (FormatTok->is(tok::l_paren))
741 parseParens();
742 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000743 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000744 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000745 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000746 if (!tryToParseBracedList()) {
747 // A block outside of parentheses must be the last part of a
748 // structural element.
749 // FIXME: Figure out cases where this is not true, and add projections
750 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000751 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000752 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000753 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000754 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000755 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000756 return;
757 }
758 // Otherwise this was a braced init list, and the structural
759 // element continues.
760 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000761 case tok::kw_try:
762 // We arrive here when parsing function-try blocks.
763 parseTryCatch();
764 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000765 case tok::identifier: {
766 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +0000767 nextToken();
Alexander Kornienkode644272013-04-08 22:16:06 +0000768 if (Line->Tokens.size() == 1) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000769 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000770 parseLabel();
771 return;
772 }
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000773 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000774 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000775 parseParens();
Daniel Jasper352dae12014-01-03 11:50:46 +0000776 if (FormatTok->NewlinesBefore > 0 &&
Alexander Kornienkoce081262014-03-18 14:35:20 +0000777 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000778 addUnwrappedLine();
779 return;
780 }
Daniel Jasper40e19212013-05-29 13:16:10 +0000781 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
782 Text == Text.upper()) {
783 // Recognize free-standing macros like Q_OBJECT.
784 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000785 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000786 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000787 }
788 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000789 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000790 case tok::equal:
791 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000792 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000793 parseBracedList();
794 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000795 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000796 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000797 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000798 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000799 default:
800 nextToken();
801 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000802 }
803 } while (!eof());
804}
805
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000806bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000807 // FIXME: This is a dirty way to access the previous token. Find a better
808 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000809 if (!Line->Tokens.empty() &&
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000810 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator) ||
811 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000812 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000813 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000814 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000815 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000816 assert(FormatTok->is(tok::l_square));
817 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000818 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000819 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000820
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000821 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000822 if (FormatTok->isSimpleTypeSpecifier()) {
823 nextToken();
824 continue;
825 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000826 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000827 case tok::l_brace:
828 break;
829 case tok::l_paren:
830 parseParens();
831 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000832 case tok::less:
833 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000834 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000835 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000836 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000837 nextToken();
838 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000839 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000840 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000841 nextToken();
842 break;
843 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000844 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000845 }
846 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000847 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000848 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000849 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000850}
851
852bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
853 nextToken();
854 if (FormatTok->is(tok::equal)) {
855 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000856 if (FormatTok->is(tok::r_square)) {
857 nextToken();
858 return true;
859 }
860 if (FormatTok->isNot(tok::comma))
861 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000862 nextToken();
863 } else if (FormatTok->is(tok::amp)) {
864 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000865 if (FormatTok->is(tok::r_square)) {
866 nextToken();
867 return true;
868 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000869 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
870 return false;
871 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000872 if (FormatTok->is(tok::comma))
873 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000874 } else if (FormatTok->is(tok::r_square)) {
875 nextToken();
876 return true;
877 }
878 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000879 if (FormatTok->is(tok::amp))
880 nextToken();
881 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
882 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000883 nextToken();
884 if (FormatTok->is(tok::comma)) {
885 nextToken();
886 } else if (FormatTok->is(tok::r_square)) {
887 nextToken();
888 return true;
889 } else {
890 return false;
891 }
892 } while (!eof());
893 return false;
894}
895
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000896void UnwrappedLineParser::tryToParseJSFunction() {
897 nextToken();
898 if (FormatTok->isNot(tok::l_paren))
899 return;
900 nextToken();
901 while (FormatTok->isNot(tok::l_brace)) {
902 // Err on the side of caution in order to avoid consuming the full file in
903 // case of incomplete code.
904 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
905 tok::comment))
906 return;
907 nextToken();
908 }
909 parseChildBlock();
910}
911
Manuel Klimekab419912013-05-23 09:41:43 +0000912bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000913 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +0000914 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000915 assert(FormatTok->BlockKind != BK_Unknown);
916 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +0000917 return false;
918 parseBracedList();
919 return true;
920}
921
Daniel Jasper015ed022013-09-13 09:20:45 +0000922bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
923 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000924 nextToken();
925
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000926 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
927 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000928 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000929 if (Style.Language == FormatStyle::LK_JavaScript &&
930 FormatTok->TokenText == "function") {
931 tryToParseJSFunction();
932 continue;
933 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000934 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +0000935 case tok::caret:
936 nextToken();
937 if (FormatTok->is(tok::l_brace)) {
938 parseChildBlock();
939 }
940 break;
941 case tok::l_square:
942 tryToParseLambda();
943 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000944 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000945 // Assume there are no blocks inside a braced init list apart
946 // from the ones we explicitly parse out (like lambdas).
947 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000948 parseBracedList();
949 break;
950 case tok::r_brace:
951 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +0000952 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000953 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +0000954 HasError = true;
955 if (!ContinueOnSemicolons)
956 return !HasError;
957 nextToken();
958 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000959 case tok::comma:
960 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000961 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000962 default:
963 nextToken();
964 break;
965 }
966 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +0000967 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000968}
969
Daniel Jasperf7935112012-12-03 18:12:45 +0000970void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000971 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +0000972 nextToken();
973 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000974 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000975 case tok::l_paren:
976 parseParens();
977 break;
978 case tok::r_paren:
979 nextToken();
980 return;
Daniel Jasper393564f2013-05-31 14:56:29 +0000981 case tok::r_brace:
982 // A "}" inside parenthesis is an error if there wasn't a matching "{".
983 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000984 case tok::l_square:
985 tryToParseLambda();
986 break;
Nico Weber67ffb332013-02-10 04:38:23 +0000987 case tok::l_brace: {
Manuel Klimekab419912013-05-23 09:41:43 +0000988 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +0000989 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +0000990 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000991 break;
Nico Weber67ffb332013-02-10 04:38:23 +0000992 }
Nico Weber372d8dc2013-02-10 20:35:35 +0000993 case tok::at:
994 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000995 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000996 parseBracedList();
997 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000998 default:
999 nextToken();
1000 break;
1001 }
1002 } while (!eof());
1003}
1004
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001005void UnwrappedLineParser::parseSquare() {
1006 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1007 if (tryToParseLambda())
1008 return;
1009 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001010 switch (FormatTok->Tok.getKind()) {
1011 case tok::l_paren:
1012 parseParens();
1013 break;
1014 case tok::r_square:
1015 nextToken();
1016 return;
1017 case tok::r_brace:
1018 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1019 return;
1020 case tok::l_square:
1021 parseSquare();
1022 break;
1023 case tok::l_brace: {
1024 if (!tryToParseBracedList()) {
1025 parseChildBlock();
1026 }
1027 break;
1028 }
1029 case tok::at:
1030 nextToken();
1031 if (FormatTok->Tok.is(tok::l_brace))
1032 parseBracedList();
1033 break;
1034 default:
1035 nextToken();
1036 break;
1037 }
1038 } while (!eof());
1039}
1040
Daniel Jasperf7935112012-12-03 18:12:45 +00001041void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001042 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001043 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001044 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001045 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001046 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001047 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001048 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001049 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001050 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1051 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001052 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001053 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001054 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001055 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001056 } else {
1057 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001058 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001059 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001060 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001061 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001062 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001063 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001064 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001065 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001066 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001067 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001068 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001069 parseIfThenElse();
1070 } else {
1071 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001072 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001073 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001074 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001075 }
1076 } else if (NeedsUnwrappedLine) {
1077 addUnwrappedLine();
1078 }
1079}
1080
Daniel Jasper04a71a42014-05-08 11:58:24 +00001081void UnwrappedLineParser::parseTryCatch() {
1082 assert(FormatTok->is(tok::kw_try) && "'try' expected");
1083 nextToken();
1084 bool NeedsUnwrappedLine = false;
1085 if (FormatTok->is(tok::colon)) {
1086 // We are in a function try block, what comes is an initializer list.
1087 nextToken();
1088 while (FormatTok->is(tok::identifier)) {
1089 nextToken();
1090 if (FormatTok->is(tok::l_paren))
1091 parseParens();
1092 else
1093 StructuralError = true;
1094 if (FormatTok->is(tok::comma))
1095 nextToken();
1096 }
1097 }
1098 if (FormatTok->is(tok::l_brace)) {
1099 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1100 parseBlock(/*MustBeDeclaration=*/false);
1101 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1102 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1103 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1104 addUnwrappedLine();
1105 } else {
1106 NeedsUnwrappedLine = true;
1107 }
1108 } else if (!FormatTok->is(tok::kw_catch)) {
1109 // The C++ standard requires a compound-statement after a try.
1110 // If there's none, we try to assume there's a structuralElement
1111 // and try to continue.
1112 StructuralError = true;
1113 addUnwrappedLine();
1114 ++Line->Level;
1115 parseStructuralElement();
1116 --Line->Level;
1117 }
1118 while (FormatTok->is(tok::kw_catch) ||
1119 (Style.Language == FormatStyle::LK_JavaScript &&
1120 FormatTok->TokenText == "finally")) {
1121 nextToken();
1122 while (FormatTok->isNot(tok::l_brace)) {
1123 if (FormatTok->is(tok::l_paren)) {
1124 parseParens();
1125 continue;
1126 }
1127 if (FormatTok->isOneOf(tok::semi, tok::r_brace))
1128 return;
1129 nextToken();
1130 }
1131 NeedsUnwrappedLine = false;
1132 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1133 parseBlock(/*MustBeDeclaration=*/false);
1134 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1135 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1136 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1137 addUnwrappedLine();
1138 } else {
1139 NeedsUnwrappedLine = true;
1140 }
1141 }
1142 if (NeedsUnwrappedLine) {
1143 addUnwrappedLine();
1144 }
1145}
1146
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001147void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001148 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001149 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001150 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001151 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001152 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001153 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001154 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1155 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001156 addUnwrappedLine();
1157
Daniel Jasper65ee3472013-07-31 23:16:02 +00001158 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1159 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1160 DeclarationScopeStack.size() > 1);
1161 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001162 // Munch the semicolon after a namespace. This is more common than one would
1163 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001164 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001165 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001166 addUnwrappedLine();
1167 }
1168 // FIXME: Add error handling.
1169}
1170
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001171void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001172 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1173 FormatTok->IsForEachMacro) &&
1174 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001175 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001176 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001177 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001178 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001179 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001180 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001181 addUnwrappedLine();
1182 } else {
1183 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001184 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001185 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001186 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001187 }
1188}
1189
Daniel Jasperf7935112012-12-03 18:12:45 +00001190void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001191 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001192 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001193 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001194 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001195 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001196 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1197 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001198 } else {
1199 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001200 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001201 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001202 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001203 }
1204
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001205 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001206 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001207 addUnwrappedLine();
1208 return;
1209 }
1210
Daniel Jasperf7935112012-12-03 18:12:45 +00001211 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001212 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001213}
1214
1215void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001216 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001217 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001218 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001219 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001220 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001221 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001222 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001223 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001224 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1225 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1226 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001227 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001228 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001229 parseStructuralElement();
1230 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001231 addUnwrappedLine();
1232 } else {
1233 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001234 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001235 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001236}
1237
1238void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001239 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001240 // FIXME: fix handling of complex expressions here.
1241 do {
1242 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001243 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001244 parseLabel();
1245}
1246
1247void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001248 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001249 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001250 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001251 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001252 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001253 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001254 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001255 addUnwrappedLine();
1256 } else {
1257 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001258 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001259 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001260 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001261 }
1262}
1263
1264void UnwrappedLineParser::parseAccessSpecifier() {
1265 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001266 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001267 if (FormatTok->is(tok::identifier) &&
1268 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001269 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001270 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001271 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001272 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001273 addUnwrappedLine();
1274}
1275
1276void UnwrappedLineParser::parseEnum() {
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001277 if (FormatTok->Tok.is(tok::kw_enum)) {
1278 // Won't be 'enum' for NS_ENUMs.
1279 nextToken();
1280 }
Daniel Jasper2b41a822013-08-20 12:42:50 +00001281 // Eat up enum class ...
1282 if (FormatTok->Tok.is(tok::kw_class) ||
1283 FormatTok->Tok.is(tok::kw_struct))
1284 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001285 while (FormatTok->Tok.getIdentifierInfo() ||
1286 FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001287 nextToken();
1288 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001289 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001290 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001291 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001292 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001293 nextToken();
1294 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001295 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001296 FormatTok->BlockKind = BK_Block;
1297 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1298 if (HasError) {
1299 if (FormatTok->is(tok::semi))
1300 nextToken();
Manuel Klimeka027f302013-08-07 19:20:45 +00001301 addUnwrappedLine();
Daniel Jasper015ed022013-09-13 09:20:45 +00001302 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001303 }
1304 // We fall through to parsing a structural element afterwards, so that in
1305 // enum A {} n, m;
1306 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperf7935112012-12-03 18:12:45 +00001307}
1308
Manuel Klimeke01bab52013-01-15 13:38:33 +00001309void UnwrappedLineParser::parseRecord() {
Manuel Klimek28cacc72013-01-07 18:10:23 +00001310 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001311 if (FormatTok->Tok.is(tok::identifier) ||
1312 FormatTok->Tok.is(tok::kw___attribute) ||
Manuel Klimek91e48582013-10-07 09:15:41 +00001313 FormatTok->Tok.is(tok::kw___declspec) ||
1314 FormatTok->Tok.is(tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001315 nextToken();
1316 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001317 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001318 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001319 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001320 // The actual identifier can be a nested name specifier, and in macros
1321 // it is often token-pasted.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001322 while (FormatTok->Tok.is(tok::identifier) ||
1323 FormatTok->Tok.is(tok::coloncolon) ||
1324 FormatTok->Tok.is(tok::hashhash))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001325 nextToken();
1326
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001327 // Note that parsing away template declarations here leads to incorrectly
1328 // accepting function declarations as record declarations.
1329 // In general, we cannot solve this problem. Consider:
1330 // class A<int> B() {}
1331 // which can be a function definition or a class definition when B() is a
1332 // macro. If we find enough real-world cases where this is a problem, we
1333 // can parse for the 'template' keyword in the beginning of the statement,
1334 // and thus rule out the record production in case there is no template
1335 // (this would still leave us with an ambiguity between template function
1336 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001337 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1338 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1339 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001340 return;
1341 nextToken();
1342 }
1343 }
1344 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001345 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001346 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001347 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1348 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001349 addUnwrappedLine();
1350
Daniel Jasper240dfda2014-03-31 14:23:49 +00001351 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001352 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001353 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001354 // We fall through to parsing a structural element afterwards, so
1355 // class A {} n, m;
1356 // will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00001357}
1358
Nico Weber8696a8d2013-01-09 21:15:03 +00001359void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001360 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001361 do
1362 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001363 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001364 nextToken(); // Skip '>'.
1365}
1366
1367void UnwrappedLineParser::parseObjCUntilAtEnd() {
1368 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001369 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001370 nextToken();
1371 addUnwrappedLine();
1372 break;
1373 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001374 if (FormatTok->is(tok::l_brace)) {
1375 parseBlock(/*MustBeDeclaration=*/false);
1376 // In ObjC interfaces, nothing should be following the "}".
1377 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001378 } else if (FormatTok->is(tok::r_brace)) {
1379 // Ignore stray "}". parseStructuralElement doesn't consume them.
1380 nextToken();
1381 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001382 } else {
1383 parseStructuralElement();
1384 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001385 } while (!eof());
1386}
1387
Nico Weber2ce0ac52013-01-09 23:25:37 +00001388void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001389 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001390 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001391
1392 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001393 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001394 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001395 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001396 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001397 // Skip category, if present.
1398 parseParens();
1399
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001400 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001401 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001402
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001403 if (FormatTok->Tok.is(tok::l_brace)) {
1404 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1405 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1406 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001407 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001408 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001409
1410 // With instance variables, this puts '}' on its own line. Without instance
1411 // variables, this ends the @interface line.
1412 addUnwrappedLine();
1413
Nico Weber8696a8d2013-01-09 21:15:03 +00001414 parseObjCUntilAtEnd();
1415}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001416
Nico Weber8696a8d2013-01-09 21:15:03 +00001417void UnwrappedLineParser::parseObjCProtocol() {
1418 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001419 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001420
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001421 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001422 parseObjCProtocolList();
1423
1424 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001425 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001426 nextToken();
1427 return addUnwrappedLine();
1428 }
1429
1430 addUnwrappedLine();
1431 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001432}
1433
Daniel Jasper3b203a62013-09-05 16:05:56 +00001434LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1435 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001436 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1437 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1438 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1439 E = Line.Tokens.end();
1440 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001441 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001442 }
1443 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1444 E = Line.Tokens.end();
1445 I != E; ++I) {
1446 const UnwrappedLineNode &Node = *I;
1447 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1448 I = Node.Children.begin(),
1449 E = Node.Children.end();
1450 I != E; ++I) {
1451 printDebugInfo(*I, "\nChild: ");
1452 }
1453 }
1454 llvm::dbgs() << "\n";
1455}
1456
Daniel Jasperf7935112012-12-03 18:12:45 +00001457void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001458 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001459 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001460 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001461 if (CurrentLines == &Lines)
1462 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001463 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001464 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001465 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001466 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001467 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001468 I = PreprocessorDirectives.begin(),
1469 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001470 I != E; ++I) {
1471 CurrentLines->push_back(*I);
1472 }
1473 PreprocessorDirectives.clear();
1474 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001475}
1476
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001477bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001478
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001479bool UnwrappedLineParser::isOnNewLine(const FormatToken& FormatTok) {
1480 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1481 FormatTok.NewlinesBefore > 0;
1482}
1483
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001484void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1485 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001486 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001487 I = CommentsBeforeNextToken.begin(),
1488 E = CommentsBeforeNextToken.end();
1489 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001490 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001491 addUnwrappedLine();
1492 }
1493 pushToken(*I);
1494 }
1495 if (NewlineBeforeNext && JustComments) {
1496 addUnwrappedLine();
1497 }
1498 CommentsBeforeNextToken.clear();
1499}
1500
Daniel Jasperf7935112012-12-03 18:12:45 +00001501void UnwrappedLineParser::nextToken() {
1502 if (eof())
1503 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001504 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001505 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001506 readToken();
1507}
1508
1509void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001510 bool CommentsInCurrentLine = true;
1511 do {
1512 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001513 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001514 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1515 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001516 // If there is an unfinished unwrapped line, we flush the preprocessor
1517 // directives only after that unwrapped line was finished later.
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001518 bool SwitchToPreprocessorLines =
1519 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001520 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001521 // Comments stored before the preprocessor directive need to be output
1522 // before the preprocessor directive, at the same level as the
1523 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001524 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001525 parsePPDirective();
1526 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001527 while (FormatTok->Type == TT_ConflictStart ||
1528 FormatTok->Type == TT_ConflictEnd ||
1529 FormatTok->Type == TT_ConflictAlternative) {
1530 if (FormatTok->Type == TT_ConflictStart) {
1531 conditionalCompilationStart(/*Unreachable=*/false);
1532 } else if (FormatTok->Type == TT_ConflictAlternative) {
1533 conditionalCompilationAlternative();
1534 } else if(FormatTok->Type == TT_ConflictEnd) {
1535 conditionalCompilationEnd();
1536 }
1537 FormatTok = Tokens->getNextToken();
1538 FormatTok->MustBreakBefore = true;
1539 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001540
1541 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1542 !Line->InPPDirective) {
1543 continue;
1544 }
1545
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001546 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001547 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001548 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001549 CommentsInCurrentLine = false;
1550 }
1551 if (CommentsInCurrentLine) {
1552 pushToken(FormatTok);
1553 } else {
1554 CommentsBeforeNextToken.push_back(FormatTok);
1555 }
1556 } while (!eof());
1557}
1558
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001559void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001560 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001561 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001562 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001563 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001564 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001565}
1566
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001567} // end namespace format
1568} // end namespace clang