blob: 2aa4414d67ac2bd4098f5701f1f402dd83a6826b [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 Jasperdd9276e2013-03-22 16:55:40 +0000265 ScopedDeclarationState DeclarationState(
266 *Line, DeclarationScopeStack,
Daniel Jasperfca735c2015-02-19 16:14:18 +0000267 /*MustBeDeclaration=*/!Line->InPPDirective);
Nico Weber9096fc02013-06-26 00:30:14 +0000268 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000269 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000270 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000271 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000272}
273
Manuel Klimek1a18c402013-04-12 14:13:36 +0000274void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000275 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000276 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000277 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000278 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000279 nextToken();
280 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000281 break;
282 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000283 // FIXME: Add parameter whether this can happen - if this happens, we must
284 // be in a non-declaration context.
Nico Weber9096fc02013-06-26 00:30:14 +0000285 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000286 addUnwrappedLine();
287 break;
288 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000289 if (HasOpeningBrace)
290 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000291 StructuralError = true;
292 nextToken();
293 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000294 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000295 case tok::kw_default:
296 case tok::kw_case:
Daniel Jasper72407622013-09-02 08:26:29 +0000297 if (!SwitchLabelEncountered &&
298 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
299 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000300 SwitchLabelEncountered = true;
301 parseStructuralElement();
302 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000303 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000304 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000305 break;
306 }
307 } while (!eof());
308}
309
Manuel Klimekab419912013-05-23 09:41:43 +0000310void UnwrappedLineParser::calculateBraceTypes() {
311 // We'll parse forward through the tokens until we hit
312 // a closing brace or eof - note that getNextToken() will
313 // parse macros, so this will magically work inside macro
314 // definitions, too.
315 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000316 FormatToken *Tok = FormatTok;
Manuel Klimekab419912013-05-23 09:41:43 +0000317 // Keep a stack of positions of lbrace tokens. We will
318 // update information about whether an lbrace starts a
319 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000320 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000321 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000322 do {
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000323 // Get next none-comment token.
324 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000325 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000326 do {
327 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000328 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000329 } while (NextTok->is(tok::comment));
330
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000331 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000332 case tok::l_brace:
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000333 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000334 break;
335 case tok::r_brace:
336 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000337 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Daniel Jasper220c0d12014-04-10 07:27:12 +0000338 bool ProbablyBracedList = false;
339 if (Style.Language == FormatStyle::LK_Proto) {
340 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
341 } else {
Daniel Jasper91b032a2014-05-22 12:46:38 +0000342 // Using OriginalColumn to distinguish between ObjC methods and
343 // binary operators is a bit hacky.
344 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
345 NextTok->OriginalColumn == 0;
346
Daniel Jasper220c0d12014-04-10 07:27:12 +0000347 // If there is a comma, semicolon or right paren after the closing
348 // brace, we assume this is a braced initializer list. Note that
349 // regardless how we mark inner braces here, we will overwrite the
350 // BlockKind later if we parse a braced list (where all blocks
351 // inside are by default braced lists), or when we explicitly detect
352 // blocks (for example while parsing lambdas).
353 //
354 // We exclude + and - as they can be ObjC visibility modifiers.
355 ProbablyBracedList =
356 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
Daniel Jasper438059e2014-05-22 12:11:13 +0000357 tok::r_paren, tok::r_square, tok::l_brace,
Daniel Jasper65df5aa2014-08-04 14:51:02 +0000358 tok::l_paren, tok::ellipsis) ||
Daniel Jasper91b032a2014-05-22 12:46:38 +0000359 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000360 }
361 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000362 Tok->BlockKind = BK_BracedInit;
363 LBraceStack.back()->BlockKind = BK_BracedInit;
364 } else {
365 Tok->BlockKind = BK_Block;
366 LBraceStack.back()->BlockKind = BK_Block;
367 }
Manuel Klimekab419912013-05-23 09:41:43 +0000368 }
369 LBraceStack.pop_back();
370 }
371 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000372 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000373 case tok::semi:
374 case tok::kw_if:
375 case tok::kw_while:
376 case tok::kw_for:
377 case tok::kw_switch:
378 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000379 case tok::kw___try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000380 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000381 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000382 break;
383 default:
384 break;
385 }
386 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000387 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000388 // Assume other blocks for all unclosed opening braces.
389 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000390 if (LBraceStack[i]->BlockKind == BK_Unknown)
391 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000392 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000393
Manuel Klimekab419912013-05-23 09:41:43 +0000394 FormatTok = Tokens->setPosition(StoredPosition);
395}
396
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000397void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
398 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000399 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000400 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000401 nextToken();
402
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000403 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000404
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000405 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
406 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000407 if (AddLevel)
408 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000409 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000410
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000411 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000412 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000413 StructuralError = true;
414 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000415 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000416
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000417 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000418 if (MunchSemi && FormatTok->Tok.is(tok::semi))
419 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000420 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000421}
422
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000423static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000424 // FIXME: Closure-library specific stuff should not be hard-coded but be
425 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000426 if (Line.Tokens.size() < 4)
427 return false;
428 auto I = Line.Tokens.begin();
429 if (I->Tok->TokenText != "goog")
430 return false;
431 ++I;
432 if (I->Tok->isNot(tok::period))
433 return false;
434 ++I;
435 if (I->Tok->TokenText != "scope")
436 return false;
437 ++I;
438 return I->Tok->is(tok::l_paren);
439}
440
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000441static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
442 const FormatToken &InitialToken) {
443 switch (Style.BreakBeforeBraces) {
444 case FormatStyle::BS_Linux:
445 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
446 case FormatStyle::BS_Allman:
447 case FormatStyle::BS_GNU:
448 return true;
449 default:
450 return false;
451 }
452}
453
Manuel Klimek516e0542013-09-04 13:25:30 +0000454void UnwrappedLineParser::parseChildBlock() {
455 FormatTok->BlockKind = BK_Block;
456 nextToken();
457 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000458 bool GoogScope =
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000459 Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000460 ScopedLineState LineState(*this);
461 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
462 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000463 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000464 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000465 flushComments(isOnNewLine(*FormatTok));
Daniel Jasper4a39c842014-05-06 13:54:10 +0000466 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000467 }
468 nextToken();
469}
470
Daniel Jasperf7935112012-12-03 18:12:45 +0000471void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000472 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000473 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000474 nextToken();
475
Craig Topper2145bc02014-05-09 08:15:10 +0000476 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000477 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000478 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000479 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000480
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000481 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000482 case tok::pp_define:
483 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000484 return;
485 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000486 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000487 break;
488 case tok::pp_ifdef:
489 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000490 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000491 break;
492 case tok::pp_else:
493 parsePPElse();
494 break;
495 case tok::pp_elif:
496 parsePPElIf();
497 break;
498 case tok::pp_endif:
499 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000500 break;
501 default:
502 parsePPUnknown();
503 break;
504 }
505}
506
Manuel Klimek68b03042014-04-14 09:14:11 +0000507void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
508 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000509 PPStack.push_back(PP_Unreachable);
510 else
511 PPStack.push_back(PP_Conditional);
512}
513
Manuel Klimek68b03042014-04-14 09:14:11 +0000514void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000515 ++PPBranchLevel;
516 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
517 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
518 PPLevelBranchIndex.push_back(0);
519 PPLevelBranchCount.push_back(0);
520 }
521 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000522 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
523 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000524}
525
Manuel Klimek68b03042014-04-14 09:14:11 +0000526void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000527 if (!PPStack.empty())
528 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000529 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
530 if (!PPChainBranchIndex.empty())
531 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000532 conditionalCompilationCondition(
533 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
534 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000535}
536
Manuel Klimek68b03042014-04-14 09:14:11 +0000537void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000538 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
539 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
540 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000541 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
542 }
543 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000544 // Guard against #endif's without #if.
545 if (PPBranchLevel > 0)
546 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000547 if (!PPChainBranchIndex.empty())
548 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000549 if (!PPStack.empty())
550 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000551}
552
553void UnwrappedLineParser::parsePPIf(bool IfDef) {
554 nextToken();
555 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Daniel Jasper9d22bcc2015-01-19 10:51:05 +0000556 FormatTok->Tok.getLiteralData() != nullptr &&
Manuel Klimek68b03042014-04-14 09:14:11 +0000557 StringRef(FormatTok->Tok.getLiteralData(),
558 FormatTok->Tok.getLength()) == "0") ||
559 FormatTok->Tok.is(tok::kw_false);
560 conditionalCompilationStart(!IfDef && IsLiteralFalse);
561 parsePPUnknown();
562}
563
564void UnwrappedLineParser::parsePPElse() {
565 conditionalCompilationAlternative();
566 parsePPUnknown();
567}
568
569void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
570
571void UnwrappedLineParser::parsePPEndIf() {
572 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000573 parsePPUnknown();
574}
575
Manuel Klimek1abf7892013-01-04 23:34:14 +0000576void UnwrappedLineParser::parsePPDefine() {
577 nextToken();
578
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000579 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000580 parsePPUnknown();
581 return;
582 }
583 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000584 if (FormatTok->Tok.getKind() == tok::l_paren &&
585 FormatTok->WhitespaceRange.getBegin() ==
586 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000587 parseParens();
588 }
589 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000590 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000591
592 // Errors during a preprocessor directive can only affect the layout of the
593 // preprocessor directive, and thus we ignore them. An alternative approach
594 // would be to use the same approach we use on the file level (no
595 // re-indentation if there was a structural error) within the macro
596 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000597 parseFile();
598}
599
600void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000601 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000602 nextToken();
603 } while (!eof());
604 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000605}
606
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000607// Here we blacklist certain tokens that are not usually the first token in an
608// unwrapped line. This is used in attempt to distinguish macro calls without
609// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000610static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000611 // Semicolon can be a null-statement, l_square can be a start of a macro or
612 // a C++11 attribute, but this doesn't seem to be common.
613 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
614 Tok.isNot(tok::l_square) &&
615 // Tokens that can only be used as binary operators and a part of
616 // overloaded operator names.
617 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
618 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
619 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
620 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
621 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
622 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
623 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
624 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
625 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
626 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
627 Tok.isNot(tok::lesslessequal) &&
628 // Colon is used in labels, base class lists, initializer lists,
629 // range-based for loops, ternary operator, but should never be the
630 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000631 Tok.isNot(tok::colon) &&
632 // 'noexcept' is a trailing annotation.
633 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000634}
635
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000636void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000637 assert(!FormatTok->Tok.is(tok::l_brace));
638 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000639 case tok::at:
640 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000641 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000642 parseBracedList();
643 break;
644 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000645 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000646 case tok::objc_public:
647 case tok::objc_protected:
648 case tok::objc_package:
649 case tok::objc_private:
650 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000651 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000652 case tok::objc_implementation:
653 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000654 case tok::objc_protocol:
655 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000656 case tok::objc_end:
657 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000658 case tok::objc_optional:
659 case tok::objc_required:
660 nextToken();
661 addUnwrappedLine();
662 return;
Nico Weber33381f52015-02-07 01:57:32 +0000663 case tok::objc_try:
664 // This branch isn't strictly necessary (the kw_try case below would
665 // do this too after the tok::at is parsed above). But be explicit.
666 parseTryCatch();
667 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000668 default:
669 break;
670 }
671 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000672 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000673 nextToken();
674 if (FormatTok->is(tok::l_brace)) {
Daniel Jasper2337f282015-01-12 10:14:56 +0000675 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000676 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000677 if (FormatTok->is(tok::r_brace)) {
678 nextToken();
679 break;
680 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000681 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000682 nextToken();
683 }
684 }
685 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000686 case tok::kw_namespace:
687 parseNamespace();
688 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000689 case tok::kw_inline:
690 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000691 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000692 parseNamespace();
693 return;
694 }
695 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000696 case tok::kw_public:
697 case tok::kw_protected:
698 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +0000699 if (Style.Language == FormatStyle::LK_Java ||
700 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000701 nextToken();
702 else
703 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000704 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000705 case tok::kw_if:
706 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000707 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000708 case tok::kw_for:
709 case tok::kw_while:
710 parseForOrWhileLoop();
711 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000712 case tok::kw_do:
713 parseDoWhile();
714 return;
715 case tok::kw_switch:
716 parseSwitch();
717 return;
718 case tok::kw_default:
719 nextToken();
720 parseLabel();
721 return;
722 case tok::kw_case:
723 parseCaseLabel();
724 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000725 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000726 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +0000727 parseTryCatch();
728 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000729 case tok::kw_extern:
730 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000731 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000732 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000733 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000734 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000735 addUnwrappedLine();
736 return;
737 }
738 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000739 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +0000740 case tok::kw_export:
741 if (Style.Language == FormatStyle::LK_JavaScript) {
742 parseJavaScriptEs6ImportExport();
743 return;
744 }
745 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000746 case tok::identifier:
747 if (FormatTok->IsForEachMacro) {
748 parseForOrWhileLoop();
749 return;
750 }
Daniel Jasper354aa512015-02-19 16:07:32 +0000751 if (Style.Language == FormatStyle::LK_JavaScript &&
752 FormatTok->is(Keywords.kw_import)) {
Daniel Jasperfca735c2015-02-19 16:14:18 +0000753 parseJavaScriptEs6ImportExport();
Daniel Jasper354aa512015-02-19 16:07:32 +0000754 return;
755 }
Daniel Jasper53395402015-04-07 15:04:40 +0000756 if (FormatTok->is(Keywords.kw_signals)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +0000757 nextToken();
758 if (FormatTok->is(tok::colon)) {
759 nextToken();
760 addUnwrappedLine();
761 }
Daniel Jasper53395402015-04-07 15:04:40 +0000762 return;
763 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000764 // In all other cases, parse the declaration.
765 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000766 default:
767 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000768 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000769 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000770 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000771 case tok::at:
772 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000773 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000774 parseBracedList();
775 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000776 case tok::kw_enum:
777 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000778 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000779 case tok::kw_typedef:
780 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000781 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
782 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000783 parseEnum();
784 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000785 case tok::kw_struct:
786 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000787 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000788 parseRecord();
789 // A record declaration or definition is always the start of a structural
790 // element.
791 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000792 case tok::period:
793 nextToken();
794 // In Java, classes have an implicit static member "class".
795 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
796 FormatTok->is(tok::kw_class))
797 nextToken();
798 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000799 case tok::semi:
800 nextToken();
801 addUnwrappedLine();
802 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000803 case tok::r_brace:
804 addUnwrappedLine();
805 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000806 case tok::l_paren:
807 parseParens();
808 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000809 case tok::caret:
810 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000811 if (FormatTok->Tok.isAnyIdentifier() ||
812 FormatTok->isSimpleTypeSpecifier())
813 nextToken();
814 if (FormatTok->is(tok::l_paren))
815 parseParens();
816 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000817 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000818 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000819 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000820 if (!tryToParseBracedList()) {
821 // A block outside of parentheses must be the last part of a
822 // structural element.
823 // FIXME: Figure out cases where this is not true, and add projections
824 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000825 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000826 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000827 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000828 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000829 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000830 return;
831 }
832 // Otherwise this was a braced init list, and the structural
833 // element continues.
834 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000835 case tok::kw_try:
836 // We arrive here when parsing function-try blocks.
837 parseTryCatch();
838 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000839 case tok::identifier: {
840 StringRef Text = FormatTok->TokenText;
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000841 // Parse function literal unless 'function' is the first token in a line
842 // in which case this should be treated as a free-standing function.
843 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
844 Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000845 tryToParseJSFunction();
846 break;
847 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000848 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +0000849 if (Line->Tokens.size() == 1 &&
850 // JS doesn't have macros, and within classes colons indicate fields,
851 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +0000852 Style.Language != FormatStyle::LK_JavaScript) {
853 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000854 parseLabel();
855 return;
856 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000857 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +0000858 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +0000859 bool FunctionLike = FormatTok->is(tok::l_paren);
860 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000861 parseParens();
Daniel Jasper680b09b2014-11-05 10:48:04 +0000862 if (FormatTok->NewlinesBefore > 0 &&
863 (Text.size() >= 5 || FunctionLike) &&
864 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000865 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000866 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000867 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000868 }
869 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000870 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000871 case tok::equal:
872 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000873 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000874 parseBracedList();
875 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000876 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000877 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000878 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000879 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +0000880 case tok::kw_new:
881 parseNew();
882 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000883 default:
884 nextToken();
885 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000886 }
887 } while (!eof());
888}
889
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000890bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000891 // FIXME: This is a dirty way to access the previous token. Find a better
892 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000893 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000894 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
895 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000896 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000897 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000898 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000899 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000900 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000901 assert(FormatTok->is(tok::l_square));
902 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000903 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000904 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000905
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000906 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000907 if (FormatTok->isSimpleTypeSpecifier()) {
908 nextToken();
909 continue;
910 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000911 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000912 case tok::l_brace:
913 break;
914 case tok::l_paren:
915 parseParens();
916 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000917 case tok::amp:
918 case tok::star:
919 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000920 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000921 case tok::less:
922 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000923 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000924 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000925 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000926 nextToken();
927 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000928 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000929 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000930 nextToken();
931 break;
932 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000933 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000934 }
935 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000936 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000937 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000938 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000939}
940
941bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
942 nextToken();
943 if (FormatTok->is(tok::equal)) {
944 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000945 if (FormatTok->is(tok::r_square)) {
946 nextToken();
947 return true;
948 }
949 if (FormatTok->isNot(tok::comma))
950 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000951 nextToken();
952 } else if (FormatTok->is(tok::amp)) {
953 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000954 if (FormatTok->is(tok::r_square)) {
955 nextToken();
956 return true;
957 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000958 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
959 return false;
960 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000961 if (FormatTok->is(tok::comma))
962 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000963 } else if (FormatTok->is(tok::r_square)) {
964 nextToken();
965 return true;
966 }
967 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000968 if (FormatTok->is(tok::amp))
969 nextToken();
970 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
971 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000972 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000973 if (FormatTok->is(tok::ellipsis))
974 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000975 if (FormatTok->is(tok::comma)) {
976 nextToken();
977 } else if (FormatTok->is(tok::r_square)) {
978 nextToken();
979 return true;
980 } else {
981 return false;
982 }
983 } while (!eof());
984 return false;
985}
986
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000987void UnwrappedLineParser::tryToParseJSFunction() {
988 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000989
990 // Consume function name.
991 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +0000992 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000993
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000994 if (FormatTok->isNot(tok::l_paren))
995 return;
996 nextToken();
997 while (FormatTok->isNot(tok::l_brace)) {
998 // Err on the side of caution in order to avoid consuming the full file in
999 // case of incomplete code.
1000 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
1001 tok::comment))
1002 return;
1003 nextToken();
1004 }
1005 parseChildBlock();
1006}
1007
Manuel Klimekab419912013-05-23 09:41:43 +00001008bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001009 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +00001010 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001011 assert(FormatTok->BlockKind != BK_Unknown);
1012 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001013 return false;
1014 parseBracedList();
1015 return true;
1016}
1017
Daniel Jasper015ed022013-09-13 09:20:45 +00001018bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1019 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001020 nextToken();
1021
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001022 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1023 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001024 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001025 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001026 FormatTok->is(Keywords.kw_function)) {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001027 tryToParseJSFunction();
1028 continue;
1029 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001030 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001031 case tok::caret:
1032 nextToken();
1033 if (FormatTok->is(tok::l_brace)) {
1034 parseChildBlock();
1035 }
1036 break;
1037 case tok::l_square:
1038 tryToParseLambda();
1039 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001040 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001041 // Assume there are no blocks inside a braced init list apart
1042 // from the ones we explicitly parse out (like lambdas).
1043 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001044 parseBracedList();
1045 break;
Daniel Jasperf46dec82015-03-31 14:34:15 +00001046 case tok::r_paren:
1047 // JavaScript can just have free standing methods and getters/setters in
1048 // object literals. Detect them by a "{" following ")".
1049 if (Style.Language == FormatStyle::LK_JavaScript) {
1050 nextToken();
1051 if (FormatTok->is(tok::l_brace))
1052 parseChildBlock();
1053 break;
1054 }
1055 nextToken();
1056 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001057 case tok::r_brace:
1058 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001059 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001060 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001061 HasError = true;
1062 if (!ContinueOnSemicolons)
1063 return !HasError;
1064 nextToken();
1065 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001066 case tok::comma:
1067 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001068 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001069 default:
1070 nextToken();
1071 break;
1072 }
1073 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001074 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001075}
1076
Daniel Jasperf7935112012-12-03 18:12:45 +00001077void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001078 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001079 nextToken();
1080 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001081 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001082 case tok::l_paren:
1083 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001084 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1085 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001086 break;
1087 case tok::r_paren:
1088 nextToken();
1089 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001090 case tok::r_brace:
1091 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1092 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001093 case tok::l_square:
1094 tryToParseLambda();
1095 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001096 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001097 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001098 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001099 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001100 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001101 case tok::at:
1102 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001103 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001104 parseBracedList();
1105 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001106 case tok::identifier:
1107 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001108 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001109 tryToParseJSFunction();
1110 else
1111 nextToken();
1112 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001113 default:
1114 nextToken();
1115 break;
1116 }
1117 } while (!eof());
1118}
1119
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001120void UnwrappedLineParser::parseSquare() {
1121 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1122 if (tryToParseLambda())
1123 return;
1124 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001125 switch (FormatTok->Tok.getKind()) {
1126 case tok::l_paren:
1127 parseParens();
1128 break;
1129 case tok::r_square:
1130 nextToken();
1131 return;
1132 case tok::r_brace:
1133 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1134 return;
1135 case tok::l_square:
1136 parseSquare();
1137 break;
1138 case tok::l_brace: {
1139 if (!tryToParseBracedList()) {
1140 parseChildBlock();
1141 }
1142 break;
1143 }
1144 case tok::at:
1145 nextToken();
1146 if (FormatTok->Tok.is(tok::l_brace))
1147 parseBracedList();
1148 break;
1149 default:
1150 nextToken();
1151 break;
1152 }
1153 } while (!eof());
1154}
1155
Daniel Jasperf7935112012-12-03 18:12:45 +00001156void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001157 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001158 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001159 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001160 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001161 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001162 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001163 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001164 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001165 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1166 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001167 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001168 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001169 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001170 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001171 } else {
1172 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001173 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001174 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001175 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001176 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001177 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001178 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1179 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001180 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001181 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001182 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001183 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001184 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001185 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001186 parseIfThenElse();
1187 } else {
1188 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001189 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001190 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001191 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001192 }
1193 } else if (NeedsUnwrappedLine) {
1194 addUnwrappedLine();
1195 }
1196}
1197
Daniel Jasper04a71a42014-05-08 11:58:24 +00001198void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001199 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001200 nextToken();
1201 bool NeedsUnwrappedLine = false;
1202 if (FormatTok->is(tok::colon)) {
1203 // We are in a function try block, what comes is an initializer list.
1204 nextToken();
1205 while (FormatTok->is(tok::identifier)) {
1206 nextToken();
1207 if (FormatTok->is(tok::l_paren))
1208 parseParens();
1209 else
1210 StructuralError = true;
1211 if (FormatTok->is(tok::comma))
1212 nextToken();
1213 }
1214 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001215 // Parse try with resource.
1216 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1217 parseParens();
1218 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001219 if (FormatTok->is(tok::l_brace)) {
1220 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1221 parseBlock(/*MustBeDeclaration=*/false);
1222 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1223 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1224 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1225 addUnwrappedLine();
1226 } else {
1227 NeedsUnwrappedLine = true;
1228 }
1229 } else if (!FormatTok->is(tok::kw_catch)) {
1230 // The C++ standard requires a compound-statement after a try.
1231 // If there's none, we try to assume there's a structuralElement
1232 // and try to continue.
1233 StructuralError = true;
1234 addUnwrappedLine();
1235 ++Line->Level;
1236 parseStructuralElement();
1237 --Line->Level;
1238 }
Nico Weber33381f52015-02-07 01:57:32 +00001239 while (1) {
1240 if (FormatTok->is(tok::at))
1241 nextToken();
1242 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1243 tok::kw___finally) ||
1244 ((Style.Language == FormatStyle::LK_Java ||
1245 Style.Language == FormatStyle::LK_JavaScript) &&
1246 FormatTok->is(Keywords.kw_finally)) ||
1247 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1248 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1249 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001250 nextToken();
1251 while (FormatTok->isNot(tok::l_brace)) {
1252 if (FormatTok->is(tok::l_paren)) {
1253 parseParens();
1254 continue;
1255 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001256 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001257 return;
1258 nextToken();
1259 }
1260 NeedsUnwrappedLine = false;
1261 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1262 parseBlock(/*MustBeDeclaration=*/false);
1263 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1264 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1265 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1266 addUnwrappedLine();
1267 } else {
1268 NeedsUnwrappedLine = true;
1269 }
1270 }
1271 if (NeedsUnwrappedLine) {
1272 addUnwrappedLine();
1273 }
1274}
1275
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001276void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001277 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001278
1279 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001280 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001281 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001282 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001283 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001284 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001285 addUnwrappedLine();
1286
Daniel Jasper65ee3472013-07-31 23:16:02 +00001287 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1288 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1289 DeclarationScopeStack.size() > 1);
1290 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001291 // Munch the semicolon after a namespace. This is more common than one would
1292 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001293 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001294 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001295 addUnwrappedLine();
1296 }
1297 // FIXME: Add error handling.
1298}
1299
Daniel Jasper6acf5132015-03-12 14:44:29 +00001300void UnwrappedLineParser::parseNew() {
1301 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1302 nextToken();
1303 if (Style.Language != FormatStyle::LK_Java)
1304 return;
1305
1306 // In Java, we can parse everything up to the parens, which aren't optional.
1307 do {
1308 // There should not be a ;, { or } before the new's open paren.
1309 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1310 return;
1311
1312 // Consume the parens.
1313 if (FormatTok->is(tok::l_paren)) {
1314 parseParens();
1315
1316 // If there is a class body of an anonymous class, consume that as child.
1317 if (FormatTok->is(tok::l_brace))
1318 parseChildBlock();
1319 return;
1320 }
1321 nextToken();
1322 } while (!eof());
1323}
1324
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001325void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001326 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1327 FormatTok->IsForEachMacro) &&
1328 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001329 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001330 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001331 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001332 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001333 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001334 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001335 addUnwrappedLine();
1336 } else {
1337 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001338 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001339 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001340 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001341 }
1342}
1343
Daniel Jasperf7935112012-12-03 18:12:45 +00001344void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001345 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001346 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001347 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001348 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001349 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001350 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1351 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001352 } else {
1353 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001354 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001355 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001356 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001357 }
1358
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001359 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001360 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001361 addUnwrappedLine();
1362 return;
1363 }
1364
Daniel Jasperf7935112012-12-03 18:12:45 +00001365 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001366 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001367}
1368
1369void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001370 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001371 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001372 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001373 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001374 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001375 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001376 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001377 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001378 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1379 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1380 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001381 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001382 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001383 parseStructuralElement();
1384 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001385 addUnwrappedLine();
1386 } else {
1387 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001388 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001389 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001390}
1391
1392void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001393 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001394 // FIXME: fix handling of complex expressions here.
1395 do {
1396 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001397 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001398 parseLabel();
1399}
1400
1401void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001402 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001403 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001404 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001405 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001406 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001407 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001408 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001409 addUnwrappedLine();
1410 } else {
1411 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001412 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001413 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001414 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001415 }
1416}
1417
1418void UnwrappedLineParser::parseAccessSpecifier() {
1419 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001420 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001421 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001422 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001423 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001424 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001425 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001426 addUnwrappedLine();
1427}
1428
1429void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001430 // Won't be 'enum' for NS_ENUMs.
1431 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001432 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001433
Daniel Jasper2b41a822013-08-20 12:42:50 +00001434 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001435 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1436 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001437 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001438 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1439 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001440 nextToken();
1441 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001442 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001443 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001444 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001445 nextToken();
1446 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001447
1448 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001449 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001450 return;
1451 FormatTok->BlockKind = BK_Block;
1452
1453 if (Style.Language == FormatStyle::LK_Java) {
1454 // Java enums are different.
1455 parseJavaEnumBody();
1456 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001457 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001458
1459 // Parse enum body.
1460 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1461 if (HasError) {
1462 if (FormatTok->is(tok::semi))
1463 nextToken();
1464 addUnwrappedLine();
1465 }
1466
Manuel Klimek2cec0192013-01-21 19:17:52 +00001467 // We fall through to parsing a structural element afterwards, so that in
1468 // enum A {} n, m;
1469 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001470}
1471
1472void UnwrappedLineParser::parseJavaEnumBody() {
1473 // Determine whether the enum is simple, i.e. does not have a semicolon or
1474 // constants with class bodies. Simple enums can be formatted like braced
1475 // lists, contracted to a single line, etc.
1476 unsigned StoredPosition = Tokens->getPosition();
1477 bool IsSimple = true;
1478 FormatToken *Tok = Tokens->getNextToken();
1479 while (Tok) {
1480 if (Tok->is(tok::r_brace))
1481 break;
1482 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1483 IsSimple = false;
1484 break;
1485 }
1486 // FIXME: This will also mark enums with braces in the arguments to enum
1487 // constants as "not simple". This is probably fine in practice, though.
1488 Tok = Tokens->getNextToken();
1489 }
1490 FormatTok = Tokens->setPosition(StoredPosition);
1491
1492 if (IsSimple) {
1493 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001494 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001495 return;
1496 }
1497
1498 // Parse the body of a more complex enum.
1499 // First add a line for everything up to the "{".
1500 nextToken();
1501 addUnwrappedLine();
1502 ++Line->Level;
1503
1504 // Parse the enum constants.
1505 while (FormatTok) {
1506 if (FormatTok->is(tok::l_brace)) {
1507 // Parse the constant's class body.
1508 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1509 /*MunchSemi=*/false);
1510 } else if (FormatTok->is(tok::l_paren)) {
1511 parseParens();
1512 } else if (FormatTok->is(tok::comma)) {
1513 nextToken();
1514 addUnwrappedLine();
1515 } else if (FormatTok->is(tok::semi)) {
1516 nextToken();
1517 addUnwrappedLine();
1518 break;
1519 } else if (FormatTok->is(tok::r_brace)) {
1520 addUnwrappedLine();
1521 break;
1522 } else {
1523 nextToken();
1524 }
1525 }
1526
1527 // Parse the class body after the enum's ";" if any.
1528 parseLevel(/*HasOpeningBrace=*/true);
1529 nextToken();
1530 --Line->Level;
1531 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001532}
1533
Manuel Klimeke01bab52013-01-15 13:38:33 +00001534void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001535 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001536 nextToken();
Manuel Klimek45bf56c2014-07-31 07:19:30 +00001537 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1538 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001539 nextToken();
1540 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001541 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001542 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001543 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001544 // The actual identifier can be a nested name specifier, and in macros
1545 // it is often token-pasted.
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001546 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1547 FormatTok->is(tok::hashhash) ||
Daniel Jasper6a5d38d2015-04-13 14:56:54 +00001548 ((Style.Language == FormatStyle::LK_Java ||
1549 Style.Language == FormatStyle::LK_JavaScript) &&
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001550 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001551 nextToken();
1552
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001553 // Note that parsing away template declarations here leads to incorrectly
1554 // accepting function declarations as record declarations.
1555 // In general, we cannot solve this problem. Consider:
1556 // class A<int> B() {}
1557 // which can be a function definition or a class definition when B() is a
1558 // macro. If we find enough real-world cases where this is a problem, we
1559 // can parse for the 'template' keyword in the beginning of the statement,
1560 // and thus rule out the record production in case there is no template
1561 // (this would still leave us with an ambiguity between template function
1562 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001563 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1564 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1565 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001566 return;
1567 nextToken();
1568 }
1569 }
1570 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001571 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001572 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001573 addUnwrappedLine();
1574
Daniel Jasper240dfda2014-03-31 14:23:49 +00001575 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001576 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001577 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001578 // We fall through to parsing a structural element afterwards, so
1579 // class A {} n, m;
1580 // will end up in one unwrapped line.
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001581 // This does not apply for Java.
Daniel Jasper6fa9ec72015-02-19 16:03:16 +00001582 if (Style.Language == FormatStyle::LK_Java ||
1583 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001584 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001585}
1586
Nico Weber8696a8d2013-01-09 21:15:03 +00001587void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001588 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001589 do
1590 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001591 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001592 nextToken(); // Skip '>'.
1593}
1594
1595void UnwrappedLineParser::parseObjCUntilAtEnd() {
1596 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001597 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001598 nextToken();
1599 addUnwrappedLine();
1600 break;
1601 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001602 if (FormatTok->is(tok::l_brace)) {
1603 parseBlock(/*MustBeDeclaration=*/false);
1604 // In ObjC interfaces, nothing should be following the "}".
1605 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001606 } else if (FormatTok->is(tok::r_brace)) {
1607 // Ignore stray "}". parseStructuralElement doesn't consume them.
1608 nextToken();
1609 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001610 } else {
1611 parseStructuralElement();
1612 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001613 } while (!eof());
1614}
1615
Nico Weber2ce0ac52013-01-09 23:25:37 +00001616void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001617 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001618 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001619
1620 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001621 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001622 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001623 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001624 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001625 // Skip category, if present.
1626 parseParens();
1627
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001628 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001629 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001630
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001631 if (FormatTok->Tok.is(tok::l_brace)) {
1632 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1633 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1634 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001635 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001636 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001637
1638 // With instance variables, this puts '}' on its own line. Without instance
1639 // variables, this ends the @interface line.
1640 addUnwrappedLine();
1641
Nico Weber8696a8d2013-01-09 21:15:03 +00001642 parseObjCUntilAtEnd();
1643}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001644
Nico Weber8696a8d2013-01-09 21:15:03 +00001645void UnwrappedLineParser::parseObjCProtocol() {
1646 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001647 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001648
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001649 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001650 parseObjCProtocolList();
1651
1652 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001653 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001654 nextToken();
1655 return addUnwrappedLine();
1656 }
1657
1658 addUnwrappedLine();
1659 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001660}
1661
Daniel Jasperfca735c2015-02-19 16:14:18 +00001662void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1663 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00001664 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00001665
1666 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_function,
1667 Keywords.kw_var))
1668 return; // Fall through to parsing the corresponding structure.
1669
1670 if (FormatTok->is(tok::kw_default)) {
1671 nextToken(); // export default ..., fall through after eating 'default'.
1672 return;
1673 }
1674
Daniel Jasper354aa512015-02-19 16:07:32 +00001675 if (FormatTok->is(tok::l_brace)) {
1676 FormatTok->BlockKind = BK_Block;
1677 parseBracedList();
1678 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001679
Daniel Jasper354aa512015-02-19 16:07:32 +00001680 while (!eof() && FormatTok->isNot(tok::semi) &&
1681 FormatTok->isNot(tok::l_brace)) {
1682 nextToken();
1683 }
1684}
1685
Daniel Jasper3b203a62013-09-05 16:05:56 +00001686LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1687 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001688 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1689 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1690 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1691 E = Line.Tokens.end();
1692 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001693 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001694 }
1695 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1696 E = Line.Tokens.end();
1697 I != E; ++I) {
1698 const UnwrappedLineNode &Node = *I;
1699 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1700 I = Node.Children.begin(),
1701 E = Node.Children.end();
1702 I != E; ++I) {
1703 printDebugInfo(*I, "\nChild: ");
1704 }
1705 }
1706 llvm::dbgs() << "\n";
1707}
1708
Daniel Jasperf7935112012-12-03 18:12:45 +00001709void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001710 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001711 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001712 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001713 if (CurrentLines == &Lines)
1714 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001715 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001716 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001717 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001718 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001719 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001720 I = PreprocessorDirectives.begin(),
1721 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001722 I != E; ++I) {
1723 CurrentLines->push_back(*I);
1724 }
1725 PreprocessorDirectives.clear();
1726 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001727}
1728
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001729bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001730
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001731bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001732 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1733 FormatTok.NewlinesBefore > 0;
1734}
1735
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001736void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1737 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001738 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001739 I = CommentsBeforeNextToken.begin(),
1740 E = CommentsBeforeNextToken.end();
1741 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001742 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001743 addUnwrappedLine();
1744 }
1745 pushToken(*I);
1746 }
1747 if (NewlineBeforeNext && JustComments) {
1748 addUnwrappedLine();
1749 }
1750 CommentsBeforeNextToken.clear();
1751}
1752
Daniel Jasperf7935112012-12-03 18:12:45 +00001753void UnwrappedLineParser::nextToken() {
1754 if (eof())
1755 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001756 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001757 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001758 readToken();
1759}
1760
1761void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001762 bool CommentsInCurrentLine = true;
1763 do {
1764 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001765 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001766 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1767 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001768 // If there is an unfinished unwrapped line, we flush the preprocessor
1769 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001770 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001771 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001772 // Comments stored before the preprocessor directive need to be output
1773 // before the preprocessor directive, at the same level as the
1774 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001775 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001776 parsePPDirective();
1777 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001778 while (FormatTok->Type == TT_ConflictStart ||
1779 FormatTok->Type == TT_ConflictEnd ||
1780 FormatTok->Type == TT_ConflictAlternative) {
1781 if (FormatTok->Type == TT_ConflictStart) {
1782 conditionalCompilationStart(/*Unreachable=*/false);
1783 } else if (FormatTok->Type == TT_ConflictAlternative) {
1784 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001785 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001786 conditionalCompilationEnd();
1787 }
1788 FormatTok = Tokens->getNextToken();
1789 FormatTok->MustBreakBefore = true;
1790 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001791
1792 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1793 !Line->InPPDirective) {
1794 continue;
1795 }
1796
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001797 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001798 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001799 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001800 CommentsInCurrentLine = false;
1801 }
1802 if (CommentsInCurrentLine) {
1803 pushToken(FormatTok);
1804 } else {
1805 CommentsBeforeNextToken.push_back(FormatTok);
1806 }
1807 } while (!eof());
1808}
1809
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001810void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001811 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001812 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001813 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001814 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001815 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001816}
1817
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001818} // end namespace format
1819} // end namespace clang