blob: 6a8156f92eed32170188b75564513add3b9ebdf6 [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 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000167 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000168
169private:
170 unsigned &LineLevel;
171 unsigned OldLineLevel;
172};
173
Craig Topper69665e12013-07-01 04:21:54 +0000174namespace {
175
Manuel Klimekab419912013-05-23 09:41:43 +0000176class IndexedTokenSource : public FormatTokenSource {
177public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000178 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000179 : Tokens(Tokens), Position(-1) {}
180
Craig Topperfb6b25b2014-03-15 04:29:04 +0000181 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000182 ++Position;
183 return Tokens[Position];
184 }
185
Craig Topperfb6b25b2014-03-15 04:29:04 +0000186 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000187 assert(Position >= 0);
188 return Position;
189 }
190
Craig Topperfb6b25b2014-03-15 04:29:04 +0000191 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000192 Position = P;
193 return Tokens[Position];
194 }
195
Manuel Klimek71814b42013-10-11 21:25:45 +0000196 void reset() { Position = -1; }
197
Manuel Klimekab419912013-05-23 09:41:43 +0000198private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000199 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000200 int Position;
201};
202
Craig Topper69665e12013-07-01 04:21:54 +0000203} // end anonymous namespace
204
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000205UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000206 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000207 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000208 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
209 CurrentLines(&Lines), StructuralError(false), Style(Style),
210 Tokens(nullptr), Callback(Callback), AllTokens(Tokens),
211 PPBranchLevel(-1) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000212
213void UnwrappedLineParser::reset() {
214 PPBranchLevel = -1;
215 Line.reset(new UnwrappedLine);
216 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000217 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000218 MustBreakBeforeNextToken = false;
219 PreprocessorDirectives.clear();
220 CurrentLines = &Lines;
221 DeclarationScopeStack.clear();
222 StructuralError = false;
223 PPStack.clear();
224}
Daniel Jasperf7935112012-12-03 18:12:45 +0000225
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000226bool UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000227 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimek71814b42013-10-11 21:25:45 +0000228 do {
229 DEBUG(llvm::dbgs() << "----\n");
230 reset();
231 Tokens = &TokenSource;
232 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000233
Manuel Klimek71814b42013-10-11 21:25:45 +0000234 readToken();
235 parseFile();
236 // Create line with eof token.
237 pushToken(FormatTok);
238 addUnwrappedLine();
239
240 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
241 E = Lines.end();
242 I != E; ++I) {
243 Callback.consumeUnwrappedLine(*I);
244 }
245 Callback.finishRun();
246 Lines.clear();
247 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000248 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000249 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
250 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
251 }
252 if (!PPLevelBranchIndex.empty()) {
253 ++PPLevelBranchIndex.back();
254 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
255 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
256 }
257 } while (!PPLevelBranchIndex.empty());
258
Manuel Klimek1a18c402013-04-12 14:13:36 +0000259 return StructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000260}
261
Manuel Klimek1a18c402013-04-12 14:13:36 +0000262void UnwrappedLineParser::parseFile() {
Daniel Jasperdd9276e2013-03-22 16:55:40 +0000263 ScopedDeclarationState DeclarationState(
264 *Line, DeclarationScopeStack,
265 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber9096fc02013-06-26 00:30:14 +0000266 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000267 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000268 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000269 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000270}
271
Manuel Klimek1a18c402013-04-12 14:13:36 +0000272void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000273 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000274 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000275 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000276 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000277 nextToken();
278 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000279 break;
280 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000281 // FIXME: Add parameter whether this can happen - if this happens, we must
282 // be in a non-declaration context.
Nico Weber9096fc02013-06-26 00:30:14 +0000283 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000284 addUnwrappedLine();
285 break;
286 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000287 if (HasOpeningBrace)
288 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000289 StructuralError = true;
290 nextToken();
291 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000292 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000293 case tok::kw_default:
294 case tok::kw_case:
Daniel Jasper72407622013-09-02 08:26:29 +0000295 if (!SwitchLabelEncountered &&
296 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
297 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000298 SwitchLabelEncountered = true;
299 parseStructuralElement();
300 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000301 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000302 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000303 break;
304 }
305 } while (!eof());
306}
307
Manuel Klimekab419912013-05-23 09:41:43 +0000308void UnwrappedLineParser::calculateBraceTypes() {
309 // We'll parse forward through the tokens until we hit
310 // a closing brace or eof - note that getNextToken() will
311 // parse macros, so this will magically work inside macro
312 // definitions, too.
313 unsigned StoredPosition = Tokens->getPosition();
314 unsigned Position = StoredPosition;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000315 FormatToken *Tok = FormatTok;
Manuel Klimekab419912013-05-23 09:41:43 +0000316 // Keep a stack of positions of lbrace tokens. We will
317 // update information about whether an lbrace starts a
318 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000319 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000320 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000321 do {
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000322 // Get next none-comment token.
323 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000324 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000325 do {
326 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000327 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000328 } while (NextTok->is(tok::comment));
329
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000330 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000331 case tok::l_brace:
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000332 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000333 break;
334 case tok::r_brace:
335 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000336 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Daniel Jasper220c0d12014-04-10 07:27:12 +0000337 bool ProbablyBracedList = false;
338 if (Style.Language == FormatStyle::LK_Proto) {
339 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
340 } else {
Daniel Jasper91b032a2014-05-22 12:46:38 +0000341 // Using OriginalColumn to distinguish between ObjC methods and
342 // binary operators is a bit hacky.
343 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
344 NextTok->OriginalColumn == 0;
345
Daniel Jasper220c0d12014-04-10 07:27:12 +0000346 // If there is a comma, semicolon or right paren after the closing
347 // brace, we assume this is a braced initializer list. Note that
348 // regardless how we mark inner braces here, we will overwrite the
349 // BlockKind later if we parse a braced list (where all blocks
350 // inside are by default braced lists), or when we explicitly detect
351 // blocks (for example while parsing lambdas).
352 //
353 // We exclude + and - as they can be ObjC visibility modifiers.
354 ProbablyBracedList =
355 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
Daniel Jasper438059e2014-05-22 12:11:13 +0000356 tok::r_paren, tok::r_square, tok::l_brace,
357 tok::l_paren) ||
Daniel Jasper91b032a2014-05-22 12:46:38 +0000358 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000359 }
360 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000361 Tok->BlockKind = BK_BracedInit;
362 LBraceStack.back()->BlockKind = BK_BracedInit;
363 } else {
364 Tok->BlockKind = BK_Block;
365 LBraceStack.back()->BlockKind = BK_Block;
366 }
Manuel Klimekab419912013-05-23 09:41:43 +0000367 }
368 LBraceStack.pop_back();
369 }
370 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000371 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000372 case tok::semi:
373 case tok::kw_if:
374 case tok::kw_while:
375 case tok::kw_for:
376 case tok::kw_switch:
377 case tok::kw_try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000378 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000379 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000380 break;
381 default:
382 break;
383 }
384 Tok = NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000385 Position += ReadTokens;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000386 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000387 // Assume other blocks for all unclosed opening braces.
388 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000389 if (LBraceStack[i]->BlockKind == BK_Unknown)
390 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000391 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000392
Manuel Klimekab419912013-05-23 09:41:43 +0000393 FormatTok = Tokens->setPosition(StoredPosition);
394}
395
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000396void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
397 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000398 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000399 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000400 nextToken();
401
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000402 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000403
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000404 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
405 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000406 if (AddLevel)
407 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000408 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000409
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000410 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000411 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000412 StructuralError = true;
413 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000414 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000415
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000416 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000417 if (MunchSemi && FormatTok->Tok.is(tok::semi))
418 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000419 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000420}
421
Daniel Jasper4a39c842014-05-06 13:54:10 +0000422static bool IsGoogScope(const UnwrappedLine &Line) {
423 if (Line.Tokens.size() < 4)
424 return false;
425 auto I = Line.Tokens.begin();
426 if (I->Tok->TokenText != "goog")
427 return false;
428 ++I;
429 if (I->Tok->isNot(tok::period))
430 return false;
431 ++I;
432 if (I->Tok->TokenText != "scope")
433 return false;
434 ++I;
435 return I->Tok->is(tok::l_paren);
436}
437
Manuel Klimek516e0542013-09-04 13:25:30 +0000438void UnwrappedLineParser::parseChildBlock() {
439 FormatTok->BlockKind = BK_Block;
440 nextToken();
441 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000442 bool GoogScope =
443 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000444 ScopedLineState LineState(*this);
445 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
446 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000447 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000448 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000449 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000450 }
451 nextToken();
452}
453
Daniel Jasperf7935112012-12-03 18:12:45 +0000454void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000455 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000456 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000457 nextToken();
458
Craig Topper2145bc02014-05-09 08:15:10 +0000459 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000460 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000461 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000462 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000463
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000464 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000465 case tok::pp_define:
466 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000467 return;
468 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000469 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000470 break;
471 case tok::pp_ifdef:
472 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000473 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000474 break;
475 case tok::pp_else:
476 parsePPElse();
477 break;
478 case tok::pp_elif:
479 parsePPElIf();
480 break;
481 case tok::pp_endif:
482 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000483 break;
484 default:
485 parsePPUnknown();
486 break;
487 }
488}
489
Manuel Klimek68b03042014-04-14 09:14:11 +0000490void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
491 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000492 PPStack.push_back(PP_Unreachable);
493 else
494 PPStack.push_back(PP_Conditional);
495}
496
Manuel Klimek68b03042014-04-14 09:14:11 +0000497void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000498 ++PPBranchLevel;
499 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
500 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
501 PPLevelBranchIndex.push_back(0);
502 PPLevelBranchCount.push_back(0);
503 }
504 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000505 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
506 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000507}
508
Manuel Klimek68b03042014-04-14 09:14:11 +0000509void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000510 if (!PPStack.empty())
511 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000512 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
513 if (!PPChainBranchIndex.empty())
514 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000515 conditionalCompilationCondition(
516 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
517 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000518}
519
Manuel Klimek68b03042014-04-14 09:14:11 +0000520void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000521 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
522 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
523 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000524 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
525 }
526 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000527 // Guard against #endif's without #if.
528 if (PPBranchLevel > 0)
529 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000530 if (!PPChainBranchIndex.empty())
531 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000532 if (!PPStack.empty())
533 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000534}
535
536void UnwrappedLineParser::parsePPIf(bool IfDef) {
537 nextToken();
538 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
539 StringRef(FormatTok->Tok.getLiteralData(),
540 FormatTok->Tok.getLength()) == "0") ||
541 FormatTok->Tok.is(tok::kw_false);
542 conditionalCompilationStart(!IfDef && IsLiteralFalse);
543 parsePPUnknown();
544}
545
546void UnwrappedLineParser::parsePPElse() {
547 conditionalCompilationAlternative();
548 parsePPUnknown();
549}
550
551void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
552
553void UnwrappedLineParser::parsePPEndIf() {
554 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000555 parsePPUnknown();
556}
557
Manuel Klimek1abf7892013-01-04 23:34:14 +0000558void UnwrappedLineParser::parsePPDefine() {
559 nextToken();
560
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000561 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000562 parsePPUnknown();
563 return;
564 }
565 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000566 if (FormatTok->Tok.getKind() == tok::l_paren &&
567 FormatTok->WhitespaceRange.getBegin() ==
568 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000569 parseParens();
570 }
571 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000572 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000573
574 // Errors during a preprocessor directive can only affect the layout of the
575 // preprocessor directive, and thus we ignore them. An alternative approach
576 // would be to use the same approach we use on the file level (no
577 // re-indentation if there was a structural error) within the macro
578 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000579 parseFile();
580}
581
582void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000583 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000584 nextToken();
585 } while (!eof());
586 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000587}
588
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000589// Here we blacklist certain tokens that are not usually the first token in an
590// unwrapped line. This is used in attempt to distinguish macro calls without
591// trailing semicolons from other constructs split to several lines.
592bool tokenCanStartNewLine(clang::Token Tok) {
593 // Semicolon can be a null-statement, l_square can be a start of a macro or
594 // a C++11 attribute, but this doesn't seem to be common.
595 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
596 Tok.isNot(tok::l_square) &&
597 // Tokens that can only be used as binary operators and a part of
598 // overloaded operator names.
599 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
600 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
601 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
602 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
603 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
604 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
605 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
606 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
607 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
608 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
609 Tok.isNot(tok::lesslessequal) &&
610 // Colon is used in labels, base class lists, initializer lists,
611 // range-based for loops, ternary operator, but should never be the
612 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000613 Tok.isNot(tok::colon) &&
614 // 'noexcept' is a trailing annotation.
615 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000616}
617
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000618void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000619 assert(!FormatTok->Tok.is(tok::l_brace));
620 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000621 case tok::at:
622 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000623 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000624 parseBracedList();
625 break;
626 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000627 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000628 case tok::objc_public:
629 case tok::objc_protected:
630 case tok::objc_package:
631 case tok::objc_private:
632 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000633 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000634 case tok::objc_implementation:
635 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000636 case tok::objc_protocol:
637 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000638 case tok::objc_end:
639 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000640 case tok::objc_optional:
641 case tok::objc_required:
642 nextToken();
643 addUnwrappedLine();
644 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000645 default:
646 break;
647 }
648 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000649 case tok::kw_namespace:
650 parseNamespace();
651 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000652 case tok::kw_inline:
653 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000654 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000655 parseNamespace();
656 return;
657 }
658 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000659 case tok::kw_public:
660 case tok::kw_protected:
661 case tok::kw_private:
Daniel Jasperf7935112012-12-03 18:12:45 +0000662 parseAccessSpecifier();
663 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000664 case tok::kw_if:
665 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000666 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000667 case tok::kw_for:
668 case tok::kw_while:
669 parseForOrWhileLoop();
670 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000671 case tok::kw_do:
672 parseDoWhile();
673 return;
674 case tok::kw_switch:
675 parseSwitch();
676 return;
677 case tok::kw_default:
678 nextToken();
679 parseLabel();
680 return;
681 case tok::kw_case:
682 parseCaseLabel();
683 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000684 case tok::kw_try:
685 parseTryCatch();
686 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000687 case tok::kw_extern:
688 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000689 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000690 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000691 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000692 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000693 addUnwrappedLine();
694 return;
695 }
696 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000697 break;
698 case tok::identifier:
699 if (FormatTok->IsForEachMacro) {
700 parseForOrWhileLoop();
701 return;
702 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000703 // In all other cases, parse the declaration.
704 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000705 default:
706 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000707 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000708 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000709 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000710 case tok::at:
711 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000712 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000713 parseBracedList();
714 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000715 case tok::kw_enum:
716 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000717 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000718 case tok::kw_typedef:
719 nextToken();
720 // FIXME: Use the IdentifierTable instead.
721 if (FormatTok->TokenText == "NS_ENUM")
722 parseEnum();
723 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000724 case tok::kw_struct:
725 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000726 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000727 parseRecord();
728 // A record declaration or definition is always the start of a structural
729 // element.
730 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000731 case tok::semi:
732 nextToken();
733 addUnwrappedLine();
734 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000735 case tok::r_brace:
736 addUnwrappedLine();
737 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000738 case tok::l_paren:
739 parseParens();
740 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000741 case tok::caret:
742 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000743 if (FormatTok->Tok.isAnyIdentifier() ||
744 FormatTok->isSimpleTypeSpecifier())
745 nextToken();
746 if (FormatTok->is(tok::l_paren))
747 parseParens();
748 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000749 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000750 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000751 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000752 if (!tryToParseBracedList()) {
753 // A block outside of parentheses must be the last part of a
754 // structural element.
755 // FIXME: Figure out cases where this is not true, and add projections
756 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000757 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000758 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000759 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000760 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000761 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000762 return;
763 }
764 // Otherwise this was a braced init list, and the structural
765 // element continues.
766 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000767 case tok::kw_try:
768 // We arrive here when parsing function-try blocks.
769 parseTryCatch();
770 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000771 case tok::identifier: {
772 StringRef Text = FormatTok->TokenText;
Daniel Jasper069e5f42014-05-20 11:14:57 +0000773 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function") {
774 tryToParseJSFunction();
775 break;
776 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000777 nextToken();
Alexander Kornienkode644272013-04-08 22:16:06 +0000778 if (Line->Tokens.size() == 1) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000779 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000780 parseLabel();
781 return;
782 }
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000783 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000784 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000785 parseParens();
Daniel Jasper352dae12014-01-03 11:50:46 +0000786 if (FormatTok->NewlinesBefore > 0 &&
Alexander Kornienkoce081262014-03-18 14:35:20 +0000787 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000788 addUnwrappedLine();
789 return;
790 }
Daniel Jasper40e19212013-05-29 13:16:10 +0000791 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
792 Text == Text.upper()) {
793 // Recognize free-standing macros like Q_OBJECT.
794 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000795 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000796 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000797 }
798 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000799 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000800 case tok::equal:
801 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000802 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000803 parseBracedList();
804 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000805 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000806 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000807 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000808 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000809 default:
810 nextToken();
811 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000812 }
813 } while (!eof());
814}
815
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000816bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000817 // FIXME: This is a dirty way to access the previous token. Find a better
818 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000819 if (!Line->Tokens.empty() &&
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000820 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator) ||
821 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000822 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000823 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000824 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000825 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000826 assert(FormatTok->is(tok::l_square));
827 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000828 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000829 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000830
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000831 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000832 if (FormatTok->isSimpleTypeSpecifier()) {
833 nextToken();
834 continue;
835 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000836 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000837 case tok::l_brace:
838 break;
839 case tok::l_paren:
840 parseParens();
841 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000842 case tok::less:
843 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000844 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000845 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000846 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000847 nextToken();
848 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000849 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000850 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000851 nextToken();
852 break;
853 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000854 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000855 }
856 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000857 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000858 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000859 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000860}
861
862bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
863 nextToken();
864 if (FormatTok->is(tok::equal)) {
865 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000866 if (FormatTok->is(tok::r_square)) {
867 nextToken();
868 return true;
869 }
870 if (FormatTok->isNot(tok::comma))
871 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000872 nextToken();
873 } else if (FormatTok->is(tok::amp)) {
874 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000875 if (FormatTok->is(tok::r_square)) {
876 nextToken();
877 return true;
878 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000879 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
880 return false;
881 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000882 if (FormatTok->is(tok::comma))
883 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000884 } else if (FormatTok->is(tok::r_square)) {
885 nextToken();
886 return true;
887 }
888 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000889 if (FormatTok->is(tok::amp))
890 nextToken();
891 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
892 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000893 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000894 if (FormatTok->is(tok::ellipsis))
895 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000896 if (FormatTok->is(tok::comma)) {
897 nextToken();
898 } else if (FormatTok->is(tok::r_square)) {
899 nextToken();
900 return true;
901 } else {
902 return false;
903 }
904 } while (!eof());
905 return false;
906}
907
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000908void UnwrappedLineParser::tryToParseJSFunction() {
909 nextToken();
910 if (FormatTok->isNot(tok::l_paren))
911 return;
912 nextToken();
913 while (FormatTok->isNot(tok::l_brace)) {
914 // Err on the side of caution in order to avoid consuming the full file in
915 // case of incomplete code.
916 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
917 tok::comment))
918 return;
919 nextToken();
920 }
921 parseChildBlock();
922}
923
Manuel Klimekab419912013-05-23 09:41:43 +0000924bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000925 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +0000926 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000927 assert(FormatTok->BlockKind != BK_Unknown);
928 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +0000929 return false;
930 parseBracedList();
931 return true;
932}
933
Daniel Jasper015ed022013-09-13 09:20:45 +0000934bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
935 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000936 nextToken();
937
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000938 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
939 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000940 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000941 if (Style.Language == FormatStyle::LK_JavaScript &&
942 FormatTok->TokenText == "function") {
943 tryToParseJSFunction();
944 continue;
945 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000946 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +0000947 case tok::caret:
948 nextToken();
949 if (FormatTok->is(tok::l_brace)) {
950 parseChildBlock();
951 }
952 break;
953 case tok::l_square:
954 tryToParseLambda();
955 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000956 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000957 // Assume there are no blocks inside a braced init list apart
958 // from the ones we explicitly parse out (like lambdas).
959 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000960 parseBracedList();
961 break;
962 case tok::r_brace:
963 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +0000964 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000965 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +0000966 HasError = true;
967 if (!ContinueOnSemicolons)
968 return !HasError;
969 nextToken();
970 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000971 case tok::comma:
972 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000973 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000974 default:
975 nextToken();
976 break;
977 }
978 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +0000979 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000980}
981
Daniel Jasperf7935112012-12-03 18:12:45 +0000982void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000983 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +0000984 nextToken();
985 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000986 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000987 case tok::l_paren:
988 parseParens();
989 break;
990 case tok::r_paren:
991 nextToken();
992 return;
Daniel Jasper393564f2013-05-31 14:56:29 +0000993 case tok::r_brace:
994 // A "}" inside parenthesis is an error if there wasn't a matching "{".
995 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000996 case tok::l_square:
997 tryToParseLambda();
998 break;
Nico Weber67ffb332013-02-10 04:38:23 +0000999 case tok::l_brace: {
Manuel Klimekab419912013-05-23 09:41:43 +00001000 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001001 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001002 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001003 break;
Nico Weber67ffb332013-02-10 04:38:23 +00001004 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001005 case tok::at:
1006 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001007 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001008 parseBracedList();
1009 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001010 default:
1011 nextToken();
1012 break;
1013 }
1014 } while (!eof());
1015}
1016
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001017void UnwrappedLineParser::parseSquare() {
1018 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1019 if (tryToParseLambda())
1020 return;
1021 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001022 switch (FormatTok->Tok.getKind()) {
1023 case tok::l_paren:
1024 parseParens();
1025 break;
1026 case tok::r_square:
1027 nextToken();
1028 return;
1029 case tok::r_brace:
1030 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1031 return;
1032 case tok::l_square:
1033 parseSquare();
1034 break;
1035 case tok::l_brace: {
1036 if (!tryToParseBracedList()) {
1037 parseChildBlock();
1038 }
1039 break;
1040 }
1041 case tok::at:
1042 nextToken();
1043 if (FormatTok->Tok.is(tok::l_brace))
1044 parseBracedList();
1045 break;
1046 default:
1047 nextToken();
1048 break;
1049 }
1050 } while (!eof());
1051}
1052
Daniel Jasperf7935112012-12-03 18:12:45 +00001053void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001054 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001055 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001056 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001057 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001058 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001059 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001060 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001061 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001062 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1063 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001064 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001065 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001066 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001067 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001068 } else {
1069 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001070 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001071 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001072 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001073 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001074 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001075 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001076 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001077 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001078 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001079 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001080 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001081 parseIfThenElse();
1082 } else {
1083 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001084 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001085 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001086 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001087 }
1088 } else if (NeedsUnwrappedLine) {
1089 addUnwrappedLine();
1090 }
1091}
1092
Daniel Jasper04a71a42014-05-08 11:58:24 +00001093void UnwrappedLineParser::parseTryCatch() {
1094 assert(FormatTok->is(tok::kw_try) && "'try' expected");
1095 nextToken();
1096 bool NeedsUnwrappedLine = false;
1097 if (FormatTok->is(tok::colon)) {
1098 // We are in a function try block, what comes is an initializer list.
1099 nextToken();
1100 while (FormatTok->is(tok::identifier)) {
1101 nextToken();
1102 if (FormatTok->is(tok::l_paren))
1103 parseParens();
1104 else
1105 StructuralError = true;
1106 if (FormatTok->is(tok::comma))
1107 nextToken();
1108 }
1109 }
1110 if (FormatTok->is(tok::l_brace)) {
1111 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1112 parseBlock(/*MustBeDeclaration=*/false);
1113 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1114 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1115 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1116 addUnwrappedLine();
1117 } else {
1118 NeedsUnwrappedLine = true;
1119 }
1120 } else if (!FormatTok->is(tok::kw_catch)) {
1121 // The C++ standard requires a compound-statement after a try.
1122 // If there's none, we try to assume there's a structuralElement
1123 // and try to continue.
1124 StructuralError = true;
1125 addUnwrappedLine();
1126 ++Line->Level;
1127 parseStructuralElement();
1128 --Line->Level;
1129 }
1130 while (FormatTok->is(tok::kw_catch) ||
1131 (Style.Language == FormatStyle::LK_JavaScript &&
1132 FormatTok->TokenText == "finally")) {
1133 nextToken();
1134 while (FormatTok->isNot(tok::l_brace)) {
1135 if (FormatTok->is(tok::l_paren)) {
1136 parseParens();
1137 continue;
1138 }
1139 if (FormatTok->isOneOf(tok::semi, tok::r_brace))
1140 return;
1141 nextToken();
1142 }
1143 NeedsUnwrappedLine = false;
1144 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1145 parseBlock(/*MustBeDeclaration=*/false);
1146 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1147 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1148 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1149 addUnwrappedLine();
1150 } else {
1151 NeedsUnwrappedLine = true;
1152 }
1153 }
1154 if (NeedsUnwrappedLine) {
1155 addUnwrappedLine();
1156 }
1157}
1158
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001159void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001160 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001161 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001162 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001163 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001164 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001165 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001166 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1167 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001168 addUnwrappedLine();
1169
Daniel Jasper65ee3472013-07-31 23:16:02 +00001170 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1171 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1172 DeclarationScopeStack.size() > 1);
1173 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001174 // Munch the semicolon after a namespace. This is more common than one would
1175 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001176 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001177 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001178 addUnwrappedLine();
1179 }
1180 // FIXME: Add error handling.
1181}
1182
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001183void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001184 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1185 FormatTok->IsForEachMacro) &&
1186 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001187 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001188 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001189 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001190 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001191 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001192 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001193 addUnwrappedLine();
1194 } else {
1195 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001196 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001197 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001198 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001199 }
1200}
1201
Daniel Jasperf7935112012-12-03 18:12:45 +00001202void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001203 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001204 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001205 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001206 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001207 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001208 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1209 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001210 } else {
1211 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001212 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001213 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001214 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001215 }
1216
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001217 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001218 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001219 addUnwrappedLine();
1220 return;
1221 }
1222
Daniel Jasperf7935112012-12-03 18:12:45 +00001223 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001224 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001225}
1226
1227void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001228 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001229 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001230 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001231 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001232 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001233 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001234 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001235 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001236 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1237 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1238 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001239 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001240 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001241 parseStructuralElement();
1242 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001243 addUnwrappedLine();
1244 } else {
1245 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001246 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001247 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001248}
1249
1250void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001251 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001252 // FIXME: fix handling of complex expressions here.
1253 do {
1254 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001255 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001256 parseLabel();
1257}
1258
1259void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001260 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001261 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001262 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001263 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001264 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001265 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001266 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001267 addUnwrappedLine();
1268 } else {
1269 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001270 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001271 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001272 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001273 }
1274}
1275
1276void UnwrappedLineParser::parseAccessSpecifier() {
1277 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001278 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001279 if (FormatTok->is(tok::identifier) &&
1280 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001281 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001282 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001283 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001284 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001285 addUnwrappedLine();
1286}
1287
1288void UnwrappedLineParser::parseEnum() {
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001289 if (FormatTok->Tok.is(tok::kw_enum)) {
1290 // Won't be 'enum' for NS_ENUMs.
1291 nextToken();
1292 }
Daniel Jasper2b41a822013-08-20 12:42:50 +00001293 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001294 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1295 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001296 while (FormatTok->Tok.getIdentifierInfo() ||
1297 FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001298 nextToken();
1299 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001300 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001301 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001302 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001303 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001304 nextToken();
1305 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001306 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001307 FormatTok->BlockKind = BK_Block;
1308 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1309 if (HasError) {
1310 if (FormatTok->is(tok::semi))
1311 nextToken();
Manuel Klimeka027f302013-08-07 19:20:45 +00001312 addUnwrappedLine();
Daniel Jasper015ed022013-09-13 09:20:45 +00001313 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001314 }
1315 // We fall through to parsing a structural element afterwards, so that in
1316 // enum A {} n, m;
1317 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperf7935112012-12-03 18:12:45 +00001318}
1319
Manuel Klimeke01bab52013-01-15 13:38:33 +00001320void UnwrappedLineParser::parseRecord() {
Manuel Klimek28cacc72013-01-07 18:10:23 +00001321 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001322 if (FormatTok->Tok.is(tok::identifier) ||
1323 FormatTok->Tok.is(tok::kw___attribute) ||
Manuel Klimek91e48582013-10-07 09:15:41 +00001324 FormatTok->Tok.is(tok::kw___declspec) ||
1325 FormatTok->Tok.is(tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001326 nextToken();
1327 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001328 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001329 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001330 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001331 // The actual identifier can be a nested name specifier, and in macros
1332 // it is often token-pasted.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001333 while (FormatTok->Tok.is(tok::identifier) ||
1334 FormatTok->Tok.is(tok::coloncolon) ||
1335 FormatTok->Tok.is(tok::hashhash))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001336 nextToken();
1337
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001338 // Note that parsing away template declarations here leads to incorrectly
1339 // accepting function declarations as record declarations.
1340 // In general, we cannot solve this problem. Consider:
1341 // class A<int> B() {}
1342 // which can be a function definition or a class definition when B() is a
1343 // macro. If we find enough real-world cases where this is a problem, we
1344 // can parse for the 'template' keyword in the beginning of the statement,
1345 // and thus rule out the record production in case there is no template
1346 // (this would still leave us with an ambiguity between template function
1347 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001348 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1349 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1350 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001351 return;
1352 nextToken();
1353 }
1354 }
1355 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001356 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001357 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001358 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1359 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001360 addUnwrappedLine();
1361
Daniel Jasper240dfda2014-03-31 14:23:49 +00001362 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001363 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001364 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001365 // We fall through to parsing a structural element afterwards, so
1366 // class A {} n, m;
1367 // will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00001368}
1369
Nico Weber8696a8d2013-01-09 21:15:03 +00001370void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001371 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001372 do
1373 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001374 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001375 nextToken(); // Skip '>'.
1376}
1377
1378void UnwrappedLineParser::parseObjCUntilAtEnd() {
1379 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001380 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001381 nextToken();
1382 addUnwrappedLine();
1383 break;
1384 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001385 if (FormatTok->is(tok::l_brace)) {
1386 parseBlock(/*MustBeDeclaration=*/false);
1387 // In ObjC interfaces, nothing should be following the "}".
1388 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001389 } else if (FormatTok->is(tok::r_brace)) {
1390 // Ignore stray "}". parseStructuralElement doesn't consume them.
1391 nextToken();
1392 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001393 } else {
1394 parseStructuralElement();
1395 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001396 } while (!eof());
1397}
1398
Nico Weber2ce0ac52013-01-09 23:25:37 +00001399void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001400 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001401 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001402
1403 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001404 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001405 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001406 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001407 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001408 // Skip category, if present.
1409 parseParens();
1410
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001411 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001412 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001413
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001414 if (FormatTok->Tok.is(tok::l_brace)) {
1415 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1416 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1417 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001418 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001419 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001420
1421 // With instance variables, this puts '}' on its own line. Without instance
1422 // variables, this ends the @interface line.
1423 addUnwrappedLine();
1424
Nico Weber8696a8d2013-01-09 21:15:03 +00001425 parseObjCUntilAtEnd();
1426}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001427
Nico Weber8696a8d2013-01-09 21:15:03 +00001428void UnwrappedLineParser::parseObjCProtocol() {
1429 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001430 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001431
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001432 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001433 parseObjCProtocolList();
1434
1435 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001436 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001437 nextToken();
1438 return addUnwrappedLine();
1439 }
1440
1441 addUnwrappedLine();
1442 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001443}
1444
Daniel Jasper3b203a62013-09-05 16:05:56 +00001445LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1446 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001447 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1448 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1449 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1450 E = Line.Tokens.end();
1451 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001452 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001453 }
1454 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1455 E = Line.Tokens.end();
1456 I != E; ++I) {
1457 const UnwrappedLineNode &Node = *I;
1458 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1459 I = Node.Children.begin(),
1460 E = Node.Children.end();
1461 I != E; ++I) {
1462 printDebugInfo(*I, "\nChild: ");
1463 }
1464 }
1465 llvm::dbgs() << "\n";
1466}
1467
Daniel Jasperf7935112012-12-03 18:12:45 +00001468void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001469 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001470 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001471 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001472 if (CurrentLines == &Lines)
1473 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001474 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001475 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001476 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001477 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001478 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001479 I = PreprocessorDirectives.begin(),
1480 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001481 I != E; ++I) {
1482 CurrentLines->push_back(*I);
1483 }
1484 PreprocessorDirectives.clear();
1485 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001486}
1487
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001488bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001489
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001490bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001491 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1492 FormatTok.NewlinesBefore > 0;
1493}
1494
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001495void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1496 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001497 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001498 I = CommentsBeforeNextToken.begin(),
1499 E = CommentsBeforeNextToken.end();
1500 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001501 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001502 addUnwrappedLine();
1503 }
1504 pushToken(*I);
1505 }
1506 if (NewlineBeforeNext && JustComments) {
1507 addUnwrappedLine();
1508 }
1509 CommentsBeforeNextToken.clear();
1510}
1511
Daniel Jasperf7935112012-12-03 18:12:45 +00001512void UnwrappedLineParser::nextToken() {
1513 if (eof())
1514 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001515 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001516 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001517 readToken();
1518}
1519
1520void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001521 bool CommentsInCurrentLine = true;
1522 do {
1523 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001524 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001525 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1526 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001527 // If there is an unfinished unwrapped line, we flush the preprocessor
1528 // directives only after that unwrapped line was finished later.
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001529 bool SwitchToPreprocessorLines =
1530 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001531 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001532 // Comments stored before the preprocessor directive need to be output
1533 // before the preprocessor directive, at the same level as the
1534 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001535 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001536 parsePPDirective();
1537 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001538 while (FormatTok->Type == TT_ConflictStart ||
1539 FormatTok->Type == TT_ConflictEnd ||
1540 FormatTok->Type == TT_ConflictAlternative) {
1541 if (FormatTok->Type == TT_ConflictStart) {
1542 conditionalCompilationStart(/*Unreachable=*/false);
1543 } else if (FormatTok->Type == TT_ConflictAlternative) {
1544 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001545 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001546 conditionalCompilationEnd();
1547 }
1548 FormatTok = Tokens->getNextToken();
1549 FormatTok->MustBreakBefore = true;
1550 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001551
1552 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1553 !Line->InPPDirective) {
1554 continue;
1555 }
1556
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001557 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001558 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001559 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001560 CommentsInCurrentLine = false;
1561 }
1562 if (CommentsInCurrentLine) {
1563 pushToken(FormatTok);
1564 } else {
1565 CommentsBeforeNextToken.push_back(FormatTok);
1566 }
1567 } while (!eof());
1568}
1569
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001570void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001571 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001572 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001573 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001574 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001575 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001576}
1577
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001578} // end namespace format
1579} // end namespace clang