blob: cea6e8a2016ddfdca75587a2b4dcc2cf503e7d8f [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;
Daniel Jasper6acf5132015-03-12 14:44:29 +0000871 case tok::kw_new:
872 parseNew();
873 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000874 default:
875 nextToken();
876 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000877 }
878 } while (!eof());
879}
880
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000881bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000882 // FIXME: This is a dirty way to access the previous token. Find a better
883 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000884 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000885 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
886 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000887 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000888 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000889 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000890 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000891 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000892 assert(FormatTok->is(tok::l_square));
893 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000894 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000895 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000896
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000897 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000898 if (FormatTok->isSimpleTypeSpecifier()) {
899 nextToken();
900 continue;
901 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000902 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000903 case tok::l_brace:
904 break;
905 case tok::l_paren:
906 parseParens();
907 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000908 case tok::amp:
909 case tok::star:
910 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000911 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000912 case tok::less:
913 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000914 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000915 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000916 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000917 nextToken();
918 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000919 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000920 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000921 nextToken();
922 break;
923 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000924 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000925 }
926 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000927 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000928 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000929 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000930}
931
932bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
933 nextToken();
934 if (FormatTok->is(tok::equal)) {
935 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000936 if (FormatTok->is(tok::r_square)) {
937 nextToken();
938 return true;
939 }
940 if (FormatTok->isNot(tok::comma))
941 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000942 nextToken();
943 } else if (FormatTok->is(tok::amp)) {
944 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000945 if (FormatTok->is(tok::r_square)) {
946 nextToken();
947 return true;
948 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000949 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
950 return false;
951 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000952 if (FormatTok->is(tok::comma))
953 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000954 } else if (FormatTok->is(tok::r_square)) {
955 nextToken();
956 return true;
957 }
958 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000959 if (FormatTok->is(tok::amp))
960 nextToken();
961 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
962 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000963 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000964 if (FormatTok->is(tok::ellipsis))
965 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000966 if (FormatTok->is(tok::comma)) {
967 nextToken();
968 } else if (FormatTok->is(tok::r_square)) {
969 nextToken();
970 return true;
971 } else {
972 return false;
973 }
974 } while (!eof());
975 return false;
976}
977
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000978void UnwrappedLineParser::tryToParseJSFunction() {
979 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000980
981 // Consume function name.
982 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +0000983 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000984
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000985 if (FormatTok->isNot(tok::l_paren))
986 return;
987 nextToken();
988 while (FormatTok->isNot(tok::l_brace)) {
989 // Err on the side of caution in order to avoid consuming the full file in
990 // case of incomplete code.
991 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
992 tok::comment))
993 return;
994 nextToken();
995 }
996 parseChildBlock();
997}
998
Manuel Klimekab419912013-05-23 09:41:43 +0000999bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001000 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +00001001 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001002 assert(FormatTok->BlockKind != BK_Unknown);
1003 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001004 return false;
1005 parseBracedList();
1006 return true;
1007}
1008
Daniel Jasper015ed022013-09-13 09:20:45 +00001009bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1010 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001011 nextToken();
1012
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001013 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1014 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001015 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001016 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001017 FormatTok->is(Keywords.kw_function)) {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001018 tryToParseJSFunction();
1019 continue;
1020 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001021 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001022 case tok::caret:
1023 nextToken();
1024 if (FormatTok->is(tok::l_brace)) {
1025 parseChildBlock();
1026 }
1027 break;
1028 case tok::l_square:
1029 tryToParseLambda();
1030 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001031 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001032 // Assume there are no blocks inside a braced init list apart
1033 // from the ones we explicitly parse out (like lambdas).
1034 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001035 parseBracedList();
1036 break;
1037 case tok::r_brace:
1038 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001039 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001040 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001041 HasError = true;
1042 if (!ContinueOnSemicolons)
1043 return !HasError;
1044 nextToken();
1045 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001046 case tok::comma:
1047 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001048 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001049 default:
1050 nextToken();
1051 break;
1052 }
1053 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001054 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001055}
1056
Daniel Jasperf7935112012-12-03 18:12:45 +00001057void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001058 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001059 nextToken();
1060 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001061 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001062 case tok::l_paren:
1063 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001064 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1065 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001066 break;
1067 case tok::r_paren:
1068 nextToken();
1069 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001070 case tok::r_brace:
1071 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1072 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001073 case tok::l_square:
1074 tryToParseLambda();
1075 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001076 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001077 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001078 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001079 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001080 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001081 case tok::at:
1082 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001083 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001084 parseBracedList();
1085 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001086 case tok::identifier:
1087 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001088 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001089 tryToParseJSFunction();
1090 else
1091 nextToken();
1092 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001093 default:
1094 nextToken();
1095 break;
1096 }
1097 } while (!eof());
1098}
1099
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001100void UnwrappedLineParser::parseSquare() {
1101 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1102 if (tryToParseLambda())
1103 return;
1104 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001105 switch (FormatTok->Tok.getKind()) {
1106 case tok::l_paren:
1107 parseParens();
1108 break;
1109 case tok::r_square:
1110 nextToken();
1111 return;
1112 case tok::r_brace:
1113 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1114 return;
1115 case tok::l_square:
1116 parseSquare();
1117 break;
1118 case tok::l_brace: {
1119 if (!tryToParseBracedList()) {
1120 parseChildBlock();
1121 }
1122 break;
1123 }
1124 case tok::at:
1125 nextToken();
1126 if (FormatTok->Tok.is(tok::l_brace))
1127 parseBracedList();
1128 break;
1129 default:
1130 nextToken();
1131 break;
1132 }
1133 } while (!eof());
1134}
1135
Daniel Jasperf7935112012-12-03 18:12:45 +00001136void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001137 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001138 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001139 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001140 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001141 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001142 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001143 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001144 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001145 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1146 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001147 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001148 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001149 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001150 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001151 } else {
1152 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001153 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001154 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001155 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001156 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001157 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001158 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1159 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001160 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001161 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001162 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001163 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001164 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001165 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001166 parseIfThenElse();
1167 } else {
1168 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001169 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001170 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001171 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001172 }
1173 } else if (NeedsUnwrappedLine) {
1174 addUnwrappedLine();
1175 }
1176}
1177
Daniel Jasper04a71a42014-05-08 11:58:24 +00001178void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001179 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001180 nextToken();
1181 bool NeedsUnwrappedLine = false;
1182 if (FormatTok->is(tok::colon)) {
1183 // We are in a function try block, what comes is an initializer list.
1184 nextToken();
1185 while (FormatTok->is(tok::identifier)) {
1186 nextToken();
1187 if (FormatTok->is(tok::l_paren))
1188 parseParens();
1189 else
1190 StructuralError = true;
1191 if (FormatTok->is(tok::comma))
1192 nextToken();
1193 }
1194 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001195 // Parse try with resource.
1196 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1197 parseParens();
1198 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001199 if (FormatTok->is(tok::l_brace)) {
1200 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1201 parseBlock(/*MustBeDeclaration=*/false);
1202 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1203 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1204 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1205 addUnwrappedLine();
1206 } else {
1207 NeedsUnwrappedLine = true;
1208 }
1209 } else if (!FormatTok->is(tok::kw_catch)) {
1210 // The C++ standard requires a compound-statement after a try.
1211 // If there's none, we try to assume there's a structuralElement
1212 // and try to continue.
1213 StructuralError = true;
1214 addUnwrappedLine();
1215 ++Line->Level;
1216 parseStructuralElement();
1217 --Line->Level;
1218 }
Nico Weber33381f52015-02-07 01:57:32 +00001219 while (1) {
1220 if (FormatTok->is(tok::at))
1221 nextToken();
1222 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1223 tok::kw___finally) ||
1224 ((Style.Language == FormatStyle::LK_Java ||
1225 Style.Language == FormatStyle::LK_JavaScript) &&
1226 FormatTok->is(Keywords.kw_finally)) ||
1227 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1228 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1229 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001230 nextToken();
1231 while (FormatTok->isNot(tok::l_brace)) {
1232 if (FormatTok->is(tok::l_paren)) {
1233 parseParens();
1234 continue;
1235 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001236 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001237 return;
1238 nextToken();
1239 }
1240 NeedsUnwrappedLine = false;
1241 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1242 parseBlock(/*MustBeDeclaration=*/false);
1243 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1244 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1245 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1246 addUnwrappedLine();
1247 } else {
1248 NeedsUnwrappedLine = true;
1249 }
1250 }
1251 if (NeedsUnwrappedLine) {
1252 addUnwrappedLine();
1253 }
1254}
1255
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001256void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001257 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001258
1259 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001260 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001261 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001262 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001263 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001264 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001265 addUnwrappedLine();
1266
Daniel Jasper65ee3472013-07-31 23:16:02 +00001267 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1268 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1269 DeclarationScopeStack.size() > 1);
1270 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001271 // Munch the semicolon after a namespace. This is more common than one would
1272 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001273 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001274 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001275 addUnwrappedLine();
1276 }
1277 // FIXME: Add error handling.
1278}
1279
Daniel Jasper6acf5132015-03-12 14:44:29 +00001280void UnwrappedLineParser::parseNew() {
1281 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1282 nextToken();
1283 if (Style.Language != FormatStyle::LK_Java)
1284 return;
1285
1286 // In Java, we can parse everything up to the parens, which aren't optional.
1287 do {
1288 // There should not be a ;, { or } before the new's open paren.
1289 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1290 return;
1291
1292 // Consume the parens.
1293 if (FormatTok->is(tok::l_paren)) {
1294 parseParens();
1295
1296 // If there is a class body of an anonymous class, consume that as child.
1297 if (FormatTok->is(tok::l_brace))
1298 parseChildBlock();
1299 return;
1300 }
1301 nextToken();
1302 } while (!eof());
1303}
1304
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001305void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001306 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1307 FormatTok->IsForEachMacro) &&
1308 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001309 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001310 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001311 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001312 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001313 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001314 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001315 addUnwrappedLine();
1316 } else {
1317 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001318 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001319 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001320 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001321 }
1322}
1323
Daniel Jasperf7935112012-12-03 18:12:45 +00001324void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001325 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001326 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001327 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001328 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001329 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001330 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1331 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001332 } else {
1333 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001334 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001335 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001336 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001337 }
1338
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001339 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001340 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001341 addUnwrappedLine();
1342 return;
1343 }
1344
Daniel Jasperf7935112012-12-03 18:12:45 +00001345 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001346 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001347}
1348
1349void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001350 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001351 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001352 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001353 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001354 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001355 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001356 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001357 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001358 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1359 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1360 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001361 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001362 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001363 parseStructuralElement();
1364 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001365 addUnwrappedLine();
1366 } else {
1367 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001368 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001369 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001370}
1371
1372void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001373 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001374 // FIXME: fix handling of complex expressions here.
1375 do {
1376 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001377 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001378 parseLabel();
1379}
1380
1381void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001382 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001383 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001384 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001385 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001386 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001387 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001388 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001389 addUnwrappedLine();
1390 } else {
1391 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001392 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001393 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001394 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001395 }
1396}
1397
1398void UnwrappedLineParser::parseAccessSpecifier() {
1399 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001400 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001401 if (FormatTok->is(tok::identifier) &&
1402 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001403 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001404 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001405 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001406 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001407 addUnwrappedLine();
1408}
1409
1410void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001411 // Won't be 'enum' for NS_ENUMs.
1412 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001413 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001414
Daniel Jasper2b41a822013-08-20 12:42:50 +00001415 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001416 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1417 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001418 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001419 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1420 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001421 nextToken();
1422 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001423 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001424 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001425 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001426 nextToken();
1427 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001428
1429 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001430 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001431 return;
1432 FormatTok->BlockKind = BK_Block;
1433
1434 if (Style.Language == FormatStyle::LK_Java) {
1435 // Java enums are different.
1436 parseJavaEnumBody();
1437 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001438 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001439
1440 // Parse enum body.
1441 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1442 if (HasError) {
1443 if (FormatTok->is(tok::semi))
1444 nextToken();
1445 addUnwrappedLine();
1446 }
1447
Manuel Klimek2cec0192013-01-21 19:17:52 +00001448 // We fall through to parsing a structural element afterwards, so that in
1449 // enum A {} n, m;
1450 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001451}
1452
1453void UnwrappedLineParser::parseJavaEnumBody() {
1454 // Determine whether the enum is simple, i.e. does not have a semicolon or
1455 // constants with class bodies. Simple enums can be formatted like braced
1456 // lists, contracted to a single line, etc.
1457 unsigned StoredPosition = Tokens->getPosition();
1458 bool IsSimple = true;
1459 FormatToken *Tok = Tokens->getNextToken();
1460 while (Tok) {
1461 if (Tok->is(tok::r_brace))
1462 break;
1463 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1464 IsSimple = false;
1465 break;
1466 }
1467 // FIXME: This will also mark enums with braces in the arguments to enum
1468 // constants as "not simple". This is probably fine in practice, though.
1469 Tok = Tokens->getNextToken();
1470 }
1471 FormatTok = Tokens->setPosition(StoredPosition);
1472
1473 if (IsSimple) {
1474 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001475 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001476 return;
1477 }
1478
1479 // Parse the body of a more complex enum.
1480 // First add a line for everything up to the "{".
1481 nextToken();
1482 addUnwrappedLine();
1483 ++Line->Level;
1484
1485 // Parse the enum constants.
1486 while (FormatTok) {
1487 if (FormatTok->is(tok::l_brace)) {
1488 // Parse the constant's class body.
1489 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1490 /*MunchSemi=*/false);
1491 } else if (FormatTok->is(tok::l_paren)) {
1492 parseParens();
1493 } else if (FormatTok->is(tok::comma)) {
1494 nextToken();
1495 addUnwrappedLine();
1496 } else if (FormatTok->is(tok::semi)) {
1497 nextToken();
1498 addUnwrappedLine();
1499 break;
1500 } else if (FormatTok->is(tok::r_brace)) {
1501 addUnwrappedLine();
1502 break;
1503 } else {
1504 nextToken();
1505 }
1506 }
1507
1508 // Parse the class body after the enum's ";" if any.
1509 parseLevel(/*HasOpeningBrace=*/true);
1510 nextToken();
1511 --Line->Level;
1512 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001513}
1514
Manuel Klimeke01bab52013-01-15 13:38:33 +00001515void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001516 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001517 nextToken();
Manuel Klimek45bf56c2014-07-31 07:19:30 +00001518 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1519 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001520 nextToken();
1521 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001522 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001523 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001524 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001525 // The actual identifier can be a nested name specifier, and in macros
1526 // it is often token-pasted.
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001527 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1528 FormatTok->is(tok::hashhash) ||
1529 (Style.Language == FormatStyle::LK_Java &&
1530 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001531 nextToken();
1532
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001533 // Note that parsing away template declarations here leads to incorrectly
1534 // accepting function declarations as record declarations.
1535 // In general, we cannot solve this problem. Consider:
1536 // class A<int> B() {}
1537 // which can be a function definition or a class definition when B() is a
1538 // macro. If we find enough real-world cases where this is a problem, we
1539 // can parse for the 'template' keyword in the beginning of the statement,
1540 // and thus rule out the record production in case there is no template
1541 // (this would still leave us with an ambiguity between template function
1542 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001543 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1544 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1545 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001546 return;
1547 nextToken();
1548 }
1549 }
1550 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001551 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001552 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001553 addUnwrappedLine();
1554
Daniel Jasper240dfda2014-03-31 14:23:49 +00001555 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001556 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001557 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001558 // We fall through to parsing a structural element afterwards, so
1559 // class A {} n, m;
1560 // will end up in one unwrapped line.
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001561 // This does not apply for Java.
Daniel Jasper6fa9ec72015-02-19 16:03:16 +00001562 if (Style.Language == FormatStyle::LK_Java ||
1563 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001564 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001565}
1566
Nico Weber8696a8d2013-01-09 21:15:03 +00001567void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001568 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001569 do
1570 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001571 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001572 nextToken(); // Skip '>'.
1573}
1574
1575void UnwrappedLineParser::parseObjCUntilAtEnd() {
1576 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001577 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001578 nextToken();
1579 addUnwrappedLine();
1580 break;
1581 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001582 if (FormatTok->is(tok::l_brace)) {
1583 parseBlock(/*MustBeDeclaration=*/false);
1584 // In ObjC interfaces, nothing should be following the "}".
1585 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001586 } else if (FormatTok->is(tok::r_brace)) {
1587 // Ignore stray "}". parseStructuralElement doesn't consume them.
1588 nextToken();
1589 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001590 } else {
1591 parseStructuralElement();
1592 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001593 } while (!eof());
1594}
1595
Nico Weber2ce0ac52013-01-09 23:25:37 +00001596void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001597 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001598 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001599
1600 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001601 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001602 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001603 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001604 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001605 // Skip category, if present.
1606 parseParens();
1607
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001608 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001609 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001610
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001611 if (FormatTok->Tok.is(tok::l_brace)) {
1612 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1613 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1614 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001615 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001616 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001617
1618 // With instance variables, this puts '}' on its own line. Without instance
1619 // variables, this ends the @interface line.
1620 addUnwrappedLine();
1621
Nico Weber8696a8d2013-01-09 21:15:03 +00001622 parseObjCUntilAtEnd();
1623}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001624
Nico Weber8696a8d2013-01-09 21:15:03 +00001625void UnwrappedLineParser::parseObjCProtocol() {
1626 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001627 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001628
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001629 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001630 parseObjCProtocolList();
1631
1632 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001633 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001634 nextToken();
1635 return addUnwrappedLine();
1636 }
1637
1638 addUnwrappedLine();
1639 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001640}
1641
Daniel Jasperfca735c2015-02-19 16:14:18 +00001642void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1643 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00001644 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00001645
1646 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_function,
1647 Keywords.kw_var))
1648 return; // Fall through to parsing the corresponding structure.
1649
1650 if (FormatTok->is(tok::kw_default)) {
1651 nextToken(); // export default ..., fall through after eating 'default'.
1652 return;
1653 }
1654
Daniel Jasper354aa512015-02-19 16:07:32 +00001655 if (FormatTok->is(tok::l_brace)) {
1656 FormatTok->BlockKind = BK_Block;
1657 parseBracedList();
1658 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001659
Daniel Jasper354aa512015-02-19 16:07:32 +00001660 while (!eof() && FormatTok->isNot(tok::semi) &&
1661 FormatTok->isNot(tok::l_brace)) {
1662 nextToken();
1663 }
1664}
1665
Daniel Jasper3b203a62013-09-05 16:05:56 +00001666LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1667 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001668 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1669 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1670 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1671 E = Line.Tokens.end();
1672 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001673 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001674 }
1675 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1676 E = Line.Tokens.end();
1677 I != E; ++I) {
1678 const UnwrappedLineNode &Node = *I;
1679 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1680 I = Node.Children.begin(),
1681 E = Node.Children.end();
1682 I != E; ++I) {
1683 printDebugInfo(*I, "\nChild: ");
1684 }
1685 }
1686 llvm::dbgs() << "\n";
1687}
1688
Daniel Jasperf7935112012-12-03 18:12:45 +00001689void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001690 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001691 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001692 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001693 if (CurrentLines == &Lines)
1694 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001695 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001696 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001697 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001698 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001699 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001700 I = PreprocessorDirectives.begin(),
1701 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001702 I != E; ++I) {
1703 CurrentLines->push_back(*I);
1704 }
1705 PreprocessorDirectives.clear();
1706 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001707}
1708
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001709bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001710
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001711bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001712 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1713 FormatTok.NewlinesBefore > 0;
1714}
1715
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001716void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1717 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001718 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001719 I = CommentsBeforeNextToken.begin(),
1720 E = CommentsBeforeNextToken.end();
1721 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001722 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001723 addUnwrappedLine();
1724 }
1725 pushToken(*I);
1726 }
1727 if (NewlineBeforeNext && JustComments) {
1728 addUnwrappedLine();
1729 }
1730 CommentsBeforeNextToken.clear();
1731}
1732
Daniel Jasperf7935112012-12-03 18:12:45 +00001733void UnwrappedLineParser::nextToken() {
1734 if (eof())
1735 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001736 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001737 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001738 readToken();
1739}
1740
1741void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001742 bool CommentsInCurrentLine = true;
1743 do {
1744 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001745 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001746 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1747 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001748 // If there is an unfinished unwrapped line, we flush the preprocessor
1749 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001750 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001751 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001752 // Comments stored before the preprocessor directive need to be output
1753 // before the preprocessor directive, at the same level as the
1754 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001755 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001756 parsePPDirective();
1757 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001758 while (FormatTok->Type == TT_ConflictStart ||
1759 FormatTok->Type == TT_ConflictEnd ||
1760 FormatTok->Type == TT_ConflictAlternative) {
1761 if (FormatTok->Type == TT_ConflictStart) {
1762 conditionalCompilationStart(/*Unreachable=*/false);
1763 } else if (FormatTok->Type == TT_ConflictAlternative) {
1764 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001765 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001766 conditionalCompilationEnd();
1767 }
1768 FormatTok = Tokens->getNextToken();
1769 FormatTok->MustBreakBefore = true;
1770 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001771
1772 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1773 !Line->InPPDirective) {
1774 continue;
1775 }
1776
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001777 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001778 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001779 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001780 CommentsInCurrentLine = false;
1781 }
1782 if (CommentsInCurrentLine) {
1783 pushToken(FormatTok);
1784 } else {
1785 CommentsBeforeNextToken.push_back(FormatTok);
1786 }
1787 } while (!eof());
1788}
1789
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001790void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001791 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001792 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001793 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001794 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001795 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001796}
1797
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001798} // end namespace format
1799} // end namespace clang