blob: d0a8cfbe69f7e5946b64e4a9f435e64ec200c4c6 [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 Jasperb1f74a82013-07-09 09:06:29 +0000328 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000329 break;
330 case tok::r_brace:
331 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000332 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Daniel Jasper220c0d12014-04-10 07:27:12 +0000333 bool ProbablyBracedList = false;
334 if (Style.Language == FormatStyle::LK_Proto) {
335 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
336 } else {
Daniel Jasper91b032a2014-05-22 12:46:38 +0000337 // Using OriginalColumn to distinguish between ObjC methods and
338 // binary operators is a bit hacky.
339 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
340 NextTok->OriginalColumn == 0;
341
Daniel Jasper220c0d12014-04-10 07:27:12 +0000342 // If there is a comma, semicolon or right paren after the closing
343 // brace, we assume this is a braced initializer list. Note that
344 // regardless how we mark inner braces here, we will overwrite the
345 // BlockKind later if we parse a braced list (where all blocks
346 // inside are by default braced lists), or when we explicitly detect
347 // blocks (for example while parsing lambdas).
348 //
349 // We exclude + and - as they can be ObjC visibility modifiers.
350 ProbablyBracedList =
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000351 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
Daniel Jasper438059e2014-05-22 12:11:13 +0000352 tok::r_paren, tok::r_square, tok::l_brace,
Daniel Jasper65df5aa2014-08-04 14:51:02 +0000353 tok::l_paren, tok::ellipsis) ||
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000354 (NextTok->is(tok::semi) && !ExpectClassBody) ||
Daniel Jasper91b032a2014-05-22 12:46:38 +0000355 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000356 }
357 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000358 Tok->BlockKind = BK_BracedInit;
359 LBraceStack.back()->BlockKind = BK_BracedInit;
360 } else {
361 Tok->BlockKind = BK_Block;
362 LBraceStack.back()->BlockKind = BK_Block;
363 }
Manuel Klimekab419912013-05-23 09:41:43 +0000364 }
365 LBraceStack.pop_back();
366 }
367 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000368 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000369 case tok::semi:
370 case tok::kw_if:
371 case tok::kw_while:
372 case tok::kw_for:
373 case tok::kw_switch:
374 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000375 case tok::kw___try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000376 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000377 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000378 break;
379 default:
380 break;
381 }
382 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000383 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000384 // Assume other blocks for all unclosed opening braces.
385 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000386 if (LBraceStack[i]->BlockKind == BK_Unknown)
387 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000388 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000389
Manuel Klimekab419912013-05-23 09:41:43 +0000390 FormatTok = Tokens->setPosition(StoredPosition);
391}
392
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000393void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
394 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000395 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000396 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000397 nextToken();
398
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000399 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000400
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000401 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
402 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000403 if (AddLevel)
404 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000405 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000406
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000407 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000408 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000409 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000410 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000411
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000412 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000413 if (MunchSemi && FormatTok->Tok.is(tok::semi))
414 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000415 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000416}
417
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000418static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000419 // FIXME: Closure-library specific stuff should not be hard-coded but be
420 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000421 if (Line.Tokens.size() < 4)
422 return false;
423 auto I = Line.Tokens.begin();
424 if (I->Tok->TokenText != "goog")
425 return false;
426 ++I;
427 if (I->Tok->isNot(tok::period))
428 return false;
429 ++I;
430 if (I->Tok->TokenText != "scope")
431 return false;
432 ++I;
433 return I->Tok->is(tok::l_paren);
434}
435
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000436static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
437 const FormatToken &InitialToken) {
438 switch (Style.BreakBeforeBraces) {
439 case FormatStyle::BS_Linux:
440 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
441 case FormatStyle::BS_Allman:
442 case FormatStyle::BS_GNU:
443 return true;
444 default:
445 return false;
446 }
447}
448
Manuel Klimek516e0542013-09-04 13:25:30 +0000449void UnwrappedLineParser::parseChildBlock() {
450 FormatTok->BlockKind = BK_Block;
451 nextToken();
452 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000453 bool GoogScope =
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000454 Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000455 ScopedLineState LineState(*this);
456 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
457 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000458 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000459 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000460 flushComments(isOnNewLine(*FormatTok));
Daniel Jasper4a39c842014-05-06 13:54:10 +0000461 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000462 }
463 nextToken();
464}
465
Daniel Jasperf7935112012-12-03 18:12:45 +0000466void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000467 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000468 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000469 nextToken();
470
Craig Topper2145bc02014-05-09 08:15:10 +0000471 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000472 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000473 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000474 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000475
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000476 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000477 case tok::pp_define:
478 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000479 return;
480 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000481 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000482 break;
483 case tok::pp_ifdef:
484 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000485 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000486 break;
487 case tok::pp_else:
488 parsePPElse();
489 break;
490 case tok::pp_elif:
491 parsePPElIf();
492 break;
493 case tok::pp_endif:
494 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000495 break;
496 default:
497 parsePPUnknown();
498 break;
499 }
500}
501
Manuel Klimek68b03042014-04-14 09:14:11 +0000502void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
503 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000504 PPStack.push_back(PP_Unreachable);
505 else
506 PPStack.push_back(PP_Conditional);
507}
508
Manuel Klimek68b03042014-04-14 09:14:11 +0000509void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000510 ++PPBranchLevel;
511 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
512 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
513 PPLevelBranchIndex.push_back(0);
514 PPLevelBranchCount.push_back(0);
515 }
516 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000517 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
518 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000519}
520
Manuel Klimek68b03042014-04-14 09:14:11 +0000521void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000522 if (!PPStack.empty())
523 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000524 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
525 if (!PPChainBranchIndex.empty())
526 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000527 conditionalCompilationCondition(
528 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
529 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000530}
531
Manuel Klimek68b03042014-04-14 09:14:11 +0000532void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000533 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
534 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
535 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000536 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
537 }
538 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000539 // Guard against #endif's without #if.
540 if (PPBranchLevel > 0)
541 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000542 if (!PPChainBranchIndex.empty())
543 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000544 if (!PPStack.empty())
545 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000546}
547
548void UnwrappedLineParser::parsePPIf(bool IfDef) {
549 nextToken();
550 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Daniel Jasper9d22bcc2015-01-19 10:51:05 +0000551 FormatTok->Tok.getLiteralData() != nullptr &&
Manuel Klimek68b03042014-04-14 09:14:11 +0000552 StringRef(FormatTok->Tok.getLiteralData(),
553 FormatTok->Tok.getLength()) == "0") ||
554 FormatTok->Tok.is(tok::kw_false);
555 conditionalCompilationStart(!IfDef && IsLiteralFalse);
556 parsePPUnknown();
557}
558
559void UnwrappedLineParser::parsePPElse() {
560 conditionalCompilationAlternative();
561 parsePPUnknown();
562}
563
564void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
565
566void UnwrappedLineParser::parsePPEndIf() {
567 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000568 parsePPUnknown();
569}
570
Manuel Klimek1abf7892013-01-04 23:34:14 +0000571void UnwrappedLineParser::parsePPDefine() {
572 nextToken();
573
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000574 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000575 parsePPUnknown();
576 return;
577 }
578 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000579 if (FormatTok->Tok.getKind() == tok::l_paren &&
580 FormatTok->WhitespaceRange.getBegin() ==
581 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000582 parseParens();
583 }
584 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000585 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000586
587 // Errors during a preprocessor directive can only affect the layout of the
588 // preprocessor directive, and thus we ignore them. An alternative approach
589 // would be to use the same approach we use on the file level (no
590 // re-indentation if there was a structural error) within the macro
591 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000592 parseFile();
593}
594
595void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000596 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000597 nextToken();
598 } while (!eof());
599 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000600}
601
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000602// Here we blacklist certain tokens that are not usually the first token in an
603// unwrapped line. This is used in attempt to distinguish macro calls without
604// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000605static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000606 // Semicolon can be a null-statement, l_square can be a start of a macro or
607 // a C++11 attribute, but this doesn't seem to be common.
608 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
609 Tok.isNot(tok::l_square) &&
610 // Tokens that can only be used as binary operators and a part of
611 // overloaded operator names.
612 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
613 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
614 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
615 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
616 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
617 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
618 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
619 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
620 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
621 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
622 Tok.isNot(tok::lesslessequal) &&
623 // Colon is used in labels, base class lists, initializer lists,
624 // range-based for loops, ternary operator, but should never be the
625 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000626 Tok.isNot(tok::colon) &&
627 // 'noexcept' is a trailing annotation.
628 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000629}
630
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000631void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000632 assert(!FormatTok->Tok.is(tok::l_brace));
633 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000634 case tok::at:
635 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000636 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000637 parseBracedList();
638 break;
639 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000640 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000641 case tok::objc_public:
642 case tok::objc_protected:
643 case tok::objc_package:
644 case tok::objc_private:
645 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000646 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000647 case tok::objc_implementation:
648 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000649 case tok::objc_protocol:
650 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000651 case tok::objc_end:
652 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000653 case tok::objc_optional:
654 case tok::objc_required:
655 nextToken();
656 addUnwrappedLine();
657 return;
Nico Weber33381f52015-02-07 01:57:32 +0000658 case tok::objc_try:
659 // This branch isn't strictly necessary (the kw_try case below would
660 // do this too after the tok::at is parsed above). But be explicit.
661 parseTryCatch();
662 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000663 default:
664 break;
665 }
666 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000667 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000668 nextToken();
669 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000670 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000671 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000672 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000673 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000674 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000675 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000676 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000677 break;
678 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000679 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000680 nextToken();
681 }
682 }
683 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000684 case tok::kw_namespace:
685 parseNamespace();
686 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000687 case tok::kw_inline:
688 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000689 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000690 parseNamespace();
691 return;
692 }
693 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000694 case tok::kw_public:
695 case tok::kw_protected:
696 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +0000697 if (Style.Language == FormatStyle::LK_Java ||
698 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000699 nextToken();
700 else
701 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000702 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000703 case tok::kw_if:
704 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000705 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000706 case tok::kw_for:
707 case tok::kw_while:
708 parseForOrWhileLoop();
709 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000710 case tok::kw_do:
711 parseDoWhile();
712 return;
713 case tok::kw_switch:
714 parseSwitch();
715 return;
716 case tok::kw_default:
717 nextToken();
718 parseLabel();
719 return;
720 case tok::kw_case:
721 parseCaseLabel();
722 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000723 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000724 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +0000725 parseTryCatch();
726 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000727 case tok::kw_extern:
728 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000729 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000730 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000731 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000732 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000733 addUnwrappedLine();
734 return;
735 }
736 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000737 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +0000738 case tok::kw_export:
739 if (Style.Language == FormatStyle::LK_JavaScript) {
740 parseJavaScriptEs6ImportExport();
741 return;
742 }
743 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000744 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +0000745 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +0000746 parseForOrWhileLoop();
747 return;
748 }
Daniel Jasper354aa512015-02-19 16:07:32 +0000749 if (Style.Language == FormatStyle::LK_JavaScript &&
750 FormatTok->is(Keywords.kw_import)) {
Daniel Jasperfca735c2015-02-19 16:14:18 +0000751 parseJavaScriptEs6ImportExport();
Daniel Jasper354aa512015-02-19 16:07:32 +0000752 return;
753 }
Daniel Jasper53395402015-04-07 15:04:40 +0000754 if (FormatTok->is(Keywords.kw_signals)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +0000755 nextToken();
756 if (FormatTok->is(tok::colon)) {
757 nextToken();
758 addUnwrappedLine();
759 }
Daniel Jasper53395402015-04-07 15:04:40 +0000760 return;
761 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000762 // In all other cases, parse the declaration.
763 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000764 default:
765 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000766 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000767 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000768 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000769 case tok::at:
770 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000771 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000772 parseBracedList();
773 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000774 case tok::kw_enum:
775 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000776 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000777 case tok::kw_typedef:
778 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000779 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
780 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000781 parseEnum();
782 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000783 case tok::kw_struct:
784 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000785 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000786 parseRecord();
787 // A record declaration or definition is always the start of a structural
788 // element.
789 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000790 case tok::period:
791 nextToken();
792 // In Java, classes have an implicit static member "class".
793 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
794 FormatTok->is(tok::kw_class))
795 nextToken();
796 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000797 case tok::semi:
798 nextToken();
799 addUnwrappedLine();
800 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000801 case tok::r_brace:
802 addUnwrappedLine();
803 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000804 case tok::l_paren:
805 parseParens();
806 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000807 case tok::caret:
808 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000809 if (FormatTok->Tok.isAnyIdentifier() ||
810 FormatTok->isSimpleTypeSpecifier())
811 nextToken();
812 if (FormatTok->is(tok::l_paren))
813 parseParens();
814 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000815 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000816 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000817 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000818 if (!tryToParseBracedList()) {
819 // A block outside of parentheses must be the last part of a
820 // structural element.
821 // FIXME: Figure out cases where this is not true, and add projections
822 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000823 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000824 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000825 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000826 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000827 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000828 return;
829 }
830 // Otherwise this was a braced init list, and the structural
831 // element continues.
832 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000833 case tok::kw_try:
834 // We arrive here when parsing function-try blocks.
835 parseTryCatch();
836 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000837 case tok::identifier: {
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000838 // Parse function literal unless 'function' is the first token in a line
839 // in which case this should be treated as a free-standing function.
Daniel Jasper9326f912015-05-05 08:40:32 +0000840 if (Style.Language == FormatStyle::LK_JavaScript &&
841 FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000842 tryToParseJSFunction();
843 break;
844 }
Daniel Jasper9326f912015-05-05 08:40:32 +0000845 if ((Style.Language == FormatStyle::LK_JavaScript ||
846 Style.Language == FormatStyle::LK_Java) &&
847 FormatTok->is(Keywords.kw_interface)) {
848 parseRecord();
849 break;
850 }
851
852 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +0000853 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +0000854 if (Line->Tokens.size() == 1 &&
855 // JS doesn't have macros, and within classes colons indicate fields,
856 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +0000857 Style.Language != FormatStyle::LK_JavaScript) {
858 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000859 parseLabel();
860 return;
861 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000862 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +0000863 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +0000864 bool FunctionLike = FormatTok->is(tok::l_paren);
865 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000866 parseParens();
Daniel Jaspere60cba12015-05-13 11:35:53 +0000867
868 bool FollowedByNewline =
869 CommentsBeforeNextToken.empty()
870 ? FormatTok->NewlinesBefore > 0
871 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
872
873 if (FollowedByNewline &&
Daniel Jasper680b09b2014-11-05 10:48:04 +0000874 (Text.size() >= 5 || FunctionLike) &&
875 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000876 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000877 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000878 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000879 }
880 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000881 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000882 case tok::equal:
883 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000884 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000885 parseBracedList();
886 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000887 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000888 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000889 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000890 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +0000891 case tok::kw_new:
892 parseNew();
893 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000894 default:
895 nextToken();
896 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000897 }
898 } while (!eof());
899}
900
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000901bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000902 // FIXME: This is a dirty way to access the previous token. Find a better
903 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000904 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000905 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
906 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000907 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000908 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000909 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000910 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000911 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000912 assert(FormatTok->is(tok::l_square));
913 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000914 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000915 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000916
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000917 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000918 if (FormatTok->isSimpleTypeSpecifier()) {
919 nextToken();
920 continue;
921 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000922 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000923 case tok::l_brace:
924 break;
925 case tok::l_paren:
926 parseParens();
927 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000928 case tok::amp:
929 case tok::star:
930 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000931 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000932 case tok::less:
933 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000934 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000935 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000936 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000937 nextToken();
938 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000939 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000940 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000941 nextToken();
942 break;
943 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000944 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000945 }
946 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000947 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000948 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000949 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000950}
951
952bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
953 nextToken();
954 if (FormatTok->is(tok::equal)) {
955 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000956 if (FormatTok->is(tok::r_square)) {
957 nextToken();
958 return true;
959 }
960 if (FormatTok->isNot(tok::comma))
961 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000962 nextToken();
963 } else if (FormatTok->is(tok::amp)) {
964 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000965 if (FormatTok->is(tok::r_square)) {
966 nextToken();
967 return true;
968 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000969 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
970 return false;
971 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000972 if (FormatTok->is(tok::comma))
973 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000974 } else if (FormatTok->is(tok::r_square)) {
975 nextToken();
976 return true;
977 }
978 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000979 if (FormatTok->is(tok::amp))
980 nextToken();
981 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
982 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000983 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000984 if (FormatTok->is(tok::ellipsis))
985 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000986 if (FormatTok->is(tok::comma)) {
987 nextToken();
988 } else if (FormatTok->is(tok::r_square)) {
989 nextToken();
990 return true;
991 } else {
992 return false;
993 }
994 } while (!eof());
995 return false;
996}
997
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000998void UnwrappedLineParser::tryToParseJSFunction() {
999 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001000
1001 // Consume function name.
1002 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001003 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001004
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001005 if (FormatTok->isNot(tok::l_paren))
1006 return;
1007 nextToken();
1008 while (FormatTok->isNot(tok::l_brace)) {
1009 // Err on the side of caution in order to avoid consuming the full file in
1010 // case of incomplete code.
1011 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
1012 tok::comment))
1013 return;
1014 nextToken();
1015 }
1016 parseChildBlock();
1017}
1018
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001019bool UnwrappedLineParser::tryToParseBracedList(bool ExpectClassBody) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001020 if (FormatTok->BlockKind == BK_Unknown)
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001021 calculateBraceTypes(ExpectClassBody);
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001022 assert(FormatTok->BlockKind != BK_Unknown);
1023 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001024 return false;
1025 parseBracedList();
1026 return true;
1027}
1028
Daniel Jasper015ed022013-09-13 09:20:45 +00001029bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1030 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001031 nextToken();
1032
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001033 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1034 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001035 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001036 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001037 FormatTok->is(Keywords.kw_function)) {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001038 tryToParseJSFunction();
1039 continue;
1040 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001041 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001042 case tok::caret:
1043 nextToken();
1044 if (FormatTok->is(tok::l_brace)) {
1045 parseChildBlock();
1046 }
1047 break;
1048 case tok::l_square:
1049 tryToParseLambda();
1050 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001051 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001052 // Assume there are no blocks inside a braced init list apart
1053 // from the ones we explicitly parse out (like lambdas).
1054 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001055 parseBracedList();
1056 break;
Daniel Jasperf46dec82015-03-31 14:34:15 +00001057 case tok::r_paren:
1058 // JavaScript can just have free standing methods and getters/setters in
1059 // object literals. Detect them by a "{" following ")".
1060 if (Style.Language == FormatStyle::LK_JavaScript) {
1061 nextToken();
1062 if (FormatTok->is(tok::l_brace))
1063 parseChildBlock();
1064 break;
1065 }
1066 nextToken();
1067 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001068 case tok::r_brace:
1069 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001070 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001071 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001072 HasError = true;
1073 if (!ContinueOnSemicolons)
1074 return !HasError;
1075 nextToken();
1076 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001077 case tok::comma:
1078 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001079 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001080 default:
1081 nextToken();
1082 break;
1083 }
1084 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001085 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001086}
1087
Daniel Jasperf7935112012-12-03 18:12:45 +00001088void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001089 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001090 nextToken();
1091 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001092 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001093 case tok::l_paren:
1094 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001095 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1096 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001097 break;
1098 case tok::r_paren:
1099 nextToken();
1100 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001101 case tok::r_brace:
1102 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1103 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001104 case tok::l_square:
1105 tryToParseLambda();
1106 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001107 case tok::l_brace:
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001108 if (!tryToParseBracedList())
Manuel Klimekf017dc02013-09-04 13:34:14 +00001109 parseChildBlock();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001110 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001111 case tok::at:
1112 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001113 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001114 parseBracedList();
1115 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001116 case tok::identifier:
1117 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001118 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001119 tryToParseJSFunction();
1120 else
1121 nextToken();
1122 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001123 default:
1124 nextToken();
1125 break;
1126 }
1127 } while (!eof());
1128}
1129
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001130void UnwrappedLineParser::parseSquare() {
1131 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1132 if (tryToParseLambda())
1133 return;
1134 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001135 switch (FormatTok->Tok.getKind()) {
1136 case tok::l_paren:
1137 parseParens();
1138 break;
1139 case tok::r_square:
1140 nextToken();
1141 return;
1142 case tok::r_brace:
1143 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1144 return;
1145 case tok::l_square:
1146 parseSquare();
1147 break;
1148 case tok::l_brace: {
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001149 if (!tryToParseBracedList())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001150 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001151 break;
1152 }
1153 case tok::at:
1154 nextToken();
1155 if (FormatTok->Tok.is(tok::l_brace))
1156 parseBracedList();
1157 break;
1158 default:
1159 nextToken();
1160 break;
1161 }
1162 } while (!eof());
1163}
1164
Daniel Jasperf7935112012-12-03 18:12:45 +00001165void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001166 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001167 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001168 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001169 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001170 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001171 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001172 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001173 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001174 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1175 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001176 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001177 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001178 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001179 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001180 } else {
1181 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001182 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001183 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001184 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001185 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001186 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001187 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1188 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001189 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001190 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001191 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001192 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001193 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001194 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001195 parseIfThenElse();
1196 } else {
1197 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001198 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001199 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001200 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001201 }
1202 } else if (NeedsUnwrappedLine) {
1203 addUnwrappedLine();
1204 }
1205}
1206
Daniel Jasper04a71a42014-05-08 11:58:24 +00001207void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001208 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001209 nextToken();
1210 bool NeedsUnwrappedLine = false;
1211 if (FormatTok->is(tok::colon)) {
1212 // We are in a function try block, what comes is an initializer list.
1213 nextToken();
1214 while (FormatTok->is(tok::identifier)) {
1215 nextToken();
1216 if (FormatTok->is(tok::l_paren))
1217 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001218 if (FormatTok->is(tok::comma))
1219 nextToken();
1220 }
1221 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001222 // Parse try with resource.
1223 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1224 parseParens();
1225 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001226 if (FormatTok->is(tok::l_brace)) {
1227 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1228 parseBlock(/*MustBeDeclaration=*/false);
1229 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1230 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1231 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1232 addUnwrappedLine();
1233 } else {
1234 NeedsUnwrappedLine = true;
1235 }
1236 } else if (!FormatTok->is(tok::kw_catch)) {
1237 // The C++ standard requires a compound-statement after a try.
1238 // If there's none, we try to assume there's a structuralElement
1239 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001240 addUnwrappedLine();
1241 ++Line->Level;
1242 parseStructuralElement();
1243 --Line->Level;
1244 }
Nico Weber33381f52015-02-07 01:57:32 +00001245 while (1) {
1246 if (FormatTok->is(tok::at))
1247 nextToken();
1248 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1249 tok::kw___finally) ||
1250 ((Style.Language == FormatStyle::LK_Java ||
1251 Style.Language == FormatStyle::LK_JavaScript) &&
1252 FormatTok->is(Keywords.kw_finally)) ||
1253 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1254 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1255 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001256 nextToken();
1257 while (FormatTok->isNot(tok::l_brace)) {
1258 if (FormatTok->is(tok::l_paren)) {
1259 parseParens();
1260 continue;
1261 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001262 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001263 return;
1264 nextToken();
1265 }
1266 NeedsUnwrappedLine = false;
1267 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1268 parseBlock(/*MustBeDeclaration=*/false);
1269 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1270 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1271 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1272 addUnwrappedLine();
1273 } else {
1274 NeedsUnwrappedLine = true;
1275 }
1276 }
1277 if (NeedsUnwrappedLine) {
1278 addUnwrappedLine();
1279 }
1280}
1281
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001282void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001283 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001284
1285 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001286 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001287 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001288 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001289 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001290 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001291 addUnwrappedLine();
1292
Daniel Jasper65ee3472013-07-31 23:16:02 +00001293 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1294 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1295 DeclarationScopeStack.size() > 1);
1296 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001297 // Munch the semicolon after a namespace. This is more common than one would
1298 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001299 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001300 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001301 addUnwrappedLine();
1302 }
1303 // FIXME: Add error handling.
1304}
1305
Daniel Jasper6acf5132015-03-12 14:44:29 +00001306void UnwrappedLineParser::parseNew() {
1307 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1308 nextToken();
1309 if (Style.Language != FormatStyle::LK_Java)
1310 return;
1311
1312 // In Java, we can parse everything up to the parens, which aren't optional.
1313 do {
1314 // There should not be a ;, { or } before the new's open paren.
1315 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1316 return;
1317
1318 // Consume the parens.
1319 if (FormatTok->is(tok::l_paren)) {
1320 parseParens();
1321
1322 // If there is a class body of an anonymous class, consume that as child.
1323 if (FormatTok->is(tok::l_brace))
1324 parseChildBlock();
1325 return;
1326 }
1327 nextToken();
1328 } while (!eof());
1329}
1330
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001331void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001332 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001333 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001334 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001335 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001336 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001337 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001338 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001339 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001340 addUnwrappedLine();
1341 } else {
1342 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001343 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001344 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001345 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001346 }
1347}
1348
Daniel Jasperf7935112012-12-03 18:12:45 +00001349void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001350 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001351 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001352 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001353 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001354 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001355 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1356 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001357 } else {
1358 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001359 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001360 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001361 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001362 }
1363
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001364 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001365 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001366 addUnwrappedLine();
1367 return;
1368 }
1369
Daniel Jasperf7935112012-12-03 18:12:45 +00001370 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001371 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001372}
1373
1374void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001375 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001376 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001377 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001378 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001379 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001380 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001381 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001382 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001383 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1384 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1385 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001386 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001387 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001388 parseStructuralElement();
1389 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001390 addUnwrappedLine();
1391 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001392 if (FormatTok->is(tok::semi))
1393 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001394 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001395 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001396 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001397}
1398
1399void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001400 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001401 // FIXME: fix handling of complex expressions here.
1402 do {
1403 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001404 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001405 parseLabel();
1406}
1407
1408void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001409 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001410 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001411 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001412 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001413 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001414 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001415 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001416 addUnwrappedLine();
1417 } else {
1418 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001419 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001420 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001421 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001422 }
1423}
1424
1425void UnwrappedLineParser::parseAccessSpecifier() {
1426 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001427 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001428 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001429 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001430 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001431 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001432 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001433 addUnwrappedLine();
1434}
1435
1436void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001437 // Won't be 'enum' for NS_ENUMs.
1438 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001439 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001440
Daniel Jasper2b41a822013-08-20 12:42:50 +00001441 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001442 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1443 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001444 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001445 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1446 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001447 nextToken();
1448 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001449 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001450 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001451 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001452 nextToken();
1453 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001454
1455 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001456 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001457 return;
1458 FormatTok->BlockKind = BK_Block;
1459
1460 if (Style.Language == FormatStyle::LK_Java) {
1461 // Java enums are different.
1462 parseJavaEnumBody();
1463 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001464 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001465
1466 // Parse enum body.
1467 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1468 if (HasError) {
1469 if (FormatTok->is(tok::semi))
1470 nextToken();
1471 addUnwrappedLine();
1472 }
1473
Manuel Klimek2cec0192013-01-21 19:17:52 +00001474 // We fall through to parsing a structural element afterwards, so that in
1475 // enum A {} n, m;
1476 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001477}
1478
1479void UnwrappedLineParser::parseJavaEnumBody() {
1480 // Determine whether the enum is simple, i.e. does not have a semicolon or
1481 // constants with class bodies. Simple enums can be formatted like braced
1482 // lists, contracted to a single line, etc.
1483 unsigned StoredPosition = Tokens->getPosition();
1484 bool IsSimple = true;
1485 FormatToken *Tok = Tokens->getNextToken();
1486 while (Tok) {
1487 if (Tok->is(tok::r_brace))
1488 break;
1489 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1490 IsSimple = false;
1491 break;
1492 }
1493 // FIXME: This will also mark enums with braces in the arguments to enum
1494 // constants as "not simple". This is probably fine in practice, though.
1495 Tok = Tokens->getNextToken();
1496 }
1497 FormatTok = Tokens->setPosition(StoredPosition);
1498
1499 if (IsSimple) {
1500 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001501 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001502 return;
1503 }
1504
1505 // Parse the body of a more complex enum.
1506 // First add a line for everything up to the "{".
1507 nextToken();
1508 addUnwrappedLine();
1509 ++Line->Level;
1510
1511 // Parse the enum constants.
1512 while (FormatTok) {
1513 if (FormatTok->is(tok::l_brace)) {
1514 // Parse the constant's class body.
1515 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1516 /*MunchSemi=*/false);
1517 } else if (FormatTok->is(tok::l_paren)) {
1518 parseParens();
1519 } else if (FormatTok->is(tok::comma)) {
1520 nextToken();
1521 addUnwrappedLine();
1522 } else if (FormatTok->is(tok::semi)) {
1523 nextToken();
1524 addUnwrappedLine();
1525 break;
1526 } else if (FormatTok->is(tok::r_brace)) {
1527 addUnwrappedLine();
1528 break;
1529 } else {
1530 nextToken();
1531 }
1532 }
1533
1534 // Parse the class body after the enum's ";" if any.
1535 parseLevel(/*HasOpeningBrace=*/true);
1536 nextToken();
1537 --Line->Level;
1538 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001539}
1540
Manuel Klimeke01bab52013-01-15 13:38:33 +00001541void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001542 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001543 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00001544
1545
1546 // The actual identifier can be a nested name specifier, and in macros
1547 // it is often token-pasted.
1548 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
1549 tok::kw___attribute, tok::kw___declspec,
1550 tok::kw_alignas) ||
1551 ((Style.Language == FormatStyle::LK_Java ||
1552 Style.Language == FormatStyle::LK_JavaScript) &&
1553 FormatTok->isOneOf(tok::period, tok::comma))) {
1554 bool IsNonMacroIdentifier =
1555 FormatTok->is(tok::identifier) &&
1556 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00001557 nextToken();
1558 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00001559 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001560 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00001561 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001562
Daniel Jasper04785d02015-05-06 14:03:02 +00001563 // Note that parsing away template declarations here leads to incorrectly
1564 // accepting function declarations as record declarations.
1565 // In general, we cannot solve this problem. Consider:
1566 // class A<int> B() {}
1567 // which can be a function definition or a class definition when B() is a
1568 // macro. If we find enough real-world cases where this is a problem, we
1569 // can parse for the 'template' keyword in the beginning of the statement,
1570 // and thus rule out the record production in case there is no template
1571 // (this would still leave us with an ambiguity between template function
1572 // and class declarations).
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001573 if (FormatTok->isOneOf(tok::colon, tok::less)) {
1574 while (!eof()) {
1575 if (FormatTok->is(tok::l_brace) &&
1576 !tryToParseBracedList(/*ExpectClassBody=*/true))
1577 break;
Daniel Jasper04785d02015-05-06 14:03:02 +00001578 if (FormatTok->Tok.is(tok::semi))
1579 return;
1580 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00001581 }
1582 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001583 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001584 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001585 addUnwrappedLine();
1586
Daniel Jasper240dfda2014-03-31 14:23:49 +00001587 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001588 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001589 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001590 // We fall through to parsing a structural element afterwards, so
1591 // class A {} n, m;
1592 // will end up in one unwrapped line.
Daniel Jasper9326f912015-05-05 08:40:32 +00001593 // This does not apply for Java and JavaScript.
Daniel Jasper6fa9ec72015-02-19 16:03:16 +00001594 if (Style.Language == FormatStyle::LK_Java ||
1595 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001596 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001597}
1598
Nico Weber8696a8d2013-01-09 21:15:03 +00001599void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001600 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001601 do
1602 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001603 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001604 nextToken(); // Skip '>'.
1605}
1606
1607void UnwrappedLineParser::parseObjCUntilAtEnd() {
1608 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001609 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001610 nextToken();
1611 addUnwrappedLine();
1612 break;
1613 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001614 if (FormatTok->is(tok::l_brace)) {
1615 parseBlock(/*MustBeDeclaration=*/false);
1616 // In ObjC interfaces, nothing should be following the "}".
1617 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001618 } else if (FormatTok->is(tok::r_brace)) {
1619 // Ignore stray "}". parseStructuralElement doesn't consume them.
1620 nextToken();
1621 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001622 } else {
1623 parseStructuralElement();
1624 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001625 } while (!eof());
1626}
1627
Nico Weber2ce0ac52013-01-09 23:25:37 +00001628void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001629 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001630 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001631
1632 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001633 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001634 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001635 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001636 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001637 // Skip category, if present.
1638 parseParens();
1639
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001640 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001641 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001642
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001643 if (FormatTok->Tok.is(tok::l_brace)) {
1644 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1645 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1646 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001647 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001648 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001649
1650 // With instance variables, this puts '}' on its own line. Without instance
1651 // variables, this ends the @interface line.
1652 addUnwrappedLine();
1653
Nico Weber8696a8d2013-01-09 21:15:03 +00001654 parseObjCUntilAtEnd();
1655}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001656
Nico Weber8696a8d2013-01-09 21:15:03 +00001657void UnwrappedLineParser::parseObjCProtocol() {
1658 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001659 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001660
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001661 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001662 parseObjCProtocolList();
1663
1664 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001665 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001666 nextToken();
1667 return addUnwrappedLine();
1668 }
1669
1670 addUnwrappedLine();
1671 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001672}
1673
Daniel Jasperfca735c2015-02-19 16:14:18 +00001674void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1675 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00001676 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00001677
Daniel Jasperec05fc72015-05-11 09:14:50 +00001678 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00001679 if (FormatTok->is(tok::kw_default))
1680 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00001681
1682 // Consume "function" and "default function", so that these get parsed as
1683 // free-standing JS functions, i.e. do not require a trailing semicolon.
Daniel Jasper668c7bb2015-05-11 09:03:10 +00001684 if (FormatTok->is(Keywords.kw_function)) {
1685 nextToken();
1686 return;
1687 }
1688
1689 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_var))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001690 return; // Fall through to parsing the corresponding structure.
1691
Daniel Jasper354aa512015-02-19 16:07:32 +00001692 if (FormatTok->is(tok::l_brace)) {
1693 FormatTok->BlockKind = BK_Block;
1694 parseBracedList();
1695 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001696
Daniel Jasper354aa512015-02-19 16:07:32 +00001697 while (!eof() && FormatTok->isNot(tok::semi) &&
1698 FormatTok->isNot(tok::l_brace)) {
1699 nextToken();
1700 }
1701}
1702
Daniel Jasper3b203a62013-09-05 16:05:56 +00001703LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1704 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001705 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1706 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1707 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1708 E = Line.Tokens.end();
1709 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001710 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001711 }
1712 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1713 E = Line.Tokens.end();
1714 I != E; ++I) {
1715 const UnwrappedLineNode &Node = *I;
1716 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1717 I = Node.Children.begin(),
1718 E = Node.Children.end();
1719 I != E; ++I) {
1720 printDebugInfo(*I, "\nChild: ");
1721 }
1722 }
1723 llvm::dbgs() << "\n";
1724}
1725
Daniel Jasperf7935112012-12-03 18:12:45 +00001726void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001727 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001728 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001729 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001730 if (CurrentLines == &Lines)
1731 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001732 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001733 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001734 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001735 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001736 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001737 I = PreprocessorDirectives.begin(),
1738 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001739 I != E; ++I) {
1740 CurrentLines->push_back(*I);
1741 }
1742 PreprocessorDirectives.clear();
1743 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001744}
1745
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001746bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001747
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001748bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001749 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1750 FormatTok.NewlinesBefore > 0;
1751}
1752
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001753void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1754 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001755 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001756 I = CommentsBeforeNextToken.begin(),
1757 E = CommentsBeforeNextToken.end();
1758 I != E; ++I) {
Daniel Jaspere60cba12015-05-13 11:35:53 +00001759 if (isOnNewLine(**I) && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001760 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001761 pushToken(*I);
1762 }
Daniel Jaspere60cba12015-05-13 11:35:53 +00001763 if (NewlineBeforeNext && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001764 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001765 CommentsBeforeNextToken.clear();
1766}
1767
Daniel Jasperf7935112012-12-03 18:12:45 +00001768void UnwrappedLineParser::nextToken() {
1769 if (eof())
1770 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001771 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001772 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001773 readToken();
1774}
1775
1776void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001777 bool CommentsInCurrentLine = true;
1778 do {
1779 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001780 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001781 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1782 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001783 // If there is an unfinished unwrapped line, we flush the preprocessor
1784 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001785 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001786 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001787 // Comments stored before the preprocessor directive need to be output
1788 // before the preprocessor directive, at the same level as the
1789 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001790 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001791 parsePPDirective();
1792 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001793 while (FormatTok->Type == TT_ConflictStart ||
1794 FormatTok->Type == TT_ConflictEnd ||
1795 FormatTok->Type == TT_ConflictAlternative) {
1796 if (FormatTok->Type == TT_ConflictStart) {
1797 conditionalCompilationStart(/*Unreachable=*/false);
1798 } else if (FormatTok->Type == TT_ConflictAlternative) {
1799 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001800 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001801 conditionalCompilationEnd();
1802 }
1803 FormatTok = Tokens->getNextToken();
1804 FormatTok->MustBreakBefore = true;
1805 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001806
1807 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1808 !Line->InPPDirective) {
1809 continue;
1810 }
1811
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001812 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001813 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001814 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001815 CommentsInCurrentLine = false;
1816 }
1817 if (CommentsInCurrentLine) {
1818 pushToken(FormatTok);
1819 } else {
1820 CommentsBeforeNextToken.push_back(FormatTok);
1821 }
1822 } while (!eof());
1823}
1824
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001825void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001826 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001827 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001828 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001829 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001830 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001831}
1832
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001833} // end namespace format
1834} // end namespace clang