blob: 23b9d550fb1d28cead1cd71a4366e3b974aad965 [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file contains the implementation of the UnwrappedLineParser,
12/// which turns a stream of tokens into UnwrappedLines.
13///
Daniel Jasperf7935112012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Chandler Carruth4b417452013-01-19 08:09:44 +000016#include "UnwrappedLineParser.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000017#include "llvm/Support/Debug.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000018
Chandler Carruth10346662014-04-22 03:17:02 +000019#define DEBUG_TYPE "format-parser"
20
Daniel Jasperf7935112012-12-03 18:12:45 +000021namespace clang {
22namespace format {
23
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000024class FormatTokenSource {
25public:
26 virtual ~FormatTokenSource() {}
27 virtual FormatToken *getNextToken() = 0;
28
29 virtual unsigned getPosition() = 0;
30 virtual FormatToken *setPosition(unsigned Position) = 0;
31};
32
Craig Topper69665e12013-07-01 04:21:54 +000033namespace {
34
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000035class ScopedDeclarationState {
36public:
37 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
38 bool MustBeDeclaration)
39 : Line(Line), Stack(Stack) {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000040 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek39080572013-01-23 11:03:04 +000041 Stack.push_back(MustBeDeclaration);
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000042 }
43 ~ScopedDeclarationState() {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000044 Stack.pop_back();
Manuel Klimekc1237a82013-01-23 14:08:21 +000045 if (!Stack.empty())
46 Line.MustBeDeclaration = Stack.back();
47 else
48 Line.MustBeDeclaration = true;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000049 }
Daniel Jasper393564f2013-05-31 14:56:29 +000050
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000051private:
52 UnwrappedLine &Line;
53 std::vector<bool> &Stack;
54};
55
Manuel Klimek1abf7892013-01-04 23:34:14 +000056class ScopedMacroState : public FormatTokenSource {
57public:
58 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000059 FormatToken *&ResetToken, bool &StructuralError)
Manuel Klimek1abf7892013-01-04 23:34:14 +000060 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000061 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
62 StructuralError(StructuralError),
Craig Topper2145bc02014-05-09 08:15:10 +000063 PreviousStructuralError(StructuralError), Token(nullptr) {
Manuel Klimek1abf7892013-01-04 23:34:14 +000064 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000065 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000066 Line.InPPDirective = true;
67 }
68
69 ~ScopedMacroState() {
70 TokenSource = PreviousTokenSource;
71 ResetToken = Token;
72 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000073 Line.Level = PreviousLineLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +000074 StructuralError = PreviousStructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +000075 }
76
Craig Topperfb6b25b2014-03-15 04:29:04 +000077 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +000078 // The \c UnwrappedLineParser guards against this by never calling
79 // \c getNextToken() after it has encountered the first eof token.
80 assert(!eof());
Manuel Klimek1abf7892013-01-04 23:34:14 +000081 Token = PreviousTokenSource->getNextToken();
82 if (eof())
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000083 return getFakeEOF();
Manuel Klimek1abf7892013-01-04 23:34:14 +000084 return Token;
85 }
86
Craig Topperfb6b25b2014-03-15 04:29:04 +000087 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +000088
Craig Topperfb6b25b2014-03-15 04:29:04 +000089 FormatToken *setPosition(unsigned Position) override {
Manuel Klimekab419912013-05-23 09:41:43 +000090 Token = PreviousTokenSource->setPosition(Position);
91 return Token;
92 }
93
Manuel Klimek1abf7892013-01-04 23:34:14 +000094private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000095 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimek1abf7892013-01-04 23:34:14 +000096
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000097 FormatToken *getFakeEOF() {
98 static bool EOFInitialized = false;
99 static FormatToken FormatTok;
100 if (!EOFInitialized) {
101 FormatTok.Tok.startToken();
102 FormatTok.Tok.setKind(tok::eof);
103 EOFInitialized = true;
104 }
105 return &FormatTok;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000106 }
107
108 UnwrappedLine &Line;
109 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000110 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000111 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000112 FormatTokenSource *PreviousTokenSource;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000113 bool &StructuralError;
114 bool PreviousStructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000115
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000116 FormatToken *Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000117};
118
Craig Topper69665e12013-07-01 04:21:54 +0000119} // end anonymous namespace
120
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000121class ScopedLineState {
122public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000123 ScopedLineState(UnwrappedLineParser &Parser,
124 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000125 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000126 if (SwitchToPreprocessorLines)
127 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000128 else if (!Parser.Line->Tokens.empty())
129 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000130 PreBlockLine = std::move(Parser.Line);
131 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000132 Parser.Line->Level = PreBlockLine->Level;
133 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000134 }
135
136 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000137 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000138 Parser.addUnwrappedLine();
139 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000140 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000141 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000142 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
143 Parser.MustBreakBeforeNextToken = true;
144 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000145 }
146
147private:
148 UnwrappedLineParser &Parser;
149
David Blaikieefb6eb22014-08-09 20:02:07 +0000150 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000151 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000152};
153
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000154class CompoundStatementIndenter {
155public:
156 CompoundStatementIndenter(UnwrappedLineParser *Parser,
157 const FormatStyle &Style, unsigned &LineLevel)
158 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
159 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
160 Parser->addUnwrappedLine();
161 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
162 Parser->addUnwrappedLine();
163 ++LineLevel;
164 }
165 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000166 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000167
168private:
169 unsigned &LineLevel;
170 unsigned OldLineLevel;
171};
172
Craig Topper69665e12013-07-01 04:21:54 +0000173namespace {
174
Manuel Klimekab419912013-05-23 09:41:43 +0000175class IndexedTokenSource : public FormatTokenSource {
176public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000177 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000178 : Tokens(Tokens), Position(-1) {}
179
Craig Topperfb6b25b2014-03-15 04:29:04 +0000180 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000181 ++Position;
182 return Tokens[Position];
183 }
184
Craig Topperfb6b25b2014-03-15 04:29:04 +0000185 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000186 assert(Position >= 0);
187 return Position;
188 }
189
Craig Topperfb6b25b2014-03-15 04:29:04 +0000190 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000191 Position = P;
192 return Tokens[Position];
193 }
194
Manuel Klimek71814b42013-10-11 21:25:45 +0000195 void reset() { Position = -1; }
196
Manuel Klimekab419912013-05-23 09:41:43 +0000197private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000198 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000199 int Position;
200};
201
Craig Topper69665e12013-07-01 04:21:54 +0000202} // end anonymous namespace
203
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000204UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000205 const AdditionalKeywords &Keywords,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000206 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000207 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000208 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
209 CurrentLines(&Lines), StructuralError(false), Style(Style),
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000210 Keywords(Keywords), Tokens(nullptr), Callback(Callback),
211 AllTokens(Tokens), PPBranchLevel(-1) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000212
213void UnwrappedLineParser::reset() {
214 PPBranchLevel = -1;
215 Line.reset(new UnwrappedLine);
216 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000217 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000218 MustBreakBeforeNextToken = false;
219 PreprocessorDirectives.clear();
220 CurrentLines = &Lines;
221 DeclarationScopeStack.clear();
222 StructuralError = false;
223 PPStack.clear();
224}
Daniel Jasperf7935112012-12-03 18:12:45 +0000225
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000226bool UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000227 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimek71814b42013-10-11 21:25:45 +0000228 do {
229 DEBUG(llvm::dbgs() << "----\n");
230 reset();
231 Tokens = &TokenSource;
232 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000233
Manuel Klimek71814b42013-10-11 21:25:45 +0000234 readToken();
235 parseFile();
236 // Create line with eof token.
237 pushToken(FormatTok);
238 addUnwrappedLine();
239
240 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
241 E = Lines.end();
242 I != E; ++I) {
243 Callback.consumeUnwrappedLine(*I);
244 }
245 Callback.finishRun();
246 Lines.clear();
247 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000248 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000249 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
250 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
251 }
252 if (!PPLevelBranchIndex.empty()) {
253 ++PPLevelBranchIndex.back();
254 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
255 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
256 }
257 } while (!PPLevelBranchIndex.empty());
258
Manuel Klimek1a18c402013-04-12 14:13:36 +0000259 return StructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000260}
261
Manuel Klimek1a18c402013-04-12 14:13:36 +0000262void UnwrappedLineParser::parseFile() {
Daniel Jasperdd9276e2013-03-22 16:55:40 +0000263 ScopedDeclarationState DeclarationState(
264 *Line, DeclarationScopeStack,
265 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber9096fc02013-06-26 00:30:14 +0000266 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000267 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000268 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000269 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000270}
271
Manuel Klimek1a18c402013-04-12 14:13:36 +0000272void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000273 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000274 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000275 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000276 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000277 nextToken();
278 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000279 break;
280 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000281 // FIXME: Add parameter whether this can happen - if this happens, we must
282 // be in a non-declaration context.
Nico Weber9096fc02013-06-26 00:30:14 +0000283 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000284 addUnwrappedLine();
285 break;
286 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000287 if (HasOpeningBrace)
288 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000289 StructuralError = true;
290 nextToken();
291 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000292 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000293 case tok::kw_default:
294 case tok::kw_case:
Daniel Jasper72407622013-09-02 08:26:29 +0000295 if (!SwitchLabelEncountered &&
296 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
297 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000298 SwitchLabelEncountered = true;
299 parseStructuralElement();
300 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000301 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000302 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000303 break;
304 }
305 } while (!eof());
306}
307
Manuel Klimekab419912013-05-23 09:41:43 +0000308void UnwrappedLineParser::calculateBraceTypes() {
309 // We'll parse forward through the tokens until we hit
310 // a closing brace or eof - note that getNextToken() will
311 // parse macros, so this will magically work inside macro
312 // definitions, too.
313 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000314 FormatToken *Tok = FormatTok;
Manuel Klimekab419912013-05-23 09:41:43 +0000315 // Keep a stack of positions of lbrace tokens. We will
316 // update information about whether an lbrace starts a
317 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000318 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000319 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000320 do {
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000321 // Get next none-comment token.
322 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000323 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000324 do {
325 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000326 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000327 } while (NextTok->is(tok::comment));
328
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000329 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000330 case tok::l_brace:
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000331 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000332 break;
333 case tok::r_brace:
334 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000335 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Daniel Jasper220c0d12014-04-10 07:27:12 +0000336 bool ProbablyBracedList = false;
337 if (Style.Language == FormatStyle::LK_Proto) {
338 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
339 } else {
Daniel Jasper91b032a2014-05-22 12:46:38 +0000340 // Using OriginalColumn to distinguish between ObjC methods and
341 // binary operators is a bit hacky.
342 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
343 NextTok->OriginalColumn == 0;
344
Daniel Jasper220c0d12014-04-10 07:27:12 +0000345 // If there is a comma, semicolon or right paren after the closing
346 // brace, we assume this is a braced initializer list. Note that
347 // regardless how we mark inner braces here, we will overwrite the
348 // BlockKind later if we parse a braced list (where all blocks
349 // inside are by default braced lists), or when we explicitly detect
350 // blocks (for example while parsing lambdas).
351 //
352 // We exclude + and - as they can be ObjC visibility modifiers.
353 ProbablyBracedList =
354 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
Daniel Jasper438059e2014-05-22 12:11:13 +0000355 tok::r_paren, tok::r_square, tok::l_brace,
Daniel Jasper65df5aa2014-08-04 14:51:02 +0000356 tok::l_paren, tok::ellipsis) ||
Daniel Jasper91b032a2014-05-22 12:46:38 +0000357 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000358 }
359 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000360 Tok->BlockKind = BK_BracedInit;
361 LBraceStack.back()->BlockKind = BK_BracedInit;
362 } else {
363 Tok->BlockKind = BK_Block;
364 LBraceStack.back()->BlockKind = BK_Block;
365 }
Manuel Klimekab419912013-05-23 09:41:43 +0000366 }
367 LBraceStack.pop_back();
368 }
369 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000370 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000371 case tok::semi:
372 case tok::kw_if:
373 case tok::kw_while:
374 case tok::kw_for:
375 case tok::kw_switch:
376 case tok::kw_try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000377 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000378 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000379 break;
380 default:
381 break;
382 }
383 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000384 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000385 // Assume other blocks for all unclosed opening braces.
386 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000387 if (LBraceStack[i]->BlockKind == BK_Unknown)
388 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000389 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000390
Manuel Klimekab419912013-05-23 09:41:43 +0000391 FormatTok = Tokens->setPosition(StoredPosition);
392}
393
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000394void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
395 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000396 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000397 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000398 nextToken();
399
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000400 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000401
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000402 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
403 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000404 if (AddLevel)
405 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000406 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000407
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000408 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000409 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000410 StructuralError = true;
411 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000412 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000413
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000414 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000415 if (MunchSemi && FormatTok->Tok.is(tok::semi))
416 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000417 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000418}
419
Daniel Jasper4a39c842014-05-06 13:54:10 +0000420static bool IsGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000421 // FIXME: Closure-library specific stuff should not be hard-coded but be
422 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000423 if (Line.Tokens.size() < 4)
424 return false;
425 auto I = Line.Tokens.begin();
426 if (I->Tok->TokenText != "goog")
427 return false;
428 ++I;
429 if (I->Tok->isNot(tok::period))
430 return false;
431 ++I;
432 if (I->Tok->TokenText != "scope")
433 return false;
434 ++I;
435 return I->Tok->is(tok::l_paren);
436}
437
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000438static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
439 const FormatToken &InitialToken) {
440 switch (Style.BreakBeforeBraces) {
441 case FormatStyle::BS_Linux:
442 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
443 case FormatStyle::BS_Allman:
444 case FormatStyle::BS_GNU:
445 return true;
446 default:
447 return false;
448 }
449}
450
Manuel Klimek516e0542013-09-04 13:25:30 +0000451void UnwrappedLineParser::parseChildBlock() {
452 FormatTok->BlockKind = BK_Block;
453 nextToken();
454 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000455 bool GoogScope =
456 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000457 ScopedLineState LineState(*this);
458 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
459 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000460 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000461 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000462 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000463 }
464 nextToken();
465}
466
Daniel Jasperf7935112012-12-03 18:12:45 +0000467void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000468 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000469 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000470 nextToken();
471
Craig Topper2145bc02014-05-09 08:15:10 +0000472 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000473 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000474 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000475 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000476
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000477 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000478 case tok::pp_define:
479 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000480 return;
481 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000482 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000483 break;
484 case tok::pp_ifdef:
485 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000486 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000487 break;
488 case tok::pp_else:
489 parsePPElse();
490 break;
491 case tok::pp_elif:
492 parsePPElIf();
493 break;
494 case tok::pp_endif:
495 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000496 break;
497 default:
498 parsePPUnknown();
499 break;
500 }
501}
502
Manuel Klimek68b03042014-04-14 09:14:11 +0000503void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
504 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000505 PPStack.push_back(PP_Unreachable);
506 else
507 PPStack.push_back(PP_Conditional);
508}
509
Manuel Klimek68b03042014-04-14 09:14:11 +0000510void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000511 ++PPBranchLevel;
512 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
513 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
514 PPLevelBranchIndex.push_back(0);
515 PPLevelBranchCount.push_back(0);
516 }
517 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000518 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
519 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000520}
521
Manuel Klimek68b03042014-04-14 09:14:11 +0000522void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000523 if (!PPStack.empty())
524 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000525 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
526 if (!PPChainBranchIndex.empty())
527 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000528 conditionalCompilationCondition(
529 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
530 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000531}
532
Manuel Klimek68b03042014-04-14 09:14:11 +0000533void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000534 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
535 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
536 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000537 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
538 }
539 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000540 // Guard against #endif's without #if.
541 if (PPBranchLevel > 0)
542 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000543 if (!PPChainBranchIndex.empty())
544 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000545 if (!PPStack.empty())
546 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000547}
548
549void UnwrappedLineParser::parsePPIf(bool IfDef) {
550 nextToken();
551 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Daniel Jasper9d22bcc2015-01-19 10:51:05 +0000552 FormatTok->Tok.getLiteralData() != nullptr &&
Manuel Klimek68b03042014-04-14 09:14:11 +0000553 StringRef(FormatTok->Tok.getLiteralData(),
554 FormatTok->Tok.getLength()) == "0") ||
555 FormatTok->Tok.is(tok::kw_false);
556 conditionalCompilationStart(!IfDef && IsLiteralFalse);
557 parsePPUnknown();
558}
559
560void UnwrappedLineParser::parsePPElse() {
561 conditionalCompilationAlternative();
562 parsePPUnknown();
563}
564
565void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
566
567void UnwrappedLineParser::parsePPEndIf() {
568 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000569 parsePPUnknown();
570}
571
Manuel Klimek1abf7892013-01-04 23:34:14 +0000572void UnwrappedLineParser::parsePPDefine() {
573 nextToken();
574
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000575 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000576 parsePPUnknown();
577 return;
578 }
579 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000580 if (FormatTok->Tok.getKind() == tok::l_paren &&
581 FormatTok->WhitespaceRange.getBegin() ==
582 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000583 parseParens();
584 }
585 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000586 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000587
588 // Errors during a preprocessor directive can only affect the layout of the
589 // preprocessor directive, and thus we ignore them. An alternative approach
590 // would be to use the same approach we use on the file level (no
591 // re-indentation if there was a structural error) within the macro
592 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000593 parseFile();
594}
595
596void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000597 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000598 nextToken();
599 } while (!eof());
600 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000601}
602
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000603// Here we blacklist certain tokens that are not usually the first token in an
604// unwrapped line. This is used in attempt to distinguish macro calls without
605// trailing semicolons from other constructs split to several lines.
606bool tokenCanStartNewLine(clang::Token Tok) {
607 // Semicolon can be a null-statement, l_square can be a start of a macro or
608 // a C++11 attribute, but this doesn't seem to be common.
609 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
610 Tok.isNot(tok::l_square) &&
611 // Tokens that can only be used as binary operators and a part of
612 // overloaded operator names.
613 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
614 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
615 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
616 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
617 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
618 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
619 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
620 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
621 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
622 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
623 Tok.isNot(tok::lesslessequal) &&
624 // Colon is used in labels, base class lists, initializer lists,
625 // range-based for loops, ternary operator, but should never be the
626 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000627 Tok.isNot(tok::colon) &&
628 // 'noexcept' is a trailing annotation.
629 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000630}
631
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000632void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000633 assert(!FormatTok->Tok.is(tok::l_brace));
634 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000635 case tok::at:
636 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000637 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000638 parseBracedList();
639 break;
640 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000641 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000642 case tok::objc_public:
643 case tok::objc_protected:
644 case tok::objc_package:
645 case tok::objc_private:
646 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000647 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000648 case tok::objc_implementation:
649 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000650 case tok::objc_protocol:
651 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000652 case tok::objc_end:
653 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000654 case tok::objc_optional:
655 case tok::objc_required:
656 nextToken();
657 addUnwrappedLine();
658 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000659 default:
660 break;
661 }
662 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000663 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000664 nextToken();
665 if (FormatTok->is(tok::l_brace)) {
Daniel Jasper2337f282015-01-12 10:14:56 +0000666 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000667 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000668 if (FormatTok->is(tok::r_brace)) {
669 nextToken();
670 break;
671 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000672 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000673 nextToken();
674 }
675 }
676 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000677 case tok::kw_namespace:
678 parseNamespace();
679 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000680 case tok::kw_inline:
681 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000682 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000683 parseNamespace();
684 return;
685 }
686 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000687 case tok::kw_public:
688 case tok::kw_protected:
689 case tok::kw_private:
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000690 if (Style.Language == FormatStyle::LK_Java)
691 nextToken();
692 else
693 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000694 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000695 case tok::kw_if:
696 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000697 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000698 case tok::kw_for:
699 case tok::kw_while:
700 parseForOrWhileLoop();
701 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000702 case tok::kw_do:
703 parseDoWhile();
704 return;
705 case tok::kw_switch:
706 parseSwitch();
707 return;
708 case tok::kw_default:
709 nextToken();
710 parseLabel();
711 return;
712 case tok::kw_case:
713 parseCaseLabel();
714 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000715 case tok::kw_try:
716 parseTryCatch();
717 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000718 case tok::kw_extern:
719 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000720 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000721 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000722 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000723 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000724 addUnwrappedLine();
725 return;
726 }
727 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000728 break;
729 case tok::identifier:
730 if (FormatTok->IsForEachMacro) {
731 parseForOrWhileLoop();
732 return;
733 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000734 // In all other cases, parse the declaration.
735 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000736 default:
737 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000738 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000739 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000740 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000741 case tok::at:
742 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000743 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000744 parseBracedList();
745 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000746 case tok::kw_enum:
747 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000748 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000749 case tok::kw_typedef:
750 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000751 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
752 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000753 parseEnum();
754 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000755 case tok::kw_struct:
756 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000757 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000758 parseRecord();
759 // A record declaration or definition is always the start of a structural
760 // element.
761 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000762 case tok::period:
763 nextToken();
764 // In Java, classes have an implicit static member "class".
765 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
766 FormatTok->is(tok::kw_class))
767 nextToken();
768 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000769 case tok::semi:
770 nextToken();
771 addUnwrappedLine();
772 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000773 case tok::r_brace:
774 addUnwrappedLine();
775 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000776 case tok::l_paren:
777 parseParens();
778 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000779 case tok::caret:
780 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000781 if (FormatTok->Tok.isAnyIdentifier() ||
782 FormatTok->isSimpleTypeSpecifier())
783 nextToken();
784 if (FormatTok->is(tok::l_paren))
785 parseParens();
786 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000787 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000788 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000789 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000790 if (!tryToParseBracedList()) {
791 // A block outside of parentheses must be the last part of a
792 // structural element.
793 // FIXME: Figure out cases where this is not true, and add projections
794 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000795 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000796 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000797 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000798 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000799 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000800 return;
801 }
802 // Otherwise this was a braced init list, and the structural
803 // element continues.
804 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000805 case tok::kw_try:
806 // We arrive here when parsing function-try blocks.
807 parseTryCatch();
808 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000809 case tok::identifier: {
810 StringRef Text = FormatTok->TokenText;
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000811 // Parse function literal unless 'function' is the first token in a line
812 // in which case this should be treated as a free-standing function.
813 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
814 Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000815 tryToParseJSFunction();
816 break;
817 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000818 nextToken();
Alexander Kornienkode644272013-04-08 22:16:06 +0000819 if (Line->Tokens.size() == 1) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000820 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000821 parseLabel();
822 return;
823 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000824 // Recognize function-like macro usages without trailing semicolon as
825 // well as free-standing macrose like Q_OBJECT.
826 bool FunctionLike = FormatTok->is(tok::l_paren);
827 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000828 parseParens();
Daniel Jasper680b09b2014-11-05 10:48:04 +0000829 if (FormatTok->NewlinesBefore > 0 &&
830 (Text.size() >= 5 || FunctionLike) &&
831 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000832 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000833 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000834 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000835 }
836 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000837 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000838 case tok::equal:
839 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000840 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000841 parseBracedList();
842 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000843 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000844 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000845 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000846 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000847 default:
848 nextToken();
849 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000850 }
851 } while (!eof());
852}
853
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000854bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000855 // FIXME: This is a dirty way to access the previous token. Find a better
856 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000857 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000858 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
859 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000860 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000861 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000862 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000863 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000864 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000865 assert(FormatTok->is(tok::l_square));
866 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000867 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000868 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000869
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000870 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000871 if (FormatTok->isSimpleTypeSpecifier()) {
872 nextToken();
873 continue;
874 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000875 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000876 case tok::l_brace:
877 break;
878 case tok::l_paren:
879 parseParens();
880 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000881 case tok::amp:
882 case tok::star:
883 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000884 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000885 case tok::less:
886 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000887 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000888 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000889 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000890 nextToken();
891 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000892 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000893 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000894 nextToken();
895 break;
896 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000897 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000898 }
899 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000900 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000901 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000902 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000903}
904
905bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
906 nextToken();
907 if (FormatTok->is(tok::equal)) {
908 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000909 if (FormatTok->is(tok::r_square)) {
910 nextToken();
911 return true;
912 }
913 if (FormatTok->isNot(tok::comma))
914 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000915 nextToken();
916 } else if (FormatTok->is(tok::amp)) {
917 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000918 if (FormatTok->is(tok::r_square)) {
919 nextToken();
920 return true;
921 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000922 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
923 return false;
924 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000925 if (FormatTok->is(tok::comma))
926 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000927 } else if (FormatTok->is(tok::r_square)) {
928 nextToken();
929 return true;
930 }
931 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000932 if (FormatTok->is(tok::amp))
933 nextToken();
934 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
935 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000936 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000937 if (FormatTok->is(tok::ellipsis))
938 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000939 if (FormatTok->is(tok::comma)) {
940 nextToken();
941 } else if (FormatTok->is(tok::r_square)) {
942 nextToken();
943 return true;
944 } else {
945 return false;
946 }
947 } while (!eof());
948 return false;
949}
950
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000951void UnwrappedLineParser::tryToParseJSFunction() {
952 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000953
954 // Consume function name.
955 if (FormatTok->is(tok::identifier))
956 nextToken();
957
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000958 if (FormatTok->isNot(tok::l_paren))
959 return;
960 nextToken();
961 while (FormatTok->isNot(tok::l_brace)) {
962 // Err on the side of caution in order to avoid consuming the full file in
963 // case of incomplete code.
964 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
965 tok::comment))
966 return;
967 nextToken();
968 }
969 parseChildBlock();
970}
971
Manuel Klimekab419912013-05-23 09:41:43 +0000972bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000973 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +0000974 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000975 assert(FormatTok->BlockKind != BK_Unknown);
976 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +0000977 return false;
978 parseBracedList();
979 return true;
980}
981
Daniel Jasper015ed022013-09-13 09:20:45 +0000982bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
983 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000984 nextToken();
985
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000986 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
987 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000988 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000989 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000990 FormatTok->is(Keywords.kw_function)) {
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000991 tryToParseJSFunction();
992 continue;
993 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000994 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +0000995 case tok::caret:
996 nextToken();
997 if (FormatTok->is(tok::l_brace)) {
998 parseChildBlock();
999 }
1000 break;
1001 case tok::l_square:
1002 tryToParseLambda();
1003 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001004 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001005 // Assume there are no blocks inside a braced init list apart
1006 // from the ones we explicitly parse out (like lambdas).
1007 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001008 parseBracedList();
1009 break;
1010 case tok::r_brace:
1011 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001012 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001013 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001014 HasError = true;
1015 if (!ContinueOnSemicolons)
1016 return !HasError;
1017 nextToken();
1018 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001019 case tok::comma:
1020 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001021 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001022 default:
1023 nextToken();
1024 break;
1025 }
1026 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001027 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001028}
1029
Daniel Jasperf7935112012-12-03 18:12:45 +00001030void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001031 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001032 nextToken();
1033 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001034 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001035 case tok::l_paren:
1036 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001037 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1038 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001039 break;
1040 case tok::r_paren:
1041 nextToken();
1042 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001043 case tok::r_brace:
1044 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1045 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001046 case tok::l_square:
1047 tryToParseLambda();
1048 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001049 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001050 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001051 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001052 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001053 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001054 case tok::at:
1055 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001056 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001057 parseBracedList();
1058 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001059 case tok::identifier:
1060 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001061 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001062 tryToParseJSFunction();
1063 else
1064 nextToken();
1065 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001066 default:
1067 nextToken();
1068 break;
1069 }
1070 } while (!eof());
1071}
1072
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001073void UnwrappedLineParser::parseSquare() {
1074 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1075 if (tryToParseLambda())
1076 return;
1077 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001078 switch (FormatTok->Tok.getKind()) {
1079 case tok::l_paren:
1080 parseParens();
1081 break;
1082 case tok::r_square:
1083 nextToken();
1084 return;
1085 case tok::r_brace:
1086 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1087 return;
1088 case tok::l_square:
1089 parseSquare();
1090 break;
1091 case tok::l_brace: {
1092 if (!tryToParseBracedList()) {
1093 parseChildBlock();
1094 }
1095 break;
1096 }
1097 case tok::at:
1098 nextToken();
1099 if (FormatTok->Tok.is(tok::l_brace))
1100 parseBracedList();
1101 break;
1102 default:
1103 nextToken();
1104 break;
1105 }
1106 } while (!eof());
1107}
1108
Daniel Jasperf7935112012-12-03 18:12:45 +00001109void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001110 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001111 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001112 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001113 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001114 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001115 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001116 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001117 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001118 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1119 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001120 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001121 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001122 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001123 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001124 } else {
1125 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001126 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001127 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001128 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001129 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001130 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001131 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1132 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001133 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001134 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001135 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001136 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001137 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001138 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001139 parseIfThenElse();
1140 } else {
1141 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001142 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001143 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001144 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001145 }
1146 } else if (NeedsUnwrappedLine) {
1147 addUnwrappedLine();
1148 }
1149}
1150
Daniel Jasper04a71a42014-05-08 11:58:24 +00001151void UnwrappedLineParser::parseTryCatch() {
1152 assert(FormatTok->is(tok::kw_try) && "'try' expected");
1153 nextToken();
1154 bool NeedsUnwrappedLine = false;
1155 if (FormatTok->is(tok::colon)) {
1156 // We are in a function try block, what comes is an initializer list.
1157 nextToken();
1158 while (FormatTok->is(tok::identifier)) {
1159 nextToken();
1160 if (FormatTok->is(tok::l_paren))
1161 parseParens();
1162 else
1163 StructuralError = true;
1164 if (FormatTok->is(tok::comma))
1165 nextToken();
1166 }
1167 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001168 // Parse try with resource.
1169 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1170 parseParens();
1171 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001172 if (FormatTok->is(tok::l_brace)) {
1173 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1174 parseBlock(/*MustBeDeclaration=*/false);
1175 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1176 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1177 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1178 addUnwrappedLine();
1179 } else {
1180 NeedsUnwrappedLine = true;
1181 }
1182 } else if (!FormatTok->is(tok::kw_catch)) {
1183 // The C++ standard requires a compound-statement after a try.
1184 // If there's none, we try to assume there's a structuralElement
1185 // and try to continue.
1186 StructuralError = true;
1187 addUnwrappedLine();
1188 ++Line->Level;
1189 parseStructuralElement();
1190 --Line->Level;
1191 }
1192 while (FormatTok->is(tok::kw_catch) ||
Daniel Jaspera3ddf862014-11-02 19:21:48 +00001193 ((Style.Language == FormatStyle::LK_Java ||
1194 Style.Language == FormatStyle::LK_JavaScript) &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001195 FormatTok->is(Keywords.kw_finally))) {
Daniel Jasper04a71a42014-05-08 11:58:24 +00001196 nextToken();
1197 while (FormatTok->isNot(tok::l_brace)) {
1198 if (FormatTok->is(tok::l_paren)) {
1199 parseParens();
1200 continue;
1201 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001202 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001203 return;
1204 nextToken();
1205 }
1206 NeedsUnwrappedLine = false;
1207 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1208 parseBlock(/*MustBeDeclaration=*/false);
1209 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1210 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1211 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1212 addUnwrappedLine();
1213 } else {
1214 NeedsUnwrappedLine = true;
1215 }
1216 }
1217 if (NeedsUnwrappedLine) {
1218 addUnwrappedLine();
1219 }
1220}
1221
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001222void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001223 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001224
1225 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001226 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001227 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001228 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001229 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001230 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001231 addUnwrappedLine();
1232
Daniel Jasper65ee3472013-07-31 23:16:02 +00001233 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1234 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1235 DeclarationScopeStack.size() > 1);
1236 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001237 // Munch the semicolon after a namespace. This is more common than one would
1238 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001239 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001240 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001241 addUnwrappedLine();
1242 }
1243 // FIXME: Add error handling.
1244}
1245
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001246void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001247 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1248 FormatTok->IsForEachMacro) &&
1249 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001250 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001251 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001252 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001253 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001254 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001255 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001256 addUnwrappedLine();
1257 } else {
1258 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001259 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001260 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001261 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001262 }
1263}
1264
Daniel Jasperf7935112012-12-03 18:12:45 +00001265void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001266 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001267 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001268 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001269 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001270 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001271 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1272 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001273 } else {
1274 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001275 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001276 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001277 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001278 }
1279
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001280 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001281 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001282 addUnwrappedLine();
1283 return;
1284 }
1285
Daniel Jasperf7935112012-12-03 18:12:45 +00001286 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001287 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001288}
1289
1290void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001291 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001292 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001293 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001294 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001295 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001296 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001297 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001298 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001299 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1300 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1301 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001302 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001303 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001304 parseStructuralElement();
1305 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001306 addUnwrappedLine();
1307 } else {
1308 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001309 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001310 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001311}
1312
1313void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001314 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001315 // FIXME: fix handling of complex expressions here.
1316 do {
1317 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001318 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001319 parseLabel();
1320}
1321
1322void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001323 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001324 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001325 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001326 parseParens();
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);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001329 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001330 addUnwrappedLine();
1331 } else {
1332 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001333 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001334 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001335 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001336 }
1337}
1338
1339void UnwrappedLineParser::parseAccessSpecifier() {
1340 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001341 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001342 if (FormatTok->is(tok::identifier) &&
1343 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001344 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001345 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001346 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001347 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001348 addUnwrappedLine();
1349}
1350
1351void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001352 // Won't be 'enum' for NS_ENUMs.
1353 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001354 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001355
Daniel Jasper2b41a822013-08-20 12:42:50 +00001356 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001357 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1358 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001359 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001360 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1361 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001362 nextToken();
1363 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001364 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001365 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001366 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001367 nextToken();
1368 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001369
1370 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001371 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001372 return;
1373 FormatTok->BlockKind = BK_Block;
1374
1375 if (Style.Language == FormatStyle::LK_Java) {
1376 // Java enums are different.
1377 parseJavaEnumBody();
1378 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001379 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001380
1381 // Parse enum body.
1382 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1383 if (HasError) {
1384 if (FormatTok->is(tok::semi))
1385 nextToken();
1386 addUnwrappedLine();
1387 }
1388
Manuel Klimek2cec0192013-01-21 19:17:52 +00001389 // We fall through to parsing a structural element afterwards, so that in
1390 // enum A {} n, m;
1391 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001392}
1393
1394void UnwrappedLineParser::parseJavaEnumBody() {
1395 // Determine whether the enum is simple, i.e. does not have a semicolon or
1396 // constants with class bodies. Simple enums can be formatted like braced
1397 // lists, contracted to a single line, etc.
1398 unsigned StoredPosition = Tokens->getPosition();
1399 bool IsSimple = true;
1400 FormatToken *Tok = Tokens->getNextToken();
1401 while (Tok) {
1402 if (Tok->is(tok::r_brace))
1403 break;
1404 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1405 IsSimple = false;
1406 break;
1407 }
1408 // FIXME: This will also mark enums with braces in the arguments to enum
1409 // constants as "not simple". This is probably fine in practice, though.
1410 Tok = Tokens->getNextToken();
1411 }
1412 FormatTok = Tokens->setPosition(StoredPosition);
1413
1414 if (IsSimple) {
1415 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001416 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001417 return;
1418 }
1419
1420 // Parse the body of a more complex enum.
1421 // First add a line for everything up to the "{".
1422 nextToken();
1423 addUnwrappedLine();
1424 ++Line->Level;
1425
1426 // Parse the enum constants.
1427 while (FormatTok) {
1428 if (FormatTok->is(tok::l_brace)) {
1429 // Parse the constant's class body.
1430 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1431 /*MunchSemi=*/false);
1432 } else if (FormatTok->is(tok::l_paren)) {
1433 parseParens();
1434 } else if (FormatTok->is(tok::comma)) {
1435 nextToken();
1436 addUnwrappedLine();
1437 } else if (FormatTok->is(tok::semi)) {
1438 nextToken();
1439 addUnwrappedLine();
1440 break;
1441 } else if (FormatTok->is(tok::r_brace)) {
1442 addUnwrappedLine();
1443 break;
1444 } else {
1445 nextToken();
1446 }
1447 }
1448
1449 // Parse the class body after the enum's ";" if any.
1450 parseLevel(/*HasOpeningBrace=*/true);
1451 nextToken();
1452 --Line->Level;
1453 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001454}
1455
Manuel Klimeke01bab52013-01-15 13:38:33 +00001456void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001457 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001458 nextToken();
Manuel Klimek45bf56c2014-07-31 07:19:30 +00001459 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1460 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001461 nextToken();
1462 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001463 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001464 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001465 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001466 // The actual identifier can be a nested name specifier, and in macros
1467 // it is often token-pasted.
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001468 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1469 FormatTok->is(tok::hashhash) ||
1470 (Style.Language == FormatStyle::LK_Java &&
1471 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001472 nextToken();
1473
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001474 // Note that parsing away template declarations here leads to incorrectly
1475 // accepting function declarations as record declarations.
1476 // In general, we cannot solve this problem. Consider:
1477 // class A<int> B() {}
1478 // which can be a function definition or a class definition when B() is a
1479 // macro. If we find enough real-world cases where this is a problem, we
1480 // can parse for the 'template' keyword in the beginning of the statement,
1481 // and thus rule out the record production in case there is no template
1482 // (this would still leave us with an ambiguity between template function
1483 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001484 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1485 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1486 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001487 return;
1488 nextToken();
1489 }
1490 }
1491 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001492 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001493 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001494 addUnwrappedLine();
1495
Daniel Jasper240dfda2014-03-31 14:23:49 +00001496 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001497 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001498 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001499 // We fall through to parsing a structural element afterwards, so
1500 // class A {} n, m;
1501 // will end up in one unwrapped line.
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001502 // This does not apply for Java.
1503 if (Style.Language == FormatStyle::LK_Java)
1504 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001505}
1506
Nico Weber8696a8d2013-01-09 21:15:03 +00001507void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001508 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001509 do
1510 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001511 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001512 nextToken(); // Skip '>'.
1513}
1514
1515void UnwrappedLineParser::parseObjCUntilAtEnd() {
1516 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001517 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001518 nextToken();
1519 addUnwrappedLine();
1520 break;
1521 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001522 if (FormatTok->is(tok::l_brace)) {
1523 parseBlock(/*MustBeDeclaration=*/false);
1524 // In ObjC interfaces, nothing should be following the "}".
1525 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001526 } else if (FormatTok->is(tok::r_brace)) {
1527 // Ignore stray "}". parseStructuralElement doesn't consume them.
1528 nextToken();
1529 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001530 } else {
1531 parseStructuralElement();
1532 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001533 } while (!eof());
1534}
1535
Nico Weber2ce0ac52013-01-09 23:25:37 +00001536void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001537 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001538 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001539
1540 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001541 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001542 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001543 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001544 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001545 // Skip category, if present.
1546 parseParens();
1547
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001548 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001549 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001550
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001551 if (FormatTok->Tok.is(tok::l_brace)) {
1552 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1553 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1554 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001555 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001556 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001557
1558 // With instance variables, this puts '}' on its own line. Without instance
1559 // variables, this ends the @interface line.
1560 addUnwrappedLine();
1561
Nico Weber8696a8d2013-01-09 21:15:03 +00001562 parseObjCUntilAtEnd();
1563}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001564
Nico Weber8696a8d2013-01-09 21:15:03 +00001565void UnwrappedLineParser::parseObjCProtocol() {
1566 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001567 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001568
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001569 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001570 parseObjCProtocolList();
1571
1572 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001573 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001574 nextToken();
1575 return addUnwrappedLine();
1576 }
1577
1578 addUnwrappedLine();
1579 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001580}
1581
Daniel Jasper3b203a62013-09-05 16:05:56 +00001582LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1583 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001584 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1585 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1586 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1587 E = Line.Tokens.end();
1588 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001589 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001590 }
1591 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1592 E = Line.Tokens.end();
1593 I != E; ++I) {
1594 const UnwrappedLineNode &Node = *I;
1595 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1596 I = Node.Children.begin(),
1597 E = Node.Children.end();
1598 I != E; ++I) {
1599 printDebugInfo(*I, "\nChild: ");
1600 }
1601 }
1602 llvm::dbgs() << "\n";
1603}
1604
Daniel Jasperf7935112012-12-03 18:12:45 +00001605void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001606 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001607 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001608 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001609 if (CurrentLines == &Lines)
1610 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001611 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001612 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001613 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001614 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001615 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001616 I = PreprocessorDirectives.begin(),
1617 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001618 I != E; ++I) {
1619 CurrentLines->push_back(*I);
1620 }
1621 PreprocessorDirectives.clear();
1622 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001623}
1624
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001625bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001626
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001627bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001628 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1629 FormatTok.NewlinesBefore > 0;
1630}
1631
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001632void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1633 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001634 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001635 I = CommentsBeforeNextToken.begin(),
1636 E = CommentsBeforeNextToken.end();
1637 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001638 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001639 addUnwrappedLine();
1640 }
1641 pushToken(*I);
1642 }
1643 if (NewlineBeforeNext && JustComments) {
1644 addUnwrappedLine();
1645 }
1646 CommentsBeforeNextToken.clear();
1647}
1648
Daniel Jasperf7935112012-12-03 18:12:45 +00001649void UnwrappedLineParser::nextToken() {
1650 if (eof())
1651 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001652 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001653 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001654 readToken();
1655}
1656
1657void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001658 bool CommentsInCurrentLine = true;
1659 do {
1660 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001661 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001662 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1663 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001664 // If there is an unfinished unwrapped line, we flush the preprocessor
1665 // directives only after that unwrapped line was finished later.
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001666 bool SwitchToPreprocessorLines =
1667 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001668 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001669 // Comments stored before the preprocessor directive need to be output
1670 // before the preprocessor directive, at the same level as the
1671 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001672 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001673 parsePPDirective();
1674 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001675 while (FormatTok->Type == TT_ConflictStart ||
1676 FormatTok->Type == TT_ConflictEnd ||
1677 FormatTok->Type == TT_ConflictAlternative) {
1678 if (FormatTok->Type == TT_ConflictStart) {
1679 conditionalCompilationStart(/*Unreachable=*/false);
1680 } else if (FormatTok->Type == TT_ConflictAlternative) {
1681 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001682 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001683 conditionalCompilationEnd();
1684 }
1685 FormatTok = Tokens->getNextToken();
1686 FormatTok->MustBreakBefore = true;
1687 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001688
1689 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1690 !Line->InPPDirective) {
1691 continue;
1692 }
1693
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001694 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001695 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001696 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001697 CommentsInCurrentLine = false;
1698 }
1699 if (CommentsInCurrentLine) {
1700 pushToken(FormatTok);
1701 } else {
1702 CommentsBeforeNextToken.push_back(FormatTok);
1703 }
1704 } while (!eof());
1705}
1706
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001707void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001708 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001709 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001710 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001711 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001712 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001713}
1714
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001715} // end namespace format
1716} // end namespace clang