blob: 52224180d5b1db853731d4a4194ecc71e595735e [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"
Benjamin Kramer33335df2015-03-01 21:36:40 +000017#include "llvm/ADT/STLExtras.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000018#include "llvm/Support/Debug.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000019
Chandler Carruth10346662014-04-22 03:17:02 +000020#define DEBUG_TYPE "format-parser"
21
Daniel Jasperf7935112012-12-03 18:12:45 +000022namespace clang {
23namespace format {
24
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000025class FormatTokenSource {
26public:
27 virtual ~FormatTokenSource() {}
28 virtual FormatToken *getNextToken() = 0;
29
30 virtual unsigned getPosition() = 0;
31 virtual FormatToken *setPosition(unsigned Position) = 0;
32};
33
Craig Topper69665e12013-07-01 04:21:54 +000034namespace {
35
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000036class ScopedDeclarationState {
37public:
38 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
39 bool MustBeDeclaration)
40 : Line(Line), Stack(Stack) {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000041 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek39080572013-01-23 11:03:04 +000042 Stack.push_back(MustBeDeclaration);
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000043 }
44 ~ScopedDeclarationState() {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000045 Stack.pop_back();
Manuel Klimekc1237a82013-01-23 14:08:21 +000046 if (!Stack.empty())
47 Line.MustBeDeclaration = Stack.back();
48 else
49 Line.MustBeDeclaration = true;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000050 }
Daniel Jasper393564f2013-05-31 14:56:29 +000051
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000052private:
53 UnwrappedLine &Line;
54 std::vector<bool> &Stack;
55};
56
Manuel Klimek1abf7892013-01-04 23:34:14 +000057class ScopedMacroState : public FormatTokenSource {
58public:
59 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000060 FormatToken *&ResetToken, bool &StructuralError)
Manuel Klimek1abf7892013-01-04 23:34:14 +000061 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000062 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
63 StructuralError(StructuralError),
Craig Topper2145bc02014-05-09 08:15:10 +000064 PreviousStructuralError(StructuralError), Token(nullptr) {
Manuel Klimek1abf7892013-01-04 23:34:14 +000065 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000066 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000067 Line.InPPDirective = true;
68 }
69
70 ~ScopedMacroState() {
71 TokenSource = PreviousTokenSource;
72 ResetToken = Token;
73 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000074 Line.Level = PreviousLineLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +000075 StructuralError = PreviousStructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +000076 }
77
Craig Topperfb6b25b2014-03-15 04:29:04 +000078 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +000079 // The \c UnwrappedLineParser guards against this by never calling
80 // \c getNextToken() after it has encountered the first eof token.
81 assert(!eof());
Manuel Klimek1abf7892013-01-04 23:34:14 +000082 Token = PreviousTokenSource->getNextToken();
83 if (eof())
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000084 return getFakeEOF();
Manuel Klimek1abf7892013-01-04 23:34:14 +000085 return Token;
86 }
87
Craig Topperfb6b25b2014-03-15 04:29:04 +000088 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +000089
Craig Topperfb6b25b2014-03-15 04:29:04 +000090 FormatToken *setPosition(unsigned Position) override {
Manuel Klimekab419912013-05-23 09:41:43 +000091 Token = PreviousTokenSource->setPosition(Position);
92 return Token;
93 }
94
Manuel Klimek1abf7892013-01-04 23:34:14 +000095private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000096 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimek1abf7892013-01-04 23:34:14 +000097
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000098 FormatToken *getFakeEOF() {
99 static bool EOFInitialized = false;
100 static FormatToken FormatTok;
101 if (!EOFInitialized) {
102 FormatTok.Tok.startToken();
103 FormatTok.Tok.setKind(tok::eof);
104 EOFInitialized = true;
105 }
106 return &FormatTok;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000107 }
108
109 UnwrappedLine &Line;
110 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000111 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000112 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000113 FormatTokenSource *PreviousTokenSource;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000114 bool &StructuralError;
115 bool PreviousStructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000116
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000117 FormatToken *Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000118};
119
Craig Topper69665e12013-07-01 04:21:54 +0000120} // end anonymous namespace
121
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000122class ScopedLineState {
123public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000124 ScopedLineState(UnwrappedLineParser &Parser,
125 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000126 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000127 if (SwitchToPreprocessorLines)
128 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000129 else if (!Parser.Line->Tokens.empty())
130 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000131 PreBlockLine = std::move(Parser.Line);
132 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000133 Parser.Line->Level = PreBlockLine->Level;
134 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000135 }
136
137 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000138 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000139 Parser.addUnwrappedLine();
140 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000141 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000142 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000143 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
144 Parser.MustBreakBeforeNextToken = true;
145 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000146 }
147
148private:
149 UnwrappedLineParser &Parser;
150
David Blaikieefb6eb22014-08-09 20:02:07 +0000151 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000152 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000153};
154
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000155class CompoundStatementIndenter {
156public:
157 CompoundStatementIndenter(UnwrappedLineParser *Parser,
158 const FormatStyle &Style, unsigned &LineLevel)
159 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
160 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
161 Parser->addUnwrappedLine();
162 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
163 Parser->addUnwrappedLine();
164 ++LineLevel;
165 }
166 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000167 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000168
169private:
170 unsigned &LineLevel;
171 unsigned OldLineLevel;
172};
173
Craig Topper69665e12013-07-01 04:21:54 +0000174namespace {
175
Manuel Klimekab419912013-05-23 09:41:43 +0000176class IndexedTokenSource : public FormatTokenSource {
177public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000178 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000179 : Tokens(Tokens), Position(-1) {}
180
Craig Topperfb6b25b2014-03-15 04:29:04 +0000181 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000182 ++Position;
183 return Tokens[Position];
184 }
185
Craig Topperfb6b25b2014-03-15 04:29:04 +0000186 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000187 assert(Position >= 0);
188 return Position;
189 }
190
Craig Topperfb6b25b2014-03-15 04:29:04 +0000191 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000192 Position = P;
193 return Tokens[Position];
194 }
195
Manuel Klimek71814b42013-10-11 21:25:45 +0000196 void reset() { Position = -1; }
197
Manuel Klimekab419912013-05-23 09:41:43 +0000198private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000199 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000200 int Position;
201};
202
Craig Topper69665e12013-07-01 04:21:54 +0000203} // end anonymous namespace
204
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000205UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000206 const AdditionalKeywords &Keywords,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000207 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000208 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000209 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
210 CurrentLines(&Lines), StructuralError(false), Style(Style),
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000211 Keywords(Keywords), Tokens(nullptr), Callback(Callback),
212 AllTokens(Tokens), PPBranchLevel(-1) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000213
214void UnwrappedLineParser::reset() {
215 PPBranchLevel = -1;
216 Line.reset(new UnwrappedLine);
217 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000218 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000219 MustBreakBeforeNextToken = false;
220 PreprocessorDirectives.clear();
221 CurrentLines = &Lines;
222 DeclarationScopeStack.clear();
223 StructuralError = false;
224 PPStack.clear();
225}
Daniel Jasperf7935112012-12-03 18:12:45 +0000226
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000227bool UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000228 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimek71814b42013-10-11 21:25:45 +0000229 do {
230 DEBUG(llvm::dbgs() << "----\n");
231 reset();
232 Tokens = &TokenSource;
233 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000234
Manuel Klimek71814b42013-10-11 21:25:45 +0000235 readToken();
236 parseFile();
237 // Create line with eof token.
238 pushToken(FormatTok);
239 addUnwrappedLine();
240
241 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
242 E = Lines.end();
243 I != E; ++I) {
244 Callback.consumeUnwrappedLine(*I);
245 }
246 Callback.finishRun();
247 Lines.clear();
248 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000249 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000250 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
251 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
252 }
253 if (!PPLevelBranchIndex.empty()) {
254 ++PPLevelBranchIndex.back();
255 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
256 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
257 }
258 } while (!PPLevelBranchIndex.empty());
259
Manuel Klimek1a18c402013-04-12 14:13:36 +0000260 return StructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000261}
262
Manuel Klimek1a18c402013-04-12 14:13:36 +0000263void UnwrappedLineParser::parseFile() {
Daniel Jasperdd9276e2013-03-22 16:55:40 +0000264 ScopedDeclarationState DeclarationState(
265 *Line, DeclarationScopeStack,
Daniel Jasperfca735c2015-02-19 16:14:18 +0000266 /*MustBeDeclaration=*/!Line->InPPDirective);
Nico Weber9096fc02013-06-26 00:30:14 +0000267 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000268 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000269 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000270 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000271}
272
Manuel Klimek1a18c402013-04-12 14:13:36 +0000273void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000274 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000275 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000276 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000277 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000278 nextToken();
279 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000280 break;
281 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000282 // FIXME: Add parameter whether this can happen - if this happens, we must
283 // be in a non-declaration context.
Nico Weber9096fc02013-06-26 00:30:14 +0000284 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000285 addUnwrappedLine();
286 break;
287 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000288 if (HasOpeningBrace)
289 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000290 StructuralError = true;
291 nextToken();
292 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000293 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000294 case tok::kw_default:
295 case tok::kw_case:
Daniel Jasper72407622013-09-02 08:26:29 +0000296 if (!SwitchLabelEncountered &&
297 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
298 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000299 SwitchLabelEncountered = true;
300 parseStructuralElement();
301 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000302 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000303 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000304 break;
305 }
306 } while (!eof());
307}
308
Manuel Klimekab419912013-05-23 09:41:43 +0000309void UnwrappedLineParser::calculateBraceTypes() {
310 // We'll parse forward through the tokens until we hit
311 // a closing brace or eof - note that getNextToken() will
312 // parse macros, so this will magically work inside macro
313 // definitions, too.
314 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000315 FormatToken *Tok = FormatTok;
Manuel Klimekab419912013-05-23 09:41:43 +0000316 // Keep a stack of positions of lbrace tokens. We will
317 // update information about whether an lbrace starts a
318 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000319 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000320 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000321 do {
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000322 // Get next none-comment token.
323 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000324 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000325 do {
326 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000327 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000328 } while (NextTok->is(tok::comment));
329
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000330 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000331 case tok::l_brace:
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000332 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000333 break;
334 case tok::r_brace:
335 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000336 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Daniel Jasper220c0d12014-04-10 07:27:12 +0000337 bool ProbablyBracedList = false;
338 if (Style.Language == FormatStyle::LK_Proto) {
339 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
340 } else {
Daniel Jasper91b032a2014-05-22 12:46:38 +0000341 // Using OriginalColumn to distinguish between ObjC methods and
342 // binary operators is a bit hacky.
343 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
344 NextTok->OriginalColumn == 0;
345
Daniel Jasper220c0d12014-04-10 07:27:12 +0000346 // If there is a comma, semicolon or right paren after the closing
347 // brace, we assume this is a braced initializer list. Note that
348 // regardless how we mark inner braces here, we will overwrite the
349 // BlockKind later if we parse a braced list (where all blocks
350 // inside are by default braced lists), or when we explicitly detect
351 // blocks (for example while parsing lambdas).
352 //
353 // We exclude + and - as they can be ObjC visibility modifiers.
354 ProbablyBracedList =
355 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
Daniel Jasper438059e2014-05-22 12:11:13 +0000356 tok::r_paren, tok::r_square, tok::l_brace,
Daniel Jasper65df5aa2014-08-04 14:51:02 +0000357 tok::l_paren, tok::ellipsis) ||
Daniel Jasper91b032a2014-05-22 12:46:38 +0000358 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000359 }
360 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000361 Tok->BlockKind = BK_BracedInit;
362 LBraceStack.back()->BlockKind = BK_BracedInit;
363 } else {
364 Tok->BlockKind = BK_Block;
365 LBraceStack.back()->BlockKind = BK_Block;
366 }
Manuel Klimekab419912013-05-23 09:41:43 +0000367 }
368 LBraceStack.pop_back();
369 }
370 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000371 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000372 case tok::semi:
373 case tok::kw_if:
374 case tok::kw_while:
375 case tok::kw_for:
376 case tok::kw_switch:
377 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000378 case tok::kw___try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000379 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000380 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000381 break;
382 default:
383 break;
384 }
385 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000386 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000387 // Assume other blocks for all unclosed opening braces.
388 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000389 if (LBraceStack[i]->BlockKind == BK_Unknown)
390 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000391 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000392
Manuel Klimekab419912013-05-23 09:41:43 +0000393 FormatTok = Tokens->setPosition(StoredPosition);
394}
395
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000396void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
397 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000398 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000399 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000400 nextToken();
401
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000402 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000403
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000404 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
405 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000406 if (AddLevel)
407 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000408 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000409
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000410 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000411 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000412 StructuralError = true;
413 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000414 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000415
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000416 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000417 if (MunchSemi && FormatTok->Tok.is(tok::semi))
418 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000419 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000420}
421
Daniel Jasper4a39c842014-05-06 13:54:10 +0000422static bool IsGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000423 // FIXME: Closure-library specific stuff should not be hard-coded but be
424 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000425 if (Line.Tokens.size() < 4)
426 return false;
427 auto I = Line.Tokens.begin();
428 if (I->Tok->TokenText != "goog")
429 return false;
430 ++I;
431 if (I->Tok->isNot(tok::period))
432 return false;
433 ++I;
434 if (I->Tok->TokenText != "scope")
435 return false;
436 ++I;
437 return I->Tok->is(tok::l_paren);
438}
439
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000440static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
441 const FormatToken &InitialToken) {
442 switch (Style.BreakBeforeBraces) {
443 case FormatStyle::BS_Linux:
444 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
445 case FormatStyle::BS_Allman:
446 case FormatStyle::BS_GNU:
447 return true;
448 default:
449 return false;
450 }
451}
452
Manuel Klimek516e0542013-09-04 13:25:30 +0000453void UnwrappedLineParser::parseChildBlock() {
454 FormatTok->BlockKind = BK_Block;
455 nextToken();
456 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000457 bool GoogScope =
458 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000459 ScopedLineState LineState(*this);
460 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
461 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000462 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000463 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000464 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000465 }
466 nextToken();
467}
468
Daniel Jasperf7935112012-12-03 18:12:45 +0000469void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000470 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000471 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000472 nextToken();
473
Craig Topper2145bc02014-05-09 08:15:10 +0000474 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000475 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000476 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000477 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000478
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000479 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000480 case tok::pp_define:
481 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000482 return;
483 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000484 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000485 break;
486 case tok::pp_ifdef:
487 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000488 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000489 break;
490 case tok::pp_else:
491 parsePPElse();
492 break;
493 case tok::pp_elif:
494 parsePPElIf();
495 break;
496 case tok::pp_endif:
497 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000498 break;
499 default:
500 parsePPUnknown();
501 break;
502 }
503}
504
Manuel Klimek68b03042014-04-14 09:14:11 +0000505void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
506 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000507 PPStack.push_back(PP_Unreachable);
508 else
509 PPStack.push_back(PP_Conditional);
510}
511
Manuel Klimek68b03042014-04-14 09:14:11 +0000512void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000513 ++PPBranchLevel;
514 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
515 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
516 PPLevelBranchIndex.push_back(0);
517 PPLevelBranchCount.push_back(0);
518 }
519 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000520 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
521 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000522}
523
Manuel Klimek68b03042014-04-14 09:14:11 +0000524void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000525 if (!PPStack.empty())
526 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000527 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
528 if (!PPChainBranchIndex.empty())
529 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000530 conditionalCompilationCondition(
531 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
532 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000533}
534
Manuel Klimek68b03042014-04-14 09:14:11 +0000535void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000536 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
537 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
538 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000539 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
540 }
541 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000542 // Guard against #endif's without #if.
543 if (PPBranchLevel > 0)
544 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000545 if (!PPChainBranchIndex.empty())
546 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000547 if (!PPStack.empty())
548 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000549}
550
551void UnwrappedLineParser::parsePPIf(bool IfDef) {
552 nextToken();
553 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Daniel Jasper9d22bcc2015-01-19 10:51:05 +0000554 FormatTok->Tok.getLiteralData() != nullptr &&
Manuel Klimek68b03042014-04-14 09:14:11 +0000555 StringRef(FormatTok->Tok.getLiteralData(),
556 FormatTok->Tok.getLength()) == "0") ||
557 FormatTok->Tok.is(tok::kw_false);
558 conditionalCompilationStart(!IfDef && IsLiteralFalse);
559 parsePPUnknown();
560}
561
562void UnwrappedLineParser::parsePPElse() {
563 conditionalCompilationAlternative();
564 parsePPUnknown();
565}
566
567void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
568
569void UnwrappedLineParser::parsePPEndIf() {
570 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000571 parsePPUnknown();
572}
573
Manuel Klimek1abf7892013-01-04 23:34:14 +0000574void UnwrappedLineParser::parsePPDefine() {
575 nextToken();
576
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000577 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000578 parsePPUnknown();
579 return;
580 }
581 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000582 if (FormatTok->Tok.getKind() == tok::l_paren &&
583 FormatTok->WhitespaceRange.getBegin() ==
584 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000585 parseParens();
586 }
587 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000588 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000589
590 // Errors during a preprocessor directive can only affect the layout of the
591 // preprocessor directive, and thus we ignore them. An alternative approach
592 // would be to use the same approach we use on the file level (no
593 // re-indentation if there was a structural error) within the macro
594 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000595 parseFile();
596}
597
598void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000599 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000600 nextToken();
601 } while (!eof());
602 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000603}
604
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000605// Here we blacklist certain tokens that are not usually the first token in an
606// unwrapped line. This is used in attempt to distinguish macro calls without
607// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000608static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000609 // Semicolon can be a null-statement, l_square can be a start of a macro or
610 // a C++11 attribute, but this doesn't seem to be common.
611 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
612 Tok.isNot(tok::l_square) &&
613 // Tokens that can only be used as binary operators and a part of
614 // overloaded operator names.
615 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
616 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
617 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
618 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
619 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
620 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
621 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
622 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
623 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
624 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
625 Tok.isNot(tok::lesslessequal) &&
626 // Colon is used in labels, base class lists, initializer lists,
627 // range-based for loops, ternary operator, but should never be the
628 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000629 Tok.isNot(tok::colon) &&
630 // 'noexcept' is a trailing annotation.
631 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000632}
633
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000634void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000635 assert(!FormatTok->Tok.is(tok::l_brace));
636 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000637 case tok::at:
638 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000639 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000640 parseBracedList();
641 break;
642 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000643 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000644 case tok::objc_public:
645 case tok::objc_protected:
646 case tok::objc_package:
647 case tok::objc_private:
648 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000649 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000650 case tok::objc_implementation:
651 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000652 case tok::objc_protocol:
653 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000654 case tok::objc_end:
655 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000656 case tok::objc_optional:
657 case tok::objc_required:
658 nextToken();
659 addUnwrappedLine();
660 return;
Nico Weber33381f52015-02-07 01:57:32 +0000661 case tok::objc_try:
662 // This branch isn't strictly necessary (the kw_try case below would
663 // do this too after the tok::at is parsed above). But be explicit.
664 parseTryCatch();
665 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000666 default:
667 break;
668 }
669 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000670 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000671 nextToken();
672 if (FormatTok->is(tok::l_brace)) {
Daniel Jasper2337f282015-01-12 10:14:56 +0000673 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000674 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000675 if (FormatTok->is(tok::r_brace)) {
676 nextToken();
677 break;
678 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000679 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000680 nextToken();
681 }
682 }
683 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000684 case tok::kw_namespace:
685 parseNamespace();
686 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000687 case tok::kw_inline:
688 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000689 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000690 parseNamespace();
691 return;
692 }
693 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000694 case tok::kw_public:
695 case tok::kw_protected:
696 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +0000697 if (Style.Language == FormatStyle::LK_Java ||
698 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000699 nextToken();
700 else
701 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000702 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000703 case tok::kw_if:
704 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000705 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000706 case tok::kw_for:
707 case tok::kw_while:
708 parseForOrWhileLoop();
709 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000710 case tok::kw_do:
711 parseDoWhile();
712 return;
713 case tok::kw_switch:
714 parseSwitch();
715 return;
716 case tok::kw_default:
717 nextToken();
718 parseLabel();
719 return;
720 case tok::kw_case:
721 parseCaseLabel();
722 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000723 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000724 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +0000725 parseTryCatch();
726 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000727 case tok::kw_extern:
728 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000729 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000730 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000731 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000732 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000733 addUnwrappedLine();
734 return;
735 }
736 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000737 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +0000738 case tok::kw_export:
739 if (Style.Language == FormatStyle::LK_JavaScript) {
740 parseJavaScriptEs6ImportExport();
741 return;
742 }
743 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000744 case tok::identifier:
745 if (FormatTok->IsForEachMacro) {
746 parseForOrWhileLoop();
747 return;
748 }
Daniel Jasper354aa512015-02-19 16:07:32 +0000749 if (Style.Language == FormatStyle::LK_JavaScript &&
750 FormatTok->is(Keywords.kw_import)) {
Daniel Jasperfca735c2015-02-19 16:14:18 +0000751 parseJavaScriptEs6ImportExport();
Daniel Jasper354aa512015-02-19 16:07:32 +0000752 return;
753 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000754 // In all other cases, parse the declaration.
755 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000756 default:
757 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000758 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000759 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000760 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000761 case tok::at:
762 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000763 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000764 parseBracedList();
765 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000766 case tok::kw_enum:
767 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000768 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000769 case tok::kw_typedef:
770 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000771 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
772 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000773 parseEnum();
774 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000775 case tok::kw_struct:
776 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000777 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000778 parseRecord();
779 // A record declaration or definition is always the start of a structural
780 // element.
781 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000782 case tok::period:
783 nextToken();
784 // In Java, classes have an implicit static member "class".
785 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
786 FormatTok->is(tok::kw_class))
787 nextToken();
788 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000789 case tok::semi:
790 nextToken();
791 addUnwrappedLine();
792 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000793 case tok::r_brace:
794 addUnwrappedLine();
795 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000796 case tok::l_paren:
797 parseParens();
798 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000799 case tok::caret:
800 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000801 if (FormatTok->Tok.isAnyIdentifier() ||
802 FormatTok->isSimpleTypeSpecifier())
803 nextToken();
804 if (FormatTok->is(tok::l_paren))
805 parseParens();
806 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000807 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000808 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000809 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000810 if (!tryToParseBracedList()) {
811 // A block outside of parentheses must be the last part of a
812 // structural element.
813 // FIXME: Figure out cases where this is not true, and add projections
814 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000815 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000816 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000817 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000818 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000819 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000820 return;
821 }
822 // Otherwise this was a braced init list, and the structural
823 // element continues.
824 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000825 case tok::kw_try:
826 // We arrive here when parsing function-try blocks.
827 parseTryCatch();
828 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000829 case tok::identifier: {
830 StringRef Text = FormatTok->TokenText;
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000831 // Parse function literal unless 'function' is the first token in a line
832 // in which case this should be treated as a free-standing function.
833 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
834 Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000835 tryToParseJSFunction();
836 break;
837 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000838 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +0000839 if (Line->Tokens.size() == 1 &&
840 // JS doesn't have macros, and within classes colons indicate fields,
841 // not labels.
842 (Style.Language != FormatStyle::LK_JavaScript ||
843 !Line->MustBeDeclaration)) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000844 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000845 parseLabel();
846 return;
847 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000848 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +0000849 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +0000850 bool FunctionLike = FormatTok->is(tok::l_paren);
851 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000852 parseParens();
Daniel Jasper680b09b2014-11-05 10:48:04 +0000853 if (FormatTok->NewlinesBefore > 0 &&
854 (Text.size() >= 5 || FunctionLike) &&
855 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000856 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000857 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000858 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000859 }
860 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000861 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000862 case tok::equal:
863 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000864 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000865 parseBracedList();
866 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000867 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000868 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000869 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000870 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000871 default:
872 nextToken();
873 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000874 }
875 } while (!eof());
876}
877
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000878bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000879 // FIXME: This is a dirty way to access the previous token. Find a better
880 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000881 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000882 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
883 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000884 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000885 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000886 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000887 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000888 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000889 assert(FormatTok->is(tok::l_square));
890 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000891 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000892 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000893
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000894 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000895 if (FormatTok->isSimpleTypeSpecifier()) {
896 nextToken();
897 continue;
898 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000899 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000900 case tok::l_brace:
901 break;
902 case tok::l_paren:
903 parseParens();
904 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000905 case tok::amp:
906 case tok::star:
907 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000908 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000909 case tok::less:
910 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000911 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000912 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000913 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000914 nextToken();
915 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000916 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000917 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000918 nextToken();
919 break;
920 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000921 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000922 }
923 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000924 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000925 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000926 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000927}
928
929bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
930 nextToken();
931 if (FormatTok->is(tok::equal)) {
932 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000933 if (FormatTok->is(tok::r_square)) {
934 nextToken();
935 return true;
936 }
937 if (FormatTok->isNot(tok::comma))
938 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000939 nextToken();
940 } else if (FormatTok->is(tok::amp)) {
941 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000942 if (FormatTok->is(tok::r_square)) {
943 nextToken();
944 return true;
945 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000946 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
947 return false;
948 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000949 if (FormatTok->is(tok::comma))
950 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000951 } else if (FormatTok->is(tok::r_square)) {
952 nextToken();
953 return true;
954 }
955 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000956 if (FormatTok->is(tok::amp))
957 nextToken();
958 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
959 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000960 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000961 if (FormatTok->is(tok::ellipsis))
962 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000963 if (FormatTok->is(tok::comma)) {
964 nextToken();
965 } else if (FormatTok->is(tok::r_square)) {
966 nextToken();
967 return true;
968 } else {
969 return false;
970 }
971 } while (!eof());
972 return false;
973}
974
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000975void UnwrappedLineParser::tryToParseJSFunction() {
976 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000977
978 // Consume function name.
979 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +0000980 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000981
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000982 if (FormatTok->isNot(tok::l_paren))
983 return;
984 nextToken();
985 while (FormatTok->isNot(tok::l_brace)) {
986 // Err on the side of caution in order to avoid consuming the full file in
987 // case of incomplete code.
988 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
989 tok::comment))
990 return;
991 nextToken();
992 }
993 parseChildBlock();
994}
995
Manuel Klimekab419912013-05-23 09:41:43 +0000996bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000997 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +0000998 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000999 assert(FormatTok->BlockKind != BK_Unknown);
1000 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001001 return false;
1002 parseBracedList();
1003 return true;
1004}
1005
Daniel Jasper015ed022013-09-13 09:20:45 +00001006bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1007 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001008 nextToken();
1009
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001010 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1011 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001012 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001013 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001014 FormatTok->is(Keywords.kw_function)) {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001015 tryToParseJSFunction();
1016 continue;
1017 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001018 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001019 case tok::caret:
1020 nextToken();
1021 if (FormatTok->is(tok::l_brace)) {
1022 parseChildBlock();
1023 }
1024 break;
1025 case tok::l_square:
1026 tryToParseLambda();
1027 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001028 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001029 // Assume there are no blocks inside a braced init list apart
1030 // from the ones we explicitly parse out (like lambdas).
1031 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001032 parseBracedList();
1033 break;
1034 case tok::r_brace:
1035 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001036 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001037 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001038 HasError = true;
1039 if (!ContinueOnSemicolons)
1040 return !HasError;
1041 nextToken();
1042 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001043 case tok::comma:
1044 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001045 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001046 default:
1047 nextToken();
1048 break;
1049 }
1050 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001051 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001052}
1053
Daniel Jasperf7935112012-12-03 18:12:45 +00001054void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001055 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001056 nextToken();
1057 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001058 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001059 case tok::l_paren:
1060 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001061 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1062 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001063 break;
1064 case tok::r_paren:
1065 nextToken();
1066 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001067 case tok::r_brace:
1068 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1069 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001070 case tok::l_square:
1071 tryToParseLambda();
1072 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001073 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001074 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001075 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001076 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001077 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001078 case tok::at:
1079 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001080 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001081 parseBracedList();
1082 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001083 case tok::identifier:
1084 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001085 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001086 tryToParseJSFunction();
1087 else
1088 nextToken();
1089 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001090 default:
1091 nextToken();
1092 break;
1093 }
1094 } while (!eof());
1095}
1096
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001097void UnwrappedLineParser::parseSquare() {
1098 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1099 if (tryToParseLambda())
1100 return;
1101 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001102 switch (FormatTok->Tok.getKind()) {
1103 case tok::l_paren:
1104 parseParens();
1105 break;
1106 case tok::r_square:
1107 nextToken();
1108 return;
1109 case tok::r_brace:
1110 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1111 return;
1112 case tok::l_square:
1113 parseSquare();
1114 break;
1115 case tok::l_brace: {
1116 if (!tryToParseBracedList()) {
1117 parseChildBlock();
1118 }
1119 break;
1120 }
1121 case tok::at:
1122 nextToken();
1123 if (FormatTok->Tok.is(tok::l_brace))
1124 parseBracedList();
1125 break;
1126 default:
1127 nextToken();
1128 break;
1129 }
1130 } while (!eof());
1131}
1132
Daniel Jasperf7935112012-12-03 18:12:45 +00001133void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001134 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001135 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001136 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001137 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001138 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001139 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001140 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001141 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001142 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1143 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001144 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001145 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001146 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001147 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001148 } else {
1149 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001150 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001151 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001152 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001153 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001154 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001155 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1156 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001157 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001158 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001159 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001160 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001161 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001162 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001163 parseIfThenElse();
1164 } else {
1165 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001166 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001167 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001168 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001169 }
1170 } else if (NeedsUnwrappedLine) {
1171 addUnwrappedLine();
1172 }
1173}
1174
Daniel Jasper04a71a42014-05-08 11:58:24 +00001175void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001176 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001177 nextToken();
1178 bool NeedsUnwrappedLine = false;
1179 if (FormatTok->is(tok::colon)) {
1180 // We are in a function try block, what comes is an initializer list.
1181 nextToken();
1182 while (FormatTok->is(tok::identifier)) {
1183 nextToken();
1184 if (FormatTok->is(tok::l_paren))
1185 parseParens();
1186 else
1187 StructuralError = true;
1188 if (FormatTok->is(tok::comma))
1189 nextToken();
1190 }
1191 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001192 // Parse try with resource.
1193 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1194 parseParens();
1195 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001196 if (FormatTok->is(tok::l_brace)) {
1197 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1198 parseBlock(/*MustBeDeclaration=*/false);
1199 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1200 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1201 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1202 addUnwrappedLine();
1203 } else {
1204 NeedsUnwrappedLine = true;
1205 }
1206 } else if (!FormatTok->is(tok::kw_catch)) {
1207 // The C++ standard requires a compound-statement after a try.
1208 // If there's none, we try to assume there's a structuralElement
1209 // and try to continue.
1210 StructuralError = true;
1211 addUnwrappedLine();
1212 ++Line->Level;
1213 parseStructuralElement();
1214 --Line->Level;
1215 }
Nico Weber33381f52015-02-07 01:57:32 +00001216 while (1) {
1217 if (FormatTok->is(tok::at))
1218 nextToken();
1219 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1220 tok::kw___finally) ||
1221 ((Style.Language == FormatStyle::LK_Java ||
1222 Style.Language == FormatStyle::LK_JavaScript) &&
1223 FormatTok->is(Keywords.kw_finally)) ||
1224 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1225 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1226 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001227 nextToken();
1228 while (FormatTok->isNot(tok::l_brace)) {
1229 if (FormatTok->is(tok::l_paren)) {
1230 parseParens();
1231 continue;
1232 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001233 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001234 return;
1235 nextToken();
1236 }
1237 NeedsUnwrappedLine = false;
1238 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1239 parseBlock(/*MustBeDeclaration=*/false);
1240 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1241 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1242 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1243 addUnwrappedLine();
1244 } else {
1245 NeedsUnwrappedLine = true;
1246 }
1247 }
1248 if (NeedsUnwrappedLine) {
1249 addUnwrappedLine();
1250 }
1251}
1252
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001253void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001254 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001255
1256 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001257 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001258 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001259 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001260 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001261 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001262 addUnwrappedLine();
1263
Daniel Jasper65ee3472013-07-31 23:16:02 +00001264 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1265 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1266 DeclarationScopeStack.size() > 1);
1267 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001268 // Munch the semicolon after a namespace. This is more common than one would
1269 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001270 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001271 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001272 addUnwrappedLine();
1273 }
1274 // FIXME: Add error handling.
1275}
1276
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001277void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001278 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1279 FormatTok->IsForEachMacro) &&
1280 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001281 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001282 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001283 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001284 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001285 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001286 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001287 addUnwrappedLine();
1288 } else {
1289 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001290 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001291 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001292 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001293 }
1294}
1295
Daniel Jasperf7935112012-12-03 18:12:45 +00001296void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001297 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001298 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001299 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001300 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001301 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001302 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1303 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001304 } else {
1305 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001306 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001307 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001308 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001309 }
1310
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001311 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001312 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001313 addUnwrappedLine();
1314 return;
1315 }
1316
Daniel Jasperf7935112012-12-03 18:12:45 +00001317 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001318 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001319}
1320
1321void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001322 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001323 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001324 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001325 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001326 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001327 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001328 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001329 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001330 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1331 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1332 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001333 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001334 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001335 parseStructuralElement();
1336 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001337 addUnwrappedLine();
1338 } else {
1339 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001340 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001341 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001342}
1343
1344void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001345 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001346 // FIXME: fix handling of complex expressions here.
1347 do {
1348 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001349 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001350 parseLabel();
1351}
1352
1353void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001354 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001355 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001356 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001357 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001358 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001359 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001360 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001361 addUnwrappedLine();
1362 } else {
1363 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001364 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001365 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001366 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001367 }
1368}
1369
1370void UnwrappedLineParser::parseAccessSpecifier() {
1371 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001372 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001373 if (FormatTok->is(tok::identifier) &&
1374 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001375 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001376 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001377 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001378 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001379 addUnwrappedLine();
1380}
1381
1382void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001383 // Won't be 'enum' for NS_ENUMs.
1384 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001385 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001386
Daniel Jasper2b41a822013-08-20 12:42:50 +00001387 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001388 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1389 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001390 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001391 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1392 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001393 nextToken();
1394 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001395 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001396 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001397 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001398 nextToken();
1399 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001400
1401 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001402 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001403 return;
1404 FormatTok->BlockKind = BK_Block;
1405
1406 if (Style.Language == FormatStyle::LK_Java) {
1407 // Java enums are different.
1408 parseJavaEnumBody();
1409 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001410 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001411
1412 // Parse enum body.
1413 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1414 if (HasError) {
1415 if (FormatTok->is(tok::semi))
1416 nextToken();
1417 addUnwrappedLine();
1418 }
1419
Manuel Klimek2cec0192013-01-21 19:17:52 +00001420 // We fall through to parsing a structural element afterwards, so that in
1421 // enum A {} n, m;
1422 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001423}
1424
1425void UnwrappedLineParser::parseJavaEnumBody() {
1426 // Determine whether the enum is simple, i.e. does not have a semicolon or
1427 // constants with class bodies. Simple enums can be formatted like braced
1428 // lists, contracted to a single line, etc.
1429 unsigned StoredPosition = Tokens->getPosition();
1430 bool IsSimple = true;
1431 FormatToken *Tok = Tokens->getNextToken();
1432 while (Tok) {
1433 if (Tok->is(tok::r_brace))
1434 break;
1435 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1436 IsSimple = false;
1437 break;
1438 }
1439 // FIXME: This will also mark enums with braces in the arguments to enum
1440 // constants as "not simple". This is probably fine in practice, though.
1441 Tok = Tokens->getNextToken();
1442 }
1443 FormatTok = Tokens->setPosition(StoredPosition);
1444
1445 if (IsSimple) {
1446 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001447 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001448 return;
1449 }
1450
1451 // Parse the body of a more complex enum.
1452 // First add a line for everything up to the "{".
1453 nextToken();
1454 addUnwrappedLine();
1455 ++Line->Level;
1456
1457 // Parse the enum constants.
1458 while (FormatTok) {
1459 if (FormatTok->is(tok::l_brace)) {
1460 // Parse the constant's class body.
1461 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1462 /*MunchSemi=*/false);
1463 } else if (FormatTok->is(tok::l_paren)) {
1464 parseParens();
1465 } else if (FormatTok->is(tok::comma)) {
1466 nextToken();
1467 addUnwrappedLine();
1468 } else if (FormatTok->is(tok::semi)) {
1469 nextToken();
1470 addUnwrappedLine();
1471 break;
1472 } else if (FormatTok->is(tok::r_brace)) {
1473 addUnwrappedLine();
1474 break;
1475 } else {
1476 nextToken();
1477 }
1478 }
1479
1480 // Parse the class body after the enum's ";" if any.
1481 parseLevel(/*HasOpeningBrace=*/true);
1482 nextToken();
1483 --Line->Level;
1484 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001485}
1486
Manuel Klimeke01bab52013-01-15 13:38:33 +00001487void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001488 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001489 nextToken();
Manuel Klimek45bf56c2014-07-31 07:19:30 +00001490 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1491 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001492 nextToken();
1493 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001494 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001495 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001496 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001497 // The actual identifier can be a nested name specifier, and in macros
1498 // it is often token-pasted.
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001499 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1500 FormatTok->is(tok::hashhash) ||
1501 (Style.Language == FormatStyle::LK_Java &&
1502 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001503 nextToken();
1504
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001505 // Note that parsing away template declarations here leads to incorrectly
1506 // accepting function declarations as record declarations.
1507 // In general, we cannot solve this problem. Consider:
1508 // class A<int> B() {}
1509 // which can be a function definition or a class definition when B() is a
1510 // macro. If we find enough real-world cases where this is a problem, we
1511 // can parse for the 'template' keyword in the beginning of the statement,
1512 // and thus rule out the record production in case there is no template
1513 // (this would still leave us with an ambiguity between template function
1514 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001515 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1516 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1517 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001518 return;
1519 nextToken();
1520 }
1521 }
1522 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001523 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001524 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001525 addUnwrappedLine();
1526
Daniel Jasper240dfda2014-03-31 14:23:49 +00001527 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001528 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001529 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001530 // We fall through to parsing a structural element afterwards, so
1531 // class A {} n, m;
1532 // will end up in one unwrapped line.
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001533 // This does not apply for Java.
Daniel Jasper6fa9ec72015-02-19 16:03:16 +00001534 if (Style.Language == FormatStyle::LK_Java ||
1535 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001536 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001537}
1538
Nico Weber8696a8d2013-01-09 21:15:03 +00001539void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001540 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001541 do
1542 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001543 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001544 nextToken(); // Skip '>'.
1545}
1546
1547void UnwrappedLineParser::parseObjCUntilAtEnd() {
1548 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001549 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001550 nextToken();
1551 addUnwrappedLine();
1552 break;
1553 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001554 if (FormatTok->is(tok::l_brace)) {
1555 parseBlock(/*MustBeDeclaration=*/false);
1556 // In ObjC interfaces, nothing should be following the "}".
1557 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001558 } else if (FormatTok->is(tok::r_brace)) {
1559 // Ignore stray "}". parseStructuralElement doesn't consume them.
1560 nextToken();
1561 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001562 } else {
1563 parseStructuralElement();
1564 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001565 } while (!eof());
1566}
1567
Nico Weber2ce0ac52013-01-09 23:25:37 +00001568void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001569 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001570 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001571
1572 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001573 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001574 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001575 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001576 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001577 // Skip category, if present.
1578 parseParens();
1579
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001580 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001581 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001582
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001583 if (FormatTok->Tok.is(tok::l_brace)) {
1584 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1585 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1586 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001587 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001588 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001589
1590 // With instance variables, this puts '}' on its own line. Without instance
1591 // variables, this ends the @interface line.
1592 addUnwrappedLine();
1593
Nico Weber8696a8d2013-01-09 21:15:03 +00001594 parseObjCUntilAtEnd();
1595}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001596
Nico Weber8696a8d2013-01-09 21:15:03 +00001597void UnwrappedLineParser::parseObjCProtocol() {
1598 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001599 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001600
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001601 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001602 parseObjCProtocolList();
1603
1604 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001605 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001606 nextToken();
1607 return addUnwrappedLine();
1608 }
1609
1610 addUnwrappedLine();
1611 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001612}
1613
Daniel Jasperfca735c2015-02-19 16:14:18 +00001614void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1615 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00001616 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00001617
1618 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_function,
1619 Keywords.kw_var))
1620 return; // Fall through to parsing the corresponding structure.
1621
1622 if (FormatTok->is(tok::kw_default)) {
1623 nextToken(); // export default ..., fall through after eating 'default'.
1624 return;
1625 }
1626
Daniel Jasper354aa512015-02-19 16:07:32 +00001627 if (FormatTok->is(tok::l_brace)) {
1628 FormatTok->BlockKind = BK_Block;
1629 parseBracedList();
1630 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001631
Daniel Jasper354aa512015-02-19 16:07:32 +00001632 while (!eof() && FormatTok->isNot(tok::semi) &&
1633 FormatTok->isNot(tok::l_brace)) {
1634 nextToken();
1635 }
1636}
1637
Daniel Jasper3b203a62013-09-05 16:05:56 +00001638LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1639 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001640 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1641 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1642 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1643 E = Line.Tokens.end();
1644 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001645 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001646 }
1647 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1648 E = Line.Tokens.end();
1649 I != E; ++I) {
1650 const UnwrappedLineNode &Node = *I;
1651 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1652 I = Node.Children.begin(),
1653 E = Node.Children.end();
1654 I != E; ++I) {
1655 printDebugInfo(*I, "\nChild: ");
1656 }
1657 }
1658 llvm::dbgs() << "\n";
1659}
1660
Daniel Jasperf7935112012-12-03 18:12:45 +00001661void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001662 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001663 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001664 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001665 if (CurrentLines == &Lines)
1666 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001667 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001668 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001669 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001670 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001671 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001672 I = PreprocessorDirectives.begin(),
1673 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001674 I != E; ++I) {
1675 CurrentLines->push_back(*I);
1676 }
1677 PreprocessorDirectives.clear();
1678 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001679}
1680
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001681bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001682
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001683bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001684 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1685 FormatTok.NewlinesBefore > 0;
1686}
1687
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001688void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1689 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001690 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001691 I = CommentsBeforeNextToken.begin(),
1692 E = CommentsBeforeNextToken.end();
1693 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001694 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001695 addUnwrappedLine();
1696 }
1697 pushToken(*I);
1698 }
1699 if (NewlineBeforeNext && JustComments) {
1700 addUnwrappedLine();
1701 }
1702 CommentsBeforeNextToken.clear();
1703}
1704
Daniel Jasperf7935112012-12-03 18:12:45 +00001705void UnwrappedLineParser::nextToken() {
1706 if (eof())
1707 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001708 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001709 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001710 readToken();
1711}
1712
1713void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001714 bool CommentsInCurrentLine = true;
1715 do {
1716 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001717 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001718 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1719 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001720 // If there is an unfinished unwrapped line, we flush the preprocessor
1721 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001722 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001723 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001724 // Comments stored before the preprocessor directive need to be output
1725 // before the preprocessor directive, at the same level as the
1726 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001727 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001728 parsePPDirective();
1729 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001730 while (FormatTok->Type == TT_ConflictStart ||
1731 FormatTok->Type == TT_ConflictEnd ||
1732 FormatTok->Type == TT_ConflictAlternative) {
1733 if (FormatTok->Type == TT_ConflictStart) {
1734 conditionalCompilationStart(/*Unreachable=*/false);
1735 } else if (FormatTok->Type == TT_ConflictAlternative) {
1736 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001737 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001738 conditionalCompilationEnd();
1739 }
1740 FormatTok = Tokens->getNextToken();
1741 FormatTok->MustBreakBefore = true;
1742 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001743
1744 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1745 !Line->InPPDirective) {
1746 continue;
1747 }
1748
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001749 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001750 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001751 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001752 CommentsInCurrentLine = false;
1753 }
1754 if (CommentsInCurrentLine) {
1755 pushToken(FormatTok);
1756 } else {
1757 CommentsBeforeNextToken.push_back(FormatTok);
1758 }
1759 } while (!eof());
1760}
1761
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001762void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001763 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001764 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001765 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001766 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001767 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001768}
1769
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001770} // end namespace format
1771} // end namespace clang