blob: af1e94cfe844a2736dee99322f94874cadaa3d52 [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
Chandler Carruthb1ba0ef2013-01-19 08:09:44 +000016#include "UnwrappedLineParser.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000017#include "llvm/Support/Debug.h"
Manuel Klimek8fa37992013-01-16 12:31:12 +000018
Stephen Hines6bcf27b2014-05-29 04:14:42 -070019#define DEBUG_TYPE "format-parser"
20
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),
Stephen Hines6bcf27b2014-05-29 04:14:42 -070063 PreviousStructuralError(StructuralError), Token(nullptr) {
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)
Stephen Hines176edba2014-12-01 14:53:08 -0800125 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimek525fe162013-01-18 14:04:34 +0000126 if (SwitchToPreprocessorLines)
127 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000128 else if (!Parser.Line->Tokens.empty())
129 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
Stephen Hines176edba2014-12-01 14:53:08 -0800130 PreBlockLine = std::move(Parser.Line);
131 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000132 Parser.Line->Level = PreBlockLine->Level;
133 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000134 }
135
136 ~ScopedLineState() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000137 if (!Parser.Line->Tokens.empty()) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000138 Parser.addUnwrappedLine();
139 }
Daniel Jaspercbb6c412013-01-16 09:10:19 +0000140 assert(Parser.Line->Tokens.empty());
Stephen Hines176edba2014-12-01 14:53:08 -0800141 Parser.Line = std::move(PreBlockLine);
Daniel Jasper567dcf92013-09-05 09:29:45 +0000142 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
143 Parser.MustBreakBeforeNextToken = true;
144 Parser.CurrentLines = OriginalLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000145 }
146
147private:
148 UnwrappedLineParser &Parser;
149
Stephen Hines176edba2014-12-01 14:53:08 -0800150 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper567dcf92013-09-05 09:29:45 +0000151 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000152};
153
Stephen Hines651f13c2014-04-23 16:59:28 -0700154class CompoundStatementIndenter {
155public:
156 CompoundStatementIndenter(UnwrappedLineParser *Parser,
157 const FormatStyle &Style, unsigned &LineLevel)
158 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
159 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
160 Parser->addUnwrappedLine();
161 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
162 Parser->addUnwrappedLine();
163 ++LineLevel;
164 }
165 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700166 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Stephen Hines651f13c2014-04-23 16:59:28 -0700167
168private:
169 unsigned &LineLevel;
170 unsigned OldLineLevel;
171};
172
Craig Toppere50947f2013-07-01 04:21:54 +0000173namespace {
174
Manuel Klimek80829bd2013-05-23 09:41:43 +0000175class IndexedTokenSource : public FormatTokenSource {
176public:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000177 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000178 : Tokens(Tokens), Position(-1) {}
179
Stephen Hines651f13c2014-04-23 16:59:28 -0700180 FormatToken *getNextToken() override {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000181 ++Position;
182 return Tokens[Position];
183 }
184
Stephen Hines651f13c2014-04-23 16:59:28 -0700185 unsigned getPosition() override {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000186 assert(Position >= 0);
187 return Position;
188 }
189
Stephen Hines651f13c2014-04-23 16:59:28 -0700190 FormatToken *setPosition(unsigned P) override {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000191 Position = P;
192 return Tokens[Position];
193 }
194
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000195 void reset() { Position = -1; }
196
Manuel Klimek80829bd2013-05-23 09:41:43 +0000197private:
Manuel Klimek96e888b2013-05-28 11:55:06 +0000198 ArrayRef<FormatToken *> Tokens;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000199 int Position;
200};
201
Craig Toppere50947f2013-07-01 04:21:54 +0000202} // end anonymous namespace
203
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000204UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Stephen Hines176edba2014-12-01 14:53:08 -0800205 const AdditionalKeywords &Keywords,
Manuel Klimek96e888b2013-05-28 11:55:06 +0000206 ArrayRef<FormatToken *> Tokens,
Daniel Jaspercaf42a32013-05-15 08:14:19 +0000207 UnwrappedLineConsumer &Callback)
Manuel Klimek525fe162013-01-18 14:04:34 +0000208 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700209 CurrentLines(&Lines), StructuralError(false), Style(Style),
Stephen Hines176edba2014-12-01 14:53:08 -0800210 Keywords(Keywords), Tokens(nullptr), Callback(Callback),
211 AllTokens(Tokens), PPBranchLevel(-1) {}
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000212
213void UnwrappedLineParser::reset() {
214 PPBranchLevel = -1;
215 Line.reset(new UnwrappedLine);
216 CommentsBeforeNextToken.clear();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700217 FormatTok = nullptr;
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000218 MustBreakBeforeNextToken = false;
219 PreprocessorDirectives.clear();
220 CurrentLines = &Lines;
221 DeclarationScopeStack.clear();
222 StructuralError = false;
223 PPStack.clear();
224}
Daniel Jasperbac016b2012-12-03 18:12:45 +0000225
Alexander Kornienkocff563c2012-12-04 17:27:50 +0000226bool UnwrappedLineParser::parse() {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000227 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000228 do {
229 DEBUG(llvm::dbgs() << "----\n");
230 reset();
231 Tokens = &TokenSource;
232 TokenSource.reset();
Daniel Jasper516fb312013-03-01 18:11:39 +0000233
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000234 readToken();
235 parseFile();
236 // Create line with eof token.
237 pushToken(FormatTok);
238 addUnwrappedLine();
239
240 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
241 E = Lines.end();
242 I != E; ++I) {
243 Callback.consumeUnwrappedLine(*I);
244 }
245 Callback.finishRun();
246 Lines.clear();
247 while (!PPLevelBranchIndex.empty() &&
Daniel Jasperac4d0182013-10-12 13:32:56 +0000248 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000249 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
250 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
251 }
252 if (!PPLevelBranchIndex.empty()) {
253 ++PPLevelBranchIndex.back();
254 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
255 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
256 }
257 } while (!PPLevelBranchIndex.empty());
258
Manuel Klimek67d080d2013-04-12 14:13:36 +0000259 return StructuralError;
Manuel Klimekd4397b92013-01-04 23:34:14 +0000260}
261
Manuel Klimek67d080d2013-04-12 14:13:36 +0000262void UnwrappedLineParser::parseFile() {
Daniel Jasper627707b2013-03-22 16:55:40 +0000263 ScopedDeclarationState DeclarationState(
264 *Line, DeclarationScopeStack,
265 /*MustBeDeclaration=*/ !Line->InPPDirective);
Nico Weber27268772013-06-26 00:30:14 +0000266 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000267 // Make sure to format the remaining tokens.
Manuel Klimek86721d22013-01-22 16:31:55 +0000268 flushComments(true);
Manuel Klimekd4397b92013-01-04 23:34:14 +0000269 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000270}
271
Manuel Klimek67d080d2013-04-12 14:13:36 +0000272void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000273 bool SwitchLabelEncountered = false;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000274 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000275 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +0000276 case tok::comment:
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000277 nextToken();
278 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000279 break;
280 case tok::l_brace:
Manuel Klimek70b03f42013-01-23 09:32:48 +0000281 // FIXME: Add parameter whether this can happen - if this happens, we must
282 // be in a non-declaration context.
Nico Weber27268772013-06-26 00:30:14 +0000283 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +0000284 addUnwrappedLine();
285 break;
286 case tok::r_brace:
Manuel Klimek67d080d2013-04-12 14:13:36 +0000287 if (HasOpeningBrace)
288 return;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000289 StructuralError = true;
290 nextToken();
291 addUnwrappedLine();
Manuel Klimeka5342db2013-01-06 20:07:31 +0000292 break;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000293 case tok::kw_default:
294 case tok::kw_case:
Daniel Jasper67cf1db2013-09-02 08:26:29 +0000295 if (!SwitchLabelEncountered &&
296 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
297 ++Line->Level;
Daniel Jaspere865cc52013-07-25 11:31:57 +0000298 SwitchLabelEncountered = true;
299 parseStructuralElement();
300 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000301 default:
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000302 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000303 break;
304 }
305 } while (!eof());
306}
307
Manuel Klimek80829bd2013-05-23 09:41:43 +0000308void UnwrappedLineParser::calculateBraceTypes() {
309 // We'll parse forward through the tokens until we hit
310 // a closing brace or eof - note that getNextToken() will
311 // parse macros, so this will magically work inside macro
312 // definitions, too.
313 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000314 FormatToken *Tok = FormatTok;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000315 // Keep a stack of positions of lbrace tokens. We will
316 // update information about whether an lbrace starts a
317 // braced init list or a different block during the loop.
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000318 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek96e888b2013-05-28 11:55:06 +0000319 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimek80829bd2013-05-23 09:41:43 +0000320 do {
Daniel Jasper02eacc22013-07-01 09:15:46 +0000321 // Get next none-comment token.
322 FormatToken *NextTok;
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000323 unsigned ReadTokens = 0;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000324 do {
325 NextTok = Tokens->getNextToken();
Daniel Jasperf50dbfa2013-07-01 16:43:38 +0000326 ++ReadTokens;
Daniel Jasper02eacc22013-07-01 09:15:46 +0000327 } while (NextTok->is(tok::comment));
328
Manuel Klimek96e888b2013-05-28 11:55:06 +0000329 switch (Tok->Tok.getKind()) {
Manuel Klimek80829bd2013-05-23 09:41:43 +0000330 case tok::l_brace:
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000331 LBraceStack.push_back(Tok);
Manuel Klimek80829bd2013-05-23 09:41:43 +0000332 break;
333 case tok::r_brace:
334 if (!LBraceStack.empty()) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000335 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700336 bool ProbablyBracedList = false;
337 if (Style.Language == FormatStyle::LK_Proto) {
338 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
339 } else {
340 // Using OriginalColumn to distinguish between ObjC methods and
341 // binary operators is a bit hacky.
342 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
343 NextTok->OriginalColumn == 0;
344
345 // If there is a comma, semicolon or right paren after the closing
346 // brace, we assume this is a braced initializer list. Note that
347 // regardless how we mark inner braces here, we will overwrite the
348 // BlockKind later if we parse a braced list (where all blocks
349 // inside are by default braced lists), or when we explicitly detect
350 // blocks (for example while parsing lambdas).
351 //
352 // We exclude + and - as they can be ObjC visibility modifiers.
353 ProbablyBracedList =
354 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
355 tok::r_paren, tok::r_square, tok::l_brace,
Stephen Hines176edba2014-12-01 14:53:08 -0800356 tok::l_paren, tok::ellipsis) ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700357 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
358 }
359 if (ProbablyBracedList) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000360 Tok->BlockKind = BK_BracedInit;
361 LBraceStack.back()->BlockKind = BK_BracedInit;
362 } else {
363 Tok->BlockKind = BK_Block;
364 LBraceStack.back()->BlockKind = BK_Block;
365 }
Manuel Klimek80829bd2013-05-23 09:41:43 +0000366 }
367 LBraceStack.pop_back();
368 }
369 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700370 case tok::at:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000371 case tok::semi:
372 case tok::kw_if:
373 case tok::kw_while:
374 case tok::kw_for:
375 case tok::kw_switch:
376 case tok::kw_try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000377 if (!LBraceStack.empty())
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000378 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000379 break;
380 default:
381 break;
382 }
383 Tok = NextTok;
Manuel Klimek31e44f72013-09-04 08:20:47 +0000384 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimek80829bd2013-05-23 09:41:43 +0000385 // Assume other blocks for all unclosed opening braces.
386 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000387 if (LBraceStack[i]->BlockKind == BK_Unknown)
388 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000389 }
Manuel Klimek31e44f72013-09-04 08:20:47 +0000390
Manuel Klimek80829bd2013-05-23 09:41:43 +0000391 FormatTok = Tokens->setPosition(StoredPosition);
392}
393
Manuel Klimekaabfb272013-10-12 22:46:56 +0000394void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
395 bool MunchSemi) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000396 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jaspere865cc52013-07-25 11:31:57 +0000397 unsigned InitialLevel = Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000398 nextToken();
399
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000400 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000401
Manuel Klimek70b03f42013-01-23 09:32:48 +0000402 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
403 MustBeDeclaration);
Daniel Jaspereff18b92013-07-31 23:16:02 +0000404 if (AddLevel)
405 ++Line->Level;
Nico Weber27268772013-06-26 00:30:14 +0000406 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000407
Manuel Klimek96e888b2013-05-28 11:55:06 +0000408 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000409 Line->Level = InitialLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000410 StructuralError = true;
411 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000412 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000413
Daniel Jasperf9955d32013-03-20 12:37:50 +0000414 nextToken(); // Munch the closing brace.
Manuel Klimekaabfb272013-10-12 22:46:56 +0000415 if (MunchSemi && FormatTok->Tok.is(tok::semi))
416 nextToken();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000417 Line->Level = InitialLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000418}
419
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700420static bool IsGoogScope(const UnwrappedLine &Line) {
421 if (Line.Tokens.size() < 4)
422 return false;
423 auto I = Line.Tokens.begin();
424 if (I->Tok->TokenText != "goog")
425 return false;
426 ++I;
427 if (I->Tok->isNot(tok::period))
428 return false;
429 ++I;
430 if (I->Tok->TokenText != "scope")
431 return false;
432 ++I;
433 return I->Tok->is(tok::l_paren);
434}
435
Stephen Hines176edba2014-12-01 14:53:08 -0800436static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
437 const FormatToken &InitialToken) {
438 switch (Style.BreakBeforeBraces) {
439 case FormatStyle::BS_Linux:
440 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
441 case FormatStyle::BS_Allman:
442 case FormatStyle::BS_GNU:
443 return true;
444 default:
445 return false;
446 }
447}
448
Manuel Klimek753a5112013-09-04 13:25:30 +0000449void UnwrappedLineParser::parseChildBlock() {
450 FormatTok->BlockKind = BK_Block;
451 nextToken();
452 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700453 bool GoogScope =
454 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
Manuel Klimek753a5112013-09-04 13:25:30 +0000455 ScopedLineState LineState(*this);
456 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
457 /*MustBeDeclaration=*/false);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700458 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek753a5112013-09-04 13:25:30 +0000459 parseLevel(/*HasOpeningBrace=*/true);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700460 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek753a5112013-09-04 13:25:30 +0000461 }
462 nextToken();
463}
464
Daniel Jasperbac016b2012-12-03 18:12:45 +0000465void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000466 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000467 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000468 nextToken();
469
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700470 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000471 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000472 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000473 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000474
Manuel Klimek96e888b2013-05-28 11:55:06 +0000475 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000476 case tok::pp_define:
477 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000478 return;
479 case tok::pp_if:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000480 parsePPIf(/*IfDef=*/false);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000481 break;
482 case tok::pp_ifdef:
483 case tok::pp_ifndef:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000484 parsePPIf(/*IfDef=*/true);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000485 break;
486 case tok::pp_else:
487 parsePPElse();
488 break;
489 case tok::pp_elif:
490 parsePPElIf();
491 break;
492 case tok::pp_endif:
493 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000494 break;
495 default:
496 parsePPUnknown();
497 break;
498 }
499}
500
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700501void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
502 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000503 PPStack.push_back(PP_Unreachable);
504 else
505 PPStack.push_back(PP_Conditional);
506}
507
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700508void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000509 ++PPBranchLevel;
510 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
511 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
512 PPLevelBranchIndex.push_back(0);
513 PPLevelBranchCount.push_back(0);
514 }
515 PPChainBranchIndex.push(0);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700516 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
517 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000518}
519
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700520void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000521 if (!PPStack.empty())
522 PPStack.pop_back();
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000523 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
524 if (!PPChainBranchIndex.empty())
525 ++PPChainBranchIndex.top();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700526 conditionalCompilationCondition(
527 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
528 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000529}
530
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700531void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000532 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
533 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
534 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000535 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
536 }
537 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700538 // Guard against #endif's without #if.
539 if (PPBranchLevel > 0)
540 --PPBranchLevel;
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000541 if (!PPChainBranchIndex.empty())
542 PPChainBranchIndex.pop();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000543 if (!PPStack.empty())
544 PPStack.pop_back();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700545}
546
547void UnwrappedLineParser::parsePPIf(bool IfDef) {
548 nextToken();
549 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
550 StringRef(FormatTok->Tok.getLiteralData(),
551 FormatTok->Tok.getLength()) == "0") ||
552 FormatTok->Tok.is(tok::kw_false);
553 conditionalCompilationStart(!IfDef && IsLiteralFalse);
554 parsePPUnknown();
555}
556
557void UnwrappedLineParser::parsePPElse() {
558 conditionalCompilationAlternative();
559 parsePPUnknown();
560}
561
562void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
563
564void UnwrappedLineParser::parsePPEndIf() {
565 conditionalCompilationEnd();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000566 parsePPUnknown();
567}
568
Manuel Klimekd4397b92013-01-04 23:34:14 +0000569void UnwrappedLineParser::parsePPDefine() {
570 nextToken();
571
Manuel Klimek96e888b2013-05-28 11:55:06 +0000572 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000573 parsePPUnknown();
574 return;
575 }
576 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000577 if (FormatTok->Tok.getKind() == tok::l_paren &&
578 FormatTok->WhitespaceRange.getBegin() ==
579 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000580 parseParens();
581 }
582 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000583 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000584
585 // Errors during a preprocessor directive can only affect the layout of the
586 // preprocessor directive, and thus we ignore them. An alternative approach
587 // would be to use the same approach we use on the file level (no
588 // re-indentation if there was a structural error) within the macro
589 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000590 parseFile();
591}
592
593void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000594 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000595 nextToken();
596 } while (!eof());
597 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000598}
599
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000600// Here we blacklist certain tokens that are not usually the first token in an
601// unwrapped line. This is used in attempt to distinguish macro calls without
602// trailing semicolons from other constructs split to several lines.
603bool tokenCanStartNewLine(clang::Token Tok) {
604 // Semicolon can be a null-statement, l_square can be a start of a macro or
605 // a C++11 attribute, but this doesn't seem to be common.
606 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
607 Tok.isNot(tok::l_square) &&
608 // Tokens that can only be used as binary operators and a part of
609 // overloaded operator names.
610 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
611 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
612 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
613 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
614 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
615 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
616 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
617 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
618 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
619 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
620 Tok.isNot(tok::lesslessequal) &&
621 // Colon is used in labels, base class lists, initializer lists,
622 // range-based for loops, ternary operator, but should never be the
623 // first token in an unwrapped line.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700624 Tok.isNot(tok::colon) &&
625 // 'noexcept' is a trailing annotation.
626 Tok.isNot(tok::kw_noexcept);
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000627}
628
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000629void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000630 assert(!FormatTok->Tok.is(tok::l_brace));
631 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000632 case tok::at:
633 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000634 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000635 parseBracedList();
636 break;
637 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000638 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000639 case tok::objc_public:
640 case tok::objc_protected:
641 case tok::objc_package:
642 case tok::objc_private:
643 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000644 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000645 case tok::objc_implementation:
646 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000647 case tok::objc_protocol:
648 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000649 case tok::objc_end:
650 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000651 case tok::objc_optional:
652 case tok::objc_required:
653 nextToken();
654 addUnwrappedLine();
655 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000656 default:
657 break;
658 }
659 break;
Stephen Hines176edba2014-12-01 14:53:08 -0800660 case tok::kw_asm:
661 FormatTok->Finalized = true;
662 nextToken();
663 if (FormatTok->is(tok::l_brace)) {
664 while (FormatTok && FormatTok->isNot(tok::eof)) {
665 FormatTok->Finalized = true;
666 if (FormatTok->is(tok::r_brace)) {
667 nextToken();
668 break;
669 }
670 nextToken();
671 }
672 }
673 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000674 case tok::kw_namespace:
675 parseNamespace();
676 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000677 case tok::kw_inline:
678 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000679 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000680 parseNamespace();
681 return;
682 }
683 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000684 case tok::kw_public:
685 case tok::kw_protected:
686 case tok::kw_private:
Stephen Hines176edba2014-12-01 14:53:08 -0800687 if (Style.Language == FormatStyle::LK_Java)
688 nextToken();
689 else
690 parseAccessSpecifier();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000691 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000692 case tok::kw_if:
693 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000694 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000695 case tok::kw_for:
696 case tok::kw_while:
697 parseForOrWhileLoop();
698 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000699 case tok::kw_do:
700 parseDoWhile();
701 return;
702 case tok::kw_switch:
703 parseSwitch();
704 return;
705 case tok::kw_default:
706 nextToken();
707 parseLabel();
708 return;
709 case tok::kw_case:
710 parseCaseLabel();
711 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700712 case tok::kw_try:
713 parseTryCatch();
714 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000715 case tok::kw_extern:
716 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000717 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000718 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000719 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000720 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000721 addUnwrappedLine();
722 return;
723 }
724 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700725 break;
726 case tok::identifier:
727 if (FormatTok->IsForEachMacro) {
728 parseForOrWhileLoop();
729 return;
730 }
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000731 // In all other cases, parse the declaration.
732 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000733 default:
734 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000735 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000736 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000737 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000738 case tok::at:
739 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000740 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000741 parseBracedList();
742 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000743 case tok::kw_enum:
744 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000745 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700746 case tok::kw_typedef:
747 nextToken();
Stephen Hines176edba2014-12-01 14:53:08 -0800748 if (FormatTok->is(Keywords.kw_NS_ENUM))
Stephen Hines651f13c2014-04-23 16:59:28 -0700749 parseEnum();
750 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000751 case tok::kw_struct:
752 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000753 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000754 parseRecord();
755 // A record declaration or definition is always the start of a structural
756 // element.
757 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000758 case tok::semi:
759 nextToken();
760 addUnwrappedLine();
761 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000762 case tok::r_brace:
763 addUnwrappedLine();
764 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000765 case tok::l_paren:
766 parseParens();
767 break;
Manuel Klimek753a5112013-09-04 13:25:30 +0000768 case tok::caret:
769 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700770 if (FormatTok->Tok.isAnyIdentifier() ||
771 FormatTok->isSimpleTypeSpecifier())
772 nextToken();
773 if (FormatTok->is(tok::l_paren))
774 parseParens();
775 if (FormatTok->is(tok::l_brace))
Manuel Klimek753a5112013-09-04 13:25:30 +0000776 parseChildBlock();
Manuel Klimek753a5112013-09-04 13:25:30 +0000777 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000778 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000779 if (!tryToParseBracedList()) {
780 // A block outside of parentheses must be the last part of a
781 // structural element.
782 // FIXME: Figure out cases where this is not true, and add projections
783 // for them (the one we know is missing are lambdas).
Stephen Hines651f13c2014-04-23 16:59:28 -0700784 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000785 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -0700786 FormatTok->Type = TT_FunctionLBrace;
Nico Weber27268772013-06-26 00:30:14 +0000787 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000788 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000789 return;
790 }
791 // Otherwise this was a braced init list, and the structural
792 // element continues.
793 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700794 case tok::kw_try:
795 // We arrive here when parsing function-try blocks.
796 parseTryCatch();
797 return;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000798 case tok::identifier: {
799 StringRef Text = FormatTok->TokenText;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700800 // Parse function literal unless 'function' is the first token in a line
801 // in which case this should be treated as a free-standing function.
802 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
803 Line->Tokens.size() > 0) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700804 tryToParseJSFunction();
805 break;
806 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000807 nextToken();
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000808 if (Line->Tokens.size() == 1) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000809 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000810 parseLabel();
811 return;
812 }
Stephen Hines176edba2014-12-01 14:53:08 -0800813 // Recognize function-like macro usages without trailing semicolon as
814 // well as free-standing macrose like Q_OBJECT.
815 bool FunctionLike = FormatTok->is(tok::l_paren);
816 if (FunctionLike)
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000817 parseParens();
Stephen Hines176edba2014-12-01 14:53:08 -0800818 if (FormatTok->NewlinesBefore > 0 &&
819 (Text.size() >= 5 || FunctionLike) &&
820 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000821 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000822 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000823 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000824 }
825 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000826 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000827 case tok::equal:
828 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000829 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000830 parseBracedList();
831 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000832 break;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000833 case tok::l_square:
Stephen Hines651f13c2014-04-23 16:59:28 -0700834 parseSquare();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000835 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000836 default:
837 nextToken();
838 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000839 }
840 } while (!eof());
841}
842
Stephen Hines651f13c2014-04-23 16:59:28 -0700843bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper2d657052013-09-05 11:49:39 +0000844 // FIXME: This is a dirty way to access the previous token. Find a better
845 // solution.
Daniel Jasper520cca82013-09-06 21:25:51 +0000846 if (!Line->Tokens.empty() &&
Stephen Hines176edba2014-12-01 14:53:08 -0800847 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
848 tok::kw_new, tok::kw_delete) ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700849 Line->Tokens.back().Tok->closesScope() ||
850 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasper2d657052013-09-05 11:49:39 +0000851 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700852 return false;
Daniel Jasper2d657052013-09-05 11:49:39 +0000853 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000854 assert(FormatTok->is(tok::l_square));
855 FormatToken &LSquare = *FormatTok;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000856 if (!tryToParseLambdaIntroducer())
Stephen Hines651f13c2014-04-23 16:59:28 -0700857 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000858
859 while (FormatTok->isNot(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700860 if (FormatTok->isSimpleTypeSpecifier()) {
861 nextToken();
862 continue;
863 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000864 switch (FormatTok->Tok.getKind()) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000865 case tok::l_brace:
866 break;
867 case tok::l_paren:
868 parseParens();
869 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700870 case tok::less:
871 case tok::greater:
Daniel Jasperac2c9742013-09-05 10:04:31 +0000872 case tok::identifier:
Stephen Hines651f13c2014-04-23 16:59:28 -0700873 case tok::coloncolon:
Daniel Jasperac2c9742013-09-05 10:04:31 +0000874 case tok::kw_mutable:
875 nextToken();
876 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700877 case tok::arrow:
878 FormatTok->Type = TT_TrailingReturnArrow;
879 nextToken();
880 break;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000881 default:
Stephen Hines651f13c2014-04-23 16:59:28 -0700882 return true;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000883 }
884 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000885 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek753a5112013-09-04 13:25:30 +0000886 parseChildBlock();
Stephen Hines651f13c2014-04-23 16:59:28 -0700887 return true;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000888}
889
890bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
891 nextToken();
892 if (FormatTok->is(tok::equal)) {
893 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000894 if (FormatTok->is(tok::r_square)) {
895 nextToken();
896 return true;
897 }
898 if (FormatTok->isNot(tok::comma))
899 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000900 nextToken();
901 } else if (FormatTok->is(tok::amp)) {
902 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000903 if (FormatTok->is(tok::r_square)) {
904 nextToken();
905 return true;
906 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000907 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
908 return false;
909 }
Daniel Jasperac2c9742013-09-05 10:04:31 +0000910 if (FormatTok->is(tok::comma))
911 nextToken();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000912 } else if (FormatTok->is(tok::r_square)) {
913 nextToken();
914 return true;
915 }
916 do {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000917 if (FormatTok->is(tok::amp))
918 nextToken();
919 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
920 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000921 nextToken();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700922 if (FormatTok->is(tok::ellipsis))
923 nextToken();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000924 if (FormatTok->is(tok::comma)) {
925 nextToken();
926 } else if (FormatTok->is(tok::r_square)) {
927 nextToken();
928 return true;
929 } else {
930 return false;
931 }
932 } while (!eof());
933 return false;
934}
935
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700936void UnwrappedLineParser::tryToParseJSFunction() {
937 nextToken();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700938
939 // Consume function name.
940 if (FormatTok->is(tok::identifier))
941 nextToken();
942
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700943 if (FormatTok->isNot(tok::l_paren))
944 return;
945 nextToken();
946 while (FormatTok->isNot(tok::l_brace)) {
947 // Err on the side of caution in order to avoid consuming the full file in
948 // case of incomplete code.
949 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
950 tok::comment))
951 return;
952 nextToken();
953 }
954 parseChildBlock();
955}
956
Manuel Klimek80829bd2013-05-23 09:41:43 +0000957bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000958 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000959 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000960 assert(FormatTok->BlockKind != BK_Unknown);
961 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000962 return false;
963 parseBracedList();
964 return true;
965}
966
Daniel Jasper57981202013-09-13 09:20:45 +0000967bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
968 bool HasError = false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000969 nextToken();
970
Manuel Klimek423dd932013-04-10 09:52:05 +0000971 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
972 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000973 do {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700974 if (Style.Language == FormatStyle::LK_JavaScript &&
Stephen Hines176edba2014-12-01 14:53:08 -0800975 FormatTok->is(Keywords.kw_function)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700976 tryToParseJSFunction();
977 continue;
978 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000979 switch (FormatTok->Tok.getKind()) {
Manuel Klimek753a5112013-09-04 13:25:30 +0000980 case tok::caret:
981 nextToken();
982 if (FormatTok->is(tok::l_brace)) {
983 parseChildBlock();
984 }
985 break;
986 case tok::l_square:
987 tryToParseLambda();
988 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000989 case tok::l_brace:
Manuel Klimek31e44f72013-09-04 08:20:47 +0000990 // Assume there are no blocks inside a braced init list apart
991 // from the ones we explicitly parse out (like lambdas).
992 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000993 parseBracedList();
994 break;
995 case tok::r_brace:
996 nextToken();
Daniel Jasper57981202013-09-13 09:20:45 +0000997 return !HasError;
Manuel Klimek423dd932013-04-10 09:52:05 +0000998 case tok::semi:
Daniel Jasper57981202013-09-13 09:20:45 +0000999 HasError = true;
1000 if (!ContinueOnSemicolons)
1001 return !HasError;
1002 nextToken();
1003 break;
Manuel Klimek423dd932013-04-10 09:52:05 +00001004 case tok::comma:
1005 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +00001006 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001007 default:
1008 nextToken();
1009 break;
1010 }
1011 } while (!eof());
Daniel Jasper57981202013-09-13 09:20:45 +00001012 return false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001013}
1014
Daniel Jasperbac016b2012-12-03 18:12:45 +00001015void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001016 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001017 nextToken();
1018 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001019 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001020 case tok::l_paren:
1021 parseParens();
1022 break;
1023 case tok::r_paren:
1024 nextToken();
1025 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +00001026 case tok::r_brace:
1027 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1028 return;
Daniel Jasperac2c9742013-09-05 10:04:31 +00001029 case tok::l_square:
1030 tryToParseLambda();
1031 break;
Nico Weber2afbe522013-02-10 04:38:23 +00001032 case tok::l_brace: {
Manuel Klimek80829bd2013-05-23 09:41:43 +00001033 if (!tryToParseBracedList()) {
Manuel Klimekb7d98a12013-09-04 13:34:14 +00001034 parseChildBlock();
Manuel Klimek80829bd2013-05-23 09:41:43 +00001035 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001036 break;
Nico Weber2afbe522013-02-10 04:38:23 +00001037 }
Nico Weberd74fcdb2013-02-10 20:35:35 +00001038 case tok::at:
1039 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001040 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +00001041 parseBracedList();
1042 break;
Stephen Hines176edba2014-12-01 14:53:08 -08001043 case tok::identifier:
1044 if (Style.Language == FormatStyle::LK_JavaScript &&
1045 FormatTok->is(Keywords.kw_function))
1046 tryToParseJSFunction();
1047 else
1048 nextToken();
1049 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001050 default:
1051 nextToken();
1052 break;
1053 }
1054 } while (!eof());
1055}
1056
Stephen Hines651f13c2014-04-23 16:59:28 -07001057void UnwrappedLineParser::parseSquare() {
1058 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1059 if (tryToParseLambda())
1060 return;
1061 do {
1062 switch (FormatTok->Tok.getKind()) {
1063 case tok::l_paren:
1064 parseParens();
1065 break;
1066 case tok::r_square:
1067 nextToken();
1068 return;
1069 case tok::r_brace:
1070 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1071 return;
1072 case tok::l_square:
1073 parseSquare();
1074 break;
1075 case tok::l_brace: {
1076 if (!tryToParseBracedList()) {
1077 parseChildBlock();
1078 }
1079 break;
1080 }
1081 case tok::at:
1082 nextToken();
1083 if (FormatTok->Tok.is(tok::l_brace))
1084 parseBracedList();
1085 break;
1086 default:
1087 nextToken();
1088 break;
1089 }
1090 } while (!eof());
1091}
1092
Daniel Jasperbac016b2012-12-03 18:12:45 +00001093void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001094 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001095 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001096 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +00001097 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001098 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001099 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001100 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001101 parseBlock(/*MustBeDeclaration=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07001102 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1103 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001104 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001105 } else {
Manuel Klimeke4907052013-08-02 21:31:59 +00001106 NeedsUnwrappedLine = true;
Stephen Hines651f13c2014-04-23 16:59:28 -07001107 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001108 } else {
1109 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001110 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001111 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001112 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001113 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001114 if (FormatTok->Tok.is(tok::kw_else)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001115 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1116 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001117 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001118 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001119 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001120 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001121 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001122 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001123 parseIfThenElse();
1124 } else {
1125 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001126 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001127 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001128 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001129 }
1130 } else if (NeedsUnwrappedLine) {
1131 addUnwrappedLine();
1132 }
1133}
1134
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001135void UnwrappedLineParser::parseTryCatch() {
1136 assert(FormatTok->is(tok::kw_try) && "'try' expected");
1137 nextToken();
1138 bool NeedsUnwrappedLine = false;
1139 if (FormatTok->is(tok::colon)) {
1140 // We are in a function try block, what comes is an initializer list.
1141 nextToken();
1142 while (FormatTok->is(tok::identifier)) {
1143 nextToken();
1144 if (FormatTok->is(tok::l_paren))
1145 parseParens();
1146 else
1147 StructuralError = true;
1148 if (FormatTok->is(tok::comma))
1149 nextToken();
1150 }
1151 }
1152 if (FormatTok->is(tok::l_brace)) {
1153 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1154 parseBlock(/*MustBeDeclaration=*/false);
1155 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1156 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1157 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1158 addUnwrappedLine();
1159 } else {
1160 NeedsUnwrappedLine = true;
1161 }
1162 } else if (!FormatTok->is(tok::kw_catch)) {
1163 // The C++ standard requires a compound-statement after a try.
1164 // If there's none, we try to assume there's a structuralElement
1165 // and try to continue.
1166 StructuralError = true;
1167 addUnwrappedLine();
1168 ++Line->Level;
1169 parseStructuralElement();
1170 --Line->Level;
1171 }
1172 while (FormatTok->is(tok::kw_catch) ||
Stephen Hines176edba2014-12-01 14:53:08 -08001173 ((Style.Language == FormatStyle::LK_Java ||
1174 Style.Language == FormatStyle::LK_JavaScript) &&
1175 FormatTok->is(Keywords.kw_finally))) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001176 nextToken();
1177 while (FormatTok->isNot(tok::l_brace)) {
1178 if (FormatTok->is(tok::l_paren)) {
1179 parseParens();
1180 continue;
1181 }
1182 if (FormatTok->isOneOf(tok::semi, tok::r_brace))
1183 return;
1184 nextToken();
1185 }
1186 NeedsUnwrappedLine = false;
1187 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1188 parseBlock(/*MustBeDeclaration=*/false);
1189 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1190 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1191 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1192 addUnwrappedLine();
1193 } else {
1194 NeedsUnwrappedLine = true;
1195 }
1196 }
1197 if (NeedsUnwrappedLine) {
1198 addUnwrappedLine();
1199 }
1200}
1201
Alexander Kornienko15757312012-12-06 18:03:27 +00001202void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001203 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Stephen Hines176edba2014-12-01 14:53:08 -08001204
1205 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko15757312012-12-06 18:03:27 +00001206 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001207 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +00001208 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001209 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001210 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimek44135b82013-05-13 12:51:40 +00001211 addUnwrappedLine();
1212
Daniel Jaspereff18b92013-07-31 23:16:02 +00001213 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1214 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1215 DeclarationScopeStack.size() > 1);
1216 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +00001217 // Munch the semicolon after a namespace. This is more common than one would
1218 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001219 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +00001220 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +00001221 addUnwrappedLine();
1222 }
1223 // FIXME: Add error handling.
1224}
1225
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001226void UnwrappedLineParser::parseForOrWhileLoop() {
Stephen Hines651f13c2014-04-23 16:59:28 -07001227 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1228 FormatTok->IsForEachMacro) &&
1229 "'for', 'while' or foreach macro expected");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001230 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001231 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001232 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001233 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001234 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001235 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001236 addUnwrappedLine();
1237 } else {
1238 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001239 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001240 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001241 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001242 }
1243}
1244
Daniel Jasperbac016b2012-12-03 18:12:45 +00001245void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001246 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001247 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001248 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001249 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001250 parseBlock(/*MustBeDeclaration=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07001251 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1252 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001253 } else {
1254 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001255 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001256 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001257 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001258 }
1259
Alexander Kornienko393b0082012-12-04 15:40:36 +00001260 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001261 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +00001262 addUnwrappedLine();
1263 return;
1264 }
1265
Daniel Jasperbac016b2012-12-03 18:12:45 +00001266 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001267 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001268}
1269
1270void UnwrappedLineParser::parseLabel() {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001271 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +00001272 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +00001273 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +00001274 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001275 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001276 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001277 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +00001278 if (FormatTok->Tok.is(tok::kw_break)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001279 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1280 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1281 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001282 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001283 }
Manuel Klimeke4907052013-08-02 21:31:59 +00001284 parseStructuralElement();
1285 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001286 addUnwrappedLine();
1287 } else {
1288 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001289 }
Manuel Klimek526ed112013-01-09 15:25:02 +00001290 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001291}
1292
1293void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001294 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001295 // FIXME: fix handling of complex expressions here.
1296 do {
1297 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001298 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001299 parseLabel();
1300}
1301
1302void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001303 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001304 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001305 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001306 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001307 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001308 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jaspereff18b92013-07-31 23:16:02 +00001309 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001310 addUnwrappedLine();
1311 } else {
1312 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001313 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001314 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001315 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001316 }
1317}
1318
1319void UnwrappedLineParser::parseAccessSpecifier() {
1320 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001321 // Understand Qt's slots.
1322 if (FormatTok->is(tok::identifier) &&
1323 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
1324 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001325 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001326 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001327 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001328 addUnwrappedLine();
1329}
1330
1331void UnwrappedLineParser::parseEnum() {
Stephen Hines176edba2014-12-01 14:53:08 -08001332 // Won't be 'enum' for NS_ENUMs.
1333 if (FormatTok->Tok.is(tok::kw_enum))
Stephen Hines651f13c2014-04-23 16:59:28 -07001334 nextToken();
Stephen Hines176edba2014-12-01 14:53:08 -08001335
Daniel Jaspercbeb1c62013-08-20 12:42:50 +00001336 // Eat up enum class ...
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001337 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1338 nextToken();
Daniel Jaspere27dc5d2013-09-06 21:32:35 +00001339 while (FormatTok->Tok.getIdentifierInfo() ||
Stephen Hines176edba2014-12-01 14:53:08 -08001340 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1341 tok::greater, tok::comma, tok::question)) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001342 nextToken();
1343 // We can have macros or attributes in between 'enum' and the enum name.
Stephen Hines176edba2014-12-01 14:53:08 -08001344 if (FormatTok->is(tok::l_paren))
Alexander Kornienkoa166e732012-12-04 14:46:19 +00001345 parseParens();
Stephen Hines176edba2014-12-01 14:53:08 -08001346 if (FormatTok->is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +00001347 nextToken();
1348 }
Stephen Hines176edba2014-12-01 14:53:08 -08001349
1350 // Just a declaration or something is wrong.
1351 if (FormatTok->isNot(tok::l_brace))
1352 return;
1353 FormatTok->BlockKind = BK_Block;
1354
1355 if (Style.Language == FormatStyle::LK_Java) {
1356 // Java enums are different.
1357 parseJavaEnumBody();
1358 return;
Manuel Klimek308232c2013-01-21 19:17:52 +00001359 }
Stephen Hines176edba2014-12-01 14:53:08 -08001360
1361 // Parse enum body.
1362 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1363 if (HasError) {
1364 if (FormatTok->is(tok::semi))
1365 nextToken();
1366 addUnwrappedLine();
1367 }
1368
Manuel Klimek308232c2013-01-21 19:17:52 +00001369 // We fall through to parsing a structural element afterwards, so that in
1370 // enum A {} n, m;
1371 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +00001372}
1373
Stephen Hines176edba2014-12-01 14:53:08 -08001374void UnwrappedLineParser::parseJavaEnumBody() {
1375 // Determine whether the enum is simple, i.e. does not have a semicolon or
1376 // constants with class bodies. Simple enums can be formatted like braced
1377 // lists, contracted to a single line, etc.
1378 unsigned StoredPosition = Tokens->getPosition();
1379 bool IsSimple = true;
1380 FormatToken *Tok = Tokens->getNextToken();
1381 while (Tok) {
1382 if (Tok->is(tok::r_brace))
1383 break;
1384 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1385 IsSimple = false;
1386 break;
1387 }
1388 // FIXME: This will also mark enums with braces in the arguments to enum
1389 // constants as "not simple". This is probably fine in practice, though.
1390 Tok = Tokens->getNextToken();
1391 }
1392 FormatTok = Tokens->setPosition(StoredPosition);
1393
1394 if (IsSimple) {
1395 parseBracedList();
1396 addUnwrappedLine();
1397 return;
1398 }
1399
1400 // Parse the body of a more complex enum.
1401 // First add a line for everything up to the "{".
Manuel Klimekde768542013-01-07 18:10:23 +00001402 nextToken();
Stephen Hines176edba2014-12-01 14:53:08 -08001403 addUnwrappedLine();
1404 ++Line->Level;
1405
1406 // Parse the enum constants.
1407 while (FormatTok) {
1408 if (FormatTok->is(tok::l_brace)) {
1409 // Parse the constant's class body.
1410 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1411 /*MunchSemi=*/false);
1412 } else if (FormatTok->is(tok::l_paren)) {
1413 parseParens();
1414 } else if (FormatTok->is(tok::comma)) {
1415 nextToken();
1416 addUnwrappedLine();
1417 } else if (FormatTok->is(tok::semi)) {
1418 nextToken();
1419 addUnwrappedLine();
1420 break;
1421 } else if (FormatTok->is(tok::r_brace)) {
1422 addUnwrappedLine();
1423 break;
1424 } else {
1425 nextToken();
1426 }
1427 }
1428
1429 // Parse the class body after the enum's ";" if any.
1430 parseLevel(/*HasOpeningBrace=*/true);
1431 nextToken();
1432 --Line->Level;
1433 addUnwrappedLine();
1434}
1435
1436void UnwrappedLineParser::parseRecord() {
1437 const FormatToken &InitialToken = *FormatTok;
1438 nextToken();
1439 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1440 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001441 nextToken();
1442 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001443 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001444 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +00001445 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +00001446 // The actual identifier can be a nested name specifier, and in macros
1447 // it is often token-pasted.
Stephen Hines176edba2014-12-01 14:53:08 -08001448 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1449 FormatTok->is(tok::hashhash) ||
1450 (Style.Language == FormatStyle::LK_Java &&
1451 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001452 nextToken();
1453
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001454 // Note that parsing away template declarations here leads to incorrectly
1455 // accepting function declarations as record declarations.
1456 // In general, we cannot solve this problem. Consider:
1457 // class A<int> B() {}
1458 // which can be a function definition or a class definition when B() is a
1459 // macro. If we find enough real-world cases where this is a problem, we
1460 // can parse for the 'template' keyword in the beginning of the statement,
1461 // and thus rule out the record production in case there is no template
1462 // (this would still leave us with an ambiguity between template function
1463 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +00001464 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1465 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1466 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001467 return;
1468 nextToken();
1469 }
1470 }
1471 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001472 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001473 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimek44135b82013-05-13 12:51:40 +00001474 addUnwrappedLine();
1475
Stephen Hines651f13c2014-04-23 16:59:28 -07001476 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekaabfb272013-10-12 22:46:56 +00001477 /*MunchSemi=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +00001478 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001479 // We fall through to parsing a structural element afterwards, so
1480 // class A {} n, m;
1481 // will end up in one unwrapped line.
Stephen Hines176edba2014-12-01 14:53:08 -08001482 // This does not apply for Java.
1483 if (Style.Language == FormatStyle::LK_Java)
1484 addUnwrappedLine();
Manuel Klimekde768542013-01-07 18:10:23 +00001485}
1486
Nico Weber1abe6ea2013-01-09 21:15:03 +00001487void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001488 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001489 do
1490 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001491 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +00001492 nextToken(); // Skip '>'.
1493}
1494
1495void UnwrappedLineParser::parseObjCUntilAtEnd() {
1496 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001497 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001498 nextToken();
1499 addUnwrappedLine();
1500 break;
1501 }
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001502 if (FormatTok->is(tok::l_brace)) {
1503 parseBlock(/*MustBeDeclaration=*/false);
1504 // In ObjC interfaces, nothing should be following the "}".
1505 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001506 } else if (FormatTok->is(tok::r_brace)) {
1507 // Ignore stray "}". parseStructuralElement doesn't consume them.
1508 nextToken();
1509 addUnwrappedLine();
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001510 } else {
1511 parseStructuralElement();
1512 }
Nico Weber1abe6ea2013-01-09 21:15:03 +00001513 } while (!eof());
1514}
1515
Nico Weber50767d82013-01-09 23:25:37 +00001516void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001517 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001518 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001519
1520 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001521 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001522 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001523 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001524 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001525 // Skip category, if present.
1526 parseParens();
1527
Manuel Klimek96e888b2013-05-28 11:55:06 +00001528 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001529 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001530
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001531 if (FormatTok->Tok.is(tok::l_brace)) {
1532 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1533 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1534 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +00001535 parseBlock(/*MustBeDeclaration=*/true);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001536 }
Nico Weber27d13672013-01-09 20:25:35 +00001537
1538 // With instance variables, this puts '}' on its own line. Without instance
1539 // variables, this ends the @interface line.
1540 addUnwrappedLine();
1541
Nico Weber1abe6ea2013-01-09 21:15:03 +00001542 parseObjCUntilAtEnd();
1543}
Nico Weber27d13672013-01-09 20:25:35 +00001544
Nico Weber1abe6ea2013-01-09 21:15:03 +00001545void UnwrappedLineParser::parseObjCProtocol() {
1546 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001547 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001548
Manuel Klimek96e888b2013-05-28 11:55:06 +00001549 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001550 parseObjCProtocolList();
1551
1552 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001553 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001554 nextToken();
1555 return addUnwrappedLine();
1556 }
1557
1558 addUnwrappedLine();
1559 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001560}
1561
Daniel Jasperd98927d2013-09-05 16:05:56 +00001562LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1563 StringRef Prefix = "") {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001564 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1565 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1566 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1567 E = Line.Tokens.end();
1568 I != E; ++I) {
Daniel Jasperac2c9742013-09-05 10:04:31 +00001569 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper567dcf92013-09-05 09:29:45 +00001570 }
1571 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1572 E = Line.Tokens.end();
1573 I != E; ++I) {
1574 const UnwrappedLineNode &Node = *I;
1575 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1576 I = Node.Children.begin(),
1577 E = Node.Children.end();
1578 I != E; ++I) {
1579 printDebugInfo(*I, "\nChild: ");
1580 }
1581 }
1582 llvm::dbgs() << "\n";
1583}
1584
Daniel Jasperbac016b2012-12-03 18:12:45 +00001585void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001586 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001587 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001588 DEBUG({
Daniel Jasper567dcf92013-09-05 09:29:45 +00001589 if (CurrentLines == &Lines)
1590 printDebugInfo(*Line);
Manuel Klimek8fa37992013-01-16 12:31:12 +00001591 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001592 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001593 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001594 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001595 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jasper516fb312013-03-01 18:11:39 +00001596 I = PreprocessorDirectives.begin(),
1597 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001598 I != E; ++I) {
1599 CurrentLines->push_back(*I);
1600 }
1601 PreprocessorDirectives.clear();
1602 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001603}
1604
Manuel Klimek96e888b2013-05-28 11:55:06 +00001605bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001606
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001607bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
1608 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1609 FormatTok.NewlinesBefore > 0;
1610}
1611
Manuel Klimek86721d22013-01-22 16:31:55 +00001612void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1613 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001614 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001615 I = CommentsBeforeNextToken.begin(),
1616 E = CommentsBeforeNextToken.end();
1617 I != E; ++I) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001618 if (isOnNewLine(**I) && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001619 addUnwrappedLine();
1620 }
1621 pushToken(*I);
1622 }
1623 if (NewlineBeforeNext && JustComments) {
1624 addUnwrappedLine();
1625 }
1626 CommentsBeforeNextToken.clear();
1627}
1628
Daniel Jasperbac016b2012-12-03 18:12:45 +00001629void UnwrappedLineParser::nextToken() {
1630 if (eof())
1631 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001632 flushComments(isOnNewLine(*FormatTok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001633 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001634 readToken();
1635}
1636
1637void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001638 bool CommentsInCurrentLine = true;
1639 do {
1640 FormatTok = Tokens->getNextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001641 assert(FormatTok);
Manuel Klimek96e888b2013-05-28 11:55:06 +00001642 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1643 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001644 // If there is an unfinished unwrapped line, we flush the preprocessor
1645 // directives only after that unwrapped line was finished later.
Daniel Jasperf9955d32013-03-20 12:37:50 +00001646 bool SwitchToPreprocessorLines =
1647 !Line->Tokens.empty() && CurrentLines == &Lines;
Manuel Klimek86721d22013-01-22 16:31:55 +00001648 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001649 // Comments stored before the preprocessor directive need to be output
1650 // before the preprocessor directive, at the same level as the
1651 // preprocessor directive, as we consider them to apply to the directive.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001652 flushComments(isOnNewLine(*FormatTok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001653 parsePPDirective();
1654 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001655 while (FormatTok->Type == TT_ConflictStart ||
1656 FormatTok->Type == TT_ConflictEnd ||
1657 FormatTok->Type == TT_ConflictAlternative) {
1658 if (FormatTok->Type == TT_ConflictStart) {
1659 conditionalCompilationStart(/*Unreachable=*/false);
1660 } else if (FormatTok->Type == TT_ConflictAlternative) {
1661 conditionalCompilationAlternative();
1662 } else if (FormatTok->Type == TT_ConflictEnd) {
1663 conditionalCompilationEnd();
1664 }
1665 FormatTok = Tokens->getNextToken();
1666 FormatTok->MustBreakBefore = true;
1667 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001668
1669 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1670 !Line->InPPDirective) {
1671 continue;
1672 }
1673
Manuel Klimek96e888b2013-05-28 11:55:06 +00001674 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001675 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001676 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001677 CommentsInCurrentLine = false;
1678 }
1679 if (CommentsInCurrentLine) {
1680 pushToken(FormatTok);
1681 } else {
1682 CommentsBeforeNextToken.push_back(FormatTok);
1683 }
1684 } while (!eof());
1685}
1686
Manuel Klimek96e888b2013-05-28 11:55:06 +00001687void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001688 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001689 if (MustBreakBeforeNextToken) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001690 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001691 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001692 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001693}
1694
Daniel Jaspercd162382013-01-07 13:26:07 +00001695} // end namespace format
1696} // end namespace clang