blob: 20f5d3f07875b06a64b28224edec0c5b5f680673 [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file contains the implementation of the UnwrappedLineParser,
12/// which turns a stream of tokens into UnwrappedLines.
13///
Daniel Jasperf7935112012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Chandler Carruth4b417452013-01-19 08:09:44 +000016#include "UnwrappedLineParser.h"
Benjamin Kramer33335df2015-03-01 21:36:40 +000017#include "llvm/ADT/STLExtras.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000018#include "llvm/Support/Debug.h"
Benjamin Kramer53f5e892015-03-23 18:05:43 +000019#include "llvm/Support/raw_ostream.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000020
Chandler Carruth10346662014-04-22 03:17:02 +000021#define DEBUG_TYPE "format-parser"
22
Daniel Jasperf7935112012-12-03 18:12:45 +000023namespace clang {
24namespace format {
25
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000026class FormatTokenSource {
27public:
28 virtual ~FormatTokenSource() {}
29 virtual FormatToken *getNextToken() = 0;
30
31 virtual unsigned getPosition() = 0;
32 virtual FormatToken *setPosition(unsigned Position) = 0;
33};
34
Craig Topper69665e12013-07-01 04:21:54 +000035namespace {
36
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000037class ScopedDeclarationState {
38public:
39 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
40 bool MustBeDeclaration)
41 : Line(Line), Stack(Stack) {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000042 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek39080572013-01-23 11:03:04 +000043 Stack.push_back(MustBeDeclaration);
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000044 }
45 ~ScopedDeclarationState() {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000046 Stack.pop_back();
Manuel Klimekc1237a82013-01-23 14:08:21 +000047 if (!Stack.empty())
48 Line.MustBeDeclaration = Stack.back();
49 else
50 Line.MustBeDeclaration = true;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000051 }
Daniel Jasper393564f2013-05-31 14:56:29 +000052
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000053private:
54 UnwrappedLine &Line;
55 std::vector<bool> &Stack;
56};
57
Manuel Klimek1abf7892013-01-04 23:34:14 +000058class ScopedMacroState : public FormatTokenSource {
59public:
60 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek20e0af62015-05-06 11:56:29 +000061 FormatToken *&ResetToken)
Manuel Klimek1abf7892013-01-04 23:34:14 +000062 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000063 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
Manuel Klimek20e0af62015-05-06 11:56:29 +000064 Token(nullptr) {
Manuel Klimek1abf7892013-01-04 23:34:14 +000065 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000066 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000067 Line.InPPDirective = true;
68 }
69
Alexander Kornienko34eb2072015-04-11 02:00:23 +000070 ~ScopedMacroState() override {
Manuel Klimek1abf7892013-01-04 23:34:14 +000071 TokenSource = PreviousTokenSource;
72 ResetToken = Token;
73 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000074 Line.Level = PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +000075 }
76
Craig Topperfb6b25b2014-03-15 04:29:04 +000077 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +000078 // The \c UnwrappedLineParser guards against this by never calling
79 // \c getNextToken() after it has encountered the first eof token.
80 assert(!eof());
Manuel Klimek1abf7892013-01-04 23:34:14 +000081 Token = PreviousTokenSource->getNextToken();
82 if (eof())
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000083 return getFakeEOF();
Manuel Klimek1abf7892013-01-04 23:34:14 +000084 return Token;
85 }
86
Craig Topperfb6b25b2014-03-15 04:29:04 +000087 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +000088
Craig Topperfb6b25b2014-03-15 04:29:04 +000089 FormatToken *setPosition(unsigned Position) override {
Manuel Klimekab419912013-05-23 09:41:43 +000090 Token = PreviousTokenSource->setPosition(Position);
91 return Token;
92 }
93
Manuel Klimek1abf7892013-01-04 23:34:14 +000094private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000095 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimek1abf7892013-01-04 23:34:14 +000096
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000097 FormatToken *getFakeEOF() {
98 static bool EOFInitialized = false;
99 static FormatToken FormatTok;
100 if (!EOFInitialized) {
101 FormatTok.Tok.startToken();
102 FormatTok.Tok.setKind(tok::eof);
103 EOFInitialized = true;
104 }
105 return &FormatTok;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000106 }
107
108 UnwrappedLine &Line;
109 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000110 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000111 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000112 FormatTokenSource *PreviousTokenSource;
113
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000114 FormatToken *Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000115};
116
Craig Topper69665e12013-07-01 04:21:54 +0000117} // end anonymous namespace
118
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000119class ScopedLineState {
120public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000121 ScopedLineState(UnwrappedLineParser &Parser,
122 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000123 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000124 if (SwitchToPreprocessorLines)
125 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000126 else if (!Parser.Line->Tokens.empty())
127 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000128 PreBlockLine = std::move(Parser.Line);
129 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000130 Parser.Line->Level = PreBlockLine->Level;
131 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000132 }
133
134 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000135 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000136 Parser.addUnwrappedLine();
137 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000138 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000139 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000140 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
141 Parser.MustBreakBeforeNextToken = true;
142 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000143 }
144
145private:
146 UnwrappedLineParser &Parser;
147
David Blaikieefb6eb22014-08-09 20:02:07 +0000148 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000149 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000150};
151
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000152class CompoundStatementIndenter {
153public:
154 CompoundStatementIndenter(UnwrappedLineParser *Parser,
155 const FormatStyle &Style, unsigned &LineLevel)
156 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
157 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
158 Parser->addUnwrappedLine();
159 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
160 Parser->addUnwrappedLine();
161 ++LineLevel;
162 }
163 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000164 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000165
166private:
167 unsigned &LineLevel;
168 unsigned OldLineLevel;
169};
170
Craig Topper69665e12013-07-01 04:21:54 +0000171namespace {
172
Manuel Klimekab419912013-05-23 09:41:43 +0000173class IndexedTokenSource : public FormatTokenSource {
174public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000175 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000176 : Tokens(Tokens), Position(-1) {}
177
Craig Topperfb6b25b2014-03-15 04:29:04 +0000178 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000179 ++Position;
180 return Tokens[Position];
181 }
182
Craig Topperfb6b25b2014-03-15 04:29:04 +0000183 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000184 assert(Position >= 0);
185 return Position;
186 }
187
Craig Topperfb6b25b2014-03-15 04:29:04 +0000188 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000189 Position = P;
190 return Tokens[Position];
191 }
192
Manuel Klimek71814b42013-10-11 21:25:45 +0000193 void reset() { Position = -1; }
194
Manuel Klimekab419912013-05-23 09:41:43 +0000195private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000196 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000197 int Position;
198};
199
Craig Topper69665e12013-07-01 04:21:54 +0000200} // end anonymous namespace
201
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000202UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000203 const AdditionalKeywords &Keywords,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000204 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000205 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000206 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Manuel Klimek20e0af62015-05-06 11:56:29 +0000207 CurrentLines(&Lines), Style(Style), Keywords(Keywords), Tokens(nullptr),
208 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000209
210void UnwrappedLineParser::reset() {
211 PPBranchLevel = -1;
212 Line.reset(new UnwrappedLine);
213 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000214 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000215 MustBreakBeforeNextToken = false;
216 PreprocessorDirectives.clear();
217 CurrentLines = &Lines;
218 DeclarationScopeStack.clear();
Manuel Klimek71814b42013-10-11 21:25:45 +0000219 PPStack.clear();
220}
Daniel Jasperf7935112012-12-03 18:12:45 +0000221
Manuel Klimek20e0af62015-05-06 11:56:29 +0000222void UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000223 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimek71814b42013-10-11 21:25:45 +0000224 do {
225 DEBUG(llvm::dbgs() << "----\n");
226 reset();
227 Tokens = &TokenSource;
228 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000229
Manuel Klimek71814b42013-10-11 21:25:45 +0000230 readToken();
231 parseFile();
232 // Create line with eof token.
233 pushToken(FormatTok);
234 addUnwrappedLine();
235
236 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
237 E = Lines.end();
238 I != E; ++I) {
239 Callback.consumeUnwrappedLine(*I);
240 }
241 Callback.finishRun();
242 Lines.clear();
243 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000244 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000245 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
246 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
247 }
248 if (!PPLevelBranchIndex.empty()) {
249 ++PPLevelBranchIndex.back();
250 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
251 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
252 }
253 } while (!PPLevelBranchIndex.empty());
254
Manuel Klimek1abf7892013-01-04 23:34:14 +0000255}
256
Manuel Klimek1a18c402013-04-12 14:13:36 +0000257void UnwrappedLineParser::parseFile() {
Daniel Jasper9326f912015-05-05 08:40:32 +0000258 // The top-level context in a file always has declarations, except for pre-
259 // processor directives and JavaScript files.
260 bool MustBeDeclaration =
261 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
262 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
263 MustBeDeclaration);
Nico Weber9096fc02013-06-26 00:30:14 +0000264 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000265 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000266 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000267 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000268}
269
Manuel Klimek1a18c402013-04-12 14:13:36 +0000270void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000271 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000272 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000273 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000274 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000275 nextToken();
276 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000277 break;
278 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000279 // FIXME: Add parameter whether this can happen - if this happens, we must
280 // be in a non-declaration context.
Nico Weber9096fc02013-06-26 00:30:14 +0000281 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000282 addUnwrappedLine();
283 break;
284 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000285 if (HasOpeningBrace)
286 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000287 nextToken();
288 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000289 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000290 case tok::kw_default:
291 case tok::kw_case:
Daniel Jasper72407622013-09-02 08:26:29 +0000292 if (!SwitchLabelEncountered &&
293 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
294 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000295 SwitchLabelEncountered = true;
296 parseStructuralElement();
297 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000298 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000299 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000300 break;
301 }
302 } while (!eof());
303}
304
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000305void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
Manuel Klimekab419912013-05-23 09:41:43 +0000306 // We'll parse forward through the tokens until we hit
307 // a closing brace or eof - note that getNextToken() will
308 // parse macros, so this will magically work inside macro
309 // definitions, too.
310 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000311 FormatToken *Tok = FormatTok;
Manuel Klimekab419912013-05-23 09:41:43 +0000312 // Keep a stack of positions of lbrace tokens. We will
313 // update information about whether an lbrace starts a
314 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000315 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000316 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000317 do {
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000318 // Get next none-comment token.
319 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000320 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000321 do {
322 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000323 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000324 } while (NextTok->is(tok::comment));
325
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000326 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000327 case tok::l_brace:
Daniel Jasper3c883d12015-05-18 14:49:19 +0000328 Tok->BlockKind = BK_Unknown;
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000329 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000330 break;
331 case tok::r_brace:
332 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000333 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Daniel Jasper220c0d12014-04-10 07:27:12 +0000334 bool ProbablyBracedList = false;
335 if (Style.Language == FormatStyle::LK_Proto) {
336 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
337 } else {
Daniel Jasper91b032a2014-05-22 12:46:38 +0000338 // Using OriginalColumn to distinguish between ObjC methods and
339 // binary operators is a bit hacky.
340 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
341 NextTok->OriginalColumn == 0;
342
Daniel Jasper220c0d12014-04-10 07:27:12 +0000343 // If there is a comma, semicolon or right paren after the closing
344 // brace, we assume this is a braced initializer list. Note that
345 // regardless how we mark inner braces here, we will overwrite the
346 // BlockKind later if we parse a braced list (where all blocks
347 // inside are by default braced lists), or when we explicitly detect
348 // blocks (for example while parsing lambdas).
349 //
350 // We exclude + and - as they can be ObjC visibility modifiers.
351 ProbablyBracedList =
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000352 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
Daniel Jasper438059e2014-05-22 12:11:13 +0000353 tok::r_paren, tok::r_square, tok::l_brace,
Daniel Jasper65df5aa2014-08-04 14:51:02 +0000354 tok::l_paren, tok::ellipsis) ||
Daniel Jaspercec9ffd2015-05-18 14:12:24 +0000355 (NextTok->is(tok::semi) &&
356 (!ExpectClassBody || LBraceStack.size() != 1)) ||
Daniel Jasper91b032a2014-05-22 12:46:38 +0000357 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000358 }
359 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000360 Tok->BlockKind = BK_BracedInit;
361 LBraceStack.back()->BlockKind = BK_BracedInit;
362 } else {
363 Tok->BlockKind = BK_Block;
364 LBraceStack.back()->BlockKind = BK_Block;
365 }
Manuel Klimekab419912013-05-23 09:41:43 +0000366 }
367 LBraceStack.pop_back();
368 }
369 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000370 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000371 case tok::semi:
372 case tok::kw_if:
373 case tok::kw_while:
374 case tok::kw_for:
375 case tok::kw_switch:
376 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000377 case tok::kw___try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000378 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000379 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000380 break;
381 default:
382 break;
383 }
384 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000385 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000386 // Assume other blocks for all unclosed opening braces.
387 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000388 if (LBraceStack[i]->BlockKind == BK_Unknown)
389 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000390 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000391
Manuel Klimekab419912013-05-23 09:41:43 +0000392 FormatTok = Tokens->setPosition(StoredPosition);
393}
394
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000395void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
396 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000397 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000398 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000399 nextToken();
400
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000401 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000402
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000403 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
404 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000405 if (AddLevel)
406 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000407 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000408
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000409 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000410 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000411 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000412 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000413
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000414 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000415 if (MunchSemi && FormatTok->Tok.is(tok::semi))
416 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000417 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000418}
419
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000420static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000421 // FIXME: Closure-library specific stuff should not be hard-coded but be
422 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000423 if (Line.Tokens.size() < 4)
424 return false;
425 auto I = Line.Tokens.begin();
426 if (I->Tok->TokenText != "goog")
427 return false;
428 ++I;
429 if (I->Tok->isNot(tok::period))
430 return false;
431 ++I;
432 if (I->Tok->TokenText != "scope")
433 return false;
434 ++I;
435 return I->Tok->is(tok::l_paren);
436}
437
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000438static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
439 const FormatToken &InitialToken) {
440 switch (Style.BreakBeforeBraces) {
441 case FormatStyle::BS_Linux:
442 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
443 case FormatStyle::BS_Allman:
444 case FormatStyle::BS_GNU:
445 return true;
446 default:
447 return false;
448 }
449}
450
Manuel Klimek516e0542013-09-04 13:25:30 +0000451void UnwrappedLineParser::parseChildBlock() {
452 FormatTok->BlockKind = BK_Block;
453 nextToken();
454 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000455 bool GoogScope =
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000456 Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000457 ScopedLineState LineState(*this);
458 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
459 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000460 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000461 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000462 flushComments(isOnNewLine(*FormatTok));
Daniel Jasper4a39c842014-05-06 13:54:10 +0000463 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000464 }
465 nextToken();
466}
467
Daniel Jasperf7935112012-12-03 18:12:45 +0000468void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000469 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000470 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000471 nextToken();
472
Craig Topper2145bc02014-05-09 08:15:10 +0000473 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000474 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000475 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000476 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000477
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000478 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000479 case tok::pp_define:
480 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000481 return;
482 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000483 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000484 break;
485 case tok::pp_ifdef:
486 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000487 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000488 break;
489 case tok::pp_else:
490 parsePPElse();
491 break;
492 case tok::pp_elif:
493 parsePPElIf();
494 break;
495 case tok::pp_endif:
496 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000497 break;
498 default:
499 parsePPUnknown();
500 break;
501 }
502}
503
Manuel Klimek68b03042014-04-14 09:14:11 +0000504void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
505 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000506 PPStack.push_back(PP_Unreachable);
507 else
508 PPStack.push_back(PP_Conditional);
509}
510
Manuel Klimek68b03042014-04-14 09:14:11 +0000511void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000512 ++PPBranchLevel;
513 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
514 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
515 PPLevelBranchIndex.push_back(0);
516 PPLevelBranchCount.push_back(0);
517 }
518 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000519 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
520 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000521}
522
Manuel Klimek68b03042014-04-14 09:14:11 +0000523void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000524 if (!PPStack.empty())
525 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000526 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
527 if (!PPChainBranchIndex.empty())
528 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000529 conditionalCompilationCondition(
530 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
531 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000532}
533
Manuel Klimek68b03042014-04-14 09:14:11 +0000534void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000535 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
536 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
537 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000538 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
539 }
540 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000541 // Guard against #endif's without #if.
542 if (PPBranchLevel > 0)
543 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000544 if (!PPChainBranchIndex.empty())
545 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000546 if (!PPStack.empty())
547 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000548}
549
550void UnwrappedLineParser::parsePPIf(bool IfDef) {
551 nextToken();
552 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Daniel Jasper9d22bcc2015-01-19 10:51:05 +0000553 FormatTok->Tok.getLiteralData() != nullptr &&
Manuel Klimek68b03042014-04-14 09:14:11 +0000554 StringRef(FormatTok->Tok.getLiteralData(),
555 FormatTok->Tok.getLength()) == "0") ||
556 FormatTok->Tok.is(tok::kw_false);
557 conditionalCompilationStart(!IfDef && IsLiteralFalse);
558 parsePPUnknown();
559}
560
561void UnwrappedLineParser::parsePPElse() {
562 conditionalCompilationAlternative();
563 parsePPUnknown();
564}
565
566void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
567
568void UnwrappedLineParser::parsePPEndIf() {
569 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000570 parsePPUnknown();
571}
572
Manuel Klimek1abf7892013-01-04 23:34:14 +0000573void UnwrappedLineParser::parsePPDefine() {
574 nextToken();
575
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000576 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000577 parsePPUnknown();
578 return;
579 }
580 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000581 if (FormatTok->Tok.getKind() == tok::l_paren &&
582 FormatTok->WhitespaceRange.getBegin() ==
583 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000584 parseParens();
585 }
586 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000587 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000588
589 // Errors during a preprocessor directive can only affect the layout of the
590 // preprocessor directive, and thus we ignore them. An alternative approach
591 // would be to use the same approach we use on the file level (no
592 // re-indentation if there was a structural error) within the macro
593 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000594 parseFile();
595}
596
597void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000598 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000599 nextToken();
600 } while (!eof());
601 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000602}
603
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000604// Here we blacklist certain tokens that are not usually the first token in an
605// unwrapped line. This is used in attempt to distinguish macro calls without
606// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000607static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000608 // Semicolon can be a null-statement, l_square can be a start of a macro or
609 // a C++11 attribute, but this doesn't seem to be common.
610 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
611 Tok.isNot(tok::l_square) &&
612 // Tokens that can only be used as binary operators and a part of
613 // overloaded operator names.
614 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
615 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
616 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
617 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
618 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
619 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
620 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
621 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
622 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
623 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
624 Tok.isNot(tok::lesslessequal) &&
625 // Colon is used in labels, base class lists, initializer lists,
626 // range-based for loops, ternary operator, but should never be the
627 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000628 Tok.isNot(tok::colon) &&
629 // 'noexcept' is a trailing annotation.
630 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000631}
632
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000633void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000634 assert(!FormatTok->Tok.is(tok::l_brace));
635 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000636 case tok::at:
637 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000638 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000639 parseBracedList();
640 break;
641 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000642 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000643 case tok::objc_public:
644 case tok::objc_protected:
645 case tok::objc_package:
646 case tok::objc_private:
647 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000648 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000649 case tok::objc_implementation:
650 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000651 case tok::objc_protocol:
652 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000653 case tok::objc_end:
654 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000655 case tok::objc_optional:
656 case tok::objc_required:
657 nextToken();
658 addUnwrappedLine();
659 return;
Nico Weber33381f52015-02-07 01:57:32 +0000660 case tok::objc_try:
661 // This branch isn't strictly necessary (the kw_try case below would
662 // do this too after the tok::at is parsed above). But be explicit.
663 parseTryCatch();
664 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000665 default:
666 break;
667 }
668 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000669 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000670 nextToken();
671 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000672 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000673 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000674 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000675 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000676 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000677 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000678 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000679 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:
Daniel Jasper66cb8c52015-05-04 09:22:29 +0000747 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +0000748 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:
Daniel Jasper90cf3802015-06-17 09:44:02 +0000777 // parseEnum falls through and does not yet add an unwrapped line as an
778 // enum definition can start a structural element.
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000779 parseEnum();
Daniel Jasper90cf3802015-06-17 09:44:02 +0000780 // This does not apply for Java and JavaScript.
781 if (Style.Language == FormatStyle::LK_Java ||
782 Style.Language == FormatStyle::LK_JavaScript) {
783 addUnwrappedLine();
784 return;
785 }
Manuel Klimek2cec0192013-01-21 19:17:52 +0000786 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000787 case tok::kw_typedef:
788 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000789 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
790 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000791 parseEnum();
792 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000793 case tok::kw_struct:
794 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000795 case tok::kw_class:
Daniel Jasper910807d2015-06-12 04:52:02 +0000796 // parseRecord falls through and does not yet add an unwrapped line as a
797 // record declaration or definition can start a structural element.
Manuel Klimeke01bab52013-01-15 13:38:33 +0000798 parseRecord();
Daniel Jasper910807d2015-06-12 04:52:02 +0000799 // This does not apply for Java and JavaScript.
800 if (Style.Language == FormatStyle::LK_Java ||
801 Style.Language == FormatStyle::LK_JavaScript) {
802 addUnwrappedLine();
803 return;
804 }
Manuel Klimeke01bab52013-01-15 13:38:33 +0000805 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000806 case tok::period:
807 nextToken();
808 // In Java, classes have an implicit static member "class".
809 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
810 FormatTok->is(tok::kw_class))
811 nextToken();
812 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000813 case tok::semi:
814 nextToken();
815 addUnwrappedLine();
816 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000817 case tok::r_brace:
818 addUnwrappedLine();
819 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000820 case tok::l_paren:
821 parseParens();
822 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000823 case tok::caret:
824 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000825 if (FormatTok->Tok.isAnyIdentifier() ||
826 FormatTok->isSimpleTypeSpecifier())
827 nextToken();
828 if (FormatTok->is(tok::l_paren))
829 parseParens();
830 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000831 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000832 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000833 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000834 if (!tryToParseBracedList()) {
835 // A block outside of parentheses must be the last part of a
836 // structural element.
837 // FIXME: Figure out cases where this is not true, and add projections
838 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000839 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000840 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000841 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000842 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000843 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000844 return;
845 }
846 // Otherwise this was a braced init list, and the structural
847 // element continues.
848 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000849 case tok::kw_try:
850 // We arrive here when parsing function-try blocks.
851 parseTryCatch();
852 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000853 case tok::identifier: {
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000854 // Parse function literal unless 'function' is the first token in a line
855 // in which case this should be treated as a free-standing function.
Daniel Jasper9326f912015-05-05 08:40:32 +0000856 if (Style.Language == FormatStyle::LK_JavaScript &&
857 FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000858 tryToParseJSFunction();
859 break;
860 }
Daniel Jasper9326f912015-05-05 08:40:32 +0000861 if ((Style.Language == FormatStyle::LK_JavaScript ||
862 Style.Language == FormatStyle::LK_Java) &&
863 FormatTok->is(Keywords.kw_interface)) {
864 parseRecord();
Daniel Jasper259188b2015-06-12 04:56:34 +0000865 addUnwrappedLine();
Daniel Jasper9326f912015-05-05 08:40:32 +0000866 break;
867 }
868
869 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +0000870 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +0000871 if (Line->Tokens.size() == 1 &&
872 // JS doesn't have macros, and within classes colons indicate fields,
873 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +0000874 Style.Language != FormatStyle::LK_JavaScript) {
875 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000876 parseLabel();
877 return;
878 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000879 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +0000880 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +0000881 bool FunctionLike = FormatTok->is(tok::l_paren);
882 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000883 parseParens();
Daniel Jaspere60cba12015-05-13 11:35:53 +0000884
885 bool FollowedByNewline =
886 CommentsBeforeNextToken.empty()
887 ? FormatTok->NewlinesBefore > 0
888 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
889
890 if (FollowedByNewline &&
Daniel Jasper680b09b2014-11-05 10:48:04 +0000891 (Text.size() >= 5 || FunctionLike) &&
892 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000893 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000894 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000895 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000896 }
897 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000898 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000899 case tok::equal:
Manuel Klimek79e06082015-05-21 12:23:34 +0000900 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
901 // TT_JsFatArrow. The always start an expression or a child block if
902 // followed by a curly.
903 if (FormatTok->is(TT_JsFatArrow)) {
904 nextToken();
Daniel Jasperbe520bd2015-05-31 08:51:54 +0000905 if (FormatTok->is(tok::l_brace))
Manuel Klimek79e06082015-05-21 12:23:34 +0000906 parseChildBlock();
Manuel Klimek79e06082015-05-21 12:23:34 +0000907 break;
908 }
909
Daniel Jaspere25509f2012-12-17 11:29:41 +0000910 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000911 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000912 parseBracedList();
913 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000914 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000915 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000916 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000917 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +0000918 case tok::kw_new:
919 parseNew();
920 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000921 default:
922 nextToken();
923 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000924 }
925 } while (!eof());
926}
927
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000928bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper1feab0f2015-06-02 15:31:37 +0000929 if (Style.Language != FormatStyle::LK_Cpp) {
930 nextToken();
931 return false;
932 }
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000933 // FIXME: This is a dirty way to access the previous token. Find a better
934 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000935 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000936 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
937 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000938 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000939 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000940 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000941 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000942 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000943 assert(FormatTok->is(tok::l_square));
944 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000945 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000946 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000947
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000948 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000949 if (FormatTok->isSimpleTypeSpecifier()) {
950 nextToken();
951 continue;
952 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000953 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000954 case tok::l_brace:
955 break;
956 case tok::l_paren:
957 parseParens();
958 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000959 case tok::amp:
960 case tok::star:
961 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000962 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000963 case tok::less:
964 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000965 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000966 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000967 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000968 nextToken();
969 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000970 case tok::arrow:
Daniel Jasper6f2b88a2015-06-05 13:18:09 +0000971 FormatTok->Type = TT_LambdaArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000972 nextToken();
973 break;
974 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000975 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000976 }
977 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000978 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000979 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000980 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000981}
982
983bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
984 nextToken();
985 if (FormatTok->is(tok::equal)) {
986 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000987 if (FormatTok->is(tok::r_square)) {
988 nextToken();
989 return true;
990 }
991 if (FormatTok->isNot(tok::comma))
992 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000993 nextToken();
994 } else if (FormatTok->is(tok::amp)) {
995 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000996 if (FormatTok->is(tok::r_square)) {
997 nextToken();
998 return true;
999 }
Manuel Klimekffdeb592013-09-03 15:10:01 +00001000 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
1001 return false;
1002 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001003 if (FormatTok->is(tok::comma))
1004 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001005 } else if (FormatTok->is(tok::r_square)) {
1006 nextToken();
1007 return true;
1008 }
1009 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001010 if (FormatTok->is(tok::amp))
1011 nextToken();
1012 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
1013 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001014 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +00001015 if (FormatTok->is(tok::ellipsis))
1016 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001017 if (FormatTok->is(tok::comma)) {
1018 nextToken();
1019 } else if (FormatTok->is(tok::r_square)) {
1020 nextToken();
1021 return true;
1022 } else {
1023 return false;
1024 }
1025 } while (!eof());
1026 return false;
1027}
1028
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001029void UnwrappedLineParser::tryToParseJSFunction() {
1030 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001031
1032 // Consume function name.
1033 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001034 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001035
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001036 if (FormatTok->isNot(tok::l_paren))
1037 return;
Manuel Klimek79e06082015-05-21 12:23:34 +00001038
1039 // Parse formal parameter list.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001040 parseParens();
Manuel Klimek79e06082015-05-21 12:23:34 +00001041
1042 if (FormatTok->is(tok::colon)) {
1043 // Parse a type definition.
1044 nextToken();
1045
1046 // Eat the type declaration. For braced inline object types, balance braces,
1047 // otherwise just parse until finding an l_brace for the function body.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001048 if (FormatTok->is(tok::l_brace))
1049 tryToParseBracedList();
1050 else
1051 while(FormatTok->isNot(tok::l_brace) && !eof())
Manuel Klimek79e06082015-05-21 12:23:34 +00001052 nextToken();
Manuel Klimek79e06082015-05-21 12:23:34 +00001053 }
1054
1055 parseChildBlock();
1056}
1057
Daniel Jasper3c883d12015-05-18 14:49:19 +00001058bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001059 if (FormatTok->BlockKind == BK_Unknown)
Daniel Jasper3c883d12015-05-18 14:49:19 +00001060 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001061 assert(FormatTok->BlockKind != BK_Unknown);
1062 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001063 return false;
1064 parseBracedList();
1065 return true;
1066}
1067
Daniel Jasper015ed022013-09-13 09:20:45 +00001068bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1069 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001070 nextToken();
1071
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001072 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1073 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001074 do {
Manuel Klimek79e06082015-05-21 12:23:34 +00001075 if (Style.Language == FormatStyle::LK_JavaScript) {
1076 if (FormatTok->is(Keywords.kw_function)) {
1077 tryToParseJSFunction();
1078 continue;
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001079 }
1080 if (FormatTok->is(TT_JsFatArrow)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001081 nextToken();
1082 // Fat arrows can be followed by simple expressions or by child blocks
1083 // in curly braces.
1084 if (FormatTok->is(tok::l_brace)){
1085 parseChildBlock();
1086 continue;
1087 }
1088 }
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001089 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001090 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001091 case tok::caret:
1092 nextToken();
1093 if (FormatTok->is(tok::l_brace)) {
1094 parseChildBlock();
1095 }
1096 break;
1097 case tok::l_square:
1098 tryToParseLambda();
1099 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001100 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001101 // Assume there are no blocks inside a braced init list apart
1102 // from the ones we explicitly parse out (like lambdas).
1103 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001104 parseBracedList();
1105 break;
Daniel Jasperf46dec82015-03-31 14:34:15 +00001106 case tok::r_paren:
1107 // JavaScript can just have free standing methods and getters/setters in
1108 // object literals. Detect them by a "{" following ")".
1109 if (Style.Language == FormatStyle::LK_JavaScript) {
1110 nextToken();
1111 if (FormatTok->is(tok::l_brace))
1112 parseChildBlock();
1113 break;
1114 }
1115 nextToken();
1116 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001117 case tok::r_brace:
1118 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001119 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001120 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001121 HasError = true;
1122 if (!ContinueOnSemicolons)
1123 return !HasError;
1124 nextToken();
1125 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001126 case tok::comma:
1127 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001128 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001129 default:
1130 nextToken();
1131 break;
1132 }
1133 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001134 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001135}
1136
Daniel Jasperf7935112012-12-03 18:12:45 +00001137void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001138 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001139 nextToken();
1140 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001141 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001142 case tok::l_paren:
1143 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001144 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1145 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001146 break;
1147 case tok::r_paren:
1148 nextToken();
1149 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001150 case tok::r_brace:
1151 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1152 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001153 case tok::l_square:
1154 tryToParseLambda();
1155 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001156 case tok::l_brace:
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001157 if (!tryToParseBracedList())
Manuel Klimekf017dc02013-09-04 13:34:14 +00001158 parseChildBlock();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001159 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001160 case tok::at:
1161 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001162 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001163 parseBracedList();
1164 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001165 case tok::identifier:
1166 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001167 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001168 tryToParseJSFunction();
1169 else
1170 nextToken();
1171 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001172 default:
1173 nextToken();
1174 break;
1175 }
1176 } while (!eof());
1177}
1178
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001179void UnwrappedLineParser::parseSquare() {
1180 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1181 if (tryToParseLambda())
1182 return;
1183 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001184 switch (FormatTok->Tok.getKind()) {
1185 case tok::l_paren:
1186 parseParens();
1187 break;
1188 case tok::r_square:
1189 nextToken();
1190 return;
1191 case tok::r_brace:
1192 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1193 return;
1194 case tok::l_square:
1195 parseSquare();
1196 break;
1197 case tok::l_brace: {
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001198 if (!tryToParseBracedList())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001199 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001200 break;
1201 }
1202 case tok::at:
1203 nextToken();
1204 if (FormatTok->Tok.is(tok::l_brace))
1205 parseBracedList();
1206 break;
1207 default:
1208 nextToken();
1209 break;
1210 }
1211 } while (!eof());
1212}
1213
Daniel Jasperf7935112012-12-03 18:12:45 +00001214void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001215 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001216 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001217 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001218 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001219 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001220 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001221 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001222 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001223 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1224 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001225 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001226 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001227 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001228 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001229 } else {
1230 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001231 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001232 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001233 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001234 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001235 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001236 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1237 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001238 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001239 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001240 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001241 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001242 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001243 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001244 parseIfThenElse();
1245 } else {
1246 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001247 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001248 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001249 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001250 }
1251 } else if (NeedsUnwrappedLine) {
1252 addUnwrappedLine();
1253 }
1254}
1255
Daniel Jasper04a71a42014-05-08 11:58:24 +00001256void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001257 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001258 nextToken();
1259 bool NeedsUnwrappedLine = false;
1260 if (FormatTok->is(tok::colon)) {
1261 // We are in a function try block, what comes is an initializer list.
1262 nextToken();
1263 while (FormatTok->is(tok::identifier)) {
1264 nextToken();
1265 if (FormatTok->is(tok::l_paren))
1266 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001267 if (FormatTok->is(tok::comma))
1268 nextToken();
1269 }
1270 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001271 // Parse try with resource.
1272 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1273 parseParens();
1274 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001275 if (FormatTok->is(tok::l_brace)) {
1276 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1277 parseBlock(/*MustBeDeclaration=*/false);
1278 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1279 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1280 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1281 addUnwrappedLine();
1282 } else {
1283 NeedsUnwrappedLine = true;
1284 }
1285 } else if (!FormatTok->is(tok::kw_catch)) {
1286 // The C++ standard requires a compound-statement after a try.
1287 // If there's none, we try to assume there's a structuralElement
1288 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001289 addUnwrappedLine();
1290 ++Line->Level;
1291 parseStructuralElement();
1292 --Line->Level;
1293 }
Nico Weber33381f52015-02-07 01:57:32 +00001294 while (1) {
1295 if (FormatTok->is(tok::at))
1296 nextToken();
1297 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1298 tok::kw___finally) ||
1299 ((Style.Language == FormatStyle::LK_Java ||
1300 Style.Language == FormatStyle::LK_JavaScript) &&
1301 FormatTok->is(Keywords.kw_finally)) ||
1302 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1303 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1304 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001305 nextToken();
1306 while (FormatTok->isNot(tok::l_brace)) {
1307 if (FormatTok->is(tok::l_paren)) {
1308 parseParens();
1309 continue;
1310 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001311 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001312 return;
1313 nextToken();
1314 }
1315 NeedsUnwrappedLine = false;
1316 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1317 parseBlock(/*MustBeDeclaration=*/false);
1318 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1319 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1320 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1321 addUnwrappedLine();
1322 } else {
1323 NeedsUnwrappedLine = true;
1324 }
1325 }
1326 if (NeedsUnwrappedLine) {
1327 addUnwrappedLine();
1328 }
1329}
1330
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001331void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001332 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001333
1334 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001335 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001336 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001337 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001338 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001339 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001340 addUnwrappedLine();
1341
Daniel Jasper65ee3472013-07-31 23:16:02 +00001342 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1343 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1344 DeclarationScopeStack.size() > 1);
1345 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001346 // Munch the semicolon after a namespace. This is more common than one would
1347 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001348 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001349 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001350 addUnwrappedLine();
1351 }
1352 // FIXME: Add error handling.
1353}
1354
Daniel Jasper6acf5132015-03-12 14:44:29 +00001355void UnwrappedLineParser::parseNew() {
1356 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1357 nextToken();
1358 if (Style.Language != FormatStyle::LK_Java)
1359 return;
1360
1361 // In Java, we can parse everything up to the parens, which aren't optional.
1362 do {
1363 // There should not be a ;, { or } before the new's open paren.
1364 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1365 return;
1366
1367 // Consume the parens.
1368 if (FormatTok->is(tok::l_paren)) {
1369 parseParens();
1370
1371 // If there is a class body of an anonymous class, consume that as child.
1372 if (FormatTok->is(tok::l_brace))
1373 parseChildBlock();
1374 return;
1375 }
1376 nextToken();
1377 } while (!eof());
1378}
1379
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001380void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001381 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001382 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001383 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001384 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001385 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001386 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001387 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001388 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001389 addUnwrappedLine();
1390 } else {
1391 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001392 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001393 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001394 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001395 }
1396}
1397
Daniel Jasperf7935112012-12-03 18:12:45 +00001398void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001399 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001400 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001401 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001402 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001403 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001404 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1405 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001406 } else {
1407 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001408 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001409 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001410 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001411 }
1412
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001413 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001414 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001415 addUnwrappedLine();
1416 return;
1417 }
1418
Daniel Jasperf7935112012-12-03 18:12:45 +00001419 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001420 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001421}
1422
1423void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001424 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001425 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001426 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001427 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001428 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001429 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001430 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001431 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001432 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1433 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1434 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001435 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001436 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001437 parseStructuralElement();
1438 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001439 addUnwrappedLine();
1440 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001441 if (FormatTok->is(tok::semi))
1442 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001443 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001444 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001445 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001446}
1447
1448void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001449 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001450 // FIXME: fix handling of complex expressions here.
1451 do {
1452 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001453 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001454 parseLabel();
1455}
1456
1457void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001458 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001459 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001460 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001461 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001462 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001463 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001464 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001465 addUnwrappedLine();
1466 } else {
1467 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001468 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001469 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001470 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001471 }
1472}
1473
1474void UnwrappedLineParser::parseAccessSpecifier() {
1475 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001476 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001477 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001478 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001479 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001480 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001481 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001482 addUnwrappedLine();
1483}
1484
1485void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001486 // Won't be 'enum' for NS_ENUMs.
1487 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001488 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001489
Daniel Jasper2b41a822013-08-20 12:42:50 +00001490 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001491 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1492 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001493 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001494 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1495 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001496 nextToken();
1497 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001498 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001499 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001500 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001501 nextToken();
1502 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001503
1504 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001505 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001506 return;
1507 FormatTok->BlockKind = BK_Block;
1508
1509 if (Style.Language == FormatStyle::LK_Java) {
1510 // Java enums are different.
1511 parseJavaEnumBody();
1512 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001513 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001514
1515 // Parse enum body.
1516 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1517 if (HasError) {
1518 if (FormatTok->is(tok::semi))
1519 nextToken();
1520 addUnwrappedLine();
1521 }
1522
Daniel Jasper90cf3802015-06-17 09:44:02 +00001523 // There is no addUnwrappedLine() here so that we fall through to parsing a
1524 // structural element afterwards. Thus, in "enum A {} n, m;",
Manuel Klimek2cec0192013-01-21 19:17:52 +00001525 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001526}
1527
1528void UnwrappedLineParser::parseJavaEnumBody() {
1529 // Determine whether the enum is simple, i.e. does not have a semicolon or
1530 // constants with class bodies. Simple enums can be formatted like braced
1531 // lists, contracted to a single line, etc.
1532 unsigned StoredPosition = Tokens->getPosition();
1533 bool IsSimple = true;
1534 FormatToken *Tok = Tokens->getNextToken();
1535 while (Tok) {
1536 if (Tok->is(tok::r_brace))
1537 break;
1538 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1539 IsSimple = false;
1540 break;
1541 }
1542 // FIXME: This will also mark enums with braces in the arguments to enum
1543 // constants as "not simple". This is probably fine in practice, though.
1544 Tok = Tokens->getNextToken();
1545 }
1546 FormatTok = Tokens->setPosition(StoredPosition);
1547
1548 if (IsSimple) {
1549 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001550 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001551 return;
1552 }
1553
1554 // Parse the body of a more complex enum.
1555 // First add a line for everything up to the "{".
1556 nextToken();
1557 addUnwrappedLine();
1558 ++Line->Level;
1559
1560 // Parse the enum constants.
1561 while (FormatTok) {
1562 if (FormatTok->is(tok::l_brace)) {
1563 // Parse the constant's class body.
1564 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1565 /*MunchSemi=*/false);
1566 } else if (FormatTok->is(tok::l_paren)) {
1567 parseParens();
1568 } else if (FormatTok->is(tok::comma)) {
1569 nextToken();
1570 addUnwrappedLine();
1571 } else if (FormatTok->is(tok::semi)) {
1572 nextToken();
1573 addUnwrappedLine();
1574 break;
1575 } else if (FormatTok->is(tok::r_brace)) {
1576 addUnwrappedLine();
1577 break;
1578 } else {
1579 nextToken();
1580 }
1581 }
1582
1583 // Parse the class body after the enum's ";" if any.
1584 parseLevel(/*HasOpeningBrace=*/true);
1585 nextToken();
1586 --Line->Level;
1587 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001588}
1589
Manuel Klimeke01bab52013-01-15 13:38:33 +00001590void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001591 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001592 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00001593
1594
1595 // The actual identifier can be a nested name specifier, and in macros
1596 // it is often token-pasted.
1597 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
1598 tok::kw___attribute, tok::kw___declspec,
1599 tok::kw_alignas) ||
1600 ((Style.Language == FormatStyle::LK_Java ||
1601 Style.Language == FormatStyle::LK_JavaScript) &&
1602 FormatTok->isOneOf(tok::period, tok::comma))) {
1603 bool IsNonMacroIdentifier =
1604 FormatTok->is(tok::identifier) &&
1605 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00001606 nextToken();
1607 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00001608 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001609 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00001610 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001611
Daniel Jasper04785d02015-05-06 14:03:02 +00001612 // Note that parsing away template declarations here leads to incorrectly
1613 // accepting function declarations as record declarations.
1614 // In general, we cannot solve this problem. Consider:
1615 // class A<int> B() {}
1616 // which can be a function definition or a class definition when B() is a
1617 // macro. If we find enough real-world cases where this is a problem, we
1618 // can parse for the 'template' keyword in the beginning of the statement,
1619 // and thus rule out the record production in case there is no template
1620 // (this would still leave us with an ambiguity between template function
1621 // and class declarations).
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001622 if (FormatTok->isOneOf(tok::colon, tok::less)) {
1623 while (!eof()) {
Daniel Jasper3c883d12015-05-18 14:49:19 +00001624 if (FormatTok->is(tok::l_brace)) {
1625 calculateBraceTypes(/*ExpectClassBody=*/true);
1626 if (!tryToParseBracedList())
1627 break;
1628 }
Daniel Jasper04785d02015-05-06 14:03:02 +00001629 if (FormatTok->Tok.is(tok::semi))
1630 return;
1631 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00001632 }
1633 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001634 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001635 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001636 addUnwrappedLine();
1637
Daniel Jasper240dfda2014-03-31 14:23:49 +00001638 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001639 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001640 }
Daniel Jasper90cf3802015-06-17 09:44:02 +00001641 // There is no addUnwrappedLine() here so that we fall through to parsing a
1642 // structural element afterwards. Thus, in "class A {} n, m;",
1643 // "} n, m;" will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00001644}
1645
Nico Weber8696a8d2013-01-09 21:15:03 +00001646void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001647 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001648 do
1649 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001650 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001651 nextToken(); // Skip '>'.
1652}
1653
1654void UnwrappedLineParser::parseObjCUntilAtEnd() {
1655 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001656 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001657 nextToken();
1658 addUnwrappedLine();
1659 break;
1660 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001661 if (FormatTok->is(tok::l_brace)) {
1662 parseBlock(/*MustBeDeclaration=*/false);
1663 // In ObjC interfaces, nothing should be following the "}".
1664 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001665 } else if (FormatTok->is(tok::r_brace)) {
1666 // Ignore stray "}". parseStructuralElement doesn't consume them.
1667 nextToken();
1668 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001669 } else {
1670 parseStructuralElement();
1671 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001672 } while (!eof());
1673}
1674
Nico Weber2ce0ac52013-01-09 23:25:37 +00001675void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001676 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001677 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001678
1679 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001680 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001681 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001682 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001683 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001684 // Skip category, if present.
1685 parseParens();
1686
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001687 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001688 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001689
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001690 if (FormatTok->Tok.is(tok::l_brace)) {
1691 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1692 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1693 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001694 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001695 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001696
1697 // With instance variables, this puts '}' on its own line. Without instance
1698 // variables, this ends the @interface line.
1699 addUnwrappedLine();
1700
Nico Weber8696a8d2013-01-09 21:15:03 +00001701 parseObjCUntilAtEnd();
1702}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001703
Nico Weber8696a8d2013-01-09 21:15:03 +00001704void UnwrappedLineParser::parseObjCProtocol() {
1705 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001706 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001707
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001708 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001709 parseObjCProtocolList();
1710
1711 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001712 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001713 nextToken();
1714 return addUnwrappedLine();
1715 }
1716
1717 addUnwrappedLine();
1718 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001719}
1720
Daniel Jasperfca735c2015-02-19 16:14:18 +00001721void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1722 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00001723 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00001724
Daniel Jasperec05fc72015-05-11 09:14:50 +00001725 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00001726 if (FormatTok->is(tok::kw_default))
1727 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00001728
1729 // Consume "function" and "default function", so that these get parsed as
1730 // free-standing JS functions, i.e. do not require a trailing semicolon.
Daniel Jasper668c7bb2015-05-11 09:03:10 +00001731 if (FormatTok->is(Keywords.kw_function)) {
1732 nextToken();
1733 return;
1734 }
1735
Daniel Jasper216c9cd2015-06-12 05:08:18 +00001736 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, tok::kw_enum,
1737 Keywords.kw_var))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001738 return; // Fall through to parsing the corresponding structure.
1739
Daniel Jasper354aa512015-02-19 16:07:32 +00001740 if (FormatTok->is(tok::l_brace)) {
1741 FormatTok->BlockKind = BK_Block;
1742 parseBracedList();
1743 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001744
Daniel Jasper354aa512015-02-19 16:07:32 +00001745 while (!eof() && FormatTok->isNot(tok::semi) &&
1746 FormatTok->isNot(tok::l_brace)) {
1747 nextToken();
1748 }
1749}
1750
Daniel Jasper3b203a62013-09-05 16:05:56 +00001751LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1752 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001753 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1754 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1755 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1756 E = Line.Tokens.end();
1757 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001758 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001759 }
1760 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1761 E = Line.Tokens.end();
1762 I != E; ++I) {
1763 const UnwrappedLineNode &Node = *I;
1764 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1765 I = Node.Children.begin(),
1766 E = Node.Children.end();
1767 I != E; ++I) {
1768 printDebugInfo(*I, "\nChild: ");
1769 }
1770 }
1771 llvm::dbgs() << "\n";
1772}
1773
Daniel Jasperf7935112012-12-03 18:12:45 +00001774void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001775 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001776 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001777 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001778 if (CurrentLines == &Lines)
1779 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001780 });
Benjamin Kramerc7551a42015-05-31 11:18:05 +00001781 CurrentLines->push_back(std::move(*Line));
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001782 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001783 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Benjamin Kramerc7551a42015-05-31 11:18:05 +00001784 CurrentLines->append(
1785 std::make_move_iterator(PreprocessorDirectives.begin()),
1786 std::make_move_iterator(PreprocessorDirectives.end()));
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001787 PreprocessorDirectives.clear();
1788 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001789}
1790
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001791bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001792
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001793bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001794 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1795 FormatTok.NewlinesBefore > 0;
1796}
1797
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001798void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1799 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001800 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001801 I = CommentsBeforeNextToken.begin(),
1802 E = CommentsBeforeNextToken.end();
1803 I != E; ++I) {
Daniel Jaspere60cba12015-05-13 11:35:53 +00001804 if (isOnNewLine(**I) && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001805 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001806 pushToken(*I);
1807 }
Daniel Jaspere60cba12015-05-13 11:35:53 +00001808 if (NewlineBeforeNext && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001809 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001810 CommentsBeforeNextToken.clear();
1811}
1812
Daniel Jasperf7935112012-12-03 18:12:45 +00001813void UnwrappedLineParser::nextToken() {
1814 if (eof())
1815 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001816 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001817 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001818 readToken();
1819}
1820
1821void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001822 bool CommentsInCurrentLine = true;
1823 do {
1824 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001825 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001826 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1827 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001828 // If there is an unfinished unwrapped line, we flush the preprocessor
1829 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001830 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001831 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001832 // Comments stored before the preprocessor directive need to be output
1833 // before the preprocessor directive, at the same level as the
1834 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001835 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001836 parsePPDirective();
1837 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001838 while (FormatTok->Type == TT_ConflictStart ||
1839 FormatTok->Type == TT_ConflictEnd ||
1840 FormatTok->Type == TT_ConflictAlternative) {
1841 if (FormatTok->Type == TT_ConflictStart) {
1842 conditionalCompilationStart(/*Unreachable=*/false);
1843 } else if (FormatTok->Type == TT_ConflictAlternative) {
1844 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001845 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001846 conditionalCompilationEnd();
1847 }
1848 FormatTok = Tokens->getNextToken();
1849 FormatTok->MustBreakBefore = true;
1850 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001851
1852 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1853 !Line->InPPDirective) {
1854 continue;
1855 }
1856
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001857 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001858 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001859 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001860 CommentsInCurrentLine = false;
1861 }
1862 if (CommentsInCurrentLine) {
1863 pushToken(FormatTok);
1864 } else {
1865 CommentsBeforeNextToken.push_back(FormatTok);
1866 }
1867 } while (!eof());
1868}
1869
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001870void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001871 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001872 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001873 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001874 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001875 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001876}
1877
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001878} // end namespace format
1879} // end namespace clang