blob: 08115036cdf735eac7212af4b96726bafc06e2d6 [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file contains the implementation of the UnwrappedLineParser,
12/// which turns a stream of tokens into UnwrappedLines.
13///
Daniel Jasperf7935112012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Chandler Carruth4b417452013-01-19 08:09:44 +000016#include "UnwrappedLineParser.h"
Benjamin Kramer33335df2015-03-01 21:36:40 +000017#include "llvm/ADT/STLExtras.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000018#include "llvm/Support/Debug.h"
Benjamin Kramer53f5e892015-03-23 18:05:43 +000019#include "llvm/Support/raw_ostream.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000020
Chandler Carruth10346662014-04-22 03:17:02 +000021#define DEBUG_TYPE "format-parser"
22
Daniel Jasperf7935112012-12-03 18:12:45 +000023namespace clang {
24namespace format {
25
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000026class FormatTokenSource {
27public:
28 virtual ~FormatTokenSource() {}
29 virtual FormatToken *getNextToken() = 0;
30
31 virtual unsigned getPosition() = 0;
32 virtual FormatToken *setPosition(unsigned Position) = 0;
33};
34
Craig Topper69665e12013-07-01 04:21:54 +000035namespace {
36
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000037class ScopedDeclarationState {
38public:
39 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
40 bool MustBeDeclaration)
41 : Line(Line), Stack(Stack) {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000042 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek39080572013-01-23 11:03:04 +000043 Stack.push_back(MustBeDeclaration);
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000044 }
45 ~ScopedDeclarationState() {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000046 Stack.pop_back();
Manuel Klimekc1237a82013-01-23 14:08:21 +000047 if (!Stack.empty())
48 Line.MustBeDeclaration = Stack.back();
49 else
50 Line.MustBeDeclaration = true;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000051 }
Daniel Jasper393564f2013-05-31 14:56:29 +000052
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000053private:
54 UnwrappedLine &Line;
55 std::vector<bool> &Stack;
56};
57
Manuel Klimek1abf7892013-01-04 23:34:14 +000058class ScopedMacroState : public FormatTokenSource {
59public:
60 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek20e0af62015-05-06 11:56:29 +000061 FormatToken *&ResetToken)
Manuel Klimek1abf7892013-01-04 23:34:14 +000062 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000063 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
Manuel Klimek20e0af62015-05-06 11:56:29 +000064 Token(nullptr) {
Manuel Klimek1abf7892013-01-04 23:34:14 +000065 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000066 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000067 Line.InPPDirective = true;
68 }
69
Alexander Kornienko34eb2072015-04-11 02:00:23 +000070 ~ScopedMacroState() override {
Manuel Klimek1abf7892013-01-04 23:34:14 +000071 TokenSource = PreviousTokenSource;
72 ResetToken = Token;
73 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000074 Line.Level = PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +000075 }
76
Craig Topperfb6b25b2014-03-15 04:29:04 +000077 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +000078 // The \c UnwrappedLineParser guards against this by never calling
79 // \c getNextToken() after it has encountered the first eof token.
80 assert(!eof());
Manuel Klimek1abf7892013-01-04 23:34:14 +000081 Token = PreviousTokenSource->getNextToken();
82 if (eof())
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000083 return getFakeEOF();
Manuel Klimek1abf7892013-01-04 23:34:14 +000084 return Token;
85 }
86
Craig Topperfb6b25b2014-03-15 04:29:04 +000087 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +000088
Craig Topperfb6b25b2014-03-15 04:29:04 +000089 FormatToken *setPosition(unsigned Position) override {
Manuel Klimekab419912013-05-23 09:41:43 +000090 Token = PreviousTokenSource->setPosition(Position);
91 return Token;
92 }
93
Manuel Klimek1abf7892013-01-04 23:34:14 +000094private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000095 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimek1abf7892013-01-04 23:34:14 +000096
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000097 FormatToken *getFakeEOF() {
98 static bool EOFInitialized = false;
99 static FormatToken FormatTok;
100 if (!EOFInitialized) {
101 FormatTok.Tok.startToken();
102 FormatTok.Tok.setKind(tok::eof);
103 EOFInitialized = true;
104 }
105 return &FormatTok;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000106 }
107
108 UnwrappedLine &Line;
109 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000110 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000111 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000112 FormatTokenSource *PreviousTokenSource;
113
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000114 FormatToken *Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000115};
116
Craig Topper69665e12013-07-01 04:21:54 +0000117} // end anonymous namespace
118
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000119class ScopedLineState {
120public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000121 ScopedLineState(UnwrappedLineParser &Parser,
122 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000123 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000124 if (SwitchToPreprocessorLines)
125 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000126 else if (!Parser.Line->Tokens.empty())
127 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000128 PreBlockLine = std::move(Parser.Line);
129 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000130 Parser.Line->Level = PreBlockLine->Level;
131 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000132 }
133
134 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000135 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000136 Parser.addUnwrappedLine();
137 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000138 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000139 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000140 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
141 Parser.MustBreakBeforeNextToken = true;
142 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000143 }
144
145private:
146 UnwrappedLineParser &Parser;
147
David Blaikieefb6eb22014-08-09 20:02:07 +0000148 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000149 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000150};
151
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000152class CompoundStatementIndenter {
153public:
154 CompoundStatementIndenter(UnwrappedLineParser *Parser,
155 const FormatStyle &Style, unsigned &LineLevel)
156 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
157 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
158 Parser->addUnwrappedLine();
159 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
160 Parser->addUnwrappedLine();
161 ++LineLevel;
162 }
163 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000164 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000165
166private:
167 unsigned &LineLevel;
168 unsigned OldLineLevel;
169};
170
Craig Topper69665e12013-07-01 04:21:54 +0000171namespace {
172
Manuel Klimekab419912013-05-23 09:41:43 +0000173class IndexedTokenSource : public FormatTokenSource {
174public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000175 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000176 : Tokens(Tokens), Position(-1) {}
177
Craig Topperfb6b25b2014-03-15 04:29:04 +0000178 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000179 ++Position;
180 return Tokens[Position];
181 }
182
Craig Topperfb6b25b2014-03-15 04:29:04 +0000183 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000184 assert(Position >= 0);
185 return Position;
186 }
187
Craig Topperfb6b25b2014-03-15 04:29:04 +0000188 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000189 Position = P;
190 return Tokens[Position];
191 }
192
Manuel Klimek71814b42013-10-11 21:25:45 +0000193 void reset() { Position = -1; }
194
Manuel Klimekab419912013-05-23 09:41:43 +0000195private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000196 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000197 int Position;
198};
199
Craig Topper69665e12013-07-01 04:21:54 +0000200} // end anonymous namespace
201
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000202UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000203 const AdditionalKeywords &Keywords,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000204 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000205 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000206 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Manuel Klimek20e0af62015-05-06 11:56:29 +0000207 CurrentLines(&Lines), Style(Style), Keywords(Keywords), Tokens(nullptr),
208 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000209
210void UnwrappedLineParser::reset() {
211 PPBranchLevel = -1;
212 Line.reset(new UnwrappedLine);
213 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000214 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000215 MustBreakBeforeNextToken = false;
216 PreprocessorDirectives.clear();
217 CurrentLines = &Lines;
218 DeclarationScopeStack.clear();
Manuel Klimek71814b42013-10-11 21:25:45 +0000219 PPStack.clear();
220}
Daniel Jasperf7935112012-12-03 18:12:45 +0000221
Manuel Klimek20e0af62015-05-06 11:56:29 +0000222void UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000223 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimek71814b42013-10-11 21:25:45 +0000224 do {
225 DEBUG(llvm::dbgs() << "----\n");
226 reset();
227 Tokens = &TokenSource;
228 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000229
Manuel Klimek71814b42013-10-11 21:25:45 +0000230 readToken();
231 parseFile();
232 // Create line with eof token.
233 pushToken(FormatTok);
234 addUnwrappedLine();
235
236 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
237 E = Lines.end();
238 I != E; ++I) {
239 Callback.consumeUnwrappedLine(*I);
240 }
241 Callback.finishRun();
242 Lines.clear();
243 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000244 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000245 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
246 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
247 }
248 if (!PPLevelBranchIndex.empty()) {
249 ++PPLevelBranchIndex.back();
250 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
251 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
252 }
253 } while (!PPLevelBranchIndex.empty());
Manuel Klimek1abf7892013-01-04 23:34:14 +0000254}
255
Manuel Klimek1a18c402013-04-12 14:13:36 +0000256void UnwrappedLineParser::parseFile() {
Daniel Jasper9326f912015-05-05 08:40:32 +0000257 // The top-level context in a file always has declarations, except for pre-
258 // processor directives and JavaScript files.
259 bool MustBeDeclaration =
260 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
261 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
262 MustBeDeclaration);
Nico Weber9096fc02013-06-26 00:30:14 +0000263 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000264 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000265 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000266 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000267}
268
Manuel Klimek1a18c402013-04-12 14:13:36 +0000269void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000270 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000271 do {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000272 tok::TokenKind kind = FormatTok->Tok.getKind();
273 if (FormatTok->Type == TT_MacroBlockBegin) {
274 kind = tok::l_brace;
275 } else if (FormatTok->Type == TT_MacroBlockEnd) {
276 kind = tok::r_brace;
277 }
278
279 switch (kind) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000280 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000281 nextToken();
282 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000283 break;
284 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000285 // FIXME: Add parameter whether this can happen - if this happens, we must
286 // be in a non-declaration context.
Daniel Jasperb86e2722015-08-24 13:23:37 +0000287 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
288 continue;
Nico Weber9096fc02013-06-26 00:30:14 +0000289 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000290 addUnwrappedLine();
291 break;
292 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000293 if (HasOpeningBrace)
294 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000295 nextToken();
296 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000297 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000298 case tok::kw_default:
299 case tok::kw_case:
Daniel Jasper72407622013-09-02 08:26:29 +0000300 if (!SwitchLabelEncountered &&
301 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
302 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000303 SwitchLabelEncountered = true;
304 parseStructuralElement();
305 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000306 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000307 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000308 break;
309 }
310 } while (!eof());
311}
312
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000313void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
Manuel Klimekab419912013-05-23 09:41:43 +0000314 // We'll parse forward through the tokens until we hit
315 // a closing brace or eof - note that getNextToken() will
316 // parse macros, so this will magically work inside macro
317 // definitions, too.
318 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000319 FormatToken *Tok = FormatTok;
Manuel Klimekab419912013-05-23 09:41:43 +0000320 // Keep a stack of positions of lbrace tokens. We will
321 // update information about whether an lbrace starts a
322 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000323 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000324 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000325 do {
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000326 // Get next none-comment token.
327 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000328 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000329 do {
330 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000331 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000332 } while (NextTok->is(tok::comment));
333
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000334 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000335 case tok::l_brace:
Daniel Jasper3c883d12015-05-18 14:49:19 +0000336 Tok->BlockKind = BK_Unknown;
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000337 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000338 break;
339 case tok::r_brace:
340 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000341 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Daniel Jasper220c0d12014-04-10 07:27:12 +0000342 bool ProbablyBracedList = false;
343 if (Style.Language == FormatStyle::LK_Proto) {
344 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
345 } else {
Daniel Jasper91b032a2014-05-22 12:46:38 +0000346 // Using OriginalColumn to distinguish between ObjC methods and
347 // binary operators is a bit hacky.
348 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
349 NextTok->OriginalColumn == 0;
350
Daniel Jasper220c0d12014-04-10 07:27:12 +0000351 // If there is a comma, semicolon or right paren after the closing
352 // brace, we assume this is a braced initializer list. Note that
353 // regardless how we mark inner braces here, we will overwrite the
354 // BlockKind later if we parse a braced list (where all blocks
355 // inside are by default braced lists), or when we explicitly detect
356 // blocks (for example while parsing lambdas).
357 //
358 // We exclude + and - as they can be ObjC visibility modifiers.
359 ProbablyBracedList =
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000360 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
Daniel Jasper438059e2014-05-22 12:11:13 +0000361 tok::r_paren, tok::r_square, tok::l_brace,
Daniel Jasper65df5aa2014-08-04 14:51:02 +0000362 tok::l_paren, tok::ellipsis) ||
Daniel Jaspercec9ffd2015-05-18 14:12:24 +0000363 (NextTok->is(tok::semi) &&
364 (!ExpectClassBody || LBraceStack.size() != 1)) ||
Daniel Jasper91b032a2014-05-22 12:46:38 +0000365 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000366 }
367 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000368 Tok->BlockKind = BK_BracedInit;
369 LBraceStack.back()->BlockKind = BK_BracedInit;
370 } else {
371 Tok->BlockKind = BK_Block;
372 LBraceStack.back()->BlockKind = BK_Block;
373 }
Manuel Klimekab419912013-05-23 09:41:43 +0000374 }
375 LBraceStack.pop_back();
376 }
377 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000378 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000379 case tok::semi:
380 case tok::kw_if:
381 case tok::kw_while:
382 case tok::kw_for:
383 case tok::kw_switch:
384 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000385 case tok::kw___try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000386 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000387 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000388 break;
389 default:
390 break;
391 }
392 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000393 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000394 // Assume other blocks for all unclosed opening braces.
395 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000396 if (LBraceStack[i]->BlockKind == BK_Unknown)
397 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000398 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000399
Manuel Klimekab419912013-05-23 09:41:43 +0000400 FormatTok = Tokens->setPosition(StoredPosition);
401}
402
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000403void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
404 bool MunchSemi) {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000405 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
406 "'{' or macro block token expected");
407 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
408
Daniel Jasper516d7972013-07-25 11:31:57 +0000409 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000410 nextToken();
411
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000412 if (MacroBlock && FormatTok->is(tok::l_paren))
413 parseParens();
414
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000415 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000416
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000417 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
418 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000419 if (AddLevel)
420 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000421 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000422
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000423 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
424 : !FormatTok->is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000425 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000426 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000427 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000428
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000429 nextToken(); // Munch the closing brace.
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000430
431 if (MacroBlock && FormatTok->is(tok::l_paren))
432 parseParens();
433
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000434 if (MunchSemi && FormatTok->Tok.is(tok::semi))
435 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000436 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000437}
438
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000439static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000440 // FIXME: Closure-library specific stuff should not be hard-coded but be
441 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000442 if (Line.Tokens.size() < 4)
443 return false;
444 auto I = Line.Tokens.begin();
445 if (I->Tok->TokenText != "goog")
446 return false;
447 ++I;
448 if (I->Tok->isNot(tok::period))
449 return false;
450 ++I;
451 if (I->Tok->TokenText != "scope")
452 return false;
453 ++I;
454 return I->Tok->is(tok::l_paren);
455}
456
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000457static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
458 const FormatToken &InitialToken) {
459 switch (Style.BreakBeforeBraces) {
460 case FormatStyle::BS_Linux:
461 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
Birunthan Mohanathas305fa9c2015-07-12 03:13:54 +0000462 case FormatStyle::BS_Mozilla:
463 return InitialToken.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union);
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000464 case FormatStyle::BS_Allman:
465 case FormatStyle::BS_GNU:
466 return true;
467 default:
468 return false;
469 }
470}
471
Manuel Klimek516e0542013-09-04 13:25:30 +0000472void UnwrappedLineParser::parseChildBlock() {
473 FormatTok->BlockKind = BK_Block;
474 nextToken();
475 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000476 bool GoogScope =
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000477 Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000478 ScopedLineState LineState(*this);
479 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
480 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000481 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000482 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000483 flushComments(isOnNewLine(*FormatTok));
Daniel Jasper4a39c842014-05-06 13:54:10 +0000484 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000485 }
486 nextToken();
487}
488
Daniel Jasperf7935112012-12-03 18:12:45 +0000489void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000490 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000491 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000492 nextToken();
493
Craig Topper2145bc02014-05-09 08:15:10 +0000494 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000495 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000496 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000497 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000498
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000499 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000500 case tok::pp_define:
501 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000502 return;
503 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000504 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000505 break;
506 case tok::pp_ifdef:
507 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000508 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000509 break;
510 case tok::pp_else:
511 parsePPElse();
512 break;
513 case tok::pp_elif:
514 parsePPElIf();
515 break;
516 case tok::pp_endif:
517 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000518 break;
519 default:
520 parsePPUnknown();
521 break;
522 }
523}
524
Manuel Klimek68b03042014-04-14 09:14:11 +0000525void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
526 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000527 PPStack.push_back(PP_Unreachable);
528 else
529 PPStack.push_back(PP_Conditional);
530}
531
Manuel Klimek68b03042014-04-14 09:14:11 +0000532void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000533 ++PPBranchLevel;
534 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
535 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
536 PPLevelBranchIndex.push_back(0);
537 PPLevelBranchCount.push_back(0);
538 }
539 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000540 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
541 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000542}
543
Manuel Klimek68b03042014-04-14 09:14:11 +0000544void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000545 if (!PPStack.empty())
546 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000547 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
548 if (!PPChainBranchIndex.empty())
549 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000550 conditionalCompilationCondition(
551 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
552 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000553}
554
Manuel Klimek68b03042014-04-14 09:14:11 +0000555void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000556 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
557 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
558 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000559 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
560 }
561 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000562 // Guard against #endif's without #if.
563 if (PPBranchLevel > 0)
564 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000565 if (!PPChainBranchIndex.empty())
566 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000567 if (!PPStack.empty())
568 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000569}
570
571void UnwrappedLineParser::parsePPIf(bool IfDef) {
572 nextToken();
573 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Daniel Jasper9d22bcc2015-01-19 10:51:05 +0000574 FormatTok->Tok.getLiteralData() != nullptr &&
Manuel Klimek68b03042014-04-14 09:14:11 +0000575 StringRef(FormatTok->Tok.getLiteralData(),
576 FormatTok->Tok.getLength()) == "0") ||
577 FormatTok->Tok.is(tok::kw_false);
578 conditionalCompilationStart(!IfDef && IsLiteralFalse);
579 parsePPUnknown();
580}
581
582void UnwrappedLineParser::parsePPElse() {
583 conditionalCompilationAlternative();
584 parsePPUnknown();
585}
586
587void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
588
589void UnwrappedLineParser::parsePPEndIf() {
590 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000591 parsePPUnknown();
592}
593
Manuel Klimek1abf7892013-01-04 23:34:14 +0000594void UnwrappedLineParser::parsePPDefine() {
595 nextToken();
596
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000597 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000598 parsePPUnknown();
599 return;
600 }
601 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000602 if (FormatTok->Tok.getKind() == tok::l_paren &&
603 FormatTok->WhitespaceRange.getBegin() ==
604 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000605 parseParens();
606 }
607 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000608 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000609
610 // Errors during a preprocessor directive can only affect the layout of the
611 // preprocessor directive, and thus we ignore them. An alternative approach
612 // would be to use the same approach we use on the file level (no
613 // re-indentation if there was a structural error) within the macro
614 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000615 parseFile();
616}
617
618void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000619 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000620 nextToken();
621 } while (!eof());
622 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000623}
624
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000625// Here we blacklist certain tokens that are not usually the first token in an
626// unwrapped line. This is used in attempt to distinguish macro calls without
627// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000628static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000629 // Semicolon can be a null-statement, l_square can be a start of a macro or
630 // a C++11 attribute, but this doesn't seem to be common.
631 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
632 Tok.isNot(tok::l_square) &&
633 // Tokens that can only be used as binary operators and a part of
634 // overloaded operator names.
635 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
636 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
637 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
638 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
639 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
640 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
641 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
642 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
643 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
644 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
645 Tok.isNot(tok::lesslessequal) &&
646 // Colon is used in labels, base class lists, initializer lists,
647 // range-based for loops, ternary operator, but should never be the
648 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000649 Tok.isNot(tok::colon) &&
650 // 'noexcept' is a trailing annotation.
651 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000652}
653
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000654void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000655 assert(!FormatTok->Tok.is(tok::l_brace));
656 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000657 case tok::at:
658 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000659 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000660 parseBracedList();
661 break;
662 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000663 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000664 case tok::objc_public:
665 case tok::objc_protected:
666 case tok::objc_package:
667 case tok::objc_private:
668 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000669 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000670 case tok::objc_implementation:
671 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000672 case tok::objc_protocol:
673 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000674 case tok::objc_end:
675 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000676 case tok::objc_optional:
677 case tok::objc_required:
678 nextToken();
679 addUnwrappedLine();
680 return;
Nico Weber45c48122015-06-28 01:06:16 +0000681 case tok::objc_autoreleasepool:
682 nextToken();
683 if (FormatTok->Tok.is(tok::l_brace)) {
684 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
685 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
686 addUnwrappedLine();
687 parseBlock(/*MustBeDeclaration=*/false);
688 }
689 addUnwrappedLine();
690 return;
Nico Weber33381f52015-02-07 01:57:32 +0000691 case tok::objc_try:
692 // This branch isn't strictly necessary (the kw_try case below would
693 // do this too after the tok::at is parsed above). But be explicit.
694 parseTryCatch();
695 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000696 default:
697 break;
698 }
699 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000700 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000701 nextToken();
702 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000703 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000704 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000705 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000706 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000707 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000708 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000709 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000710 break;
711 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000712 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000713 nextToken();
714 }
715 }
716 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000717 case tok::kw_namespace:
718 parseNamespace();
719 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000720 case tok::kw_inline:
721 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000722 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000723 parseNamespace();
724 return;
725 }
726 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000727 case tok::kw_public:
728 case tok::kw_protected:
729 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +0000730 if (Style.Language == FormatStyle::LK_Java ||
731 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000732 nextToken();
733 else
734 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000735 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000736 case tok::kw_if:
737 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000738 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000739 case tok::kw_for:
740 case tok::kw_while:
741 parseForOrWhileLoop();
742 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000743 case tok::kw_do:
744 parseDoWhile();
745 return;
746 case tok::kw_switch:
747 parseSwitch();
748 return;
749 case tok::kw_default:
750 nextToken();
751 parseLabel();
752 return;
753 case tok::kw_case:
754 parseCaseLabel();
755 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000756 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000757 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +0000758 parseTryCatch();
759 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000760 case tok::kw_extern:
761 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000762 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000763 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000764 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000765 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000766 addUnwrappedLine();
767 return;
768 }
769 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000770 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +0000771 case tok::kw_export:
772 if (Style.Language == FormatStyle::LK_JavaScript) {
773 parseJavaScriptEs6ImportExport();
774 return;
775 }
776 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000777 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +0000778 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +0000779 parseForOrWhileLoop();
780 return;
781 }
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000782 if (FormatTok->is(TT_MacroBlockBegin)) {
783 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
784 /*MunchSemi=*/false);
785 return;
786 }
Daniel Jasper354aa512015-02-19 16:07:32 +0000787 if (Style.Language == FormatStyle::LK_JavaScript &&
788 FormatTok->is(Keywords.kw_import)) {
Daniel Jasperfca735c2015-02-19 16:14:18 +0000789 parseJavaScriptEs6ImportExport();
Daniel Jasper354aa512015-02-19 16:07:32 +0000790 return;
791 }
Daniel Jasper53395402015-04-07 15:04:40 +0000792 if (FormatTok->is(Keywords.kw_signals)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +0000793 nextToken();
794 if (FormatTok->is(tok::colon)) {
795 nextToken();
796 addUnwrappedLine();
797 }
Daniel Jasper53395402015-04-07 15:04:40 +0000798 return;
799 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000800 // In all other cases, parse the declaration.
801 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000802 default:
803 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000804 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000805 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000806 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000807 case tok::at:
808 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000809 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000810 parseBracedList();
811 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000812 case tok::kw_enum:
Daniel Jasper90cf3802015-06-17 09:44:02 +0000813 // parseEnum falls through and does not yet add an unwrapped line as an
814 // enum definition can start a structural element.
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000815 parseEnum();
Daniel Jasperc6dd2732015-07-16 14:25:43 +0000816 // This only applies for C++.
817 if (Style.Language != FormatStyle::LK_Cpp) {
Daniel Jasper90cf3802015-06-17 09:44:02 +0000818 addUnwrappedLine();
819 return;
820 }
Manuel Klimek2cec0192013-01-21 19:17:52 +0000821 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000822 case tok::kw_typedef:
823 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000824 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
825 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000826 parseEnum();
827 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000828 case tok::kw_struct:
829 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000830 case tok::kw_class:
Daniel Jasper910807d2015-06-12 04:52:02 +0000831 // parseRecord falls through and does not yet add an unwrapped line as a
832 // record declaration or definition can start a structural element.
Manuel Klimeke01bab52013-01-15 13:38:33 +0000833 parseRecord();
Daniel Jasper910807d2015-06-12 04:52:02 +0000834 // This does not apply for Java and JavaScript.
835 if (Style.Language == FormatStyle::LK_Java ||
836 Style.Language == FormatStyle::LK_JavaScript) {
837 addUnwrappedLine();
838 return;
839 }
Manuel Klimeke01bab52013-01-15 13:38:33 +0000840 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000841 case tok::period:
842 nextToken();
843 // In Java, classes have an implicit static member "class".
844 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
845 FormatTok->is(tok::kw_class))
846 nextToken();
Daniel Jasperba52fcb2015-09-28 14:29:45 +0000847 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
848 FormatTok->Tok.getIdentifierInfo())
849 // JavaScript only has pseudo keywords, all keywords are allowed to
850 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
851 nextToken();
Daniel Jaspere5d74862014-11-26 08:17:08 +0000852 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000853 case tok::semi:
854 nextToken();
855 addUnwrappedLine();
856 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000857 case tok::r_brace:
858 addUnwrappedLine();
859 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000860 case tok::l_paren:
861 parseParens();
862 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000863 case tok::caret:
864 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000865 if (FormatTok->Tok.isAnyIdentifier() ||
866 FormatTok->isSimpleTypeSpecifier())
867 nextToken();
868 if (FormatTok->is(tok::l_paren))
869 parseParens();
870 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000871 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000872 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000873 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000874 if (!tryToParseBracedList()) {
875 // A block outside of parentheses must be the last part of a
876 // structural element.
877 // FIXME: Figure out cases where this is not true, and add projections
878 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000879 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000880 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000881 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000882 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000883 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000884 return;
885 }
886 // Otherwise this was a braced init list, and the structural
887 // element continues.
888 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000889 case tok::kw_try:
890 // We arrive here when parsing function-try blocks.
891 parseTryCatch();
892 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000893 case tok::identifier: {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000894 if (FormatTok->is(TT_MacroBlockEnd)) {
895 addUnwrappedLine();
896 return;
897 }
898
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000899 // Parse function literal unless 'function' is the first token in a line
900 // in which case this should be treated as a free-standing function.
Daniel Jasper9326f912015-05-05 08:40:32 +0000901 if (Style.Language == FormatStyle::LK_JavaScript &&
902 FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000903 tryToParseJSFunction();
904 break;
905 }
Daniel Jasper9326f912015-05-05 08:40:32 +0000906 if ((Style.Language == FormatStyle::LK_JavaScript ||
907 Style.Language == FormatStyle::LK_Java) &&
908 FormatTok->is(Keywords.kw_interface)) {
909 parseRecord();
Daniel Jasper259188b2015-06-12 04:56:34 +0000910 addUnwrappedLine();
Daniel Jasper5c235c02015-07-06 14:26:04 +0000911 return;
Daniel Jasper9326f912015-05-05 08:40:32 +0000912 }
913
914 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +0000915 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +0000916 if (Line->Tokens.size() == 1 &&
917 // JS doesn't have macros, and within classes colons indicate fields,
918 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +0000919 Style.Language != FormatStyle::LK_JavaScript) {
920 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000921 parseLabel();
922 return;
923 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000924 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +0000925 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +0000926 bool FunctionLike = FormatTok->is(tok::l_paren);
927 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000928 parseParens();
Daniel Jaspere60cba12015-05-13 11:35:53 +0000929
930 bool FollowedByNewline =
931 CommentsBeforeNextToken.empty()
932 ? FormatTok->NewlinesBefore > 0
933 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
934
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000935 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
Daniel Jasper680b09b2014-11-05 10:48:04 +0000936 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000937 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000938 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000939 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000940 }
941 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000942 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000943 case tok::equal:
Manuel Klimek79e06082015-05-21 12:23:34 +0000944 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
945 // TT_JsFatArrow. The always start an expression or a child block if
946 // followed by a curly.
947 if (FormatTok->is(TT_JsFatArrow)) {
948 nextToken();
Daniel Jasperbe520bd2015-05-31 08:51:54 +0000949 if (FormatTok->is(tok::l_brace))
Manuel Klimek79e06082015-05-21 12:23:34 +0000950 parseChildBlock();
Manuel Klimek79e06082015-05-21 12:23:34 +0000951 break;
952 }
953
Daniel Jaspere25509f2012-12-17 11:29:41 +0000954 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000955 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000956 parseBracedList();
957 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000958 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000959 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000960 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000961 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +0000962 case tok::kw_new:
963 parseNew();
964 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000965 default:
966 nextToken();
967 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000968 }
969 } while (!eof());
970}
971
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000972bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper1feab0f2015-06-02 15:31:37 +0000973 if (Style.Language != FormatStyle::LK_Cpp) {
974 nextToken();
975 return false;
976 }
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000977 // FIXME: This is a dirty way to access the previous token. Find a better
978 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000979 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000980 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
981 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000982 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000983 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000984 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000985 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000986 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000987 assert(FormatTok->is(tok::l_square));
988 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000989 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000990 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000991
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000992 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000993 if (FormatTok->isSimpleTypeSpecifier()) {
994 nextToken();
995 continue;
996 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000997 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000998 case tok::l_brace:
999 break;
1000 case tok::l_paren:
1001 parseParens();
1002 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +00001003 case tok::amp:
1004 case tok::star:
1005 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +00001006 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001007 case tok::less:
1008 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001009 case tok::identifier:
Daniel Jasper5eaa0092015-08-13 13:37:08 +00001010 case tok::numeric_constant:
Daniel Jasper1067ab02014-02-11 10:16:55 +00001011 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001012 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +00001013 nextToken();
1014 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001015 case tok::arrow:
Daniel Jasper6f2b88a2015-06-05 13:18:09 +00001016 FormatTok->Type = TT_LambdaArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001017 nextToken();
1018 break;
1019 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001020 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001021 }
1022 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001023 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +00001024 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001025 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001026}
1027
1028bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1029 nextToken();
1030 if (FormatTok->is(tok::equal)) {
1031 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001032 if (FormatTok->is(tok::r_square)) {
1033 nextToken();
1034 return true;
1035 }
1036 if (FormatTok->isNot(tok::comma))
1037 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001038 nextToken();
1039 } else if (FormatTok->is(tok::amp)) {
1040 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001041 if (FormatTok->is(tok::r_square)) {
1042 nextToken();
1043 return true;
1044 }
Manuel Klimekffdeb592013-09-03 15:10:01 +00001045 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
1046 return false;
1047 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001048 if (FormatTok->is(tok::comma))
1049 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001050 } else if (FormatTok->is(tok::r_square)) {
1051 nextToken();
1052 return true;
1053 }
1054 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001055 if (FormatTok->is(tok::amp))
1056 nextToken();
1057 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
1058 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001059 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +00001060 if (FormatTok->is(tok::ellipsis))
1061 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001062 if (FormatTok->is(tok::comma)) {
1063 nextToken();
1064 } else if (FormatTok->is(tok::r_square)) {
1065 nextToken();
1066 return true;
1067 } else {
1068 return false;
1069 }
1070 } while (!eof());
1071 return false;
1072}
1073
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001074void UnwrappedLineParser::tryToParseJSFunction() {
1075 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001076
1077 // Consume function name.
1078 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001079 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001080
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001081 if (FormatTok->isNot(tok::l_paren))
1082 return;
Manuel Klimek79e06082015-05-21 12:23:34 +00001083
1084 // Parse formal parameter list.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001085 parseParens();
Manuel Klimek79e06082015-05-21 12:23:34 +00001086
1087 if (FormatTok->is(tok::colon)) {
1088 // Parse a type definition.
1089 nextToken();
1090
1091 // Eat the type declaration. For braced inline object types, balance braces,
1092 // otherwise just parse until finding an l_brace for the function body.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001093 if (FormatTok->is(tok::l_brace))
1094 tryToParseBracedList();
1095 else
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001096 while (FormatTok->isNot(tok::l_brace) && !eof())
Manuel Klimek79e06082015-05-21 12:23:34 +00001097 nextToken();
Manuel Klimek79e06082015-05-21 12:23:34 +00001098 }
1099
1100 parseChildBlock();
1101}
1102
Daniel Jasper3c883d12015-05-18 14:49:19 +00001103bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001104 if (FormatTok->BlockKind == BK_Unknown)
Daniel Jasper3c883d12015-05-18 14:49:19 +00001105 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001106 assert(FormatTok->BlockKind != BK_Unknown);
1107 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001108 return false;
1109 parseBracedList();
1110 return true;
1111}
1112
Daniel Jasper015ed022013-09-13 09:20:45 +00001113bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1114 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001115 nextToken();
1116
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001117 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1118 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001119 do {
Manuel Klimek79e06082015-05-21 12:23:34 +00001120 if (Style.Language == FormatStyle::LK_JavaScript) {
1121 if (FormatTok->is(Keywords.kw_function)) {
1122 tryToParseJSFunction();
1123 continue;
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001124 }
1125 if (FormatTok->is(TT_JsFatArrow)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001126 nextToken();
1127 // Fat arrows can be followed by simple expressions or by child blocks
1128 // in curly braces.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001129 if (FormatTok->is(tok::l_brace)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001130 parseChildBlock();
1131 continue;
1132 }
1133 }
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001134 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001135 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001136 case tok::caret:
1137 nextToken();
1138 if (FormatTok->is(tok::l_brace)) {
1139 parseChildBlock();
1140 }
1141 break;
1142 case tok::l_square:
1143 tryToParseLambda();
1144 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001145 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001146 // Assume there are no blocks inside a braced init list apart
1147 // from the ones we explicitly parse out (like lambdas).
1148 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001149 parseBracedList();
1150 break;
Daniel Jaspera87af7a2015-06-30 11:32:22 +00001151 case tok::l_paren:
1152 parseParens();
Daniel Jasperf46dec82015-03-31 14:34:15 +00001153 // JavaScript can just have free standing methods and getters/setters in
1154 // object literals. Detect them by a "{" following ")".
1155 if (Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperf46dec82015-03-31 14:34:15 +00001156 if (FormatTok->is(tok::l_brace))
1157 parseChildBlock();
1158 break;
1159 }
Daniel Jasperf46dec82015-03-31 14:34:15 +00001160 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001161 case tok::r_brace:
1162 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001163 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001164 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001165 HasError = true;
1166 if (!ContinueOnSemicolons)
1167 return !HasError;
1168 nextToken();
1169 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001170 case tok::comma:
1171 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001172 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001173 default:
1174 nextToken();
1175 break;
1176 }
1177 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001178 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001179}
1180
Daniel Jasperf7935112012-12-03 18:12:45 +00001181void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001182 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001183 nextToken();
1184 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001185 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001186 case tok::l_paren:
1187 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001188 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1189 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001190 break;
1191 case tok::r_paren:
1192 nextToken();
1193 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001194 case tok::r_brace:
1195 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1196 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001197 case tok::l_square:
1198 tryToParseLambda();
1199 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001200 case tok::l_brace:
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001201 if (!tryToParseBracedList())
Manuel Klimekf017dc02013-09-04 13:34:14 +00001202 parseChildBlock();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001203 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001204 case tok::at:
1205 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001206 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001207 parseBracedList();
1208 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001209 case tok::identifier:
1210 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001211 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001212 tryToParseJSFunction();
1213 else
1214 nextToken();
1215 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001216 default:
1217 nextToken();
1218 break;
1219 }
1220 } while (!eof());
1221}
1222
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001223void UnwrappedLineParser::parseSquare() {
1224 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1225 if (tryToParseLambda())
1226 return;
1227 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001228 switch (FormatTok->Tok.getKind()) {
1229 case tok::l_paren:
1230 parseParens();
1231 break;
1232 case tok::r_square:
1233 nextToken();
1234 return;
1235 case tok::r_brace:
1236 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1237 return;
1238 case tok::l_square:
1239 parseSquare();
1240 break;
1241 case tok::l_brace: {
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001242 if (!tryToParseBracedList())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001243 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001244 break;
1245 }
1246 case tok::at:
1247 nextToken();
1248 if (FormatTok->Tok.is(tok::l_brace))
1249 parseBracedList();
1250 break;
1251 default:
1252 nextToken();
1253 break;
1254 }
1255 } while (!eof());
1256}
1257
Daniel Jasperf7935112012-12-03 18:12:45 +00001258void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001259 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001260 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001261 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001262 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001263 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001264 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001265 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001266 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001267 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1268 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001269 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001270 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001271 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001272 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001273 } else {
1274 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001275 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001276 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001277 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001278 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001279 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001280 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1281 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001282 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001283 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001284 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001285 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001286 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001287 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001288 parseIfThenElse();
1289 } else {
1290 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001291 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001292 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001293 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001294 }
1295 } else if (NeedsUnwrappedLine) {
1296 addUnwrappedLine();
1297 }
1298}
1299
Daniel Jasper04a71a42014-05-08 11:58:24 +00001300void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001301 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001302 nextToken();
1303 bool NeedsUnwrappedLine = false;
1304 if (FormatTok->is(tok::colon)) {
1305 // We are in a function try block, what comes is an initializer list.
1306 nextToken();
1307 while (FormatTok->is(tok::identifier)) {
1308 nextToken();
1309 if (FormatTok->is(tok::l_paren))
1310 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001311 if (FormatTok->is(tok::comma))
1312 nextToken();
1313 }
1314 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001315 // Parse try with resource.
1316 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1317 parseParens();
1318 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001319 if (FormatTok->is(tok::l_brace)) {
1320 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1321 parseBlock(/*MustBeDeclaration=*/false);
1322 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1323 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1324 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1325 addUnwrappedLine();
1326 } else {
1327 NeedsUnwrappedLine = true;
1328 }
1329 } else if (!FormatTok->is(tok::kw_catch)) {
1330 // The C++ standard requires a compound-statement after a try.
1331 // If there's none, we try to assume there's a structuralElement
1332 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001333 addUnwrappedLine();
1334 ++Line->Level;
1335 parseStructuralElement();
1336 --Line->Level;
1337 }
Nico Weber33381f52015-02-07 01:57:32 +00001338 while (1) {
1339 if (FormatTok->is(tok::at))
1340 nextToken();
1341 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1342 tok::kw___finally) ||
1343 ((Style.Language == FormatStyle::LK_Java ||
1344 Style.Language == FormatStyle::LK_JavaScript) &&
1345 FormatTok->is(Keywords.kw_finally)) ||
1346 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1347 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1348 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001349 nextToken();
1350 while (FormatTok->isNot(tok::l_brace)) {
1351 if (FormatTok->is(tok::l_paren)) {
1352 parseParens();
1353 continue;
1354 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001355 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001356 return;
1357 nextToken();
1358 }
1359 NeedsUnwrappedLine = false;
1360 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1361 parseBlock(/*MustBeDeclaration=*/false);
1362 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1363 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1364 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1365 addUnwrappedLine();
1366 } else {
1367 NeedsUnwrappedLine = true;
1368 }
1369 }
1370 if (NeedsUnwrappedLine) {
1371 addUnwrappedLine();
1372 }
1373}
1374
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001375void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001376 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001377
1378 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001379 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001380 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001381 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001382 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001383 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001384 addUnwrappedLine();
1385
Daniel Jasper65ee3472013-07-31 23:16:02 +00001386 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1387 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1388 DeclarationScopeStack.size() > 1);
1389 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001390 // Munch the semicolon after a namespace. This is more common than one would
1391 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001392 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001393 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001394 addUnwrappedLine();
1395 }
1396 // FIXME: Add error handling.
1397}
1398
Daniel Jasper6acf5132015-03-12 14:44:29 +00001399void UnwrappedLineParser::parseNew() {
1400 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1401 nextToken();
1402 if (Style.Language != FormatStyle::LK_Java)
1403 return;
1404
1405 // In Java, we can parse everything up to the parens, which aren't optional.
1406 do {
1407 // There should not be a ;, { or } before the new's open paren.
1408 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1409 return;
1410
1411 // Consume the parens.
1412 if (FormatTok->is(tok::l_paren)) {
1413 parseParens();
1414
1415 // If there is a class body of an anonymous class, consume that as child.
1416 if (FormatTok->is(tok::l_brace))
1417 parseChildBlock();
1418 return;
1419 }
1420 nextToken();
1421 } while (!eof());
1422}
1423
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001424void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001425 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001426 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001427 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001428 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001429 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001430 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001431 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001432 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001433 addUnwrappedLine();
1434 } else {
1435 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001436 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001437 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001438 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001439 }
1440}
1441
Daniel Jasperf7935112012-12-03 18:12:45 +00001442void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001443 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001444 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001445 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001446 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001447 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001448 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1449 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001450 } else {
1451 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001452 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001453 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001454 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001455 }
1456
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001457 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001458 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001459 addUnwrappedLine();
1460 return;
1461 }
1462
Daniel Jasperf7935112012-12-03 18:12:45 +00001463 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001464 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001465}
1466
1467void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001468 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001469 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001470 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001471 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001472 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001473 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001474 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001475 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001476 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1477 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1478 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001479 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001480 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001481 parseStructuralElement();
1482 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001483 addUnwrappedLine();
1484 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001485 if (FormatTok->is(tok::semi))
1486 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001487 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001488 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001489 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001490}
1491
1492void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001493 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001494 // FIXME: fix handling of complex expressions here.
1495 do {
1496 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001497 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001498 parseLabel();
1499}
1500
1501void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001502 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001503 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001504 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001505 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001506 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001507 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001508 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001509 addUnwrappedLine();
1510 } else {
1511 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001512 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001513 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001514 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001515 }
1516}
1517
1518void UnwrappedLineParser::parseAccessSpecifier() {
1519 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001520 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001521 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001522 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001523 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001524 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001525 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001526 addUnwrappedLine();
1527}
1528
1529void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001530 // Won't be 'enum' for NS_ENUMs.
1531 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001532 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001533
Daniel Jasper2b41a822013-08-20 12:42:50 +00001534 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001535 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1536 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001537
Daniel Jasper786a5502013-09-06 21:32:35 +00001538 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001539 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1540 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001541 nextToken();
1542 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001543 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001544 parseParens();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001545 if (FormatTok->is(tok::identifier)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001546 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001547 // If there are two identifiers in a row, this is likely an elaborate
1548 // return type. In Java, this can be "implements", etc.
1549 if (Style.Language == FormatStyle::LK_Cpp &&
1550 FormatTok->is(tok::identifier))
1551 return;
1552 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001553 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001554
1555 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001556 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001557 return;
1558 FormatTok->BlockKind = BK_Block;
1559
1560 if (Style.Language == FormatStyle::LK_Java) {
1561 // Java enums are different.
1562 parseJavaEnumBody();
1563 return;
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001564 } else if (Style.Language == FormatStyle::LK_Proto) {
1565 parseBlock(/*MustBeDeclaration=*/true);
1566 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001567 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001568
1569 // Parse enum body.
1570 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1571 if (HasError) {
1572 if (FormatTok->is(tok::semi))
1573 nextToken();
1574 addUnwrappedLine();
1575 }
1576
Daniel Jasper90cf3802015-06-17 09:44:02 +00001577 // There is no addUnwrappedLine() here so that we fall through to parsing a
1578 // structural element afterwards. Thus, in "enum A {} n, m;",
Manuel Klimek2cec0192013-01-21 19:17:52 +00001579 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001580}
1581
1582void UnwrappedLineParser::parseJavaEnumBody() {
1583 // Determine whether the enum is simple, i.e. does not have a semicolon or
1584 // constants with class bodies. Simple enums can be formatted like braced
1585 // lists, contracted to a single line, etc.
1586 unsigned StoredPosition = Tokens->getPosition();
1587 bool IsSimple = true;
1588 FormatToken *Tok = Tokens->getNextToken();
1589 while (Tok) {
1590 if (Tok->is(tok::r_brace))
1591 break;
1592 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1593 IsSimple = false;
1594 break;
1595 }
1596 // FIXME: This will also mark enums with braces in the arguments to enum
1597 // constants as "not simple". This is probably fine in practice, though.
1598 Tok = Tokens->getNextToken();
1599 }
1600 FormatTok = Tokens->setPosition(StoredPosition);
1601
1602 if (IsSimple) {
1603 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001604 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001605 return;
1606 }
1607
1608 // Parse the body of a more complex enum.
1609 // First add a line for everything up to the "{".
1610 nextToken();
1611 addUnwrappedLine();
1612 ++Line->Level;
1613
1614 // Parse the enum constants.
1615 while (FormatTok) {
1616 if (FormatTok->is(tok::l_brace)) {
1617 // Parse the constant's class body.
1618 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1619 /*MunchSemi=*/false);
1620 } else if (FormatTok->is(tok::l_paren)) {
1621 parseParens();
1622 } else if (FormatTok->is(tok::comma)) {
1623 nextToken();
1624 addUnwrappedLine();
1625 } else if (FormatTok->is(tok::semi)) {
1626 nextToken();
1627 addUnwrappedLine();
1628 break;
1629 } else if (FormatTok->is(tok::r_brace)) {
1630 addUnwrappedLine();
1631 break;
1632 } else {
1633 nextToken();
1634 }
1635 }
1636
1637 // Parse the class body after the enum's ";" if any.
1638 parseLevel(/*HasOpeningBrace=*/true);
1639 nextToken();
1640 --Line->Level;
1641 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001642}
1643
Manuel Klimeke01bab52013-01-15 13:38:33 +00001644void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001645 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001646 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00001647
Daniel Jasper04785d02015-05-06 14:03:02 +00001648 // The actual identifier can be a nested name specifier, and in macros
1649 // it is often token-pasted.
1650 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
1651 tok::kw___attribute, tok::kw___declspec,
1652 tok::kw_alignas) ||
1653 ((Style.Language == FormatStyle::LK_Java ||
1654 Style.Language == FormatStyle::LK_JavaScript) &&
1655 FormatTok->isOneOf(tok::period, tok::comma))) {
1656 bool IsNonMacroIdentifier =
1657 FormatTok->is(tok::identifier) &&
1658 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00001659 nextToken();
1660 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00001661 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001662 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00001663 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001664
Daniel Jasper04785d02015-05-06 14:03:02 +00001665 // Note that parsing away template declarations here leads to incorrectly
1666 // accepting function declarations as record declarations.
1667 // In general, we cannot solve this problem. Consider:
1668 // class A<int> B() {}
1669 // which can be a function definition or a class definition when B() is a
1670 // macro. If we find enough real-world cases where this is a problem, we
1671 // can parse for the 'template' keyword in the beginning of the statement,
1672 // and thus rule out the record production in case there is no template
1673 // (this would still leave us with an ambiguity between template function
1674 // and class declarations).
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001675 if (FormatTok->isOneOf(tok::colon, tok::less)) {
1676 while (!eof()) {
Daniel Jasper3c883d12015-05-18 14:49:19 +00001677 if (FormatTok->is(tok::l_brace)) {
1678 calculateBraceTypes(/*ExpectClassBody=*/true);
1679 if (!tryToParseBracedList())
1680 break;
1681 }
Daniel Jasper04785d02015-05-06 14:03:02 +00001682 if (FormatTok->Tok.is(tok::semi))
1683 return;
1684 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00001685 }
1686 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001687 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001688 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001689 addUnwrappedLine();
1690
Daniel Jasper240dfda2014-03-31 14:23:49 +00001691 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001692 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001693 }
Daniel Jasper90cf3802015-06-17 09:44:02 +00001694 // There is no addUnwrappedLine() here so that we fall through to parsing a
1695 // structural element afterwards. Thus, in "class A {} n, m;",
1696 // "} n, m;" will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00001697}
1698
Nico Weber8696a8d2013-01-09 21:15:03 +00001699void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001700 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001701 do
1702 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001703 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001704 nextToken(); // Skip '>'.
1705}
1706
1707void UnwrappedLineParser::parseObjCUntilAtEnd() {
1708 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001709 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001710 nextToken();
1711 addUnwrappedLine();
1712 break;
1713 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001714 if (FormatTok->is(tok::l_brace)) {
1715 parseBlock(/*MustBeDeclaration=*/false);
1716 // In ObjC interfaces, nothing should be following the "}".
1717 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001718 } else if (FormatTok->is(tok::r_brace)) {
1719 // Ignore stray "}". parseStructuralElement doesn't consume them.
1720 nextToken();
1721 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001722 } else {
1723 parseStructuralElement();
1724 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001725 } while (!eof());
1726}
1727
Nico Weber2ce0ac52013-01-09 23:25:37 +00001728void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001729 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001730 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001731
1732 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001733 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001734 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001735 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001736 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001737 // Skip category, if present.
1738 parseParens();
1739
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001740 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001741 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001742
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001743 if (FormatTok->Tok.is(tok::l_brace)) {
1744 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1745 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1746 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001747 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001748 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001749
1750 // With instance variables, this puts '}' on its own line. Without instance
1751 // variables, this ends the @interface line.
1752 addUnwrappedLine();
1753
Nico Weber8696a8d2013-01-09 21:15:03 +00001754 parseObjCUntilAtEnd();
1755}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001756
Nico Weber8696a8d2013-01-09 21:15:03 +00001757void UnwrappedLineParser::parseObjCProtocol() {
1758 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001759 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001760
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001761 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001762 parseObjCProtocolList();
1763
1764 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001765 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001766 nextToken();
1767 return addUnwrappedLine();
1768 }
1769
1770 addUnwrappedLine();
1771 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001772}
1773
Daniel Jasperfca735c2015-02-19 16:14:18 +00001774void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1775 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00001776 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00001777
Daniel Jasperec05fc72015-05-11 09:14:50 +00001778 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00001779 if (FormatTok->is(tok::kw_default))
1780 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00001781
1782 // Consume "function" and "default function", so that these get parsed as
1783 // free-standing JS functions, i.e. do not require a trailing semicolon.
Daniel Jasper668c7bb2015-05-11 09:03:10 +00001784 if (FormatTok->is(Keywords.kw_function)) {
1785 nextToken();
1786 return;
1787 }
1788
Daniel Jasper216c9cd2015-06-12 05:08:18 +00001789 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, tok::kw_enum,
Daniel Jasper9f642f72015-09-28 14:28:08 +00001790 Keywords.kw_let, Keywords.kw_var))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001791 return; // Fall through to parsing the corresponding structure.
1792
Daniel Jasper354aa512015-02-19 16:07:32 +00001793 if (FormatTok->is(tok::l_brace)) {
1794 FormatTok->BlockKind = BK_Block;
1795 parseBracedList();
1796 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001797
Daniel Jasper354aa512015-02-19 16:07:32 +00001798 while (!eof() && FormatTok->isNot(tok::semi) &&
1799 FormatTok->isNot(tok::l_brace)) {
1800 nextToken();
1801 }
1802}
1803
Daniel Jasper3b203a62013-09-05 16:05:56 +00001804LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1805 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001806 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1807 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1808 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1809 E = Line.Tokens.end();
1810 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001811 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001812 }
1813 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1814 E = Line.Tokens.end();
1815 I != E; ++I) {
1816 const UnwrappedLineNode &Node = *I;
1817 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1818 I = Node.Children.begin(),
1819 E = Node.Children.end();
1820 I != E; ++I) {
1821 printDebugInfo(*I, "\nChild: ");
1822 }
1823 }
1824 llvm::dbgs() << "\n";
1825}
1826
Daniel Jasperf7935112012-12-03 18:12:45 +00001827void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001828 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001829 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001830 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001831 if (CurrentLines == &Lines)
1832 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001833 });
Benjamin Kramerc7551a42015-05-31 11:18:05 +00001834 CurrentLines->push_back(std::move(*Line));
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001835 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001836 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Benjamin Kramerc7551a42015-05-31 11:18:05 +00001837 CurrentLines->append(
1838 std::make_move_iterator(PreprocessorDirectives.begin()),
1839 std::make_move_iterator(PreprocessorDirectives.end()));
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001840 PreprocessorDirectives.clear();
1841 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001842}
1843
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001844bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001845
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001846bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001847 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1848 FormatTok.NewlinesBefore > 0;
1849}
1850
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001851void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1852 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001853 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001854 I = CommentsBeforeNextToken.begin(),
1855 E = CommentsBeforeNextToken.end();
1856 I != E; ++I) {
Daniel Jaspere60cba12015-05-13 11:35:53 +00001857 if (isOnNewLine(**I) && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001858 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001859 pushToken(*I);
1860 }
Daniel Jaspere60cba12015-05-13 11:35:53 +00001861 if (NewlineBeforeNext && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001862 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001863 CommentsBeforeNextToken.clear();
1864}
1865
Daniel Jasperf7935112012-12-03 18:12:45 +00001866void UnwrappedLineParser::nextToken() {
1867 if (eof())
1868 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001869 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001870 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001871 readToken();
1872}
1873
1874void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001875 bool CommentsInCurrentLine = true;
1876 do {
1877 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001878 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001879 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1880 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001881 // If there is an unfinished unwrapped line, we flush the preprocessor
1882 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001883 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001884 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001885 // Comments stored before the preprocessor directive need to be output
1886 // before the preprocessor directive, at the same level as the
1887 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001888 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001889 parsePPDirective();
1890 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001891 while (FormatTok->Type == TT_ConflictStart ||
1892 FormatTok->Type == TT_ConflictEnd ||
1893 FormatTok->Type == TT_ConflictAlternative) {
1894 if (FormatTok->Type == TT_ConflictStart) {
1895 conditionalCompilationStart(/*Unreachable=*/false);
1896 } else if (FormatTok->Type == TT_ConflictAlternative) {
1897 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001898 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001899 conditionalCompilationEnd();
1900 }
1901 FormatTok = Tokens->getNextToken();
1902 FormatTok->MustBreakBefore = true;
1903 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001904
1905 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1906 !Line->InPPDirective) {
1907 continue;
1908 }
1909
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001910 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001911 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001912 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001913 CommentsInCurrentLine = false;
1914 }
1915 if (CommentsInCurrentLine) {
1916 pushToken(FormatTok);
1917 } else {
1918 CommentsBeforeNextToken.push_back(FormatTok);
1919 }
1920 } while (!eof());
1921}
1922
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001923void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001924 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001925 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001926 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001927 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001928 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001929}
1930
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001931} // end namespace format
1932} // end namespace clang