blob: 58a581e97edb31b383f2119f5e62b7a1c6c60946 [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
Manuel Klimekab3dc002013-01-16 12:31:12 +000016#define DEBUG_TYPE "format-parser"
Daniel Jasperf7935112012-12-03 18:12:45 +000017
Chandler Carruth4b417452013-01-19 08:09:44 +000018#include "UnwrappedLineParser.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000019#include "llvm/Support/Debug.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000020
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),
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000063 PreviousStructuralError(StructuralError), Token(NULL) {
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)
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000125 : Parser(Parser) {
126 OriginalLines = Parser.CurrentLines;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000127 if (SwitchToPreprocessorLines)
128 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000129 else if (!Parser.Line->Tokens.empty())
130 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000131 PreBlockLine = Parser.Line.release();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000132 Parser.Line.reset(new UnwrappedLine());
133 Parser.Line->Level = PreBlockLine->Level;
134 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000135 }
136
137 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000138 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000139 Parser.addUnwrappedLine();
140 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000141 assert(Parser.Line->Tokens.empty());
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000142 Parser.Line.reset(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000143 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
144 Parser.MustBreakBeforeNextToken = true;
145 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000146 }
147
148private:
149 UnwrappedLineParser &Parser;
150
151 UnwrappedLine *PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000152 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000153};
154
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000155class CompoundStatementIndenter {
156public:
157 CompoundStatementIndenter(UnwrappedLineParser *Parser,
158 const FormatStyle &Style, unsigned &LineLevel)
159 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
160 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
161 Parser->addUnwrappedLine();
162 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
163 Parser->addUnwrappedLine();
164 ++LineLevel;
165 }
166 }
167 ~CompoundStatementIndenter() {
168 LineLevel = OldLineLevel;
169 }
170
171private:
172 unsigned &LineLevel;
173 unsigned OldLineLevel;
174};
175
Craig Topper69665e12013-07-01 04:21:54 +0000176namespace {
177
Manuel Klimekab419912013-05-23 09:41:43 +0000178class IndexedTokenSource : public FormatTokenSource {
179public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000180 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000181 : Tokens(Tokens), Position(-1) {}
182
Craig Topperfb6b25b2014-03-15 04:29:04 +0000183 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000184 ++Position;
185 return Tokens[Position];
186 }
187
Craig Topperfb6b25b2014-03-15 04:29:04 +0000188 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000189 assert(Position >= 0);
190 return Position;
191 }
192
Craig Topperfb6b25b2014-03-15 04:29:04 +0000193 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000194 Position = P;
195 return Tokens[Position];
196 }
197
Manuel Klimek71814b42013-10-11 21:25:45 +0000198 void reset() { Position = -1; }
199
Manuel Klimekab419912013-05-23 09:41:43 +0000200private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000201 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000202 int Position;
203};
204
Craig Topper69665e12013-07-01 04:21:54 +0000205} // end anonymous namespace
206
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000207UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000208 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000209 UnwrappedLineConsumer &Callback)
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000210 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000211 CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL),
Manuel Klimek71814b42013-10-11 21:25:45 +0000212 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
213
214void UnwrappedLineParser::reset() {
215 PPBranchLevel = -1;
216 Line.reset(new UnwrappedLine);
217 CommentsBeforeNextToken.clear();
218 FormatTok = NULL;
219 MustBreakBeforeNextToken = false;
220 PreprocessorDirectives.clear();
221 CurrentLines = &Lines;
222 DeclarationScopeStack.clear();
223 StructuralError = false;
224 PPStack.clear();
225}
Daniel Jasperf7935112012-12-03 18:12:45 +0000226
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000227bool UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000228 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimek71814b42013-10-11 21:25:45 +0000229 do {
230 DEBUG(llvm::dbgs() << "----\n");
231 reset();
232 Tokens = &TokenSource;
233 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000234
Manuel Klimek71814b42013-10-11 21:25:45 +0000235 readToken();
236 parseFile();
237 // Create line with eof token.
238 pushToken(FormatTok);
239 addUnwrappedLine();
240
241 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
242 E = Lines.end();
243 I != E; ++I) {
244 Callback.consumeUnwrappedLine(*I);
245 }
246 Callback.finishRun();
247 Lines.clear();
248 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000249 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000250 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
251 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
252 }
253 if (!PPLevelBranchIndex.empty()) {
254 ++PPLevelBranchIndex.back();
255 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
256 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
257 }
258 } while (!PPLevelBranchIndex.empty());
259
Manuel Klimek1a18c402013-04-12 14:13:36 +0000260 return StructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000261}
262
Manuel Klimek1a18c402013-04-12 14:13:36 +0000263void UnwrappedLineParser::parseFile() {
Daniel Jasperdd9276e2013-03-22 16:55:40 +0000264 ScopedDeclarationState DeclarationState(
265 *Line, DeclarationScopeStack,
266 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber9096fc02013-06-26 00:30:14 +0000267 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000268 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000269 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000270 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000271}
272
Manuel Klimek1a18c402013-04-12 14:13:36 +0000273void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000274 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000275 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000276 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000277 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000278 nextToken();
279 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000280 break;
281 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000282 // FIXME: Add parameter whether this can happen - if this happens, we must
283 // be in a non-declaration context.
Nico Weber9096fc02013-06-26 00:30:14 +0000284 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000285 addUnwrappedLine();
286 break;
287 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000288 if (HasOpeningBrace)
289 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000290 StructuralError = true;
291 nextToken();
292 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000293 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000294 case tok::kw_default:
295 case tok::kw_case:
Daniel Jasper72407622013-09-02 08:26:29 +0000296 if (!SwitchLabelEncountered &&
297 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
298 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000299 SwitchLabelEncountered = true;
300 parseStructuralElement();
301 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000302 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000303 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000304 break;
305 }
306 } while (!eof());
307}
308
Manuel Klimekab419912013-05-23 09:41:43 +0000309void UnwrappedLineParser::calculateBraceTypes() {
310 // We'll parse forward through the tokens until we hit
311 // a closing brace or eof - note that getNextToken() will
312 // parse macros, so this will magically work inside macro
313 // definitions, too.
314 unsigned StoredPosition = Tokens->getPosition();
315 unsigned Position = StoredPosition;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000316 FormatToken *Tok = FormatTok;
Manuel Klimekab419912013-05-23 09:41:43 +0000317 // Keep a stack of positions of lbrace tokens. We will
318 // update information about whether an lbrace starts a
319 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000320 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000321 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000322 do {
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000323 // Get next none-comment token.
324 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000325 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000326 do {
327 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000328 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000329 } while (NextTok->is(tok::comment));
330
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000331 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000332 case tok::l_brace:
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000333 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000334 break;
335 case tok::r_brace:
336 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000337 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Manuel Klimekab419912013-05-23 09:41:43 +0000338 // If there is a comma, semicolon or right paren after the closing
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000339 // brace, we assume this is a braced initializer list. Note that
340 // regardless how we mark inner braces here, we will overwrite the
341 // BlockKind later if we parse a braced list (where all blocks inside
342 // are by default braced lists), or when we explicitly detect blocks
343 // (for example while parsing lambdas).
Daniel Jasper65b79822013-08-28 07:50:37 +0000344 //
345 // We exclude + and - as they can be ObjC visibility modifiers.
Daniel Jasperb631d5e2013-11-22 07:26:53 +0000346 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren, tok::period,
Daniel Jasperf3d97792013-09-13 10:55:31 +0000347 tok::r_square, tok::l_brace, tok::colon) ||
Daniel Jasper65b79822013-08-28 07:50:37 +0000348 (NextTok->isBinaryOperator() &&
349 !NextTok->isOneOf(tok::plus, tok::minus))) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000350 Tok->BlockKind = BK_BracedInit;
351 LBraceStack.back()->BlockKind = BK_BracedInit;
352 } else {
353 Tok->BlockKind = BK_Block;
354 LBraceStack.back()->BlockKind = BK_Block;
355 }
Manuel Klimekab419912013-05-23 09:41:43 +0000356 }
357 LBraceStack.pop_back();
358 }
359 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000360 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000361 case tok::semi:
362 case tok::kw_if:
363 case tok::kw_while:
364 case tok::kw_for:
365 case tok::kw_switch:
366 case tok::kw_try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000367 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000368 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000369 break;
370 default:
371 break;
372 }
373 Tok = NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000374 Position += ReadTokens;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000375 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000376 // Assume other blocks for all unclosed opening braces.
377 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000378 if (LBraceStack[i]->BlockKind == BK_Unknown)
379 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000380 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000381
Manuel Klimekab419912013-05-23 09:41:43 +0000382 FormatTok = Tokens->setPosition(StoredPosition);
383}
384
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000385void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
386 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000387 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000388 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000389 nextToken();
390
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000391 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000392
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000393 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
394 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000395 if (AddLevel)
396 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000397 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000398
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000399 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000400 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000401 StructuralError = true;
402 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000403 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000404
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000405 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000406 if (MunchSemi && FormatTok->Tok.is(tok::semi))
407 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000408 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000409}
410
Manuel Klimek516e0542013-09-04 13:25:30 +0000411void UnwrappedLineParser::parseChildBlock() {
412 FormatTok->BlockKind = BK_Block;
413 nextToken();
414 {
415 ScopedLineState LineState(*this);
416 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
417 /*MustBeDeclaration=*/false);
418 Line->Level += 1;
419 parseLevel(/*HasOpeningBrace=*/true);
420 Line->Level -= 1;
421 }
422 nextToken();
423}
424
Daniel Jasperf7935112012-12-03 18:12:45 +0000425void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000426 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000427 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000428 nextToken();
429
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000430 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000431 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000432 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000433 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000434
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000435 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000436 case tok::pp_define:
437 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000438 return;
439 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000440 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000441 break;
442 case tok::pp_ifdef:
443 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000444 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000445 break;
446 case tok::pp_else:
447 parsePPElse();
448 break;
449 case tok::pp_elif:
450 parsePPElIf();
451 break;
452 case tok::pp_endif:
453 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000454 break;
455 default:
456 parsePPUnknown();
457 break;
458 }
459}
460
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000461void UnwrappedLineParser::pushPPConditional() {
462 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
463 PPStack.push_back(PP_Unreachable);
464 else
465 PPStack.push_back(PP_Conditional);
466}
467
Manuel Klimek71814b42013-10-11 21:25:45 +0000468void UnwrappedLineParser::parsePPIf(bool IfDef) {
469 ++PPBranchLevel;
470 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
471 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
472 PPLevelBranchIndex.push_back(0);
473 PPLevelBranchCount.push_back(0);
474 }
475 PPChainBranchIndex.push(0);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000476 nextToken();
Manuel Klimek71814b42013-10-11 21:25:45 +0000477 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
478 StringRef(FormatTok->Tok.getLiteralData(),
479 FormatTok->Tok.getLength()) == "0") ||
480 FormatTok->Tok.is(tok::kw_false);
481 if ((!IfDef && IsLiteralFalse) || PPLevelBranchIndex[PPBranchLevel] > 0) {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000482 PPStack.push_back(PP_Unreachable);
483 } else {
484 pushPPConditional();
485 }
486 parsePPUnknown();
487}
488
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000489void UnwrappedLineParser::parsePPElse() {
490 if (!PPStack.empty())
491 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000492 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
493 if (!PPChainBranchIndex.empty())
494 ++PPChainBranchIndex.top();
495 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
496 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()) {
497 PPStack.push_back(PP_Unreachable);
498 } else {
499 pushPPConditional();
500 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000501 parsePPUnknown();
502}
503
Daniel Jasper393564f2013-05-31 14:56:29 +0000504void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000505
506void UnwrappedLineParser::parsePPEndIf() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000507 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
508 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
509 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000510 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
511 }
512 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000513 // Guard against #endif's without #if.
514 if (PPBranchLevel > 0)
515 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000516 if (!PPChainBranchIndex.empty())
517 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000518 if (!PPStack.empty())
519 PPStack.pop_back();
520 parsePPUnknown();
521}
522
Manuel Klimek1abf7892013-01-04 23:34:14 +0000523void UnwrappedLineParser::parsePPDefine() {
524 nextToken();
525
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000526 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000527 parsePPUnknown();
528 return;
529 }
530 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000531 if (FormatTok->Tok.getKind() == tok::l_paren &&
532 FormatTok->WhitespaceRange.getBegin() ==
533 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000534 parseParens();
535 }
536 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000537 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000538
539 // Errors during a preprocessor directive can only affect the layout of the
540 // preprocessor directive, and thus we ignore them. An alternative approach
541 // would be to use the same approach we use on the file level (no
542 // re-indentation if there was a structural error) within the macro
543 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000544 parseFile();
545}
546
547void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000548 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000549 nextToken();
550 } while (!eof());
551 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000552}
553
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000554// Here we blacklist certain tokens that are not usually the first token in an
555// unwrapped line. This is used in attempt to distinguish macro calls without
556// trailing semicolons from other constructs split to several lines.
557bool tokenCanStartNewLine(clang::Token Tok) {
558 // Semicolon can be a null-statement, l_square can be a start of a macro or
559 // a C++11 attribute, but this doesn't seem to be common.
560 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
561 Tok.isNot(tok::l_square) &&
562 // Tokens that can only be used as binary operators and a part of
563 // overloaded operator names.
564 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
565 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
566 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
567 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
568 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
569 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
570 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
571 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
572 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
573 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
574 Tok.isNot(tok::lesslessequal) &&
575 // Colon is used in labels, base class lists, initializer lists,
576 // range-based for loops, ternary operator, but should never be the
577 // first token in an unwrapped line.
578 Tok.isNot(tok::colon);
579}
580
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000581void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000582 assert(!FormatTok->Tok.is(tok::l_brace));
583 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000584 case tok::at:
585 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000586 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000587 parseBracedList();
588 break;
589 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000590 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000591 case tok::objc_public:
592 case tok::objc_protected:
593 case tok::objc_package:
594 case tok::objc_private:
595 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000596 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000597 case tok::objc_implementation:
598 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000599 case tok::objc_protocol:
600 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000601 case tok::objc_end:
602 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000603 case tok::objc_optional:
604 case tok::objc_required:
605 nextToken();
606 addUnwrappedLine();
607 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000608 default:
609 break;
610 }
611 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000612 case tok::kw_namespace:
613 parseNamespace();
614 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000615 case tok::kw_inline:
616 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000617 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000618 parseNamespace();
619 return;
620 }
621 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000622 case tok::kw_public:
623 case tok::kw_protected:
624 case tok::kw_private:
Daniel Jasperf7935112012-12-03 18:12:45 +0000625 parseAccessSpecifier();
626 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000627 case tok::kw_if:
628 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000629 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000630 case tok::kw_for:
631 case tok::kw_while:
632 parseForOrWhileLoop();
633 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000634 case tok::kw_do:
635 parseDoWhile();
636 return;
637 case tok::kw_switch:
638 parseSwitch();
639 return;
640 case tok::kw_default:
641 nextToken();
642 parseLabel();
643 return;
644 case tok::kw_case:
645 parseCaseLabel();
646 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000647 case tok::kw_extern:
648 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000649 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000650 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000651 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000652 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000653 addUnwrappedLine();
654 return;
655 }
656 }
657 // In all other cases, parse the declaration.
658 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000659 default:
660 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000661 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000662 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000663 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000664 case tok::at:
665 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000666 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000667 parseBracedList();
668 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000669 case tok::kw_enum:
670 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000671 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000672 case tok::kw_typedef:
673 nextToken();
674 // FIXME: Use the IdentifierTable instead.
675 if (FormatTok->TokenText == "NS_ENUM")
676 parseEnum();
677 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000678 case tok::kw_struct:
679 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000680 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000681 parseRecord();
682 // A record declaration or definition is always the start of a structural
683 // element.
684 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000685 case tok::semi:
686 nextToken();
687 addUnwrappedLine();
688 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000689 case tok::r_brace:
690 addUnwrappedLine();
691 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000692 case tok::l_paren:
693 parseParens();
694 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000695 case tok::caret:
696 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000697 if (FormatTok->Tok.isAnyIdentifier() ||
698 FormatTok->isSimpleTypeSpecifier())
699 nextToken();
700 if (FormatTok->is(tok::l_paren))
701 parseParens();
702 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000703 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000704 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000705 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000706 if (!tryToParseBracedList()) {
707 // A block outside of parentheses must be the last part of a
708 // structural element.
709 // FIXME: Figure out cases where this is not true, and add projections
710 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000711 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000712 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000713 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000714 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000715 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000716 return;
717 }
718 // Otherwise this was a braced init list, and the structural
719 // element continues.
720 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000721 case tok::identifier: {
722 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +0000723 nextToken();
Alexander Kornienkode644272013-04-08 22:16:06 +0000724 if (Line->Tokens.size() == 1) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000725 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000726 parseLabel();
727 return;
728 }
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000729 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000730 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000731 parseParens();
Daniel Jasper352dae12014-01-03 11:50:46 +0000732 if (FormatTok->NewlinesBefore > 0 &&
Alexander Kornienkoce081262014-03-18 14:35:20 +0000733 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000734 addUnwrappedLine();
735 return;
736 }
Daniel Jasper40e19212013-05-29 13:16:10 +0000737 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
738 Text == Text.upper()) {
739 // Recognize free-standing macros like Q_OBJECT.
740 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000741 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000742 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000743 }
744 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000745 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000746 case tok::equal:
747 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000748 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000749 parseBracedList();
750 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000751 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000752 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000753 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000754 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000755 default:
756 nextToken();
757 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000758 }
759 } while (!eof());
760}
761
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000762bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000763 // FIXME: This is a dirty way to access the previous token. Find a better
764 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000765 if (!Line->Tokens.empty() &&
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000766 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator) ||
767 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000768 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000769 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000770 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000771 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000772 assert(FormatTok->is(tok::l_square));
773 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000774 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000775 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000776
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000777 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000778 if (FormatTok->isSimpleTypeSpecifier()) {
779 nextToken();
780 continue;
781 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000782 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000783 case tok::l_brace:
784 break;
785 case tok::l_paren:
786 parseParens();
787 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000788 case tok::less:
789 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000790 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000791 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000792 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000793 nextToken();
794 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000795 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000796 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000797 nextToken();
798 break;
799 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000800 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000801 }
802 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000803 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000804 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000805 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000806}
807
808bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
809 nextToken();
810 if (FormatTok->is(tok::equal)) {
811 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000812 if (FormatTok->is(tok::r_square)) {
813 nextToken();
814 return true;
815 }
816 if (FormatTok->isNot(tok::comma))
817 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000818 nextToken();
819 } else if (FormatTok->is(tok::amp)) {
820 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000821 if (FormatTok->is(tok::r_square)) {
822 nextToken();
823 return true;
824 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000825 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
826 return false;
827 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000828 if (FormatTok->is(tok::comma))
829 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000830 } else if (FormatTok->is(tok::r_square)) {
831 nextToken();
832 return true;
833 }
834 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000835 if (FormatTok->is(tok::amp))
836 nextToken();
837 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
838 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000839 nextToken();
840 if (FormatTok->is(tok::comma)) {
841 nextToken();
842 } else if (FormatTok->is(tok::r_square)) {
843 nextToken();
844 return true;
845 } else {
846 return false;
847 }
848 } while (!eof());
849 return false;
850}
851
Manuel Klimekab419912013-05-23 09:41:43 +0000852bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000853 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +0000854 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000855 assert(FormatTok->BlockKind != BK_Unknown);
856 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +0000857 return false;
858 parseBracedList();
859 return true;
860}
861
Daniel Jasper015ed022013-09-13 09:20:45 +0000862bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
863 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000864 nextToken();
865
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000866 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
867 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000868 do {
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000869 // FIXME: When we start to support lambdas, we'll want to parse them away
870 // here, otherwise our bail-out scenarios below break. The better solution
871 // might be to just implement a more or less complete expression parser.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000872 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +0000873 case tok::caret:
874 nextToken();
875 if (FormatTok->is(tok::l_brace)) {
876 parseChildBlock();
877 }
878 break;
879 case tok::l_square:
880 tryToParseLambda();
881 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000882 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000883 // Assume there are no blocks inside a braced init list apart
884 // from the ones we explicitly parse out (like lambdas).
885 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000886 parseBracedList();
887 break;
888 case tok::r_brace:
889 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +0000890 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000891 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +0000892 HasError = true;
893 if (!ContinueOnSemicolons)
894 return !HasError;
895 nextToken();
896 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000897 case tok::comma:
898 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000899 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000900 default:
901 nextToken();
902 break;
903 }
904 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +0000905 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000906}
907
Daniel Jasperf7935112012-12-03 18:12:45 +0000908void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000909 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +0000910 nextToken();
911 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000912 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000913 case tok::l_paren:
914 parseParens();
915 break;
916 case tok::r_paren:
917 nextToken();
918 return;
Daniel Jasper393564f2013-05-31 14:56:29 +0000919 case tok::r_brace:
920 // A "}" inside parenthesis is an error if there wasn't a matching "{".
921 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000922 case tok::l_square:
923 tryToParseLambda();
924 break;
Nico Weber67ffb332013-02-10 04:38:23 +0000925 case tok::l_brace: {
Manuel Klimekab419912013-05-23 09:41:43 +0000926 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +0000927 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +0000928 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000929 break;
Nico Weber67ffb332013-02-10 04:38:23 +0000930 }
Nico Weber372d8dc2013-02-10 20:35:35 +0000931 case tok::at:
932 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000933 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000934 parseBracedList();
935 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000936 default:
937 nextToken();
938 break;
939 }
940 } while (!eof());
941}
942
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000943void UnwrappedLineParser::parseSquare() {
944 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
945 if (tryToParseLambda())
946 return;
947 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000948 switch (FormatTok->Tok.getKind()) {
949 case tok::l_paren:
950 parseParens();
951 break;
952 case tok::r_square:
953 nextToken();
954 return;
955 case tok::r_brace:
956 // A "}" inside parenthesis is an error if there wasn't a matching "{".
957 return;
958 case tok::l_square:
959 parseSquare();
960 break;
961 case tok::l_brace: {
962 if (!tryToParseBracedList()) {
963 parseChildBlock();
964 }
965 break;
966 }
967 case tok::at:
968 nextToken();
969 if (FormatTok->Tok.is(tok::l_brace))
970 parseBracedList();
971 break;
972 default:
973 nextToken();
974 break;
975 }
976 } while (!eof());
977}
978
Daniel Jasperf7935112012-12-03 18:12:45 +0000979void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000980 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +0000981 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000982 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +0000983 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +0000984 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000985 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000986 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +0000987 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000988 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
989 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +0000990 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000991 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +0000992 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000993 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000994 } else {
995 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000996 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000997 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +0000998 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000999 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001000 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001001 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001002 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001003 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001004 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001005 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001006 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001007 parseIfThenElse();
1008 } else {
1009 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001010 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001011 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001012 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001013 }
1014 } else if (NeedsUnwrappedLine) {
1015 addUnwrappedLine();
1016 }
1017}
1018
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001019void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001020 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001021 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001022 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001023 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001024 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001025 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001026 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1027 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001028 addUnwrappedLine();
1029
Daniel Jasper65ee3472013-07-31 23:16:02 +00001030 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1031 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1032 DeclarationScopeStack.size() > 1);
1033 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001034 // Munch the semicolon after a namespace. This is more common than one would
1035 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001036 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001037 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001038 addUnwrappedLine();
1039 }
1040 // FIXME: Add error handling.
1041}
1042
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001043void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001044 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001045 "'for' or 'while' expected");
1046 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001047 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001048 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001049 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001050 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001051 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001052 addUnwrappedLine();
1053 } else {
1054 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001055 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001056 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001057 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001058 }
1059}
1060
Daniel Jasperf7935112012-12-03 18:12:45 +00001061void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001062 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001063 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001064 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001065 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001066 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001067 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1068 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001069 } else {
1070 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001071 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001072 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001073 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001074 }
1075
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001076 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001077 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001078 addUnwrappedLine();
1079 return;
1080 }
1081
Daniel Jasperf7935112012-12-03 18:12:45 +00001082 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001083 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001084}
1085
1086void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001087 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001088 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001089 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001090 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001091 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001092 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001093 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001094 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001095 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1096 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1097 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001098 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001099 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001100 parseStructuralElement();
1101 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001102 addUnwrappedLine();
1103 } else {
1104 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001105 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001106 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001107}
1108
1109void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001110 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001111 // FIXME: fix handling of complex expressions here.
1112 do {
1113 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001114 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001115 parseLabel();
1116}
1117
1118void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001119 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001120 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001121 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001122 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001123 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001124 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001125 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001126 addUnwrappedLine();
1127 } else {
1128 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001129 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001130 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001131 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001132 }
1133}
1134
1135void UnwrappedLineParser::parseAccessSpecifier() {
1136 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001137 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001138 if (FormatTok->is(tok::identifier) &&
1139 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001140 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001141 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001142 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001143 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001144 addUnwrappedLine();
1145}
1146
1147void UnwrappedLineParser::parseEnum() {
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001148 if (FormatTok->Tok.is(tok::kw_enum)) {
1149 // Won't be 'enum' for NS_ENUMs.
1150 nextToken();
1151 }
Daniel Jasper2b41a822013-08-20 12:42:50 +00001152 // Eat up enum class ...
1153 if (FormatTok->Tok.is(tok::kw_class) ||
1154 FormatTok->Tok.is(tok::kw_struct))
1155 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001156 while (FormatTok->Tok.getIdentifierInfo() ||
1157 FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001158 nextToken();
1159 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001160 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001161 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001162 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001163 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001164 nextToken();
1165 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001166 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001167 FormatTok->BlockKind = BK_Block;
1168 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1169 if (HasError) {
1170 if (FormatTok->is(tok::semi))
1171 nextToken();
Manuel Klimeka027f302013-08-07 19:20:45 +00001172 addUnwrappedLine();
Daniel Jasper015ed022013-09-13 09:20:45 +00001173 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001174 }
1175 // We fall through to parsing a structural element afterwards, so that in
1176 // enum A {} n, m;
1177 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperf7935112012-12-03 18:12:45 +00001178}
1179
Manuel Klimeke01bab52013-01-15 13:38:33 +00001180void UnwrappedLineParser::parseRecord() {
Manuel Klimek28cacc72013-01-07 18:10:23 +00001181 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001182 if (FormatTok->Tok.is(tok::identifier) ||
1183 FormatTok->Tok.is(tok::kw___attribute) ||
Manuel Klimek91e48582013-10-07 09:15:41 +00001184 FormatTok->Tok.is(tok::kw___declspec) ||
1185 FormatTok->Tok.is(tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001186 nextToken();
1187 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001188 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001189 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001190 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001191 // The actual identifier can be a nested name specifier, and in macros
1192 // it is often token-pasted.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001193 while (FormatTok->Tok.is(tok::identifier) ||
1194 FormatTok->Tok.is(tok::coloncolon) ||
1195 FormatTok->Tok.is(tok::hashhash))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001196 nextToken();
1197
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001198 // Note that parsing away template declarations here leads to incorrectly
1199 // accepting function declarations as record declarations.
1200 // In general, we cannot solve this problem. Consider:
1201 // class A<int> B() {}
1202 // which can be a function definition or a class definition when B() is a
1203 // macro. If we find enough real-world cases where this is a problem, we
1204 // can parse for the 'template' keyword in the beginning of the statement,
1205 // and thus rule out the record production in case there is no template
1206 // (this would still leave us with an ambiguity between template function
1207 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001208 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1209 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1210 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001211 return;
1212 nextToken();
1213 }
1214 }
1215 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001216 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001217 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001218 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1219 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001220 addUnwrappedLine();
1221
Daniel Jasper240dfda2014-03-31 14:23:49 +00001222 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001223 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001224 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001225 // We fall through to parsing a structural element afterwards, so
1226 // class A {} n, m;
1227 // will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00001228}
1229
Nico Weber8696a8d2013-01-09 21:15:03 +00001230void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001231 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001232 do
1233 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001234 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001235 nextToken(); // Skip '>'.
1236}
1237
1238void UnwrappedLineParser::parseObjCUntilAtEnd() {
1239 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001240 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001241 nextToken();
1242 addUnwrappedLine();
1243 break;
1244 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001245 if (FormatTok->is(tok::l_brace)) {
1246 parseBlock(/*MustBeDeclaration=*/false);
1247 // In ObjC interfaces, nothing should be following the "}".
1248 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001249 } else if (FormatTok->is(tok::r_brace)) {
1250 // Ignore stray "}". parseStructuralElement doesn't consume them.
1251 nextToken();
1252 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001253 } else {
1254 parseStructuralElement();
1255 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001256 } while (!eof());
1257}
1258
Nico Weber2ce0ac52013-01-09 23:25:37 +00001259void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001260 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001261 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001262
1263 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001264 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001265 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001266 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001267 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001268 // Skip category, if present.
1269 parseParens();
1270
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001271 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001272 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001273
1274 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001275 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber9096fc02013-06-26 00:30:14 +00001276 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber7eecf4b2013-01-09 20:25:35 +00001277
1278 // With instance variables, this puts '}' on its own line. Without instance
1279 // variables, this ends the @interface line.
1280 addUnwrappedLine();
1281
Nico Weber8696a8d2013-01-09 21:15:03 +00001282 parseObjCUntilAtEnd();
1283}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001284
Nico Weber8696a8d2013-01-09 21:15:03 +00001285void UnwrappedLineParser::parseObjCProtocol() {
1286 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001287 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001288
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001289 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001290 parseObjCProtocolList();
1291
1292 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001293 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001294 nextToken();
1295 return addUnwrappedLine();
1296 }
1297
1298 addUnwrappedLine();
1299 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001300}
1301
Daniel Jasper3b203a62013-09-05 16:05:56 +00001302LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1303 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001304 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1305 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1306 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1307 E = Line.Tokens.end();
1308 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001309 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001310 }
1311 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1312 E = Line.Tokens.end();
1313 I != E; ++I) {
1314 const UnwrappedLineNode &Node = *I;
1315 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1316 I = Node.Children.begin(),
1317 E = Node.Children.end();
1318 I != E; ++I) {
1319 printDebugInfo(*I, "\nChild: ");
1320 }
1321 }
1322 llvm::dbgs() << "\n";
1323}
1324
Daniel Jasperf7935112012-12-03 18:12:45 +00001325void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001326 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001327 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001328 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001329 if (CurrentLines == &Lines)
1330 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001331 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001332 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001333 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001334 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001335 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001336 I = PreprocessorDirectives.begin(),
1337 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001338 I != E; ++I) {
1339 CurrentLines->push_back(*I);
1340 }
1341 PreprocessorDirectives.clear();
1342 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001343}
1344
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001345bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001346
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001347void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1348 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001349 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001350 I = CommentsBeforeNextToken.begin(),
1351 E = CommentsBeforeNextToken.end();
1352 I != E; ++I) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001353 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001354 addUnwrappedLine();
1355 }
1356 pushToken(*I);
1357 }
1358 if (NewlineBeforeNext && JustComments) {
1359 addUnwrappedLine();
1360 }
1361 CommentsBeforeNextToken.clear();
1362}
1363
Daniel Jasperf7935112012-12-03 18:12:45 +00001364void UnwrappedLineParser::nextToken() {
1365 if (eof())
1366 return;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001367 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001368 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001369 readToken();
1370}
1371
1372void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001373 bool CommentsInCurrentLine = true;
1374 do {
1375 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001376 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001377 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1378 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001379 // If there is an unfinished unwrapped line, we flush the preprocessor
1380 // directives only after that unwrapped line was finished later.
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001381 bool SwitchToPreprocessorLines =
1382 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001383 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001384 // Comments stored before the preprocessor directive need to be output
1385 // before the preprocessor directive, at the same level as the
1386 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001387 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001388 parsePPDirective();
1389 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001390
1391 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1392 !Line->InPPDirective) {
1393 continue;
1394 }
1395
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001396 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001397 return;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001398 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001399 CommentsInCurrentLine = false;
1400 }
1401 if (CommentsInCurrentLine) {
1402 pushToken(FormatTok);
1403 } else {
1404 CommentsBeforeNextToken.push_back(FormatTok);
1405 }
1406 } while (!eof());
1407}
1408
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001409void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001410 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001411 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001412 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001413 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001414 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001415}
1416
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001417} // end namespace format
1418} // end namespace clang