blob: 7bab55ce42328f6b447b7d2314f3c8332687eb8c [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,
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700265 /*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:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700377 case tok::kw___try:
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +0000378 if (!LBraceStack.empty())
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000379 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000380 break;
381 default:
382 break;
383 }
384 Tok = NextTok;
Manuel Klimek31e44f72013-09-04 08:20:47 +0000385 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimek80829bd2013-05-23 09:41:43 +0000386 // Assume other blocks for all unclosed opening braces.
387 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000388 if (LBraceStack[i]->BlockKind == BK_Unknown)
389 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimek80829bd2013-05-23 09:41:43 +0000390 }
Manuel Klimek31e44f72013-09-04 08:20:47 +0000391
Manuel Klimek80829bd2013-05-23 09:41:43 +0000392 FormatTok = Tokens->setPosition(StoredPosition);
393}
394
Manuel Klimekaabfb272013-10-12 22:46:56 +0000395void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
396 bool MunchSemi) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000397 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jaspere865cc52013-07-25 11:31:57 +0000398 unsigned InitialLevel = Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000399 nextToken();
400
Manuel Klimek2f1ac412013-01-21 16:42:44 +0000401 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000402
Manuel Klimek70b03f42013-01-23 09:32:48 +0000403 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
404 MustBeDeclaration);
Daniel Jaspereff18b92013-07-31 23:16:02 +0000405 if (AddLevel)
406 ++Line->Level;
Nico Weber27268772013-06-26 00:30:14 +0000407 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko15757312012-12-06 18:03:27 +0000408
Manuel Klimek96e888b2013-05-28 11:55:06 +0000409 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jaspere865cc52013-07-25 11:31:57 +0000410 Line->Level = InitialLevel;
Manuel Klimek67d080d2013-04-12 14:13:36 +0000411 StructuralError = true;
412 return;
Manuel Klimek86721d22013-01-22 16:31:55 +0000413 }
Alexander Kornienko393b0082012-12-04 15:40:36 +0000414
Daniel Jasperf9955d32013-03-20 12:37:50 +0000415 nextToken(); // Munch the closing brace.
Manuel Klimekaabfb272013-10-12 22:46:56 +0000416 if (MunchSemi && FormatTok->Tok.is(tok::semi))
417 nextToken();
Daniel Jaspere865cc52013-07-25 11:31:57 +0000418 Line->Level = InitialLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000419}
420
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700421static bool IsGoogScope(const UnwrappedLine &Line) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700422 // FIXME: Closure-library specific stuff should not be hard-coded but be
423 // configurable.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700424 if (Line.Tokens.size() < 4)
425 return false;
426 auto I = Line.Tokens.begin();
427 if (I->Tok->TokenText != "goog")
428 return false;
429 ++I;
430 if (I->Tok->isNot(tok::period))
431 return false;
432 ++I;
433 if (I->Tok->TokenText != "scope")
434 return false;
435 ++I;
436 return I->Tok->is(tok::l_paren);
437}
438
Stephen Hines176edba2014-12-01 14:53:08 -0800439static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
440 const FormatToken &InitialToken) {
441 switch (Style.BreakBeforeBraces) {
442 case FormatStyle::BS_Linux:
443 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
444 case FormatStyle::BS_Allman:
445 case FormatStyle::BS_GNU:
446 return true;
447 default:
448 return false;
449 }
450}
451
Manuel Klimek753a5112013-09-04 13:25:30 +0000452void UnwrappedLineParser::parseChildBlock() {
453 FormatTok->BlockKind = BK_Block;
454 nextToken();
455 {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700456 bool GoogScope =
457 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
Manuel Klimek753a5112013-09-04 13:25:30 +0000458 ScopedLineState LineState(*this);
459 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
460 /*MustBeDeclaration=*/false);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700461 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek753a5112013-09-04 13:25:30 +0000462 parseLevel(/*HasOpeningBrace=*/true);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700463 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek753a5112013-09-04 13:25:30 +0000464 }
465 nextToken();
466}
467
Daniel Jasperbac016b2012-12-03 18:12:45 +0000468void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000469 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek67d080d2013-04-12 14:13:36 +0000470 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka080a182013-01-02 16:30:12 +0000471 nextToken();
472
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700473 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimekbd04f2a2013-01-31 15:58:48 +0000474 parsePPUnknown();
Manuel Klimeka080a182013-01-02 16:30:12 +0000475 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000476 }
Manuel Klimeka080a182013-01-02 16:30:12 +0000477
Manuel Klimek96e888b2013-05-28 11:55:06 +0000478 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000479 case tok::pp_define:
480 parsePPDefine();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000481 return;
482 case tok::pp_if:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000483 parsePPIf(/*IfDef=*/false);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000484 break;
485 case tok::pp_ifdef:
486 case tok::pp_ifndef:
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000487 parsePPIf(/*IfDef=*/true);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000488 break;
489 case tok::pp_else:
490 parsePPElse();
491 break;
492 case tok::pp_elif:
493 parsePPElIf();
494 break;
495 case tok::pp_endif:
496 parsePPEndIf();
Manuel Klimekd4397b92013-01-04 23:34:14 +0000497 break;
498 default:
499 parsePPUnknown();
500 break;
501 }
502}
503
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700504void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
505 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000506 PPStack.push_back(PP_Unreachable);
507 else
508 PPStack.push_back(PP_Conditional);
509}
510
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700511void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000512 ++PPBranchLevel;
513 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
514 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
515 PPLevelBranchIndex.push_back(0);
516 PPLevelBranchCount.push_back(0);
517 }
518 PPChainBranchIndex.push(0);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700519 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
520 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000521}
522
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700523void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000524 if (!PPStack.empty())
525 PPStack.pop_back();
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000526 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
527 if (!PPChainBranchIndex.empty())
528 ++PPChainBranchIndex.top();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700529 conditionalCompilationCondition(
530 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
531 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000532}
533
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700534void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000535 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
536 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
537 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000538 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
539 }
540 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700541 // Guard against #endif's without #if.
542 if (PPBranchLevel > 0)
543 --PPBranchLevel;
Manuel Klimekae76f7f2013-10-11 21:25:45 +0000544 if (!PPChainBranchIndex.empty())
545 PPChainBranchIndex.pop();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000546 if (!PPStack.empty())
547 PPStack.pop_back();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700548}
549
550void UnwrappedLineParser::parsePPIf(bool IfDef) {
551 nextToken();
552 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700553 FormatTok->Tok.getLiteralData() != nullptr &&
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700554 StringRef(FormatTok->Tok.getLiteralData(),
555 FormatTok->Tok.getLength()) == "0") ||
556 FormatTok->Tok.is(tok::kw_false);
557 conditionalCompilationStart(!IfDef && IsLiteralFalse);
558 parsePPUnknown();
559}
560
561void UnwrappedLineParser::parsePPElse() {
562 conditionalCompilationAlternative();
563 parsePPUnknown();
564}
565
566void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
567
568void UnwrappedLineParser::parsePPEndIf() {
569 conditionalCompilationEnd();
Alexander Kornienko6fb46b02013-05-24 18:24:24 +0000570 parsePPUnknown();
571}
572
Manuel Klimekd4397b92013-01-04 23:34:14 +0000573void UnwrappedLineParser::parsePPDefine() {
574 nextToken();
575
Manuel Klimek96e888b2013-05-28 11:55:06 +0000576 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000577 parsePPUnknown();
578 return;
579 }
580 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000581 if (FormatTok->Tok.getKind() == tok::l_paren &&
582 FormatTok->WhitespaceRange.getBegin() ==
583 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimekd4397b92013-01-04 23:34:14 +0000584 parseParens();
585 }
586 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +0000587 Line->Level = 1;
Manuel Klimekc3d0c822013-01-07 09:34:28 +0000588
589 // Errors during a preprocessor directive can only affect the layout of the
590 // preprocessor directive, and thus we ignore them. An alternative approach
591 // would be to use the same approach we use on the file level (no
592 // re-indentation if there was a structural error) within the macro
593 // definition.
Manuel Klimekd4397b92013-01-04 23:34:14 +0000594 parseFile();
595}
596
597void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka080a182013-01-02 16:30:12 +0000598 do {
Manuel Klimeka080a182013-01-02 16:30:12 +0000599 nextToken();
600 } while (!eof());
601 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000602}
603
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000604// Here we blacklist certain tokens that are not usually the first token in an
605// unwrapped line. This is used in attempt to distinguish macro calls without
606// trailing semicolons from other constructs split to several lines.
607bool tokenCanStartNewLine(clang::Token Tok) {
608 // Semicolon can be a null-statement, l_square can be a start of a macro or
609 // a C++11 attribute, but this doesn't seem to be common.
610 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
611 Tok.isNot(tok::l_square) &&
612 // Tokens that can only be used as binary operators and a part of
613 // overloaded operator names.
614 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
615 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
616 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
617 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
618 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
619 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
620 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
621 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
622 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
623 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
624 Tok.isNot(tok::lesslessequal) &&
625 // Colon is used in labels, base class lists, initializer lists,
626 // range-based for loops, ternary operator, but should never be the
627 // first token in an unwrapped line.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700628 Tok.isNot(tok::colon) &&
629 // 'noexcept' is a trailing annotation.
630 Tok.isNot(tok::kw_noexcept);
Alexander Kornienko99b0e142013-04-09 16:15:19 +0000631}
632
Manuel Klimekf0ab0a32013-01-07 14:56:16 +0000633void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000634 assert(!FormatTok->Tok.is(tok::l_brace));
635 switch (FormatTok->Tok.getKind()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000636 case tok::at:
637 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000638 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000639 parseBracedList();
640 break;
641 }
Manuel Klimek96e888b2013-05-28 11:55:06 +0000642 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber6092d4e2013-01-07 19:05:19 +0000643 case tok::objc_public:
644 case tok::objc_protected:
645 case tok::objc_package:
646 case tok::objc_private:
647 return parseAccessSpecifier();
Nico Weber27d13672013-01-09 20:25:35 +0000648 case tok::objc_interface:
Nico Weber50767d82013-01-09 23:25:37 +0000649 case tok::objc_implementation:
650 return parseObjCInterfaceOrImplementation();
Nico Weber1abe6ea2013-01-09 21:15:03 +0000651 case tok::objc_protocol:
652 return parseObjCProtocol();
Nico Weber049c4472013-01-09 21:42:32 +0000653 case tok::objc_end:
654 return; // Handled by the caller.
Nico Weberb530fa32013-01-10 00:25:19 +0000655 case tok::objc_optional:
656 case tok::objc_required:
657 nextToken();
658 addUnwrappedLine();
659 return;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700660 case tok::objc_try:
661 // This branch isn't strictly necessary (the kw_try case below would
662 // do this too after the tok::at is parsed above). But be explicit.
663 parseTryCatch();
664 return;
Nico Weber6092d4e2013-01-07 19:05:19 +0000665 default:
666 break;
667 }
668 break;
Stephen Hines176edba2014-12-01 14:53:08 -0800669 case tok::kw_asm:
Stephen Hines176edba2014-12-01 14:53:08 -0800670 nextToken();
671 if (FormatTok->is(tok::l_brace)) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700672 nextToken();
Stephen Hines176edba2014-12-01 14:53:08 -0800673 while (FormatTok && FormatTok->isNot(tok::eof)) {
Stephen Hines176edba2014-12-01 14:53:08 -0800674 if (FormatTok->is(tok::r_brace)) {
675 nextToken();
676 break;
677 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700678 FormatTok->Finalized = true;
Stephen Hines176edba2014-12-01 14:53:08 -0800679 nextToken();
680 }
681 }
682 break;
Alexander Kornienko15757312012-12-06 18:03:27 +0000683 case tok::kw_namespace:
684 parseNamespace();
685 return;
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000686 case tok::kw_inline:
687 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000688 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko1f94f2b2012-12-30 21:27:25 +0000689 parseNamespace();
690 return;
691 }
692 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000693 case tok::kw_public:
694 case tok::kw_protected:
695 case tok::kw_private:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700696 if (Style.Language == FormatStyle::LK_Java ||
697 Style.Language == FormatStyle::LK_JavaScript)
Stephen Hines176edba2014-12-01 14:53:08 -0800698 nextToken();
699 else
700 parseAccessSpecifier();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000701 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000702 case tok::kw_if:
703 parseIfThenElse();
Daniel Jasperbac016b2012-12-03 18:12:45 +0000704 return;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +0000705 case tok::kw_for:
706 case tok::kw_while:
707 parseForOrWhileLoop();
708 return;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000709 case tok::kw_do:
710 parseDoWhile();
711 return;
712 case tok::kw_switch:
713 parseSwitch();
714 return;
715 case tok::kw_default:
716 nextToken();
717 parseLabel();
718 return;
719 case tok::kw_case:
720 parseCaseLabel();
721 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700722 case tok::kw_try:
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700723 case tok::kw___try:
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700724 parseTryCatch();
725 return;
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000726 case tok::kw_extern:
727 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000728 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000729 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000730 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jaspereff18b92013-07-31 23:16:02 +0000731 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000732 addUnwrappedLine();
733 return;
734 }
735 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700736 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700737 case tok::kw_export:
738 if (Style.Language == FormatStyle::LK_JavaScript) {
739 parseJavaScriptEs6ImportExport();
740 return;
741 }
742 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700743 case tok::identifier:
744 if (FormatTok->IsForEachMacro) {
745 parseForOrWhileLoop();
746 return;
747 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700748 if (Style.Language == FormatStyle::LK_JavaScript &&
749 FormatTok->is(Keywords.kw_import)) {
750 parseJavaScriptEs6ImportExport();
751 return;
752 }
Manuel Klimekd19dc2d2013-01-21 14:32:05 +0000753 // In all other cases, parse the declaration.
754 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000755 default:
756 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000757 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000758 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000759 switch (FormatTok->Tok.getKind()) {
Nico Weberd74fcdb2013-02-10 20:35:35 +0000760 case tok::at:
761 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000762 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +0000763 parseBracedList();
764 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000765 case tok::kw_enum:
766 parseEnum();
Manuel Klimek308232c2013-01-21 19:17:52 +0000767 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700768 case tok::kw_typedef:
769 nextToken();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700770 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
771 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Stephen Hines651f13c2014-04-23 16:59:28 -0700772 parseEnum();
773 break;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000774 case tok::kw_struct:
775 case tok::kw_union:
Manuel Klimekde768542013-01-07 18:10:23 +0000776 case tok::kw_class:
Manuel Klimek47ea7f62013-01-15 13:38:33 +0000777 parseRecord();
778 // A record declaration or definition is always the start of a structural
779 // element.
780 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700781 case tok::period:
782 nextToken();
783 // In Java, classes have an implicit static member "class".
784 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
785 FormatTok->is(tok::kw_class))
786 nextToken();
787 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000788 case tok::semi:
789 nextToken();
790 addUnwrappedLine();
791 return;
Alexander Kornienkod8818752013-01-16 11:43:46 +0000792 case tok::r_brace:
793 addUnwrappedLine();
794 return;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000795 case tok::l_paren:
796 parseParens();
797 break;
Manuel Klimek753a5112013-09-04 13:25:30 +0000798 case tok::caret:
799 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700800 if (FormatTok->Tok.isAnyIdentifier() ||
801 FormatTok->isSimpleTypeSpecifier())
802 nextToken();
803 if (FormatTok->is(tok::l_paren))
804 parseParens();
805 if (FormatTok->is(tok::l_brace))
Manuel Klimek753a5112013-09-04 13:25:30 +0000806 parseChildBlock();
Manuel Klimek753a5112013-09-04 13:25:30 +0000807 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000808 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +0000809 if (!tryToParseBracedList()) {
810 // A block outside of parentheses must be the last part of a
811 // structural element.
812 // FIXME: Figure out cases where this is not true, and add projections
813 // for them (the one we know is missing are lambdas).
Stephen Hines651f13c2014-04-23 16:59:28 -0700814 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000815 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -0700816 FormatTok->Type = TT_FunctionLBrace;
Nico Weber27268772013-06-26 00:30:14 +0000817 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +0000818 addUnwrappedLine();
Manuel Klimek80829bd2013-05-23 09:41:43 +0000819 return;
820 }
821 // Otherwise this was a braced init list, and the structural
822 // element continues.
823 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700824 case tok::kw_try:
825 // We arrive here when parsing function-try blocks.
826 parseTryCatch();
827 return;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000828 case tok::identifier: {
829 StringRef Text = FormatTok->TokenText;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700830 // Parse function literal unless 'function' is the first token in a line
831 // in which case this should be treated as a free-standing function.
832 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
833 Line->Tokens.size() > 0) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700834 tryToParseJSFunction();
835 break;
836 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000837 nextToken();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700838 if (Line->Tokens.size() == 1 &&
839 // JS doesn't have macros, and within classes colons indicate fields,
840 // not labels.
841 (Style.Language != FormatStyle::LK_JavaScript ||
842 !Line->MustBeDeclaration)) {
Manuel Klimek96e888b2013-05-28 11:55:06 +0000843 if (FormatTok->Tok.is(tok::colon)) {
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000844 parseLabel();
845 return;
846 }
Stephen Hines176edba2014-12-01 14:53:08 -0800847 // Recognize function-like macro usages without trailing semicolon as
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700848 // well as free-standing macros like Q_OBJECT.
Stephen Hines176edba2014-12-01 14:53:08 -0800849 bool FunctionLike = FormatTok->is(tok::l_paren);
850 if (FunctionLike)
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000851 parseParens();
Stephen Hines176edba2014-12-01 14:53:08 -0800852 if (FormatTok->NewlinesBefore > 0 &&
853 (Text.size() >= 5 || FunctionLike) &&
854 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000855 addUnwrappedLine();
Daniel Jasperc76d59d2013-05-29 14:09:17 +0000856 return;
Alexander Kornienko3d713a72013-04-08 22:16:06 +0000857 }
Daniel Jasperbac016b2012-12-03 18:12:45 +0000858 }
859 break;
Daniel Jasper7e70f4c2013-05-29 13:16:10 +0000860 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000861 case tok::equal:
862 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +0000863 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimekbb42bf12013-01-10 11:52:21 +0000864 parseBracedList();
865 }
Daniel Jasper05b1ac82012-12-17 11:29:41 +0000866 break;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000867 case tok::l_square:
Stephen Hines651f13c2014-04-23 16:59:28 -0700868 parseSquare();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000869 break;
Alexander Kornienkoa166e732012-12-04 14:46:19 +0000870 default:
871 nextToken();
872 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +0000873 }
874 } while (!eof());
875}
876
Stephen Hines651f13c2014-04-23 16:59:28 -0700877bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper2d657052013-09-05 11:49:39 +0000878 // FIXME: This is a dirty way to access the previous token. Find a better
879 // solution.
Daniel Jasper520cca82013-09-06 21:25:51 +0000880 if (!Line->Tokens.empty() &&
Stephen Hines176edba2014-12-01 14:53:08 -0800881 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
882 tok::kw_new, tok::kw_delete) ||
Stephen Hines651f13c2014-04-23 16:59:28 -0700883 Line->Tokens.back().Tok->closesScope() ||
884 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasper2d657052013-09-05 11:49:39 +0000885 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -0700886 return false;
Daniel Jasper2d657052013-09-05 11:49:39 +0000887 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000888 assert(FormatTok->is(tok::l_square));
889 FormatToken &LSquare = *FormatTok;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000890 if (!tryToParseLambdaIntroducer())
Stephen Hines651f13c2014-04-23 16:59:28 -0700891 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000892
893 while (FormatTok->isNot(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700894 if (FormatTok->isSimpleTypeSpecifier()) {
895 nextToken();
896 continue;
897 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000898 switch (FormatTok->Tok.getKind()) {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000899 case tok::l_brace:
900 break;
901 case tok::l_paren:
902 parseParens();
903 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700904 case tok::amp:
905 case tok::star:
906 case tok::kw_const:
907 case tok::comma:
Stephen Hines651f13c2014-04-23 16:59:28 -0700908 case tok::less:
909 case tok::greater:
Daniel Jasperac2c9742013-09-05 10:04:31 +0000910 case tok::identifier:
Stephen Hines651f13c2014-04-23 16:59:28 -0700911 case tok::coloncolon:
Daniel Jasperac2c9742013-09-05 10:04:31 +0000912 case tok::kw_mutable:
913 nextToken();
914 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700915 case tok::arrow:
916 FormatTok->Type = TT_TrailingReturnArrow;
917 nextToken();
918 break;
Daniel Jasperac2c9742013-09-05 10:04:31 +0000919 default:
Stephen Hines651f13c2014-04-23 16:59:28 -0700920 return true;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000921 }
922 }
Daniel Jasper567dcf92013-09-05 09:29:45 +0000923 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek753a5112013-09-04 13:25:30 +0000924 parseChildBlock();
Stephen Hines651f13c2014-04-23 16:59:28 -0700925 return true;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000926}
927
928bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
929 nextToken();
930 if (FormatTok->is(tok::equal)) {
931 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000932 if (FormatTok->is(tok::r_square)) {
933 nextToken();
934 return true;
935 }
936 if (FormatTok->isNot(tok::comma))
937 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000938 nextToken();
939 } else if (FormatTok->is(tok::amp)) {
940 nextToken();
Daniel Jasperac2c9742013-09-05 10:04:31 +0000941 if (FormatTok->is(tok::r_square)) {
942 nextToken();
943 return true;
944 }
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000945 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
946 return false;
947 }
Daniel Jasperac2c9742013-09-05 10:04:31 +0000948 if (FormatTok->is(tok::comma))
949 nextToken();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000950 } else if (FormatTok->is(tok::r_square)) {
951 nextToken();
952 return true;
953 }
954 do {
Daniel Jasperac2c9742013-09-05 10:04:31 +0000955 if (FormatTok->is(tok::amp))
956 nextToken();
957 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
958 return false;
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000959 nextToken();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700960 if (FormatTok->is(tok::ellipsis))
961 nextToken();
Manuel Klimekb61a8af2013-09-03 15:10:01 +0000962 if (FormatTok->is(tok::comma)) {
963 nextToken();
964 } else if (FormatTok->is(tok::r_square)) {
965 nextToken();
966 return true;
967 } else {
968 return false;
969 }
970 } while (!eof());
971 return false;
972}
973
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700974void UnwrappedLineParser::tryToParseJSFunction() {
975 nextToken();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700976
977 // Consume function name.
978 if (FormatTok->is(tok::identifier))
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700979 nextToken();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700980
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700981 if (FormatTok->isNot(tok::l_paren))
982 return;
983 nextToken();
984 while (FormatTok->isNot(tok::l_brace)) {
985 // Err on the side of caution in order to avoid consuming the full file in
986 // case of incomplete code.
987 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
988 tok::comment))
989 return;
990 nextToken();
991 }
992 parseChildBlock();
993}
994
Manuel Klimek80829bd2013-05-23 09:41:43 +0000995bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000996 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimek80829bd2013-05-23 09:41:43 +0000997 calculateBraceTypes();
Daniel Jasper0de1c4d2013-07-09 09:06:29 +0000998 assert(FormatTok->BlockKind != BK_Unknown);
999 if (FormatTok->BlockKind == BK_Block)
Manuel Klimek80829bd2013-05-23 09:41:43 +00001000 return false;
1001 parseBracedList();
1002 return true;
1003}
1004
Daniel Jasper57981202013-09-13 09:20:45 +00001005bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1006 bool HasError = false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001007 nextToken();
1008
Manuel Klimek423dd932013-04-10 09:52:05 +00001009 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1010 // replace this by using parseAssigmentExpression() inside.
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001011 do {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001012 if (Style.Language == FormatStyle::LK_JavaScript &&
Stephen Hines176edba2014-12-01 14:53:08 -08001013 FormatTok->is(Keywords.kw_function)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001014 tryToParseJSFunction();
1015 continue;
1016 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001017 switch (FormatTok->Tok.getKind()) {
Manuel Klimek753a5112013-09-04 13:25:30 +00001018 case tok::caret:
1019 nextToken();
1020 if (FormatTok->is(tok::l_brace)) {
1021 parseChildBlock();
1022 }
1023 break;
1024 case tok::l_square:
1025 tryToParseLambda();
1026 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001027 case tok::l_brace:
Manuel Klimek31e44f72013-09-04 08:20:47 +00001028 // Assume there are no blocks inside a braced init list apart
1029 // from the ones we explicitly parse out (like lambdas).
1030 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001031 parseBracedList();
1032 break;
1033 case tok::r_brace:
1034 nextToken();
Daniel Jasper57981202013-09-13 09:20:45 +00001035 return !HasError;
Manuel Klimek423dd932013-04-10 09:52:05 +00001036 case tok::semi:
Daniel Jasper57981202013-09-13 09:20:45 +00001037 HasError = true;
1038 if (!ContinueOnSemicolons)
1039 return !HasError;
1040 nextToken();
1041 break;
Manuel Klimek423dd932013-04-10 09:52:05 +00001042 case tok::comma:
1043 nextToken();
Manuel Klimek423dd932013-04-10 09:52:05 +00001044 break;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001045 default:
1046 nextToken();
1047 break;
1048 }
1049 } while (!eof());
Daniel Jasper57981202013-09-13 09:20:45 +00001050 return false;
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001051}
1052
Daniel Jasperbac016b2012-12-03 18:12:45 +00001053void UnwrappedLineParser::parseParens() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001054 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001055 nextToken();
1056 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001057 switch (FormatTok->Tok.getKind()) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001058 case tok::l_paren:
1059 parseParens();
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001060 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1061 parseChildBlock();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001062 break;
1063 case tok::r_paren:
1064 nextToken();
1065 return;
Daniel Jasperf7ec1cc2013-05-31 14:56:29 +00001066 case tok::r_brace:
1067 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1068 return;
Daniel Jasperac2c9742013-09-05 10:04:31 +00001069 case tok::l_square:
1070 tryToParseLambda();
1071 break;
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001072 case tok::l_brace:
Manuel Klimek80829bd2013-05-23 09:41:43 +00001073 if (!tryToParseBracedList()) {
Manuel Klimekb7d98a12013-09-04 13:34:14 +00001074 parseChildBlock();
Manuel Klimek80829bd2013-05-23 09:41:43 +00001075 }
Manuel Klimekbb42bf12013-01-10 11:52:21 +00001076 break;
Nico Weberd74fcdb2013-02-10 20:35:35 +00001077 case tok::at:
1078 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001079 if (FormatTok->Tok.is(tok::l_brace))
Nico Weberd74fcdb2013-02-10 20:35:35 +00001080 parseBracedList();
1081 break;
Stephen Hines176edba2014-12-01 14:53:08 -08001082 case tok::identifier:
1083 if (Style.Language == FormatStyle::LK_JavaScript &&
1084 FormatTok->is(Keywords.kw_function))
1085 tryToParseJSFunction();
1086 else
1087 nextToken();
1088 break;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001089 default:
1090 nextToken();
1091 break;
1092 }
1093 } while (!eof());
1094}
1095
Stephen Hines651f13c2014-04-23 16:59:28 -07001096void UnwrappedLineParser::parseSquare() {
1097 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1098 if (tryToParseLambda())
1099 return;
1100 do {
1101 switch (FormatTok->Tok.getKind()) {
1102 case tok::l_paren:
1103 parseParens();
1104 break;
1105 case tok::r_square:
1106 nextToken();
1107 return;
1108 case tok::r_brace:
1109 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1110 return;
1111 case tok::l_square:
1112 parseSquare();
1113 break;
1114 case tok::l_brace: {
1115 if (!tryToParseBracedList()) {
1116 parseChildBlock();
1117 }
1118 break;
1119 }
1120 case tok::at:
1121 nextToken();
1122 if (FormatTok->Tok.is(tok::l_brace))
1123 parseBracedList();
1124 break;
1125 default:
1126 nextToken();
1127 break;
1128 }
1129 } while (!eof());
1130}
1131
Daniel Jasperbac016b2012-12-03 18:12:45 +00001132void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001133 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001134 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001135 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekd4658432013-01-11 18:28:36 +00001136 parseParens();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001137 bool NeedsUnwrappedLine = false;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001138 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001139 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001140 parseBlock(/*MustBeDeclaration=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07001141 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1142 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001143 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001144 } else {
Manuel Klimeke4907052013-08-02 21:31:59 +00001145 NeedsUnwrappedLine = true;
Stephen Hines651f13c2014-04-23 16:59:28 -07001146 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001147 } else {
1148 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001149 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001150 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001151 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001152 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001153 if (FormatTok->Tok.is(tok::kw_else)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001154 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1155 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001156 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001157 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001158 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001159 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001160 addUnwrappedLine();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001161 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001162 parseIfThenElse();
1163 } else {
1164 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001165 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001166 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001167 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001168 }
1169 } else if (NeedsUnwrappedLine) {
1170 addUnwrappedLine();
1171 }
1172}
1173
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001174void UnwrappedLineParser::parseTryCatch() {
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001175 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001176 nextToken();
1177 bool NeedsUnwrappedLine = false;
1178 if (FormatTok->is(tok::colon)) {
1179 // We are in a function try block, what comes is an initializer list.
1180 nextToken();
1181 while (FormatTok->is(tok::identifier)) {
1182 nextToken();
1183 if (FormatTok->is(tok::l_paren))
1184 parseParens();
1185 else
1186 StructuralError = true;
1187 if (FormatTok->is(tok::comma))
1188 nextToken();
1189 }
1190 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001191 // Parse try with resource.
1192 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1193 parseParens();
1194 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001195 if (FormatTok->is(tok::l_brace)) {
1196 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1197 parseBlock(/*MustBeDeclaration=*/false);
1198 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1199 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1200 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1201 addUnwrappedLine();
1202 } else {
1203 NeedsUnwrappedLine = true;
1204 }
1205 } else if (!FormatTok->is(tok::kw_catch)) {
1206 // The C++ standard requires a compound-statement after a try.
1207 // If there's none, we try to assume there's a structuralElement
1208 // and try to continue.
1209 StructuralError = true;
1210 addUnwrappedLine();
1211 ++Line->Level;
1212 parseStructuralElement();
1213 --Line->Level;
1214 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001215 while (1) {
1216 if (FormatTok->is(tok::at))
1217 nextToken();
1218 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1219 tok::kw___finally) ||
1220 ((Style.Language == FormatStyle::LK_Java ||
1221 Style.Language == FormatStyle::LK_JavaScript) &&
1222 FormatTok->is(Keywords.kw_finally)) ||
1223 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1224 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1225 break;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001226 nextToken();
1227 while (FormatTok->isNot(tok::l_brace)) {
1228 if (FormatTok->is(tok::l_paren)) {
1229 parseParens();
1230 continue;
1231 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001232 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001233 return;
1234 nextToken();
1235 }
1236 NeedsUnwrappedLine = false;
1237 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1238 parseBlock(/*MustBeDeclaration=*/false);
1239 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1240 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1241 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1242 addUnwrappedLine();
1243 } else {
1244 NeedsUnwrappedLine = true;
1245 }
1246 }
1247 if (NeedsUnwrappedLine) {
1248 addUnwrappedLine();
1249 }
1250}
1251
Alexander Kornienko15757312012-12-06 18:03:27 +00001252void UnwrappedLineParser::parseNamespace() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001253 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Stephen Hines176edba2014-12-01 14:53:08 -08001254
1255 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko15757312012-12-06 18:03:27 +00001256 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001257 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko15757312012-12-06 18:03:27 +00001258 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001259 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001260 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimek44135b82013-05-13 12:51:40 +00001261 addUnwrappedLine();
1262
Daniel Jaspereff18b92013-07-31 23:16:02 +00001263 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1264 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1265 DeclarationScopeStack.size() > 1);
1266 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek7fc2db02013-02-06 16:08:09 +00001267 // Munch the semicolon after a namespace. This is more common than one would
1268 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001269 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek7fc2db02013-02-06 16:08:09 +00001270 nextToken();
Alexander Kornienko15757312012-12-06 18:03:27 +00001271 addUnwrappedLine();
1272 }
1273 // FIXME: Add error handling.
1274}
1275
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001276void UnwrappedLineParser::parseForOrWhileLoop() {
Stephen Hines651f13c2014-04-23 16:59:28 -07001277 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1278 FormatTok->IsForEachMacro) &&
1279 "'for', 'while' or foreach macro expected");
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001280 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001281 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001282 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001283 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001284 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001285 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001286 addUnwrappedLine();
1287 } else {
1288 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001289 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001290 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001291 --Line->Level;
Alexander Kornienko2e97cfc2012-12-05 15:06:06 +00001292 }
1293}
1294
Daniel Jasperbac016b2012-12-03 18:12:45 +00001295void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001296 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001297 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001298 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001299 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001300 parseBlock(/*MustBeDeclaration=*/false);
Stephen Hines651f13c2014-04-23 16:59:28 -07001301 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1302 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001303 } else {
1304 addUnwrappedLine();
Manuel Klimek526ed112013-01-09 15:25:02 +00001305 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001306 parseStructuralElement();
Manuel Klimek526ed112013-01-09 15:25:02 +00001307 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001308 }
1309
Alexander Kornienko393b0082012-12-04 15:40:36 +00001310 // FIXME: Add error handling.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001311 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko393b0082012-12-04 15:40:36 +00001312 addUnwrappedLine();
1313 return;
1314 }
1315
Daniel Jasperbac016b2012-12-03 18:12:45 +00001316 nextToken();
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001317 parseStructuralElement();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001318}
1319
1320void UnwrappedLineParser::parseLabel() {
Daniel Jasperbac016b2012-12-03 18:12:45 +00001321 nextToken();
Manuel Klimek526ed112013-01-09 15:25:02 +00001322 unsigned OldLineLevel = Line->Level;
Daniel Jasperbcca7e42013-03-20 10:23:53 +00001323 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek526ed112013-01-09 15:25:02 +00001324 --Line->Level;
Manuel Klimek96e888b2013-05-28 11:55:06 +00001325 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001326 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber27268772013-06-26 00:30:14 +00001327 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeke4907052013-08-02 21:31:59 +00001328 if (FormatTok->Tok.is(tok::kw_break)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001329 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1330 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1331 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimeke4907052013-08-02 21:31:59 +00001332 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001333 }
Manuel Klimeke4907052013-08-02 21:31:59 +00001334 parseStructuralElement();
1335 }
Stephen Hines651f13c2014-04-23 16:59:28 -07001336 addUnwrappedLine();
1337 } else {
1338 addUnwrappedLine();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001339 }
Manuel Klimek526ed112013-01-09 15:25:02 +00001340 Line->Level = OldLineLevel;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001341}
1342
1343void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001344 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001345 // FIXME: fix handling of complex expressions here.
1346 do {
1347 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001348 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperbac016b2012-12-03 18:12:45 +00001349 parseLabel();
1350}
1351
1352void UnwrappedLineParser::parseSwitch() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001353 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperbac016b2012-12-03 18:12:45 +00001354 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001355 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek6eca03f2013-01-11 19:23:05 +00001356 parseParens();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001357 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001358 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jaspereff18b92013-07-31 23:16:02 +00001359 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperbac016b2012-12-03 18:12:45 +00001360 addUnwrappedLine();
1361 } else {
1362 addUnwrappedLine();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001363 ++Line->Level;
Manuel Klimekf0ab0a32013-01-07 14:56:16 +00001364 parseStructuralElement();
Daniel Jaspere865cc52013-07-25 11:31:57 +00001365 --Line->Level;
Daniel Jasperbac016b2012-12-03 18:12:45 +00001366 }
1367}
1368
1369void UnwrappedLineParser::parseAccessSpecifier() {
1370 nextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001371 // Understand Qt's slots.
1372 if (FormatTok->is(tok::identifier) &&
1373 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
1374 nextToken();
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001375 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001376 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko56e49c52012-12-10 16:34:48 +00001377 nextToken();
Daniel Jasperbac016b2012-12-03 18:12:45 +00001378 addUnwrappedLine();
1379}
1380
1381void UnwrappedLineParser::parseEnum() {
Stephen Hines176edba2014-12-01 14:53:08 -08001382 // Won't be 'enum' for NS_ENUMs.
1383 if (FormatTok->Tok.is(tok::kw_enum))
Stephen Hines651f13c2014-04-23 16:59:28 -07001384 nextToken();
Stephen Hines176edba2014-12-01 14:53:08 -08001385
Daniel Jaspercbeb1c62013-08-20 12:42:50 +00001386 // Eat up enum class ...
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001387 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1388 nextToken();
Daniel Jaspere27dc5d2013-09-06 21:32:35 +00001389 while (FormatTok->Tok.getIdentifierInfo() ||
Stephen Hines176edba2014-12-01 14:53:08 -08001390 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1391 tok::greater, tok::comma, tok::question)) {
Manuel Klimek308232c2013-01-21 19:17:52 +00001392 nextToken();
1393 // We can have macros or attributes in between 'enum' and the enum name.
Stephen Hines176edba2014-12-01 14:53:08 -08001394 if (FormatTok->is(tok::l_paren))
Alexander Kornienkoa166e732012-12-04 14:46:19 +00001395 parseParens();
Stephen Hines176edba2014-12-01 14:53:08 -08001396 if (FormatTok->is(tok::identifier))
Manuel Klimek308232c2013-01-21 19:17:52 +00001397 nextToken();
1398 }
Stephen Hines176edba2014-12-01 14:53:08 -08001399
1400 // Just a declaration or something is wrong.
1401 if (FormatTok->isNot(tok::l_brace))
1402 return;
1403 FormatTok->BlockKind = BK_Block;
1404
1405 if (Style.Language == FormatStyle::LK_Java) {
1406 // Java enums are different.
1407 parseJavaEnumBody();
1408 return;
Manuel Klimek308232c2013-01-21 19:17:52 +00001409 }
Stephen Hines176edba2014-12-01 14:53:08 -08001410
1411 // Parse enum body.
1412 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1413 if (HasError) {
1414 if (FormatTok->is(tok::semi))
1415 nextToken();
1416 addUnwrappedLine();
1417 }
1418
Manuel Klimek308232c2013-01-21 19:17:52 +00001419 // We fall through to parsing a structural element afterwards, so that in
1420 // enum A {} n, m;
1421 // "} n, m;" will end up in one unwrapped line.
Daniel Jasperbac016b2012-12-03 18:12:45 +00001422}
1423
Stephen Hines176edba2014-12-01 14:53:08 -08001424void UnwrappedLineParser::parseJavaEnumBody() {
1425 // Determine whether the enum is simple, i.e. does not have a semicolon or
1426 // constants with class bodies. Simple enums can be formatted like braced
1427 // lists, contracted to a single line, etc.
1428 unsigned StoredPosition = Tokens->getPosition();
1429 bool IsSimple = true;
1430 FormatToken *Tok = Tokens->getNextToken();
1431 while (Tok) {
1432 if (Tok->is(tok::r_brace))
1433 break;
1434 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1435 IsSimple = false;
1436 break;
1437 }
1438 // FIXME: This will also mark enums with braces in the arguments to enum
1439 // constants as "not simple". This is probably fine in practice, though.
1440 Tok = Tokens->getNextToken();
1441 }
1442 FormatTok = Tokens->setPosition(StoredPosition);
1443
1444 if (IsSimple) {
1445 parseBracedList();
1446 addUnwrappedLine();
1447 return;
1448 }
1449
1450 // Parse the body of a more complex enum.
1451 // First add a line for everything up to the "{".
Manuel Klimekde768542013-01-07 18:10:23 +00001452 nextToken();
Stephen Hines176edba2014-12-01 14:53:08 -08001453 addUnwrappedLine();
1454 ++Line->Level;
1455
1456 // Parse the enum constants.
1457 while (FormatTok) {
1458 if (FormatTok->is(tok::l_brace)) {
1459 // Parse the constant's class body.
1460 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1461 /*MunchSemi=*/false);
1462 } else if (FormatTok->is(tok::l_paren)) {
1463 parseParens();
1464 } else if (FormatTok->is(tok::comma)) {
1465 nextToken();
1466 addUnwrappedLine();
1467 } else if (FormatTok->is(tok::semi)) {
1468 nextToken();
1469 addUnwrappedLine();
1470 break;
1471 } else if (FormatTok->is(tok::r_brace)) {
1472 addUnwrappedLine();
1473 break;
1474 } else {
1475 nextToken();
1476 }
1477 }
1478
1479 // Parse the class body after the enum's ";" if any.
1480 parseLevel(/*HasOpeningBrace=*/true);
1481 nextToken();
1482 --Line->Level;
1483 addUnwrappedLine();
1484}
1485
1486void UnwrappedLineParser::parseRecord() {
1487 const FormatToken &InitialToken = *FormatTok;
1488 nextToken();
1489 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1490 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001491 nextToken();
1492 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001493 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001494 parseParens();
Manuel Klimekde768542013-01-07 18:10:23 +00001495 }
Manuel Klimekb8b1ce12013-02-06 15:57:54 +00001496 // The actual identifier can be a nested name specifier, and in macros
1497 // it is often token-pasted.
Stephen Hines176edba2014-12-01 14:53:08 -08001498 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1499 FormatTok->is(tok::hashhash) ||
1500 (Style.Language == FormatStyle::LK_Java &&
1501 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001502 nextToken();
1503
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001504 // Note that parsing away template declarations here leads to incorrectly
1505 // accepting function declarations as record declarations.
1506 // In general, we cannot solve this problem. Consider:
1507 // class A<int> B() {}
1508 // which can be a function definition or a class definition when B() is a
1509 // macro. If we find enough real-world cases where this is a problem, we
1510 // can parse for the 'template' keyword in the beginning of the statement,
1511 // and thus rule out the record production in case there is no template
1512 // (this would still leave us with an ambiguity between template function
1513 // and class declarations).
Manuel Klimek96e888b2013-05-28 11:55:06 +00001514 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1515 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1516 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek47ea7f62013-01-15 13:38:33 +00001517 return;
1518 nextToken();
1519 }
1520 }
1521 }
Manuel Klimek96e888b2013-05-28 11:55:06 +00001522 if (FormatTok->Tok.is(tok::l_brace)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001523 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimek44135b82013-05-13 12:51:40 +00001524 addUnwrappedLine();
1525
Stephen Hines651f13c2014-04-23 16:59:28 -07001526 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekaabfb272013-10-12 22:46:56 +00001527 /*MunchSemi=*/false);
Manuel Klimek44135b82013-05-13 12:51:40 +00001528 }
Manuel Klimek3a3408c2013-01-21 13:58:54 +00001529 // We fall through to parsing a structural element afterwards, so
1530 // class A {} n, m;
1531 // will end up in one unwrapped line.
Stephen Hines176edba2014-12-01 14:53:08 -08001532 // This does not apply for Java.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001533 if (Style.Language == FormatStyle::LK_Java ||
1534 Style.Language == FormatStyle::LK_JavaScript)
Stephen Hines176edba2014-12-01 14:53:08 -08001535 addUnwrappedLine();
Manuel Klimekde768542013-01-07 18:10:23 +00001536}
1537
Nico Weber1abe6ea2013-01-09 21:15:03 +00001538void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001539 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber1abe6ea2013-01-09 21:15:03 +00001540 do
1541 nextToken();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001542 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber1abe6ea2013-01-09 21:15:03 +00001543 nextToken(); // Skip '>'.
1544}
1545
1546void UnwrappedLineParser::parseObjCUntilAtEnd() {
1547 do {
Manuel Klimek96e888b2013-05-28 11:55:06 +00001548 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001549 nextToken();
1550 addUnwrappedLine();
1551 break;
1552 }
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001553 if (FormatTok->is(tok::l_brace)) {
1554 parseBlock(/*MustBeDeclaration=*/false);
1555 // In ObjC interfaces, nothing should be following the "}".
1556 addUnwrappedLine();
Stephen Hines651f13c2014-04-23 16:59:28 -07001557 } else if (FormatTok->is(tok::r_brace)) {
1558 // Ignore stray "}". parseStructuralElement doesn't consume them.
1559 nextToken();
1560 addUnwrappedLine();
Daniel Jasper7186ccc2013-08-28 08:04:23 +00001561 } else {
1562 parseStructuralElement();
1563 }
Nico Weber1abe6ea2013-01-09 21:15:03 +00001564 } while (!eof());
1565}
1566
Nico Weber50767d82013-01-09 23:25:37 +00001567void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber27d13672013-01-09 20:25:35 +00001568 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001569 nextToken(); // interface name
Nico Weber27d13672013-01-09 20:25:35 +00001570
1571 // @interface can be followed by either a base class, or a category.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001572 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber27d13672013-01-09 20:25:35 +00001573 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001574 nextToken(); // base class name
Manuel Klimek96e888b2013-05-28 11:55:06 +00001575 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber27d13672013-01-09 20:25:35 +00001576 // Skip category, if present.
1577 parseParens();
1578
Manuel Klimek96e888b2013-05-28 11:55:06 +00001579 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001580 parseObjCProtocolList();
Nico Weber27d13672013-01-09 20:25:35 +00001581
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001582 if (FormatTok->Tok.is(tok::l_brace)) {
1583 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1584 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1585 addUnwrappedLine();
Nico Weber27268772013-06-26 00:30:14 +00001586 parseBlock(/*MustBeDeclaration=*/true);
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001587 }
Nico Weber27d13672013-01-09 20:25:35 +00001588
1589 // With instance variables, this puts '}' on its own line. Without instance
1590 // variables, this ends the @interface line.
1591 addUnwrappedLine();
1592
Nico Weber1abe6ea2013-01-09 21:15:03 +00001593 parseObjCUntilAtEnd();
1594}
Nico Weber27d13672013-01-09 20:25:35 +00001595
Nico Weber1abe6ea2013-01-09 21:15:03 +00001596void UnwrappedLineParser::parseObjCProtocol() {
1597 nextToken();
Daniel Jasperf9955d32013-03-20 12:37:50 +00001598 nextToken(); // protocol name
Nico Weber1abe6ea2013-01-09 21:15:03 +00001599
Manuel Klimek96e888b2013-05-28 11:55:06 +00001600 if (FormatTok->Tok.is(tok::less))
Nico Weber1abe6ea2013-01-09 21:15:03 +00001601 parseObjCProtocolList();
1602
1603 // Check for protocol declaration.
Manuel Klimek96e888b2013-05-28 11:55:06 +00001604 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber1abe6ea2013-01-09 21:15:03 +00001605 nextToken();
1606 return addUnwrappedLine();
1607 }
1608
1609 addUnwrappedLine();
1610 parseObjCUntilAtEnd();
Nico Weber27d13672013-01-09 20:25:35 +00001611}
1612
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001613void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1614 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
1615 nextToken();
1616
1617 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_function,
1618 Keywords.kw_var))
1619 return; // Fall through to parsing the corresponding structure.
1620
1621 if (FormatTok->is(tok::kw_default)) {
1622 nextToken(); // export default ..., fall through after eating 'default'.
1623 return;
1624 }
1625
1626 if (FormatTok->is(tok::l_brace)) {
1627 FormatTok->BlockKind = BK_Block;
1628 parseBracedList();
1629 }
1630
1631 while (!eof() && FormatTok->isNot(tok::semi) &&
1632 FormatTok->isNot(tok::l_brace)) {
1633 nextToken();
1634 }
1635}
1636
Daniel Jasperd98927d2013-09-05 16:05:56 +00001637LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1638 StringRef Prefix = "") {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001639 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1640 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1641 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1642 E = Line.Tokens.end();
1643 I != E; ++I) {
Daniel Jasperac2c9742013-09-05 10:04:31 +00001644 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper567dcf92013-09-05 09:29:45 +00001645 }
1646 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1647 E = Line.Tokens.end();
1648 I != E; ++I) {
1649 const UnwrappedLineNode &Node = *I;
1650 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1651 I = Node.Children.begin(),
1652 E = Node.Children.end();
1653 I != E; ++I) {
1654 printDebugInfo(*I, "\nChild: ");
1655 }
1656 }
1657 llvm::dbgs() << "\n";
1658}
1659
Daniel Jasperbac016b2012-12-03 18:12:45 +00001660void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001661 if (Line->Tokens.empty())
Daniel Jasper26f7e782013-01-08 14:56:18 +00001662 return;
Manuel Klimek8fa37992013-01-16 12:31:12 +00001663 DEBUG({
Daniel Jasper567dcf92013-09-05 09:29:45 +00001664 if (CurrentLines == &Lines)
1665 printDebugInfo(*Line);
Manuel Klimek8fa37992013-01-16 12:31:12 +00001666 });
Manuel Klimek525fe162013-01-18 14:04:34 +00001667 CurrentLines->push_back(*Line);
Daniel Jaspercbb6c412013-01-16 09:10:19 +00001668 Line->Tokens.clear();
Manuel Klimek525fe162013-01-18 14:04:34 +00001669 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001670 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jasper516fb312013-03-01 18:11:39 +00001671 I = PreprocessorDirectives.begin(),
1672 E = PreprocessorDirectives.end();
Manuel Klimek525fe162013-01-18 14:04:34 +00001673 I != E; ++I) {
1674 CurrentLines->push_back(*I);
1675 }
1676 PreprocessorDirectives.clear();
1677 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001678}
1679
Manuel Klimek96e888b2013-05-28 11:55:06 +00001680bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001681
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001682bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
1683 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1684 FormatTok.NewlinesBefore > 0;
1685}
1686
Manuel Klimek86721d22013-01-22 16:31:55 +00001687void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1688 bool JustComments = Line->Tokens.empty();
Manuel Klimek96e888b2013-05-28 11:55:06 +00001689 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimek86721d22013-01-22 16:31:55 +00001690 I = CommentsBeforeNextToken.begin(),
1691 E = CommentsBeforeNextToken.end();
1692 I != E; ++I) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001693 if (isOnNewLine(**I) && JustComments) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001694 addUnwrappedLine();
1695 }
1696 pushToken(*I);
1697 }
1698 if (NewlineBeforeNext && JustComments) {
1699 addUnwrappedLine();
1700 }
1701 CommentsBeforeNextToken.clear();
1702}
1703
Daniel Jasperbac016b2012-12-03 18:12:45 +00001704void UnwrappedLineParser::nextToken() {
1705 if (eof())
1706 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001707 flushComments(isOnNewLine(*FormatTok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001708 pushToken(FormatTok);
Manuel Klimekd4397b92013-01-04 23:34:14 +00001709 readToken();
1710}
1711
1712void UnwrappedLineParser::readToken() {
Manuel Klimek86721d22013-01-22 16:31:55 +00001713 bool CommentsInCurrentLine = true;
1714 do {
1715 FormatTok = Tokens->getNextToken();
Stephen Hines651f13c2014-04-23 16:59:28 -07001716 assert(FormatTok);
Manuel Klimek96e888b2013-05-28 11:55:06 +00001717 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1718 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001719 // If there is an unfinished unwrapped line, we flush the preprocessor
1720 // directives only after that unwrapped line was finished later.
Stephen Hines0e2c34f2015-03-23 12:09:02 -07001721 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimek86721d22013-01-22 16:31:55 +00001722 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienko4128e192013-04-03 12:38:53 +00001723 // Comments stored before the preprocessor directive need to be output
1724 // before the preprocessor directive, at the same level as the
1725 // preprocessor directive, as we consider them to apply to the directive.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001726 flushComments(isOnNewLine(*FormatTok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001727 parsePPDirective();
1728 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001729 while (FormatTok->Type == TT_ConflictStart ||
1730 FormatTok->Type == TT_ConflictEnd ||
1731 FormatTok->Type == TT_ConflictAlternative) {
1732 if (FormatTok->Type == TT_ConflictStart) {
1733 conditionalCompilationStart(/*Unreachable=*/false);
1734 } else if (FormatTok->Type == TT_ConflictAlternative) {
1735 conditionalCompilationAlternative();
1736 } else if (FormatTok->Type == TT_ConflictEnd) {
1737 conditionalCompilationEnd();
1738 }
1739 FormatTok = Tokens->getNextToken();
1740 FormatTok->MustBreakBefore = true;
1741 }
Alexander Kornienko6fb46b02013-05-24 18:24:24 +00001742
1743 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1744 !Line->InPPDirective) {
1745 continue;
1746 }
1747
Manuel Klimek96e888b2013-05-28 11:55:06 +00001748 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimek86721d22013-01-22 16:31:55 +00001749 return;
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001750 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimek86721d22013-01-22 16:31:55 +00001751 CommentsInCurrentLine = false;
1752 }
1753 if (CommentsInCurrentLine) {
1754 pushToken(FormatTok);
1755 } else {
1756 CommentsBeforeNextToken.push_back(FormatTok);
1757 }
1758 } while (!eof());
1759}
1760
Manuel Klimek96e888b2013-05-28 11:55:06 +00001761void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001762 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimek86721d22013-01-22 16:31:55 +00001763 if (MustBreakBeforeNextToken) {
Daniel Jasper567dcf92013-09-05 09:29:45 +00001764 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimek86721d22013-01-22 16:31:55 +00001765 MustBreakBeforeNextToken = false;
Manuel Klimekd4397b92013-01-04 23:34:14 +00001766 }
Daniel Jasperbac016b2012-12-03 18:12:45 +00001767}
1768
Daniel Jaspercd162382013-01-07 13:26:07 +00001769} // end namespace format
1770} // end namespace clang