blob: 72652a7d0d1e86d27c4662a2a7438e42e332788d [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());
Manuel Klimek1abf7892013-01-04 23:34:14 +0000254}
255
Manuel Klimek1a18c402013-04-12 14:13:36 +0000256void UnwrappedLineParser::parseFile() {
Daniel Jasper9326f912015-05-05 08:40:32 +0000257 // The top-level context in a file always has declarations, except for pre-
258 // processor directives and JavaScript files.
259 bool MustBeDeclaration =
260 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
261 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
262 MustBeDeclaration);
Nico Weber9096fc02013-06-26 00:30:14 +0000263 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000264 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000265 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000266 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000267}
268
Manuel Klimek1a18c402013-04-12 14:13:36 +0000269void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000270 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000271 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000272 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000273 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000274 nextToken();
275 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000276 break;
277 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000278 // FIXME: Add parameter whether this can happen - if this happens, we must
279 // be in a non-declaration context.
Nico Weber9096fc02013-06-26 00:30:14 +0000280 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000281 addUnwrappedLine();
282 break;
283 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000284 if (HasOpeningBrace)
285 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000286 nextToken();
287 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000288 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000289 case tok::kw_default:
290 case tok::kw_case:
Daniel Jasper72407622013-09-02 08:26:29 +0000291 if (!SwitchLabelEncountered &&
292 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
293 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000294 SwitchLabelEncountered = true;
295 parseStructuralElement();
296 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000297 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000298 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000299 break;
300 }
301 } while (!eof());
302}
303
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000304void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
Manuel Klimekab419912013-05-23 09:41:43 +0000305 // We'll parse forward through the tokens until we hit
306 // a closing brace or eof - note that getNextToken() will
307 // parse macros, so this will magically work inside macro
308 // definitions, too.
309 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000310 FormatToken *Tok = FormatTok;
Manuel Klimekab419912013-05-23 09:41:43 +0000311 // Keep a stack of positions of lbrace tokens. We will
312 // update information about whether an lbrace starts a
313 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000314 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000315 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000316 do {
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000317 // Get next none-comment token.
318 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000319 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000320 do {
321 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000322 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000323 } while (NextTok->is(tok::comment));
324
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000325 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000326 case tok::l_brace:
Daniel Jasper3c883d12015-05-18 14:49:19 +0000327 Tok->BlockKind = BK_Unknown;
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 Jaspercec9ffd2015-05-18 14:12:24 +0000354 (NextTok->is(tok::semi) &&
355 (!ExpectClassBody || LBraceStack.size() != 1)) ||
Daniel Jasper91b032a2014-05-22 12:46:38 +0000356 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000357 }
358 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000359 Tok->BlockKind = BK_BracedInit;
360 LBraceStack.back()->BlockKind = BK_BracedInit;
361 } else {
362 Tok->BlockKind = BK_Block;
363 LBraceStack.back()->BlockKind = BK_Block;
364 }
Manuel Klimekab419912013-05-23 09:41:43 +0000365 }
366 LBraceStack.pop_back();
367 }
368 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000369 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000370 case tok::semi:
371 case tok::kw_if:
372 case tok::kw_while:
373 case tok::kw_for:
374 case tok::kw_switch:
375 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000376 case tok::kw___try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000377 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000378 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000379 break;
380 default:
381 break;
382 }
383 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000384 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000385 // Assume other blocks for all unclosed opening braces.
386 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000387 if (LBraceStack[i]->BlockKind == BK_Unknown)
388 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000389 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000390
Manuel Klimekab419912013-05-23 09:41:43 +0000391 FormatTok = Tokens->setPosition(StoredPosition);
392}
393
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000394void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
395 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000396 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000397 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000398 nextToken();
399
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000400 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000401
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000402 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
403 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000404 if (AddLevel)
405 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000406 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000407
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000408 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000409 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000410 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000411 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000412
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000413 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000414 if (MunchSemi && FormatTok->Tok.is(tok::semi))
415 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000416 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000417}
418
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000419static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000420 // FIXME: Closure-library specific stuff should not be hard-coded but be
421 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000422 if (Line.Tokens.size() < 4)
423 return false;
424 auto I = Line.Tokens.begin();
425 if (I->Tok->TokenText != "goog")
426 return false;
427 ++I;
428 if (I->Tok->isNot(tok::period))
429 return false;
430 ++I;
431 if (I->Tok->TokenText != "scope")
432 return false;
433 ++I;
434 return I->Tok->is(tok::l_paren);
435}
436
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000437static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
438 const FormatToken &InitialToken) {
439 switch (Style.BreakBeforeBraces) {
440 case FormatStyle::BS_Linux:
441 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
442 case FormatStyle::BS_Allman:
443 case FormatStyle::BS_GNU:
444 return true;
445 default:
446 return false;
447 }
448}
449
Manuel Klimek516e0542013-09-04 13:25:30 +0000450void UnwrappedLineParser::parseChildBlock() {
451 FormatTok->BlockKind = BK_Block;
452 nextToken();
453 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000454 bool GoogScope =
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000455 Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000456 ScopedLineState LineState(*this);
457 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
458 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000459 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000460 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000461 flushComments(isOnNewLine(*FormatTok));
Daniel Jasper4a39c842014-05-06 13:54:10 +0000462 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000463 }
464 nextToken();
465}
466
Daniel Jasperf7935112012-12-03 18:12:45 +0000467void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000468 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000469 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000470 nextToken();
471
Craig Topper2145bc02014-05-09 08:15:10 +0000472 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000473 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000474 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000475 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000476
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000477 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000478 case tok::pp_define:
479 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000480 return;
481 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000482 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000483 break;
484 case tok::pp_ifdef:
485 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000486 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000487 break;
488 case tok::pp_else:
489 parsePPElse();
490 break;
491 case tok::pp_elif:
492 parsePPElIf();
493 break;
494 case tok::pp_endif:
495 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000496 break;
497 default:
498 parsePPUnknown();
499 break;
500 }
501}
502
Manuel Klimek68b03042014-04-14 09:14:11 +0000503void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
504 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000505 PPStack.push_back(PP_Unreachable);
506 else
507 PPStack.push_back(PP_Conditional);
508}
509
Manuel Klimek68b03042014-04-14 09:14:11 +0000510void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000511 ++PPBranchLevel;
512 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
513 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
514 PPLevelBranchIndex.push_back(0);
515 PPLevelBranchCount.push_back(0);
516 }
517 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000518 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
519 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000520}
521
Manuel Klimek68b03042014-04-14 09:14:11 +0000522void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000523 if (!PPStack.empty())
524 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000525 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
526 if (!PPChainBranchIndex.empty())
527 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000528 conditionalCompilationCondition(
529 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
530 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000531}
532
Manuel Klimek68b03042014-04-14 09:14:11 +0000533void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000534 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
535 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
536 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000537 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
538 }
539 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000540 // Guard against #endif's without #if.
541 if (PPBranchLevel > 0)
542 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000543 if (!PPChainBranchIndex.empty())
544 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000545 if (!PPStack.empty())
546 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000547}
548
549void UnwrappedLineParser::parsePPIf(bool IfDef) {
550 nextToken();
551 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Daniel Jasper9d22bcc2015-01-19 10:51:05 +0000552 FormatTok->Tok.getLiteralData() != nullptr &&
Manuel Klimek68b03042014-04-14 09:14:11 +0000553 StringRef(FormatTok->Tok.getLiteralData(),
554 FormatTok->Tok.getLength()) == "0") ||
555 FormatTok->Tok.is(tok::kw_false);
556 conditionalCompilationStart(!IfDef && IsLiteralFalse);
557 parsePPUnknown();
558}
559
560void UnwrappedLineParser::parsePPElse() {
561 conditionalCompilationAlternative();
562 parsePPUnknown();
563}
564
565void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
566
567void UnwrappedLineParser::parsePPEndIf() {
568 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000569 parsePPUnknown();
570}
571
Manuel Klimek1abf7892013-01-04 23:34:14 +0000572void UnwrappedLineParser::parsePPDefine() {
573 nextToken();
574
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000575 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000576 parsePPUnknown();
577 return;
578 }
579 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000580 if (FormatTok->Tok.getKind() == tok::l_paren &&
581 FormatTok->WhitespaceRange.getBegin() ==
582 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000583 parseParens();
584 }
585 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000586 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000587
588 // Errors during a preprocessor directive can only affect the layout of the
589 // preprocessor directive, and thus we ignore them. An alternative approach
590 // would be to use the same approach we use on the file level (no
591 // re-indentation if there was a structural error) within the macro
592 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000593 parseFile();
594}
595
596void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000597 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000598 nextToken();
599 } while (!eof());
600 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000601}
602
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000603// Here we blacklist certain tokens that are not usually the first token in an
604// unwrapped line. This is used in attempt to distinguish macro calls without
605// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000606static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000607 // Semicolon can be a null-statement, l_square can be a start of a macro or
608 // a C++11 attribute, but this doesn't seem to be common.
609 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
610 Tok.isNot(tok::l_square) &&
611 // Tokens that can only be used as binary operators and a part of
612 // overloaded operator names.
613 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
614 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
615 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
616 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
617 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
618 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
619 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
620 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
621 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
622 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
623 Tok.isNot(tok::lesslessequal) &&
624 // Colon is used in labels, base class lists, initializer lists,
625 // range-based for loops, ternary operator, but should never be the
626 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000627 Tok.isNot(tok::colon) &&
628 // 'noexcept' is a trailing annotation.
629 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000630}
631
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000632void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000633 assert(!FormatTok->Tok.is(tok::l_brace));
634 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000635 case tok::at:
636 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000637 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000638 parseBracedList();
639 break;
640 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000641 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000642 case tok::objc_public:
643 case tok::objc_protected:
644 case tok::objc_package:
645 case tok::objc_private:
646 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000647 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000648 case tok::objc_implementation:
649 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000650 case tok::objc_protocol:
651 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000652 case tok::objc_end:
653 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000654 case tok::objc_optional:
655 case tok::objc_required:
656 nextToken();
657 addUnwrappedLine();
658 return;
Nico Weber45c48122015-06-28 01:06:16 +0000659 case tok::objc_autoreleasepool:
660 nextToken();
661 if (FormatTok->Tok.is(tok::l_brace)) {
662 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
663 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
664 addUnwrappedLine();
665 parseBlock(/*MustBeDeclaration=*/false);
666 }
667 addUnwrappedLine();
668 return;
Nico Weber33381f52015-02-07 01:57:32 +0000669 case tok::objc_try:
670 // This branch isn't strictly necessary (the kw_try case below would
671 // do this too after the tok::at is parsed above). But be explicit.
672 parseTryCatch();
673 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000674 default:
675 break;
676 }
677 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000678 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000679 nextToken();
680 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000681 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000682 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000683 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000684 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000685 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000686 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000687 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000688 break;
689 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000690 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000691 nextToken();
692 }
693 }
694 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000695 case tok::kw_namespace:
696 parseNamespace();
697 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000698 case tok::kw_inline:
699 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000700 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000701 parseNamespace();
702 return;
703 }
704 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000705 case tok::kw_public:
706 case tok::kw_protected:
707 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +0000708 if (Style.Language == FormatStyle::LK_Java ||
709 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000710 nextToken();
711 else
712 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000713 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000714 case tok::kw_if:
715 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000716 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000717 case tok::kw_for:
718 case tok::kw_while:
719 parseForOrWhileLoop();
720 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000721 case tok::kw_do:
722 parseDoWhile();
723 return;
724 case tok::kw_switch:
725 parseSwitch();
726 return;
727 case tok::kw_default:
728 nextToken();
729 parseLabel();
730 return;
731 case tok::kw_case:
732 parseCaseLabel();
733 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000734 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000735 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +0000736 parseTryCatch();
737 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000738 case tok::kw_extern:
739 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000740 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000741 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000742 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000743 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000744 addUnwrappedLine();
745 return;
746 }
747 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000748 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +0000749 case tok::kw_export:
750 if (Style.Language == FormatStyle::LK_JavaScript) {
751 parseJavaScriptEs6ImportExport();
752 return;
753 }
754 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000755 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +0000756 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +0000757 parseForOrWhileLoop();
758 return;
759 }
Daniel Jasper354aa512015-02-19 16:07:32 +0000760 if (Style.Language == FormatStyle::LK_JavaScript &&
761 FormatTok->is(Keywords.kw_import)) {
Daniel Jasperfca735c2015-02-19 16:14:18 +0000762 parseJavaScriptEs6ImportExport();
Daniel Jasper354aa512015-02-19 16:07:32 +0000763 return;
764 }
Daniel Jasper53395402015-04-07 15:04:40 +0000765 if (FormatTok->is(Keywords.kw_signals)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +0000766 nextToken();
767 if (FormatTok->is(tok::colon)) {
768 nextToken();
769 addUnwrappedLine();
770 }
Daniel Jasper53395402015-04-07 15:04:40 +0000771 return;
772 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000773 // In all other cases, parse the declaration.
774 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000775 default:
776 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000777 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000778 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000779 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000780 case tok::at:
781 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000782 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000783 parseBracedList();
784 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000785 case tok::kw_enum:
Daniel Jasper90cf3802015-06-17 09:44:02 +0000786 // parseEnum falls through and does not yet add an unwrapped line as an
787 // enum definition can start a structural element.
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000788 parseEnum();
Daniel Jasper90cf3802015-06-17 09:44:02 +0000789 // This does not apply for Java and JavaScript.
790 if (Style.Language == FormatStyle::LK_Java ||
791 Style.Language == FormatStyle::LK_JavaScript) {
792 addUnwrappedLine();
793 return;
794 }
Manuel Klimek2cec0192013-01-21 19:17:52 +0000795 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000796 case tok::kw_typedef:
797 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000798 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
799 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000800 parseEnum();
801 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000802 case tok::kw_struct:
803 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000804 case tok::kw_class:
Daniel Jasper910807d2015-06-12 04:52:02 +0000805 // parseRecord falls through and does not yet add an unwrapped line as a
806 // record declaration or definition can start a structural element.
Manuel Klimeke01bab52013-01-15 13:38:33 +0000807 parseRecord();
Daniel Jasper910807d2015-06-12 04:52:02 +0000808 // This does not apply for Java and JavaScript.
809 if (Style.Language == FormatStyle::LK_Java ||
810 Style.Language == FormatStyle::LK_JavaScript) {
811 addUnwrappedLine();
812 return;
813 }
Manuel Klimeke01bab52013-01-15 13:38:33 +0000814 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000815 case tok::period:
816 nextToken();
817 // In Java, classes have an implicit static member "class".
818 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
819 FormatTok->is(tok::kw_class))
820 nextToken();
821 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000822 case tok::semi:
823 nextToken();
824 addUnwrappedLine();
825 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000826 case tok::r_brace:
827 addUnwrappedLine();
828 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000829 case tok::l_paren:
830 parseParens();
831 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000832 case tok::caret:
833 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000834 if (FormatTok->Tok.isAnyIdentifier() ||
835 FormatTok->isSimpleTypeSpecifier())
836 nextToken();
837 if (FormatTok->is(tok::l_paren))
838 parseParens();
839 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000840 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000841 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000842 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000843 if (!tryToParseBracedList()) {
844 // A block outside of parentheses must be the last part of a
845 // structural element.
846 // FIXME: Figure out cases where this is not true, and add projections
847 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000848 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000849 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000850 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000851 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000852 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000853 return;
854 }
855 // Otherwise this was a braced init list, and the structural
856 // element continues.
857 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000858 case tok::kw_try:
859 // We arrive here when parsing function-try blocks.
860 parseTryCatch();
861 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000862 case tok::identifier: {
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000863 // Parse function literal unless 'function' is the first token in a line
864 // in which case this should be treated as a free-standing function.
Daniel Jasper9326f912015-05-05 08:40:32 +0000865 if (Style.Language == FormatStyle::LK_JavaScript &&
866 FormatTok->is(Keywords.kw_function) && Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000867 tryToParseJSFunction();
868 break;
869 }
Daniel Jasper9326f912015-05-05 08:40:32 +0000870 if ((Style.Language == FormatStyle::LK_JavaScript ||
871 Style.Language == FormatStyle::LK_Java) &&
872 FormatTok->is(Keywords.kw_interface)) {
873 parseRecord();
Daniel Jasper259188b2015-06-12 04:56:34 +0000874 addUnwrappedLine();
Daniel Jasper9326f912015-05-05 08:40:32 +0000875 break;
876 }
877
878 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +0000879 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +0000880 if (Line->Tokens.size() == 1 &&
881 // JS doesn't have macros, and within classes colons indicate fields,
882 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +0000883 Style.Language != FormatStyle::LK_JavaScript) {
884 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000885 parseLabel();
886 return;
887 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000888 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +0000889 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +0000890 bool FunctionLike = FormatTok->is(tok::l_paren);
891 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000892 parseParens();
Daniel Jaspere60cba12015-05-13 11:35:53 +0000893
894 bool FollowedByNewline =
895 CommentsBeforeNextToken.empty()
896 ? FormatTok->NewlinesBefore > 0
897 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
898
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +0000899 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
Daniel Jasper680b09b2014-11-05 10:48:04 +0000900 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000901 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000902 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000903 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000904 }
905 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000906 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000907 case tok::equal:
Manuel Klimek79e06082015-05-21 12:23:34 +0000908 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
909 // TT_JsFatArrow. The always start an expression or a child block if
910 // followed by a curly.
911 if (FormatTok->is(TT_JsFatArrow)) {
912 nextToken();
Daniel Jasperbe520bd2015-05-31 08:51:54 +0000913 if (FormatTok->is(tok::l_brace))
Manuel Klimek79e06082015-05-21 12:23:34 +0000914 parseChildBlock();
Manuel Klimek79e06082015-05-21 12:23:34 +0000915 break;
916 }
917
Daniel Jaspere25509f2012-12-17 11:29:41 +0000918 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000919 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000920 parseBracedList();
921 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000922 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000923 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000924 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000925 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +0000926 case tok::kw_new:
927 parseNew();
928 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000929 default:
930 nextToken();
931 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000932 }
933 } while (!eof());
934}
935
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000936bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper1feab0f2015-06-02 15:31:37 +0000937 if (Style.Language != FormatStyle::LK_Cpp) {
938 nextToken();
939 return false;
940 }
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000941 // FIXME: This is a dirty way to access the previous token. Find a better
942 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000943 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000944 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
945 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000946 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000947 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000948 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000949 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000950 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000951 assert(FormatTok->is(tok::l_square));
952 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000953 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000954 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000955
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000956 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000957 if (FormatTok->isSimpleTypeSpecifier()) {
958 nextToken();
959 continue;
960 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000961 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000962 case tok::l_brace:
963 break;
964 case tok::l_paren:
965 parseParens();
966 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000967 case tok::amp:
968 case tok::star:
969 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000970 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000971 case tok::less:
972 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000973 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000974 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000975 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000976 nextToken();
977 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000978 case tok::arrow:
Daniel Jasper6f2b88a2015-06-05 13:18:09 +0000979 FormatTok->Type = TT_LambdaArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000980 nextToken();
981 break;
982 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000983 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000984 }
985 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000986 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000987 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000988 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000989}
990
991bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
992 nextToken();
993 if (FormatTok->is(tok::equal)) {
994 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000995 if (FormatTok->is(tok::r_square)) {
996 nextToken();
997 return true;
998 }
999 if (FormatTok->isNot(tok::comma))
1000 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001001 nextToken();
1002 } else if (FormatTok->is(tok::amp)) {
1003 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001004 if (FormatTok->is(tok::r_square)) {
1005 nextToken();
1006 return true;
1007 }
Manuel Klimekffdeb592013-09-03 15:10:01 +00001008 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
1009 return false;
1010 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001011 if (FormatTok->is(tok::comma))
1012 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001013 } else if (FormatTok->is(tok::r_square)) {
1014 nextToken();
1015 return true;
1016 }
1017 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001018 if (FormatTok->is(tok::amp))
1019 nextToken();
1020 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
1021 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001022 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +00001023 if (FormatTok->is(tok::ellipsis))
1024 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001025 if (FormatTok->is(tok::comma)) {
1026 nextToken();
1027 } else if (FormatTok->is(tok::r_square)) {
1028 nextToken();
1029 return true;
1030 } else {
1031 return false;
1032 }
1033 } while (!eof());
1034 return false;
1035}
1036
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001037void UnwrappedLineParser::tryToParseJSFunction() {
1038 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001039
1040 // Consume function name.
1041 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001042 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001043
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001044 if (FormatTok->isNot(tok::l_paren))
1045 return;
Manuel Klimek79e06082015-05-21 12:23:34 +00001046
1047 // Parse formal parameter list.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001048 parseParens();
Manuel Klimek79e06082015-05-21 12:23:34 +00001049
1050 if (FormatTok->is(tok::colon)) {
1051 // Parse a type definition.
1052 nextToken();
1053
1054 // Eat the type declaration. For braced inline object types, balance braces,
1055 // otherwise just parse until finding an l_brace for the function body.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001056 if (FormatTok->is(tok::l_brace))
1057 tryToParseBracedList();
1058 else
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001059 while (FormatTok->isNot(tok::l_brace) && !eof())
Manuel Klimek79e06082015-05-21 12:23:34 +00001060 nextToken();
Manuel Klimek79e06082015-05-21 12:23:34 +00001061 }
1062
1063 parseChildBlock();
1064}
1065
Daniel Jasper3c883d12015-05-18 14:49:19 +00001066bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001067 if (FormatTok->BlockKind == BK_Unknown)
Daniel Jasper3c883d12015-05-18 14:49:19 +00001068 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001069 assert(FormatTok->BlockKind != BK_Unknown);
1070 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001071 return false;
1072 parseBracedList();
1073 return true;
1074}
1075
Daniel Jasper015ed022013-09-13 09:20:45 +00001076bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1077 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001078 nextToken();
1079
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001080 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1081 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001082 do {
Manuel Klimek79e06082015-05-21 12:23:34 +00001083 if (Style.Language == FormatStyle::LK_JavaScript) {
1084 if (FormatTok->is(Keywords.kw_function)) {
1085 tryToParseJSFunction();
1086 continue;
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001087 }
1088 if (FormatTok->is(TT_JsFatArrow)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001089 nextToken();
1090 // Fat arrows can be followed by simple expressions or by child blocks
1091 // in curly braces.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001092 if (FormatTok->is(tok::l_brace)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001093 parseChildBlock();
1094 continue;
1095 }
1096 }
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001097 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001098 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001099 case tok::caret:
1100 nextToken();
1101 if (FormatTok->is(tok::l_brace)) {
1102 parseChildBlock();
1103 }
1104 break;
1105 case tok::l_square:
1106 tryToParseLambda();
1107 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001108 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001109 // Assume there are no blocks inside a braced init list apart
1110 // from the ones we explicitly parse out (like lambdas).
1111 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001112 parseBracedList();
1113 break;
Daniel Jasperf46dec82015-03-31 14:34:15 +00001114 case tok::r_paren:
1115 // JavaScript can just have free standing methods and getters/setters in
1116 // object literals. Detect them by a "{" following ")".
1117 if (Style.Language == FormatStyle::LK_JavaScript) {
1118 nextToken();
1119 if (FormatTok->is(tok::l_brace))
1120 parseChildBlock();
1121 break;
1122 }
1123 nextToken();
1124 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001125 case tok::r_brace:
1126 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001127 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001128 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001129 HasError = true;
1130 if (!ContinueOnSemicolons)
1131 return !HasError;
1132 nextToken();
1133 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001134 case tok::comma:
1135 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001136 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001137 default:
1138 nextToken();
1139 break;
1140 }
1141 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001142 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001143}
1144
Daniel Jasperf7935112012-12-03 18:12:45 +00001145void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001146 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001147 nextToken();
1148 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001149 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001150 case tok::l_paren:
1151 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001152 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1153 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001154 break;
1155 case tok::r_paren:
1156 nextToken();
1157 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001158 case tok::r_brace:
1159 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1160 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001161 case tok::l_square:
1162 tryToParseLambda();
1163 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001164 case tok::l_brace:
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001165 if (!tryToParseBracedList())
Manuel Klimekf017dc02013-09-04 13:34:14 +00001166 parseChildBlock();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001167 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001168 case tok::at:
1169 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001170 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001171 parseBracedList();
1172 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001173 case tok::identifier:
1174 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001175 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001176 tryToParseJSFunction();
1177 else
1178 nextToken();
1179 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001180 default:
1181 nextToken();
1182 break;
1183 }
1184 } while (!eof());
1185}
1186
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001187void UnwrappedLineParser::parseSquare() {
1188 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1189 if (tryToParseLambda())
1190 return;
1191 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001192 switch (FormatTok->Tok.getKind()) {
1193 case tok::l_paren:
1194 parseParens();
1195 break;
1196 case tok::r_square:
1197 nextToken();
1198 return;
1199 case tok::r_brace:
1200 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1201 return;
1202 case tok::l_square:
1203 parseSquare();
1204 break;
1205 case tok::l_brace: {
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001206 if (!tryToParseBracedList())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001207 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001208 break;
1209 }
1210 case tok::at:
1211 nextToken();
1212 if (FormatTok->Tok.is(tok::l_brace))
1213 parseBracedList();
1214 break;
1215 default:
1216 nextToken();
1217 break;
1218 }
1219 } while (!eof());
1220}
1221
Daniel Jasperf7935112012-12-03 18:12:45 +00001222void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001223 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001224 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001225 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001226 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001227 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001228 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001229 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001230 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001231 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1232 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001233 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001234 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001235 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001236 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001237 } else {
1238 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001239 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001240 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001241 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001242 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001243 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001244 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1245 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001246 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001247 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001248 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001249 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001250 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001251 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001252 parseIfThenElse();
1253 } else {
1254 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001255 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001256 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001257 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001258 }
1259 } else if (NeedsUnwrappedLine) {
1260 addUnwrappedLine();
1261 }
1262}
1263
Daniel Jasper04a71a42014-05-08 11:58:24 +00001264void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001265 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001266 nextToken();
1267 bool NeedsUnwrappedLine = false;
1268 if (FormatTok->is(tok::colon)) {
1269 // We are in a function try block, what comes is an initializer list.
1270 nextToken();
1271 while (FormatTok->is(tok::identifier)) {
1272 nextToken();
1273 if (FormatTok->is(tok::l_paren))
1274 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001275 if (FormatTok->is(tok::comma))
1276 nextToken();
1277 }
1278 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001279 // Parse try with resource.
1280 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1281 parseParens();
1282 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001283 if (FormatTok->is(tok::l_brace)) {
1284 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1285 parseBlock(/*MustBeDeclaration=*/false);
1286 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1287 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1288 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1289 addUnwrappedLine();
1290 } else {
1291 NeedsUnwrappedLine = true;
1292 }
1293 } else if (!FormatTok->is(tok::kw_catch)) {
1294 // The C++ standard requires a compound-statement after a try.
1295 // If there's none, we try to assume there's a structuralElement
1296 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001297 addUnwrappedLine();
1298 ++Line->Level;
1299 parseStructuralElement();
1300 --Line->Level;
1301 }
Nico Weber33381f52015-02-07 01:57:32 +00001302 while (1) {
1303 if (FormatTok->is(tok::at))
1304 nextToken();
1305 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1306 tok::kw___finally) ||
1307 ((Style.Language == FormatStyle::LK_Java ||
1308 Style.Language == FormatStyle::LK_JavaScript) &&
1309 FormatTok->is(Keywords.kw_finally)) ||
1310 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1311 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1312 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001313 nextToken();
1314 while (FormatTok->isNot(tok::l_brace)) {
1315 if (FormatTok->is(tok::l_paren)) {
1316 parseParens();
1317 continue;
1318 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001319 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001320 return;
1321 nextToken();
1322 }
1323 NeedsUnwrappedLine = false;
1324 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1325 parseBlock(/*MustBeDeclaration=*/false);
1326 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1327 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1328 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1329 addUnwrappedLine();
1330 } else {
1331 NeedsUnwrappedLine = true;
1332 }
1333 }
1334 if (NeedsUnwrappedLine) {
1335 addUnwrappedLine();
1336 }
1337}
1338
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001339void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001340 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001341
1342 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001343 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001344 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001345 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001346 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001347 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001348 addUnwrappedLine();
1349
Daniel Jasper65ee3472013-07-31 23:16:02 +00001350 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1351 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1352 DeclarationScopeStack.size() > 1);
1353 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001354 // Munch the semicolon after a namespace. This is more common than one would
1355 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001356 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001357 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001358 addUnwrappedLine();
1359 }
1360 // FIXME: Add error handling.
1361}
1362
Daniel Jasper6acf5132015-03-12 14:44:29 +00001363void UnwrappedLineParser::parseNew() {
1364 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1365 nextToken();
1366 if (Style.Language != FormatStyle::LK_Java)
1367 return;
1368
1369 // In Java, we can parse everything up to the parens, which aren't optional.
1370 do {
1371 // There should not be a ;, { or } before the new's open paren.
1372 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1373 return;
1374
1375 // Consume the parens.
1376 if (FormatTok->is(tok::l_paren)) {
1377 parseParens();
1378
1379 // If there is a class body of an anonymous class, consume that as child.
1380 if (FormatTok->is(tok::l_brace))
1381 parseChildBlock();
1382 return;
1383 }
1384 nextToken();
1385 } while (!eof());
1386}
1387
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001388void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001389 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001390 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001391 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001392 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001393 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001394 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001395 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001396 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001397 addUnwrappedLine();
1398 } else {
1399 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001400 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001401 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001402 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001403 }
1404}
1405
Daniel Jasperf7935112012-12-03 18:12:45 +00001406void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001407 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001408 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001409 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001410 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001411 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001412 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1413 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001414 } else {
1415 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001416 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001417 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001418 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001419 }
1420
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001421 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001422 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001423 addUnwrappedLine();
1424 return;
1425 }
1426
Daniel Jasperf7935112012-12-03 18:12:45 +00001427 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001428 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001429}
1430
1431void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001432 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001433 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001434 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001435 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001436 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001437 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001438 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001439 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001440 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1441 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1442 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001443 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001444 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001445 parseStructuralElement();
1446 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001447 addUnwrappedLine();
1448 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001449 if (FormatTok->is(tok::semi))
1450 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001451 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001452 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001453 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001454}
1455
1456void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001457 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001458 // FIXME: fix handling of complex expressions here.
1459 do {
1460 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001461 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001462 parseLabel();
1463}
1464
1465void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001466 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001467 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001468 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001469 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001470 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001471 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001472 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001473 addUnwrappedLine();
1474 } else {
1475 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001476 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001477 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001478 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001479 }
1480}
1481
1482void UnwrappedLineParser::parseAccessSpecifier() {
1483 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001484 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001485 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001486 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001487 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001488 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001489 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001490 addUnwrappedLine();
1491}
1492
1493void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001494 // Won't be 'enum' for NS_ENUMs.
1495 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001496 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001497
Daniel Jasper2b41a822013-08-20 12:42:50 +00001498 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001499 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1500 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001501
Daniel Jasper786a5502013-09-06 21:32:35 +00001502 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001503 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1504 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001505 nextToken();
1506 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001507 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001508 parseParens();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001509 if (FormatTok->is(tok::identifier)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001510 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001511 // If there are two identifiers in a row, this is likely an elaborate
1512 // return type. In Java, this can be "implements", etc.
1513 if (Style.Language == FormatStyle::LK_Cpp &&
1514 FormatTok->is(tok::identifier))
1515 return;
1516 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001517 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001518
1519 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001520 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001521 return;
1522 FormatTok->BlockKind = BK_Block;
1523
1524 if (Style.Language == FormatStyle::LK_Java) {
1525 // Java enums are different.
1526 parseJavaEnumBody();
1527 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001528 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001529
1530 // Parse enum body.
1531 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1532 if (HasError) {
1533 if (FormatTok->is(tok::semi))
1534 nextToken();
1535 addUnwrappedLine();
1536 }
1537
Daniel Jasper90cf3802015-06-17 09:44:02 +00001538 // There is no addUnwrappedLine() here so that we fall through to parsing a
1539 // structural element afterwards. Thus, in "enum A {} n, m;",
Manuel Klimek2cec0192013-01-21 19:17:52 +00001540 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001541}
1542
1543void UnwrappedLineParser::parseJavaEnumBody() {
1544 // Determine whether the enum is simple, i.e. does not have a semicolon or
1545 // constants with class bodies. Simple enums can be formatted like braced
1546 // lists, contracted to a single line, etc.
1547 unsigned StoredPosition = Tokens->getPosition();
1548 bool IsSimple = true;
1549 FormatToken *Tok = Tokens->getNextToken();
1550 while (Tok) {
1551 if (Tok->is(tok::r_brace))
1552 break;
1553 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1554 IsSimple = false;
1555 break;
1556 }
1557 // FIXME: This will also mark enums with braces in the arguments to enum
1558 // constants as "not simple". This is probably fine in practice, though.
1559 Tok = Tokens->getNextToken();
1560 }
1561 FormatTok = Tokens->setPosition(StoredPosition);
1562
1563 if (IsSimple) {
1564 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001565 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001566 return;
1567 }
1568
1569 // Parse the body of a more complex enum.
1570 // First add a line for everything up to the "{".
1571 nextToken();
1572 addUnwrappedLine();
1573 ++Line->Level;
1574
1575 // Parse the enum constants.
1576 while (FormatTok) {
1577 if (FormatTok->is(tok::l_brace)) {
1578 // Parse the constant's class body.
1579 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1580 /*MunchSemi=*/false);
1581 } else if (FormatTok->is(tok::l_paren)) {
1582 parseParens();
1583 } else if (FormatTok->is(tok::comma)) {
1584 nextToken();
1585 addUnwrappedLine();
1586 } else if (FormatTok->is(tok::semi)) {
1587 nextToken();
1588 addUnwrappedLine();
1589 break;
1590 } else if (FormatTok->is(tok::r_brace)) {
1591 addUnwrappedLine();
1592 break;
1593 } else {
1594 nextToken();
1595 }
1596 }
1597
1598 // Parse the class body after the enum's ";" if any.
1599 parseLevel(/*HasOpeningBrace=*/true);
1600 nextToken();
1601 --Line->Level;
1602 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001603}
1604
Manuel Klimeke01bab52013-01-15 13:38:33 +00001605void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001606 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001607 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00001608
Daniel Jasper04785d02015-05-06 14:03:02 +00001609 // The actual identifier can be a nested name specifier, and in macros
1610 // it is often token-pasted.
1611 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
1612 tok::kw___attribute, tok::kw___declspec,
1613 tok::kw_alignas) ||
1614 ((Style.Language == FormatStyle::LK_Java ||
1615 Style.Language == FormatStyle::LK_JavaScript) &&
1616 FormatTok->isOneOf(tok::period, tok::comma))) {
1617 bool IsNonMacroIdentifier =
1618 FormatTok->is(tok::identifier) &&
1619 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00001620 nextToken();
1621 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00001622 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001623 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00001624 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001625
Daniel Jasper04785d02015-05-06 14:03:02 +00001626 // Note that parsing away template declarations here leads to incorrectly
1627 // accepting function declarations as record declarations.
1628 // In general, we cannot solve this problem. Consider:
1629 // class A<int> B() {}
1630 // which can be a function definition or a class definition when B() is a
1631 // macro. If we find enough real-world cases where this is a problem, we
1632 // can parse for the 'template' keyword in the beginning of the statement,
1633 // and thus rule out the record production in case there is no template
1634 // (this would still leave us with an ambiguity between template function
1635 // and class declarations).
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001636 if (FormatTok->isOneOf(tok::colon, tok::less)) {
1637 while (!eof()) {
Daniel Jasper3c883d12015-05-18 14:49:19 +00001638 if (FormatTok->is(tok::l_brace)) {
1639 calculateBraceTypes(/*ExpectClassBody=*/true);
1640 if (!tryToParseBracedList())
1641 break;
1642 }
Daniel Jasper04785d02015-05-06 14:03:02 +00001643 if (FormatTok->Tok.is(tok::semi))
1644 return;
1645 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00001646 }
1647 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001648 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001649 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001650 addUnwrappedLine();
1651
Daniel Jasper240dfda2014-03-31 14:23:49 +00001652 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001653 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001654 }
Daniel Jasper90cf3802015-06-17 09:44:02 +00001655 // There is no addUnwrappedLine() here so that we fall through to parsing a
1656 // structural element afterwards. Thus, in "class A {} n, m;",
1657 // "} n, m;" will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00001658}
1659
Nico Weber8696a8d2013-01-09 21:15:03 +00001660void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001661 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001662 do
1663 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001664 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001665 nextToken(); // Skip '>'.
1666}
1667
1668void UnwrappedLineParser::parseObjCUntilAtEnd() {
1669 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001670 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001671 nextToken();
1672 addUnwrappedLine();
1673 break;
1674 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001675 if (FormatTok->is(tok::l_brace)) {
1676 parseBlock(/*MustBeDeclaration=*/false);
1677 // In ObjC interfaces, nothing should be following the "}".
1678 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001679 } else if (FormatTok->is(tok::r_brace)) {
1680 // Ignore stray "}". parseStructuralElement doesn't consume them.
1681 nextToken();
1682 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001683 } else {
1684 parseStructuralElement();
1685 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001686 } while (!eof());
1687}
1688
Nico Weber2ce0ac52013-01-09 23:25:37 +00001689void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001690 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001691 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001692
1693 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001694 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001695 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001696 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001697 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001698 // Skip category, if present.
1699 parseParens();
1700
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001701 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001702 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001703
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001704 if (FormatTok->Tok.is(tok::l_brace)) {
1705 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1706 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1707 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001708 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001709 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001710
1711 // With instance variables, this puts '}' on its own line. Without instance
1712 // variables, this ends the @interface line.
1713 addUnwrappedLine();
1714
Nico Weber8696a8d2013-01-09 21:15:03 +00001715 parseObjCUntilAtEnd();
1716}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001717
Nico Weber8696a8d2013-01-09 21:15:03 +00001718void UnwrappedLineParser::parseObjCProtocol() {
1719 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001720 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001721
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001722 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001723 parseObjCProtocolList();
1724
1725 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001726 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001727 nextToken();
1728 return addUnwrappedLine();
1729 }
1730
1731 addUnwrappedLine();
1732 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001733}
1734
Daniel Jasperfca735c2015-02-19 16:14:18 +00001735void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1736 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00001737 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00001738
Daniel Jasperec05fc72015-05-11 09:14:50 +00001739 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00001740 if (FormatTok->is(tok::kw_default))
1741 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00001742
1743 // Consume "function" and "default function", so that these get parsed as
1744 // free-standing JS functions, i.e. do not require a trailing semicolon.
Daniel Jasper668c7bb2015-05-11 09:03:10 +00001745 if (FormatTok->is(Keywords.kw_function)) {
1746 nextToken();
1747 return;
1748 }
1749
Daniel Jasper216c9cd2015-06-12 05:08:18 +00001750 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, tok::kw_enum,
1751 Keywords.kw_var))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001752 return; // Fall through to parsing the corresponding structure.
1753
Daniel Jasper354aa512015-02-19 16:07:32 +00001754 if (FormatTok->is(tok::l_brace)) {
1755 FormatTok->BlockKind = BK_Block;
1756 parseBracedList();
1757 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001758
Daniel Jasper354aa512015-02-19 16:07:32 +00001759 while (!eof() && FormatTok->isNot(tok::semi) &&
1760 FormatTok->isNot(tok::l_brace)) {
1761 nextToken();
1762 }
1763}
1764
Daniel Jasper3b203a62013-09-05 16:05:56 +00001765LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1766 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001767 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1768 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1769 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1770 E = Line.Tokens.end();
1771 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001772 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001773 }
1774 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1775 E = Line.Tokens.end();
1776 I != E; ++I) {
1777 const UnwrappedLineNode &Node = *I;
1778 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1779 I = Node.Children.begin(),
1780 E = Node.Children.end();
1781 I != E; ++I) {
1782 printDebugInfo(*I, "\nChild: ");
1783 }
1784 }
1785 llvm::dbgs() << "\n";
1786}
1787
Daniel Jasperf7935112012-12-03 18:12:45 +00001788void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001789 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001790 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001791 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001792 if (CurrentLines == &Lines)
1793 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001794 });
Benjamin Kramerc7551a42015-05-31 11:18:05 +00001795 CurrentLines->push_back(std::move(*Line));
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001796 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001797 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Benjamin Kramerc7551a42015-05-31 11:18:05 +00001798 CurrentLines->append(
1799 std::make_move_iterator(PreprocessorDirectives.begin()),
1800 std::make_move_iterator(PreprocessorDirectives.end()));
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001801 PreprocessorDirectives.clear();
1802 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001803}
1804
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001805bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001806
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001807bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001808 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1809 FormatTok.NewlinesBefore > 0;
1810}
1811
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001812void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1813 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001814 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001815 I = CommentsBeforeNextToken.begin(),
1816 E = CommentsBeforeNextToken.end();
1817 I != E; ++I) {
Daniel Jaspere60cba12015-05-13 11:35:53 +00001818 if (isOnNewLine(**I) && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001819 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001820 pushToken(*I);
1821 }
Daniel Jaspere60cba12015-05-13 11:35:53 +00001822 if (NewlineBeforeNext && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001823 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001824 CommentsBeforeNextToken.clear();
1825}
1826
Daniel Jasperf7935112012-12-03 18:12:45 +00001827void UnwrappedLineParser::nextToken() {
1828 if (eof())
1829 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001830 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001831 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001832 readToken();
1833}
1834
1835void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001836 bool CommentsInCurrentLine = true;
1837 do {
1838 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001839 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001840 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1841 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001842 // If there is an unfinished unwrapped line, we flush the preprocessor
1843 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001844 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001845 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001846 // Comments stored before the preprocessor directive need to be output
1847 // before the preprocessor directive, at the same level as the
1848 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001849 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001850 parsePPDirective();
1851 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001852 while (FormatTok->Type == TT_ConflictStart ||
1853 FormatTok->Type == TT_ConflictEnd ||
1854 FormatTok->Type == TT_ConflictAlternative) {
1855 if (FormatTok->Type == TT_ConflictStart) {
1856 conditionalCompilationStart(/*Unreachable=*/false);
1857 } else if (FormatTok->Type == TT_ConflictAlternative) {
1858 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001859 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001860 conditionalCompilationEnd();
1861 }
1862 FormatTok = Tokens->getNextToken();
1863 FormatTok->MustBreakBefore = true;
1864 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001865
1866 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1867 !Line->InPPDirective) {
1868 continue;
1869 }
1870
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001871 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001872 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001873 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001874 CommentsInCurrentLine = false;
1875 }
1876 if (CommentsInCurrentLine) {
1877 pushToken(FormatTok);
1878 } else {
1879 CommentsBeforeNextToken.push_back(FormatTok);
1880 }
1881 } while (!eof());
1882}
1883
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001884void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001885 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001886 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001887 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001888 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001889 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001890}
1891
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001892} // end namespace format
1893} // end namespace clang