blob: a1fe9cea3de9eb51bc48ad6652d05ac7a7f07740 [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 Klimek15dfe7a2013-05-28 11:55:06 +000061 FormatToken *&ResetToken, bool &StructuralError)
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),
64 StructuralError(StructuralError),
Craig Topper2145bc02014-05-09 08:15:10 +000065 PreviousStructuralError(StructuralError), Token(nullptr) {
Manuel Klimek1abf7892013-01-04 23:34:14 +000066 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000067 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000068 Line.InPPDirective = true;
69 }
70
Alexander Kornienko34eb2072015-04-11 02:00:23 +000071 ~ScopedMacroState() override {
Manuel Klimek1abf7892013-01-04 23:34:14 +000072 TokenSource = PreviousTokenSource;
73 ResetToken = Token;
74 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000075 Line.Level = PreviousLineLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +000076 StructuralError = PreviousStructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +000077 }
78
Craig Topperfb6b25b2014-03-15 04:29:04 +000079 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +000080 // The \c UnwrappedLineParser guards against this by never calling
81 // \c getNextToken() after it has encountered the first eof token.
82 assert(!eof());
Manuel Klimek1abf7892013-01-04 23:34:14 +000083 Token = PreviousTokenSource->getNextToken();
84 if (eof())
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000085 return getFakeEOF();
Manuel Klimek1abf7892013-01-04 23:34:14 +000086 return Token;
87 }
88
Craig Topperfb6b25b2014-03-15 04:29:04 +000089 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +000090
Craig Topperfb6b25b2014-03-15 04:29:04 +000091 FormatToken *setPosition(unsigned Position) override {
Manuel Klimekab419912013-05-23 09:41:43 +000092 Token = PreviousTokenSource->setPosition(Position);
93 return Token;
94 }
95
Manuel Klimek1abf7892013-01-04 23:34:14 +000096private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000097 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimek1abf7892013-01-04 23:34:14 +000098
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000099 FormatToken *getFakeEOF() {
100 static bool EOFInitialized = false;
101 static FormatToken FormatTok;
102 if (!EOFInitialized) {
103 FormatTok.Tok.startToken();
104 FormatTok.Tok.setKind(tok::eof);
105 EOFInitialized = true;
106 }
107 return &FormatTok;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000108 }
109
110 UnwrappedLine &Line;
111 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000112 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000113 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000114 FormatTokenSource *PreviousTokenSource;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000115 bool &StructuralError;
116 bool PreviousStructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000117
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000118 FormatToken *Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000119};
120
Craig Topper69665e12013-07-01 04:21:54 +0000121} // end anonymous namespace
122
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000123class ScopedLineState {
124public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000125 ScopedLineState(UnwrappedLineParser &Parser,
126 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000127 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000128 if (SwitchToPreprocessorLines)
129 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000130 else if (!Parser.Line->Tokens.empty())
131 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000132 PreBlockLine = std::move(Parser.Line);
133 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000134 Parser.Line->Level = PreBlockLine->Level;
135 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000136 }
137
138 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000139 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000140 Parser.addUnwrappedLine();
141 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000142 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000143 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000144 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
145 Parser.MustBreakBeforeNextToken = true;
146 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000147 }
148
149private:
150 UnwrappedLineParser &Parser;
151
David Blaikieefb6eb22014-08-09 20:02:07 +0000152 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000153 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000154};
155
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000156class CompoundStatementIndenter {
157public:
158 CompoundStatementIndenter(UnwrappedLineParser *Parser,
159 const FormatStyle &Style, unsigned &LineLevel)
160 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
161 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
162 Parser->addUnwrappedLine();
163 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
164 Parser->addUnwrappedLine();
165 ++LineLevel;
166 }
167 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000168 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000169
170private:
171 unsigned &LineLevel;
172 unsigned OldLineLevel;
173};
174
Craig Topper69665e12013-07-01 04:21:54 +0000175namespace {
176
Manuel Klimekab419912013-05-23 09:41:43 +0000177class IndexedTokenSource : public FormatTokenSource {
178public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000179 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000180 : Tokens(Tokens), Position(-1) {}
181
Craig Topperfb6b25b2014-03-15 04:29:04 +0000182 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000183 ++Position;
184 return Tokens[Position];
185 }
186
Craig Topperfb6b25b2014-03-15 04:29:04 +0000187 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000188 assert(Position >= 0);
189 return Position;
190 }
191
Craig Topperfb6b25b2014-03-15 04:29:04 +0000192 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000193 Position = P;
194 return Tokens[Position];
195 }
196
Manuel Klimek71814b42013-10-11 21:25:45 +0000197 void reset() { Position = -1; }
198
Manuel Klimekab419912013-05-23 09:41:43 +0000199private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000200 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000201 int Position;
202};
203
Craig Topper69665e12013-07-01 04:21:54 +0000204} // end anonymous namespace
205
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000206UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000207 const AdditionalKeywords &Keywords,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000208 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000209 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000210 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
211 CurrentLines(&Lines), StructuralError(false), Style(Style),
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000212 Keywords(Keywords), Tokens(nullptr), Callback(Callback),
213 AllTokens(Tokens), PPBranchLevel(-1) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000214
215void UnwrappedLineParser::reset() {
216 PPBranchLevel = -1;
217 Line.reset(new UnwrappedLine);
218 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000219 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000220 MustBreakBeforeNextToken = false;
221 PreprocessorDirectives.clear();
222 CurrentLines = &Lines;
223 DeclarationScopeStack.clear();
224 StructuralError = false;
225 PPStack.clear();
226}
Daniel Jasperf7935112012-12-03 18:12:45 +0000227
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000228bool UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000229 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimek71814b42013-10-11 21:25:45 +0000230 do {
231 DEBUG(llvm::dbgs() << "----\n");
232 reset();
233 Tokens = &TokenSource;
234 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000235
Manuel Klimek71814b42013-10-11 21:25:45 +0000236 readToken();
237 parseFile();
238 // Create line with eof token.
239 pushToken(FormatTok);
240 addUnwrappedLine();
241
242 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
243 E = Lines.end();
244 I != E; ++I) {
245 Callback.consumeUnwrappedLine(*I);
246 }
247 Callback.finishRun();
248 Lines.clear();
249 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000250 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000251 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
252 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
253 }
254 if (!PPLevelBranchIndex.empty()) {
255 ++PPLevelBranchIndex.back();
256 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
257 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
258 }
259 } while (!PPLevelBranchIndex.empty());
260
Manuel Klimek1a18c402013-04-12 14:13:36 +0000261 return StructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000262}
263
Manuel Klimek1a18c402013-04-12 14:13:36 +0000264void UnwrappedLineParser::parseFile() {
Daniel Jasper9326f912015-05-05 08:40:32 +0000265 // The top-level context in a file always has declarations, except for pre-
266 // processor directives and JavaScript files.
267 bool MustBeDeclaration =
268 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
269 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
270 MustBeDeclaration);
Nico Weber9096fc02013-06-26 00:30:14 +0000271 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000272 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000273 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000274 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000275}
276
Manuel Klimek1a18c402013-04-12 14:13:36 +0000277void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000278 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000279 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000280 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000281 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000282 nextToken();
283 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000284 break;
285 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000286 // FIXME: Add parameter whether this can happen - if this happens, we must
287 // be in a non-declaration context.
Nico Weber9096fc02013-06-26 00:30:14 +0000288 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000289 addUnwrappedLine();
290 break;
291 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000292 if (HasOpeningBrace)
293 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000294 StructuralError = true;
295 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
Manuel Klimekab419912013-05-23 09:41:43 +0000313void UnwrappedLineParser::calculateBraceTypes() {
314 // 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 Jasperb1f74a82013-07-09 09:06:29 +0000336 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000337 break;
338 case tok::r_brace:
339 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000340 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Daniel Jasper220c0d12014-04-10 07:27:12 +0000341 bool ProbablyBracedList = false;
342 if (Style.Language == FormatStyle::LK_Proto) {
343 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
344 } else {
Daniel Jasper91b032a2014-05-22 12:46:38 +0000345 // Using OriginalColumn to distinguish between ObjC methods and
346 // binary operators is a bit hacky.
347 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
348 NextTok->OriginalColumn == 0;
349
Daniel Jasper220c0d12014-04-10 07:27:12 +0000350 // If there is a comma, semicolon or right paren after the closing
351 // brace, we assume this is a braced initializer list. Note that
352 // regardless how we mark inner braces here, we will overwrite the
353 // BlockKind later if we parse a braced list (where all blocks
354 // inside are by default braced lists), or when we explicitly detect
355 // blocks (for example while parsing lambdas).
356 //
357 // We exclude + and - as they can be ObjC visibility modifiers.
358 ProbablyBracedList =
359 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
Daniel Jasper438059e2014-05-22 12:11:13 +0000360 tok::r_paren, tok::r_square, tok::l_brace,
Daniel Jasper65df5aa2014-08-04 14:51:02 +0000361 tok::l_paren, tok::ellipsis) ||
Daniel Jasper91b032a2014-05-22 12:46:38 +0000362 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000363 }
364 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000365 Tok->BlockKind = BK_BracedInit;
366 LBraceStack.back()->BlockKind = BK_BracedInit;
367 } else {
368 Tok->BlockKind = BK_Block;
369 LBraceStack.back()->BlockKind = BK_Block;
370 }
Manuel Klimekab419912013-05-23 09:41:43 +0000371 }
372 LBraceStack.pop_back();
373 }
374 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000375 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000376 case tok::semi:
377 case tok::kw_if:
378 case tok::kw_while:
379 case tok::kw_for:
380 case tok::kw_switch:
381 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000382 case tok::kw___try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000383 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000384 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000385 break;
386 default:
387 break;
388 }
389 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000390 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000391 // Assume other blocks for all unclosed opening braces.
392 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000393 if (LBraceStack[i]->BlockKind == BK_Unknown)
394 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000395 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000396
Manuel Klimekab419912013-05-23 09:41:43 +0000397 FormatTok = Tokens->setPosition(StoredPosition);
398}
399
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000400void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
401 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000402 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000403 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000404 nextToken();
405
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000406 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000407
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000408 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
409 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000410 if (AddLevel)
411 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000412 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000413
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000414 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000415 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000416 StructuralError = true;
417 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000418 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000419
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000420 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000421 if (MunchSemi && FormatTok->Tok.is(tok::semi))
422 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000423 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000424}
425
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000426static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000427 // FIXME: Closure-library specific stuff should not be hard-coded but be
428 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000429 if (Line.Tokens.size() < 4)
430 return false;
431 auto I = Line.Tokens.begin();
432 if (I->Tok->TokenText != "goog")
433 return false;
434 ++I;
435 if (I->Tok->isNot(tok::period))
436 return false;
437 ++I;
438 if (I->Tok->TokenText != "scope")
439 return false;
440 ++I;
441 return I->Tok->is(tok::l_paren);
442}
443
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000444static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
445 const FormatToken &InitialToken) {
446 switch (Style.BreakBeforeBraces) {
447 case FormatStyle::BS_Linux:
448 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
449 case FormatStyle::BS_Allman:
450 case FormatStyle::BS_GNU:
451 return true;
452 default:
453 return false;
454 }
455}
456
Manuel Klimek516e0542013-09-04 13:25:30 +0000457void UnwrappedLineParser::parseChildBlock() {
458 FormatTok->BlockKind = BK_Block;
459 nextToken();
460 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000461 bool GoogScope =
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000462 Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000463 ScopedLineState LineState(*this);
464 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
465 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000466 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000467 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000468 flushComments(isOnNewLine(*FormatTok));
Daniel Jasper4a39c842014-05-06 13:54:10 +0000469 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000470 }
471 nextToken();
472}
473
Daniel Jasperf7935112012-12-03 18:12:45 +0000474void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000475 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000476 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000477 nextToken();
478
Craig Topper2145bc02014-05-09 08:15:10 +0000479 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000480 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000481 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000482 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000483
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000484 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000485 case tok::pp_define:
486 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000487 return;
488 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000489 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000490 break;
491 case tok::pp_ifdef:
492 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000493 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000494 break;
495 case tok::pp_else:
496 parsePPElse();
497 break;
498 case tok::pp_elif:
499 parsePPElIf();
500 break;
501 case tok::pp_endif:
502 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000503 break;
504 default:
505 parsePPUnknown();
506 break;
507 }
508}
509
Manuel Klimek68b03042014-04-14 09:14:11 +0000510void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
511 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000512 PPStack.push_back(PP_Unreachable);
513 else
514 PPStack.push_back(PP_Conditional);
515}
516
Manuel Klimek68b03042014-04-14 09:14:11 +0000517void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000518 ++PPBranchLevel;
519 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
520 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
521 PPLevelBranchIndex.push_back(0);
522 PPLevelBranchCount.push_back(0);
523 }
524 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000525 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
526 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000527}
528
Manuel Klimek68b03042014-04-14 09:14:11 +0000529void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000530 if (!PPStack.empty())
531 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000532 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
533 if (!PPChainBranchIndex.empty())
534 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000535 conditionalCompilationCondition(
536 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
537 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000538}
539
Manuel Klimek68b03042014-04-14 09:14:11 +0000540void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000541 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
542 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
543 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000544 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
545 }
546 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000547 // Guard against #endif's without #if.
548 if (PPBranchLevel > 0)
549 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000550 if (!PPChainBranchIndex.empty())
551 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000552 if (!PPStack.empty())
553 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000554}
555
556void UnwrappedLineParser::parsePPIf(bool IfDef) {
557 nextToken();
558 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Daniel Jasper9d22bcc2015-01-19 10:51:05 +0000559 FormatTok->Tok.getLiteralData() != nullptr &&
Manuel Klimek68b03042014-04-14 09:14:11 +0000560 StringRef(FormatTok->Tok.getLiteralData(),
561 FormatTok->Tok.getLength()) == "0") ||
562 FormatTok->Tok.is(tok::kw_false);
563 conditionalCompilationStart(!IfDef && IsLiteralFalse);
564 parsePPUnknown();
565}
566
567void UnwrappedLineParser::parsePPElse() {
568 conditionalCompilationAlternative();
569 parsePPUnknown();
570}
571
572void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
573
574void UnwrappedLineParser::parsePPEndIf() {
575 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000576 parsePPUnknown();
577}
578
Manuel Klimek1abf7892013-01-04 23:34:14 +0000579void UnwrappedLineParser::parsePPDefine() {
580 nextToken();
581
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000582 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000583 parsePPUnknown();
584 return;
585 }
586 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000587 if (FormatTok->Tok.getKind() == tok::l_paren &&
588 FormatTok->WhitespaceRange.getBegin() ==
589 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000590 parseParens();
591 }
592 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000593 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000594
595 // Errors during a preprocessor directive can only affect the layout of the
596 // preprocessor directive, and thus we ignore them. An alternative approach
597 // would be to use the same approach we use on the file level (no
598 // re-indentation if there was a structural error) within the macro
599 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000600 parseFile();
601}
602
603void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000604 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000605 nextToken();
606 } while (!eof());
607 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000608}
609
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000610// Here we blacklist certain tokens that are not usually the first token in an
611// unwrapped line. This is used in attempt to distinguish macro calls without
612// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000613static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000614 // Semicolon can be a null-statement, l_square can be a start of a macro or
615 // a C++11 attribute, but this doesn't seem to be common.
616 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
617 Tok.isNot(tok::l_square) &&
618 // Tokens that can only be used as binary operators and a part of
619 // overloaded operator names.
620 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
621 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
622 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
623 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
624 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
625 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
626 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
627 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
628 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
629 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
630 Tok.isNot(tok::lesslessequal) &&
631 // Colon is used in labels, base class lists, initializer lists,
632 // range-based for loops, ternary operator, but should never be the
633 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000634 Tok.isNot(tok::colon) &&
635 // 'noexcept' is a trailing annotation.
636 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000637}
638
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000639void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000640 assert(!FormatTok->Tok.is(tok::l_brace));
641 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000642 case tok::at:
643 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000644 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000645 parseBracedList();
646 break;
647 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000648 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000649 case tok::objc_public:
650 case tok::objc_protected:
651 case tok::objc_package:
652 case tok::objc_private:
653 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000654 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000655 case tok::objc_implementation:
656 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000657 case tok::objc_protocol:
658 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000659 case tok::objc_end:
660 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000661 case tok::objc_optional:
662 case tok::objc_required:
663 nextToken();
664 addUnwrappedLine();
665 return;
Nico Weber33381f52015-02-07 01:57:32 +0000666 case tok::objc_try:
667 // This branch isn't strictly necessary (the kw_try case below would
668 // do this too after the tok::at is parsed above). But be explicit.
669 parseTryCatch();
670 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000671 default:
672 break;
673 }
674 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000675 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000676 nextToken();
677 if (FormatTok->is(tok::l_brace)) {
Daniel Jasper2337f282015-01-12 10:14:56 +0000678 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000679 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000680 if (FormatTok->is(tok::r_brace)) {
681 nextToken();
682 break;
683 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000684 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000685 nextToken();
686 }
687 }
688 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000689 case tok::kw_namespace:
690 parseNamespace();
691 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000692 case tok::kw_inline:
693 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000694 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000695 parseNamespace();
696 return;
697 }
698 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000699 case tok::kw_public:
700 case tok::kw_protected:
701 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +0000702 if (Style.Language == FormatStyle::LK_Java ||
703 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000704 nextToken();
705 else
706 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000707 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000708 case tok::kw_if:
709 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000710 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000711 case tok::kw_for:
712 case tok::kw_while:
713 parseForOrWhileLoop();
714 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000715 case tok::kw_do:
716 parseDoWhile();
717 return;
718 case tok::kw_switch:
719 parseSwitch();
720 return;
721 case tok::kw_default:
722 nextToken();
723 parseLabel();
724 return;
725 case tok::kw_case:
726 parseCaseLabel();
727 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000728 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000729 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +0000730 parseTryCatch();
731 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000732 case tok::kw_extern:
733 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000734 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000735 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000736 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000737 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000738 addUnwrappedLine();
739 return;
740 }
741 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000742 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +0000743 case tok::kw_export:
744 if (Style.Language == FormatStyle::LK_JavaScript) {
745 parseJavaScriptEs6ImportExport();
746 return;
747 }
748 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000749 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +0000750 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +0000751 parseForOrWhileLoop();
752 return;
753 }
Daniel Jasper354aa512015-02-19 16:07:32 +0000754 if (Style.Language == FormatStyle::LK_JavaScript &&
755 FormatTok->is(Keywords.kw_import)) {
Daniel Jasperfca735c2015-02-19 16:14:18 +0000756 parseJavaScriptEs6ImportExport();
Daniel Jasper354aa512015-02-19 16:07:32 +0000757 return;
758 }
Daniel Jasper53395402015-04-07 15:04:40 +0000759 if (FormatTok->is(Keywords.kw_signals)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +0000760 nextToken();
761 if (FormatTok->is(tok::colon)) {
762 nextToken();
763 addUnwrappedLine();
764 }
Daniel Jasper53395402015-04-07 15:04:40 +0000765 return;
766 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000767 // In all other cases, parse the declaration.
768 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000769 default:
770 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000771 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000772 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000773 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000774 case tok::at:
775 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000776 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000777 parseBracedList();
778 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000779 case tok::kw_enum:
780 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000781 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000782 case tok::kw_typedef:
783 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000784 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
785 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000786 parseEnum();
787 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000788 case tok::kw_struct:
789 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000790 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000791 parseRecord();
792 // A record declaration or definition is always the start of a structural
793 // element.
794 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000795 case tok::period:
796 nextToken();
797 // In Java, classes have an implicit static member "class".
798 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
799 FormatTok->is(tok::kw_class))
800 nextToken();
801 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000802 case tok::semi:
803 nextToken();
804 addUnwrappedLine();
805 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000806 case tok::r_brace:
807 addUnwrappedLine();
808 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000809 case tok::l_paren:
810 parseParens();
811 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000812 case tok::caret:
813 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000814 if (FormatTok->Tok.isAnyIdentifier() ||
815 FormatTok->isSimpleTypeSpecifier())
816 nextToken();
817 if (FormatTok->is(tok::l_paren))
818 parseParens();
819 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000820 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000821 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000822 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000823 if (!tryToParseBracedList()) {
824 // A block outside of parentheses must be the last part of a
825 // structural element.
826 // FIXME: Figure out cases where this is not true, and add projections
827 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000828 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000829 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000830 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000831 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000832 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000833 return;
834 }
835 // Otherwise this was a braced init list, and the structural
836 // element continues.
837 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000838 case tok::kw_try:
839 // We arrive here when parsing function-try blocks.
840 parseTryCatch();
841 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000842 case tok::identifier: {
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000843 // Parse function literal unless 'function' is the first token in a line
844 // in which case this should be treated as a free-standing function.
Daniel Jasper9326f912015-05-05 08:40:32 +0000845 if (Style.Language == FormatStyle::LK_JavaScript &&
846 FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000847 tryToParseJSFunction();
848 break;
849 }
Daniel Jasper9326f912015-05-05 08:40:32 +0000850 if ((Style.Language == FormatStyle::LK_JavaScript ||
851 Style.Language == FormatStyle::LK_Java) &&
852 FormatTok->is(Keywords.kw_interface)) {
853 parseRecord();
854 break;
855 }
856
857 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +0000858 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +0000859 if (Line->Tokens.size() == 1 &&
860 // JS doesn't have macros, and within classes colons indicate fields,
861 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +0000862 Style.Language != FormatStyle::LK_JavaScript) {
863 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000864 parseLabel();
865 return;
866 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000867 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +0000868 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +0000869 bool FunctionLike = FormatTok->is(tok::l_paren);
870 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000871 parseParens();
Daniel Jasper680b09b2014-11-05 10:48:04 +0000872 if (FormatTok->NewlinesBefore > 0 &&
873 (Text.size() >= 5 || FunctionLike) &&
874 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000875 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000876 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000877 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000878 }
879 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000880 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000881 case tok::equal:
882 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000883 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000884 parseBracedList();
885 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000886 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000887 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000888 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000889 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +0000890 case tok::kw_new:
891 parseNew();
892 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000893 default:
894 nextToken();
895 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000896 }
897 } while (!eof());
898}
899
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000900bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000901 // FIXME: This is a dirty way to access the previous token. Find a better
902 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000903 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000904 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
905 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000906 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000907 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000908 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000909 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000910 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000911 assert(FormatTok->is(tok::l_square));
912 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000913 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000914 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000915
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000916 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000917 if (FormatTok->isSimpleTypeSpecifier()) {
918 nextToken();
919 continue;
920 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000921 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000922 case tok::l_brace:
923 break;
924 case tok::l_paren:
925 parseParens();
926 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000927 case tok::amp:
928 case tok::star:
929 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000930 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000931 case tok::less:
932 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000933 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000934 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000935 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000936 nextToken();
937 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000938 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000939 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000940 nextToken();
941 break;
942 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000943 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000944 }
945 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000946 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000947 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000948 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000949}
950
951bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
952 nextToken();
953 if (FormatTok->is(tok::equal)) {
954 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000955 if (FormatTok->is(tok::r_square)) {
956 nextToken();
957 return true;
958 }
959 if (FormatTok->isNot(tok::comma))
960 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000961 nextToken();
962 } else if (FormatTok->is(tok::amp)) {
963 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000964 if (FormatTok->is(tok::r_square)) {
965 nextToken();
966 return true;
967 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000968 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
969 return false;
970 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000971 if (FormatTok->is(tok::comma))
972 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000973 } else if (FormatTok->is(tok::r_square)) {
974 nextToken();
975 return true;
976 }
977 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000978 if (FormatTok->is(tok::amp))
979 nextToken();
980 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
981 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000982 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000983 if (FormatTok->is(tok::ellipsis))
984 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000985 if (FormatTok->is(tok::comma)) {
986 nextToken();
987 } else if (FormatTok->is(tok::r_square)) {
988 nextToken();
989 return true;
990 } else {
991 return false;
992 }
993 } while (!eof());
994 return false;
995}
996
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000997void UnwrappedLineParser::tryToParseJSFunction() {
998 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000999
1000 // Consume function name.
1001 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001002 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001003
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001004 if (FormatTok->isNot(tok::l_paren))
1005 return;
1006 nextToken();
1007 while (FormatTok->isNot(tok::l_brace)) {
1008 // Err on the side of caution in order to avoid consuming the full file in
1009 // case of incomplete code.
1010 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
1011 tok::comment))
1012 return;
1013 nextToken();
1014 }
1015 parseChildBlock();
1016}
1017
Manuel Klimekab419912013-05-23 09:41:43 +00001018bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001019 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +00001020 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001021 assert(FormatTok->BlockKind != BK_Unknown);
1022 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001023 return false;
1024 parseBracedList();
1025 return true;
1026}
1027
Daniel Jasper015ed022013-09-13 09:20:45 +00001028bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1029 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001030 nextToken();
1031
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001032 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1033 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001034 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001035 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001036 FormatTok->is(Keywords.kw_function)) {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001037 tryToParseJSFunction();
1038 continue;
1039 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001040 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001041 case tok::caret:
1042 nextToken();
1043 if (FormatTok->is(tok::l_brace)) {
1044 parseChildBlock();
1045 }
1046 break;
1047 case tok::l_square:
1048 tryToParseLambda();
1049 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001050 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001051 // Assume there are no blocks inside a braced init list apart
1052 // from the ones we explicitly parse out (like lambdas).
1053 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001054 parseBracedList();
1055 break;
Daniel Jasperf46dec82015-03-31 14:34:15 +00001056 case tok::r_paren:
1057 // JavaScript can just have free standing methods and getters/setters in
1058 // object literals. Detect them by a "{" following ")".
1059 if (Style.Language == FormatStyle::LK_JavaScript) {
1060 nextToken();
1061 if (FormatTok->is(tok::l_brace))
1062 parseChildBlock();
1063 break;
1064 }
1065 nextToken();
1066 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001067 case tok::r_brace:
1068 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001069 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001070 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001071 HasError = true;
1072 if (!ContinueOnSemicolons)
1073 return !HasError;
1074 nextToken();
1075 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001076 case tok::comma:
1077 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001078 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001079 default:
1080 nextToken();
1081 break;
1082 }
1083 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001084 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001085}
1086
Daniel Jasperf7935112012-12-03 18:12:45 +00001087void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001088 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001089 nextToken();
1090 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001091 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001092 case tok::l_paren:
1093 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001094 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1095 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001096 break;
1097 case tok::r_paren:
1098 nextToken();
1099 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001100 case tok::r_brace:
1101 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1102 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001103 case tok::l_square:
1104 tryToParseLambda();
1105 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001106 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001107 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001108 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001109 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001110 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001111 case tok::at:
1112 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001113 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001114 parseBracedList();
1115 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001116 case tok::identifier:
1117 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001118 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001119 tryToParseJSFunction();
1120 else
1121 nextToken();
1122 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001123 default:
1124 nextToken();
1125 break;
1126 }
1127 } while (!eof());
1128}
1129
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001130void UnwrappedLineParser::parseSquare() {
1131 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1132 if (tryToParseLambda())
1133 return;
1134 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001135 switch (FormatTok->Tok.getKind()) {
1136 case tok::l_paren:
1137 parseParens();
1138 break;
1139 case tok::r_square:
1140 nextToken();
1141 return;
1142 case tok::r_brace:
1143 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1144 return;
1145 case tok::l_square:
1146 parseSquare();
1147 break;
1148 case tok::l_brace: {
1149 if (!tryToParseBracedList()) {
1150 parseChildBlock();
1151 }
1152 break;
1153 }
1154 case tok::at:
1155 nextToken();
1156 if (FormatTok->Tok.is(tok::l_brace))
1157 parseBracedList();
1158 break;
1159 default:
1160 nextToken();
1161 break;
1162 }
1163 } while (!eof());
1164}
1165
Daniel Jasperf7935112012-12-03 18:12:45 +00001166void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001167 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001168 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001169 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001170 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001171 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001172 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001173 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001174 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001175 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1176 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001177 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001178 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001179 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001180 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001181 } else {
1182 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001183 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001184 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001185 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001186 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001187 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001188 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1189 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001190 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001191 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001192 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001193 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001194 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001195 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001196 parseIfThenElse();
1197 } else {
1198 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001199 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001200 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001201 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001202 }
1203 } else if (NeedsUnwrappedLine) {
1204 addUnwrappedLine();
1205 }
1206}
1207
Daniel Jasper04a71a42014-05-08 11:58:24 +00001208void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001209 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001210 nextToken();
1211 bool NeedsUnwrappedLine = false;
1212 if (FormatTok->is(tok::colon)) {
1213 // We are in a function try block, what comes is an initializer list.
1214 nextToken();
1215 while (FormatTok->is(tok::identifier)) {
1216 nextToken();
1217 if (FormatTok->is(tok::l_paren))
1218 parseParens();
1219 else
1220 StructuralError = true;
1221 if (FormatTok->is(tok::comma))
1222 nextToken();
1223 }
1224 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001225 // Parse try with resource.
1226 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1227 parseParens();
1228 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001229 if (FormatTok->is(tok::l_brace)) {
1230 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1231 parseBlock(/*MustBeDeclaration=*/false);
1232 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1233 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1234 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1235 addUnwrappedLine();
1236 } else {
1237 NeedsUnwrappedLine = true;
1238 }
1239 } else if (!FormatTok->is(tok::kw_catch)) {
1240 // The C++ standard requires a compound-statement after a try.
1241 // If there's none, we try to assume there's a structuralElement
1242 // and try to continue.
1243 StructuralError = true;
1244 addUnwrappedLine();
1245 ++Line->Level;
1246 parseStructuralElement();
1247 --Line->Level;
1248 }
Nico Weber33381f52015-02-07 01:57:32 +00001249 while (1) {
1250 if (FormatTok->is(tok::at))
1251 nextToken();
1252 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1253 tok::kw___finally) ||
1254 ((Style.Language == FormatStyle::LK_Java ||
1255 Style.Language == FormatStyle::LK_JavaScript) &&
1256 FormatTok->is(Keywords.kw_finally)) ||
1257 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1258 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1259 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001260 nextToken();
1261 while (FormatTok->isNot(tok::l_brace)) {
1262 if (FormatTok->is(tok::l_paren)) {
1263 parseParens();
1264 continue;
1265 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001266 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001267 return;
1268 nextToken();
1269 }
1270 NeedsUnwrappedLine = false;
1271 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1272 parseBlock(/*MustBeDeclaration=*/false);
1273 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1274 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1275 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1276 addUnwrappedLine();
1277 } else {
1278 NeedsUnwrappedLine = true;
1279 }
1280 }
1281 if (NeedsUnwrappedLine) {
1282 addUnwrappedLine();
1283 }
1284}
1285
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001286void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001287 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001288
1289 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001290 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001291 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001292 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001293 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001294 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001295 addUnwrappedLine();
1296
Daniel Jasper65ee3472013-07-31 23:16:02 +00001297 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1298 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1299 DeclarationScopeStack.size() > 1);
1300 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001301 // Munch the semicolon after a namespace. This is more common than one would
1302 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001303 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001304 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001305 addUnwrappedLine();
1306 }
1307 // FIXME: Add error handling.
1308}
1309
Daniel Jasper6acf5132015-03-12 14:44:29 +00001310void UnwrappedLineParser::parseNew() {
1311 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1312 nextToken();
1313 if (Style.Language != FormatStyle::LK_Java)
1314 return;
1315
1316 // In Java, we can parse everything up to the parens, which aren't optional.
1317 do {
1318 // There should not be a ;, { or } before the new's open paren.
1319 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1320 return;
1321
1322 // Consume the parens.
1323 if (FormatTok->is(tok::l_paren)) {
1324 parseParens();
1325
1326 // If there is a class body of an anonymous class, consume that as child.
1327 if (FormatTok->is(tok::l_brace))
1328 parseChildBlock();
1329 return;
1330 }
1331 nextToken();
1332 } while (!eof());
1333}
1334
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001335void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001336 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001337 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001338 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001339 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001340 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001341 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001342 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001343 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001344 addUnwrappedLine();
1345 } else {
1346 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001347 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001348 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001349 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001350 }
1351}
1352
Daniel Jasperf7935112012-12-03 18:12:45 +00001353void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001354 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001355 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001356 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001357 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001358 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001359 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1360 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001361 } else {
1362 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001363 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001364 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001365 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001366 }
1367
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001368 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001369 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001370 addUnwrappedLine();
1371 return;
1372 }
1373
Daniel Jasperf7935112012-12-03 18:12:45 +00001374 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001375 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001376}
1377
1378void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001379 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001380 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001381 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001382 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001383 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001384 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001385 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001386 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001387 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1388 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1389 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001390 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001391 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001392 parseStructuralElement();
1393 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001394 addUnwrappedLine();
1395 } else {
1396 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001397 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001398 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001399}
1400
1401void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001402 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001403 // FIXME: fix handling of complex expressions here.
1404 do {
1405 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001406 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001407 parseLabel();
1408}
1409
1410void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001411 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001412 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001413 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001414 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001415 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001416 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001417 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001418 addUnwrappedLine();
1419 } else {
1420 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001421 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001422 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001423 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001424 }
1425}
1426
1427void UnwrappedLineParser::parseAccessSpecifier() {
1428 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001429 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001430 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001431 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001432 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001433 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001434 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001435 addUnwrappedLine();
1436}
1437
1438void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001439 // Won't be 'enum' for NS_ENUMs.
1440 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001441 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001442
Daniel Jasper2b41a822013-08-20 12:42:50 +00001443 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001444 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1445 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001446 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001447 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1448 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001449 nextToken();
1450 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001451 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001452 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001453 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001454 nextToken();
1455 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001456
1457 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001458 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001459 return;
1460 FormatTok->BlockKind = BK_Block;
1461
1462 if (Style.Language == FormatStyle::LK_Java) {
1463 // Java enums are different.
1464 parseJavaEnumBody();
1465 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001466 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001467
1468 // Parse enum body.
1469 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1470 if (HasError) {
1471 if (FormatTok->is(tok::semi))
1472 nextToken();
1473 addUnwrappedLine();
1474 }
1475
Manuel Klimek2cec0192013-01-21 19:17:52 +00001476 // We fall through to parsing a structural element afterwards, so that in
1477 // enum A {} n, m;
1478 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001479}
1480
1481void UnwrappedLineParser::parseJavaEnumBody() {
1482 // Determine whether the enum is simple, i.e. does not have a semicolon or
1483 // constants with class bodies. Simple enums can be formatted like braced
1484 // lists, contracted to a single line, etc.
1485 unsigned StoredPosition = Tokens->getPosition();
1486 bool IsSimple = true;
1487 FormatToken *Tok = Tokens->getNextToken();
1488 while (Tok) {
1489 if (Tok->is(tok::r_brace))
1490 break;
1491 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1492 IsSimple = false;
1493 break;
1494 }
1495 // FIXME: This will also mark enums with braces in the arguments to enum
1496 // constants as "not simple". This is probably fine in practice, though.
1497 Tok = Tokens->getNextToken();
1498 }
1499 FormatTok = Tokens->setPosition(StoredPosition);
1500
1501 if (IsSimple) {
1502 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001503 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001504 return;
1505 }
1506
1507 // Parse the body of a more complex enum.
1508 // First add a line for everything up to the "{".
1509 nextToken();
1510 addUnwrappedLine();
1511 ++Line->Level;
1512
1513 // Parse the enum constants.
1514 while (FormatTok) {
1515 if (FormatTok->is(tok::l_brace)) {
1516 // Parse the constant's class body.
1517 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1518 /*MunchSemi=*/false);
1519 } else if (FormatTok->is(tok::l_paren)) {
1520 parseParens();
1521 } else if (FormatTok->is(tok::comma)) {
1522 nextToken();
1523 addUnwrappedLine();
1524 } else if (FormatTok->is(tok::semi)) {
1525 nextToken();
1526 addUnwrappedLine();
1527 break;
1528 } else if (FormatTok->is(tok::r_brace)) {
1529 addUnwrappedLine();
1530 break;
1531 } else {
1532 nextToken();
1533 }
1534 }
1535
1536 // Parse the class body after the enum's ";" if any.
1537 parseLevel(/*HasOpeningBrace=*/true);
1538 nextToken();
1539 --Line->Level;
1540 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001541}
1542
Manuel Klimeke01bab52013-01-15 13:38:33 +00001543void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001544 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001545 nextToken();
Manuel Klimek45bf56c2014-07-31 07:19:30 +00001546 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1547 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001548 nextToken();
1549 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001550 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001551 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001552 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001553 // The actual identifier can be a nested name specifier, and in macros
1554 // it is often token-pasted.
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001555 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1556 FormatTok->is(tok::hashhash) ||
Daniel Jasper6a5d38d2015-04-13 14:56:54 +00001557 ((Style.Language == FormatStyle::LK_Java ||
1558 Style.Language == FormatStyle::LK_JavaScript) &&
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001559 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001560 nextToken();
1561
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001562 // Note that parsing away template declarations here leads to incorrectly
1563 // accepting function declarations as record declarations.
1564 // In general, we cannot solve this problem. Consider:
1565 // class A<int> B() {}
1566 // which can be a function definition or a class definition when B() is a
1567 // macro. If we find enough real-world cases where this is a problem, we
1568 // can parse for the 'template' keyword in the beginning of the statement,
1569 // and thus rule out the record production in case there is no template
1570 // (this would still leave us with an ambiguity between template function
1571 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001572 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1573 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1574 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001575 return;
1576 nextToken();
1577 }
1578 }
1579 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001580 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001581 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001582 addUnwrappedLine();
1583
Daniel Jasper240dfda2014-03-31 14:23:49 +00001584 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001585 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001586 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001587 // We fall through to parsing a structural element afterwards, so
1588 // class A {} n, m;
1589 // will end up in one unwrapped line.
Daniel Jasper9326f912015-05-05 08:40:32 +00001590 // This does not apply for Java and JavaScript.
Daniel Jasper6fa9ec72015-02-19 16:03:16 +00001591 if (Style.Language == FormatStyle::LK_Java ||
1592 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001593 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001594}
1595
Nico Weber8696a8d2013-01-09 21:15:03 +00001596void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001597 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001598 do
1599 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001600 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001601 nextToken(); // Skip '>'.
1602}
1603
1604void UnwrappedLineParser::parseObjCUntilAtEnd() {
1605 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001606 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001607 nextToken();
1608 addUnwrappedLine();
1609 break;
1610 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001611 if (FormatTok->is(tok::l_brace)) {
1612 parseBlock(/*MustBeDeclaration=*/false);
1613 // In ObjC interfaces, nothing should be following the "}".
1614 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001615 } else if (FormatTok->is(tok::r_brace)) {
1616 // Ignore stray "}". parseStructuralElement doesn't consume them.
1617 nextToken();
1618 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001619 } else {
1620 parseStructuralElement();
1621 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001622 } while (!eof());
1623}
1624
Nico Weber2ce0ac52013-01-09 23:25:37 +00001625void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001626 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001627 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001628
1629 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001630 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001631 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001632 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001633 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001634 // Skip category, if present.
1635 parseParens();
1636
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001637 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001638 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001639
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001640 if (FormatTok->Tok.is(tok::l_brace)) {
1641 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1642 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1643 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001644 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001645 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001646
1647 // With instance variables, this puts '}' on its own line. Without instance
1648 // variables, this ends the @interface line.
1649 addUnwrappedLine();
1650
Nico Weber8696a8d2013-01-09 21:15:03 +00001651 parseObjCUntilAtEnd();
1652}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001653
Nico Weber8696a8d2013-01-09 21:15:03 +00001654void UnwrappedLineParser::parseObjCProtocol() {
1655 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001656 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001657
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001658 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001659 parseObjCProtocolList();
1660
1661 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001662 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001663 nextToken();
1664 return addUnwrappedLine();
1665 }
1666
1667 addUnwrappedLine();
1668 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001669}
1670
Daniel Jasperfca735c2015-02-19 16:14:18 +00001671void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1672 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00001673 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00001674
1675 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_function,
1676 Keywords.kw_var))
1677 return; // Fall through to parsing the corresponding structure.
1678
1679 if (FormatTok->is(tok::kw_default)) {
1680 nextToken(); // export default ..., fall through after eating 'default'.
1681 return;
1682 }
1683
Daniel Jasper354aa512015-02-19 16:07:32 +00001684 if (FormatTok->is(tok::l_brace)) {
1685 FormatTok->BlockKind = BK_Block;
1686 parseBracedList();
1687 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001688
Daniel Jasper354aa512015-02-19 16:07:32 +00001689 while (!eof() && FormatTok->isNot(tok::semi) &&
1690 FormatTok->isNot(tok::l_brace)) {
1691 nextToken();
1692 }
1693}
1694
Daniel Jasper3b203a62013-09-05 16:05:56 +00001695LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1696 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001697 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1698 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1699 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1700 E = Line.Tokens.end();
1701 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001702 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001703 }
1704 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1705 E = Line.Tokens.end();
1706 I != E; ++I) {
1707 const UnwrappedLineNode &Node = *I;
1708 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1709 I = Node.Children.begin(),
1710 E = Node.Children.end();
1711 I != E; ++I) {
1712 printDebugInfo(*I, "\nChild: ");
1713 }
1714 }
1715 llvm::dbgs() << "\n";
1716}
1717
Daniel Jasperf7935112012-12-03 18:12:45 +00001718void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001719 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001720 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001721 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001722 if (CurrentLines == &Lines)
1723 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001724 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001725 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001726 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001727 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001728 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001729 I = PreprocessorDirectives.begin(),
1730 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001731 I != E; ++I) {
1732 CurrentLines->push_back(*I);
1733 }
1734 PreprocessorDirectives.clear();
1735 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001736}
1737
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001738bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001739
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001740bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001741 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1742 FormatTok.NewlinesBefore > 0;
1743}
1744
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001745void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1746 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001747 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001748 I = CommentsBeforeNextToken.begin(),
1749 E = CommentsBeforeNextToken.end();
1750 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001751 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001752 addUnwrappedLine();
1753 }
1754 pushToken(*I);
1755 }
1756 if (NewlineBeforeNext && JustComments) {
1757 addUnwrappedLine();
1758 }
1759 CommentsBeforeNextToken.clear();
1760}
1761
Daniel Jasperf7935112012-12-03 18:12:45 +00001762void UnwrappedLineParser::nextToken() {
1763 if (eof())
1764 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001765 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001766 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001767 readToken();
1768}
1769
1770void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001771 bool CommentsInCurrentLine = true;
1772 do {
1773 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001774 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001775 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1776 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001777 // If there is an unfinished unwrapped line, we flush the preprocessor
1778 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001779 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001780 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001781 // Comments stored before the preprocessor directive need to be output
1782 // before the preprocessor directive, at the same level as the
1783 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001784 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001785 parsePPDirective();
1786 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001787 while (FormatTok->Type == TT_ConflictStart ||
1788 FormatTok->Type == TT_ConflictEnd ||
1789 FormatTok->Type == TT_ConflictAlternative) {
1790 if (FormatTok->Type == TT_ConflictStart) {
1791 conditionalCompilationStart(/*Unreachable=*/false);
1792 } else if (FormatTok->Type == TT_ConflictAlternative) {
1793 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001794 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001795 conditionalCompilationEnd();
1796 }
1797 FormatTok = Tokens->getNextToken();
1798 FormatTok->MustBreakBefore = true;
1799 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001800
1801 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1802 !Line->InPPDirective) {
1803 continue;
1804 }
1805
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001806 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001807 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001808 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001809 CommentsInCurrentLine = false;
1810 }
1811 if (CommentsInCurrentLine) {
1812 pushToken(FormatTok);
1813 } else {
1814 CommentsBeforeNextToken.push_back(FormatTok);
1815 }
1816 } while (!eof());
1817}
1818
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001819void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001820 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001821 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001822 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001823 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001824 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001825}
1826
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001827} // end namespace format
1828} // end namespace clang