blob: ef55b68194020b8219433c86e8d5d0b63d856072 [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
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000077 virtual FormatToken *getNextToken() {
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
Daniel Jasper393564f2013-05-31 14:56:29 +000087 virtual unsigned getPosition() { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +000088
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000089 virtual FormatToken *setPosition(unsigned Position) {
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;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000131 PreBlockLine = Parser.Line.take();
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
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000183 virtual FormatToken *getNextToken() {
Manuel Klimekab419912013-05-23 09:41:43 +0000184 ++Position;
185 return Tokens[Position];
186 }
187
188 virtual unsigned getPosition() {
189 assert(Position >= 0);
190 return Position;
191 }
192
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000193 virtual FormatToken *setPosition(unsigned P) {
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;
360 case tok::semi:
361 case tok::kw_if:
362 case tok::kw_while:
363 case tok::kw_for:
364 case tok::kw_switch:
365 case tok::kw_try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000366 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000367 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000368 break;
369 default:
370 break;
371 }
372 Tok = NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000373 Position += ReadTokens;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000374 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000375 // Assume other blocks for all unclosed opening braces.
376 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000377 if (LBraceStack[i]->BlockKind == BK_Unknown)
378 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000379 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000380
Manuel Klimekab419912013-05-23 09:41:43 +0000381 FormatTok = Tokens->setPosition(StoredPosition);
382}
383
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000384void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
385 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000386 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000387 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000388 nextToken();
389
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000390 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000391
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000392 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
393 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000394 if (AddLevel)
395 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000396 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000397
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000398 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000399 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000400 StructuralError = true;
401 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000402 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000403
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000404 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000405 if (MunchSemi && FormatTok->Tok.is(tok::semi))
406 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000407 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000408}
409
Manuel Klimek516e0542013-09-04 13:25:30 +0000410void UnwrappedLineParser::parseChildBlock() {
411 FormatTok->BlockKind = BK_Block;
412 nextToken();
413 {
414 ScopedLineState LineState(*this);
415 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
416 /*MustBeDeclaration=*/false);
417 Line->Level += 1;
418 parseLevel(/*HasOpeningBrace=*/true);
419 Line->Level -= 1;
420 }
421 nextToken();
422}
423
Daniel Jasperf7935112012-12-03 18:12:45 +0000424void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000425 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000426 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000427 nextToken();
428
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000429 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000430 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000431 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000432 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000433
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000434 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000435 case tok::pp_define:
436 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000437 return;
438 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000439 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000440 break;
441 case tok::pp_ifdef:
442 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000443 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000444 break;
445 case tok::pp_else:
446 parsePPElse();
447 break;
448 case tok::pp_elif:
449 parsePPElIf();
450 break;
451 case tok::pp_endif:
452 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000453 break;
454 default:
455 parsePPUnknown();
456 break;
457 }
458}
459
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000460void UnwrappedLineParser::pushPPConditional() {
461 if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
462 PPStack.push_back(PP_Unreachable);
463 else
464 PPStack.push_back(PP_Conditional);
465}
466
Manuel Klimek71814b42013-10-11 21:25:45 +0000467void UnwrappedLineParser::parsePPIf(bool IfDef) {
468 ++PPBranchLevel;
469 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
470 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
471 PPLevelBranchIndex.push_back(0);
472 PPLevelBranchCount.push_back(0);
473 }
474 PPChainBranchIndex.push(0);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000475 nextToken();
Manuel Klimek71814b42013-10-11 21:25:45 +0000476 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
477 StringRef(FormatTok->Tok.getLiteralData(),
478 FormatTok->Tok.getLength()) == "0") ||
479 FormatTok->Tok.is(tok::kw_false);
480 if ((!IfDef && IsLiteralFalse) || PPLevelBranchIndex[PPBranchLevel] > 0) {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000481 PPStack.push_back(PP_Unreachable);
482 } else {
483 pushPPConditional();
484 }
485 parsePPUnknown();
486}
487
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000488void UnwrappedLineParser::parsePPElse() {
489 if (!PPStack.empty())
490 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000491 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
492 if (!PPChainBranchIndex.empty())
493 ++PPChainBranchIndex.top();
494 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
495 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()) {
496 PPStack.push_back(PP_Unreachable);
497 } else {
498 pushPPConditional();
499 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000500 parsePPUnknown();
501}
502
Daniel Jasper393564f2013-05-31 14:56:29 +0000503void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000504
505void UnwrappedLineParser::parsePPEndIf() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000506 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
507 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
508 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000509 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
510 }
511 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000512 // Guard against #endif's without #if.
513 if (PPBranchLevel > 0)
514 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000515 if (!PPChainBranchIndex.empty())
516 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000517 if (!PPStack.empty())
518 PPStack.pop_back();
519 parsePPUnknown();
520}
521
Manuel Klimek1abf7892013-01-04 23:34:14 +0000522void UnwrappedLineParser::parsePPDefine() {
523 nextToken();
524
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000525 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000526 parsePPUnknown();
527 return;
528 }
529 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000530 if (FormatTok->Tok.getKind() == tok::l_paren &&
531 FormatTok->WhitespaceRange.getBegin() ==
532 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000533 parseParens();
534 }
535 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000536 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000537
538 // Errors during a preprocessor directive can only affect the layout of the
539 // preprocessor directive, and thus we ignore them. An alternative approach
540 // would be to use the same approach we use on the file level (no
541 // re-indentation if there was a structural error) within the macro
542 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000543 parseFile();
544}
545
546void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000547 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000548 nextToken();
549 } while (!eof());
550 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000551}
552
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000553// Here we blacklist certain tokens that are not usually the first token in an
554// unwrapped line. This is used in attempt to distinguish macro calls without
555// trailing semicolons from other constructs split to several lines.
556bool tokenCanStartNewLine(clang::Token Tok) {
557 // Semicolon can be a null-statement, l_square can be a start of a macro or
558 // a C++11 attribute, but this doesn't seem to be common.
559 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
560 Tok.isNot(tok::l_square) &&
561 // Tokens that can only be used as binary operators and a part of
562 // overloaded operator names.
563 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
564 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
565 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
566 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
567 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
568 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
569 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
570 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
571 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
572 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
573 Tok.isNot(tok::lesslessequal) &&
574 // Colon is used in labels, base class lists, initializer lists,
575 // range-based for loops, ternary operator, but should never be the
576 // first token in an unwrapped line.
577 Tok.isNot(tok::colon);
578}
579
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000580void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000581 assert(!FormatTok->Tok.is(tok::l_brace));
582 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000583 case tok::at:
584 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000585 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000586 parseBracedList();
587 break;
588 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000589 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000590 case tok::objc_public:
591 case tok::objc_protected:
592 case tok::objc_package:
593 case tok::objc_private:
594 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000595 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000596 case tok::objc_implementation:
597 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000598 case tok::objc_protocol:
599 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000600 case tok::objc_end:
601 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000602 case tok::objc_optional:
603 case tok::objc_required:
604 nextToken();
605 addUnwrappedLine();
606 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000607 default:
608 break;
609 }
610 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000611 case tok::kw_namespace:
612 parseNamespace();
613 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000614 case tok::kw_inline:
615 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000616 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000617 parseNamespace();
618 return;
619 }
620 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000621 case tok::kw_public:
622 case tok::kw_protected:
623 case tok::kw_private:
Daniel Jasperf7935112012-12-03 18:12:45 +0000624 parseAccessSpecifier();
625 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000626 case tok::kw_if:
627 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000628 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000629 case tok::kw_for:
630 case tok::kw_while:
631 parseForOrWhileLoop();
632 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000633 case tok::kw_do:
634 parseDoWhile();
635 return;
636 case tok::kw_switch:
637 parseSwitch();
638 return;
639 case tok::kw_default:
640 nextToken();
641 parseLabel();
642 return;
643 case tok::kw_case:
644 parseCaseLabel();
645 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000646 case tok::kw_extern:
647 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000648 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000649 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000650 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000651 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000652 addUnwrappedLine();
653 return;
654 }
655 }
656 // In all other cases, parse the declaration.
657 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000658 default:
659 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000660 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000661 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000662 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000663 case tok::at:
664 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000665 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000666 parseBracedList();
667 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000668 case tok::kw_enum:
669 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000670 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000671 case tok::kw_typedef:
672 nextToken();
673 // FIXME: Use the IdentifierTable instead.
674 if (FormatTok->TokenText == "NS_ENUM")
675 parseEnum();
676 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000677 case tok::kw_struct:
678 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000679 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000680 parseRecord();
681 // A record declaration or definition is always the start of a structural
682 // element.
683 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000684 case tok::semi:
685 nextToken();
686 addUnwrappedLine();
687 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000688 case tok::r_brace:
689 addUnwrappedLine();
690 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000691 case tok::l_paren:
692 parseParens();
693 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000694 case tok::caret:
695 nextToken();
696 if (FormatTok->is(tok::l_brace)) {
697 parseChildBlock();
698 }
699 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000700 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000701 if (!tryToParseBracedList()) {
702 // A block outside of parentheses must be the last part of a
703 // structural element.
704 // FIXME: Figure out cases where this is not true, and add projections
705 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000706 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000707 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000708 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000709 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000710 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000711 return;
712 }
713 // Otherwise this was a braced init list, and the structural
714 // element continues.
715 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000716 case tok::identifier: {
717 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +0000718 nextToken();
Alexander Kornienkode644272013-04-08 22:16:06 +0000719 if (Line->Tokens.size() == 1) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000720 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000721 parseLabel();
722 return;
723 }
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000724 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000725 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000726 parseParens();
Daniel Jasper352dae12014-01-03 11:50:46 +0000727 if (FormatTok->NewlinesBefore > 0 &&
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000728 tokenCanStartNewLine(FormatTok->Tok)) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000729 addUnwrappedLine();
730 return;
731 }
Daniel Jasper40e19212013-05-29 13:16:10 +0000732 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
733 Text == Text.upper()) {
734 // Recognize free-standing macros like Q_OBJECT.
735 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000736 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000737 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000738 }
739 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000740 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000741 case tok::equal:
742 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000743 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000744 parseBracedList();
745 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000746 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000747 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000748 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000749 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000750 default:
751 nextToken();
752 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000753 }
754 } while (!eof());
755}
756
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000757bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000758 // FIXME: This is a dirty way to access the previous token. Find a better
759 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000760 if (!Line->Tokens.empty() &&
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000761 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator) ||
762 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000763 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000764 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000765 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000766 assert(FormatTok->is(tok::l_square));
767 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000768 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000769 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000770
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000771 while (FormatTok && FormatTok->isNot(tok::l_brace)) {
772 if (FormatTok->isSimpleTypeSpecifier()) {
773 nextToken();
774 continue;
775 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000776 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000777 case tok::l_brace:
778 break;
779 case tok::l_paren:
780 parseParens();
781 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000782 case tok::less:
783 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000784 case tok::identifier:
785 case tok::kw_mutable:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000786 case tok::arrow:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000787 nextToken();
788 break;
789 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000790 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000791 }
792 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000793 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000794 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000795 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000796}
797
798bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
799 nextToken();
800 if (FormatTok->is(tok::equal)) {
801 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000802 if (FormatTok->is(tok::r_square)) {
803 nextToken();
804 return true;
805 }
806 if (FormatTok->isNot(tok::comma))
807 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000808 nextToken();
809 } else if (FormatTok->is(tok::amp)) {
810 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000811 if (FormatTok->is(tok::r_square)) {
812 nextToken();
813 return true;
814 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000815 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
816 return false;
817 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000818 if (FormatTok->is(tok::comma))
819 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000820 } else if (FormatTok->is(tok::r_square)) {
821 nextToken();
822 return true;
823 }
824 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000825 if (FormatTok->is(tok::amp))
826 nextToken();
827 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
828 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000829 nextToken();
830 if (FormatTok->is(tok::comma)) {
831 nextToken();
832 } else if (FormatTok->is(tok::r_square)) {
833 nextToken();
834 return true;
835 } else {
836 return false;
837 }
838 } while (!eof());
839 return false;
840}
841
Manuel Klimekab419912013-05-23 09:41:43 +0000842bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000843 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +0000844 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000845 assert(FormatTok->BlockKind != BK_Unknown);
846 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +0000847 return false;
848 parseBracedList();
849 return true;
850}
851
Daniel Jasper015ed022013-09-13 09:20:45 +0000852bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
853 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000854 nextToken();
855
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000856 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
857 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000858 do {
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000859 // FIXME: When we start to support lambdas, we'll want to parse them away
860 // here, otherwise our bail-out scenarios below break. The better solution
861 // might be to just implement a more or less complete expression parser.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000862 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +0000863 case tok::caret:
864 nextToken();
865 if (FormatTok->is(tok::l_brace)) {
866 parseChildBlock();
867 }
868 break;
869 case tok::l_square:
870 tryToParseLambda();
871 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000872 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000873 // Assume there are no blocks inside a braced init list apart
874 // from the ones we explicitly parse out (like lambdas).
875 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000876 parseBracedList();
877 break;
878 case tok::r_brace:
879 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +0000880 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000881 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +0000882 HasError = true;
883 if (!ContinueOnSemicolons)
884 return !HasError;
885 nextToken();
886 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000887 case tok::comma:
888 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +0000889 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000890 default:
891 nextToken();
892 break;
893 }
894 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +0000895 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000896}
897
Daniel Jasperf7935112012-12-03 18:12:45 +0000898void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000899 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +0000900 nextToken();
901 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000902 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000903 case tok::l_paren:
904 parseParens();
905 break;
906 case tok::r_paren:
907 nextToken();
908 return;
Daniel Jasper393564f2013-05-31 14:56:29 +0000909 case tok::r_brace:
910 // A "}" inside parenthesis is an error if there wasn't a matching "{".
911 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000912 case tok::l_square:
913 tryToParseLambda();
914 break;
Nico Weber67ffb332013-02-10 04:38:23 +0000915 case tok::l_brace: {
Manuel Klimekab419912013-05-23 09:41:43 +0000916 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +0000917 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +0000918 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000919 break;
Nico Weber67ffb332013-02-10 04:38:23 +0000920 }
Nico Weber372d8dc2013-02-10 20:35:35 +0000921 case tok::at:
922 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000923 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000924 parseBracedList();
925 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000926 default:
927 nextToken();
928 break;
929 }
930 } while (!eof());
931}
932
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000933void UnwrappedLineParser::parseSquare() {
934 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
935 if (tryToParseLambda())
936 return;
937 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000938 switch (FormatTok->Tok.getKind()) {
939 case tok::l_paren:
940 parseParens();
941 break;
942 case tok::r_square:
943 nextToken();
944 return;
945 case tok::r_brace:
946 // A "}" inside parenthesis is an error if there wasn't a matching "{".
947 return;
948 case tok::l_square:
949 parseSquare();
950 break;
951 case tok::l_brace: {
952 if (!tryToParseBracedList()) {
953 parseChildBlock();
954 }
955 break;
956 }
957 case tok::at:
958 nextToken();
959 if (FormatTok->Tok.is(tok::l_brace))
960 parseBracedList();
961 break;
962 default:
963 nextToken();
964 break;
965 }
966 } while (!eof());
967}
968
Daniel Jasperf7935112012-12-03 18:12:45 +0000969void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000970 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +0000971 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000972 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +0000973 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +0000974 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000975 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000976 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +0000977 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000978 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
979 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +0000980 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000981 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +0000982 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000983 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000984 } else {
985 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000986 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000987 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +0000988 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000989 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000990 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000991 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000992 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000993 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +0000994 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000995 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000996 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000997 parseIfThenElse();
998 } else {
999 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001000 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001001 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001002 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001003 }
1004 } else if (NeedsUnwrappedLine) {
1005 addUnwrappedLine();
1006 }
1007}
1008
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001009void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001010 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001011 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001012 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001013 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001014 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001015 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001016 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1017 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001018 addUnwrappedLine();
1019
Daniel Jasper65ee3472013-07-31 23:16:02 +00001020 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1021 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1022 DeclarationScopeStack.size() > 1);
1023 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001024 // Munch the semicolon after a namespace. This is more common than one would
1025 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001026 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001027 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001028 addUnwrappedLine();
1029 }
1030 // FIXME: Add error handling.
1031}
1032
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001033void UnwrappedLineParser::parseForOrWhileLoop() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001034 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001035 "'for' or 'while' expected");
1036 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001037 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001038 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001039 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001040 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001041 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001042 addUnwrappedLine();
1043 } else {
1044 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001045 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001046 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001047 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001048 }
1049}
1050
Daniel Jasperf7935112012-12-03 18:12:45 +00001051void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001052 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001053 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001054 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001055 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001056 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001057 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1058 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001059 } else {
1060 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001061 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001062 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001063 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001064 }
1065
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001066 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001067 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001068 addUnwrappedLine();
1069 return;
1070 }
1071
Daniel Jasperf7935112012-12-03 18:12:45 +00001072 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001073 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001074}
1075
1076void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001077 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001078 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001079 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001080 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001081 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001082 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001083 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001084 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001085 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1086 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1087 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001088 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001089 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001090 parseStructuralElement();
1091 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001092 addUnwrappedLine();
1093 } else {
1094 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001095 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001096 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001097}
1098
1099void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001100 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001101 // FIXME: fix handling of complex expressions here.
1102 do {
1103 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001104 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001105 parseLabel();
1106}
1107
1108void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001109 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001110 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001111 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001112 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001113 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001114 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001115 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001116 addUnwrappedLine();
1117 } else {
1118 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001119 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001120 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001121 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001122 }
1123}
1124
1125void UnwrappedLineParser::parseAccessSpecifier() {
1126 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001127 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001128 if (FormatTok->is(tok::identifier) &&
1129 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001130 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001131 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001132 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001133 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001134 addUnwrappedLine();
1135}
1136
1137void UnwrappedLineParser::parseEnum() {
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001138 if (FormatTok->Tok.is(tok::kw_enum)) {
1139 // Won't be 'enum' for NS_ENUMs.
1140 nextToken();
1141 }
Daniel Jasper2b41a822013-08-20 12:42:50 +00001142 // Eat up enum class ...
1143 if (FormatTok->Tok.is(tok::kw_class) ||
1144 FormatTok->Tok.is(tok::kw_struct))
1145 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001146 while (FormatTok->Tok.getIdentifierInfo() ||
1147 FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001148 nextToken();
1149 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001150 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001151 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001152 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001153 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001154 nextToken();
1155 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001156 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001157 FormatTok->BlockKind = BK_Block;
1158 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1159 if (HasError) {
1160 if (FormatTok->is(tok::semi))
1161 nextToken();
Manuel Klimeka027f302013-08-07 19:20:45 +00001162 addUnwrappedLine();
Daniel Jasper015ed022013-09-13 09:20:45 +00001163 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001164 }
1165 // We fall through to parsing a structural element afterwards, so that in
1166 // enum A {} n, m;
1167 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperf7935112012-12-03 18:12:45 +00001168}
1169
Manuel Klimeke01bab52013-01-15 13:38:33 +00001170void UnwrappedLineParser::parseRecord() {
Manuel Klimek28cacc72013-01-07 18:10:23 +00001171 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001172 if (FormatTok->Tok.is(tok::identifier) ||
1173 FormatTok->Tok.is(tok::kw___attribute) ||
Manuel Klimek91e48582013-10-07 09:15:41 +00001174 FormatTok->Tok.is(tok::kw___declspec) ||
1175 FormatTok->Tok.is(tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001176 nextToken();
1177 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001178 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001179 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001180 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001181 // The actual identifier can be a nested name specifier, and in macros
1182 // it is often token-pasted.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001183 while (FormatTok->Tok.is(tok::identifier) ||
1184 FormatTok->Tok.is(tok::coloncolon) ||
1185 FormatTok->Tok.is(tok::hashhash))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001186 nextToken();
1187
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001188 // Note that parsing away template declarations here leads to incorrectly
1189 // accepting function declarations as record declarations.
1190 // In general, we cannot solve this problem. Consider:
1191 // class A<int> B() {}
1192 // which can be a function definition or a class definition when B() is a
1193 // macro. If we find enough real-world cases where this is a problem, we
1194 // can parse for the 'template' keyword in the beginning of the statement,
1195 // and thus rule out the record production in case there is no template
1196 // (this would still leave us with an ambiguity between template function
1197 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001198 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1199 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1200 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001201 return;
1202 nextToken();
1203 }
1204 }
1205 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001206 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001207 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001208 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1209 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001210 addUnwrappedLine();
1211
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001212 parseBlock(/*MustBeDeclaration=*/true, /*Addlevel=*/true,
1213 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001214 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001215 // We fall through to parsing a structural element afterwards, so
1216 // class A {} n, m;
1217 // will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00001218}
1219
Nico Weber8696a8d2013-01-09 21:15:03 +00001220void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001221 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001222 do
1223 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001224 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001225 nextToken(); // Skip '>'.
1226}
1227
1228void UnwrappedLineParser::parseObjCUntilAtEnd() {
1229 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001230 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001231 nextToken();
1232 addUnwrappedLine();
1233 break;
1234 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001235 if (FormatTok->is(tok::l_brace)) {
1236 parseBlock(/*MustBeDeclaration=*/false);
1237 // In ObjC interfaces, nothing should be following the "}".
1238 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001239 } else if (FormatTok->is(tok::r_brace)) {
1240 // Ignore stray "}". parseStructuralElement doesn't consume them.
1241 nextToken();
1242 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001243 } else {
1244 parseStructuralElement();
1245 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001246 } while (!eof());
1247}
1248
Nico Weber2ce0ac52013-01-09 23:25:37 +00001249void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001250 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001251 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001252
1253 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001254 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001255 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001256 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001257 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001258 // Skip category, if present.
1259 parseParens();
1260
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001261 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001262 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001263
1264 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001265 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber9096fc02013-06-26 00:30:14 +00001266 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber7eecf4b2013-01-09 20:25:35 +00001267
1268 // With instance variables, this puts '}' on its own line. Without instance
1269 // variables, this ends the @interface line.
1270 addUnwrappedLine();
1271
Nico Weber8696a8d2013-01-09 21:15:03 +00001272 parseObjCUntilAtEnd();
1273}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001274
Nico Weber8696a8d2013-01-09 21:15:03 +00001275void UnwrappedLineParser::parseObjCProtocol() {
1276 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001277 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001278
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001279 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001280 parseObjCProtocolList();
1281
1282 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001283 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001284 nextToken();
1285 return addUnwrappedLine();
1286 }
1287
1288 addUnwrappedLine();
1289 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001290}
1291
Daniel Jasper3b203a62013-09-05 16:05:56 +00001292LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1293 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001294 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1295 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1296 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1297 E = Line.Tokens.end();
1298 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001299 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001300 }
1301 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1302 E = Line.Tokens.end();
1303 I != E; ++I) {
1304 const UnwrappedLineNode &Node = *I;
1305 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1306 I = Node.Children.begin(),
1307 E = Node.Children.end();
1308 I != E; ++I) {
1309 printDebugInfo(*I, "\nChild: ");
1310 }
1311 }
1312 llvm::dbgs() << "\n";
1313}
1314
Daniel Jasperf7935112012-12-03 18:12:45 +00001315void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001316 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001317 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001318 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001319 if (CurrentLines == &Lines)
1320 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001321 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001322 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001323 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001324 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001325 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001326 I = PreprocessorDirectives.begin(),
1327 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001328 I != E; ++I) {
1329 CurrentLines->push_back(*I);
1330 }
1331 PreprocessorDirectives.clear();
1332 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001333}
1334
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001335bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001336
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001337void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1338 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001339 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001340 I = CommentsBeforeNextToken.begin(),
1341 E = CommentsBeforeNextToken.end();
1342 I != E; ++I) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001343 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001344 addUnwrappedLine();
1345 }
1346 pushToken(*I);
1347 }
1348 if (NewlineBeforeNext && JustComments) {
1349 addUnwrappedLine();
1350 }
1351 CommentsBeforeNextToken.clear();
1352}
1353
Daniel Jasperf7935112012-12-03 18:12:45 +00001354void UnwrappedLineParser::nextToken() {
1355 if (eof())
1356 return;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001357 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001358 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001359 readToken();
1360}
1361
1362void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001363 bool CommentsInCurrentLine = true;
1364 do {
1365 FormatTok = Tokens->getNextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001366 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1367 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001368 // If there is an unfinished unwrapped line, we flush the preprocessor
1369 // directives only after that unwrapped line was finished later.
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001370 bool SwitchToPreprocessorLines =
1371 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001372 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001373 // Comments stored before the preprocessor directive need to be output
1374 // before the preprocessor directive, at the same level as the
1375 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001376 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001377 parsePPDirective();
1378 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001379
1380 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1381 !Line->InPPDirective) {
1382 continue;
1383 }
1384
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001385 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001386 return;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001387 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001388 CommentsInCurrentLine = false;
1389 }
1390 if (CommentsInCurrentLine) {
1391 pushToken(FormatTok);
1392 } else {
1393 CommentsBeforeNextToken.push_back(FormatTok);
1394 }
1395 } while (!eof());
1396}
1397
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001398void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001399 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001400 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001401 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001402 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001403 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001404}
1405
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001406} // end namespace format
1407} // end namespace clang