blob: 6dee251a918a622b53b8a8591523a72d873e3a61 [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
Manuel Klimekab419912013-05-23 09:41:43 +0000305void UnwrappedLineParser::calculateBraceTypes() {
306 // 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 =
351 NextTok->isOneOf(tok::comma, tok::semi, 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 Jasper91b032a2014-05-22 12:46:38 +0000354 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000355 }
356 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000357 Tok->BlockKind = BK_BracedInit;
358 LBraceStack.back()->BlockKind = BK_BracedInit;
359 } else {
360 Tok->BlockKind = BK_Block;
361 LBraceStack.back()->BlockKind = BK_Block;
362 }
Manuel Klimekab419912013-05-23 09:41:43 +0000363 }
364 LBraceStack.pop_back();
365 }
366 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000367 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000368 case tok::semi:
369 case tok::kw_if:
370 case tok::kw_while:
371 case tok::kw_for:
372 case tok::kw_switch:
373 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000374 case tok::kw___try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000375 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000376 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000377 break;
378 default:
379 break;
380 }
381 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000382 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000383 // Assume other blocks for all unclosed opening braces.
384 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000385 if (LBraceStack[i]->BlockKind == BK_Unknown)
386 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000387 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000388
Manuel Klimekab419912013-05-23 09:41:43 +0000389 FormatTok = Tokens->setPosition(StoredPosition);
390}
391
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000392void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
393 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000394 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000395 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000396 nextToken();
397
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000398 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000399
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000400 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
401 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000402 if (AddLevel)
403 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000404 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000405
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000406 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000407 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000408 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000409 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000410
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000411 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000412 if (MunchSemi && FormatTok->Tok.is(tok::semi))
413 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000414 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000415}
416
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000417static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000418 // FIXME: Closure-library specific stuff should not be hard-coded but be
419 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000420 if (Line.Tokens.size() < 4)
421 return false;
422 auto I = Line.Tokens.begin();
423 if (I->Tok->TokenText != "goog")
424 return false;
425 ++I;
426 if (I->Tok->isNot(tok::period))
427 return false;
428 ++I;
429 if (I->Tok->TokenText != "scope")
430 return false;
431 ++I;
432 return I->Tok->is(tok::l_paren);
433}
434
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000435static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
436 const FormatToken &InitialToken) {
437 switch (Style.BreakBeforeBraces) {
438 case FormatStyle::BS_Linux:
439 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
440 case FormatStyle::BS_Allman:
441 case FormatStyle::BS_GNU:
442 return true;
443 default:
444 return false;
445 }
446}
447
Manuel Klimek516e0542013-09-04 13:25:30 +0000448void UnwrappedLineParser::parseChildBlock() {
449 FormatTok->BlockKind = BK_Block;
450 nextToken();
451 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000452 bool GoogScope =
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000453 Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000454 ScopedLineState LineState(*this);
455 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
456 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000457 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000458 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000459 flushComments(isOnNewLine(*FormatTok));
Daniel Jasper4a39c842014-05-06 13:54:10 +0000460 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000461 }
462 nextToken();
463}
464
Daniel Jasperf7935112012-12-03 18:12:45 +0000465void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000466 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000467 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000468 nextToken();
469
Craig Topper2145bc02014-05-09 08:15:10 +0000470 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000471 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000472 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000473 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000474
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000475 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000476 case tok::pp_define:
477 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000478 return;
479 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000480 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000481 break;
482 case tok::pp_ifdef:
483 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000484 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000485 break;
486 case tok::pp_else:
487 parsePPElse();
488 break;
489 case tok::pp_elif:
490 parsePPElIf();
491 break;
492 case tok::pp_endif:
493 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000494 break;
495 default:
496 parsePPUnknown();
497 break;
498 }
499}
500
Manuel Klimek68b03042014-04-14 09:14:11 +0000501void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
502 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000503 PPStack.push_back(PP_Unreachable);
504 else
505 PPStack.push_back(PP_Conditional);
506}
507
Manuel Klimek68b03042014-04-14 09:14:11 +0000508void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000509 ++PPBranchLevel;
510 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
511 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
512 PPLevelBranchIndex.push_back(0);
513 PPLevelBranchCount.push_back(0);
514 }
515 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000516 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
517 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000518}
519
Manuel Klimek68b03042014-04-14 09:14:11 +0000520void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000521 if (!PPStack.empty())
522 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000523 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
524 if (!PPChainBranchIndex.empty())
525 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000526 conditionalCompilationCondition(
527 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
528 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000529}
530
Manuel Klimek68b03042014-04-14 09:14:11 +0000531void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000532 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
533 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
534 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000535 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
536 }
537 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000538 // Guard against #endif's without #if.
539 if (PPBranchLevel > 0)
540 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000541 if (!PPChainBranchIndex.empty())
542 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000543 if (!PPStack.empty())
544 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000545}
546
547void UnwrappedLineParser::parsePPIf(bool IfDef) {
548 nextToken();
549 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Daniel Jasper9d22bcc2015-01-19 10:51:05 +0000550 FormatTok->Tok.getLiteralData() != nullptr &&
Manuel Klimek68b03042014-04-14 09:14:11 +0000551 StringRef(FormatTok->Tok.getLiteralData(),
552 FormatTok->Tok.getLength()) == "0") ||
553 FormatTok->Tok.is(tok::kw_false);
554 conditionalCompilationStart(!IfDef && IsLiteralFalse);
555 parsePPUnknown();
556}
557
558void UnwrappedLineParser::parsePPElse() {
559 conditionalCompilationAlternative();
560 parsePPUnknown();
561}
562
563void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
564
565void UnwrappedLineParser::parsePPEndIf() {
566 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000567 parsePPUnknown();
568}
569
Manuel Klimek1abf7892013-01-04 23:34:14 +0000570void UnwrappedLineParser::parsePPDefine() {
571 nextToken();
572
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000573 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000574 parsePPUnknown();
575 return;
576 }
577 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000578 if (FormatTok->Tok.getKind() == tok::l_paren &&
579 FormatTok->WhitespaceRange.getBegin() ==
580 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000581 parseParens();
582 }
583 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000584 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000585
586 // Errors during a preprocessor directive can only affect the layout of the
587 // preprocessor directive, and thus we ignore them. An alternative approach
588 // would be to use the same approach we use on the file level (no
589 // re-indentation if there was a structural error) within the macro
590 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000591 parseFile();
592}
593
594void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000595 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000596 nextToken();
597 } while (!eof());
598 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000599}
600
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000601// Here we blacklist certain tokens that are not usually the first token in an
602// unwrapped line. This is used in attempt to distinguish macro calls without
603// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000604static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000605 // Semicolon can be a null-statement, l_square can be a start of a macro or
606 // a C++11 attribute, but this doesn't seem to be common.
607 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
608 Tok.isNot(tok::l_square) &&
609 // Tokens that can only be used as binary operators and a part of
610 // overloaded operator names.
611 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
612 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
613 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
614 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
615 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
616 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
617 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
618 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
619 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
620 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
621 Tok.isNot(tok::lesslessequal) &&
622 // Colon is used in labels, base class lists, initializer lists,
623 // range-based for loops, ternary operator, but should never be the
624 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000625 Tok.isNot(tok::colon) &&
626 // 'noexcept' is a trailing annotation.
627 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000628}
629
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000630void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000631 assert(!FormatTok->Tok.is(tok::l_brace));
632 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000633 case tok::at:
634 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000635 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000636 parseBracedList();
637 break;
638 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000639 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000640 case tok::objc_public:
641 case tok::objc_protected:
642 case tok::objc_package:
643 case tok::objc_private:
644 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000645 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000646 case tok::objc_implementation:
647 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000648 case tok::objc_protocol:
649 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000650 case tok::objc_end:
651 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000652 case tok::objc_optional:
653 case tok::objc_required:
654 nextToken();
655 addUnwrappedLine();
656 return;
Nico Weber33381f52015-02-07 01:57:32 +0000657 case tok::objc_try:
658 // This branch isn't strictly necessary (the kw_try case below would
659 // do this too after the tok::at is parsed above). But be explicit.
660 parseTryCatch();
661 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000662 default:
663 break;
664 }
665 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000666 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000667 nextToken();
668 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000669 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000670 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000671 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000672 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000673 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000674 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000675 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000676 break;
677 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000678 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000679 nextToken();
680 }
681 }
682 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000683 case tok::kw_namespace:
684 parseNamespace();
685 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000686 case tok::kw_inline:
687 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000688 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000689 parseNamespace();
690 return;
691 }
692 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000693 case tok::kw_public:
694 case tok::kw_protected:
695 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +0000696 if (Style.Language == FormatStyle::LK_Java ||
697 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000698 nextToken();
699 else
700 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000701 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000702 case tok::kw_if:
703 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000704 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000705 case tok::kw_for:
706 case tok::kw_while:
707 parseForOrWhileLoop();
708 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000709 case tok::kw_do:
710 parseDoWhile();
711 return;
712 case tok::kw_switch:
713 parseSwitch();
714 return;
715 case tok::kw_default:
716 nextToken();
717 parseLabel();
718 return;
719 case tok::kw_case:
720 parseCaseLabel();
721 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000722 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000723 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +0000724 parseTryCatch();
725 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000726 case tok::kw_extern:
727 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000728 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000729 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000730 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000731 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000732 addUnwrappedLine();
733 return;
734 }
735 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000736 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +0000737 case tok::kw_export:
738 if (Style.Language == FormatStyle::LK_JavaScript) {
739 parseJavaScriptEs6ImportExport();
740 return;
741 }
742 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000743 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +0000744 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +0000745 parseForOrWhileLoop();
746 return;
747 }
Daniel Jasper354aa512015-02-19 16:07:32 +0000748 if (Style.Language == FormatStyle::LK_JavaScript &&
749 FormatTok->is(Keywords.kw_import)) {
Daniel Jasperfca735c2015-02-19 16:14:18 +0000750 parseJavaScriptEs6ImportExport();
Daniel Jasper354aa512015-02-19 16:07:32 +0000751 return;
752 }
Daniel Jasper53395402015-04-07 15:04:40 +0000753 if (FormatTok->is(Keywords.kw_signals)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +0000754 nextToken();
755 if (FormatTok->is(tok::colon)) {
756 nextToken();
757 addUnwrappedLine();
758 }
Daniel Jasper53395402015-04-07 15:04:40 +0000759 return;
760 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000761 // In all other cases, parse the declaration.
762 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000763 default:
764 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000765 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000766 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000767 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000768 case tok::at:
769 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000770 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000771 parseBracedList();
772 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000773 case tok::kw_enum:
774 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000775 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000776 case tok::kw_typedef:
777 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000778 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
779 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000780 parseEnum();
781 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000782 case tok::kw_struct:
783 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000784 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000785 parseRecord();
786 // A record declaration or definition is always the start of a structural
787 // element.
788 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000789 case tok::period:
790 nextToken();
791 // In Java, classes have an implicit static member "class".
792 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
793 FormatTok->is(tok::kw_class))
794 nextToken();
795 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000796 case tok::semi:
797 nextToken();
798 addUnwrappedLine();
799 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000800 case tok::r_brace:
801 addUnwrappedLine();
802 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000803 case tok::l_paren:
804 parseParens();
805 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000806 case tok::caret:
807 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000808 if (FormatTok->Tok.isAnyIdentifier() ||
809 FormatTok->isSimpleTypeSpecifier())
810 nextToken();
811 if (FormatTok->is(tok::l_paren))
812 parseParens();
813 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000814 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000815 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000816 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000817 if (!tryToParseBracedList()) {
818 // A block outside of parentheses must be the last part of a
819 // structural element.
820 // FIXME: Figure out cases where this is not true, and add projections
821 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000822 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000823 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000824 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000825 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000826 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000827 return;
828 }
829 // Otherwise this was a braced init list, and the structural
830 // element continues.
831 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000832 case tok::kw_try:
833 // We arrive here when parsing function-try blocks.
834 parseTryCatch();
835 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000836 case tok::identifier: {
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000837 // Parse function literal unless 'function' is the first token in a line
838 // in which case this should be treated as a free-standing function.
Daniel Jasper9326f912015-05-05 08:40:32 +0000839 if (Style.Language == FormatStyle::LK_JavaScript &&
840 FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000841 tryToParseJSFunction();
842 break;
843 }
Daniel Jasper9326f912015-05-05 08:40:32 +0000844 if ((Style.Language == FormatStyle::LK_JavaScript ||
845 Style.Language == FormatStyle::LK_Java) &&
846 FormatTok->is(Keywords.kw_interface)) {
847 parseRecord();
848 break;
849 }
850
851 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +0000852 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +0000853 if (Line->Tokens.size() == 1 &&
854 // JS doesn't have macros, and within classes colons indicate fields,
855 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +0000856 Style.Language != FormatStyle::LK_JavaScript) {
857 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000858 parseLabel();
859 return;
860 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000861 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +0000862 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +0000863 bool FunctionLike = FormatTok->is(tok::l_paren);
864 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000865 parseParens();
Daniel Jasper680b09b2014-11-05 10:48:04 +0000866 if (FormatTok->NewlinesBefore > 0 &&
867 (Text.size() >= 5 || FunctionLike) &&
868 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000869 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000870 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000871 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000872 }
873 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000874 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000875 case tok::equal:
876 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000877 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000878 parseBracedList();
879 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000880 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000881 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000882 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000883 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +0000884 case tok::kw_new:
885 parseNew();
886 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000887 default:
888 nextToken();
889 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000890 }
891 } while (!eof());
892}
893
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000894bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000895 // FIXME: This is a dirty way to access the previous token. Find a better
896 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000897 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000898 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
899 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000900 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000901 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000902 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000903 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000904 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000905 assert(FormatTok->is(tok::l_square));
906 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000907 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000908 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000909
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000910 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000911 if (FormatTok->isSimpleTypeSpecifier()) {
912 nextToken();
913 continue;
914 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000915 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000916 case tok::l_brace:
917 break;
918 case tok::l_paren:
919 parseParens();
920 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000921 case tok::amp:
922 case tok::star:
923 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000924 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000925 case tok::less:
926 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000927 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000928 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000929 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000930 nextToken();
931 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000932 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000933 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000934 nextToken();
935 break;
936 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000937 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000938 }
939 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000940 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000941 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000942 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000943}
944
945bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
946 nextToken();
947 if (FormatTok->is(tok::equal)) {
948 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000949 if (FormatTok->is(tok::r_square)) {
950 nextToken();
951 return true;
952 }
953 if (FormatTok->isNot(tok::comma))
954 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000955 nextToken();
956 } else if (FormatTok->is(tok::amp)) {
957 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000958 if (FormatTok->is(tok::r_square)) {
959 nextToken();
960 return true;
961 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000962 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
963 return false;
964 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000965 if (FormatTok->is(tok::comma))
966 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000967 } else if (FormatTok->is(tok::r_square)) {
968 nextToken();
969 return true;
970 }
971 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000972 if (FormatTok->is(tok::amp))
973 nextToken();
974 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
975 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000976 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000977 if (FormatTok->is(tok::ellipsis))
978 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000979 if (FormatTok->is(tok::comma)) {
980 nextToken();
981 } else if (FormatTok->is(tok::r_square)) {
982 nextToken();
983 return true;
984 } else {
985 return false;
986 }
987 } while (!eof());
988 return false;
989}
990
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000991void UnwrappedLineParser::tryToParseJSFunction() {
992 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000993
994 // Consume function name.
995 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +0000996 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000997
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000998 if (FormatTok->isNot(tok::l_paren))
999 return;
1000 nextToken();
1001 while (FormatTok->isNot(tok::l_brace)) {
1002 // Err on the side of caution in order to avoid consuming the full file in
1003 // case of incomplete code.
1004 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
1005 tok::comment))
1006 return;
1007 nextToken();
1008 }
1009 parseChildBlock();
1010}
1011
Manuel Klimekab419912013-05-23 09:41:43 +00001012bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001013 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +00001014 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001015 assert(FormatTok->BlockKind != BK_Unknown);
1016 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001017 return false;
1018 parseBracedList();
1019 return true;
1020}
1021
Daniel Jasper015ed022013-09-13 09:20:45 +00001022bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1023 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001024 nextToken();
1025
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001026 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1027 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001028 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001029 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001030 FormatTok->is(Keywords.kw_function)) {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001031 tryToParseJSFunction();
1032 continue;
1033 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001034 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001035 case tok::caret:
1036 nextToken();
1037 if (FormatTok->is(tok::l_brace)) {
1038 parseChildBlock();
1039 }
1040 break;
1041 case tok::l_square:
1042 tryToParseLambda();
1043 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001044 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001045 // Assume there are no blocks inside a braced init list apart
1046 // from the ones we explicitly parse out (like lambdas).
1047 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001048 parseBracedList();
1049 break;
Daniel Jasperf46dec82015-03-31 14:34:15 +00001050 case tok::r_paren:
1051 // JavaScript can just have free standing methods and getters/setters in
1052 // object literals. Detect them by a "{" following ")".
1053 if (Style.Language == FormatStyle::LK_JavaScript) {
1054 nextToken();
1055 if (FormatTok->is(tok::l_brace))
1056 parseChildBlock();
1057 break;
1058 }
1059 nextToken();
1060 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001061 case tok::r_brace:
1062 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001063 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001064 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001065 HasError = true;
1066 if (!ContinueOnSemicolons)
1067 return !HasError;
1068 nextToken();
1069 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001070 case tok::comma:
1071 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001072 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001073 default:
1074 nextToken();
1075 break;
1076 }
1077 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001078 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001079}
1080
Daniel Jasperf7935112012-12-03 18:12:45 +00001081void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001082 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001083 nextToken();
1084 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001085 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001086 case tok::l_paren:
1087 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001088 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1089 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001090 break;
1091 case tok::r_paren:
1092 nextToken();
1093 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001094 case tok::r_brace:
1095 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1096 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001097 case tok::l_square:
1098 tryToParseLambda();
1099 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001100 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001101 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001102 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001103 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001104 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001105 case tok::at:
1106 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001107 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001108 parseBracedList();
1109 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001110 case tok::identifier:
1111 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001112 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001113 tryToParseJSFunction();
1114 else
1115 nextToken();
1116 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001117 default:
1118 nextToken();
1119 break;
1120 }
1121 } while (!eof());
1122}
1123
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001124void UnwrappedLineParser::parseSquare() {
1125 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1126 if (tryToParseLambda())
1127 return;
1128 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001129 switch (FormatTok->Tok.getKind()) {
1130 case tok::l_paren:
1131 parseParens();
1132 break;
1133 case tok::r_square:
1134 nextToken();
1135 return;
1136 case tok::r_brace:
1137 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1138 return;
1139 case tok::l_square:
1140 parseSquare();
1141 break;
1142 case tok::l_brace: {
1143 if (!tryToParseBracedList()) {
1144 parseChildBlock();
1145 }
1146 break;
1147 }
1148 case tok::at:
1149 nextToken();
1150 if (FormatTok->Tok.is(tok::l_brace))
1151 parseBracedList();
1152 break;
1153 default:
1154 nextToken();
1155 break;
1156 }
1157 } while (!eof());
1158}
1159
Daniel Jasperf7935112012-12-03 18:12:45 +00001160void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001161 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001162 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001163 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001164 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001165 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001166 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001167 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001168 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001169 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1170 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001171 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001172 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001173 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001174 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001175 } else {
1176 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001177 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001178 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001179 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001180 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001181 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001182 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1183 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001184 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001185 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001186 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001187 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001188 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001189 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001190 parseIfThenElse();
1191 } else {
1192 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001193 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001194 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001195 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001196 }
1197 } else if (NeedsUnwrappedLine) {
1198 addUnwrappedLine();
1199 }
1200}
1201
Daniel Jasper04a71a42014-05-08 11:58:24 +00001202void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001203 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001204 nextToken();
1205 bool NeedsUnwrappedLine = false;
1206 if (FormatTok->is(tok::colon)) {
1207 // We are in a function try block, what comes is an initializer list.
1208 nextToken();
1209 while (FormatTok->is(tok::identifier)) {
1210 nextToken();
1211 if (FormatTok->is(tok::l_paren))
1212 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001213 if (FormatTok->is(tok::comma))
1214 nextToken();
1215 }
1216 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001217 // Parse try with resource.
1218 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1219 parseParens();
1220 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001221 if (FormatTok->is(tok::l_brace)) {
1222 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1223 parseBlock(/*MustBeDeclaration=*/false);
1224 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1225 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1226 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1227 addUnwrappedLine();
1228 } else {
1229 NeedsUnwrappedLine = true;
1230 }
1231 } else if (!FormatTok->is(tok::kw_catch)) {
1232 // The C++ standard requires a compound-statement after a try.
1233 // If there's none, we try to assume there's a structuralElement
1234 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001235 addUnwrappedLine();
1236 ++Line->Level;
1237 parseStructuralElement();
1238 --Line->Level;
1239 }
Nico Weber33381f52015-02-07 01:57:32 +00001240 while (1) {
1241 if (FormatTok->is(tok::at))
1242 nextToken();
1243 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1244 tok::kw___finally) ||
1245 ((Style.Language == FormatStyle::LK_Java ||
1246 Style.Language == FormatStyle::LK_JavaScript) &&
1247 FormatTok->is(Keywords.kw_finally)) ||
1248 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1249 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1250 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001251 nextToken();
1252 while (FormatTok->isNot(tok::l_brace)) {
1253 if (FormatTok->is(tok::l_paren)) {
1254 parseParens();
1255 continue;
1256 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001257 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001258 return;
1259 nextToken();
1260 }
1261 NeedsUnwrappedLine = false;
1262 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1263 parseBlock(/*MustBeDeclaration=*/false);
1264 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1265 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1266 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1267 addUnwrappedLine();
1268 } else {
1269 NeedsUnwrappedLine = true;
1270 }
1271 }
1272 if (NeedsUnwrappedLine) {
1273 addUnwrappedLine();
1274 }
1275}
1276
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001277void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001278 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001279
1280 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001281 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001282 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001283 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001284 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001285 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001286 addUnwrappedLine();
1287
Daniel Jasper65ee3472013-07-31 23:16:02 +00001288 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1289 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1290 DeclarationScopeStack.size() > 1);
1291 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001292 // Munch the semicolon after a namespace. This is more common than one would
1293 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001294 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001295 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001296 addUnwrappedLine();
1297 }
1298 // FIXME: Add error handling.
1299}
1300
Daniel Jasper6acf5132015-03-12 14:44:29 +00001301void UnwrappedLineParser::parseNew() {
1302 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1303 nextToken();
1304 if (Style.Language != FormatStyle::LK_Java)
1305 return;
1306
1307 // In Java, we can parse everything up to the parens, which aren't optional.
1308 do {
1309 // There should not be a ;, { or } before the new's open paren.
1310 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1311 return;
1312
1313 // Consume the parens.
1314 if (FormatTok->is(tok::l_paren)) {
1315 parseParens();
1316
1317 // If there is a class body of an anonymous class, consume that as child.
1318 if (FormatTok->is(tok::l_brace))
1319 parseChildBlock();
1320 return;
1321 }
1322 nextToken();
1323 } while (!eof());
1324}
1325
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001326void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001327 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001328 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001329 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001330 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001331 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001332 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001333 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001334 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001335 addUnwrappedLine();
1336 } else {
1337 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001338 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001339 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001340 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001341 }
1342}
1343
Daniel Jasperf7935112012-12-03 18:12:45 +00001344void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001345 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001346 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001347 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001348 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001349 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001350 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1351 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001352 } else {
1353 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001354 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001355 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001356 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001357 }
1358
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001359 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001360 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001361 addUnwrappedLine();
1362 return;
1363 }
1364
Daniel Jasperf7935112012-12-03 18:12:45 +00001365 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001366 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001367}
1368
1369void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001370 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001371 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001372 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001373 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001374 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001375 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001376 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001377 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001378 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1379 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1380 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001381 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001382 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001383 parseStructuralElement();
1384 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001385 addUnwrappedLine();
1386 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001387 if (FormatTok->is(tok::semi))
1388 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001389 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001390 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001391 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001392}
1393
1394void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001395 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001396 // FIXME: fix handling of complex expressions here.
1397 do {
1398 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001399 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001400 parseLabel();
1401}
1402
1403void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001404 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001405 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001406 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001407 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001408 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001409 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001410 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001411 addUnwrappedLine();
1412 } else {
1413 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001414 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001415 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001416 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001417 }
1418}
1419
1420void UnwrappedLineParser::parseAccessSpecifier() {
1421 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001422 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001423 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001424 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001425 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001426 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001427 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001428 addUnwrappedLine();
1429}
1430
1431void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001432 // Won't be 'enum' for NS_ENUMs.
1433 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001434 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001435
Daniel Jasper2b41a822013-08-20 12:42:50 +00001436 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001437 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1438 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001439 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001440 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1441 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001442 nextToken();
1443 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001444 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001445 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001446 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001447 nextToken();
1448 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001449
1450 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001451 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001452 return;
1453 FormatTok->BlockKind = BK_Block;
1454
1455 if (Style.Language == FormatStyle::LK_Java) {
1456 // Java enums are different.
1457 parseJavaEnumBody();
1458 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001459 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001460
1461 // Parse enum body.
1462 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1463 if (HasError) {
1464 if (FormatTok->is(tok::semi))
1465 nextToken();
1466 addUnwrappedLine();
1467 }
1468
Manuel Klimek2cec0192013-01-21 19:17:52 +00001469 // We fall through to parsing a structural element afterwards, so that in
1470 // enum A {} n, m;
1471 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001472}
1473
1474void UnwrappedLineParser::parseJavaEnumBody() {
1475 // Determine whether the enum is simple, i.e. does not have a semicolon or
1476 // constants with class bodies. Simple enums can be formatted like braced
1477 // lists, contracted to a single line, etc.
1478 unsigned StoredPosition = Tokens->getPosition();
1479 bool IsSimple = true;
1480 FormatToken *Tok = Tokens->getNextToken();
1481 while (Tok) {
1482 if (Tok->is(tok::r_brace))
1483 break;
1484 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1485 IsSimple = false;
1486 break;
1487 }
1488 // FIXME: This will also mark enums with braces in the arguments to enum
1489 // constants as "not simple". This is probably fine in practice, though.
1490 Tok = Tokens->getNextToken();
1491 }
1492 FormatTok = Tokens->setPosition(StoredPosition);
1493
1494 if (IsSimple) {
1495 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001496 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001497 return;
1498 }
1499
1500 // Parse the body of a more complex enum.
1501 // First add a line for everything up to the "{".
1502 nextToken();
1503 addUnwrappedLine();
1504 ++Line->Level;
1505
1506 // Parse the enum constants.
1507 while (FormatTok) {
1508 if (FormatTok->is(tok::l_brace)) {
1509 // Parse the constant's class body.
1510 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1511 /*MunchSemi=*/false);
1512 } else if (FormatTok->is(tok::l_paren)) {
1513 parseParens();
1514 } else if (FormatTok->is(tok::comma)) {
1515 nextToken();
1516 addUnwrappedLine();
1517 } else if (FormatTok->is(tok::semi)) {
1518 nextToken();
1519 addUnwrappedLine();
1520 break;
1521 } else if (FormatTok->is(tok::r_brace)) {
1522 addUnwrappedLine();
1523 break;
1524 } else {
1525 nextToken();
1526 }
1527 }
1528
1529 // Parse the class body after the enum's ";" if any.
1530 parseLevel(/*HasOpeningBrace=*/true);
1531 nextToken();
1532 --Line->Level;
1533 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001534}
1535
Manuel Klimeke01bab52013-01-15 13:38:33 +00001536void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001537 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001538 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00001539
1540
1541 // The actual identifier can be a nested name specifier, and in macros
1542 // it is often token-pasted.
1543 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
1544 tok::kw___attribute, tok::kw___declspec,
1545 tok::kw_alignas) ||
1546 ((Style.Language == FormatStyle::LK_Java ||
1547 Style.Language == FormatStyle::LK_JavaScript) &&
1548 FormatTok->isOneOf(tok::period, tok::comma))) {
1549 bool IsNonMacroIdentifier =
1550 FormatTok->is(tok::identifier) &&
1551 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00001552 nextToken();
1553 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00001554 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001555 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00001556 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001557
Daniel Jasper04785d02015-05-06 14:03:02 +00001558 // Note that parsing away template declarations here leads to incorrectly
1559 // accepting function declarations as record declarations.
1560 // In general, we cannot solve this problem. Consider:
1561 // class A<int> B() {}
1562 // which can be a function definition or a class definition when B() is a
1563 // macro. If we find enough real-world cases where this is a problem, we
1564 // can parse for the 'template' keyword in the beginning of the statement,
1565 // and thus rule out the record production in case there is no template
1566 // (this would still leave us with an ambiguity between template function
1567 // and class declarations).
1568 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1569 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1570 if (FormatTok->Tok.is(tok::semi))
1571 return;
1572 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00001573 }
1574 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001575 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001576 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001577 addUnwrappedLine();
1578
Daniel Jasper240dfda2014-03-31 14:23:49 +00001579 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001580 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001581 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001582 // We fall through to parsing a structural element afterwards, so
1583 // class A {} n, m;
1584 // will end up in one unwrapped line.
Daniel Jasper9326f912015-05-05 08:40:32 +00001585 // This does not apply for Java and JavaScript.
Daniel Jasper6fa9ec72015-02-19 16:03:16 +00001586 if (Style.Language == FormatStyle::LK_Java ||
1587 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001588 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001589}
1590
Nico Weber8696a8d2013-01-09 21:15:03 +00001591void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001592 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001593 do
1594 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001595 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001596 nextToken(); // Skip '>'.
1597}
1598
1599void UnwrappedLineParser::parseObjCUntilAtEnd() {
1600 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001601 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001602 nextToken();
1603 addUnwrappedLine();
1604 break;
1605 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001606 if (FormatTok->is(tok::l_brace)) {
1607 parseBlock(/*MustBeDeclaration=*/false);
1608 // In ObjC interfaces, nothing should be following the "}".
1609 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001610 } else if (FormatTok->is(tok::r_brace)) {
1611 // Ignore stray "}". parseStructuralElement doesn't consume them.
1612 nextToken();
1613 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001614 } else {
1615 parseStructuralElement();
1616 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001617 } while (!eof());
1618}
1619
Nico Weber2ce0ac52013-01-09 23:25:37 +00001620void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001621 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001622 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001623
1624 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001625 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001626 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001627 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001628 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001629 // Skip category, if present.
1630 parseParens();
1631
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001632 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001633 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001634
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001635 if (FormatTok->Tok.is(tok::l_brace)) {
1636 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1637 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1638 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001639 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001640 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001641
1642 // With instance variables, this puts '}' on its own line. Without instance
1643 // variables, this ends the @interface line.
1644 addUnwrappedLine();
1645
Nico Weber8696a8d2013-01-09 21:15:03 +00001646 parseObjCUntilAtEnd();
1647}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001648
Nico Weber8696a8d2013-01-09 21:15:03 +00001649void UnwrappedLineParser::parseObjCProtocol() {
1650 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001651 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001652
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001653 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001654 parseObjCProtocolList();
1655
1656 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001657 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001658 nextToken();
1659 return addUnwrappedLine();
1660 }
1661
1662 addUnwrappedLine();
1663 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001664}
1665
Daniel Jasperfca735c2015-02-19 16:14:18 +00001666void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1667 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00001668 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00001669
Daniel Jasperec05fc72015-05-11 09:14:50 +00001670 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00001671 if (FormatTok->is(tok::kw_default))
1672 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00001673
1674 // Consume "function" and "default function", so that these get parsed as
1675 // free-standing JS functions, i.e. do not require a trailing semicolon.
Daniel Jasper668c7bb2015-05-11 09:03:10 +00001676 if (FormatTok->is(Keywords.kw_function)) {
1677 nextToken();
1678 return;
1679 }
1680
1681 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_var))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001682 return; // Fall through to parsing the corresponding structure.
1683
Daniel Jasper354aa512015-02-19 16:07:32 +00001684 if (FormatTok->is(tok::l_brace)) {
1685 FormatTok->BlockKind = BK_Block;
1686 parseBracedList();
1687 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001688
Daniel Jasper354aa512015-02-19 16:07:32 +00001689 while (!eof() && FormatTok->isNot(tok::semi) &&
1690 FormatTok->isNot(tok::l_brace)) {
1691 nextToken();
1692 }
1693}
1694
Daniel Jasper3b203a62013-09-05 16:05:56 +00001695LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1696 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001697 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1698 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1699 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1700 E = Line.Tokens.end();
1701 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001702 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001703 }
1704 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1705 E = Line.Tokens.end();
1706 I != E; ++I) {
1707 const UnwrappedLineNode &Node = *I;
1708 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1709 I = Node.Children.begin(),
1710 E = Node.Children.end();
1711 I != E; ++I) {
1712 printDebugInfo(*I, "\nChild: ");
1713 }
1714 }
1715 llvm::dbgs() << "\n";
1716}
1717
Daniel Jasperf7935112012-12-03 18:12:45 +00001718void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001719 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001720 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001721 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001722 if (CurrentLines == &Lines)
1723 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001724 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001725 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001726 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001727 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001728 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001729 I = PreprocessorDirectives.begin(),
1730 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001731 I != E; ++I) {
1732 CurrentLines->push_back(*I);
1733 }
1734 PreprocessorDirectives.clear();
1735 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001736}
1737
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001738bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001739
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001740bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001741 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1742 FormatTok.NewlinesBefore > 0;
1743}
1744
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001745void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1746 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001747 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001748 I = CommentsBeforeNextToken.begin(),
1749 E = CommentsBeforeNextToken.end();
1750 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001751 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001752 addUnwrappedLine();
1753 }
1754 pushToken(*I);
1755 }
1756 if (NewlineBeforeNext && JustComments) {
1757 addUnwrappedLine();
1758 }
1759 CommentsBeforeNextToken.clear();
1760}
1761
Daniel Jasperf7935112012-12-03 18:12:45 +00001762void UnwrappedLineParser::nextToken() {
1763 if (eof())
1764 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001765 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001766 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001767 readToken();
1768}
1769
1770void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001771 bool CommentsInCurrentLine = true;
1772 do {
1773 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001774 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001775 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1776 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001777 // If there is an unfinished unwrapped line, we flush the preprocessor
1778 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001779 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001780 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001781 // Comments stored before the preprocessor directive need to be output
1782 // before the preprocessor directive, at the same level as the
1783 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001784 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001785 parsePPDirective();
1786 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001787 while (FormatTok->Type == TT_ConflictStart ||
1788 FormatTok->Type == TT_ConflictEnd ||
1789 FormatTok->Type == TT_ConflictAlternative) {
1790 if (FormatTok->Type == TT_ConflictStart) {
1791 conditionalCompilationStart(/*Unreachable=*/false);
1792 } else if (FormatTok->Type == TT_ConflictAlternative) {
1793 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001794 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001795 conditionalCompilationEnd();
1796 }
1797 FormatTok = Tokens->getNextToken();
1798 FormatTok->MustBreakBefore = true;
1799 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001800
1801 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1802 !Line->InPPDirective) {
1803 continue;
1804 }
1805
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001806 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001807 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001808 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001809 CommentsInCurrentLine = false;
1810 }
1811 if (CommentsInCurrentLine) {
1812 pushToken(FormatTok);
1813 } else {
1814 CommentsBeforeNextToken.push_back(FormatTok);
1815 }
1816 } while (!eof());
1817}
1818
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001819void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001820 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001821 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001822 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001823 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001824 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001825}
1826
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001827} // end namespace format
1828} // end namespace clang