blob: 058544255037ef874cee3dce0724575ce774d476 [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 {
341 // If there is a comma, semicolon or right paren after the closing
342 // brace, we assume this is a braced initializer list. Note that
343 // regardless how we mark inner braces here, we will overwrite the
344 // BlockKind later if we parse a braced list (where all blocks
345 // inside are by default braced lists), or when we explicitly detect
346 // blocks (for example while parsing lambdas).
347 //
348 // We exclude + and - as they can be ObjC visibility modifiers.
349 ProbablyBracedList =
350 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
351 tok::r_paren, tok::r_square, tok::l_brace) ||
352 (NextTok->isBinaryOperator() &&
353 !NextTok->isOneOf(tok::plus, tok::minus));
354 }
355 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000356 Tok->BlockKind = BK_BracedInit;
357 LBraceStack.back()->BlockKind = BK_BracedInit;
358 } else {
359 Tok->BlockKind = BK_Block;
360 LBraceStack.back()->BlockKind = BK_Block;
361 }
Manuel Klimekab419912013-05-23 09:41:43 +0000362 }
363 LBraceStack.pop_back();
364 }
365 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000366 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000367 case tok::semi:
368 case tok::kw_if:
369 case tok::kw_while:
370 case tok::kw_for:
371 case tok::kw_switch:
372 case tok::kw_try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000373 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000374 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000375 break;
376 default:
377 break;
378 }
379 Tok = NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000380 Position += ReadTokens;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000381 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000382 // Assume other blocks for all unclosed opening braces.
383 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000384 if (LBraceStack[i]->BlockKind == BK_Unknown)
385 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000386 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000387
Manuel Klimekab419912013-05-23 09:41:43 +0000388 FormatTok = Tokens->setPosition(StoredPosition);
389}
390
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000391void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
392 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000393 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000394 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000395 nextToken();
396
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000397 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000398
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000399 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
400 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000401 if (AddLevel)
402 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000403 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000404
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000405 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000406 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000407 StructuralError = true;
408 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000409 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000410
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000411 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000412 if (MunchSemi && FormatTok->Tok.is(tok::semi))
413 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000414 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000415}
416
Daniel Jasper4a39c842014-05-06 13:54:10 +0000417static bool IsGoogScope(const UnwrappedLine &Line) {
418 if (Line.Tokens.size() < 4)
419 return false;
420 auto I = Line.Tokens.begin();
421 if (I->Tok->TokenText != "goog")
422 return false;
423 ++I;
424 if (I->Tok->isNot(tok::period))
425 return false;
426 ++I;
427 if (I->Tok->TokenText != "scope")
428 return false;
429 ++I;
430 return I->Tok->is(tok::l_paren);
431}
432
Manuel Klimek516e0542013-09-04 13:25:30 +0000433void UnwrappedLineParser::parseChildBlock() {
434 FormatTok->BlockKind = BK_Block;
435 nextToken();
436 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000437 bool GoogScope =
438 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000439 ScopedLineState LineState(*this);
440 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
441 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000442 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000443 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000444 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000445 }
446 nextToken();
447}
448
Daniel Jasperf7935112012-12-03 18:12:45 +0000449void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000450 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000451 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000452 nextToken();
453
Craig Topper2145bc02014-05-09 08:15:10 +0000454 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000455 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000456 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000457 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000458
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000459 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000460 case tok::pp_define:
461 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000462 return;
463 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000464 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000465 break;
466 case tok::pp_ifdef:
467 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000468 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000469 break;
470 case tok::pp_else:
471 parsePPElse();
472 break;
473 case tok::pp_elif:
474 parsePPElIf();
475 break;
476 case tok::pp_endif:
477 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000478 break;
479 default:
480 parsePPUnknown();
481 break;
482 }
483}
484
Manuel Klimek68b03042014-04-14 09:14:11 +0000485void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
486 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000487 PPStack.push_back(PP_Unreachable);
488 else
489 PPStack.push_back(PP_Conditional);
490}
491
Manuel Klimek68b03042014-04-14 09:14:11 +0000492void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000493 ++PPBranchLevel;
494 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
495 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
496 PPLevelBranchIndex.push_back(0);
497 PPLevelBranchCount.push_back(0);
498 }
499 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000500 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
501 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000502}
503
Manuel Klimek68b03042014-04-14 09:14:11 +0000504void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000505 if (!PPStack.empty())
506 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000507 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
508 if (!PPChainBranchIndex.empty())
509 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000510 conditionalCompilationCondition(
511 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
512 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000513}
514
Manuel Klimek68b03042014-04-14 09:14:11 +0000515void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000516 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
517 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
518 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000519 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
520 }
521 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000522 // Guard against #endif's without #if.
523 if (PPBranchLevel > 0)
524 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000525 if (!PPChainBranchIndex.empty())
526 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000527 if (!PPStack.empty())
528 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000529}
530
531void UnwrappedLineParser::parsePPIf(bool IfDef) {
532 nextToken();
533 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
534 StringRef(FormatTok->Tok.getLiteralData(),
535 FormatTok->Tok.getLength()) == "0") ||
536 FormatTok->Tok.is(tok::kw_false);
537 conditionalCompilationStart(!IfDef && IsLiteralFalse);
538 parsePPUnknown();
539}
540
541void UnwrappedLineParser::parsePPElse() {
542 conditionalCompilationAlternative();
543 parsePPUnknown();
544}
545
546void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
547
548void UnwrappedLineParser::parsePPEndIf() {
549 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000550 parsePPUnknown();
551}
552
Manuel Klimek1abf7892013-01-04 23:34:14 +0000553void UnwrappedLineParser::parsePPDefine() {
554 nextToken();
555
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000556 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000557 parsePPUnknown();
558 return;
559 }
560 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000561 if (FormatTok->Tok.getKind() == tok::l_paren &&
562 FormatTok->WhitespaceRange.getBegin() ==
563 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000564 parseParens();
565 }
566 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000567 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000568
569 // Errors during a preprocessor directive can only affect the layout of the
570 // preprocessor directive, and thus we ignore them. An alternative approach
571 // would be to use the same approach we use on the file level (no
572 // re-indentation if there was a structural error) within the macro
573 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000574 parseFile();
575}
576
577void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000578 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000579 nextToken();
580 } while (!eof());
581 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000582}
583
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000584// Here we blacklist certain tokens that are not usually the first token in an
585// unwrapped line. This is used in attempt to distinguish macro calls without
586// trailing semicolons from other constructs split to several lines.
587bool tokenCanStartNewLine(clang::Token Tok) {
588 // Semicolon can be a null-statement, l_square can be a start of a macro or
589 // a C++11 attribute, but this doesn't seem to be common.
590 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
591 Tok.isNot(tok::l_square) &&
592 // Tokens that can only be used as binary operators and a part of
593 // overloaded operator names.
594 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
595 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
596 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
597 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
598 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
599 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
600 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
601 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
602 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
603 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
604 Tok.isNot(tok::lesslessequal) &&
605 // Colon is used in labels, base class lists, initializer lists,
606 // range-based for loops, ternary operator, but should never be the
607 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000608 Tok.isNot(tok::colon) &&
609 // 'noexcept' is a trailing annotation.
610 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000611}
612
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000613void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000614 assert(!FormatTok->Tok.is(tok::l_brace));
615 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000616 case tok::at:
617 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000618 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000619 parseBracedList();
620 break;
621 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000622 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000623 case tok::objc_public:
624 case tok::objc_protected:
625 case tok::objc_package:
626 case tok::objc_private:
627 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000628 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000629 case tok::objc_implementation:
630 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000631 case tok::objc_protocol:
632 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000633 case tok::objc_end:
634 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000635 case tok::objc_optional:
636 case tok::objc_required:
637 nextToken();
638 addUnwrappedLine();
639 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000640 default:
641 break;
642 }
643 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000644 case tok::kw_namespace:
645 parseNamespace();
646 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000647 case tok::kw_inline:
648 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000649 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000650 parseNamespace();
651 return;
652 }
653 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000654 case tok::kw_public:
655 case tok::kw_protected:
656 case tok::kw_private:
Daniel Jasperf7935112012-12-03 18:12:45 +0000657 parseAccessSpecifier();
658 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000659 case tok::kw_if:
660 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000661 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000662 case tok::kw_for:
663 case tok::kw_while:
664 parseForOrWhileLoop();
665 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000666 case tok::kw_do:
667 parseDoWhile();
668 return;
669 case tok::kw_switch:
670 parseSwitch();
671 return;
672 case tok::kw_default:
673 nextToken();
674 parseLabel();
675 return;
676 case tok::kw_case:
677 parseCaseLabel();
678 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000679 case tok::kw_try:
680 parseTryCatch();
681 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000682 case tok::kw_extern:
683 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000684 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000685 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000686 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000687 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000688 addUnwrappedLine();
689 return;
690 }
691 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000692 break;
693 case tok::identifier:
694 if (FormatTok->IsForEachMacro) {
695 parseForOrWhileLoop();
696 return;
697 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000698 // In all other cases, parse the declaration.
699 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000700 default:
701 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000702 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000703 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000704 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000705 case tok::at:
706 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000707 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000708 parseBracedList();
709 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000710 case tok::kw_enum:
711 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000712 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000713 case tok::kw_typedef:
714 nextToken();
715 // FIXME: Use the IdentifierTable instead.
716 if (FormatTok->TokenText == "NS_ENUM")
717 parseEnum();
718 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000719 case tok::kw_struct:
720 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000721 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000722 parseRecord();
723 // A record declaration or definition is always the start of a structural
724 // element.
725 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000726 case tok::semi:
727 nextToken();
728 addUnwrappedLine();
729 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000730 case tok::r_brace:
731 addUnwrappedLine();
732 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000733 case tok::l_paren:
734 parseParens();
735 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000736 case tok::caret:
737 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000738 if (FormatTok->Tok.isAnyIdentifier() ||
739 FormatTok->isSimpleTypeSpecifier())
740 nextToken();
741 if (FormatTok->is(tok::l_paren))
742 parseParens();
743 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000744 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000745 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000746 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000747 if (!tryToParseBracedList()) {
748 // A block outside of parentheses must be the last part of a
749 // structural element.
750 // FIXME: Figure out cases where this is not true, and add projections
751 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000752 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000753 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000754 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000755 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000756 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000757 return;
758 }
759 // Otherwise this was a braced init list, and the structural
760 // element continues.
761 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000762 case tok::kw_try:
763 // We arrive here when parsing function-try blocks.
764 parseTryCatch();
765 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000766 case tok::identifier: {
767 StringRef Text = FormatTok->TokenText;
Daniel Jasper069e5f42014-05-20 11:14:57 +0000768 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function") {
769 tryToParseJSFunction();
770 break;
771 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000772 nextToken();
Alexander Kornienkode644272013-04-08 22:16:06 +0000773 if (Line->Tokens.size() == 1) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000774 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000775 parseLabel();
776 return;
777 }
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000778 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000779 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000780 parseParens();
Daniel Jasper352dae12014-01-03 11:50:46 +0000781 if (FormatTok->NewlinesBefore > 0 &&
Alexander Kornienkoce081262014-03-18 14:35:20 +0000782 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000783 addUnwrappedLine();
784 return;
785 }
Daniel Jasper40e19212013-05-29 13:16:10 +0000786 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
787 Text == Text.upper()) {
788 // Recognize free-standing macros like Q_OBJECT.
789 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000790 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000791 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000792 }
793 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000794 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000795 case tok::equal:
796 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000797 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000798 parseBracedList();
799 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000800 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000801 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000802 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000803 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000804 default:
805 nextToken();
806 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000807 }
808 } while (!eof());
809}
810
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000811bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000812 // FIXME: This is a dirty way to access the previous token. Find a better
813 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000814 if (!Line->Tokens.empty() &&
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000815 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator) ||
816 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000817 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000818 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000819 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000820 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000821 assert(FormatTok->is(tok::l_square));
822 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000823 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000824 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000825
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000826 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000827 if (FormatTok->isSimpleTypeSpecifier()) {
828 nextToken();
829 continue;
830 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000831 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000832 case tok::l_brace:
833 break;
834 case tok::l_paren:
835 parseParens();
836 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000837 case tok::less:
838 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000839 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000840 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000841 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000842 nextToken();
843 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000844 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000845 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000846 nextToken();
847 break;
848 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000849 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000850 }
851 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000852 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000853 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000854 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000855}
856
857bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
858 nextToken();
859 if (FormatTok->is(tok::equal)) {
860 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000861 if (FormatTok->is(tok::r_square)) {
862 nextToken();
863 return true;
864 }
865 if (FormatTok->isNot(tok::comma))
866 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000867 nextToken();
868 } else if (FormatTok->is(tok::amp)) {
869 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000870 if (FormatTok->is(tok::r_square)) {
871 nextToken();
872 return true;
873 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000874 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
875 return false;
876 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000877 if (FormatTok->is(tok::comma))
878 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000879 } else if (FormatTok->is(tok::r_square)) {
880 nextToken();
881 return true;
882 }
883 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000884 if (FormatTok->is(tok::amp))
885 nextToken();
886 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
887 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000888 nextToken();
889 if (FormatTok->is(tok::comma)) {
890 nextToken();
891 } else if (FormatTok->is(tok::r_square)) {
892 nextToken();
893 return true;
894 } else {
895 return false;
896 }
897 } while (!eof());
898 return false;
899}
900
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000901void UnwrappedLineParser::tryToParseJSFunction() {
902 nextToken();
903 if (FormatTok->isNot(tok::l_paren))
904 return;
905 nextToken();
906 while (FormatTok->isNot(tok::l_brace)) {
907 // Err on the side of caution in order to avoid consuming the full file in
908 // case of incomplete code.
909 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
910 tok::comment))
911 return;
912 nextToken();
913 }
914 parseChildBlock();
915}
916
Manuel Klimekab419912013-05-23 09:41:43 +0000917bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000918 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +0000919 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000920 assert(FormatTok->BlockKind != BK_Unknown);
921 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +0000922 return false;
923 parseBracedList();
924 return true;
925}
926
Daniel Jasper015ed022013-09-13 09:20:45 +0000927bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
928 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000929 nextToken();
930
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000931 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
932 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000933 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000934 if (Style.Language == FormatStyle::LK_JavaScript &&
935 FormatTok->TokenText == "function") {
936 tryToParseJSFunction();
937 continue;
938 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000939 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +0000940 case tok::caret:
941 nextToken();
942 if (FormatTok->is(tok::l_brace)) {
943 parseChildBlock();
944 }
945 break;
946 case tok::l_square:
947 tryToParseLambda();
948 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000949 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000950 // Assume there are no blocks inside a braced init list apart
951 // from the ones we explicitly parse out (like lambdas).
952 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000953 parseBracedList();
954 break;
955 case tok::r_brace:
956 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +0000957 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000958 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +0000959 HasError = true;
960 if (!ContinueOnSemicolons)
961 return !HasError;
962 nextToken();
963 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000964 case tok::comma:
965 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000966 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000967 default:
968 nextToken();
969 break;
970 }
971 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +0000972 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000973}
974
Daniel Jasperf7935112012-12-03 18:12:45 +0000975void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000976 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +0000977 nextToken();
978 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000979 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000980 case tok::l_paren:
981 parseParens();
982 break;
983 case tok::r_paren:
984 nextToken();
985 return;
Daniel Jasper393564f2013-05-31 14:56:29 +0000986 case tok::r_brace:
987 // A "}" inside parenthesis is an error if there wasn't a matching "{".
988 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000989 case tok::l_square:
990 tryToParseLambda();
991 break;
Nico Weber67ffb332013-02-10 04:38:23 +0000992 case tok::l_brace: {
Manuel Klimekab419912013-05-23 09:41:43 +0000993 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +0000994 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +0000995 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000996 break;
Nico Weber67ffb332013-02-10 04:38:23 +0000997 }
Nico Weber372d8dc2013-02-10 20:35:35 +0000998 case tok::at:
999 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001000 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001001 parseBracedList();
1002 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001003 default:
1004 nextToken();
1005 break;
1006 }
1007 } while (!eof());
1008}
1009
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001010void UnwrappedLineParser::parseSquare() {
1011 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1012 if (tryToParseLambda())
1013 return;
1014 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001015 switch (FormatTok->Tok.getKind()) {
1016 case tok::l_paren:
1017 parseParens();
1018 break;
1019 case tok::r_square:
1020 nextToken();
1021 return;
1022 case tok::r_brace:
1023 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1024 return;
1025 case tok::l_square:
1026 parseSquare();
1027 break;
1028 case tok::l_brace: {
1029 if (!tryToParseBracedList()) {
1030 parseChildBlock();
1031 }
1032 break;
1033 }
1034 case tok::at:
1035 nextToken();
1036 if (FormatTok->Tok.is(tok::l_brace))
1037 parseBracedList();
1038 break;
1039 default:
1040 nextToken();
1041 break;
1042 }
1043 } while (!eof());
1044}
1045
Daniel Jasperf7935112012-12-03 18:12:45 +00001046void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001047 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001048 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001049 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001050 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001051 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001052 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001053 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001054 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001055 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1056 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001057 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001058 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001059 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001060 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001061 } else {
1062 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001063 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001064 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001065 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001066 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001067 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001068 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001069 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001070 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001071 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001072 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001073 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001074 parseIfThenElse();
1075 } else {
1076 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001077 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001078 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001079 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001080 }
1081 } else if (NeedsUnwrappedLine) {
1082 addUnwrappedLine();
1083 }
1084}
1085
Daniel Jasper04a71a42014-05-08 11:58:24 +00001086void UnwrappedLineParser::parseTryCatch() {
1087 assert(FormatTok->is(tok::kw_try) && "'try' expected");
1088 nextToken();
1089 bool NeedsUnwrappedLine = false;
1090 if (FormatTok->is(tok::colon)) {
1091 // We are in a function try block, what comes is an initializer list.
1092 nextToken();
1093 while (FormatTok->is(tok::identifier)) {
1094 nextToken();
1095 if (FormatTok->is(tok::l_paren))
1096 parseParens();
1097 else
1098 StructuralError = true;
1099 if (FormatTok->is(tok::comma))
1100 nextToken();
1101 }
1102 }
1103 if (FormatTok->is(tok::l_brace)) {
1104 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1105 parseBlock(/*MustBeDeclaration=*/false);
1106 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1107 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1108 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1109 addUnwrappedLine();
1110 } else {
1111 NeedsUnwrappedLine = true;
1112 }
1113 } else if (!FormatTok->is(tok::kw_catch)) {
1114 // The C++ standard requires a compound-statement after a try.
1115 // If there's none, we try to assume there's a structuralElement
1116 // and try to continue.
1117 StructuralError = true;
1118 addUnwrappedLine();
1119 ++Line->Level;
1120 parseStructuralElement();
1121 --Line->Level;
1122 }
1123 while (FormatTok->is(tok::kw_catch) ||
1124 (Style.Language == FormatStyle::LK_JavaScript &&
1125 FormatTok->TokenText == "finally")) {
1126 nextToken();
1127 while (FormatTok->isNot(tok::l_brace)) {
1128 if (FormatTok->is(tok::l_paren)) {
1129 parseParens();
1130 continue;
1131 }
1132 if (FormatTok->isOneOf(tok::semi, tok::r_brace))
1133 return;
1134 nextToken();
1135 }
1136 NeedsUnwrappedLine = false;
1137 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1138 parseBlock(/*MustBeDeclaration=*/false);
1139 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1140 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1141 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1142 addUnwrappedLine();
1143 } else {
1144 NeedsUnwrappedLine = true;
1145 }
1146 }
1147 if (NeedsUnwrappedLine) {
1148 addUnwrappedLine();
1149 }
1150}
1151
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001152void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001153 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001154 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001155 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001156 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001157 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001158 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001159 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1160 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001161 addUnwrappedLine();
1162
Daniel Jasper65ee3472013-07-31 23:16:02 +00001163 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1164 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1165 DeclarationScopeStack.size() > 1);
1166 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001167 // Munch the semicolon after a namespace. This is more common than one would
1168 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001169 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001170 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001171 addUnwrappedLine();
1172 }
1173 // FIXME: Add error handling.
1174}
1175
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001176void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001177 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1178 FormatTok->IsForEachMacro) &&
1179 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001180 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001181 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001182 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001183 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001184 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001185 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001186 addUnwrappedLine();
1187 } else {
1188 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001189 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001190 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001191 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001192 }
1193}
1194
Daniel Jasperf7935112012-12-03 18:12:45 +00001195void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001196 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001197 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001198 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001199 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001200 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001201 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1202 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001203 } else {
1204 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001205 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001206 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001207 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001208 }
1209
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001210 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001211 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001212 addUnwrappedLine();
1213 return;
1214 }
1215
Daniel Jasperf7935112012-12-03 18:12:45 +00001216 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001217 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001218}
1219
1220void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001221 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001222 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001223 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001224 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001225 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001226 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001227 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001228 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001229 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1230 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1231 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001232 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001233 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001234 parseStructuralElement();
1235 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001236 addUnwrappedLine();
1237 } else {
1238 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001239 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001240 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001241}
1242
1243void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001244 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001245 // FIXME: fix handling of complex expressions here.
1246 do {
1247 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001248 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001249 parseLabel();
1250}
1251
1252void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001253 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001254 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001255 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001256 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001257 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001258 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001259 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001260 addUnwrappedLine();
1261 } else {
1262 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001263 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001264 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001265 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001266 }
1267}
1268
1269void UnwrappedLineParser::parseAccessSpecifier() {
1270 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001271 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001272 if (FormatTok->is(tok::identifier) &&
1273 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001274 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001275 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001276 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001277 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001278 addUnwrappedLine();
1279}
1280
1281void UnwrappedLineParser::parseEnum() {
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001282 if (FormatTok->Tok.is(tok::kw_enum)) {
1283 // Won't be 'enum' for NS_ENUMs.
1284 nextToken();
1285 }
Daniel Jasper2b41a822013-08-20 12:42:50 +00001286 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001287 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1288 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001289 while (FormatTok->Tok.getIdentifierInfo() ||
1290 FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001291 nextToken();
1292 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001293 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001294 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001295 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001296 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001297 nextToken();
1298 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001299 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001300 FormatTok->BlockKind = BK_Block;
1301 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1302 if (HasError) {
1303 if (FormatTok->is(tok::semi))
1304 nextToken();
Manuel Klimeka027f302013-08-07 19:20:45 +00001305 addUnwrappedLine();
Daniel Jasper015ed022013-09-13 09:20:45 +00001306 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001307 }
1308 // We fall through to parsing a structural element afterwards, so that in
1309 // enum A {} n, m;
1310 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperf7935112012-12-03 18:12:45 +00001311}
1312
Manuel Klimeke01bab52013-01-15 13:38:33 +00001313void UnwrappedLineParser::parseRecord() {
Manuel Klimek28cacc72013-01-07 18:10:23 +00001314 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001315 if (FormatTok->Tok.is(tok::identifier) ||
1316 FormatTok->Tok.is(tok::kw___attribute) ||
Manuel Klimek91e48582013-10-07 09:15:41 +00001317 FormatTok->Tok.is(tok::kw___declspec) ||
1318 FormatTok->Tok.is(tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001319 nextToken();
1320 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001321 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001322 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001323 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001324 // The actual identifier can be a nested name specifier, and in macros
1325 // it is often token-pasted.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001326 while (FormatTok->Tok.is(tok::identifier) ||
1327 FormatTok->Tok.is(tok::coloncolon) ||
1328 FormatTok->Tok.is(tok::hashhash))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001329 nextToken();
1330
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001331 // Note that parsing away template declarations here leads to incorrectly
1332 // accepting function declarations as record declarations.
1333 // In general, we cannot solve this problem. Consider:
1334 // class A<int> B() {}
1335 // which can be a function definition or a class definition when B() is a
1336 // macro. If we find enough real-world cases where this is a problem, we
1337 // can parse for the 'template' keyword in the beginning of the statement,
1338 // and thus rule out the record production in case there is no template
1339 // (this would still leave us with an ambiguity between template function
1340 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001341 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1342 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1343 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001344 return;
1345 nextToken();
1346 }
1347 }
1348 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001349 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001350 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001351 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1352 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001353 addUnwrappedLine();
1354
Daniel Jasper240dfda2014-03-31 14:23:49 +00001355 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001356 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001357 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001358 // We fall through to parsing a structural element afterwards, so
1359 // class A {} n, m;
1360 // will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00001361}
1362
Nico Weber8696a8d2013-01-09 21:15:03 +00001363void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001364 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001365 do
1366 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001367 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001368 nextToken(); // Skip '>'.
1369}
1370
1371void UnwrappedLineParser::parseObjCUntilAtEnd() {
1372 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001373 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001374 nextToken();
1375 addUnwrappedLine();
1376 break;
1377 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001378 if (FormatTok->is(tok::l_brace)) {
1379 parseBlock(/*MustBeDeclaration=*/false);
1380 // In ObjC interfaces, nothing should be following the "}".
1381 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001382 } else if (FormatTok->is(tok::r_brace)) {
1383 // Ignore stray "}". parseStructuralElement doesn't consume them.
1384 nextToken();
1385 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001386 } else {
1387 parseStructuralElement();
1388 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001389 } while (!eof());
1390}
1391
Nico Weber2ce0ac52013-01-09 23:25:37 +00001392void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001393 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001394 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001395
1396 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001397 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001398 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001399 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001400 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001401 // Skip category, if present.
1402 parseParens();
1403
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001404 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001405 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001406
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001407 if (FormatTok->Tok.is(tok::l_brace)) {
1408 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1409 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1410 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001411 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001412 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001413
1414 // With instance variables, this puts '}' on its own line. Without instance
1415 // variables, this ends the @interface line.
1416 addUnwrappedLine();
1417
Nico Weber8696a8d2013-01-09 21:15:03 +00001418 parseObjCUntilAtEnd();
1419}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001420
Nico Weber8696a8d2013-01-09 21:15:03 +00001421void UnwrappedLineParser::parseObjCProtocol() {
1422 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001423 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001424
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001425 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001426 parseObjCProtocolList();
1427
1428 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001429 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001430 nextToken();
1431 return addUnwrappedLine();
1432 }
1433
1434 addUnwrappedLine();
1435 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001436}
1437
Daniel Jasper3b203a62013-09-05 16:05:56 +00001438LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1439 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001440 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1441 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1442 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1443 E = Line.Tokens.end();
1444 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001445 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001446 }
1447 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1448 E = Line.Tokens.end();
1449 I != E; ++I) {
1450 const UnwrappedLineNode &Node = *I;
1451 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1452 I = Node.Children.begin(),
1453 E = Node.Children.end();
1454 I != E; ++I) {
1455 printDebugInfo(*I, "\nChild: ");
1456 }
1457 }
1458 llvm::dbgs() << "\n";
1459}
1460
Daniel Jasperf7935112012-12-03 18:12:45 +00001461void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001462 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001463 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001464 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001465 if (CurrentLines == &Lines)
1466 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001467 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001468 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001469 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001470 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001471 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001472 I = PreprocessorDirectives.begin(),
1473 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001474 I != E; ++I) {
1475 CurrentLines->push_back(*I);
1476 }
1477 PreprocessorDirectives.clear();
1478 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001479}
1480
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001481bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001482
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001483bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001484 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1485 FormatTok.NewlinesBefore > 0;
1486}
1487
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001488void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1489 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001490 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001491 I = CommentsBeforeNextToken.begin(),
1492 E = CommentsBeforeNextToken.end();
1493 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001494 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001495 addUnwrappedLine();
1496 }
1497 pushToken(*I);
1498 }
1499 if (NewlineBeforeNext && JustComments) {
1500 addUnwrappedLine();
1501 }
1502 CommentsBeforeNextToken.clear();
1503}
1504
Daniel Jasperf7935112012-12-03 18:12:45 +00001505void UnwrappedLineParser::nextToken() {
1506 if (eof())
1507 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001508 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001509 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001510 readToken();
1511}
1512
1513void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001514 bool CommentsInCurrentLine = true;
1515 do {
1516 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001517 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001518 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1519 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001520 // If there is an unfinished unwrapped line, we flush the preprocessor
1521 // directives only after that unwrapped line was finished later.
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001522 bool SwitchToPreprocessorLines =
1523 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001524 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001525 // Comments stored before the preprocessor directive need to be output
1526 // before the preprocessor directive, at the same level as the
1527 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001528 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001529 parsePPDirective();
1530 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001531 while (FormatTok->Type == TT_ConflictStart ||
1532 FormatTok->Type == TT_ConflictEnd ||
1533 FormatTok->Type == TT_ConflictAlternative) {
1534 if (FormatTok->Type == TT_ConflictStart) {
1535 conditionalCompilationStart(/*Unreachable=*/false);
1536 } else if (FormatTok->Type == TT_ConflictAlternative) {
1537 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001538 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001539 conditionalCompilationEnd();
1540 }
1541 FormatTok = Tokens->getNextToken();
1542 FormatTok->MustBreakBefore = true;
1543 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001544
1545 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1546 !Line->InPPDirective) {
1547 continue;
1548 }
1549
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001550 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001551 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001552 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001553 CommentsInCurrentLine = false;
1554 }
1555 if (CommentsInCurrentLine) {
1556 pushToken(FormatTok);
1557 } else {
1558 CommentsBeforeNextToken.push_back(FormatTok);
1559 }
1560 } while (!eof());
1561}
1562
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001563void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001564 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001565 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001566 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001567 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001568 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001569}
1570
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001571} // end namespace format
1572} // end namespace clang