blob: 19e591b7031ade16eb81f6e53cdf81e68524a6a2 [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 Jasperc58c70e2014-09-15 11:21:46 +0000696 if (Style.Language == FormatStyle::LK_Java)
697 nextToken();
698 else
699 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000700 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000701 case tok::kw_if:
702 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000703 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000704 case tok::kw_for:
705 case tok::kw_while:
706 parseForOrWhileLoop();
707 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000708 case tok::kw_do:
709 parseDoWhile();
710 return;
711 case tok::kw_switch:
712 parseSwitch();
713 return;
714 case tok::kw_default:
715 nextToken();
716 parseLabel();
717 return;
718 case tok::kw_case:
719 parseCaseLabel();
720 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000721 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000722 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +0000723 parseTryCatch();
724 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000725 case tok::kw_extern:
726 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000727 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000728 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000729 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000730 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000731 addUnwrappedLine();
732 return;
733 }
734 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000735 break;
736 case tok::identifier:
737 if (FormatTok->IsForEachMacro) {
738 parseForOrWhileLoop();
739 return;
740 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000741 // In all other cases, parse the declaration.
742 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000743 default:
744 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000745 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000746 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000747 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000748 case tok::at:
749 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000750 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000751 parseBracedList();
752 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000753 case tok::kw_enum:
754 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000755 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000756 case tok::kw_typedef:
757 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000758 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
759 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000760 parseEnum();
761 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000762 case tok::kw_struct:
763 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000764 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000765 parseRecord();
766 // A record declaration or definition is always the start of a structural
767 // element.
768 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000769 case tok::period:
770 nextToken();
771 // In Java, classes have an implicit static member "class".
772 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
773 FormatTok->is(tok::kw_class))
774 nextToken();
775 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000776 case tok::semi:
777 nextToken();
778 addUnwrappedLine();
779 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000780 case tok::r_brace:
781 addUnwrappedLine();
782 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000783 case tok::l_paren:
784 parseParens();
785 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000786 case tok::caret:
787 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000788 if (FormatTok->Tok.isAnyIdentifier() ||
789 FormatTok->isSimpleTypeSpecifier())
790 nextToken();
791 if (FormatTok->is(tok::l_paren))
792 parseParens();
793 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000794 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000795 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000796 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000797 if (!tryToParseBracedList()) {
798 // A block outside of parentheses must be the last part of a
799 // structural element.
800 // FIXME: Figure out cases where this is not true, and add projections
801 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000802 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000803 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000804 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000805 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000806 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000807 return;
808 }
809 // Otherwise this was a braced init list, and the structural
810 // element continues.
811 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000812 case tok::kw_try:
813 // We arrive here when parsing function-try blocks.
814 parseTryCatch();
815 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000816 case tok::identifier: {
817 StringRef Text = FormatTok->TokenText;
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000818 // Parse function literal unless 'function' is the first token in a line
819 // in which case this should be treated as a free-standing function.
820 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
821 Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000822 tryToParseJSFunction();
823 break;
824 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000825 nextToken();
Alexander Kornienkode644272013-04-08 22:16:06 +0000826 if (Line->Tokens.size() == 1) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000827 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000828 parseLabel();
829 return;
830 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000831 // Recognize function-like macro usages without trailing semicolon as
832 // well as free-standing macrose like Q_OBJECT.
833 bool FunctionLike = FormatTok->is(tok::l_paren);
834 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000835 parseParens();
Daniel Jasper680b09b2014-11-05 10:48:04 +0000836 if (FormatTok->NewlinesBefore > 0 &&
837 (Text.size() >= 5 || FunctionLike) &&
838 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000839 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000840 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000841 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000842 }
843 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000844 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000845 case tok::equal:
846 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000847 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000848 parseBracedList();
849 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000850 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000851 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000852 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000853 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000854 default:
855 nextToken();
856 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000857 }
858 } while (!eof());
859}
860
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000861bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000862 // FIXME: This is a dirty way to access the previous token. Find a better
863 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000864 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000865 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
866 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000867 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000868 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000869 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000870 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000871 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000872 assert(FormatTok->is(tok::l_square));
873 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000874 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000875 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000876
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000877 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000878 if (FormatTok->isSimpleTypeSpecifier()) {
879 nextToken();
880 continue;
881 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000882 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000883 case tok::l_brace:
884 break;
885 case tok::l_paren:
886 parseParens();
887 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000888 case tok::amp:
889 case tok::star:
890 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000891 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000892 case tok::less:
893 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000894 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000895 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000896 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000897 nextToken();
898 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000899 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000900 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000901 nextToken();
902 break;
903 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000904 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000905 }
906 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000907 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000908 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000909 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000910}
911
912bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
913 nextToken();
914 if (FormatTok->is(tok::equal)) {
915 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000916 if (FormatTok->is(tok::r_square)) {
917 nextToken();
918 return true;
919 }
920 if (FormatTok->isNot(tok::comma))
921 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000922 nextToken();
923 } else if (FormatTok->is(tok::amp)) {
924 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000925 if (FormatTok->is(tok::r_square)) {
926 nextToken();
927 return true;
928 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000929 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
930 return false;
931 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000932 if (FormatTok->is(tok::comma))
933 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000934 } else if (FormatTok->is(tok::r_square)) {
935 nextToken();
936 return true;
937 }
938 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000939 if (FormatTok->is(tok::amp))
940 nextToken();
941 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
942 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000943 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000944 if (FormatTok->is(tok::ellipsis))
945 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000946 if (FormatTok->is(tok::comma)) {
947 nextToken();
948 } else if (FormatTok->is(tok::r_square)) {
949 nextToken();
950 return true;
951 } else {
952 return false;
953 }
954 } while (!eof());
955 return false;
956}
957
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000958void UnwrappedLineParser::tryToParseJSFunction() {
959 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000960
961 // Consume function name.
962 if (FormatTok->is(tok::identifier))
963 nextToken();
964
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000965 if (FormatTok->isNot(tok::l_paren))
966 return;
967 nextToken();
968 while (FormatTok->isNot(tok::l_brace)) {
969 // Err on the side of caution in order to avoid consuming the full file in
970 // case of incomplete code.
971 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
972 tok::comment))
973 return;
974 nextToken();
975 }
976 parseChildBlock();
977}
978
Manuel Klimekab419912013-05-23 09:41:43 +0000979bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000980 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +0000981 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000982 assert(FormatTok->BlockKind != BK_Unknown);
983 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +0000984 return false;
985 parseBracedList();
986 return true;
987}
988
Daniel Jasper015ed022013-09-13 09:20:45 +0000989bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
990 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000991 nextToken();
992
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000993 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
994 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000995 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000996 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000997 FormatTok->is(Keywords.kw_function)) {
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000998 tryToParseJSFunction();
999 continue;
1000 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001001 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001002 case tok::caret:
1003 nextToken();
1004 if (FormatTok->is(tok::l_brace)) {
1005 parseChildBlock();
1006 }
1007 break;
1008 case tok::l_square:
1009 tryToParseLambda();
1010 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001011 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001012 // Assume there are no blocks inside a braced init list apart
1013 // from the ones we explicitly parse out (like lambdas).
1014 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001015 parseBracedList();
1016 break;
1017 case tok::r_brace:
1018 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001019 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001020 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001021 HasError = true;
1022 if (!ContinueOnSemicolons)
1023 return !HasError;
1024 nextToken();
1025 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001026 case tok::comma:
1027 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001028 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001029 default:
1030 nextToken();
1031 break;
1032 }
1033 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001034 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001035}
1036
Daniel Jasperf7935112012-12-03 18:12:45 +00001037void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001038 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001039 nextToken();
1040 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001041 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001042 case tok::l_paren:
1043 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001044 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1045 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001046 break;
1047 case tok::r_paren:
1048 nextToken();
1049 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001050 case tok::r_brace:
1051 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1052 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001053 case tok::l_square:
1054 tryToParseLambda();
1055 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001056 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001057 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001058 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001059 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001060 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001061 case tok::at:
1062 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001063 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001064 parseBracedList();
1065 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001066 case tok::identifier:
1067 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001068 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001069 tryToParseJSFunction();
1070 else
1071 nextToken();
1072 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001073 default:
1074 nextToken();
1075 break;
1076 }
1077 } while (!eof());
1078}
1079
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001080void UnwrappedLineParser::parseSquare() {
1081 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1082 if (tryToParseLambda())
1083 return;
1084 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001085 switch (FormatTok->Tok.getKind()) {
1086 case tok::l_paren:
1087 parseParens();
1088 break;
1089 case tok::r_square:
1090 nextToken();
1091 return;
1092 case tok::r_brace:
1093 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1094 return;
1095 case tok::l_square:
1096 parseSquare();
1097 break;
1098 case tok::l_brace: {
1099 if (!tryToParseBracedList()) {
1100 parseChildBlock();
1101 }
1102 break;
1103 }
1104 case tok::at:
1105 nextToken();
1106 if (FormatTok->Tok.is(tok::l_brace))
1107 parseBracedList();
1108 break;
1109 default:
1110 nextToken();
1111 break;
1112 }
1113 } while (!eof());
1114}
1115
Daniel Jasperf7935112012-12-03 18:12:45 +00001116void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001117 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001118 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001119 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001120 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001121 bool NeedsUnwrappedLine = false;
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);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001125 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1126 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001127 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001128 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001129 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001130 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001131 } else {
1132 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001133 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001134 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001135 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001136 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001137 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001138 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1139 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001140 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001141 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001142 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001143 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001144 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001145 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001146 parseIfThenElse();
1147 } else {
1148 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001149 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001150 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001151 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001152 }
1153 } else if (NeedsUnwrappedLine) {
1154 addUnwrappedLine();
1155 }
1156}
1157
Daniel Jasper04a71a42014-05-08 11:58:24 +00001158void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001159 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001160 nextToken();
1161 bool NeedsUnwrappedLine = false;
1162 if (FormatTok->is(tok::colon)) {
1163 // We are in a function try block, what comes is an initializer list.
1164 nextToken();
1165 while (FormatTok->is(tok::identifier)) {
1166 nextToken();
1167 if (FormatTok->is(tok::l_paren))
1168 parseParens();
1169 else
1170 StructuralError = true;
1171 if (FormatTok->is(tok::comma))
1172 nextToken();
1173 }
1174 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001175 // Parse try with resource.
1176 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1177 parseParens();
1178 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001179 if (FormatTok->is(tok::l_brace)) {
1180 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1181 parseBlock(/*MustBeDeclaration=*/false);
1182 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1183 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1184 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1185 addUnwrappedLine();
1186 } else {
1187 NeedsUnwrappedLine = true;
1188 }
1189 } else if (!FormatTok->is(tok::kw_catch)) {
1190 // The C++ standard requires a compound-statement after a try.
1191 // If there's none, we try to assume there's a structuralElement
1192 // and try to continue.
1193 StructuralError = true;
1194 addUnwrappedLine();
1195 ++Line->Level;
1196 parseStructuralElement();
1197 --Line->Level;
1198 }
Nico Weber33381f52015-02-07 01:57:32 +00001199 while (1) {
1200 if (FormatTok->is(tok::at))
1201 nextToken();
1202 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1203 tok::kw___finally) ||
1204 ((Style.Language == FormatStyle::LK_Java ||
1205 Style.Language == FormatStyle::LK_JavaScript) &&
1206 FormatTok->is(Keywords.kw_finally)) ||
1207 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1208 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1209 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001210 nextToken();
1211 while (FormatTok->isNot(tok::l_brace)) {
1212 if (FormatTok->is(tok::l_paren)) {
1213 parseParens();
1214 continue;
1215 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001216 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001217 return;
1218 nextToken();
1219 }
1220 NeedsUnwrappedLine = false;
1221 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1222 parseBlock(/*MustBeDeclaration=*/false);
1223 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1224 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1225 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1226 addUnwrappedLine();
1227 } else {
1228 NeedsUnwrappedLine = true;
1229 }
1230 }
1231 if (NeedsUnwrappedLine) {
1232 addUnwrappedLine();
1233 }
1234}
1235
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001236void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001237 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001238
1239 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001240 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001241 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001242 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001243 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001244 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001245 addUnwrappedLine();
1246
Daniel Jasper65ee3472013-07-31 23:16:02 +00001247 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1248 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1249 DeclarationScopeStack.size() > 1);
1250 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001251 // Munch the semicolon after a namespace. This is more common than one would
1252 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001253 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001254 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001255 addUnwrappedLine();
1256 }
1257 // FIXME: Add error handling.
1258}
1259
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001260void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001261 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1262 FormatTok->IsForEachMacro) &&
1263 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001264 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001265 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001266 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001267 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001268 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001269 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001270 addUnwrappedLine();
1271 } else {
1272 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001273 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001274 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001275 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001276 }
1277}
1278
Daniel Jasperf7935112012-12-03 18:12:45 +00001279void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001280 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001281 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001282 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001283 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001284 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001285 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1286 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001287 } else {
1288 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001289 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001290 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001291 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001292 }
1293
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001294 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001295 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001296 addUnwrappedLine();
1297 return;
1298 }
1299
Daniel Jasperf7935112012-12-03 18:12:45 +00001300 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001301 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001302}
1303
1304void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001305 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001306 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001307 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001308 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001309 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001310 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001311 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001312 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001313 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1314 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1315 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001316 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001317 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001318 parseStructuralElement();
1319 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001320 addUnwrappedLine();
1321 } else {
1322 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001323 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001324 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001325}
1326
1327void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001328 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001329 // FIXME: fix handling of complex expressions here.
1330 do {
1331 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001332 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001333 parseLabel();
1334}
1335
1336void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001337 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001338 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001339 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001340 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001341 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001342 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001343 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001344 addUnwrappedLine();
1345 } else {
1346 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001347 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001348 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001349 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001350 }
1351}
1352
1353void UnwrappedLineParser::parseAccessSpecifier() {
1354 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001355 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001356 if (FormatTok->is(tok::identifier) &&
1357 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001358 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001359 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001360 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001361 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001362 addUnwrappedLine();
1363}
1364
1365void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001366 // Won't be 'enum' for NS_ENUMs.
1367 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001368 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001369
Daniel Jasper2b41a822013-08-20 12:42:50 +00001370 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001371 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1372 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001373 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001374 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1375 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001376 nextToken();
1377 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001378 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001379 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001380 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001381 nextToken();
1382 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001383
1384 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001385 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001386 return;
1387 FormatTok->BlockKind = BK_Block;
1388
1389 if (Style.Language == FormatStyle::LK_Java) {
1390 // Java enums are different.
1391 parseJavaEnumBody();
1392 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001393 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001394
1395 // Parse enum body.
1396 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1397 if (HasError) {
1398 if (FormatTok->is(tok::semi))
1399 nextToken();
1400 addUnwrappedLine();
1401 }
1402
Manuel Klimek2cec0192013-01-21 19:17:52 +00001403 // We fall through to parsing a structural element afterwards, so that in
1404 // enum A {} n, m;
1405 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001406}
1407
1408void UnwrappedLineParser::parseJavaEnumBody() {
1409 // Determine whether the enum is simple, i.e. does not have a semicolon or
1410 // constants with class bodies. Simple enums can be formatted like braced
1411 // lists, contracted to a single line, etc.
1412 unsigned StoredPosition = Tokens->getPosition();
1413 bool IsSimple = true;
1414 FormatToken *Tok = Tokens->getNextToken();
1415 while (Tok) {
1416 if (Tok->is(tok::r_brace))
1417 break;
1418 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1419 IsSimple = false;
1420 break;
1421 }
1422 // FIXME: This will also mark enums with braces in the arguments to enum
1423 // constants as "not simple". This is probably fine in practice, though.
1424 Tok = Tokens->getNextToken();
1425 }
1426 FormatTok = Tokens->setPosition(StoredPosition);
1427
1428 if (IsSimple) {
1429 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001430 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001431 return;
1432 }
1433
1434 // Parse the body of a more complex enum.
1435 // First add a line for everything up to the "{".
1436 nextToken();
1437 addUnwrappedLine();
1438 ++Line->Level;
1439
1440 // Parse the enum constants.
1441 while (FormatTok) {
1442 if (FormatTok->is(tok::l_brace)) {
1443 // Parse the constant's class body.
1444 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1445 /*MunchSemi=*/false);
1446 } else if (FormatTok->is(tok::l_paren)) {
1447 parseParens();
1448 } else if (FormatTok->is(tok::comma)) {
1449 nextToken();
1450 addUnwrappedLine();
1451 } else if (FormatTok->is(tok::semi)) {
1452 nextToken();
1453 addUnwrappedLine();
1454 break;
1455 } else if (FormatTok->is(tok::r_brace)) {
1456 addUnwrappedLine();
1457 break;
1458 } else {
1459 nextToken();
1460 }
1461 }
1462
1463 // Parse the class body after the enum's ";" if any.
1464 parseLevel(/*HasOpeningBrace=*/true);
1465 nextToken();
1466 --Line->Level;
1467 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001468}
1469
Manuel Klimeke01bab52013-01-15 13:38:33 +00001470void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001471 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001472 nextToken();
Manuel Klimek45bf56c2014-07-31 07:19:30 +00001473 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1474 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001475 nextToken();
1476 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001477 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001478 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001479 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001480 // The actual identifier can be a nested name specifier, and in macros
1481 // it is often token-pasted.
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001482 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1483 FormatTok->is(tok::hashhash) ||
1484 (Style.Language == FormatStyle::LK_Java &&
1485 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001486 nextToken();
1487
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001488 // Note that parsing away template declarations here leads to incorrectly
1489 // accepting function declarations as record declarations.
1490 // In general, we cannot solve this problem. Consider:
1491 // class A<int> B() {}
1492 // which can be a function definition or a class definition when B() is a
1493 // macro. If we find enough real-world cases where this is a problem, we
1494 // can parse for the 'template' keyword in the beginning of the statement,
1495 // and thus rule out the record production in case there is no template
1496 // (this would still leave us with an ambiguity between template function
1497 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001498 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1499 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1500 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001501 return;
1502 nextToken();
1503 }
1504 }
1505 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001506 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001507 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001508 addUnwrappedLine();
1509
Daniel Jasper240dfda2014-03-31 14:23:49 +00001510 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001511 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001512 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001513 // We fall through to parsing a structural element afterwards, so
1514 // class A {} n, m;
1515 // will end up in one unwrapped line.
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001516 // This does not apply for Java.
1517 if (Style.Language == FormatStyle::LK_Java)
1518 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001519}
1520
Nico Weber8696a8d2013-01-09 21:15:03 +00001521void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001522 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001523 do
1524 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001525 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001526 nextToken(); // Skip '>'.
1527}
1528
1529void UnwrappedLineParser::parseObjCUntilAtEnd() {
1530 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001531 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001532 nextToken();
1533 addUnwrappedLine();
1534 break;
1535 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001536 if (FormatTok->is(tok::l_brace)) {
1537 parseBlock(/*MustBeDeclaration=*/false);
1538 // In ObjC interfaces, nothing should be following the "}".
1539 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001540 } else if (FormatTok->is(tok::r_brace)) {
1541 // Ignore stray "}". parseStructuralElement doesn't consume them.
1542 nextToken();
1543 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001544 } else {
1545 parseStructuralElement();
1546 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001547 } while (!eof());
1548}
1549
Nico Weber2ce0ac52013-01-09 23:25:37 +00001550void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001551 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001552 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001553
1554 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001555 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001556 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001557 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001558 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001559 // Skip category, if present.
1560 parseParens();
1561
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001562 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001563 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001564
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001565 if (FormatTok->Tok.is(tok::l_brace)) {
1566 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1567 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1568 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001569 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001570 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001571
1572 // With instance variables, this puts '}' on its own line. Without instance
1573 // variables, this ends the @interface line.
1574 addUnwrappedLine();
1575
Nico Weber8696a8d2013-01-09 21:15:03 +00001576 parseObjCUntilAtEnd();
1577}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001578
Nico Weber8696a8d2013-01-09 21:15:03 +00001579void UnwrappedLineParser::parseObjCProtocol() {
1580 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001581 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001582
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001583 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001584 parseObjCProtocolList();
1585
1586 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001587 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001588 nextToken();
1589 return addUnwrappedLine();
1590 }
1591
1592 addUnwrappedLine();
1593 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001594}
1595
Daniel Jasper3b203a62013-09-05 16:05:56 +00001596LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1597 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001598 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1599 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1600 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1601 E = Line.Tokens.end();
1602 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001603 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001604 }
1605 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1606 E = Line.Tokens.end();
1607 I != E; ++I) {
1608 const UnwrappedLineNode &Node = *I;
1609 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1610 I = Node.Children.begin(),
1611 E = Node.Children.end();
1612 I != E; ++I) {
1613 printDebugInfo(*I, "\nChild: ");
1614 }
1615 }
1616 llvm::dbgs() << "\n";
1617}
1618
Daniel Jasperf7935112012-12-03 18:12:45 +00001619void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001620 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001621 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001622 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001623 if (CurrentLines == &Lines)
1624 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001625 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001626 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001627 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001628 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001629 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001630 I = PreprocessorDirectives.begin(),
1631 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001632 I != E; ++I) {
1633 CurrentLines->push_back(*I);
1634 }
1635 PreprocessorDirectives.clear();
1636 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001637}
1638
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001639bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001640
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001641bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001642 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1643 FormatTok.NewlinesBefore > 0;
1644}
1645
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001646void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1647 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001648 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001649 I = CommentsBeforeNextToken.begin(),
1650 E = CommentsBeforeNextToken.end();
1651 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001652 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001653 addUnwrappedLine();
1654 }
1655 pushToken(*I);
1656 }
1657 if (NewlineBeforeNext && JustComments) {
1658 addUnwrappedLine();
1659 }
1660 CommentsBeforeNextToken.clear();
1661}
1662
Daniel Jasperf7935112012-12-03 18:12:45 +00001663void UnwrappedLineParser::nextToken() {
1664 if (eof())
1665 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001666 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001667 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001668 readToken();
1669}
1670
1671void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001672 bool CommentsInCurrentLine = true;
1673 do {
1674 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001675 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001676 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1677 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001678 // If there is an unfinished unwrapped line, we flush the preprocessor
1679 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001680 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001681 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001682 // Comments stored before the preprocessor directive need to be output
1683 // before the preprocessor directive, at the same level as the
1684 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001685 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001686 parsePPDirective();
1687 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001688 while (FormatTok->Type == TT_ConflictStart ||
1689 FormatTok->Type == TT_ConflictEnd ||
1690 FormatTok->Type == TT_ConflictAlternative) {
1691 if (FormatTok->Type == TT_ConflictStart) {
1692 conditionalCompilationStart(/*Unreachable=*/false);
1693 } else if (FormatTok->Type == TT_ConflictAlternative) {
1694 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001695 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001696 conditionalCompilationEnd();
1697 }
1698 FormatTok = Tokens->getNextToken();
1699 FormatTok->MustBreakBefore = true;
1700 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001701
1702 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1703 !Line->InPPDirective) {
1704 continue;
1705 }
1706
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001707 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001708 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001709 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001710 CommentsInCurrentLine = false;
1711 }
1712 if (CommentsInCurrentLine) {
1713 pushToken(FormatTok);
1714 } else {
1715 CommentsBeforeNextToken.push_back(FormatTok);
1716 }
1717 } while (!eof());
1718}
1719
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001720void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001721 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001722 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001723 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001724 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001725 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001726}
1727
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001728} // end namespace format
1729} // end namespace clang