blob: 47b3b9c6b897ebc7a60801f048d81bd7bdc386ed [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file contains the implementation of the UnwrappedLineParser,
12/// which turns a stream of tokens into UnwrappedLines.
13///
Daniel Jasperf7935112012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Chandler Carruth4b417452013-01-19 08:09:44 +000016#include "UnwrappedLineParser.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000017#include "llvm/Support/Debug.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000018
Chandler Carruth10346662014-04-22 03:17:02 +000019#define DEBUG_TYPE "format-parser"
20
Daniel Jasperf7935112012-12-03 18:12:45 +000021namespace clang {
22namespace format {
23
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000024class FormatTokenSource {
25public:
26 virtual ~FormatTokenSource() {}
27 virtual FormatToken *getNextToken() = 0;
28
29 virtual unsigned getPosition() = 0;
30 virtual FormatToken *setPosition(unsigned Position) = 0;
31};
32
Craig Topper69665e12013-07-01 04:21:54 +000033namespace {
34
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000035class ScopedDeclarationState {
36public:
37 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
38 bool MustBeDeclaration)
39 : Line(Line), Stack(Stack) {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000040 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek39080572013-01-23 11:03:04 +000041 Stack.push_back(MustBeDeclaration);
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000042 }
43 ~ScopedDeclarationState() {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000044 Stack.pop_back();
Manuel Klimekc1237a82013-01-23 14:08:21 +000045 if (!Stack.empty())
46 Line.MustBeDeclaration = Stack.back();
47 else
48 Line.MustBeDeclaration = true;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000049 }
Daniel Jasper393564f2013-05-31 14:56:29 +000050
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000051private:
52 UnwrappedLine &Line;
53 std::vector<bool> &Stack;
54};
55
Manuel Klimek1abf7892013-01-04 23:34:14 +000056class ScopedMacroState : public FormatTokenSource {
57public:
58 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000059 FormatToken *&ResetToken, bool &StructuralError)
Manuel Klimek1abf7892013-01-04 23:34:14 +000060 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000061 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
62 StructuralError(StructuralError),
Craig Topper2145bc02014-05-09 08:15:10 +000063 PreviousStructuralError(StructuralError), Token(nullptr) {
Manuel Klimek1abf7892013-01-04 23:34:14 +000064 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000065 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000066 Line.InPPDirective = true;
67 }
68
69 ~ScopedMacroState() {
70 TokenSource = PreviousTokenSource;
71 ResetToken = Token;
72 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000073 Line.Level = PreviousLineLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +000074 StructuralError = PreviousStructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +000075 }
76
Craig Topperfb6b25b2014-03-15 04:29:04 +000077 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +000078 // The \c UnwrappedLineParser guards against this by never calling
79 // \c getNextToken() after it has encountered the first eof token.
80 assert(!eof());
Manuel Klimek1abf7892013-01-04 23:34:14 +000081 Token = PreviousTokenSource->getNextToken();
82 if (eof())
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000083 return getFakeEOF();
Manuel Klimek1abf7892013-01-04 23:34:14 +000084 return Token;
85 }
86
Craig Topperfb6b25b2014-03-15 04:29:04 +000087 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +000088
Craig Topperfb6b25b2014-03-15 04:29:04 +000089 FormatToken *setPosition(unsigned Position) override {
Manuel Klimekab419912013-05-23 09:41:43 +000090 Token = PreviousTokenSource->setPosition(Position);
91 return Token;
92 }
93
Manuel Klimek1abf7892013-01-04 23:34:14 +000094private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000095 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimek1abf7892013-01-04 23:34:14 +000096
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000097 FormatToken *getFakeEOF() {
98 static bool EOFInitialized = false;
99 static FormatToken FormatTok;
100 if (!EOFInitialized) {
101 FormatTok.Tok.startToken();
102 FormatTok.Tok.setKind(tok::eof);
103 EOFInitialized = true;
104 }
105 return &FormatTok;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000106 }
107
108 UnwrappedLine &Line;
109 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000110 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000111 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000112 FormatTokenSource *PreviousTokenSource;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000113 bool &StructuralError;
114 bool PreviousStructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000115
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000116 FormatToken *Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000117};
118
Craig Topper69665e12013-07-01 04:21:54 +0000119} // end anonymous namespace
120
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000121class ScopedLineState {
122public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000123 ScopedLineState(UnwrappedLineParser &Parser,
124 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000125 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000126 if (SwitchToPreprocessorLines)
127 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000128 else if (!Parser.Line->Tokens.empty())
129 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000130 PreBlockLine = std::move(Parser.Line);
131 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000132 Parser.Line->Level = PreBlockLine->Level;
133 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000134 }
135
136 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000137 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000138 Parser.addUnwrappedLine();
139 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000140 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000141 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000142 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
143 Parser.MustBreakBeforeNextToken = true;
144 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000145 }
146
147private:
148 UnwrappedLineParser &Parser;
149
David Blaikieefb6eb22014-08-09 20:02:07 +0000150 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000151 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000152};
153
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000154class CompoundStatementIndenter {
155public:
156 CompoundStatementIndenter(UnwrappedLineParser *Parser,
157 const FormatStyle &Style, unsigned &LineLevel)
158 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
159 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
160 Parser->addUnwrappedLine();
161 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
162 Parser->addUnwrappedLine();
163 ++LineLevel;
164 }
165 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000166 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000167
168private:
169 unsigned &LineLevel;
170 unsigned OldLineLevel;
171};
172
Craig Topper69665e12013-07-01 04:21:54 +0000173namespace {
174
Manuel Klimekab419912013-05-23 09:41:43 +0000175class IndexedTokenSource : public FormatTokenSource {
176public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000177 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000178 : Tokens(Tokens), Position(-1) {}
179
Craig Topperfb6b25b2014-03-15 04:29:04 +0000180 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000181 ++Position;
182 return Tokens[Position];
183 }
184
Craig Topperfb6b25b2014-03-15 04:29:04 +0000185 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000186 assert(Position >= 0);
187 return Position;
188 }
189
Craig Topperfb6b25b2014-03-15 04:29:04 +0000190 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000191 Position = P;
192 return Tokens[Position];
193 }
194
Manuel Klimek71814b42013-10-11 21:25:45 +0000195 void reset() { Position = -1; }
196
Manuel Klimekab419912013-05-23 09:41:43 +0000197private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000198 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000199 int Position;
200};
201
Craig Topper69665e12013-07-01 04:21:54 +0000202} // end anonymous namespace
203
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000204UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000205 const AdditionalKeywords &Keywords,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000206 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000207 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000208 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
209 CurrentLines(&Lines), StructuralError(false), Style(Style),
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000210 Keywords(Keywords), Tokens(nullptr), Callback(Callback),
211 AllTokens(Tokens), PPBranchLevel(-1) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000212
213void UnwrappedLineParser::reset() {
214 PPBranchLevel = -1;
215 Line.reset(new UnwrappedLine);
216 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000217 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000218 MustBreakBeforeNextToken = false;
219 PreprocessorDirectives.clear();
220 CurrentLines = &Lines;
221 DeclarationScopeStack.clear();
222 StructuralError = false;
223 PPStack.clear();
224}
Daniel Jasperf7935112012-12-03 18:12:45 +0000225
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000226bool UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000227 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimek71814b42013-10-11 21:25:45 +0000228 do {
229 DEBUG(llvm::dbgs() << "----\n");
230 reset();
231 Tokens = &TokenSource;
232 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000233
Manuel Klimek71814b42013-10-11 21:25:45 +0000234 readToken();
235 parseFile();
236 // Create line with eof token.
237 pushToken(FormatTok);
238 addUnwrappedLine();
239
240 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
241 E = Lines.end();
242 I != E; ++I) {
243 Callback.consumeUnwrappedLine(*I);
244 }
245 Callback.finishRun();
246 Lines.clear();
247 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000248 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000249 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
250 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
251 }
252 if (!PPLevelBranchIndex.empty()) {
253 ++PPLevelBranchIndex.back();
254 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
255 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
256 }
257 } while (!PPLevelBranchIndex.empty());
258
Manuel Klimek1a18c402013-04-12 14:13:36 +0000259 return StructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000260}
261
Manuel Klimek1a18c402013-04-12 14:13:36 +0000262void UnwrappedLineParser::parseFile() {
Daniel Jasperdd9276e2013-03-22 16:55:40 +0000263 ScopedDeclarationState DeclarationState(
264 *Line, DeclarationScopeStack,
265 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber9096fc02013-06-26 00:30:14 +0000266 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000267 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000268 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000269 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000270}
271
Manuel Klimek1a18c402013-04-12 14:13:36 +0000272void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000273 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000274 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000275 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000276 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000277 nextToken();
278 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000279 break;
280 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000281 // FIXME: Add parameter whether this can happen - if this happens, we must
282 // be in a non-declaration context.
Nico Weber9096fc02013-06-26 00:30:14 +0000283 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000284 addUnwrappedLine();
285 break;
286 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000287 if (HasOpeningBrace)
288 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000289 StructuralError = true;
290 nextToken();
291 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000292 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000293 case tok::kw_default:
294 case tok::kw_case:
Daniel Jasper72407622013-09-02 08:26:29 +0000295 if (!SwitchLabelEncountered &&
296 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
297 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000298 SwitchLabelEncountered = true;
299 parseStructuralElement();
300 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000301 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000302 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000303 break;
304 }
305 } while (!eof());
306}
307
Manuel Klimekab419912013-05-23 09:41:43 +0000308void UnwrappedLineParser::calculateBraceTypes() {
309 // We'll parse forward through the tokens until we hit
310 // a closing brace or eof - note that getNextToken() will
311 // parse macros, so this will magically work inside macro
312 // definitions, too.
313 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000314 FormatToken *Tok = FormatTok;
Manuel Klimekab419912013-05-23 09:41:43 +0000315 // Keep a stack of positions of lbrace tokens. We will
316 // update information about whether an lbrace starts a
317 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000318 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000319 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000320 do {
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000321 // Get next none-comment token.
322 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000323 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000324 do {
325 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000326 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000327 } while (NextTok->is(tok::comment));
328
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000329 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000330 case tok::l_brace:
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000331 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000332 break;
333 case tok::r_brace:
334 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000335 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Daniel Jasper220c0d12014-04-10 07:27:12 +0000336 bool ProbablyBracedList = false;
337 if (Style.Language == FormatStyle::LK_Proto) {
338 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
339 } else {
Daniel Jasper91b032a2014-05-22 12:46:38 +0000340 // Using OriginalColumn to distinguish between ObjC methods and
341 // binary operators is a bit hacky.
342 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
343 NextTok->OriginalColumn == 0;
344
Daniel Jasper220c0d12014-04-10 07:27:12 +0000345 // If there is a comma, semicolon or right paren after the closing
346 // brace, we assume this is a braced initializer list. Note that
347 // regardless how we mark inner braces here, we will overwrite the
348 // BlockKind later if we parse a braced list (where all blocks
349 // inside are by default braced lists), or when we explicitly detect
350 // blocks (for example while parsing lambdas).
351 //
352 // We exclude + and - as they can be ObjC visibility modifiers.
353 ProbablyBracedList =
354 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
Daniel Jasper438059e2014-05-22 12:11:13 +0000355 tok::r_paren, tok::r_square, tok::l_brace,
Daniel Jasper65df5aa2014-08-04 14:51:02 +0000356 tok::l_paren, tok::ellipsis) ||
Daniel Jasper91b032a2014-05-22 12:46:38 +0000357 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000358 }
359 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000360 Tok->BlockKind = BK_BracedInit;
361 LBraceStack.back()->BlockKind = BK_BracedInit;
362 } else {
363 Tok->BlockKind = BK_Block;
364 LBraceStack.back()->BlockKind = BK_Block;
365 }
Manuel Klimekab419912013-05-23 09:41:43 +0000366 }
367 LBraceStack.pop_back();
368 }
369 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000370 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000371 case tok::semi:
372 case tok::kw_if:
373 case tok::kw_while:
374 case tok::kw_for:
375 case tok::kw_switch:
376 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000377 case tok::kw___try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000378 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000379 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000380 break;
381 default:
382 break;
383 }
384 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000385 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000386 // Assume other blocks for all unclosed opening braces.
387 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000388 if (LBraceStack[i]->BlockKind == BK_Unknown)
389 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000390 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000391
Manuel Klimekab419912013-05-23 09:41:43 +0000392 FormatTok = Tokens->setPosition(StoredPosition);
393}
394
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000395void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
396 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000397 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000398 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000399 nextToken();
400
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000401 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000402
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000403 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
404 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000405 if (AddLevel)
406 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000407 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000408
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000409 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000410 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000411 StructuralError = true;
412 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000413 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000414
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000415 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000416 if (MunchSemi && FormatTok->Tok.is(tok::semi))
417 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000418 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000419}
420
Daniel Jasper4a39c842014-05-06 13:54:10 +0000421static bool IsGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000422 // FIXME: Closure-library specific stuff should not be hard-coded but be
423 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000424 if (Line.Tokens.size() < 4)
425 return false;
426 auto I = Line.Tokens.begin();
427 if (I->Tok->TokenText != "goog")
428 return false;
429 ++I;
430 if (I->Tok->isNot(tok::period))
431 return false;
432 ++I;
433 if (I->Tok->TokenText != "scope")
434 return false;
435 ++I;
436 return I->Tok->is(tok::l_paren);
437}
438
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000439static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
440 const FormatToken &InitialToken) {
441 switch (Style.BreakBeforeBraces) {
442 case FormatStyle::BS_Linux:
443 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
444 case FormatStyle::BS_Allman:
445 case FormatStyle::BS_GNU:
446 return true;
447 default:
448 return false;
449 }
450}
451
Manuel Klimek516e0542013-09-04 13:25:30 +0000452void UnwrappedLineParser::parseChildBlock() {
453 FormatTok->BlockKind = BK_Block;
454 nextToken();
455 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000456 bool GoogScope =
457 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000458 ScopedLineState LineState(*this);
459 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
460 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000461 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000462 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000463 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000464 }
465 nextToken();
466}
467
Daniel Jasperf7935112012-12-03 18:12:45 +0000468void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000469 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000470 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000471 nextToken();
472
Craig Topper2145bc02014-05-09 08:15:10 +0000473 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000474 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000475 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000476 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000477
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000478 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000479 case tok::pp_define:
480 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000481 return;
482 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000483 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000484 break;
485 case tok::pp_ifdef:
486 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000487 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000488 break;
489 case tok::pp_else:
490 parsePPElse();
491 break;
492 case tok::pp_elif:
493 parsePPElIf();
494 break;
495 case tok::pp_endif:
496 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000497 break;
498 default:
499 parsePPUnknown();
500 break;
501 }
502}
503
Manuel Klimek68b03042014-04-14 09:14:11 +0000504void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
505 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000506 PPStack.push_back(PP_Unreachable);
507 else
508 PPStack.push_back(PP_Conditional);
509}
510
Manuel Klimek68b03042014-04-14 09:14:11 +0000511void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000512 ++PPBranchLevel;
513 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
514 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
515 PPLevelBranchIndex.push_back(0);
516 PPLevelBranchCount.push_back(0);
517 }
518 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000519 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
520 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000521}
522
Manuel Klimek68b03042014-04-14 09:14:11 +0000523void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000524 if (!PPStack.empty())
525 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000526 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
527 if (!PPChainBranchIndex.empty())
528 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000529 conditionalCompilationCondition(
530 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
531 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000532}
533
Manuel Klimek68b03042014-04-14 09:14:11 +0000534void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000535 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
536 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
537 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000538 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
539 }
540 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000541 // Guard against #endif's without #if.
542 if (PPBranchLevel > 0)
543 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000544 if (!PPChainBranchIndex.empty())
545 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000546 if (!PPStack.empty())
547 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000548}
549
550void UnwrappedLineParser::parsePPIf(bool IfDef) {
551 nextToken();
552 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Daniel Jasper9d22bcc2015-01-19 10:51:05 +0000553 FormatTok->Tok.getLiteralData() != nullptr &&
Manuel Klimek68b03042014-04-14 09:14:11 +0000554 StringRef(FormatTok->Tok.getLiteralData(),
555 FormatTok->Tok.getLength()) == "0") ||
556 FormatTok->Tok.is(tok::kw_false);
557 conditionalCompilationStart(!IfDef && IsLiteralFalse);
558 parsePPUnknown();
559}
560
561void UnwrappedLineParser::parsePPElse() {
562 conditionalCompilationAlternative();
563 parsePPUnknown();
564}
565
566void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
567
568void UnwrappedLineParser::parsePPEndIf() {
569 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000570 parsePPUnknown();
571}
572
Manuel Klimek1abf7892013-01-04 23:34:14 +0000573void UnwrappedLineParser::parsePPDefine() {
574 nextToken();
575
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000576 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000577 parsePPUnknown();
578 return;
579 }
580 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000581 if (FormatTok->Tok.getKind() == tok::l_paren &&
582 FormatTok->WhitespaceRange.getBegin() ==
583 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000584 parseParens();
585 }
586 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000587 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000588
589 // Errors during a preprocessor directive can only affect the layout of the
590 // preprocessor directive, and thus we ignore them. An alternative approach
591 // would be to use the same approach we use on the file level (no
592 // re-indentation if there was a structural error) within the macro
593 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000594 parseFile();
595}
596
597void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000598 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000599 nextToken();
600 } while (!eof());
601 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000602}
603
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000604// Here we blacklist certain tokens that are not usually the first token in an
605// unwrapped line. This is used in attempt to distinguish macro calls without
606// trailing semicolons from other constructs split to several lines.
607bool tokenCanStartNewLine(clang::Token Tok) {
608 // Semicolon can be a null-statement, l_square can be a start of a macro or
609 // a C++11 attribute, but this doesn't seem to be common.
610 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
611 Tok.isNot(tok::l_square) &&
612 // Tokens that can only be used as binary operators and a part of
613 // overloaded operator names.
614 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
615 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
616 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
617 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
618 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
619 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
620 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
621 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
622 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
623 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
624 Tok.isNot(tok::lesslessequal) &&
625 // Colon is used in labels, base class lists, initializer lists,
626 // range-based for loops, ternary operator, but should never be the
627 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000628 Tok.isNot(tok::colon) &&
629 // 'noexcept' is a trailing annotation.
630 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000631}
632
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000633void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000634 assert(!FormatTok->Tok.is(tok::l_brace));
635 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000636 case tok::at:
637 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000638 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000639 parseBracedList();
640 break;
641 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000642 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000643 case tok::objc_public:
644 case tok::objc_protected:
645 case tok::objc_package:
646 case tok::objc_private:
647 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000648 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000649 case tok::objc_implementation:
650 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000651 case tok::objc_protocol:
652 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000653 case tok::objc_end:
654 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000655 case tok::objc_optional:
656 case tok::objc_required:
657 nextToken();
658 addUnwrappedLine();
659 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000660 default:
661 break;
662 }
663 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000664 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000665 nextToken();
666 if (FormatTok->is(tok::l_brace)) {
Daniel Jasper2337f282015-01-12 10:14:56 +0000667 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000668 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000669 if (FormatTok->is(tok::r_brace)) {
670 nextToken();
671 break;
672 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000673 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000674 nextToken();
675 }
676 }
677 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000678 case tok::kw_namespace:
679 parseNamespace();
680 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000681 case tok::kw_inline:
682 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000683 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000684 parseNamespace();
685 return;
686 }
687 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000688 case tok::kw_public:
689 case tok::kw_protected:
690 case tok::kw_private:
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000691 if (Style.Language == FormatStyle::LK_Java)
692 nextToken();
693 else
694 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000695 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000696 case tok::kw_if:
697 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000698 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000699 case tok::kw_for:
700 case tok::kw_while:
701 parseForOrWhileLoop();
702 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000703 case tok::kw_do:
704 parseDoWhile();
705 return;
706 case tok::kw_switch:
707 parseSwitch();
708 return;
709 case tok::kw_default:
710 nextToken();
711 parseLabel();
712 return;
713 case tok::kw_case:
714 parseCaseLabel();
715 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000716 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000717 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +0000718 parseTryCatch();
719 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000720 case tok::kw_extern:
721 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000722 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000723 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000724 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000725 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000726 addUnwrappedLine();
727 return;
728 }
729 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000730 break;
731 case tok::identifier:
732 if (FormatTok->IsForEachMacro) {
733 parseForOrWhileLoop();
734 return;
735 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000736 // In all other cases, parse the declaration.
737 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000738 default:
739 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000740 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000741 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000742 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000743 case tok::at:
744 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000745 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000746 parseBracedList();
747 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000748 case tok::kw_enum:
749 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000750 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000751 case tok::kw_typedef:
752 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000753 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
754 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000755 parseEnum();
756 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000757 case tok::kw_struct:
758 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000759 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000760 parseRecord();
761 // A record declaration or definition is always the start of a structural
762 // element.
763 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000764 case tok::period:
765 nextToken();
766 // In Java, classes have an implicit static member "class".
767 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
768 FormatTok->is(tok::kw_class))
769 nextToken();
770 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000771 case tok::semi:
772 nextToken();
773 addUnwrappedLine();
774 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000775 case tok::r_brace:
776 addUnwrappedLine();
777 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000778 case tok::l_paren:
779 parseParens();
780 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000781 case tok::caret:
782 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000783 if (FormatTok->Tok.isAnyIdentifier() ||
784 FormatTok->isSimpleTypeSpecifier())
785 nextToken();
786 if (FormatTok->is(tok::l_paren))
787 parseParens();
788 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000789 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000790 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000791 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000792 if (!tryToParseBracedList()) {
793 // A block outside of parentheses must be the last part of a
794 // structural element.
795 // FIXME: Figure out cases where this is not true, and add projections
796 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000797 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000798 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000799 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000800 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000801 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000802 return;
803 }
804 // Otherwise this was a braced init list, and the structural
805 // element continues.
806 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000807 case tok::kw_try:
808 // We arrive here when parsing function-try blocks.
809 parseTryCatch();
810 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000811 case tok::identifier: {
812 StringRef Text = FormatTok->TokenText;
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000813 // Parse function literal unless 'function' is the first token in a line
814 // in which case this should be treated as a free-standing function.
815 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
816 Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000817 tryToParseJSFunction();
818 break;
819 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000820 nextToken();
Alexander Kornienkode644272013-04-08 22:16:06 +0000821 if (Line->Tokens.size() == 1) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000822 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000823 parseLabel();
824 return;
825 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000826 // Recognize function-like macro usages without trailing semicolon as
827 // well as free-standing macrose like Q_OBJECT.
828 bool FunctionLike = FormatTok->is(tok::l_paren);
829 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000830 parseParens();
Daniel Jasper680b09b2014-11-05 10:48:04 +0000831 if (FormatTok->NewlinesBefore > 0 &&
832 (Text.size() >= 5 || FunctionLike) &&
833 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000834 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000835 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000836 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000837 }
838 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000839 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000840 case tok::equal:
841 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000842 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000843 parseBracedList();
844 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000845 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000846 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000847 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000848 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000849 default:
850 nextToken();
851 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000852 }
853 } while (!eof());
854}
855
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000856bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000857 // FIXME: This is a dirty way to access the previous token. Find a better
858 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000859 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000860 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
861 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000862 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000863 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000864 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000865 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000866 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000867 assert(FormatTok->is(tok::l_square));
868 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000869 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000870 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000871
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000872 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000873 if (FormatTok->isSimpleTypeSpecifier()) {
874 nextToken();
875 continue;
876 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000877 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000878 case tok::l_brace:
879 break;
880 case tok::l_paren:
881 parseParens();
882 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000883 case tok::amp:
884 case tok::star:
885 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000886 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000887 case tok::less:
888 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000889 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000890 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000891 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000892 nextToken();
893 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000894 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000895 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000896 nextToken();
897 break;
898 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000899 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000900 }
901 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000902 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000903 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000904 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000905}
906
907bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
908 nextToken();
909 if (FormatTok->is(tok::equal)) {
910 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000911 if (FormatTok->is(tok::r_square)) {
912 nextToken();
913 return true;
914 }
915 if (FormatTok->isNot(tok::comma))
916 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000917 nextToken();
918 } else if (FormatTok->is(tok::amp)) {
919 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000920 if (FormatTok->is(tok::r_square)) {
921 nextToken();
922 return true;
923 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000924 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
925 return false;
926 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000927 if (FormatTok->is(tok::comma))
928 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000929 } else if (FormatTok->is(tok::r_square)) {
930 nextToken();
931 return true;
932 }
933 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000934 if (FormatTok->is(tok::amp))
935 nextToken();
936 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
937 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000938 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000939 if (FormatTok->is(tok::ellipsis))
940 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000941 if (FormatTok->is(tok::comma)) {
942 nextToken();
943 } else if (FormatTok->is(tok::r_square)) {
944 nextToken();
945 return true;
946 } else {
947 return false;
948 }
949 } while (!eof());
950 return false;
951}
952
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000953void UnwrappedLineParser::tryToParseJSFunction() {
954 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000955
956 // Consume function name.
957 if (FormatTok->is(tok::identifier))
958 nextToken();
959
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000960 if (FormatTok->isNot(tok::l_paren))
961 return;
962 nextToken();
963 while (FormatTok->isNot(tok::l_brace)) {
964 // Err on the side of caution in order to avoid consuming the full file in
965 // case of incomplete code.
966 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
967 tok::comment))
968 return;
969 nextToken();
970 }
971 parseChildBlock();
972}
973
Manuel Klimekab419912013-05-23 09:41:43 +0000974bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000975 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +0000976 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000977 assert(FormatTok->BlockKind != BK_Unknown);
978 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +0000979 return false;
980 parseBracedList();
981 return true;
982}
983
Daniel Jasper015ed022013-09-13 09:20:45 +0000984bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
985 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000986 nextToken();
987
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000988 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
989 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000990 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000991 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000992 FormatTok->is(Keywords.kw_function)) {
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000993 tryToParseJSFunction();
994 continue;
995 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000996 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +0000997 case tok::caret:
998 nextToken();
999 if (FormatTok->is(tok::l_brace)) {
1000 parseChildBlock();
1001 }
1002 break;
1003 case tok::l_square:
1004 tryToParseLambda();
1005 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001006 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001007 // Assume there are no blocks inside a braced init list apart
1008 // from the ones we explicitly parse out (like lambdas).
1009 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001010 parseBracedList();
1011 break;
1012 case tok::r_brace:
1013 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001014 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001015 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001016 HasError = true;
1017 if (!ContinueOnSemicolons)
1018 return !HasError;
1019 nextToken();
1020 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001021 case tok::comma:
1022 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001023 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001024 default:
1025 nextToken();
1026 break;
1027 }
1028 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001029 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001030}
1031
Daniel Jasperf7935112012-12-03 18:12:45 +00001032void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001033 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001034 nextToken();
1035 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001036 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001037 case tok::l_paren:
1038 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001039 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1040 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001041 break;
1042 case tok::r_paren:
1043 nextToken();
1044 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001045 case tok::r_brace:
1046 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1047 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001048 case tok::l_square:
1049 tryToParseLambda();
1050 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001051 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001052 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001053 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001054 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001055 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001056 case tok::at:
1057 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001058 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001059 parseBracedList();
1060 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001061 case tok::identifier:
1062 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001063 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001064 tryToParseJSFunction();
1065 else
1066 nextToken();
1067 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001068 default:
1069 nextToken();
1070 break;
1071 }
1072 } while (!eof());
1073}
1074
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001075void UnwrappedLineParser::parseSquare() {
1076 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1077 if (tryToParseLambda())
1078 return;
1079 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001080 switch (FormatTok->Tok.getKind()) {
1081 case tok::l_paren:
1082 parseParens();
1083 break;
1084 case tok::r_square:
1085 nextToken();
1086 return;
1087 case tok::r_brace:
1088 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1089 return;
1090 case tok::l_square:
1091 parseSquare();
1092 break;
1093 case tok::l_brace: {
1094 if (!tryToParseBracedList()) {
1095 parseChildBlock();
1096 }
1097 break;
1098 }
1099 case tok::at:
1100 nextToken();
1101 if (FormatTok->Tok.is(tok::l_brace))
1102 parseBracedList();
1103 break;
1104 default:
1105 nextToken();
1106 break;
1107 }
1108 } while (!eof());
1109}
1110
Daniel Jasperf7935112012-12-03 18:12:45 +00001111void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001112 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001113 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001114 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001115 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001116 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001117 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001118 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001119 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001120 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1121 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001122 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001123 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001124 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001125 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001126 } else {
1127 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001128 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001129 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001130 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001131 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001132 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001133 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1134 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001135 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001136 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001137 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001138 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001139 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001140 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001141 parseIfThenElse();
1142 } else {
1143 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001144 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001145 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001146 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001147 }
1148 } else if (NeedsUnwrappedLine) {
1149 addUnwrappedLine();
1150 }
1151}
1152
Daniel Jasper04a71a42014-05-08 11:58:24 +00001153void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001154 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001155 nextToken();
1156 bool NeedsUnwrappedLine = false;
1157 if (FormatTok->is(tok::colon)) {
1158 // We are in a function try block, what comes is an initializer list.
1159 nextToken();
1160 while (FormatTok->is(tok::identifier)) {
1161 nextToken();
1162 if (FormatTok->is(tok::l_paren))
1163 parseParens();
1164 else
1165 StructuralError = true;
1166 if (FormatTok->is(tok::comma))
1167 nextToken();
1168 }
1169 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001170 // Parse try with resource.
1171 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1172 parseParens();
1173 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001174 if (FormatTok->is(tok::l_brace)) {
1175 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1176 parseBlock(/*MustBeDeclaration=*/false);
1177 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1178 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1179 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1180 addUnwrappedLine();
1181 } else {
1182 NeedsUnwrappedLine = true;
1183 }
1184 } else if (!FormatTok->is(tok::kw_catch)) {
1185 // The C++ standard requires a compound-statement after a try.
1186 // If there's none, we try to assume there's a structuralElement
1187 // and try to continue.
1188 StructuralError = true;
1189 addUnwrappedLine();
1190 ++Line->Level;
1191 parseStructuralElement();
1192 --Line->Level;
1193 }
Nico Weberfac23712015-02-04 15:26:27 +00001194 while (FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1195 tok::kw___finally) ||
Daniel Jaspera3ddf862014-11-02 19:21:48 +00001196 ((Style.Language == FormatStyle::LK_Java ||
1197 Style.Language == FormatStyle::LK_JavaScript) &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001198 FormatTok->is(Keywords.kw_finally))) {
Daniel Jasper04a71a42014-05-08 11:58:24 +00001199 nextToken();
1200 while (FormatTok->isNot(tok::l_brace)) {
1201 if (FormatTok->is(tok::l_paren)) {
1202 parseParens();
1203 continue;
1204 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001205 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001206 return;
1207 nextToken();
1208 }
1209 NeedsUnwrappedLine = false;
1210 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1211 parseBlock(/*MustBeDeclaration=*/false);
1212 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1213 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1214 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1215 addUnwrappedLine();
1216 } else {
1217 NeedsUnwrappedLine = true;
1218 }
1219 }
1220 if (NeedsUnwrappedLine) {
1221 addUnwrappedLine();
1222 }
1223}
1224
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001225void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001226 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001227
1228 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001229 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001230 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001231 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001232 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001233 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001234 addUnwrappedLine();
1235
Daniel Jasper65ee3472013-07-31 23:16:02 +00001236 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1237 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1238 DeclarationScopeStack.size() > 1);
1239 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001240 // Munch the semicolon after a namespace. This is more common than one would
1241 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001242 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001243 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001244 addUnwrappedLine();
1245 }
1246 // FIXME: Add error handling.
1247}
1248
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001249void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001250 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1251 FormatTok->IsForEachMacro) &&
1252 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001253 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001254 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001255 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001256 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001257 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001258 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001259 addUnwrappedLine();
1260 } else {
1261 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001262 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001263 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001264 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001265 }
1266}
1267
Daniel Jasperf7935112012-12-03 18:12:45 +00001268void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001269 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001270 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001271 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001272 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001273 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001274 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1275 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001276 } else {
1277 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001278 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001279 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001280 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001281 }
1282
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001283 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001284 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001285 addUnwrappedLine();
1286 return;
1287 }
1288
Daniel Jasperf7935112012-12-03 18:12:45 +00001289 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001290 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001291}
1292
1293void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001294 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001295 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001296 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001297 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001298 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001299 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001300 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001301 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001302 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1303 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1304 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001305 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001306 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001307 parseStructuralElement();
1308 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001309 addUnwrappedLine();
1310 } else {
1311 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001312 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001313 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001314}
1315
1316void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001317 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001318 // FIXME: fix handling of complex expressions here.
1319 do {
1320 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001321 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001322 parseLabel();
1323}
1324
1325void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001326 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001327 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001328 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001329 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001330 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001331 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001332 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001333 addUnwrappedLine();
1334 } else {
1335 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001336 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001337 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001338 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001339 }
1340}
1341
1342void UnwrappedLineParser::parseAccessSpecifier() {
1343 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001344 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001345 if (FormatTok->is(tok::identifier) &&
1346 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001347 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001348 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001349 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001350 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001351 addUnwrappedLine();
1352}
1353
1354void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001355 // Won't be 'enum' for NS_ENUMs.
1356 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001357 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001358
Daniel Jasper2b41a822013-08-20 12:42:50 +00001359 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001360 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1361 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001362 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001363 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1364 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001365 nextToken();
1366 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001367 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001368 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001369 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001370 nextToken();
1371 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001372
1373 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001374 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001375 return;
1376 FormatTok->BlockKind = BK_Block;
1377
1378 if (Style.Language == FormatStyle::LK_Java) {
1379 // Java enums are different.
1380 parseJavaEnumBody();
1381 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001382 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001383
1384 // Parse enum body.
1385 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1386 if (HasError) {
1387 if (FormatTok->is(tok::semi))
1388 nextToken();
1389 addUnwrappedLine();
1390 }
1391
Manuel Klimek2cec0192013-01-21 19:17:52 +00001392 // We fall through to parsing a structural element afterwards, so that in
1393 // enum A {} n, m;
1394 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001395}
1396
1397void UnwrappedLineParser::parseJavaEnumBody() {
1398 // Determine whether the enum is simple, i.e. does not have a semicolon or
1399 // constants with class bodies. Simple enums can be formatted like braced
1400 // lists, contracted to a single line, etc.
1401 unsigned StoredPosition = Tokens->getPosition();
1402 bool IsSimple = true;
1403 FormatToken *Tok = Tokens->getNextToken();
1404 while (Tok) {
1405 if (Tok->is(tok::r_brace))
1406 break;
1407 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1408 IsSimple = false;
1409 break;
1410 }
1411 // FIXME: This will also mark enums with braces in the arguments to enum
1412 // constants as "not simple". This is probably fine in practice, though.
1413 Tok = Tokens->getNextToken();
1414 }
1415 FormatTok = Tokens->setPosition(StoredPosition);
1416
1417 if (IsSimple) {
1418 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001419 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001420 return;
1421 }
1422
1423 // Parse the body of a more complex enum.
1424 // First add a line for everything up to the "{".
1425 nextToken();
1426 addUnwrappedLine();
1427 ++Line->Level;
1428
1429 // Parse the enum constants.
1430 while (FormatTok) {
1431 if (FormatTok->is(tok::l_brace)) {
1432 // Parse the constant's class body.
1433 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1434 /*MunchSemi=*/false);
1435 } else if (FormatTok->is(tok::l_paren)) {
1436 parseParens();
1437 } else if (FormatTok->is(tok::comma)) {
1438 nextToken();
1439 addUnwrappedLine();
1440 } else if (FormatTok->is(tok::semi)) {
1441 nextToken();
1442 addUnwrappedLine();
1443 break;
1444 } else if (FormatTok->is(tok::r_brace)) {
1445 addUnwrappedLine();
1446 break;
1447 } else {
1448 nextToken();
1449 }
1450 }
1451
1452 // Parse the class body after the enum's ";" if any.
1453 parseLevel(/*HasOpeningBrace=*/true);
1454 nextToken();
1455 --Line->Level;
1456 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001457}
1458
Manuel Klimeke01bab52013-01-15 13:38:33 +00001459void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001460 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001461 nextToken();
Manuel Klimek45bf56c2014-07-31 07:19:30 +00001462 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1463 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001464 nextToken();
1465 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001466 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001467 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001468 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001469 // The actual identifier can be a nested name specifier, and in macros
1470 // it is often token-pasted.
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001471 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1472 FormatTok->is(tok::hashhash) ||
1473 (Style.Language == FormatStyle::LK_Java &&
1474 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001475 nextToken();
1476
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001477 // Note that parsing away template declarations here leads to incorrectly
1478 // accepting function declarations as record declarations.
1479 // In general, we cannot solve this problem. Consider:
1480 // class A<int> B() {}
1481 // which can be a function definition or a class definition when B() is a
1482 // macro. If we find enough real-world cases where this is a problem, we
1483 // can parse for the 'template' keyword in the beginning of the statement,
1484 // and thus rule out the record production in case there is no template
1485 // (this would still leave us with an ambiguity between template function
1486 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001487 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1488 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1489 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001490 return;
1491 nextToken();
1492 }
1493 }
1494 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001495 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001496 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001497 addUnwrappedLine();
1498
Daniel Jasper240dfda2014-03-31 14:23:49 +00001499 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001500 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001501 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001502 // We fall through to parsing a structural element afterwards, so
1503 // class A {} n, m;
1504 // will end up in one unwrapped line.
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001505 // This does not apply for Java.
1506 if (Style.Language == FormatStyle::LK_Java)
1507 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001508}
1509
Nico Weber8696a8d2013-01-09 21:15:03 +00001510void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001511 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001512 do
1513 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001514 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001515 nextToken(); // Skip '>'.
1516}
1517
1518void UnwrappedLineParser::parseObjCUntilAtEnd() {
1519 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001520 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001521 nextToken();
1522 addUnwrappedLine();
1523 break;
1524 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001525 if (FormatTok->is(tok::l_brace)) {
1526 parseBlock(/*MustBeDeclaration=*/false);
1527 // In ObjC interfaces, nothing should be following the "}".
1528 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001529 } else if (FormatTok->is(tok::r_brace)) {
1530 // Ignore stray "}". parseStructuralElement doesn't consume them.
1531 nextToken();
1532 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001533 } else {
1534 parseStructuralElement();
1535 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001536 } while (!eof());
1537}
1538
Nico Weber2ce0ac52013-01-09 23:25:37 +00001539void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001540 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001541 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001542
1543 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001544 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001545 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001546 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001547 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001548 // Skip category, if present.
1549 parseParens();
1550
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001551 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001552 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001553
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001554 if (FormatTok->Tok.is(tok::l_brace)) {
1555 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1556 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1557 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001558 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001559 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001560
1561 // With instance variables, this puts '}' on its own line. Without instance
1562 // variables, this ends the @interface line.
1563 addUnwrappedLine();
1564
Nico Weber8696a8d2013-01-09 21:15:03 +00001565 parseObjCUntilAtEnd();
1566}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001567
Nico Weber8696a8d2013-01-09 21:15:03 +00001568void UnwrappedLineParser::parseObjCProtocol() {
1569 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001570 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001571
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001572 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001573 parseObjCProtocolList();
1574
1575 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001576 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001577 nextToken();
1578 return addUnwrappedLine();
1579 }
1580
1581 addUnwrappedLine();
1582 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001583}
1584
Daniel Jasper3b203a62013-09-05 16:05:56 +00001585LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1586 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001587 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1588 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1589 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1590 E = Line.Tokens.end();
1591 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001592 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001593 }
1594 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1595 E = Line.Tokens.end();
1596 I != E; ++I) {
1597 const UnwrappedLineNode &Node = *I;
1598 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1599 I = Node.Children.begin(),
1600 E = Node.Children.end();
1601 I != E; ++I) {
1602 printDebugInfo(*I, "\nChild: ");
1603 }
1604 }
1605 llvm::dbgs() << "\n";
1606}
1607
Daniel Jasperf7935112012-12-03 18:12:45 +00001608void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001609 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001610 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001611 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001612 if (CurrentLines == &Lines)
1613 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001614 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001615 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001616 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001617 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001618 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001619 I = PreprocessorDirectives.begin(),
1620 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001621 I != E; ++I) {
1622 CurrentLines->push_back(*I);
1623 }
1624 PreprocessorDirectives.clear();
1625 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001626}
1627
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001628bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001629
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001630bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001631 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1632 FormatTok.NewlinesBefore > 0;
1633}
1634
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001635void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1636 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001637 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001638 I = CommentsBeforeNextToken.begin(),
1639 E = CommentsBeforeNextToken.end();
1640 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001641 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001642 addUnwrappedLine();
1643 }
1644 pushToken(*I);
1645 }
1646 if (NewlineBeforeNext && JustComments) {
1647 addUnwrappedLine();
1648 }
1649 CommentsBeforeNextToken.clear();
1650}
1651
Daniel Jasperf7935112012-12-03 18:12:45 +00001652void UnwrappedLineParser::nextToken() {
1653 if (eof())
1654 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001655 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001656 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001657 readToken();
1658}
1659
1660void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001661 bool CommentsInCurrentLine = true;
1662 do {
1663 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001664 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001665 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1666 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001667 // If there is an unfinished unwrapped line, we flush the preprocessor
1668 // directives only after that unwrapped line was finished later.
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001669 bool SwitchToPreprocessorLines =
1670 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001671 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001672 // Comments stored before the preprocessor directive need to be output
1673 // before the preprocessor directive, at the same level as the
1674 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001675 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001676 parsePPDirective();
1677 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001678 while (FormatTok->Type == TT_ConflictStart ||
1679 FormatTok->Type == TT_ConflictEnd ||
1680 FormatTok->Type == TT_ConflictAlternative) {
1681 if (FormatTok->Type == TT_ConflictStart) {
1682 conditionalCompilationStart(/*Unreachable=*/false);
1683 } else if (FormatTok->Type == TT_ConflictAlternative) {
1684 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001685 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001686 conditionalCompilationEnd();
1687 }
1688 FormatTok = Tokens->getNextToken();
1689 FormatTok->MustBreakBefore = true;
1690 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001691
1692 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1693 !Line->InPPDirective) {
1694 continue;
1695 }
1696
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001697 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001698 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001699 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001700 CommentsInCurrentLine = false;
1701 }
1702 if (CommentsInCurrentLine) {
1703 pushToken(FormatTok);
1704 } else {
1705 CommentsBeforeNextToken.push_back(FormatTok);
1706 }
1707 } while (!eof());
1708}
1709
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001710void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001711 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001712 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001713 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001714 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001715 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001716}
1717
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001718} // end namespace format
1719} // end namespace clang