blob: 96d815572fbe7490d2ae2eabeb1fd7a8a3cdcd75 [file] [log] [blame]
Daniel Jasperbac016b2012-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 Jasperbac016b2012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Manuel Klimek8fa37992013-01-16 12:31:12 +000016#define DEBUG_TYPE "format-parser"
Daniel Jasperbac016b2012-12-03 18:12:45 +000017
Chandler Carruthb1ba0ef2013-01-19 08:09:44 +000018#include "UnwrappedLineParser.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000019#include "llvm/Support/Debug.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000020
Daniel Jasperbac016b2012-12-03 18:12:45 +000021namespace clang {
22namespace format {
23
Manuel Klimek96e888b2013-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 Toppere50947f2013-07-01 04:21:54 +000033namespace {
34
Manuel Klimek70b03f42013-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 Klimek70b03f42013-01-23 09:32:48 +000040 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek836b58f2013-01-23 11:03:04 +000041 Stack.push_back(MustBeDeclaration);
Manuel Klimek70b03f42013-01-23 09:32:48 +000042 }
43 ~ScopedDeclarationState() {
Manuel Klimek70b03f42013-01-23 09:32:48 +000044 Stack.pop_back();
Manuel Klimeka32a7fd2013-01-23 14:08:21 +000045 if (!Stack.empty())
46 Line.MustBeDeclaration = Stack.back();
47 else
48 Line.MustBeDeclaration = true;
Manuel Klimek70b03f42013-01-23 09:32:48 +000049 }
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +000050
Manuel Klimek70b03f42013-01-23 09:32:48 +000051private:
52 UnwrappedLine &Line;
53 std::vector<bool> &Stack;
54};
55
Manuel Klimekd4397b92013-01-04 23:34:14 +000056class ScopedMacroState : public FormatTokenSource {
57public:
58 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek96e888b2013-05-28 11:55:06 +000059 FormatToken *&ResetToken, bool &StructuralError)
Manuel Klimekd4397b92013-01-04 23:34:14 +000060 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek67d080d2013-04-12 14:13:36 +000061 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
62 StructuralError(StructuralError),
Manuel Klimek96e888b2013-05-28 11:55:06 +000063 PreviousStructuralError(StructuralError), Token(NULL) {
Manuel Klimekd4397b92013-01-04 23:34:14 +000064 TokenSource = this;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000065 Line.Level = 0;
Manuel Klimekd4397b92013-01-04 23:34:14 +000066 Line.InPPDirective = true;
67 }
68
69 ~ScopedMacroState() {
70 TokenSource = PreviousTokenSource;
71 ResetToken = Token;
72 Line.InPPDirective = false;
Manuel Klimekc37b4d62013-01-05 22:14:16 +000073 Line.Level = PreviousLineLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +000074 StructuralError = PreviousStructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +000075 }
76
Stephen Hines651f13c2014-04-23 16:59:28 -070077 FormatToken *getNextToken() override {
Manuel Klimekdd5b1012013-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 Klimekd4397b92013-01-04 23:34:14 +000081 Token = PreviousTokenSource->getNextToken();
82 if (eof())
Manuel Klimek96e888b2013-05-28 11:55:06 +000083 return getFakeEOF();
Manuel Klimekd4397b92013-01-04 23:34:14 +000084 return Token;
85 }
86
Stephen Hines651f13c2014-04-23 16:59:28 -070087 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimek80829bd2013-05-23 09:41:43 +000088
Stephen Hines651f13c2014-04-23 16:59:28 -070089 FormatToken *setPosition(unsigned Position) override {
Manuel Klimek80829bd2013-05-23 09:41:43 +000090 Token = PreviousTokenSource->setPosition(Position);
91 return Token;
92 }
93
Manuel Klimekd4397b92013-01-04 23:34:14 +000094private:
Manuel Klimek96e888b2013-05-28 11:55:06 +000095 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimekd4397b92013-01-04 23:34:14 +000096
Manuel Klimek96e888b2013-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 Klimekd4397b92013-01-04 23:34:14 +0000106 }
107
108 UnwrappedLine &Line;
109 FormatTokenSource *&TokenSource;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000110 FormatToken *&ResetToken;
Manuel Klimekc37b4d62013-01-05 22:14:16 +0000111 unsigned PreviousLineLevel;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000112 FormatTokenSource *PreviousTokenSource;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000113 bool &StructuralError;
114 bool PreviousStructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000115
Manuel Klimek96e888b2013-05-28 11:55:06 +0000116 FormatToken *Token;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000117};
118
Craig Toppere50947f2013-07-01 04:21:54 +0000119} // end anonymous namespace
120
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000121class ScopedLineState {
122public:
Manuel Klimek525fe162013-01-18 14:04:34 +0000123 ScopedLineState(UnwrappedLineParser &Parser,
124 bool SwitchToPreprocessorLines = false)
Daniel Jasper567dcf92013-09-05 09:29:45 +0000125 : Parser(Parser) {
126 OriginalLines = Parser.CurrentLines;
Manuel Klimek525fe162013-01-18 14:04:34 +0000127 if (SwitchToPreprocessorLines)
128 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000129 else if (!Parser.Line->Tokens.empty())
130 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
Stephen Hines651f13c2014-04-23 16:59:28 -0700131 PreBlockLine = Parser.Line.release();
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000132 Parser.Line.reset(new UnwrappedLine());
133 Parser.Line->Level = PreBlockLine->Level;
134 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000135 }
136
137 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000138 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000139 Parser.addUnwrappedLine();
140 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000141 assert(Parser.Line->Tokens.empty());
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000142 Parser.Line.reset(PreBlockLine);
Daniel Jasper567dcf92013-09-05 09:29:45 +0000143 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
144 Parser.MustBreakBeforeNextToken = true;
145 Parser.CurrentLines = OriginalLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000146 }
147
148private:
149 UnwrappedLineParser &Parser;
150
151 UnwrappedLine *PreBlockLine;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000152 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000153};
154
Stephen Hines651f13c2014-04-23 16:59:28 -0700155class 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 Toppere50947f2013-07-01 04:21:54 +0000176namespace {
177
Manuel Klimek80829bd2013-05-23 09:41:43 +0000178class IndexedTokenSource : public FormatTokenSource {
179public:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000180 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000181 : Tokens(Tokens), Position(-1) {}
182
Stephen Hines651f13c2014-04-23 16:59:28 -0700183 FormatToken *getNextToken() override {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000184 ++Position;
185 return Tokens[Position];
186 }
187
Stephen Hines651f13c2014-04-23 16:59:28 -0700188 unsigned getPosition() override {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000189 assert(Position >= 0);
190 return Position;
191 }
192
Stephen Hines651f13c2014-04-23 16:59:28 -0700193 FormatToken *setPosition(unsigned P) override {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000194 Position = P;
195 return Tokens[Position];
196 }
197
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000198 void reset() { Position = -1; }
199
Manuel Klimek80829bd2013-05-23 09:41:43 +0000200private:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000201 ArrayRef<FormatToken *> Tokens;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000202 int Position;
203};
204
Craig Toppere50947f2013-07-01 04:21:54 +0000205} // end anonymous namespace
206
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000207UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Manuel Klimek96e888b2013-05-28 11:55:06 +0000208 ArrayRef<FormatToken *> Tokens,
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000209 UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000210 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Manuel Klimek96e888b2013-05-28 11:55:06 +0000211 CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL),
Manuel Klimekae76f7f2013-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 Jasperbac016b2012-12-03 18:12:45 +0000226
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000227bool UnwrappedLineParser::parse() {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000228 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000229 do {
230 DEBUG(llvm::dbgs() << "----\n");
231 reset();
232 Tokens = &TokenSource;
233 TokenSource.reset();
Daniel Jasper516fb312013-03-01 18:11:39 +0000234
Manuel Klimekae76f7f2013-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 Jasperac4d0182013-10-12 13:32:56 +0000249 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimekae76f7f2013-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 Klimek67d080d2013-04-12 14:13:36 +0000260 return StructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000261}
262
Manuel Klimek67d080d2013-04-12 14:13:36 +0000263void UnwrappedLineParser::parseFile() {
Daniel Jasper627707b2013-03-22 16:55:40 +0000264 ScopedDeclarationState DeclarationState(
265 *Line, DeclarationScopeStack,
266 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber27268772013-06-26 00:30:14 +0000267 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000268 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000269 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000270 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000271}
272
Manuel Klimek67d080d2013-04-12 14:13:36 +0000273void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000274 bool SwitchLabelEncountered = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000275 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000276 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000277 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000278 nextToken();
279 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000280 break;
281 case tok::l_brace:
Manuel Klimek70b03f42013-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 Weber27268772013-06-26 00:30:14 +0000284 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000285 addUnwrappedLine();
286 break;
287 case tok::r_brace:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000288 if (HasOpeningBrace)
289 return;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000290 StructuralError = true;
291 nextToken();
292 addUnwrappedLine();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000293 break;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000294 case tok::kw_default:
295 case tok::kw_case:
Daniel Jasper67cf1db2013-09-02 08:26:29 +0000296 if (!SwitchLabelEncountered &&
297 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
298 ++Line->Level;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000299 SwitchLabelEncountered = true;
300 parseStructuralElement();
301 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000302 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000303 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000304 break;
305 }
306 } while (!eof());
307}
308
Manuel Klimek80829bd2013-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 Klimek96e888b2013-05-28 11:55:06 +0000316 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-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 Jasper0de1c4d2013-07-09 09:06:29 +0000320 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000321 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000322 do {
Daniel Jasper02eacc22013-07-01 09:15:46 +0000323 // Get next none-comment token.
324 FormatToken *NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000325 unsigned ReadTokens = 0;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000326 do {
327 NextTok = Tokens->getNextToken();
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000328 ++ReadTokens;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000329 } while (NextTok->is(tok::comment));
330
Manuel Klimek96e888b2013-05-28 11:55:06 +0000331 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000332 case tok::l_brace:
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000333 LBraceStack.push_back(Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000334 break;
335 case tok::r_brace:
336 if (!LBraceStack.empty()) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000337 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000338 // If there is a comma, semicolon or right paren after the closing
Manuel Klimek31e44f72013-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 Jasperf439dcb2013-08-28 07:50:37 +0000344 //
345 // We exclude + and - as they can be ObjC visibility modifiers.
Stephen Hines651f13c2014-04-23 16:59:28 -0700346 if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren, tok::period,
Daniel Jasperac885cd2013-09-13 10:55:31 +0000347 tok::r_square, tok::l_brace, tok::colon) ||
Daniel Jasperf439dcb2013-08-28 07:50:37 +0000348 (NextTok->isBinaryOperator() &&
349 !NextTok->isOneOf(tok::plus, tok::minus))) {
Daniel Jasper0de1c4d2013-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 Klimek80829bd2013-05-23 09:41:43 +0000356 }
357 LBraceStack.pop_back();
358 }
359 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700360 case tok::at:
Manuel Klimek80829bd2013-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 Jasperf7ec1cc2013-05-31 14:56:29 +0000367 if (!LBraceStack.empty())
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000368 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000369 break;
370 default:
371 break;
372 }
373 Tok = NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000374 Position += ReadTokens;
Manuel Klimek31e44f72013-09-04 08:20:47 +0000375 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimek80829bd2013-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 Jasper0de1c4d2013-07-09 09:06:29 +0000378 if (LBraceStack[i]->BlockKind == BK_Unknown)
379 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000380 }
Manuel Klimek31e44f72013-09-04 08:20:47 +0000381
Manuel Klimek80829bd2013-05-23 09:41:43 +0000382 FormatTok = Tokens->setPosition(StoredPosition);
383}
384
Manuel Klimekaabfb272013-10-12 22:46:56 +0000385void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
386 bool MunchSemi) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000387 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jaspere865cc52013-07-25 11:31:57 +0000388 unsigned InitialLevel = Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000389 nextToken();
390
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000391 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000392
Manuel Klimek70b03f42013-01-23 09:32:48 +0000393 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
394 MustBeDeclaration);
Daniel Jaspereff18b92013-07-31 23:16:02 +0000395 if (AddLevel)
396 ++Line->Level;
Nico Weber27268772013-06-26 00:30:14 +0000397 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000398
Manuel Klimek96e888b2013-05-28 11:55:06 +0000399 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000400 Line->Level = InitialLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000401 StructuralError = true;
402 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000403 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000404
Daniel Jasperf9955d32013-03-20 12:37:50 +0000405 nextToken(); // Munch the closing brace.
Manuel Klimekaabfb272013-10-12 22:46:56 +0000406 if (MunchSemi && FormatTok->Tok.is(tok::semi))
407 nextToken();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000408 Line->Level = InitialLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000409}
410
Manuel Klimek753a5112013-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 Jasperbac016b2012-12-03 18:12:45 +0000425void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000426 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000427 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000428 nextToken();
429
Manuel Klimek96e888b2013-05-28 11:55:06 +0000430 if (FormatTok->Tok.getIdentifierInfo() == NULL) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000431 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000432 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000433 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000434
Manuel Klimek96e888b2013-05-28 11:55:06 +0000435 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000436 case tok::pp_define:
437 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000438 return;
439 case tok::pp_if:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000440 parsePPIf(/*IfDef=*/false);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000441 break;
442 case tok::pp_ifdef:
443 case tok::pp_ifndef:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000444 parsePPIf(/*IfDef=*/true);
Alexander Kornienko6fb46b02013-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 Klimekd4397b92013-01-04 23:34:14 +0000454 break;
455 default:
456 parsePPUnknown();
457 break;
458 }
459}
460
Alexander Kornienko6fb46b02013-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 Klimekae76f7f2013-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 Kornienko6fb46b02013-05-24 18:24:24 +0000476 nextToken();
Manuel Klimekae76f7f2013-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 Kornienko6fb46b02013-05-24 18:24:24 +0000482 PPStack.push_back(PP_Unreachable);
483 } else {
484 pushPPConditional();
485 }
486 parsePPUnknown();
487}
488
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000489void UnwrappedLineParser::parsePPElse() {
490 if (!PPStack.empty())
491 PPStack.pop_back();
Manuel Klimekae76f7f2013-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 Kornienko6fb46b02013-05-24 18:24:24 +0000501 parsePPUnknown();
502}
503
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000504void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000505
506void UnwrappedLineParser::parsePPEndIf() {
Manuel Klimekae76f7f2013-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 Klimekae76f7f2013-10-11 21:25:45 +0000510 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
511 }
512 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700513 // Guard against #endif's without #if.
514 if (PPBranchLevel > 0)
515 --PPBranchLevel;
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000516 if (!PPChainBranchIndex.empty())
517 PPChainBranchIndex.pop();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000518 if (!PPStack.empty())
519 PPStack.pop_back();
520 parsePPUnknown();
521}
522
Manuel Klimekd4397b92013-01-04 23:34:14 +0000523void UnwrappedLineParser::parsePPDefine() {
524 nextToken();
525
Manuel Klimek96e888b2013-05-28 11:55:06 +0000526 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000527 parsePPUnknown();
528 return;
529 }
530 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000531 if (FormatTok->Tok.getKind() == tok::l_paren &&
532 FormatTok->WhitespaceRange.getBegin() ==
533 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000534 parseParens();
535 }
536 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000537 Line->Level = 1;
Manuel Klimekc3d0c822013-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 Klimekd4397b92013-01-04 23:34:14 +0000544 parseFile();
545}
546
547void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000548 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000549 nextToken();
550 } while (!eof());
551 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000552}
553
Alexander Kornienko99b0e142013-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 Klimekf0ab0a32013-01-07 14:56:16 +0000581void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000582 assert(!FormatTok->Tok.is(tok::l_brace));
583 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000584 case tok::at:
585 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000586 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000587 parseBracedList();
588 break;
589 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000590 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-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 Weber27d13672013-01-09 20:25:35 +0000596 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000597 case tok::objc_implementation:
598 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000599 case tok::objc_protocol:
600 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000601 case tok::objc_end:
602 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000603 case tok::objc_optional:
604 case tok::objc_required:
605 nextToken();
606 addUnwrappedLine();
607 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000608 default:
609 break;
610 }
611 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000612 case tok::kw_namespace:
613 parseNamespace();
614 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000615 case tok::kw_inline:
616 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000617 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000618 parseNamespace();
619 return;
620 }
621 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000622 case tok::kw_public:
623 case tok::kw_protected:
624 case tok::kw_private:
Daniel Jasperbac016b2012-12-03 18:12:45 +0000625 parseAccessSpecifier();
626 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000627 case tok::kw_if:
628 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000629 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000630 case tok::kw_for:
631 case tok::kw_while:
632 parseForOrWhileLoop();
633 return;
Alexander Kornienkoa166e732012-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 Klimekd19dc2d2013-01-21 14:32:05 +0000647 case tok::kw_extern:
648 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000649 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000650 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000651 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000652 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000653 addUnwrappedLine();
654 return;
655 }
656 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700657 break;
658 case tok::identifier:
659 if (FormatTok->IsForEachMacro) {
660 parseForOrWhileLoop();
661 return;
662 }
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000663 // In all other cases, parse the declaration.
664 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000665 default:
666 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000667 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000668 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000669 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000670 case tok::at:
671 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000672 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000673 parseBracedList();
674 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000675 case tok::kw_enum:
676 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000677 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700678 case tok::kw_typedef:
679 nextToken();
680 // FIXME: Use the IdentifierTable instead.
681 if (FormatTok->TokenText == "NS_ENUM")
682 parseEnum();
683 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000684 case tok::kw_struct:
685 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000686 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000687 parseRecord();
688 // A record declaration or definition is always the start of a structural
689 // element.
690 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000691 case tok::semi:
692 nextToken();
693 addUnwrappedLine();
694 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000695 case tok::r_brace:
696 addUnwrappedLine();
697 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000698 case tok::l_paren:
699 parseParens();
700 break;
Manuel Klimek753a5112013-09-04 13:25:30 +0000701 case tok::caret:
702 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700703 if (FormatTok->Tok.isAnyIdentifier() ||
704 FormatTok->isSimpleTypeSpecifier())
705 nextToken();
706 if (FormatTok->is(tok::l_paren))
707 parseParens();
708 if (FormatTok->is(tok::l_brace))
Manuel Klimek753a5112013-09-04 13:25:30 +0000709 parseChildBlock();
Manuel Klimek753a5112013-09-04 13:25:30 +0000710 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000711 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000712 if (!tryToParseBracedList()) {
713 // A block outside of parentheses must be the last part of a
714 // structural element.
715 // FIXME: Figure out cases where this is not true, and add projections
716 // for them (the one we know is missing are lambdas).
Stephen Hines651f13c2014-04-23 16:59:28 -0700717 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000718 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -0700719 FormatTok->Type = TT_FunctionLBrace;
Nico Weber27268772013-06-26 00:30:14 +0000720 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000721 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000722 return;
723 }
724 // Otherwise this was a braced init list, and the structural
725 // element continues.
726 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000727 case tok::identifier: {
728 StringRef Text = FormatTok->TokenText;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000729 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000730 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000731 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000732 parseLabel();
733 return;
734 }
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000735 // Recognize function-like macro usages without trailing semicolon.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000736 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000737 parseParens();
Stephen Hines651f13c2014-04-23 16:59:28 -0700738 if (FormatTok->NewlinesBefore > 0 &&
739 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000740 addUnwrappedLine();
741 return;
742 }
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000743 } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
744 Text == Text.upper()) {
745 // Recognize free-standing macros like Q_OBJECT.
746 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000747 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000748 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000749 }
750 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000751 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000752 case tok::equal:
753 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000754 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000755 parseBracedList();
756 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000757 break;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000758 case tok::l_square:
Stephen Hines651f13c2014-04-23 16:59:28 -0700759 parseSquare();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000760 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000761 default:
762 nextToken();
763 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000764 }
765 } while (!eof());
766}
767
Stephen Hines651f13c2014-04-23 16:59:28 -0700768bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper2d657052013-09-05 11:49:39 +0000769 // FIXME: This is a dirty way to access the previous token. Find a better
770 // solution.
Daniel Jasper520cca82013-09-06 21:25:51 +0000771 if (!Line->Tokens.empty() &&
Stephen Hines651f13c2014-04-23 16:59:28 -0700772 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator) ||
773 Line->Tokens.back().Tok->closesScope() ||
774 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasper2d657052013-09-05 11:49:39 +0000775 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700776 return false;
Daniel Jasper2d657052013-09-05 11:49:39 +0000777 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000778 assert(FormatTok->is(tok::l_square));
779 FormatToken &LSquare = *FormatTok;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000780 if (!tryToParseLambdaIntroducer())
Stephen Hines651f13c2014-04-23 16:59:28 -0700781 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000782
783 while (FormatTok->isNot(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700784 if (FormatTok->isSimpleTypeSpecifier()) {
785 nextToken();
786 continue;
787 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000788 switch (FormatTok->Tok.getKind()) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000789 case tok::l_brace:
790 break;
791 case tok::l_paren:
792 parseParens();
793 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700794 case tok::less:
795 case tok::greater:
Daniel Jasperac2c9742013-09-05 10:04:31 +0000796 case tok::identifier:
Stephen Hines651f13c2014-04-23 16:59:28 -0700797 case tok::coloncolon:
Daniel Jasperac2c9742013-09-05 10:04:31 +0000798 case tok::kw_mutable:
799 nextToken();
800 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700801 case tok::arrow:
802 FormatTok->Type = TT_TrailingReturnArrow;
803 nextToken();
804 break;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000805 default:
Stephen Hines651f13c2014-04-23 16:59:28 -0700806 return true;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000807 }
808 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000809 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek753a5112013-09-04 13:25:30 +0000810 parseChildBlock();
Stephen Hines651f13c2014-04-23 16:59:28 -0700811 return true;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000812}
813
814bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
815 nextToken();
816 if (FormatTok->is(tok::equal)) {
817 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000818 if (FormatTok->is(tok::r_square)) {
819 nextToken();
820 return true;
821 }
822 if (FormatTok->isNot(tok::comma))
823 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000824 nextToken();
825 } else if (FormatTok->is(tok::amp)) {
826 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000827 if (FormatTok->is(tok::r_square)) {
828 nextToken();
829 return true;
830 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000831 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
832 return false;
833 }
Daniel Jasperac2c9742013-09-05 10:04:31 +0000834 if (FormatTok->is(tok::comma))
835 nextToken();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000836 } else if (FormatTok->is(tok::r_square)) {
837 nextToken();
838 return true;
839 }
840 do {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000841 if (FormatTok->is(tok::amp))
842 nextToken();
843 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
844 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000845 nextToken();
846 if (FormatTok->is(tok::comma)) {
847 nextToken();
848 } else if (FormatTok->is(tok::r_square)) {
849 nextToken();
850 return true;
851 } else {
852 return false;
853 }
854 } while (!eof());
855 return false;
856}
857
Manuel Klimek80829bd2013-05-23 09:41:43 +0000858bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000859 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000860 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000861 assert(FormatTok->BlockKind != BK_Unknown);
862 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000863 return false;
864 parseBracedList();
865 return true;
866}
867
Daniel Jasper57981202013-09-13 09:20:45 +0000868bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
869 bool HasError = false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000870 nextToken();
871
Manuel Klimek423dd932013-04-10 09:52:05 +0000872 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
873 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000874 do {
Manuel Klimek423dd932013-04-10 09:52:05 +0000875 // FIXME: When we start to support lambdas, we'll want to parse them away
876 // here, otherwise our bail-out scenarios below break. The better solution
877 // might be to just implement a more or less complete expression parser.
Manuel Klimek96e888b2013-05-28 11:55:06 +0000878 switch (FormatTok->Tok.getKind()) {
Manuel Klimek753a5112013-09-04 13:25:30 +0000879 case tok::caret:
880 nextToken();
881 if (FormatTok->is(tok::l_brace)) {
882 parseChildBlock();
883 }
884 break;
885 case tok::l_square:
886 tryToParseLambda();
887 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000888 case tok::l_brace:
Manuel Klimek31e44f72013-09-04 08:20:47 +0000889 // Assume there are no blocks inside a braced init list apart
890 // from the ones we explicitly parse out (like lambdas).
891 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000892 parseBracedList();
893 break;
894 case tok::r_brace:
895 nextToken();
Daniel Jasper57981202013-09-13 09:20:45 +0000896 return !HasError;
Manuel Klimek423dd932013-04-10 09:52:05 +0000897 case tok::semi:
Daniel Jasper57981202013-09-13 09:20:45 +0000898 HasError = true;
899 if (!ContinueOnSemicolons)
900 return !HasError;
901 nextToken();
902 break;
Manuel Klimek423dd932013-04-10 09:52:05 +0000903 case tok::comma:
904 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +0000905 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000906 default:
907 nextToken();
908 break;
909 }
910 } while (!eof());
Daniel Jasper57981202013-09-13 09:20:45 +0000911 return false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000912}
913
Daniel Jasperbac016b2012-12-03 18:12:45 +0000914void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000915 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000916 nextToken();
917 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000918 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000919 case tok::l_paren:
920 parseParens();
921 break;
922 case tok::r_paren:
923 nextToken();
924 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000925 case tok::r_brace:
926 // A "}" inside parenthesis is an error if there wasn't a matching "{".
927 return;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000928 case tok::l_square:
929 tryToParseLambda();
930 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000931 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000932 if (!tryToParseBracedList()) {
Manuel Klimekb7d98a12013-09-04 13:34:14 +0000933 parseChildBlock();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000934 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000935 break;
Nico Weber2afbe522013-02-10 04:38:23 +0000936 }
Nico Weberd74fcdb2013-02-10 20:35:35 +0000937 case tok::at:
938 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000939 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000940 parseBracedList();
941 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000942 default:
943 nextToken();
944 break;
945 }
946 } while (!eof());
947}
948
Stephen Hines651f13c2014-04-23 16:59:28 -0700949void UnwrappedLineParser::parseSquare() {
950 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
951 if (tryToParseLambda())
952 return;
953 do {
954 switch (FormatTok->Tok.getKind()) {
955 case tok::l_paren:
956 parseParens();
957 break;
958 case tok::r_square:
959 nextToken();
960 return;
961 case tok::r_brace:
962 // A "}" inside parenthesis is an error if there wasn't a matching "{".
963 return;
964 case tok::l_square:
965 parseSquare();
966 break;
967 case tok::l_brace: {
968 if (!tryToParseBracedList()) {
969 parseChildBlock();
970 }
971 break;
972 }
973 case tok::at:
974 nextToken();
975 if (FormatTok->Tok.is(tok::l_brace))
976 parseBracedList();
977 break;
978 default:
979 nextToken();
980 break;
981 }
982 } while (!eof());
983}
984
Daniel Jasperbac016b2012-12-03 18:12:45 +0000985void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000986 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +0000987 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000988 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +0000989 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000990 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000991 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700992 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +0000993 parseBlock(/*MustBeDeclaration=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -0700994 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
995 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimeke4907052013-08-02 21:31:59 +0000996 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -0700997 } else {
Manuel Klimeke4907052013-08-02 21:31:59 +0000998 NeedsUnwrappedLine = true;
Stephen Hines651f13c2014-04-23 16:59:28 -0700999 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001000 } else {
1001 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001002 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001003 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001004 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001005 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001006 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001007 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001008 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001009 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001010 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001011 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001012 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001013 parseIfThenElse();
1014 } else {
1015 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001016 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001017 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001018 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001019 }
1020 } else if (NeedsUnwrappedLine) {
1021 addUnwrappedLine();
1022 }
1023}
1024
Alexander Kornienko15757312012-12-06 18:03:27 +00001025void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001026 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Alexander Kornienko15757312012-12-06 18:03:27 +00001027 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001028 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +00001029 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001030 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001031 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001032 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1033 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimek44135b82013-05-13 12:51:40 +00001034 addUnwrappedLine();
1035
Daniel Jaspereff18b92013-07-31 23:16:02 +00001036 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1037 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1038 DeclarationScopeStack.size() > 1);
1039 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +00001040 // Munch the semicolon after a namespace. This is more common than one would
1041 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001042 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +00001043 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +00001044 addUnwrappedLine();
1045 }
1046 // FIXME: Add error handling.
1047}
1048
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001049void UnwrappedLineParser::parseForOrWhileLoop() {
Stephen Hines651f13c2014-04-23 16:59:28 -07001050 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1051 FormatTok->IsForEachMacro) &&
1052 "'for', 'while' or foreach macro expected");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001053 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001054 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001055 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001056 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001057 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001058 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001059 addUnwrappedLine();
1060 } else {
1061 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001062 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001063 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001064 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001065 }
1066}
1067
Daniel Jasperbac016b2012-12-03 18:12:45 +00001068void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001069 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001070 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001071 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001072 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001073 parseBlock(/*MustBeDeclaration=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07001074 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1075 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001076 } else {
1077 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001078 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001079 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001080 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001081 }
1082
Alexander Kornienko393b0082012-12-04 15:40:36 +00001083 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001084 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +00001085 addUnwrappedLine();
1086 return;
1087 }
1088
Daniel Jasperbac016b2012-12-03 18:12:45 +00001089 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001090 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001091}
1092
1093void UnwrappedLineParser::parseLabel() {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001094 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +00001095 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +00001096 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +00001097 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001098 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001099 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001100 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +00001101 if (FormatTok->Tok.is(tok::kw_break)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001102 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1103 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1104 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001105 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001106 }
Manuel Klimeke4907052013-08-02 21:31:59 +00001107 parseStructuralElement();
1108 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001109 addUnwrappedLine();
1110 } else {
1111 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001112 }
Manuel Klimek526ed112013-01-09 15:25:02 +00001113 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001114}
1115
1116void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001117 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001118 // FIXME: fix handling of complex expressions here.
1119 do {
1120 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001121 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001122 parseLabel();
1123}
1124
1125void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001126 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001127 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001128 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001129 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001130 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001131 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jaspereff18b92013-07-31 23:16:02 +00001132 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001133 addUnwrappedLine();
1134 } else {
1135 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001136 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001137 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001138 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001139 }
1140}
1141
1142void UnwrappedLineParser::parseAccessSpecifier() {
1143 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001144 // Understand Qt's slots.
1145 if (FormatTok->is(tok::identifier) &&
1146 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
1147 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001148 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001149 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001150 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001151 addUnwrappedLine();
1152}
1153
1154void UnwrappedLineParser::parseEnum() {
Stephen Hines651f13c2014-04-23 16:59:28 -07001155 if (FormatTok->Tok.is(tok::kw_enum)) {
1156 // Won't be 'enum' for NS_ENUMs.
1157 nextToken();
1158 }
Daniel Jaspercbeb1c62013-08-20 12:42:50 +00001159 // Eat up enum class ...
1160 if (FormatTok->Tok.is(tok::kw_class) ||
1161 FormatTok->Tok.is(tok::kw_struct))
1162 nextToken();
Daniel Jaspere27dc5d2013-09-06 21:32:35 +00001163 while (FormatTok->Tok.getIdentifierInfo() ||
1164 FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001165 nextToken();
1166 // We can have macros or attributes in between 'enum' and the enum name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001167 if (FormatTok->Tok.is(tok::l_paren)) {
Alexander Kornienkoa166e732012-12-04 14:46:19 +00001168 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001169 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001170 if (FormatTok->Tok.is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +00001171 nextToken();
1172 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001173 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper57981202013-09-13 09:20:45 +00001174 FormatTok->BlockKind = BK_Block;
1175 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1176 if (HasError) {
1177 if (FormatTok->is(tok::semi))
1178 nextToken();
Manuel Klimekd3a247c2013-08-07 19:20:45 +00001179 addUnwrappedLine();
Daniel Jasper57981202013-09-13 09:20:45 +00001180 }
Manuel Klimek308232c2013-01-21 19:17:52 +00001181 }
1182 // We fall through to parsing a structural element afterwards, so that in
1183 // enum A {} n, m;
1184 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +00001185}
1186
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001187void UnwrappedLineParser::parseRecord() {
Manuel Klimekde768542013-01-07 18:10:23 +00001188 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001189 if (FormatTok->Tok.is(tok::identifier) ||
1190 FormatTok->Tok.is(tok::kw___attribute) ||
Manuel Klimek3d712892013-10-07 09:15:41 +00001191 FormatTok->Tok.is(tok::kw___declspec) ||
1192 FormatTok->Tok.is(tok::kw_alignas)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001193 nextToken();
1194 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001195 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001196 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +00001197 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +00001198 // The actual identifier can be a nested name specifier, and in macros
1199 // it is often token-pasted.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001200 while (FormatTok->Tok.is(tok::identifier) ||
1201 FormatTok->Tok.is(tok::coloncolon) ||
1202 FormatTok->Tok.is(tok::hashhash))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001203 nextToken();
1204
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001205 // Note that parsing away template declarations here leads to incorrectly
1206 // accepting function declarations as record declarations.
1207 // In general, we cannot solve this problem. Consider:
1208 // class A<int> B() {}
1209 // which can be a function definition or a class definition when B() is a
1210 // macro. If we find enough real-world cases where this is a problem, we
1211 // can parse for the 'template' keyword in the beginning of the statement,
1212 // and thus rule out the record production in case there is no template
1213 // (this would still leave us with an ambiguity between template function
1214 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +00001215 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1216 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1217 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001218 return;
1219 nextToken();
1220 }
1221 }
1222 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001223 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001224 if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
Stephen Hines651f13c2014-04-23 16:59:28 -07001225 Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1226 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
Manuel Klimek44135b82013-05-13 12:51:40 +00001227 addUnwrappedLine();
1228
Stephen Hines651f13c2014-04-23 16:59:28 -07001229 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekaabfb272013-10-12 22:46:56 +00001230 /*MunchSemi=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +00001231 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001232 // We fall through to parsing a structural element afterwards, so
1233 // class A {} n, m;
1234 // will end up in one unwrapped line.
Manuel Klimekde768542013-01-07 18:10:23 +00001235}
1236
Nico Weber1abe6ea2013-01-09 21:15:03 +00001237void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001238 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001239 do
1240 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001241 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +00001242 nextToken(); // Skip '>'.
1243}
1244
1245void UnwrappedLineParser::parseObjCUntilAtEnd() {
1246 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001247 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001248 nextToken();
1249 addUnwrappedLine();
1250 break;
1251 }
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001252 if (FormatTok->is(tok::l_brace)) {
1253 parseBlock(/*MustBeDeclaration=*/false);
1254 // In ObjC interfaces, nothing should be following the "}".
1255 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001256 } else if (FormatTok->is(tok::r_brace)) {
1257 // Ignore stray "}". parseStructuralElement doesn't consume them.
1258 nextToken();
1259 addUnwrappedLine();
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001260 } else {
1261 parseStructuralElement();
1262 }
Nico Weber1abe6ea2013-01-09 21:15:03 +00001263 } while (!eof());
1264}
1265
Nico Weber50767d82013-01-09 23:25:37 +00001266void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001267 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001268 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001269
1270 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001271 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001272 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001273 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001274 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001275 // Skip category, if present.
1276 parseParens();
1277
Manuel Klimek96e888b2013-05-28 11:55:06 +00001278 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001279 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001280
1281 // If instance variables are present, keep the '{' on the first line too.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001282 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber27268772013-06-26 00:30:14 +00001283 parseBlock(/*MustBeDeclaration=*/true);
Nico Weber27d13672013-01-09 20:25:35 +00001284
1285 // With instance variables, this puts '}' on its own line. Without instance
1286 // variables, this ends the @interface line.
1287 addUnwrappedLine();
1288
Nico Weber1abe6ea2013-01-09 21:15:03 +00001289 parseObjCUntilAtEnd();
1290}
Nico Weber27d13672013-01-09 20:25:35 +00001291
Nico Weber1abe6ea2013-01-09 21:15:03 +00001292void UnwrappedLineParser::parseObjCProtocol() {
1293 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001294 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001295
Manuel Klimek96e888b2013-05-28 11:55:06 +00001296 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001297 parseObjCProtocolList();
1298
1299 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001300 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001301 nextToken();
1302 return addUnwrappedLine();
1303 }
1304
1305 addUnwrappedLine();
1306 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001307}
1308
Daniel Jasperd98927d2013-09-05 16:05:56 +00001309LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1310 StringRef Prefix = "") {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001311 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1312 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1313 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1314 E = Line.Tokens.end();
1315 I != E; ++I) {
Daniel Jasperac2c9742013-09-05 10:04:31 +00001316 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper567dcf92013-09-05 09:29:45 +00001317 }
1318 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1319 E = Line.Tokens.end();
1320 I != E; ++I) {
1321 const UnwrappedLineNode &Node = *I;
1322 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1323 I = Node.Children.begin(),
1324 E = Node.Children.end();
1325 I != E; ++I) {
1326 printDebugInfo(*I, "\nChild: ");
1327 }
1328 }
1329 llvm::dbgs() << "\n";
1330}
1331
Daniel Jasperbac016b2012-12-03 18:12:45 +00001332void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001333 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001334 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001335 DEBUG({
Daniel Jasper567dcf92013-09-05 09:29:45 +00001336 if (CurrentLines == &Lines)
1337 printDebugInfo(*Line);
Manuel Klimek8fa37992013-01-16 12:31:12 +00001338 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001339 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001340 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001341 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001342 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jasper516fb312013-03-01 18:11:39 +00001343 I = PreprocessorDirectives.begin(),
1344 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001345 I != E; ++I) {
1346 CurrentLines->push_back(*I);
1347 }
1348 PreprocessorDirectives.clear();
1349 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001350}
1351
Manuel Klimek96e888b2013-05-28 11:55:06 +00001352bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001353
Manuel Klimek86721d22013-01-22 16:31:55 +00001354void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1355 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001356 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001357 I = CommentsBeforeNextToken.begin(),
1358 E = CommentsBeforeNextToken.end();
1359 I != E; ++I) {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001360 if ((*I)->NewlinesBefore && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001361 addUnwrappedLine();
1362 }
1363 pushToken(*I);
1364 }
1365 if (NewlineBeforeNext && JustComments) {
1366 addUnwrappedLine();
1367 }
1368 CommentsBeforeNextToken.clear();
1369}
1370
Daniel Jasperbac016b2012-12-03 18:12:45 +00001371void UnwrappedLineParser::nextToken() {
1372 if (eof())
1373 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001374 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001375 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001376 readToken();
1377}
1378
1379void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001380 bool CommentsInCurrentLine = true;
1381 do {
1382 FormatTok = Tokens->getNextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001383 assert(FormatTok);
Manuel Klimek96e888b2013-05-28 11:55:06 +00001384 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1385 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001386 // If there is an unfinished unwrapped line, we flush the preprocessor
1387 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001388 bool SwitchToPreprocessorLines =
1389 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001390 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001391 // Comments stored before the preprocessor directive need to be output
1392 // before the preprocessor directive, at the same level as the
1393 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001394 flushComments(FormatTok->NewlinesBefore > 0);
Manuel Klimek86721d22013-01-22 16:31:55 +00001395 parsePPDirective();
1396 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001397
1398 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1399 !Line->InPPDirective) {
1400 continue;
1401 }
1402
Manuel Klimek96e888b2013-05-28 11:55:06 +00001403 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001404 return;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001405 if (FormatTok->NewlinesBefore > 0 || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001406 CommentsInCurrentLine = false;
1407 }
1408 if (CommentsInCurrentLine) {
1409 pushToken(FormatTok);
1410 } else {
1411 CommentsBeforeNextToken.push_back(FormatTok);
1412 }
1413 } while (!eof());
1414}
1415
Manuel Klimek96e888b2013-05-28 11:55:06 +00001416void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001417 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001418 if (MustBreakBeforeNextToken) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001419 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001420 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001421 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001422}
1423
Daniel Jaspercd162382013-01-07 13:26:07 +00001424} // end namespace format
1425} // end namespace clang