blob: 7d54156c3bc0c79b810809fc371563e17d0e7e23 [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)
David Blaikieefb6eb22014-08-09 20:02:07 +0000125 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000126 if (SwitchToPreprocessorLines)
127 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000128 else if (!Parser.Line->Tokens.empty())
129 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000130 PreBlockLine = std::move(Parser.Line);
131 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000132 Parser.Line->Level = PreBlockLine->Level;
133 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000134 }
135
136 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000137 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000138 Parser.addUnwrappedLine();
139 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000140 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000141 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000142 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
143 Parser.MustBreakBeforeNextToken = true;
144 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000145 }
146
147private:
148 UnwrappedLineParser &Parser;
149
David Blaikieefb6eb22014-08-09 20:02:07 +0000150 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000151 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000152};
153
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000154class CompoundStatementIndenter {
155public:
156 CompoundStatementIndenter(UnwrappedLineParser *Parser,
157 const FormatStyle &Style, unsigned &LineLevel)
158 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
159 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
160 Parser->addUnwrappedLine();
161 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
162 Parser->addUnwrappedLine();
163 ++LineLevel;
164 }
165 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000166 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000167
168private:
169 unsigned &LineLevel;
170 unsigned OldLineLevel;
171};
172
Craig Topper69665e12013-07-01 04:21:54 +0000173namespace {
174
Manuel Klimekab419912013-05-23 09:41:43 +0000175class IndexedTokenSource : public FormatTokenSource {
176public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000177 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000178 : Tokens(Tokens), Position(-1) {}
179
Craig Topperfb6b25b2014-03-15 04:29:04 +0000180 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000181 ++Position;
182 return Tokens[Position];
183 }
184
Craig Topperfb6b25b2014-03-15 04:29:04 +0000185 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000186 assert(Position >= 0);
187 return Position;
188 }
189
Craig Topperfb6b25b2014-03-15 04:29:04 +0000190 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000191 Position = P;
192 return Tokens[Position];
193 }
194
Manuel Klimek71814b42013-10-11 21:25:45 +0000195 void reset() { Position = -1; }
196
Manuel Klimekab419912013-05-23 09:41:43 +0000197private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000198 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000199 int Position;
200};
201
Craig Topper69665e12013-07-01 04:21:54 +0000202} // end anonymous namespace
203
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000204UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000205 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000206 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000207 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
208 CurrentLines(&Lines), StructuralError(false), Style(Style),
209 Tokens(nullptr), Callback(Callback), AllTokens(Tokens),
210 PPBranchLevel(-1) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000211
212void UnwrappedLineParser::reset() {
213 PPBranchLevel = -1;
214 Line.reset(new UnwrappedLine);
215 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000216 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000217 MustBreakBeforeNextToken = false;
218 PreprocessorDirectives.clear();
219 CurrentLines = &Lines;
220 DeclarationScopeStack.clear();
221 StructuralError = false;
222 PPStack.clear();
223}
Daniel Jasperf7935112012-12-03 18:12:45 +0000224
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000225bool UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000226 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimek71814b42013-10-11 21:25:45 +0000227 do {
228 DEBUG(llvm::dbgs() << "----\n");
229 reset();
230 Tokens = &TokenSource;
231 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000232
Manuel Klimek71814b42013-10-11 21:25:45 +0000233 readToken();
234 parseFile();
235 // Create line with eof token.
236 pushToken(FormatTok);
237 addUnwrappedLine();
238
239 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
240 E = Lines.end();
241 I != E; ++I) {
242 Callback.consumeUnwrappedLine(*I);
243 }
244 Callback.finishRun();
245 Lines.clear();
246 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000247 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000248 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
249 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
250 }
251 if (!PPLevelBranchIndex.empty()) {
252 ++PPLevelBranchIndex.back();
253 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
254 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
255 }
256 } while (!PPLevelBranchIndex.empty());
257
Manuel Klimek1a18c402013-04-12 14:13:36 +0000258 return StructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000259}
260
Manuel Klimek1a18c402013-04-12 14:13:36 +0000261void UnwrappedLineParser::parseFile() {
Daniel Jasperdd9276e2013-03-22 16:55:40 +0000262 ScopedDeclarationState DeclarationState(
263 *Line, DeclarationScopeStack,
264 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber9096fc02013-06-26 00:30:14 +0000265 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000266 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000267 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000268 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000269}
270
Manuel Klimek1a18c402013-04-12 14:13:36 +0000271void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000272 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000273 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000274 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000275 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000276 nextToken();
277 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000278 break;
279 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000280 // FIXME: Add parameter whether this can happen - if this happens, we must
281 // be in a non-declaration context.
Nico Weber9096fc02013-06-26 00:30:14 +0000282 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000283 addUnwrappedLine();
284 break;
285 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000286 if (HasOpeningBrace)
287 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000288 StructuralError = true;
289 nextToken();
290 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000291 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000292 case tok::kw_default:
293 case tok::kw_case:
Daniel Jasper72407622013-09-02 08:26:29 +0000294 if (!SwitchLabelEncountered &&
295 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
296 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000297 SwitchLabelEncountered = true;
298 parseStructuralElement();
299 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000300 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000301 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000302 break;
303 }
304 } while (!eof());
305}
306
Manuel Klimekab419912013-05-23 09:41:43 +0000307void UnwrappedLineParser::calculateBraceTypes() {
308 // We'll parse forward through the tokens until we hit
309 // a closing brace or eof - note that getNextToken() will
310 // parse macros, so this will magically work inside macro
311 // definitions, too.
312 unsigned StoredPosition = Tokens->getPosition();
313 unsigned Position = StoredPosition;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000314 FormatToken *Tok = FormatTok;
Manuel Klimekab419912013-05-23 09:41:43 +0000315 // Keep a stack of positions of lbrace tokens. We will
316 // update information about whether an lbrace starts a
317 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000318 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000319 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000320 do {
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000321 // Get next none-comment token.
322 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000323 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000324 do {
325 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000326 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000327 } while (NextTok->is(tok::comment));
328
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000329 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000330 case tok::l_brace:
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000331 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000332 break;
333 case tok::r_brace:
334 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000335 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Daniel Jasper220c0d12014-04-10 07:27:12 +0000336 bool ProbablyBracedList = false;
337 if (Style.Language == FormatStyle::LK_Proto) {
338 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
339 } else {
Daniel Jasper91b032a2014-05-22 12:46:38 +0000340 // Using OriginalColumn to distinguish between ObjC methods and
341 // binary operators is a bit hacky.
342 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
343 NextTok->OriginalColumn == 0;
344
Daniel Jasper220c0d12014-04-10 07:27:12 +0000345 // If there is a comma, semicolon or right paren after the closing
346 // brace, we assume this is a braced initializer list. Note that
347 // regardless how we mark inner braces here, we will overwrite the
348 // BlockKind later if we parse a braced list (where all blocks
349 // inside are by default braced lists), or when we explicitly detect
350 // blocks (for example while parsing lambdas).
351 //
352 // We exclude + and - as they can be ObjC visibility modifiers.
353 ProbablyBracedList =
354 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
Daniel Jasper438059e2014-05-22 12:11:13 +0000355 tok::r_paren, tok::r_square, tok::l_brace,
Daniel Jasper65df5aa2014-08-04 14:51:02 +0000356 tok::l_paren, tok::ellipsis) ||
Daniel Jasper91b032a2014-05-22 12:46:38 +0000357 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000358 }
359 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000360 Tok->BlockKind = BK_BracedInit;
361 LBraceStack.back()->BlockKind = BK_BracedInit;
362 } else {
363 Tok->BlockKind = BK_Block;
364 LBraceStack.back()->BlockKind = BK_Block;
365 }
Manuel Klimekab419912013-05-23 09:41:43 +0000366 }
367 LBraceStack.pop_back();
368 }
369 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000370 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000371 case tok::semi:
372 case tok::kw_if:
373 case tok::kw_while:
374 case tok::kw_for:
375 case tok::kw_switch:
376 case tok::kw_try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000377 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000378 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000379 break;
380 default:
381 break;
382 }
383 Tok = NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000384 Position += ReadTokens;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000385 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000386 // Assume other blocks for all unclosed opening braces.
387 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000388 if (LBraceStack[i]->BlockKind == BK_Unknown)
389 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000390 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000391
Manuel Klimekab419912013-05-23 09:41:43 +0000392 FormatTok = Tokens->setPosition(StoredPosition);
393}
394
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000395void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
396 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000397 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000398 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000399 nextToken();
400
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000401 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000402
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000403 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
404 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000405 if (AddLevel)
406 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000407 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000408
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000409 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000410 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000411 StructuralError = true;
412 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000413 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000414
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000415 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000416 if (MunchSemi && FormatTok->Tok.is(tok::semi))
417 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000418 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000419}
420
Daniel Jasper4a39c842014-05-06 13:54:10 +0000421static bool IsGoogScope(const UnwrappedLine &Line) {
422 if (Line.Tokens.size() < 4)
423 return false;
424 auto I = Line.Tokens.begin();
425 if (I->Tok->TokenText != "goog")
426 return false;
427 ++I;
428 if (I->Tok->isNot(tok::period))
429 return false;
430 ++I;
431 if (I->Tok->TokenText != "scope")
432 return false;
433 ++I;
434 return I->Tok->is(tok::l_paren);
435}
436
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000437static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
438 const FormatToken &InitialToken) {
439 switch (Style.BreakBeforeBraces) {
440 case FormatStyle::BS_Linux:
441 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
442 case FormatStyle::BS_Allman:
443 case FormatStyle::BS_GNU:
444 return true;
445 default:
446 return false;
447 }
448}
449
Manuel Klimek516e0542013-09-04 13:25:30 +0000450void UnwrappedLineParser::parseChildBlock() {
451 FormatTok->BlockKind = BK_Block;
452 nextToken();
453 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000454 bool GoogScope =
455 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000456 ScopedLineState LineState(*this);
457 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
458 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000459 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000460 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000461 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000462 }
463 nextToken();
464}
465
Daniel Jasperf7935112012-12-03 18:12:45 +0000466void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000467 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000468 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000469 nextToken();
470
Craig Topper2145bc02014-05-09 08:15:10 +0000471 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000472 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000473 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000474 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000475
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000476 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000477 case tok::pp_define:
478 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000479 return;
480 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000481 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000482 break;
483 case tok::pp_ifdef:
484 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000485 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000486 break;
487 case tok::pp_else:
488 parsePPElse();
489 break;
490 case tok::pp_elif:
491 parsePPElIf();
492 break;
493 case tok::pp_endif:
494 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000495 break;
496 default:
497 parsePPUnknown();
498 break;
499 }
500}
501
Manuel Klimek68b03042014-04-14 09:14:11 +0000502void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
503 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000504 PPStack.push_back(PP_Unreachable);
505 else
506 PPStack.push_back(PP_Conditional);
507}
508
Manuel Klimek68b03042014-04-14 09:14:11 +0000509void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000510 ++PPBranchLevel;
511 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
512 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
513 PPLevelBranchIndex.push_back(0);
514 PPLevelBranchCount.push_back(0);
515 }
516 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000517 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
518 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000519}
520
Manuel Klimek68b03042014-04-14 09:14:11 +0000521void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000522 if (!PPStack.empty())
523 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000524 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
525 if (!PPChainBranchIndex.empty())
526 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000527 conditionalCompilationCondition(
528 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
529 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000530}
531
Manuel Klimek68b03042014-04-14 09:14:11 +0000532void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000533 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
534 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
535 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000536 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
537 }
538 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000539 // Guard against #endif's without #if.
540 if (PPBranchLevel > 0)
541 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000542 if (!PPChainBranchIndex.empty())
543 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000544 if (!PPStack.empty())
545 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000546}
547
548void UnwrappedLineParser::parsePPIf(bool IfDef) {
549 nextToken();
550 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
551 StringRef(FormatTok->Tok.getLiteralData(),
552 FormatTok->Tok.getLength()) == "0") ||
553 FormatTok->Tok.is(tok::kw_false);
554 conditionalCompilationStart(!IfDef && IsLiteralFalse);
555 parsePPUnknown();
556}
557
558void UnwrappedLineParser::parsePPElse() {
559 conditionalCompilationAlternative();
560 parsePPUnknown();
561}
562
563void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
564
565void UnwrappedLineParser::parsePPEndIf() {
566 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000567 parsePPUnknown();
568}
569
Manuel Klimek1abf7892013-01-04 23:34:14 +0000570void UnwrappedLineParser::parsePPDefine() {
571 nextToken();
572
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000573 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000574 parsePPUnknown();
575 return;
576 }
577 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000578 if (FormatTok->Tok.getKind() == tok::l_paren &&
579 FormatTok->WhitespaceRange.getBegin() ==
580 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000581 parseParens();
582 }
583 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000584 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000585
586 // Errors during a preprocessor directive can only affect the layout of the
587 // preprocessor directive, and thus we ignore them. An alternative approach
588 // would be to use the same approach we use on the file level (no
589 // re-indentation if there was a structural error) within the macro
590 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000591 parseFile();
592}
593
594void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000595 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000596 nextToken();
597 } while (!eof());
598 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000599}
600
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000601// Here we blacklist certain tokens that are not usually the first token in an
602// unwrapped line. This is used in attempt to distinguish macro calls without
603// trailing semicolons from other constructs split to several lines.
604bool tokenCanStartNewLine(clang::Token Tok) {
605 // Semicolon can be a null-statement, l_square can be a start of a macro or
606 // a C++11 attribute, but this doesn't seem to be common.
607 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
608 Tok.isNot(tok::l_square) &&
609 // Tokens that can only be used as binary operators and a part of
610 // overloaded operator names.
611 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
612 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
613 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
614 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
615 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
616 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
617 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
618 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
619 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
620 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
621 Tok.isNot(tok::lesslessequal) &&
622 // Colon is used in labels, base class lists, initializer lists,
623 // range-based for loops, ternary operator, but should never be the
624 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000625 Tok.isNot(tok::colon) &&
626 // 'noexcept' is a trailing annotation.
627 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000628}
629
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000630void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000631 assert(!FormatTok->Tok.is(tok::l_brace));
632 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000633 case tok::at:
634 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000635 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000636 parseBracedList();
637 break;
638 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000639 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000640 case tok::objc_public:
641 case tok::objc_protected:
642 case tok::objc_package:
643 case tok::objc_private:
644 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000645 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000646 case tok::objc_implementation:
647 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000648 case tok::objc_protocol:
649 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000650 case tok::objc_end:
651 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000652 case tok::objc_optional:
653 case tok::objc_required:
654 nextToken();
655 addUnwrappedLine();
656 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000657 default:
658 break;
659 }
660 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000661 case tok::kw_asm:
662 FormatTok->Finalized = true;
663 nextToken();
664 if (FormatTok->is(tok::l_brace)) {
Daniel Jasper4429f142014-08-27 17:16:46 +0000665 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000666 FormatTok->Finalized = true;
667 if (FormatTok->is(tok::r_brace)) {
668 nextToken();
669 break;
670 }
671 nextToken();
672 }
673 }
674 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000675 case tok::kw_namespace:
676 parseNamespace();
677 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000678 case tok::kw_inline:
679 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000680 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000681 parseNamespace();
682 return;
683 }
684 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000685 case tok::kw_public:
686 case tok::kw_protected:
687 case tok::kw_private:
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000688 if (Style.Language == FormatStyle::LK_Java)
689 nextToken();
690 else
691 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000692 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000693 case tok::kw_if:
694 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000695 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000696 case tok::kw_for:
697 case tok::kw_while:
698 parseForOrWhileLoop();
699 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000700 case tok::kw_do:
701 parseDoWhile();
702 return;
703 case tok::kw_switch:
704 parseSwitch();
705 return;
706 case tok::kw_default:
707 nextToken();
708 parseLabel();
709 return;
710 case tok::kw_case:
711 parseCaseLabel();
712 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000713 case tok::kw_try:
714 parseTryCatch();
715 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000716 case tok::kw_extern:
717 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000718 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000719 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000720 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000721 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000722 addUnwrappedLine();
723 return;
724 }
725 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000726 break;
727 case tok::identifier:
728 if (FormatTok->IsForEachMacro) {
729 parseForOrWhileLoop();
730 return;
731 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000732 // In all other cases, parse the declaration.
733 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000734 default:
735 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000736 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000737 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000738 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000739 case tok::at:
740 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000741 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000742 parseBracedList();
743 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000744 case tok::kw_enum:
745 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000746 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000747 case tok::kw_typedef:
748 nextToken();
749 // FIXME: Use the IdentifierTable instead.
750 if (FormatTok->TokenText == "NS_ENUM")
751 parseEnum();
752 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000753 case tok::kw_struct:
754 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000755 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000756 parseRecord();
757 // A record declaration or definition is always the start of a structural
758 // element.
759 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000760 case tok::semi:
761 nextToken();
762 addUnwrappedLine();
763 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000764 case tok::r_brace:
765 addUnwrappedLine();
766 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000767 case tok::l_paren:
768 parseParens();
769 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000770 case tok::caret:
771 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000772 if (FormatTok->Tok.isAnyIdentifier() ||
773 FormatTok->isSimpleTypeSpecifier())
774 nextToken();
775 if (FormatTok->is(tok::l_paren))
776 parseParens();
777 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000778 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000779 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000780 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000781 if (!tryToParseBracedList()) {
782 // A block outside of parentheses must be the last part of a
783 // structural element.
784 // FIXME: Figure out cases where this is not true, and add projections
785 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000786 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000787 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000788 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000789 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000790 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000791 return;
792 }
793 // Otherwise this was a braced init list, and the structural
794 // element continues.
795 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000796 case tok::kw_try:
797 // We arrive here when parsing function-try blocks.
798 parseTryCatch();
799 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000800 case tok::identifier: {
801 StringRef Text = FormatTok->TokenText;
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000802 // Parse function literal unless 'function' is the first token in a line
803 // in which case this should be treated as a free-standing function.
804 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
805 Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000806 tryToParseJSFunction();
807 break;
808 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000809 nextToken();
Alexander Kornienkode644272013-04-08 22:16:06 +0000810 if (Line->Tokens.size() == 1) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000811 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000812 parseLabel();
813 return;
814 }
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000815 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000816 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000817 parseParens();
Daniel Jasper352dae12014-01-03 11:50:46 +0000818 if (FormatTok->NewlinesBefore > 0 &&
Alexander Kornienkoce081262014-03-18 14:35:20 +0000819 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000820 addUnwrappedLine();
821 return;
822 }
Daniel Jasper40e19212013-05-29 13:16:10 +0000823 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
824 Text == Text.upper()) {
825 // Recognize free-standing macros like Q_OBJECT.
826 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000827 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000828 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000829 }
830 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000831 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000832 case tok::equal:
833 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000834 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000835 parseBracedList();
836 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000837 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000838 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000839 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000840 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000841 default:
842 nextToken();
843 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000844 }
845 } while (!eof());
846}
847
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000848bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000849 // FIXME: This is a dirty way to access the previous token. Find a better
850 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000851 if (!Line->Tokens.empty() &&
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000852 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator) ||
853 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000854 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000855 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000856 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000857 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000858 assert(FormatTok->is(tok::l_square));
859 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000860 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000861 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000862
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000863 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000864 if (FormatTok->isSimpleTypeSpecifier()) {
865 nextToken();
866 continue;
867 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000868 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000869 case tok::l_brace:
870 break;
871 case tok::l_paren:
872 parseParens();
873 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000874 case tok::less:
875 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000876 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000877 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000878 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000879 nextToken();
880 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000881 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000882 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000883 nextToken();
884 break;
885 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000886 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000887 }
888 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000889 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000890 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000891 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000892}
893
894bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
895 nextToken();
896 if (FormatTok->is(tok::equal)) {
897 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000898 if (FormatTok->is(tok::r_square)) {
899 nextToken();
900 return true;
901 }
902 if (FormatTok->isNot(tok::comma))
903 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000904 nextToken();
905 } else if (FormatTok->is(tok::amp)) {
906 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000907 if (FormatTok->is(tok::r_square)) {
908 nextToken();
909 return true;
910 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000911 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
912 return false;
913 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000914 if (FormatTok->is(tok::comma))
915 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000916 } else if (FormatTok->is(tok::r_square)) {
917 nextToken();
918 return true;
919 }
920 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000921 if (FormatTok->is(tok::amp))
922 nextToken();
923 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
924 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000925 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000926 if (FormatTok->is(tok::ellipsis))
927 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000928 if (FormatTok->is(tok::comma)) {
929 nextToken();
930 } else if (FormatTok->is(tok::r_square)) {
931 nextToken();
932 return true;
933 } else {
934 return false;
935 }
936 } while (!eof());
937 return false;
938}
939
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000940void UnwrappedLineParser::tryToParseJSFunction() {
941 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000942
943 // Consume function name.
944 if (FormatTok->is(tok::identifier))
945 nextToken();
946
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000947 if (FormatTok->isNot(tok::l_paren))
948 return;
949 nextToken();
950 while (FormatTok->isNot(tok::l_brace)) {
951 // Err on the side of caution in order to avoid consuming the full file in
952 // case of incomplete code.
953 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
954 tok::comment))
955 return;
956 nextToken();
957 }
958 parseChildBlock();
959}
960
Manuel Klimekab419912013-05-23 09:41:43 +0000961bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000962 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +0000963 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000964 assert(FormatTok->BlockKind != BK_Unknown);
965 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +0000966 return false;
967 parseBracedList();
968 return true;
969}
970
Daniel Jasper015ed022013-09-13 09:20:45 +0000971bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
972 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000973 nextToken();
974
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000975 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
976 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000977 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000978 if (Style.Language == FormatStyle::LK_JavaScript &&
979 FormatTok->TokenText == "function") {
980 tryToParseJSFunction();
981 continue;
982 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000983 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +0000984 case tok::caret:
985 nextToken();
986 if (FormatTok->is(tok::l_brace)) {
987 parseChildBlock();
988 }
989 break;
990 case tok::l_square:
991 tryToParseLambda();
992 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000993 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000994 // Assume there are no blocks inside a braced init list apart
995 // from the ones we explicitly parse out (like lambdas).
996 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000997 parseBracedList();
998 break;
999 case tok::r_brace:
1000 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001001 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001002 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001003 HasError = true;
1004 if (!ContinueOnSemicolons)
1005 return !HasError;
1006 nextToken();
1007 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001008 case tok::comma:
1009 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001010 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001011 default:
1012 nextToken();
1013 break;
1014 }
1015 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001016 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001017}
1018
Daniel Jasperf7935112012-12-03 18:12:45 +00001019void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001020 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001021 nextToken();
1022 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001023 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001024 case tok::l_paren:
1025 parseParens();
1026 break;
1027 case tok::r_paren:
1028 nextToken();
1029 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001030 case tok::r_brace:
1031 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1032 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001033 case tok::l_square:
1034 tryToParseLambda();
1035 break;
Nico Weber67ffb332013-02-10 04:38:23 +00001036 case tok::l_brace: {
Manuel Klimekab419912013-05-23 09:41:43 +00001037 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001038 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001039 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001040 break;
Nico Weber67ffb332013-02-10 04:38:23 +00001041 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001042 case tok::at:
1043 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001044 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001045 parseBracedList();
1046 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001047 case tok::identifier:
1048 if (Style.Language == FormatStyle::LK_JavaScript &&
1049 FormatTok->TokenText == "function")
1050 tryToParseJSFunction();
1051 else
1052 nextToken();
1053 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001054 default:
1055 nextToken();
1056 break;
1057 }
1058 } while (!eof());
1059}
1060
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001061void UnwrappedLineParser::parseSquare() {
1062 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1063 if (tryToParseLambda())
1064 return;
1065 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001066 switch (FormatTok->Tok.getKind()) {
1067 case tok::l_paren:
1068 parseParens();
1069 break;
1070 case tok::r_square:
1071 nextToken();
1072 return;
1073 case tok::r_brace:
1074 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1075 return;
1076 case tok::l_square:
1077 parseSquare();
1078 break;
1079 case tok::l_brace: {
1080 if (!tryToParseBracedList()) {
1081 parseChildBlock();
1082 }
1083 break;
1084 }
1085 case tok::at:
1086 nextToken();
1087 if (FormatTok->Tok.is(tok::l_brace))
1088 parseBracedList();
1089 break;
1090 default:
1091 nextToken();
1092 break;
1093 }
1094 } while (!eof());
1095}
1096
Daniel Jasperf7935112012-12-03 18:12:45 +00001097void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001098 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001099 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001100 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001101 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001102 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001103 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001104 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001105 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001106 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1107 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001108 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001109 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001110 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001111 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001112 } else {
1113 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001114 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001115 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001116 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001117 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001118 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001119 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1120 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001121 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001122 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001123 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001124 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001125 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001126 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001127 parseIfThenElse();
1128 } else {
1129 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001130 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001131 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001132 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001133 }
1134 } else if (NeedsUnwrappedLine) {
1135 addUnwrappedLine();
1136 }
1137}
1138
Daniel Jasper04a71a42014-05-08 11:58:24 +00001139void UnwrappedLineParser::parseTryCatch() {
1140 assert(FormatTok->is(tok::kw_try) && "'try' expected");
1141 nextToken();
1142 bool NeedsUnwrappedLine = false;
1143 if (FormatTok->is(tok::colon)) {
1144 // We are in a function try block, what comes is an initializer list.
1145 nextToken();
1146 while (FormatTok->is(tok::identifier)) {
1147 nextToken();
1148 if (FormatTok->is(tok::l_paren))
1149 parseParens();
1150 else
1151 StructuralError = true;
1152 if (FormatTok->is(tok::comma))
1153 nextToken();
1154 }
1155 }
1156 if (FormatTok->is(tok::l_brace)) {
1157 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1158 parseBlock(/*MustBeDeclaration=*/false);
1159 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1160 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1161 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1162 addUnwrappedLine();
1163 } else {
1164 NeedsUnwrappedLine = true;
1165 }
1166 } else if (!FormatTok->is(tok::kw_catch)) {
1167 // The C++ standard requires a compound-statement after a try.
1168 // If there's none, we try to assume there's a structuralElement
1169 // and try to continue.
1170 StructuralError = true;
1171 addUnwrappedLine();
1172 ++Line->Level;
1173 parseStructuralElement();
1174 --Line->Level;
1175 }
1176 while (FormatTok->is(tok::kw_catch) ||
Daniel Jaspera3ddf862014-11-02 19:21:48 +00001177 ((Style.Language == FormatStyle::LK_Java ||
1178 Style.Language == FormatStyle::LK_JavaScript) &&
Daniel Jasper04a71a42014-05-08 11:58:24 +00001179 FormatTok->TokenText == "finally")) {
1180 nextToken();
1181 while (FormatTok->isNot(tok::l_brace)) {
1182 if (FormatTok->is(tok::l_paren)) {
1183 parseParens();
1184 continue;
1185 }
1186 if (FormatTok->isOneOf(tok::semi, tok::r_brace))
1187 return;
1188 nextToken();
1189 }
1190 NeedsUnwrappedLine = false;
1191 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1192 parseBlock(/*MustBeDeclaration=*/false);
1193 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1194 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1195 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1196 addUnwrappedLine();
1197 } else {
1198 NeedsUnwrappedLine = true;
1199 }
1200 }
1201 if (NeedsUnwrappedLine) {
1202 addUnwrappedLine();
1203 }
1204}
1205
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001206void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001207 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001208
1209 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001210 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001211 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001212 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001213 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001214 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001215 addUnwrappedLine();
1216
Daniel Jasper65ee3472013-07-31 23:16:02 +00001217 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1218 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1219 DeclarationScopeStack.size() > 1);
1220 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001221 // Munch the semicolon after a namespace. This is more common than one would
1222 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001223 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001224 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001225 addUnwrappedLine();
1226 }
1227 // FIXME: Add error handling.
1228}
1229
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001230void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001231 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1232 FormatTok->IsForEachMacro) &&
1233 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001234 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001235 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001236 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001237 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001238 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001239 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001240 addUnwrappedLine();
1241 } else {
1242 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001243 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001244 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001245 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001246 }
1247}
1248
Daniel Jasperf7935112012-12-03 18:12:45 +00001249void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001250 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001251 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001252 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001253 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001254 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001255 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1256 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001257 } else {
1258 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001259 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001260 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001261 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001262 }
1263
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001264 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001265 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001266 addUnwrappedLine();
1267 return;
1268 }
1269
Daniel Jasperf7935112012-12-03 18:12:45 +00001270 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001271 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001272}
1273
1274void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001275 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001276 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001277 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001278 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001279 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001280 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001281 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001282 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001283 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1284 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1285 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001286 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001287 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001288 parseStructuralElement();
1289 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001290 addUnwrappedLine();
1291 } else {
1292 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001293 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001294 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001295}
1296
1297void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001298 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001299 // FIXME: fix handling of complex expressions here.
1300 do {
1301 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001302 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001303 parseLabel();
1304}
1305
1306void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001307 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001308 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001309 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001310 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001311 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001312 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001313 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001314 addUnwrappedLine();
1315 } else {
1316 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001317 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001318 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001319 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001320 }
1321}
1322
1323void UnwrappedLineParser::parseAccessSpecifier() {
1324 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001325 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001326 if (FormatTok->is(tok::identifier) &&
1327 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001328 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001329 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001330 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001331 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001332 addUnwrappedLine();
1333}
1334
1335void UnwrappedLineParser::parseEnum() {
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001336 if (FormatTok->Tok.is(tok::kw_enum)) {
1337 // Won't be 'enum' for NS_ENUMs.
1338 nextToken();
1339 }
Daniel Jasper2b41a822013-08-20 12:42:50 +00001340 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001341 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1342 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001343 while (FormatTok->Tok.getIdentifierInfo() ||
1344 FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001345 nextToken();
1346 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001347 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001348 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001349 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001350 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001351 nextToken();
1352 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001353 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001354 FormatTok->BlockKind = BK_Block;
1355 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1356 if (HasError) {
1357 if (FormatTok->is(tok::semi))
1358 nextToken();
Manuel Klimeka027f302013-08-07 19:20:45 +00001359 addUnwrappedLine();
Daniel Jasper015ed022013-09-13 09:20:45 +00001360 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001361 }
1362 // We fall through to parsing a structural element afterwards, so that in
1363 // enum A {} n, m;
1364 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001365 // This does not apply for Java.
1366 if (Style.Language == FormatStyle::LK_Java)
1367 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001368}
1369
Manuel Klimeke01bab52013-01-15 13:38:33 +00001370void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001371 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001372 nextToken();
Manuel Klimek45bf56c2014-07-31 07:19:30 +00001373 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1374 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001375 nextToken();
1376 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001377 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001378 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001379 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001380 // The actual identifier can be a nested name specifier, and in macros
1381 // it is often token-pasted.
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001382 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1383 FormatTok->is(tok::hashhash) ||
1384 (Style.Language == FormatStyle::LK_Java &&
1385 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001386 nextToken();
1387
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001388 // Note that parsing away template declarations here leads to incorrectly
1389 // accepting function declarations as record declarations.
1390 // In general, we cannot solve this problem. Consider:
1391 // class A<int> B() {}
1392 // which can be a function definition or a class definition when B() is a
1393 // macro. If we find enough real-world cases where this is a problem, we
1394 // can parse for the 'template' keyword in the beginning of the statement,
1395 // and thus rule out the record production in case there is no template
1396 // (this would still leave us with an ambiguity between template function
1397 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001398 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1399 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1400 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001401 return;
1402 nextToken();
1403 }
1404 }
1405 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001406 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001407 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001408 addUnwrappedLine();
1409
Daniel Jasper240dfda2014-03-31 14:23:49 +00001410 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001411 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001412 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001413 // We fall through to parsing a structural element afterwards, so
1414 // class A {} n, m;
1415 // will end up in one unwrapped line.
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001416 // This does not apply for Java.
1417 if (Style.Language == FormatStyle::LK_Java)
1418 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001419}
1420
Nico Weber8696a8d2013-01-09 21:15:03 +00001421void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001422 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001423 do
1424 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001425 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001426 nextToken(); // Skip '>'.
1427}
1428
1429void UnwrappedLineParser::parseObjCUntilAtEnd() {
1430 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001431 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001432 nextToken();
1433 addUnwrappedLine();
1434 break;
1435 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001436 if (FormatTok->is(tok::l_brace)) {
1437 parseBlock(/*MustBeDeclaration=*/false);
1438 // In ObjC interfaces, nothing should be following the "}".
1439 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001440 } else if (FormatTok->is(tok::r_brace)) {
1441 // Ignore stray "}". parseStructuralElement doesn't consume them.
1442 nextToken();
1443 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001444 } else {
1445 parseStructuralElement();
1446 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001447 } while (!eof());
1448}
1449
Nico Weber2ce0ac52013-01-09 23:25:37 +00001450void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001451 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001452 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001453
1454 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001455 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001456 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001457 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001458 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001459 // Skip category, if present.
1460 parseParens();
1461
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001462 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001463 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001464
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001465 if (FormatTok->Tok.is(tok::l_brace)) {
1466 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1467 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1468 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001469 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001470 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001471
1472 // With instance variables, this puts '}' on its own line. Without instance
1473 // variables, this ends the @interface line.
1474 addUnwrappedLine();
1475
Nico Weber8696a8d2013-01-09 21:15:03 +00001476 parseObjCUntilAtEnd();
1477}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001478
Nico Weber8696a8d2013-01-09 21:15:03 +00001479void UnwrappedLineParser::parseObjCProtocol() {
1480 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001481 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001482
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001483 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001484 parseObjCProtocolList();
1485
1486 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001487 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001488 nextToken();
1489 return addUnwrappedLine();
1490 }
1491
1492 addUnwrappedLine();
1493 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001494}
1495
Daniel Jasper3b203a62013-09-05 16:05:56 +00001496LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1497 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001498 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1499 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1500 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1501 E = Line.Tokens.end();
1502 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001503 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001504 }
1505 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1506 E = Line.Tokens.end();
1507 I != E; ++I) {
1508 const UnwrappedLineNode &Node = *I;
1509 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1510 I = Node.Children.begin(),
1511 E = Node.Children.end();
1512 I != E; ++I) {
1513 printDebugInfo(*I, "\nChild: ");
1514 }
1515 }
1516 llvm::dbgs() << "\n";
1517}
1518
Daniel Jasperf7935112012-12-03 18:12:45 +00001519void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001520 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001521 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001522 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001523 if (CurrentLines == &Lines)
1524 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001525 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001526 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001527 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001528 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001529 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001530 I = PreprocessorDirectives.begin(),
1531 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001532 I != E; ++I) {
1533 CurrentLines->push_back(*I);
1534 }
1535 PreprocessorDirectives.clear();
1536 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001537}
1538
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001539bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001540
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001541bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001542 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1543 FormatTok.NewlinesBefore > 0;
1544}
1545
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001546void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1547 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001548 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001549 I = CommentsBeforeNextToken.begin(),
1550 E = CommentsBeforeNextToken.end();
1551 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001552 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001553 addUnwrappedLine();
1554 }
1555 pushToken(*I);
1556 }
1557 if (NewlineBeforeNext && JustComments) {
1558 addUnwrappedLine();
1559 }
1560 CommentsBeforeNextToken.clear();
1561}
1562
Daniel Jasperf7935112012-12-03 18:12:45 +00001563void UnwrappedLineParser::nextToken() {
1564 if (eof())
1565 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001566 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001567 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001568 readToken();
1569}
1570
1571void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001572 bool CommentsInCurrentLine = true;
1573 do {
1574 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001575 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001576 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1577 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001578 // If there is an unfinished unwrapped line, we flush the preprocessor
1579 // directives only after that unwrapped line was finished later.
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001580 bool SwitchToPreprocessorLines =
1581 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001582 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001583 // Comments stored before the preprocessor directive need to be output
1584 // before the preprocessor directive, at the same level as the
1585 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001586 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001587 parsePPDirective();
1588 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001589 while (FormatTok->Type == TT_ConflictStart ||
1590 FormatTok->Type == TT_ConflictEnd ||
1591 FormatTok->Type == TT_ConflictAlternative) {
1592 if (FormatTok->Type == TT_ConflictStart) {
1593 conditionalCompilationStart(/*Unreachable=*/false);
1594 } else if (FormatTok->Type == TT_ConflictAlternative) {
1595 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001596 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001597 conditionalCompilationEnd();
1598 }
1599 FormatTok = Tokens->getNextToken();
1600 FormatTok->MustBreakBefore = true;
1601 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001602
1603 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1604 !Line->InPPDirective) {
1605 continue;
1606 }
1607
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001608 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001609 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001610 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001611 CommentsInCurrentLine = false;
1612 }
1613 if (CommentsInCurrentLine) {
1614 pushToken(FormatTok);
1615 } else {
1616 CommentsBeforeNextToken.push_back(FormatTok);
1617 }
1618 } while (!eof());
1619}
1620
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001621void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001622 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001623 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001624 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001625 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001626 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001627}
1628
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001629} // end namespace format
1630} // end namespace clang