blob: 395d3d80536faa439a3bb4eeb51c0cf6c5a8bae1 [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,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000205 const AdditionalKeywords &Keywords,
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),
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000210 Keywords(Keywords), Tokens(nullptr), Callback(Callback),
211 AllTokens(Tokens), 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();
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:
Nico Weberfac23712015-02-04 15:26:27 +0000377 case tok::kw___try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000378 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000379 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000380 break;
381 default:
382 break;
383 }
384 Tok = NextTok;
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) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000422 // FIXME: Closure-library specific stuff should not be hard-coded but be
423 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000424 if (Line.Tokens.size() < 4)
425 return false;
426 auto I = Line.Tokens.begin();
427 if (I->Tok->TokenText != "goog")
428 return false;
429 ++I;
430 if (I->Tok->isNot(tok::period))
431 return false;
432 ++I;
433 if (I->Tok->TokenText != "scope")
434 return false;
435 ++I;
436 return I->Tok->is(tok::l_paren);
437}
438
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000439static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
440 const FormatToken &InitialToken) {
441 switch (Style.BreakBeforeBraces) {
442 case FormatStyle::BS_Linux:
443 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
444 case FormatStyle::BS_Allman:
445 case FormatStyle::BS_GNU:
446 return true;
447 default:
448 return false;
449 }
450}
451
Manuel Klimek516e0542013-09-04 13:25:30 +0000452void UnwrappedLineParser::parseChildBlock() {
453 FormatTok->BlockKind = BK_Block;
454 nextToken();
455 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000456 bool GoogScope =
457 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000458 ScopedLineState LineState(*this);
459 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
460 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000461 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000462 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000463 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000464 }
465 nextToken();
466}
467
Daniel Jasperf7935112012-12-03 18:12:45 +0000468void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000469 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000470 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000471 nextToken();
472
Craig Topper2145bc02014-05-09 08:15:10 +0000473 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000474 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000475 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000476 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000477
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000478 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000479 case tok::pp_define:
480 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000481 return;
482 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000483 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000484 break;
485 case tok::pp_ifdef:
486 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000487 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000488 break;
489 case tok::pp_else:
490 parsePPElse();
491 break;
492 case tok::pp_elif:
493 parsePPElIf();
494 break;
495 case tok::pp_endif:
496 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000497 break;
498 default:
499 parsePPUnknown();
500 break;
501 }
502}
503
Manuel Klimek68b03042014-04-14 09:14:11 +0000504void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
505 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000506 PPStack.push_back(PP_Unreachable);
507 else
508 PPStack.push_back(PP_Conditional);
509}
510
Manuel Klimek68b03042014-04-14 09:14:11 +0000511void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000512 ++PPBranchLevel;
513 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
514 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
515 PPLevelBranchIndex.push_back(0);
516 PPLevelBranchCount.push_back(0);
517 }
518 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000519 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
520 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000521}
522
Manuel Klimek68b03042014-04-14 09:14:11 +0000523void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000524 if (!PPStack.empty())
525 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000526 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
527 if (!PPChainBranchIndex.empty())
528 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000529 conditionalCompilationCondition(
530 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
531 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000532}
533
Manuel Klimek68b03042014-04-14 09:14:11 +0000534void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000535 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
536 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
537 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000538 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
539 }
540 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000541 // Guard against #endif's without #if.
542 if (PPBranchLevel > 0)
543 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000544 if (!PPChainBranchIndex.empty())
545 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000546 if (!PPStack.empty())
547 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000548}
549
550void UnwrappedLineParser::parsePPIf(bool IfDef) {
551 nextToken();
552 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Daniel Jasper9d22bcc2015-01-19 10:51:05 +0000553 FormatTok->Tok.getLiteralData() != nullptr &&
Manuel Klimek68b03042014-04-14 09:14:11 +0000554 StringRef(FormatTok->Tok.getLiteralData(),
555 FormatTok->Tok.getLength()) == "0") ||
556 FormatTok->Tok.is(tok::kw_false);
557 conditionalCompilationStart(!IfDef && IsLiteralFalse);
558 parsePPUnknown();
559}
560
561void UnwrappedLineParser::parsePPElse() {
562 conditionalCompilationAlternative();
563 parsePPUnknown();
564}
565
566void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
567
568void UnwrappedLineParser::parsePPEndIf() {
569 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000570 parsePPUnknown();
571}
572
Manuel Klimek1abf7892013-01-04 23:34:14 +0000573void UnwrappedLineParser::parsePPDefine() {
574 nextToken();
575
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000576 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000577 parsePPUnknown();
578 return;
579 }
580 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000581 if (FormatTok->Tok.getKind() == tok::l_paren &&
582 FormatTok->WhitespaceRange.getBegin() ==
583 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000584 parseParens();
585 }
586 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000587 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000588
589 // Errors during a preprocessor directive can only affect the layout of the
590 // preprocessor directive, and thus we ignore them. An alternative approach
591 // would be to use the same approach we use on the file level (no
592 // re-indentation if there was a structural error) within the macro
593 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000594 parseFile();
595}
596
597void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000598 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000599 nextToken();
600 } while (!eof());
601 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000602}
603
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000604// Here we blacklist certain tokens that are not usually the first token in an
605// unwrapped line. This is used in attempt to distinguish macro calls without
606// trailing semicolons from other constructs split to several lines.
607bool tokenCanStartNewLine(clang::Token Tok) {
608 // Semicolon can be a null-statement, l_square can be a start of a macro or
609 // a C++11 attribute, but this doesn't seem to be common.
610 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
611 Tok.isNot(tok::l_square) &&
612 // Tokens that can only be used as binary operators and a part of
613 // overloaded operator names.
614 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
615 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
616 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
617 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
618 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
619 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
620 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
621 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
622 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
623 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
624 Tok.isNot(tok::lesslessequal) &&
625 // Colon is used in labels, base class lists, initializer lists,
626 // range-based for loops, ternary operator, but should never be the
627 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000628 Tok.isNot(tok::colon) &&
629 // 'noexcept' is a trailing annotation.
630 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000631}
632
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000633void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000634 assert(!FormatTok->Tok.is(tok::l_brace));
635 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000636 case tok::at:
637 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000638 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000639 parseBracedList();
640 break;
641 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000642 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000643 case tok::objc_public:
644 case tok::objc_protected:
645 case tok::objc_package:
646 case tok::objc_private:
647 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000648 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000649 case tok::objc_implementation:
650 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000651 case tok::objc_protocol:
652 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000653 case tok::objc_end:
654 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000655 case tok::objc_optional:
656 case tok::objc_required:
657 nextToken();
658 addUnwrappedLine();
659 return;
Nico Weber33381f52015-02-07 01:57:32 +0000660 case tok::objc_try:
661 // This branch isn't strictly necessary (the kw_try case below would
662 // do this too after the tok::at is parsed above). But be explicit.
663 parseTryCatch();
664 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000665 default:
666 break;
667 }
668 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000669 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000670 nextToken();
671 if (FormatTok->is(tok::l_brace)) {
Daniel Jasper2337f282015-01-12 10:14:56 +0000672 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000673 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000674 if (FormatTok->is(tok::r_brace)) {
675 nextToken();
676 break;
677 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000678 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000679 nextToken();
680 }
681 }
682 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000683 case tok::kw_namespace:
684 parseNamespace();
685 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000686 case tok::kw_inline:
687 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000688 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000689 parseNamespace();
690 return;
691 }
692 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000693 case tok::kw_public:
694 case tok::kw_protected:
695 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +0000696 if (Style.Language == FormatStyle::LK_Java ||
697 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000698 nextToken();
699 else
700 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000701 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000702 case tok::kw_if:
703 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000704 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000705 case tok::kw_for:
706 case tok::kw_while:
707 parseForOrWhileLoop();
708 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000709 case tok::kw_do:
710 parseDoWhile();
711 return;
712 case tok::kw_switch:
713 parseSwitch();
714 return;
715 case tok::kw_default:
716 nextToken();
717 parseLabel();
718 return;
719 case tok::kw_case:
720 parseCaseLabel();
721 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000722 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000723 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +0000724 parseTryCatch();
725 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000726 case tok::kw_extern:
727 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000728 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000729 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000730 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000731 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000732 addUnwrappedLine();
733 return;
734 }
735 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000736 break;
737 case tok::identifier:
738 if (FormatTok->IsForEachMacro) {
739 parseForOrWhileLoop();
740 return;
741 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000742 // In all other cases, parse the declaration.
743 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000744 default:
745 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000746 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000747 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000748 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000749 case tok::at:
750 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000751 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000752 parseBracedList();
753 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000754 case tok::kw_enum:
755 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000756 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000757 case tok::kw_typedef:
758 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000759 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
760 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000761 parseEnum();
762 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000763 case tok::kw_struct:
764 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000765 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000766 parseRecord();
767 // A record declaration or definition is always the start of a structural
768 // element.
769 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000770 case tok::period:
771 nextToken();
772 // In Java, classes have an implicit static member "class".
773 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
774 FormatTok->is(tok::kw_class))
775 nextToken();
776 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000777 case tok::semi:
778 nextToken();
779 addUnwrappedLine();
780 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000781 case tok::r_brace:
782 addUnwrappedLine();
783 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000784 case tok::l_paren:
785 parseParens();
786 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000787 case tok::caret:
788 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000789 if (FormatTok->Tok.isAnyIdentifier() ||
790 FormatTok->isSimpleTypeSpecifier())
791 nextToken();
792 if (FormatTok->is(tok::l_paren))
793 parseParens();
794 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000795 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000796 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000797 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000798 if (!tryToParseBracedList()) {
799 // A block outside of parentheses must be the last part of a
800 // structural element.
801 // FIXME: Figure out cases where this is not true, and add projections
802 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000803 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000804 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000805 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000806 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000807 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000808 return;
809 }
810 // Otherwise this was a braced init list, and the structural
811 // element continues.
812 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000813 case tok::kw_try:
814 // We arrive here when parsing function-try blocks.
815 parseTryCatch();
816 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000817 case tok::identifier: {
818 StringRef Text = FormatTok->TokenText;
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000819 // Parse function literal unless 'function' is the first token in a line
820 // in which case this should be treated as a free-standing function.
821 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
822 Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000823 tryToParseJSFunction();
824 break;
825 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000826 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +0000827 if (Line->Tokens.size() == 1 &&
828 // JS doesn't have macros, and within classes colons indicate fields,
829 // not labels.
830 (Style.Language != FormatStyle::LK_JavaScript ||
831 !Line->MustBeDeclaration)) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000832 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000833 parseLabel();
834 return;
835 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000836 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +0000837 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +0000838 bool FunctionLike = FormatTok->is(tok::l_paren);
839 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000840 parseParens();
Daniel Jasper680b09b2014-11-05 10:48:04 +0000841 if (FormatTok->NewlinesBefore > 0 &&
842 (Text.size() >= 5 || FunctionLike) &&
843 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000844 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000845 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000846 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000847 }
848 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000849 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000850 case tok::equal:
851 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000852 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000853 parseBracedList();
854 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000855 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000856 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000857 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000858 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000859 default:
860 nextToken();
861 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000862 }
863 } while (!eof());
864}
865
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000866bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000867 // FIXME: This is a dirty way to access the previous token. Find a better
868 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000869 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000870 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
871 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000872 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000873 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000874 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000875 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000876 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000877 assert(FormatTok->is(tok::l_square));
878 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000879 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000880 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000881
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000882 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000883 if (FormatTok->isSimpleTypeSpecifier()) {
884 nextToken();
885 continue;
886 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000887 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000888 case tok::l_brace:
889 break;
890 case tok::l_paren:
891 parseParens();
892 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000893 case tok::amp:
894 case tok::star:
895 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000896 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000897 case tok::less:
898 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000899 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000900 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000901 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000902 nextToken();
903 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000904 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000905 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000906 nextToken();
907 break;
908 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000909 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000910 }
911 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000912 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000913 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000914 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000915}
916
917bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
918 nextToken();
919 if (FormatTok->is(tok::equal)) {
920 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000921 if (FormatTok->is(tok::r_square)) {
922 nextToken();
923 return true;
924 }
925 if (FormatTok->isNot(tok::comma))
926 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000927 nextToken();
928 } else if (FormatTok->is(tok::amp)) {
929 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000930 if (FormatTok->is(tok::r_square)) {
931 nextToken();
932 return true;
933 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000934 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
935 return false;
936 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000937 if (FormatTok->is(tok::comma))
938 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000939 } else if (FormatTok->is(tok::r_square)) {
940 nextToken();
941 return true;
942 }
943 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000944 if (FormatTok->is(tok::amp))
945 nextToken();
946 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
947 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000948 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000949 if (FormatTok->is(tok::ellipsis))
950 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000951 if (FormatTok->is(tok::comma)) {
952 nextToken();
953 } else if (FormatTok->is(tok::r_square)) {
954 nextToken();
955 return true;
956 } else {
957 return false;
958 }
959 } while (!eof());
960 return false;
961}
962
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000963void UnwrappedLineParser::tryToParseJSFunction() {
964 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000965
966 // Consume function name.
967 if (FormatTok->is(tok::identifier))
968 nextToken();
969
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000970 if (FormatTok->isNot(tok::l_paren))
971 return;
972 nextToken();
973 while (FormatTok->isNot(tok::l_brace)) {
974 // Err on the side of caution in order to avoid consuming the full file in
975 // case of incomplete code.
976 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
977 tok::comment))
978 return;
979 nextToken();
980 }
981 parseChildBlock();
982}
983
Manuel Klimekab419912013-05-23 09:41:43 +0000984bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000985 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +0000986 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000987 assert(FormatTok->BlockKind != BK_Unknown);
988 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +0000989 return false;
990 parseBracedList();
991 return true;
992}
993
Daniel Jasper015ed022013-09-13 09:20:45 +0000994bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
995 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000996 nextToken();
997
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000998 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
999 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001000 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001001 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001002 FormatTok->is(Keywords.kw_function)) {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001003 tryToParseJSFunction();
1004 continue;
1005 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001006 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001007 case tok::caret:
1008 nextToken();
1009 if (FormatTok->is(tok::l_brace)) {
1010 parseChildBlock();
1011 }
1012 break;
1013 case tok::l_square:
1014 tryToParseLambda();
1015 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001016 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001017 // Assume there are no blocks inside a braced init list apart
1018 // from the ones we explicitly parse out (like lambdas).
1019 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001020 parseBracedList();
1021 break;
1022 case tok::r_brace:
1023 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001024 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001025 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001026 HasError = true;
1027 if (!ContinueOnSemicolons)
1028 return !HasError;
1029 nextToken();
1030 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001031 case tok::comma:
1032 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001033 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001034 default:
1035 nextToken();
1036 break;
1037 }
1038 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001039 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001040}
1041
Daniel Jasperf7935112012-12-03 18:12:45 +00001042void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001043 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001044 nextToken();
1045 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001046 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001047 case tok::l_paren:
1048 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001049 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1050 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001051 break;
1052 case tok::r_paren:
1053 nextToken();
1054 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001055 case tok::r_brace:
1056 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1057 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001058 case tok::l_square:
1059 tryToParseLambda();
1060 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001061 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001062 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001063 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001064 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001065 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001066 case tok::at:
1067 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001068 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001069 parseBracedList();
1070 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001071 case tok::identifier:
1072 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001073 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001074 tryToParseJSFunction();
1075 else
1076 nextToken();
1077 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001078 default:
1079 nextToken();
1080 break;
1081 }
1082 } while (!eof());
1083}
1084
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001085void UnwrappedLineParser::parseSquare() {
1086 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1087 if (tryToParseLambda())
1088 return;
1089 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001090 switch (FormatTok->Tok.getKind()) {
1091 case tok::l_paren:
1092 parseParens();
1093 break;
1094 case tok::r_square:
1095 nextToken();
1096 return;
1097 case tok::r_brace:
1098 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1099 return;
1100 case tok::l_square:
1101 parseSquare();
1102 break;
1103 case tok::l_brace: {
1104 if (!tryToParseBracedList()) {
1105 parseChildBlock();
1106 }
1107 break;
1108 }
1109 case tok::at:
1110 nextToken();
1111 if (FormatTok->Tok.is(tok::l_brace))
1112 parseBracedList();
1113 break;
1114 default:
1115 nextToken();
1116 break;
1117 }
1118 } while (!eof());
1119}
1120
Daniel Jasperf7935112012-12-03 18:12:45 +00001121void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001122 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001123 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001124 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001125 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001126 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001127 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001128 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001129 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001130 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1131 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001132 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001133 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001134 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001135 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001136 } else {
1137 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001138 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001139 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001140 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001141 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001142 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001143 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1144 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001145 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001146 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001147 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001148 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001149 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001150 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001151 parseIfThenElse();
1152 } else {
1153 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001154 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001155 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001156 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001157 }
1158 } else if (NeedsUnwrappedLine) {
1159 addUnwrappedLine();
1160 }
1161}
1162
Daniel Jasper04a71a42014-05-08 11:58:24 +00001163void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001164 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001165 nextToken();
1166 bool NeedsUnwrappedLine = false;
1167 if (FormatTok->is(tok::colon)) {
1168 // We are in a function try block, what comes is an initializer list.
1169 nextToken();
1170 while (FormatTok->is(tok::identifier)) {
1171 nextToken();
1172 if (FormatTok->is(tok::l_paren))
1173 parseParens();
1174 else
1175 StructuralError = true;
1176 if (FormatTok->is(tok::comma))
1177 nextToken();
1178 }
1179 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001180 // Parse try with resource.
1181 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1182 parseParens();
1183 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001184 if (FormatTok->is(tok::l_brace)) {
1185 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1186 parseBlock(/*MustBeDeclaration=*/false);
1187 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1188 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1189 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1190 addUnwrappedLine();
1191 } else {
1192 NeedsUnwrappedLine = true;
1193 }
1194 } else if (!FormatTok->is(tok::kw_catch)) {
1195 // The C++ standard requires a compound-statement after a try.
1196 // If there's none, we try to assume there's a structuralElement
1197 // and try to continue.
1198 StructuralError = true;
1199 addUnwrappedLine();
1200 ++Line->Level;
1201 parseStructuralElement();
1202 --Line->Level;
1203 }
Nico Weber33381f52015-02-07 01:57:32 +00001204 while (1) {
1205 if (FormatTok->is(tok::at))
1206 nextToken();
1207 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1208 tok::kw___finally) ||
1209 ((Style.Language == FormatStyle::LK_Java ||
1210 Style.Language == FormatStyle::LK_JavaScript) &&
1211 FormatTok->is(Keywords.kw_finally)) ||
1212 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1213 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1214 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001215 nextToken();
1216 while (FormatTok->isNot(tok::l_brace)) {
1217 if (FormatTok->is(tok::l_paren)) {
1218 parseParens();
1219 continue;
1220 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001221 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001222 return;
1223 nextToken();
1224 }
1225 NeedsUnwrappedLine = false;
1226 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1227 parseBlock(/*MustBeDeclaration=*/false);
1228 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1229 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1230 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1231 addUnwrappedLine();
1232 } else {
1233 NeedsUnwrappedLine = true;
1234 }
1235 }
1236 if (NeedsUnwrappedLine) {
1237 addUnwrappedLine();
1238 }
1239}
1240
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001241void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001242 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001243
1244 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001245 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001246 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001247 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001248 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001249 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001250 addUnwrappedLine();
1251
Daniel Jasper65ee3472013-07-31 23:16:02 +00001252 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1253 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1254 DeclarationScopeStack.size() > 1);
1255 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001256 // Munch the semicolon after a namespace. This is more common than one would
1257 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001258 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001259 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001260 addUnwrappedLine();
1261 }
1262 // FIXME: Add error handling.
1263}
1264
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001265void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001266 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1267 FormatTok->IsForEachMacro) &&
1268 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001269 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001270 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001271 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001272 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001273 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001274 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001275 addUnwrappedLine();
1276 } else {
1277 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001278 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001279 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001280 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001281 }
1282}
1283
Daniel Jasperf7935112012-12-03 18:12:45 +00001284void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001285 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001286 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001287 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001288 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001289 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001290 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1291 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001292 } else {
1293 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001294 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001295 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001296 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001297 }
1298
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001299 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001300 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001301 addUnwrappedLine();
1302 return;
1303 }
1304
Daniel Jasperf7935112012-12-03 18:12:45 +00001305 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001306 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001307}
1308
1309void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001310 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001311 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001312 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001313 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001314 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001315 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001316 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001317 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001318 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1319 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1320 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001321 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001322 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001323 parseStructuralElement();
1324 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001325 addUnwrappedLine();
1326 } else {
1327 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001328 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001329 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001330}
1331
1332void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001333 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001334 // FIXME: fix handling of complex expressions here.
1335 do {
1336 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001337 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001338 parseLabel();
1339}
1340
1341void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001342 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001343 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001344 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001345 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001346 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001347 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001348 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001349 addUnwrappedLine();
1350 } else {
1351 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001352 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001353 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001354 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001355 }
1356}
1357
1358void UnwrappedLineParser::parseAccessSpecifier() {
1359 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001360 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001361 if (FormatTok->is(tok::identifier) &&
1362 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001363 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001364 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001365 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001366 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001367 addUnwrappedLine();
1368}
1369
1370void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001371 // Won't be 'enum' for NS_ENUMs.
1372 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001373 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001374
Daniel Jasper2b41a822013-08-20 12:42:50 +00001375 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001376 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1377 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001378 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001379 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1380 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001381 nextToken();
1382 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001383 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001384 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001385 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001386 nextToken();
1387 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001388
1389 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001390 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001391 return;
1392 FormatTok->BlockKind = BK_Block;
1393
1394 if (Style.Language == FormatStyle::LK_Java) {
1395 // Java enums are different.
1396 parseJavaEnumBody();
1397 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001398 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001399
1400 // Parse enum body.
1401 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1402 if (HasError) {
1403 if (FormatTok->is(tok::semi))
1404 nextToken();
1405 addUnwrappedLine();
1406 }
1407
Manuel Klimek2cec0192013-01-21 19:17:52 +00001408 // We fall through to parsing a structural element afterwards, so that in
1409 // enum A {} n, m;
1410 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001411}
1412
1413void UnwrappedLineParser::parseJavaEnumBody() {
1414 // Determine whether the enum is simple, i.e. does not have a semicolon or
1415 // constants with class bodies. Simple enums can be formatted like braced
1416 // lists, contracted to a single line, etc.
1417 unsigned StoredPosition = Tokens->getPosition();
1418 bool IsSimple = true;
1419 FormatToken *Tok = Tokens->getNextToken();
1420 while (Tok) {
1421 if (Tok->is(tok::r_brace))
1422 break;
1423 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1424 IsSimple = false;
1425 break;
1426 }
1427 // FIXME: This will also mark enums with braces in the arguments to enum
1428 // constants as "not simple". This is probably fine in practice, though.
1429 Tok = Tokens->getNextToken();
1430 }
1431 FormatTok = Tokens->setPosition(StoredPosition);
1432
1433 if (IsSimple) {
1434 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001435 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001436 return;
1437 }
1438
1439 // Parse the body of a more complex enum.
1440 // First add a line for everything up to the "{".
1441 nextToken();
1442 addUnwrappedLine();
1443 ++Line->Level;
1444
1445 // Parse the enum constants.
1446 while (FormatTok) {
1447 if (FormatTok->is(tok::l_brace)) {
1448 // Parse the constant's class body.
1449 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1450 /*MunchSemi=*/false);
1451 } else if (FormatTok->is(tok::l_paren)) {
1452 parseParens();
1453 } else if (FormatTok->is(tok::comma)) {
1454 nextToken();
1455 addUnwrappedLine();
1456 } else if (FormatTok->is(tok::semi)) {
1457 nextToken();
1458 addUnwrappedLine();
1459 break;
1460 } else if (FormatTok->is(tok::r_brace)) {
1461 addUnwrappedLine();
1462 break;
1463 } else {
1464 nextToken();
1465 }
1466 }
1467
1468 // Parse the class body after the enum's ";" if any.
1469 parseLevel(/*HasOpeningBrace=*/true);
1470 nextToken();
1471 --Line->Level;
1472 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001473}
1474
Manuel Klimeke01bab52013-01-15 13:38:33 +00001475void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001476 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001477 nextToken();
Manuel Klimek45bf56c2014-07-31 07:19:30 +00001478 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1479 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001480 nextToken();
1481 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001482 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001483 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001484 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001485 // The actual identifier can be a nested name specifier, and in macros
1486 // it is often token-pasted.
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001487 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1488 FormatTok->is(tok::hashhash) ||
1489 (Style.Language == FormatStyle::LK_Java &&
1490 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001491 nextToken();
1492
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001493 // Note that parsing away template declarations here leads to incorrectly
1494 // accepting function declarations as record declarations.
1495 // In general, we cannot solve this problem. Consider:
1496 // class A<int> B() {}
1497 // which can be a function definition or a class definition when B() is a
1498 // macro. If we find enough real-world cases where this is a problem, we
1499 // can parse for the 'template' keyword in the beginning of the statement,
1500 // and thus rule out the record production in case there is no template
1501 // (this would still leave us with an ambiguity between template function
1502 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001503 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1504 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1505 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001506 return;
1507 nextToken();
1508 }
1509 }
1510 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001511 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001512 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001513 addUnwrappedLine();
1514
Daniel Jasper240dfda2014-03-31 14:23:49 +00001515 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001516 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001517 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001518 // We fall through to parsing a structural element afterwards, so
1519 // class A {} n, m;
1520 // will end up in one unwrapped line.
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001521 // This does not apply for Java.
Daniel Jasper6fa9ec72015-02-19 16:03:16 +00001522 if (Style.Language == FormatStyle::LK_Java ||
1523 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001524 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001525}
1526
Nico Weber8696a8d2013-01-09 21:15:03 +00001527void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001528 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001529 do
1530 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001531 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001532 nextToken(); // Skip '>'.
1533}
1534
1535void UnwrappedLineParser::parseObjCUntilAtEnd() {
1536 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001537 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001538 nextToken();
1539 addUnwrappedLine();
1540 break;
1541 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001542 if (FormatTok->is(tok::l_brace)) {
1543 parseBlock(/*MustBeDeclaration=*/false);
1544 // In ObjC interfaces, nothing should be following the "}".
1545 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001546 } else if (FormatTok->is(tok::r_brace)) {
1547 // Ignore stray "}". parseStructuralElement doesn't consume them.
1548 nextToken();
1549 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001550 } else {
1551 parseStructuralElement();
1552 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001553 } while (!eof());
1554}
1555
Nico Weber2ce0ac52013-01-09 23:25:37 +00001556void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001557 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001558 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001559
1560 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001561 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001562 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001563 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001564 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001565 // Skip category, if present.
1566 parseParens();
1567
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001568 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001569 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001570
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001571 if (FormatTok->Tok.is(tok::l_brace)) {
1572 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1573 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1574 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001575 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001576 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001577
1578 // With instance variables, this puts '}' on its own line. Without instance
1579 // variables, this ends the @interface line.
1580 addUnwrappedLine();
1581
Nico Weber8696a8d2013-01-09 21:15:03 +00001582 parseObjCUntilAtEnd();
1583}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001584
Nico Weber8696a8d2013-01-09 21:15:03 +00001585void UnwrappedLineParser::parseObjCProtocol() {
1586 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001587 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001588
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001589 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001590 parseObjCProtocolList();
1591
1592 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001593 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001594 nextToken();
1595 return addUnwrappedLine();
1596 }
1597
1598 addUnwrappedLine();
1599 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001600}
1601
Daniel Jasper3b203a62013-09-05 16:05:56 +00001602LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1603 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001604 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1605 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1606 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1607 E = Line.Tokens.end();
1608 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001609 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001610 }
1611 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1612 E = Line.Tokens.end();
1613 I != E; ++I) {
1614 const UnwrappedLineNode &Node = *I;
1615 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1616 I = Node.Children.begin(),
1617 E = Node.Children.end();
1618 I != E; ++I) {
1619 printDebugInfo(*I, "\nChild: ");
1620 }
1621 }
1622 llvm::dbgs() << "\n";
1623}
1624
Daniel Jasperf7935112012-12-03 18:12:45 +00001625void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001626 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001627 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001628 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001629 if (CurrentLines == &Lines)
1630 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001631 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001632 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001633 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001634 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001635 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001636 I = PreprocessorDirectives.begin(),
1637 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001638 I != E; ++I) {
1639 CurrentLines->push_back(*I);
1640 }
1641 PreprocessorDirectives.clear();
1642 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001643}
1644
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001645bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001646
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001647bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001648 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1649 FormatTok.NewlinesBefore > 0;
1650}
1651
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001652void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1653 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001654 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001655 I = CommentsBeforeNextToken.begin(),
1656 E = CommentsBeforeNextToken.end();
1657 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001658 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001659 addUnwrappedLine();
1660 }
1661 pushToken(*I);
1662 }
1663 if (NewlineBeforeNext && JustComments) {
1664 addUnwrappedLine();
1665 }
1666 CommentsBeforeNextToken.clear();
1667}
1668
Daniel Jasperf7935112012-12-03 18:12:45 +00001669void UnwrappedLineParser::nextToken() {
1670 if (eof())
1671 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001672 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001673 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001674 readToken();
1675}
1676
1677void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001678 bool CommentsInCurrentLine = true;
1679 do {
1680 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001681 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001682 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1683 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001684 // If there is an unfinished unwrapped line, we flush the preprocessor
1685 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001686 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001687 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001688 // Comments stored before the preprocessor directive need to be output
1689 // before the preprocessor directive, at the same level as the
1690 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001691 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001692 parsePPDirective();
1693 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001694 while (FormatTok->Type == TT_ConflictStart ||
1695 FormatTok->Type == TT_ConflictEnd ||
1696 FormatTok->Type == TT_ConflictAlternative) {
1697 if (FormatTok->Type == TT_ConflictStart) {
1698 conditionalCompilationStart(/*Unreachable=*/false);
1699 } else if (FormatTok->Type == TT_ConflictAlternative) {
1700 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001701 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001702 conditionalCompilationEnd();
1703 }
1704 FormatTok = Tokens->getNextToken();
1705 FormatTok->MustBreakBefore = true;
1706 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001707
1708 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1709 !Line->InPPDirective) {
1710 continue;
1711 }
1712
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001713 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001714 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001715 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001716 CommentsInCurrentLine = false;
1717 }
1718 if (CommentsInCurrentLine) {
1719 pushToken(FormatTok);
1720 } else {
1721 CommentsBeforeNextToken.push_back(FormatTok);
1722 }
1723 } while (!eof());
1724}
1725
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001726void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001727 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001728 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001729 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001730 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001731 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001732}
1733
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001734} // end namespace format
1735} // end namespace clang