blob: 3fac75dc6fe67dc08c57547fe81e1511dc3a45e3 [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
Martin Probst7e0f25b2017-11-25 09:19:42 +000021#include <algorithm>
22
Chandler Carruth10346662014-04-22 03:17:02 +000023#define DEBUG_TYPE "format-parser"
24
Daniel Jasperf7935112012-12-03 18:12:45 +000025namespace clang {
26namespace format {
27
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000028class FormatTokenSource {
29public:
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000030 virtual ~FormatTokenSource() {}
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000031 virtual FormatToken *getNextToken() = 0;
32
33 virtual unsigned getPosition() = 0;
34 virtual FormatToken *setPosition(unsigned Position) = 0;
35};
36
Craig Topper69665e12013-07-01 04:21:54 +000037namespace {
38
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000039class ScopedDeclarationState {
40public:
41 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
42 bool MustBeDeclaration)
43 : Line(Line), Stack(Stack) {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000044 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek39080572013-01-23 11:03:04 +000045 Stack.push_back(MustBeDeclaration);
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000046 }
47 ~ScopedDeclarationState() {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000048 Stack.pop_back();
Manuel Klimekc1237a82013-01-23 14:08:21 +000049 if (!Stack.empty())
50 Line.MustBeDeclaration = Stack.back();
51 else
52 Line.MustBeDeclaration = true;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000053 }
Daniel Jasper393564f2013-05-31 14:56:29 +000054
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000055private:
56 UnwrappedLine &Line;
57 std::vector<bool> &Stack;
58};
59
Krasimir Georgieva1c30932017-05-19 10:34:57 +000060static bool isLineComment(const FormatToken &FormatTok) {
Krasimir Georgiev410ed242017-11-10 12:50:09 +000061 return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith("/*");
Krasimir Georgieva1c30932017-05-19 10:34:57 +000062}
63
Krasimir Georgievea222a72017-05-22 10:07:56 +000064// Checks if \p FormatTok is a line comment that continues the line comment
65// \p Previous. The original column of \p MinColumnToken is used to determine
66// whether \p FormatTok is indented enough to the right to continue \p Previous.
67static bool continuesLineComment(const FormatToken &FormatTok,
68 const FormatToken *Previous,
69 const FormatToken *MinColumnToken) {
70 if (!Previous || !MinColumnToken)
71 return false;
72 unsigned MinContinueColumn =
73 MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
74 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
75 isLineComment(*Previous) &&
76 FormatTok.OriginalColumn >= MinContinueColumn;
77}
78
Manuel Klimek1abf7892013-01-04 23:34:14 +000079class ScopedMacroState : public FormatTokenSource {
80public:
81 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek20e0af62015-05-06 11:56:29 +000082 FormatToken *&ResetToken)
Manuel Klimek1abf7892013-01-04 23:34:14 +000083 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000084 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
Krasimir Georgieva1c30932017-05-19 10:34:57 +000085 Token(nullptr), PreviousToken(nullptr) {
Manuel Klimek1abf7892013-01-04 23:34:14 +000086 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000087 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000088 Line.InPPDirective = true;
89 }
90
Alexander Kornienko34eb2072015-04-11 02:00:23 +000091 ~ScopedMacroState() override {
Manuel Klimek1abf7892013-01-04 23:34:14 +000092 TokenSource = PreviousTokenSource;
93 ResetToken = Token;
94 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000095 Line.Level = PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +000096 }
97
Craig Topperfb6b25b2014-03-15 04:29:04 +000098 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +000099 // The \c UnwrappedLineParser guards against this by never calling
100 // \c getNextToken() after it has encountered the first eof token.
101 assert(!eof());
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000102 PreviousToken = Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000103 Token = PreviousTokenSource->getNextToken();
104 if (eof())
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000105 return getFakeEOF();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000106 return Token;
107 }
108
Craig Topperfb6b25b2014-03-15 04:29:04 +0000109 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +0000110
Craig Topperfb6b25b2014-03-15 04:29:04 +0000111 FormatToken *setPosition(unsigned Position) override {
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000112 PreviousToken = nullptr;
Manuel Klimekab419912013-05-23 09:41:43 +0000113 Token = PreviousTokenSource->setPosition(Position);
114 return Token;
115 }
116
Manuel Klimek1abf7892013-01-04 23:34:14 +0000117private:
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000118 bool eof() {
119 return Token && Token->HasUnescapedNewline &&
Krasimir Georgievea222a72017-05-22 10:07:56 +0000120 !continuesLineComment(*Token, PreviousToken,
121 /*MinColumnToken=*/PreviousToken);
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000122 }
Manuel Klimek1abf7892013-01-04 23:34:14 +0000123
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000124 FormatToken *getFakeEOF() {
125 static bool EOFInitialized = false;
126 static FormatToken FormatTok;
127 if (!EOFInitialized) {
128 FormatTok.Tok.startToken();
129 FormatTok.Tok.setKind(tok::eof);
130 EOFInitialized = true;
131 }
132 return &FormatTok;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000133 }
134
135 UnwrappedLine &Line;
136 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000137 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000138 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000139 FormatTokenSource *PreviousTokenSource;
140
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000141 FormatToken *Token;
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000142 FormatToken *PreviousToken;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000143};
144
Craig Topper69665e12013-07-01 04:21:54 +0000145} // end anonymous namespace
146
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000147class ScopedLineState {
148public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000149 ScopedLineState(UnwrappedLineParser &Parser,
150 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000151 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000152 if (SwitchToPreprocessorLines)
153 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000154 else if (!Parser.Line->Tokens.empty())
155 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000156 PreBlockLine = std::move(Parser.Line);
157 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000158 Parser.Line->Level = PreBlockLine->Level;
159 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000160 }
161
162 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000163 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000164 Parser.addUnwrappedLine();
165 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000166 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000167 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000168 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
169 Parser.MustBreakBeforeNextToken = true;
170 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000171 }
172
173private:
174 UnwrappedLineParser &Parser;
175
David Blaikieefb6eb22014-08-09 20:02:07 +0000176 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000177 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000178};
179
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000180class CompoundStatementIndenter {
181public:
182 CompoundStatementIndenter(UnwrappedLineParser *Parser,
183 const FormatStyle &Style, unsigned &LineLevel)
184 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000185 if (Style.BraceWrapping.AfterControlStatement)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000186 Parser->addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000187 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000188 ++LineLevel;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000189 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000190 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000191
192private:
193 unsigned &LineLevel;
194 unsigned OldLineLevel;
195};
196
Craig Topper69665e12013-07-01 04:21:54 +0000197namespace {
198
Manuel Klimekab419912013-05-23 09:41:43 +0000199class IndexedTokenSource : public FormatTokenSource {
200public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000201 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000202 : Tokens(Tokens), Position(-1) {}
203
Craig Topperfb6b25b2014-03-15 04:29:04 +0000204 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000205 ++Position;
206 return Tokens[Position];
207 }
208
Craig Topperfb6b25b2014-03-15 04:29:04 +0000209 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000210 assert(Position >= 0);
211 return Position;
212 }
213
Craig Topperfb6b25b2014-03-15 04:29:04 +0000214 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000215 Position = P;
216 return Tokens[Position];
217 }
218
Manuel Klimek71814b42013-10-11 21:25:45 +0000219 void reset() { Position = -1; }
220
Manuel Klimekab419912013-05-23 09:41:43 +0000221private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000222 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000223 int Position;
224};
225
Craig Topper69665e12013-07-01 04:21:54 +0000226} // end anonymous namespace
227
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000228UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000229 const AdditionalKeywords &Keywords,
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000230 unsigned FirstStartColumn,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000231 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000232 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000233 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000234 CurrentLines(&Lines), Style(Style), Keywords(Keywords),
235 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
Krasimir Georgievad47c902017-08-30 14:34:57 +0000236 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
237 IfNdefCondition(nullptr), FoundIncludeGuardStart(false),
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000238 IncludeGuardRejected(false), FirstStartColumn(FirstStartColumn) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000239
240void UnwrappedLineParser::reset() {
241 PPBranchLevel = -1;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000242 IfNdefCondition = nullptr;
243 FoundIncludeGuardStart = false;
244 IncludeGuardRejected = false;
Manuel Klimek71814b42013-10-11 21:25:45 +0000245 Line.reset(new UnwrappedLine);
246 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000247 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000248 MustBreakBeforeNextToken = false;
249 PreprocessorDirectives.clear();
250 CurrentLines = &Lines;
251 DeclarationScopeStack.clear();
Manuel Klimek71814b42013-10-11 21:25:45 +0000252 PPStack.clear();
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000253 Line->FirstStartColumn = FirstStartColumn;
Manuel Klimek71814b42013-10-11 21:25:45 +0000254}
Daniel Jasperf7935112012-12-03 18:12:45 +0000255
Manuel Klimek20e0af62015-05-06 11:56:29 +0000256void UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000257 IndexedTokenSource TokenSource(AllTokens);
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000258 Line->FirstStartColumn = FirstStartColumn;
Manuel Klimek71814b42013-10-11 21:25:45 +0000259 do {
260 DEBUG(llvm::dbgs() << "----\n");
261 reset();
262 Tokens = &TokenSource;
263 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000264
Manuel Klimek71814b42013-10-11 21:25:45 +0000265 readToken();
266 parseFile();
267 // Create line with eof token.
268 pushToken(FormatTok);
269 addUnwrappedLine();
270
271 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
272 E = Lines.end();
273 I != E; ++I) {
274 Callback.consumeUnwrappedLine(*I);
275 }
276 Callback.finishRun();
277 Lines.clear();
278 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000279 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000280 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
281 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
282 }
283 if (!PPLevelBranchIndex.empty()) {
284 ++PPLevelBranchIndex.back();
285 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
286 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
287 }
288 } while (!PPLevelBranchIndex.empty());
Manuel Klimek1abf7892013-01-04 23:34:14 +0000289}
290
Manuel Klimek1a18c402013-04-12 14:13:36 +0000291void UnwrappedLineParser::parseFile() {
Daniel Jasper9326f912015-05-05 08:40:32 +0000292 // The top-level context in a file always has declarations, except for pre-
293 // processor directives and JavaScript files.
294 bool MustBeDeclaration =
295 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
296 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
297 MustBeDeclaration);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000298 if (Style.Language == FormatStyle::LK_TextProto)
299 parseBracedList();
300 else
301 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000302 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000303 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000304 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000305}
306
Manuel Klimek1a18c402013-04-12 14:13:36 +0000307void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000308 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000309 do {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000310 tok::TokenKind kind = FormatTok->Tok.getKind();
311 if (FormatTok->Type == TT_MacroBlockBegin) {
312 kind = tok::l_brace;
313 } else if (FormatTok->Type == TT_MacroBlockEnd) {
314 kind = tok::r_brace;
315 }
316
317 switch (kind) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000318 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000319 nextToken();
320 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000321 break;
322 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000323 // FIXME: Add parameter whether this can happen - if this happens, we must
324 // be in a non-declaration context.
Daniel Jasperb86e2722015-08-24 13:23:37 +0000325 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
326 continue;
Nico Weber9096fc02013-06-26 00:30:14 +0000327 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000328 addUnwrappedLine();
329 break;
330 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000331 if (HasOpeningBrace)
332 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000333 nextToken();
334 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000335 break;
Nico Weberc29f83b2018-01-23 16:30:56 +0000336 case tok::kw_default: {
337 unsigned StoredPosition = Tokens->getPosition();
338 FormatToken *Next = Tokens->getNextToken();
339 FormatTok = Tokens->setPosition(StoredPosition);
340 if (Next && Next->isNot(tok::colon)) {
341 // default not followed by ':' is not a case label; treat it like
342 // an identifier.
343 parseStructuralElement();
344 break;
345 }
346 // Else, if it is 'default:', fall through to the case handling.
347 }
Daniel Jasper516d7972013-07-25 11:31:57 +0000348 case tok::kw_case:
Manuel Klimek89628f62017-09-20 09:51:03 +0000349 if (Style.Language == FormatStyle::LK_JavaScript &&
350 Line->MustBeDeclaration) {
Martin Probstf785fd92017-08-04 17:07:15 +0000351 // A 'case: string' style field declaration.
352 parseStructuralElement();
353 break;
354 }
Daniel Jasper72407622013-09-02 08:26:29 +0000355 if (!SwitchLabelEncountered &&
356 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
357 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000358 SwitchLabelEncountered = true;
359 parseStructuralElement();
360 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000361 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000362 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000363 break;
364 }
365 } while (!eof());
366}
367
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000368void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
Manuel Klimekab419912013-05-23 09:41:43 +0000369 // We'll parse forward through the tokens until we hit
370 // a closing brace or eof - note that getNextToken() will
371 // parse macros, so this will magically work inside macro
372 // definitions, too.
373 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000374 FormatToken *Tok = FormatTok;
Manuel Klimek89628f62017-09-20 09:51:03 +0000375 const FormatToken *PrevTok = Tok->Previous;
Manuel Klimekab419912013-05-23 09:41:43 +0000376 // Keep a stack of positions of lbrace tokens. We will
377 // update information about whether an lbrace starts a
378 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000379 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000380 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000381 do {
Daniel Jaspereb65e912015-12-21 18:31:15 +0000382 // Get next non-comment token.
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000383 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000384 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000385 do {
386 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000387 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000388 } while (NextTok->is(tok::comment));
389
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000390 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000391 case tok::l_brace:
Martin Probst95ed8e72017-05-31 09:29:40 +0000392 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) {
Martin Probste8e27ca2017-11-25 09:33:47 +0000393 if (PrevTok->isOneOf(tok::colon, tok::less))
394 // A ':' indicates this code is in a type, or a braced list
395 // following a label in an object literal ({a: {b: 1}}).
396 // A '<' could be an object used in a comparison, but that is nonsense
397 // code (can never return true), so more likely it is a generic type
398 // argument (`X<{a: string; b: number}>`).
399 // The code below could be confused by semicolons between the
400 // individual members in a type member list, which would normally
401 // trigger BK_Block. In both cases, this must be parsed as an inline
402 // braced init.
Martin Probst95ed8e72017-05-31 09:29:40 +0000403 Tok->BlockKind = BK_BracedInit;
404 else if (PrevTok->is(tok::r_paren))
405 // `) { }` can only occur in function or method declarations in JS.
406 Tok->BlockKind = BK_Block;
407 } else {
Daniel Jasperb9a49902016-01-09 15:56:28 +0000408 Tok->BlockKind = BK_Unknown;
Martin Probst95ed8e72017-05-31 09:29:40 +0000409 }
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000410 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000411 break;
412 case tok::r_brace:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000413 if (LBraceStack.empty())
414 break;
415 if (LBraceStack.back()->BlockKind == BK_Unknown) {
416 bool ProbablyBracedList = false;
417 if (Style.Language == FormatStyle::LK_Proto) {
418 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
419 } else {
420 // Using OriginalColumn to distinguish between ObjC methods and
421 // binary operators is a bit hacky.
422 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
423 NextTok->OriginalColumn == 0;
Daniel Jasper91b032a2014-05-22 12:46:38 +0000424
Daniel Jasperb9a49902016-01-09 15:56:28 +0000425 // If there is a comma, semicolon or right paren after the closing
426 // brace, we assume this is a braced initializer list. Note that
427 // regardless how we mark inner braces here, we will overwrite the
428 // BlockKind later if we parse a braced list (where all blocks
429 // inside are by default braced lists), or when we explicitly detect
430 // blocks (for example while parsing lambdas).
Martin Probst95ed8e72017-05-31 09:29:40 +0000431 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
432 // braced list in JS.
Daniel Jasperb9a49902016-01-09 15:56:28 +0000433 ProbablyBracedList =
Daniel Jasperacffeb82016-03-05 18:34:26 +0000434 (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probste1e12a72016-08-19 14:35:01 +0000435 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
436 Keywords.kw_as)) ||
Martin Probstb7fb2672017-05-10 13:53:29 +0000437 (Style.isCpp() && NextTok->is(tok::l_paren)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000438 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
439 tok::r_paren, tok::r_square, tok::l_brace,
Martin Probstb7fb2672017-05-10 13:53:29 +0000440 tok::l_square, tok::ellipsis) ||
Daniel Jaspere4ada022016-12-13 10:05:03 +0000441 (NextTok->is(tok::identifier) &&
442 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000443 (NextTok->is(tok::semi) &&
444 (!ExpectClassBody || LBraceStack.size() != 1)) ||
445 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Manuel Klimekab419912013-05-23 09:41:43 +0000446 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000447 if (ProbablyBracedList) {
448 Tok->BlockKind = BK_BracedInit;
449 LBraceStack.back()->BlockKind = BK_BracedInit;
450 } else {
451 Tok->BlockKind = BK_Block;
452 LBraceStack.back()->BlockKind = BK_Block;
453 }
Manuel Klimekab419912013-05-23 09:41:43 +0000454 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000455 LBraceStack.pop_back();
Manuel Klimekab419912013-05-23 09:41:43 +0000456 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000457 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000458 case tok::semi:
459 case tok::kw_if:
460 case tok::kw_while:
461 case tok::kw_for:
462 case tok::kw_switch:
463 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000464 case tok::kw___try:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000465 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000466 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000467 break;
468 default:
469 break;
470 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000471 PrevTok = Tok;
Manuel Klimekab419912013-05-23 09:41:43 +0000472 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000473 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Daniel Jasperb9a49902016-01-09 15:56:28 +0000474
Manuel Klimekab419912013-05-23 09:41:43 +0000475 // Assume other blocks for all unclosed opening braces.
476 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000477 if (LBraceStack[i]->BlockKind == BK_Unknown)
478 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000479 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000480
Manuel Klimekab419912013-05-23 09:41:43 +0000481 FormatTok = Tokens->setPosition(StoredPosition);
482}
483
Francois Ferranda98a95c2017-07-28 07:56:14 +0000484template <class T>
485static inline void hash_combine(std::size_t &seed, const T &v) {
486 std::hash<T> hasher;
487 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
488}
489
490size_t UnwrappedLineParser::computePPHash() const {
491 size_t h = 0;
492 for (const auto &i : PPStack) {
493 hash_combine(h, size_t(i.Kind));
494 hash_combine(h, i.Line);
495 }
496 return h;
497}
498
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000499void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
500 bool MunchSemi) {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000501 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
502 "'{' or macro block token expected");
503 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
Daniel Jaspereb65e912015-12-21 18:31:15 +0000504 FormatTok->BlockKind = BK_Block;
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000505
Francois Ferranda98a95c2017-07-28 07:56:14 +0000506 size_t PPStartHash = computePPHash();
507
Daniel Jasper516d7972013-07-25 11:31:57 +0000508 unsigned InitialLevel = Line->Level;
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000509 nextToken(/*LevelDifference=*/AddLevel ? 1 : 0);
Daniel Jasperf7935112012-12-03 18:12:45 +0000510
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000511 if (MacroBlock && FormatTok->is(tok::l_paren))
512 parseParens();
513
Francois Ferranda98a95c2017-07-28 07:56:14 +0000514 size_t NbPreprocessorDirectives =
515 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000516 addUnwrappedLine();
Francois Ferranda98a95c2017-07-28 07:56:14 +0000517 size_t OpeningLineIndex =
518 CurrentLines->empty()
519 ? (UnwrappedLine::kInvalidIndex)
520 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
Daniel Jasperf7935112012-12-03 18:12:45 +0000521
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000522 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
523 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000524 if (AddLevel)
525 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000526 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000527
Marianne Mailhot-Sarrasin03137c62016-04-14 14:56:49 +0000528 if (eof())
529 return;
530
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000531 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
532 : !FormatTok->is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000533 Line->Level = InitialLevel;
Daniel Jaspereb65e912015-12-21 18:31:15 +0000534 FormatTok->BlockKind = BK_Block;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000535 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000536 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000537
Francois Ferranda98a95c2017-07-28 07:56:14 +0000538 size_t PPEndHash = computePPHash();
539
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000540 // Munch the closing brace.
541 nextToken(/*LevelDifference=*/AddLevel ? -1 : 0);
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000542
543 if (MacroBlock && FormatTok->is(tok::l_paren))
544 parseParens();
545
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000546 if (MunchSemi && FormatTok->Tok.is(tok::semi))
547 nextToken();
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000548 Line->Level = InitialLevel;
Francois Ferranda98a95c2017-07-28 07:56:14 +0000549
550 if (PPStartHash == PPEndHash) {
551 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
552 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
553 // Update the opening line to add the forward reference as well
554 (*CurrentLines)[OpeningLineIndex].MatchingOpeningBlockLineIndex =
555 CurrentLines->size() - 1;
556 }
Francois Ferrande56a8292017-06-14 12:29:47 +0000557 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000558}
559
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000560static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000561 // FIXME: Closure-library specific stuff should not be hard-coded but be
562 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000563 if (Line.Tokens.size() < 4)
564 return false;
565 auto I = Line.Tokens.begin();
566 if (I->Tok->TokenText != "goog")
567 return false;
568 ++I;
569 if (I->Tok->isNot(tok::period))
570 return false;
571 ++I;
572 if (I->Tok->TokenText != "scope")
573 return false;
574 ++I;
575 return I->Tok->is(tok::l_paren);
576}
577
Martin Probst101ec892017-05-09 20:04:09 +0000578static bool isIIFE(const UnwrappedLine &Line,
579 const AdditionalKeywords &Keywords) {
580 // Look for the start of an immediately invoked anonymous function.
581 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
582 // This is commonly done in JavaScript to create a new, anonymous scope.
583 // Example: (function() { ... })()
584 if (Line.Tokens.size() < 3)
585 return false;
586 auto I = Line.Tokens.begin();
587 if (I->Tok->isNot(tok::l_paren))
588 return false;
589 ++I;
590 if (I->Tok->isNot(Keywords.kw_function))
591 return false;
592 ++I;
593 return I->Tok->is(tok::l_paren);
594}
595
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000596static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
597 const FormatToken &InitialToken) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000598 if (InitialToken.is(tok::kw_namespace))
599 return Style.BraceWrapping.AfterNamespace;
600 if (InitialToken.is(tok::kw_class))
601 return Style.BraceWrapping.AfterClass;
602 if (InitialToken.is(tok::kw_union))
603 return Style.BraceWrapping.AfterUnion;
604 if (InitialToken.is(tok::kw_struct))
605 return Style.BraceWrapping.AfterStruct;
606 return false;
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000607}
608
Manuel Klimek516e0542013-09-04 13:25:30 +0000609void UnwrappedLineParser::parseChildBlock() {
610 FormatTok->BlockKind = BK_Block;
611 nextToken();
612 {
Manuel Klimek89628f62017-09-20 09:51:03 +0000613 bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript &&
614 (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
Manuel Klimek516e0542013-09-04 13:25:30 +0000615 ScopedLineState LineState(*this);
616 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
617 /*MustBeDeclaration=*/false);
Martin Probst101ec892017-05-09 20:04:09 +0000618 Line->Level += SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000619 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000620 flushComments(isOnNewLine(*FormatTok));
Martin Probst101ec892017-05-09 20:04:09 +0000621 Line->Level -= SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000622 }
623 nextToken();
624}
625
Daniel Jasperf7935112012-12-03 18:12:45 +0000626void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000627 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000628 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000629 nextToken();
630
Craig Topper2145bc02014-05-09 08:15:10 +0000631 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000632 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000633 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000634 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000635
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000636 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000637 case tok::pp_define:
638 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000639 return;
640 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000641 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000642 break;
643 case tok::pp_ifdef:
644 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000645 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000646 break;
647 case tok::pp_else:
648 parsePPElse();
649 break;
650 case tok::pp_elif:
651 parsePPElIf();
652 break;
653 case tok::pp_endif:
654 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000655 break;
656 default:
657 parsePPUnknown();
658 break;
659 }
660}
661
Manuel Klimek68b03042014-04-14 09:14:11 +0000662void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
Francois Ferranda98a95c2017-07-28 07:56:14 +0000663 size_t Line = CurrentLines->size();
664 if (CurrentLines == &PreprocessorDirectives)
665 Line += Lines.size();
666
667 if (Unreachable ||
668 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
669 PPStack.push_back({PP_Unreachable, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000670 else
Francois Ferranda98a95c2017-07-28 07:56:14 +0000671 PPStack.push_back({PP_Conditional, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000672}
673
Manuel Klimek68b03042014-04-14 09:14:11 +0000674void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000675 ++PPBranchLevel;
676 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
677 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
678 PPLevelBranchIndex.push_back(0);
679 PPLevelBranchCount.push_back(0);
680 }
681 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000682 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
683 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000684}
685
Manuel Klimek68b03042014-04-14 09:14:11 +0000686void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000687 if (!PPStack.empty())
688 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000689 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
690 if (!PPChainBranchIndex.empty())
691 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000692 conditionalCompilationCondition(
693 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
694 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000695}
696
Manuel Klimek68b03042014-04-14 09:14:11 +0000697void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000698 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
699 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
700 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000701 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
702 }
703 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000704 // Guard against #endif's without #if.
Krasimir Georgievad47c902017-08-30 14:34:57 +0000705 if (PPBranchLevel > -1)
Manuel Klimek14bd9172014-01-29 08:49:02 +0000706 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000707 if (!PPChainBranchIndex.empty())
708 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000709 if (!PPStack.empty())
710 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000711}
712
713void UnwrappedLineParser::parsePPIf(bool IfDef) {
Daniel Jasper62703eb2017-03-01 11:10:11 +0000714 bool IfNDef = FormatTok->is(tok::pp_ifndef);
Manuel Klimek68b03042014-04-14 09:14:11 +0000715 nextToken();
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000716 bool Unreachable = false;
717 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
718 Unreachable = true;
Daniel Jasper62703eb2017-03-01 11:10:11 +0000719 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000720 Unreachable = true;
721 conditionalCompilationStart(Unreachable);
Krasimir Georgievad47c902017-08-30 14:34:57 +0000722 FormatToken *IfCondition = FormatTok;
723 // If there's a #ifndef on the first line, and the only lines before it are
724 // comments, it could be an include guard.
725 bool MaybeIncludeGuard = IfNDef;
726 if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard) {
727 for (auto &Line : Lines) {
728 if (!Line.Tokens.front().Tok->is(tok::comment)) {
729 MaybeIncludeGuard = false;
730 IncludeGuardRejected = true;
731 break;
732 }
733 }
734 }
735 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000736 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000737 ++PPBranchLevel;
738 if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard)
739 IfNdefCondition = IfCondition;
Manuel Klimek68b03042014-04-14 09:14:11 +0000740}
741
742void UnwrappedLineParser::parsePPElse() {
Krasimir Georgievad47c902017-08-30 14:34:57 +0000743 // If a potential include guard has an #else, it's not an include guard.
744 if (FoundIncludeGuardStart && PPBranchLevel == 0)
745 FoundIncludeGuardStart = false;
Manuel Klimek68b03042014-04-14 09:14:11 +0000746 conditionalCompilationAlternative();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000747 if (PPBranchLevel > -1)
748 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000749 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000750 ++PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000751}
752
753void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
754
755void UnwrappedLineParser::parsePPEndIf() {
756 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000757 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000758 // If the #endif of a potential include guard is the last thing in the file,
759 // then we count it as a real include guard and subtract one from every
760 // preprocessor indent.
761 unsigned TokenPosition = Tokens->getPosition();
762 FormatToken *PeekNext = AllTokens[TokenPosition];
Daniel Jasper4df130f2017-09-04 13:33:52 +0000763 if (FoundIncludeGuardStart && PPBranchLevel == -1 && PeekNext->is(tok::eof) &&
764 Style.IndentPPDirectives != FormatStyle::PPDIS_None)
765 for (auto &Line : Lines)
Krasimir Georgievad47c902017-08-30 14:34:57 +0000766 if (Line.InPPDirective && Line.Level > 0)
767 --Line.Level;
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000768}
769
Manuel Klimek1abf7892013-01-04 23:34:14 +0000770void UnwrappedLineParser::parsePPDefine() {
771 nextToken();
772
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000773 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000774 parsePPUnknown();
775 return;
776 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000777 if (IfNdefCondition && IfNdefCondition->TokenText == FormatTok->TokenText) {
778 FoundIncludeGuardStart = true;
779 for (auto &Line : Lines) {
780 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
781 FoundIncludeGuardStart = false;
782 break;
783 }
784 }
785 }
786 IfNdefCondition = nullptr;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000787 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000788 if (FormatTok->Tok.getKind() == tok::l_paren &&
789 FormatTok->WhitespaceRange.getBegin() ==
790 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000791 parseParens();
792 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000793 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
794 Line->Level += PPBranchLevel + 1;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000795 addUnwrappedLine();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000796 ++Line->Level;
Manuel Klimek1b896292013-01-07 09:34:28 +0000797
798 // Errors during a preprocessor directive can only affect the layout of the
799 // preprocessor directive, and thus we ignore them. An alternative approach
800 // would be to use the same approach we use on the file level (no
801 // re-indentation if there was a structural error) within the macro
802 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000803 parseFile();
804}
805
806void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000807 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000808 nextToken();
809 } while (!eof());
Krasimir Georgievad47c902017-08-30 14:34:57 +0000810 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
811 Line->Level += PPBranchLevel + 1;
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000812 addUnwrappedLine();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000813 IfNdefCondition = nullptr;
Daniel Jasperf7935112012-12-03 18:12:45 +0000814}
815
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000816// Here we blacklist certain tokens that are not usually the first token in an
817// unwrapped line. This is used in attempt to distinguish macro calls without
818// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000819static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000820 // Semicolon can be a null-statement, l_square can be a start of a macro or
821 // a C++11 attribute, but this doesn't seem to be common.
822 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
823 Tok.isNot(tok::l_square) &&
824 // Tokens that can only be used as binary operators and a part of
825 // overloaded operator names.
826 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
827 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
828 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
829 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
830 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
831 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
832 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
833 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
834 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
835 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
836 Tok.isNot(tok::lesslessequal) &&
837 // Colon is used in labels, base class lists, initializer lists,
838 // range-based for loops, ternary operator, but should never be the
839 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000840 Tok.isNot(tok::colon) &&
841 // 'noexcept' is a trailing annotation.
842 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000843}
844
Martin Probst533965c2016-04-19 18:19:06 +0000845static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
846 const FormatToken *FormatTok) {
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000847 // FIXME: This returns true for C/C++ keywords like 'struct'.
848 return FormatTok->is(tok::identifier) &&
849 (FormatTok->Tok.getIdentifierInfo() == nullptr ||
Martin Probst3dbbefa2016-11-10 16:21:02 +0000850 !FormatTok->isOneOf(
851 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
852 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
853 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
854 Keywords.kw_let, Keywords.kw_var, tok::kw_const,
855 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
Manuel Klimek89628f62017-09-20 09:51:03 +0000856 Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws,
857 Keywords.kw_from));
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000858}
859
Martin Probst533965c2016-04-19 18:19:06 +0000860static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
861 const FormatToken *FormatTok) {
Martin Probstb9316ff2016-09-18 17:21:52 +0000862 return FormatTok->Tok.isLiteral() ||
863 FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
864 mustBeJSIdent(Keywords, FormatTok);
Martin Probst533965c2016-04-19 18:19:06 +0000865}
866
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000867// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
868// when encountered after a value (see mustBeJSIdentOrValue).
869static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
870 const FormatToken *FormatTok) {
871 return FormatTok->isOneOf(
Martin Probst5f8445b2016-04-24 22:05:09 +0000872 tok::kw_return, Keywords.kw_yield,
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000873 // conditionals
874 tok::kw_if, tok::kw_else,
875 // loops
876 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
877 // switch/case
878 tok::kw_switch, tok::kw_case,
879 // exceptions
880 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
881 // declaration
882 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
Martin Probst5f8445b2016-04-24 22:05:09 +0000883 Keywords.kw_async, Keywords.kw_function,
884 // import/export
885 Keywords.kw_import, tok::kw_export);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000886}
887
888// readTokenWithJavaScriptASI reads the next token and terminates the current
889// line if JavaScript Automatic Semicolon Insertion must
890// happen between the current token and the next token.
891//
892// This method is conservative - it cannot cover all edge cases of JavaScript,
893// but only aims to correctly handle certain well known cases. It *must not*
894// return true in speculative cases.
895void UnwrappedLineParser::readTokenWithJavaScriptASI() {
896 FormatToken *Previous = FormatTok;
897 readToken();
898 FormatToken *Next = FormatTok;
899
900 bool IsOnSameLine =
901 CommentsBeforeNextToken.empty()
902 ? Next->NewlinesBefore == 0
903 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
904 if (IsOnSameLine)
905 return;
906
907 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
Martin Probst717f6dc2016-10-21 05:11:38 +0000908 bool PreviousStartsTemplateExpr =
909 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${");
Martin Probst7e0f25b2017-11-25 09:19:42 +0000910 if (PreviousMustBeValue || Previous->is(tok::r_paren)) {
911 // If the line contains an '@' sign, the previous token might be an
912 // annotation, which can precede another identifier/value.
913 bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
914 [](UnwrappedLineNode &LineNode) {
915 return LineNode.Tok->is(tok::at);
916 }) != Line->Tokens.end();
917 if (HasAt)
Martin Probstbbffeac2016-04-11 07:35:57 +0000918 return;
919 }
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000920 if (Next->is(tok::exclaim) && PreviousMustBeValue)
Martin Probstd40bca42017-01-09 08:56:36 +0000921 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000922 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
Martin Probst717f6dc2016-10-21 05:11:38 +0000923 bool NextEndsTemplateExpr =
924 Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
925 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
926 (PreviousMustBeValue ||
927 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
928 tok::minusminus)))
Martin Probstd40bca42017-01-09 08:56:36 +0000929 return addUnwrappedLine();
Martin Probst0a19d432017-08-09 15:19:16 +0000930 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) &&
931 isJSDeclOrStmt(Keywords, Next))
Martin Probstd40bca42017-01-09 08:56:36 +0000932 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000933}
934
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000935void UnwrappedLineParser::parseStructuralElement() {
Daniel Jasper498f5582015-12-25 08:53:31 +0000936 assert(!FormatTok->is(tok::l_brace));
937 if (Style.Language == FormatStyle::LK_TableGen &&
938 FormatTok->is(tok::pp_include)) {
939 nextToken();
940 if (FormatTok->is(tok::string_literal))
941 nextToken();
942 addUnwrappedLine();
943 return;
944 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000945 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000946 case tok::at:
947 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000948 if (FormatTok->Tok.is(tok::l_brace)) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000949 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +0000950 parseBracedList();
951 break;
952 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000953 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000954 case tok::objc_public:
955 case tok::objc_protected:
956 case tok::objc_package:
957 case tok::objc_private:
958 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000959 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000960 case tok::objc_implementation:
961 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000962 case tok::objc_protocol:
963 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000964 case tok::objc_end:
965 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000966 case tok::objc_optional:
967 case tok::objc_required:
968 nextToken();
969 addUnwrappedLine();
970 return;
Nico Weber45c48122015-06-28 01:06:16 +0000971 case tok::objc_autoreleasepool:
972 nextToken();
973 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000974 if (Style.BraceWrapping.AfterObjCDeclaration)
Nico Weber45c48122015-06-28 01:06:16 +0000975 addUnwrappedLine();
976 parseBlock(/*MustBeDeclaration=*/false);
977 }
978 addUnwrappedLine();
979 return;
Nico Weber33381f52015-02-07 01:57:32 +0000980 case tok::objc_try:
981 // This branch isn't strictly necessary (the kw_try case below would
982 // do this too after the tok::at is parsed above). But be explicit.
983 parseTryCatch();
984 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000985 default:
986 break;
987 }
988 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000989 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000990 nextToken();
991 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000992 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000993 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000994 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000995 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000996 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000997 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000998 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000999 break;
1000 }
Daniel Jasper2337f282015-01-12 10:14:56 +00001001 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +00001002 nextToken();
1003 }
1004 }
1005 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001006 case tok::kw_namespace:
1007 parseNamespace();
1008 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +00001009 case tok::kw_inline:
1010 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001011 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +00001012 parseNamespace();
1013 return;
1014 }
1015 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001016 case tok::kw_public:
1017 case tok::kw_protected:
1018 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +00001019 if (Style.Language == FormatStyle::LK_Java ||
1020 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001021 nextToken();
1022 else
1023 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +00001024 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001025 case tok::kw_if:
1026 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +00001027 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001028 case tok::kw_for:
1029 case tok::kw_while:
1030 parseForOrWhileLoop();
1031 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001032 case tok::kw_do:
1033 parseDoWhile();
1034 return;
1035 case tok::kw_switch:
Martin Probstf785fd92017-08-04 17:07:15 +00001036 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1037 // 'switch: string' field declaration.
1038 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001039 parseSwitch();
1040 return;
1041 case tok::kw_default:
Martin Probstf785fd92017-08-04 17:07:15 +00001042 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1043 // 'default: string' field declaration.
1044 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001045 nextToken();
Nico Weberc29f83b2018-01-23 16:30:56 +00001046 if (FormatTok->is(tok::colon)) {
1047 parseLabel();
1048 return;
1049 }
1050 // e.g. "default void f() {}" in a Java interface.
1051 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001052 case tok::kw_case:
Martin Probstf785fd92017-08-04 17:07:15 +00001053 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1054 // 'case: string' field declaration.
1055 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001056 parseCaseLabel();
1057 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001058 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +00001059 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +00001060 parseTryCatch();
1061 return;
Manuel Klimekae610d12013-01-21 14:32:05 +00001062 case tok::kw_extern:
1063 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001064 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +00001065 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001066 if (FormatTok->Tok.is(tok::l_brace)) {
Krasimir Georgievd6ce9372017-09-15 11:23:50 +00001067 if (Style.BraceWrapping.AfterExternBlock) {
1068 addUnwrappedLine();
1069 parseBlock(/*MustBeDeclaration=*/true);
1070 } else {
1071 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
1072 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001073 addUnwrappedLine();
1074 return;
1075 }
1076 }
Daniel Jaspere1e43192014-04-01 12:55:11 +00001077 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +00001078 case tok::kw_export:
1079 if (Style.Language == FormatStyle::LK_JavaScript) {
1080 parseJavaScriptEs6ImportExport();
1081 return;
1082 }
1083 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +00001084 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001085 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001086 parseForOrWhileLoop();
1087 return;
1088 }
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001089 if (FormatTok->is(TT_MacroBlockBegin)) {
1090 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
1091 /*MunchSemi=*/false);
1092 return;
1093 }
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001094 if (FormatTok->is(Keywords.kw_import)) {
1095 if (Style.Language == FormatStyle::LK_JavaScript) {
1096 parseJavaScriptEs6ImportExport();
1097 return;
1098 }
1099 if (Style.Language == FormatStyle::LK_Proto) {
1100 nextToken();
Daniel Jasper8b61d142016-06-20 20:39:53 +00001101 if (FormatTok->is(tok::kw_public))
1102 nextToken();
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001103 if (!FormatTok->is(tok::string_literal))
1104 return;
1105 nextToken();
1106 if (FormatTok->is(tok::semi))
1107 nextToken();
1108 addUnwrappedLine();
1109 return;
1110 }
Daniel Jasper354aa512015-02-19 16:07:32 +00001111 }
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001112 if (Style.isCpp() &&
Daniel Jasper72b33572017-03-31 12:04:37 +00001113 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
Daniel Jaspera00de632015-12-01 12:05:04 +00001114 Keywords.kw_slots, Keywords.kw_qslots)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001115 nextToken();
1116 if (FormatTok->is(tok::colon)) {
1117 nextToken();
1118 addUnwrappedLine();
Daniel Jasper31343832016-07-27 10:13:24 +00001119 return;
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001120 }
Daniel Jasper53395402015-04-07 15:04:40 +00001121 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001122 // In all other cases, parse the declaration.
1123 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001124 default:
1125 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001126 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001127 do {
Manuel Klimeke411aa82017-09-20 09:29:37 +00001128 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001129 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +00001130 case tok::at:
1131 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001132 if (FormatTok->Tok.is(tok::l_brace)) {
1133 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001134 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001135 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001136 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001137 case tok::kw_enum:
Daniel Jaspera7900ad2016-05-08 18:12:22 +00001138 // Ignore if this is part of "template <enum ...".
1139 if (Previous && Previous->is(tok::less)) {
1140 nextToken();
1141 break;
1142 }
1143
Daniel Jasper90cf3802015-06-17 09:44:02 +00001144 // parseEnum falls through and does not yet add an unwrapped line as an
1145 // enum definition can start a structural element.
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001146 if (!parseEnum())
1147 break;
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001148 // This only applies for C++.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001149 if (!Style.isCpp()) {
Daniel Jasper90cf3802015-06-17 09:44:02 +00001150 addUnwrappedLine();
1151 return;
1152 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001153 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001154 case tok::kw_typedef:
1155 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +00001156 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1157 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001158 parseEnum();
1159 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001160 case tok::kw_struct:
1161 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +00001162 case tok::kw_class:
Daniel Jasper910807d2015-06-12 04:52:02 +00001163 // parseRecord falls through and does not yet add an unwrapped line as a
1164 // record declaration or definition can start a structural element.
Manuel Klimeke01bab52013-01-15 13:38:33 +00001165 parseRecord();
Daniel Jasper910807d2015-06-12 04:52:02 +00001166 // This does not apply for Java and JavaScript.
1167 if (Style.Language == FormatStyle::LK_Java ||
1168 Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperd5ec65b2016-01-08 07:06:07 +00001169 if (FormatTok->is(tok::semi))
1170 nextToken();
Daniel Jasper910807d2015-06-12 04:52:02 +00001171 addUnwrappedLine();
1172 return;
1173 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001174 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +00001175 case tok::period:
1176 nextToken();
1177 // In Java, classes have an implicit static member "class".
1178 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
1179 FormatTok->is(tok::kw_class))
1180 nextToken();
Daniel Jasperba52fcb2015-09-28 14:29:45 +00001181 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
1182 FormatTok->Tok.getIdentifierInfo())
1183 // JavaScript only has pseudo keywords, all keywords are allowed to
1184 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1185 nextToken();
Daniel Jaspere5d74862014-11-26 08:17:08 +00001186 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001187 case tok::semi:
1188 nextToken();
1189 addUnwrappedLine();
1190 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001191 case tok::r_brace:
1192 addUnwrappedLine();
1193 return;
Daniel Jasperf7935112012-12-03 18:12:45 +00001194 case tok::l_paren:
1195 parseParens();
1196 break;
Daniel Jasper5af04a42015-10-07 03:43:10 +00001197 case tok::kw_operator:
1198 nextToken();
1199 if (FormatTok->isBinaryOperator())
1200 nextToken();
1201 break;
Manuel Klimek516e0542013-09-04 13:25:30 +00001202 case tok::caret:
1203 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +00001204 if (FormatTok->Tok.isAnyIdentifier() ||
1205 FormatTok->isSimpleTypeSpecifier())
1206 nextToken();
1207 if (FormatTok->is(tok::l_paren))
1208 parseParens();
1209 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +00001210 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +00001211 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001212 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001213 if (!tryToParseBracedList()) {
1214 // A block outside of parentheses must be the last part of a
1215 // structural element.
1216 // FIXME: Figure out cases where this is not true, and add projections
1217 // for them (the one we know is missing are lambdas).
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001218 if (Style.BraceWrapping.AfterFunction)
Manuel Klimekab419912013-05-23 09:41:43 +00001219 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +00001220 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +00001221 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001222 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +00001223 return;
1224 }
1225 // Otherwise this was a braced init list, and the structural
1226 // element continues.
1227 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001228 case tok::kw_try:
1229 // We arrive here when parsing function-try blocks.
1230 parseTryCatch();
1231 return;
Daniel Jasper40e19212013-05-29 13:16:10 +00001232 case tok::identifier: {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001233 if (FormatTok->is(TT_MacroBlockEnd)) {
1234 addUnwrappedLine();
1235 return;
1236 }
1237
Martin Probst973ff792017-04-27 13:07:24 +00001238 // Function declarations (as opposed to function expressions) are parsed
1239 // on their own unwrapped line by continuing this loop. Function
1240 // expressions (functions that are not on their own line) must not create
1241 // a new unwrapped line, so they are special cased below.
1242 size_t TokenCount = Line->Tokens.size();
Daniel Jasper9326f912015-05-05 08:40:32 +00001243 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst973ff792017-04-27 13:07:24 +00001244 FormatTok->is(Keywords.kw_function) &&
1245 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1246 Keywords.kw_async)))) {
Daniel Jasper069e5f42014-05-20 11:14:57 +00001247 tryToParseJSFunction();
1248 break;
1249 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001250 if ((Style.Language == FormatStyle::LK_JavaScript ||
1251 Style.Language == FormatStyle::LK_Java) &&
1252 FormatTok->is(Keywords.kw_interface)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001253 if (Style.Language == FormatStyle::LK_JavaScript) {
1254 // In JavaScript/TypeScript, "interface" can be used as a standalone
1255 // identifier, e.g. in `var interface = 1;`. If "interface" is
1256 // followed by another identifier, it is very like to be an actual
1257 // interface declaration.
1258 unsigned StoredPosition = Tokens->getPosition();
1259 FormatToken *Next = Tokens->getNextToken();
1260 FormatTok = Tokens->setPosition(StoredPosition);
Martin Probst533965c2016-04-19 18:19:06 +00001261 if (Next && !mustBeJSIdent(Keywords, Next)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001262 nextToken();
1263 break;
1264 }
1265 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001266 parseRecord();
Daniel Jasper259188b2015-06-12 04:56:34 +00001267 addUnwrappedLine();
Daniel Jasper5c235c02015-07-06 14:26:04 +00001268 return;
Daniel Jasper9326f912015-05-05 08:40:32 +00001269 }
1270
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001271 // See if the following token should start a new unwrapped line.
Daniel Jasper9326f912015-05-05 08:40:32 +00001272 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +00001273 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +00001274 if (Line->Tokens.size() == 1 &&
1275 // JS doesn't have macros, and within classes colons indicate fields,
1276 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +00001277 Style.Language != FormatStyle::LK_JavaScript) {
1278 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Daniel Jasper40609472016-04-06 15:02:46 +00001279 Line->Tokens.begin()->Tok->MustBreakBefore = true;
Alexander Kornienkode644272013-04-08 22:16:06 +00001280 parseLabel();
1281 return;
1282 }
Daniel Jasper680b09b2014-11-05 10:48:04 +00001283 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +00001284 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +00001285 bool FunctionLike = FormatTok->is(tok::l_paren);
1286 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +00001287 parseParens();
Daniel Jaspere60cba12015-05-13 11:35:53 +00001288
1289 bool FollowedByNewline =
1290 CommentsBeforeNextToken.empty()
1291 ? FormatTok->NewlinesBefore > 0
1292 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1293
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001294 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
Daniel Jasper680b09b2014-11-05 10:48:04 +00001295 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +00001296 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +00001297 return;
Alexander Kornienkode644272013-04-08 22:16:06 +00001298 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001299 }
1300 break;
Daniel Jasper40e19212013-05-29 13:16:10 +00001301 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001302 case tok::equal:
Manuel Klimek79e06082015-05-21 12:23:34 +00001303 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1304 // TT_JsFatArrow. The always start an expression or a child block if
1305 // followed by a curly.
1306 if (FormatTok->is(TT_JsFatArrow)) {
1307 nextToken();
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001308 if (FormatTok->is(tok::l_brace))
Manuel Klimek79e06082015-05-21 12:23:34 +00001309 parseChildBlock();
Manuel Klimek79e06082015-05-21 12:23:34 +00001310 break;
1311 }
1312
Daniel Jaspere25509f2012-12-17 11:29:41 +00001313 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001314 if (FormatTok->Tok.is(tok::l_brace)) {
1315 nextToken();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001316 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001317 } else if (Style.Language == FormatStyle::LK_Proto &&
Manuel Klimek89628f62017-09-20 09:51:03 +00001318 FormatTok->Tok.is(tok::less)) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001319 nextToken();
Krasimir Georgiev0b41fcb2017-06-27 13:58:41 +00001320 parseBracedList(/*ContinueOnSemicolons=*/false,
1321 /*ClosingBraceKind=*/tok::greater);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001322 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001323 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001324 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001325 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001326 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +00001327 case tok::kw_new:
1328 parseNew();
1329 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001330 default:
1331 nextToken();
1332 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001333 }
1334 } while (!eof());
1335}
1336
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001337bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001338 if (!Style.isCpp()) {
Daniel Jasper1feab0f2015-06-02 15:31:37 +00001339 nextToken();
1340 return false;
1341 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001342 assert(FormatTok->is(tok::l_square));
1343 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001344 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001345 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001346
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001347 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001348 if (FormatTok->isSimpleTypeSpecifier()) {
1349 nextToken();
1350 continue;
1351 }
Manuel Klimekffdeb592013-09-03 15:10:01 +00001352 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001353 case tok::l_brace:
1354 break;
1355 case tok::l_paren:
1356 parseParens();
1357 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +00001358 case tok::amp:
1359 case tok::star:
1360 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +00001361 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001362 case tok::less:
1363 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001364 case tok::identifier:
Daniel Jasper5eaa0092015-08-13 13:37:08 +00001365 case tok::numeric_constant:
Daniel Jasper1067ab02014-02-11 10:16:55 +00001366 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001367 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +00001368 nextToken();
1369 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001370 case tok::arrow:
Daniel Jasper6f2b88a2015-06-05 13:18:09 +00001371 FormatTok->Type = TT_LambdaArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001372 nextToken();
1373 break;
1374 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001375 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001376 }
1377 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001378 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +00001379 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001380 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001381}
1382
1383bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
Manuel Klimek89628f62017-09-20 09:51:03 +00001384 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001385 if (Previous &&
1386 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1387 tok::kw_delete) ||
Manuel Klimek89628f62017-09-20 09:51:03 +00001388 FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() ||
1389 Previous->isSimpleTypeSpecifier())) {
Manuel Klimekffdeb592013-09-03 15:10:01 +00001390 nextToken();
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001391 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001392 }
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001393 nextToken();
1394 parseSquare(/*LambdaIntroducer=*/true);
1395 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001396}
1397
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001398void UnwrappedLineParser::tryToParseJSFunction() {
Martin Probst409697e2016-05-29 14:41:07 +00001399 assert(FormatTok->is(Keywords.kw_function) ||
1400 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
Martin Probst5f8445b2016-04-24 22:05:09 +00001401 if (FormatTok->is(Keywords.kw_async))
1402 nextToken();
1403 // Consume "function".
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001404 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001405
Daniel Jasper71e50af2016-11-01 06:22:59 +00001406 // Consume * (generator function). Treat it like C++'s overloaded operators.
1407 if (FormatTok->is(tok::star)) {
1408 FormatTok->Type = TT_OverloadedOperator;
Martin Probst5f8445b2016-04-24 22:05:09 +00001409 nextToken();
Daniel Jasper71e50af2016-11-01 06:22:59 +00001410 }
Martin Probst5f8445b2016-04-24 22:05:09 +00001411
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001412 // Consume function name.
1413 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001414 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001415
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001416 if (FormatTok->isNot(tok::l_paren))
1417 return;
Manuel Klimek79e06082015-05-21 12:23:34 +00001418
1419 // Parse formal parameter list.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001420 parseParens();
Manuel Klimek79e06082015-05-21 12:23:34 +00001421
1422 if (FormatTok->is(tok::colon)) {
1423 // Parse a type definition.
1424 nextToken();
1425
1426 // Eat the type declaration. For braced inline object types, balance braces,
1427 // otherwise just parse until finding an l_brace for the function body.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001428 if (FormatTok->is(tok::l_brace))
1429 tryToParseBracedList();
1430 else
Martin Probstaf16c502017-01-04 13:36:43 +00001431 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
Manuel Klimek79e06082015-05-21 12:23:34 +00001432 nextToken();
Manuel Klimek79e06082015-05-21 12:23:34 +00001433 }
1434
Martin Probstaf16c502017-01-04 13:36:43 +00001435 if (FormatTok->is(tok::semi))
1436 return;
1437
Manuel Klimek79e06082015-05-21 12:23:34 +00001438 parseChildBlock();
1439}
1440
Daniel Jasper3c883d12015-05-18 14:49:19 +00001441bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001442 if (FormatTok->BlockKind == BK_Unknown)
Daniel Jasper3c883d12015-05-18 14:49:19 +00001443 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001444 assert(FormatTok->BlockKind != BK_Unknown);
1445 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001446 return false;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001447 nextToken();
Manuel Klimekab419912013-05-23 09:41:43 +00001448 parseBracedList();
1449 return true;
1450}
1451
Krasimir Georgievff747be2017-06-27 13:43:07 +00001452bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1453 tok::TokenKind ClosingBraceKind) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001454 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001455
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001456 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1457 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001458 do {
Manuel Klimek79e06082015-05-21 12:23:34 +00001459 if (Style.Language == FormatStyle::LK_JavaScript) {
Martin Probst409697e2016-05-29 14:41:07 +00001460 if (FormatTok->is(Keywords.kw_function) ||
1461 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001462 tryToParseJSFunction();
1463 continue;
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001464 }
1465 if (FormatTok->is(TT_JsFatArrow)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001466 nextToken();
1467 // Fat arrows can be followed by simple expressions or by child blocks
1468 // in curly braces.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001469 if (FormatTok->is(tok::l_brace)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001470 parseChildBlock();
1471 continue;
1472 }
1473 }
Martin Probst8e3eba02017-02-07 16:33:13 +00001474 if (FormatTok->is(tok::l_brace)) {
1475 // Could be a method inside of a braced list `{a() { return 1; }}`.
1476 if (tryToParseBracedList())
1477 continue;
1478 parseChildBlock();
1479 }
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001480 }
Krasimir Georgievff747be2017-06-27 13:43:07 +00001481 if (FormatTok->Tok.getKind() == ClosingBraceKind) {
1482 nextToken();
1483 return !HasError;
1484 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001485 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001486 case tok::caret:
1487 nextToken();
1488 if (FormatTok->is(tok::l_brace)) {
1489 parseChildBlock();
1490 }
1491 break;
1492 case tok::l_square:
1493 tryToParseLambda();
1494 break;
Daniel Jaspera87af7a2015-06-30 11:32:22 +00001495 case tok::l_paren:
1496 parseParens();
Daniel Jasperf46dec82015-03-31 14:34:15 +00001497 // JavaScript can just have free standing methods and getters/setters in
1498 // object literals. Detect them by a "{" following ")".
1499 if (Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperf46dec82015-03-31 14:34:15 +00001500 if (FormatTok->is(tok::l_brace))
1501 parseChildBlock();
1502 break;
1503 }
Daniel Jasperf46dec82015-03-31 14:34:15 +00001504 break;
Martin Probst8e3eba02017-02-07 16:33:13 +00001505 case tok::l_brace:
1506 // Assume there are no blocks inside a braced init list apart
1507 // from the ones we explicitly parse out (like lambdas).
1508 FormatTok->BlockKind = BK_BracedInit;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001509 nextToken();
Martin Probst8e3eba02017-02-07 16:33:13 +00001510 parseBracedList();
1511 break;
Krasimir Georgievfa4dbb62017-08-03 13:43:45 +00001512 case tok::less:
1513 if (Style.Language == FormatStyle::LK_Proto) {
1514 nextToken();
1515 parseBracedList(/*ContinueOnSemicolons=*/false,
1516 /*ClosingBraceKind=*/tok::greater);
1517 } else {
1518 nextToken();
1519 }
1520 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001521 case tok::semi:
Daniel Jasperb9a49902016-01-09 15:56:28 +00001522 // JavaScript (or more precisely TypeScript) can have semicolons in braced
1523 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1524 // used for error recovery if we have otherwise determined that this is
1525 // a braced list.
1526 if (Style.Language == FormatStyle::LK_JavaScript) {
1527 nextToken();
1528 break;
1529 }
Daniel Jasper015ed022013-09-13 09:20:45 +00001530 HasError = true;
1531 if (!ContinueOnSemicolons)
1532 return !HasError;
1533 nextToken();
1534 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001535 case tok::comma:
1536 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001537 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001538 default:
1539 nextToken();
1540 break;
1541 }
1542 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001543 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001544}
1545
Daniel Jasperf7935112012-12-03 18:12:45 +00001546void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001547 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001548 nextToken();
1549 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001550 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001551 case tok::l_paren:
1552 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001553 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1554 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001555 break;
1556 case tok::r_paren:
1557 nextToken();
1558 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001559 case tok::r_brace:
1560 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1561 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001562 case tok::l_square:
1563 tryToParseLambda();
1564 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001565 case tok::l_brace:
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001566 if (!tryToParseBracedList())
Manuel Klimekf017dc02013-09-04 13:34:14 +00001567 parseChildBlock();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001568 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001569 case tok::at:
1570 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001571 if (FormatTok->Tok.is(tok::l_brace)) {
1572 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001573 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001574 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001575 break;
Martin Probst1027fb82017-02-07 14:05:30 +00001576 case tok::kw_class:
1577 if (Style.Language == FormatStyle::LK_JavaScript)
1578 parseRecord(/*ParseAsExpr=*/true);
1579 else
1580 nextToken();
1581 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001582 case tok::identifier:
1583 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst409697e2016-05-29 14:41:07 +00001584 (FormatTok->is(Keywords.kw_function) ||
1585 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001586 tryToParseJSFunction();
1587 else
1588 nextToken();
1589 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001590 default:
1591 nextToken();
1592 break;
1593 }
1594 } while (!eof());
1595}
1596
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001597void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
1598 if (!LambdaIntroducer) {
1599 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1600 if (tryToParseLambda())
1601 return;
1602 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001603 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001604 switch (FormatTok->Tok.getKind()) {
1605 case tok::l_paren:
1606 parseParens();
1607 break;
1608 case tok::r_square:
1609 nextToken();
1610 return;
1611 case tok::r_brace:
1612 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1613 return;
1614 case tok::l_square:
1615 parseSquare();
1616 break;
1617 case tok::l_brace: {
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001618 if (!tryToParseBracedList())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001619 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001620 break;
1621 }
1622 case tok::at:
1623 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001624 if (FormatTok->Tok.is(tok::l_brace)) {
1625 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001626 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001627 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001628 break;
1629 default:
1630 nextToken();
1631 break;
1632 }
1633 } while (!eof());
1634}
1635
Daniel Jasperf7935112012-12-03 18:12:45 +00001636void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001637 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001638 nextToken();
Daniel Jasper6a7d5a72017-06-19 07:40:49 +00001639 if (FormatTok->Tok.is(tok::kw_constexpr))
1640 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001641 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001642 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001643 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001644 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001645 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001646 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001647 if (Style.BraceWrapping.BeforeElse)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001648 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001649 else
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001650 NeedsUnwrappedLine = true;
Daniel Jasperf7935112012-12-03 18:12:45 +00001651 } else {
1652 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001653 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001654 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001655 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001656 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001657 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001658 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001659 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001660 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001661 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001662 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001663 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001664 parseIfThenElse();
1665 } else {
1666 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001667 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001668 parseStructuralElement();
Daniel Jasper451544a2016-05-19 06:30:48 +00001669 if (FormatTok->is(tok::eof))
1670 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001671 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001672 }
1673 } else if (NeedsUnwrappedLine) {
1674 addUnwrappedLine();
1675 }
1676}
1677
Daniel Jasper04a71a42014-05-08 11:58:24 +00001678void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001679 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001680 nextToken();
1681 bool NeedsUnwrappedLine = false;
1682 if (FormatTok->is(tok::colon)) {
1683 // We are in a function try block, what comes is an initializer list.
1684 nextToken();
1685 while (FormatTok->is(tok::identifier)) {
1686 nextToken();
1687 if (FormatTok->is(tok::l_paren))
1688 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001689 if (FormatTok->is(tok::comma))
1690 nextToken();
1691 }
1692 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001693 // Parse try with resource.
1694 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1695 parseParens();
1696 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001697 if (FormatTok->is(tok::l_brace)) {
1698 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1699 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001700 if (Style.BraceWrapping.BeforeCatch) {
Daniel Jasper04a71a42014-05-08 11:58:24 +00001701 addUnwrappedLine();
1702 } else {
1703 NeedsUnwrappedLine = true;
1704 }
1705 } else if (!FormatTok->is(tok::kw_catch)) {
1706 // The C++ standard requires a compound-statement after a try.
1707 // If there's none, we try to assume there's a structuralElement
1708 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001709 addUnwrappedLine();
1710 ++Line->Level;
1711 parseStructuralElement();
1712 --Line->Level;
1713 }
Nico Weber33381f52015-02-07 01:57:32 +00001714 while (1) {
1715 if (FormatTok->is(tok::at))
1716 nextToken();
1717 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1718 tok::kw___finally) ||
1719 ((Style.Language == FormatStyle::LK_Java ||
1720 Style.Language == FormatStyle::LK_JavaScript) &&
1721 FormatTok->is(Keywords.kw_finally)) ||
1722 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1723 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1724 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001725 nextToken();
1726 while (FormatTok->isNot(tok::l_brace)) {
1727 if (FormatTok->is(tok::l_paren)) {
1728 parseParens();
1729 continue;
1730 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001731 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001732 return;
1733 nextToken();
1734 }
1735 NeedsUnwrappedLine = false;
1736 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1737 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001738 if (Style.BraceWrapping.BeforeCatch)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001739 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001740 else
Daniel Jasper04a71a42014-05-08 11:58:24 +00001741 NeedsUnwrappedLine = true;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001742 }
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001743 if (NeedsUnwrappedLine)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001744 addUnwrappedLine();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001745}
1746
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001747void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001748 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001749
1750 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001751 nextToken();
Saleem Abdulrasool328085f2015-10-30 05:07:56 +00001752 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001753 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001754 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001755 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001756 addUnwrappedLine();
1757
Daniel Jasper65ee3472013-07-31 23:16:02 +00001758 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1759 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1760 DeclarationScopeStack.size() > 1);
1761 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001762 // Munch the semicolon after a namespace. This is more common than one would
1763 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001764 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001765 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001766 addUnwrappedLine();
1767 }
1768 // FIXME: Add error handling.
1769}
1770
Daniel Jasper6acf5132015-03-12 14:44:29 +00001771void UnwrappedLineParser::parseNew() {
1772 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1773 nextToken();
1774 if (Style.Language != FormatStyle::LK_Java)
1775 return;
1776
1777 // In Java, we can parse everything up to the parens, which aren't optional.
1778 do {
1779 // There should not be a ;, { or } before the new's open paren.
1780 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1781 return;
1782
1783 // Consume the parens.
1784 if (FormatTok->is(tok::l_paren)) {
1785 parseParens();
1786
1787 // If there is a class body of an anonymous class, consume that as child.
1788 if (FormatTok->is(tok::l_brace))
1789 parseChildBlock();
1790 return;
1791 }
1792 nextToken();
1793 } while (!eof());
1794}
1795
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001796void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001797 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001798 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001799 nextToken();
Martin Probsta050f412017-05-18 21:19:29 +00001800 // JS' for await ( ...
Martin Probstbd49e322017-05-15 19:33:20 +00001801 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probsta050f412017-05-18 21:19:29 +00001802 FormatTok->is(Keywords.kw_await))
Martin Probstbd49e322017-05-15 19:33:20 +00001803 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001804 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001805 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001806 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001807 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001808 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001809 addUnwrappedLine();
1810 } else {
1811 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001812 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001813 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001814 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001815 }
1816}
1817
Daniel Jasperf7935112012-12-03 18:12:45 +00001818void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001819 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001820 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001821 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001822 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001823 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001824 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001825 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001826 } else {
1827 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001828 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001829 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001830 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001831 }
1832
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001833 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001834 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001835 addUnwrappedLine();
1836 return;
1837 }
1838
Daniel Jasperf7935112012-12-03 18:12:45 +00001839 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001840 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001841}
1842
1843void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001844 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001845 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001846 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001847 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001848 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001849 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001850 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001851 if (FormatTok->Tok.is(tok::kw_break)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001852 if (Style.BraceWrapping.AfterControlStatement)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001853 addUnwrappedLine();
1854 parseStructuralElement();
1855 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001856 addUnwrappedLine();
1857 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001858 if (FormatTok->is(tok::semi))
1859 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001860 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001861 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001862 Line->Level = OldLineLevel;
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001863 if (FormatTok->isNot(tok::l_brace)) {
Daniel Jasper40609472016-04-06 15:02:46 +00001864 parseStructuralElement();
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001865 addUnwrappedLine();
1866 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001867}
1868
1869void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001870 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001871 // FIXME: fix handling of complex expressions here.
1872 do {
1873 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001874 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001875 parseLabel();
1876}
1877
1878void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001879 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001880 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001881 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001882 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001883 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001884 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001885 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001886 addUnwrappedLine();
1887 } else {
1888 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001889 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001890 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001891 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001892 }
1893}
1894
1895void UnwrappedLineParser::parseAccessSpecifier() {
1896 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001897 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001898 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001899 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001900 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001901 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001902 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001903 addUnwrappedLine();
1904}
1905
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001906bool UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001907 // Won't be 'enum' for NS_ENUMs.
1908 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001909 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001910
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001911 // In TypeScript, "enum" can also be used as property name, e.g. in interface
1912 // declarations. An "enum" keyword followed by a colon would be a syntax
1913 // error and thus assume it is just an identifier.
Daniel Jasper87379302016-02-03 05:33:44 +00001914 if (Style.Language == FormatStyle::LK_JavaScript &&
1915 FormatTok->isOneOf(tok::colon, tok::question))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001916 return false;
1917
Daniel Jasper2b41a822013-08-20 12:42:50 +00001918 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001919 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1920 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001921
Daniel Jasper786a5502013-09-06 21:32:35 +00001922 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001923 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1924 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001925 nextToken();
1926 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001927 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001928 parseParens();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001929 if (FormatTok->is(tok::identifier)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001930 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001931 // If there are two identifiers in a row, this is likely an elaborate
1932 // return type. In Java, this can be "implements", etc.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001933 if (Style.isCpp() && FormatTok->is(tok::identifier))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001934 return false;
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001935 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001936 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001937
1938 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001939 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001940 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00001941 FormatTok->BlockKind = BK_Block;
1942
1943 if (Style.Language == FormatStyle::LK_Java) {
1944 // Java enums are different.
1945 parseJavaEnumBody();
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001946 return true;
1947 }
1948 if (Style.Language == FormatStyle::LK_Proto) {
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001949 parseBlock(/*MustBeDeclaration=*/true);
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001950 return true;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001951 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001952
1953 // Parse enum body.
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001954 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001955 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1956 if (HasError) {
1957 if (FormatTok->is(tok::semi))
1958 nextToken();
1959 addUnwrappedLine();
1960 }
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001961 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00001962
Daniel Jasper90cf3802015-06-17 09:44:02 +00001963 // There is no addUnwrappedLine() here so that we fall through to parsing a
1964 // structural element afterwards. Thus, in "enum A {} n, m;",
Manuel Klimek2cec0192013-01-21 19:17:52 +00001965 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001966}
1967
1968void UnwrappedLineParser::parseJavaEnumBody() {
1969 // Determine whether the enum is simple, i.e. does not have a semicolon or
1970 // constants with class bodies. Simple enums can be formatted like braced
1971 // lists, contracted to a single line, etc.
1972 unsigned StoredPosition = Tokens->getPosition();
1973 bool IsSimple = true;
1974 FormatToken *Tok = Tokens->getNextToken();
1975 while (Tok) {
1976 if (Tok->is(tok::r_brace))
1977 break;
1978 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1979 IsSimple = false;
1980 break;
1981 }
1982 // FIXME: This will also mark enums with braces in the arguments to enum
1983 // constants as "not simple". This is probably fine in practice, though.
1984 Tok = Tokens->getNextToken();
1985 }
1986 FormatTok = Tokens->setPosition(StoredPosition);
1987
1988 if (IsSimple) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001989 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001990 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001991 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001992 return;
1993 }
1994
1995 // Parse the body of a more complex enum.
1996 // First add a line for everything up to the "{".
1997 nextToken();
1998 addUnwrappedLine();
1999 ++Line->Level;
2000
2001 // Parse the enum constants.
2002 while (FormatTok) {
2003 if (FormatTok->is(tok::l_brace)) {
2004 // Parse the constant's class body.
2005 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2006 /*MunchSemi=*/false);
2007 } else if (FormatTok->is(tok::l_paren)) {
2008 parseParens();
2009 } else if (FormatTok->is(tok::comma)) {
2010 nextToken();
2011 addUnwrappedLine();
2012 } else if (FormatTok->is(tok::semi)) {
2013 nextToken();
2014 addUnwrappedLine();
2015 break;
2016 } else if (FormatTok->is(tok::r_brace)) {
2017 addUnwrappedLine();
2018 break;
2019 } else {
2020 nextToken();
2021 }
2022 }
2023
2024 // Parse the class body after the enum's ";" if any.
2025 parseLevel(/*HasOpeningBrace=*/true);
2026 nextToken();
2027 --Line->Level;
2028 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00002029}
2030
Martin Probst1027fb82017-02-07 14:05:30 +00002031void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00002032 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00002033 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00002034
Daniel Jasper04785d02015-05-06 14:03:02 +00002035 // The actual identifier can be a nested name specifier, and in macros
2036 // it is often token-pasted.
2037 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2038 tok::kw___attribute, tok::kw___declspec,
2039 tok::kw_alignas) ||
2040 ((Style.Language == FormatStyle::LK_Java ||
2041 Style.Language == FormatStyle::LK_JavaScript) &&
2042 FormatTok->isOneOf(tok::period, tok::comma))) {
Martin Probstcb870c52017-08-01 15:46:10 +00002043 if (Style.Language == FormatStyle::LK_JavaScript &&
2044 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
2045 // JavaScript/TypeScript supports inline object types in
2046 // extends/implements positions:
2047 // class Foo implements {bar: number} { }
2048 nextToken();
2049 if (FormatTok->is(tok::l_brace)) {
2050 tryToParseBracedList();
2051 continue;
2052 }
2053 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002054 bool IsNonMacroIdentifier =
2055 FormatTok->is(tok::identifier) &&
2056 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002057 nextToken();
2058 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00002059 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00002060 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00002061 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00002062
Daniel Jasper04785d02015-05-06 14:03:02 +00002063 // Note that parsing away template declarations here leads to incorrectly
2064 // accepting function declarations as record declarations.
2065 // In general, we cannot solve this problem. Consider:
2066 // class A<int> B() {}
2067 // which can be a function definition or a class definition when B() is a
2068 // macro. If we find enough real-world cases where this is a problem, we
2069 // can parse for the 'template' keyword in the beginning of the statement,
2070 // and thus rule out the record production in case there is no template
2071 // (this would still leave us with an ambiguity between template function
2072 // and class declarations).
Daniel Jasperadba2aa2015-05-18 12:52:00 +00002073 if (FormatTok->isOneOf(tok::colon, tok::less)) {
2074 while (!eof()) {
Daniel Jasper3c883d12015-05-18 14:49:19 +00002075 if (FormatTok->is(tok::l_brace)) {
2076 calculateBraceTypes(/*ExpectClassBody=*/true);
2077 if (!tryToParseBracedList())
2078 break;
2079 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002080 if (FormatTok->Tok.is(tok::semi))
2081 return;
2082 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002083 }
2084 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002085 if (FormatTok->Tok.is(tok::l_brace)) {
Martin Probst1027fb82017-02-07 14:05:30 +00002086 if (ParseAsExpr) {
2087 parseChildBlock();
2088 } else {
2089 if (ShouldBreakBeforeBrace(Style, InitialToken))
2090 addUnwrappedLine();
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002091
Martin Probst1027fb82017-02-07 14:05:30 +00002092 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2093 /*MunchSemi=*/false);
2094 }
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002095 }
Daniel Jasper90cf3802015-06-17 09:44:02 +00002096 // There is no addUnwrappedLine() here so that we fall through to parsing a
2097 // structural element afterwards. Thus, in "class A {} n, m;",
2098 // "} n, m;" will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00002099}
2100
Nico Weber8696a8d2013-01-09 21:15:03 +00002101void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002102 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00002103 do
2104 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002105 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00002106 nextToken(); // Skip '>'.
2107}
2108
2109void UnwrappedLineParser::parseObjCUntilAtEnd() {
2110 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002111 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002112 nextToken();
2113 addUnwrappedLine();
2114 break;
2115 }
Daniel Jaspera15da302013-08-28 08:04:23 +00002116 if (FormatTok->is(tok::l_brace)) {
2117 parseBlock(/*MustBeDeclaration=*/false);
2118 // In ObjC interfaces, nothing should be following the "}".
2119 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00002120 } else if (FormatTok->is(tok::r_brace)) {
2121 // Ignore stray "}". parseStructuralElement doesn't consume them.
2122 nextToken();
2123 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00002124 } else {
2125 parseStructuralElement();
2126 }
Nico Weber8696a8d2013-01-09 21:15:03 +00002127 } while (!eof());
2128}
2129
Nico Weber2ce0ac52013-01-09 23:25:37 +00002130void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00002131 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002132 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00002133
2134 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002135 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00002136 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002137 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002138 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00002139 // Skip category, if present.
2140 parseParens();
2141
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002142 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002143 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00002144
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002145 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00002146 if (Style.BraceWrapping.AfterObjCDeclaration)
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002147 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00002148 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002149 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00002150
2151 // With instance variables, this puts '}' on its own line. Without instance
2152 // variables, this ends the @interface line.
2153 addUnwrappedLine();
2154
Nico Weber8696a8d2013-01-09 21:15:03 +00002155 parseObjCUntilAtEnd();
2156}
Nico Weber7eecf4b2013-01-09 20:25:35 +00002157
Nico Weber8696a8d2013-01-09 21:15:03 +00002158void UnwrappedLineParser::parseObjCProtocol() {
2159 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002160 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00002161
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002162 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002163 parseObjCProtocolList();
2164
2165 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002166 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002167 nextToken();
2168 return addUnwrappedLine();
2169 }
2170
2171 addUnwrappedLine();
2172 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00002173}
2174
Daniel Jasperfca735c2015-02-19 16:14:18 +00002175void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
Martin Probst053f1aa2016-04-19 14:55:37 +00002176 bool IsImport = FormatTok->is(Keywords.kw_import);
2177 assert(IsImport || FormatTok->is(tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00002178 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00002179
Daniel Jasperec05fc72015-05-11 09:14:50 +00002180 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002181 if (FormatTok->is(tok::kw_default))
2182 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00002183
Martin Probst5f8445b2016-04-24 22:05:09 +00002184 // Consume "async function", "function" and "default function", so that these
2185 // get parsed as free-standing JS functions, i.e. do not require a trailing
2186 // semicolon.
2187 if (FormatTok->is(Keywords.kw_async))
2188 nextToken();
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002189 if (FormatTok->is(Keywords.kw_function)) {
2190 nextToken();
2191 return;
2192 }
2193
Martin Probst053f1aa2016-04-19 14:55:37 +00002194 // For imports, `export *`, `export {...}`, consume the rest of the line up
2195 // to the terminating `;`. For everything else, just return and continue
2196 // parsing the structural element, i.e. the declaration or expression for
2197 // `export default`.
2198 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
2199 !FormatTok->isStringLiteral())
2200 return;
Daniel Jasperfca735c2015-02-19 16:14:18 +00002201
Martin Probstd40bca42017-01-09 08:56:36 +00002202 while (!eof()) {
2203 if (FormatTok->is(tok::semi))
2204 return;
Krasimir Georgiev112c2e92017-11-09 13:22:03 +00002205 if (Line->Tokens.empty()) {
Martin Probstd40bca42017-01-09 08:56:36 +00002206 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
2207 // import statement should terminate.
2208 return;
2209 }
Daniel Jasperefc1a832016-01-07 08:53:35 +00002210 if (FormatTok->is(tok::l_brace)) {
2211 FormatTok->BlockKind = BK_Block;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002212 nextToken();
Daniel Jasperefc1a832016-01-07 08:53:35 +00002213 parseBracedList();
2214 } else {
2215 nextToken();
2216 }
Daniel Jasper354aa512015-02-19 16:07:32 +00002217 }
2218}
2219
Daniel Jasper3b203a62013-09-05 16:05:56 +00002220LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
2221 StringRef Prefix = "") {
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002222 llvm::dbgs() << Prefix << "Line(" << Line.Level
2223 << ", FSC=" << Line.FirstStartColumn << ")"
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002224 << (Line.InPPDirective ? " MACRO" : "") << ": ";
2225 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2226 E = Line.Tokens.end();
2227 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002228 llvm::dbgs() << I->Tok->Tok.getName() << "["
Manuel Klimek89628f62017-09-20 09:51:03 +00002229 << "T=" << I->Tok->Type << ", OC=" << I->Tok->OriginalColumn
2230 << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002231 }
2232 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2233 E = Line.Tokens.end();
2234 I != E; ++I) {
2235 const UnwrappedLineNode &Node = *I;
2236 for (SmallVectorImpl<UnwrappedLine>::const_iterator
2237 I = Node.Children.begin(),
2238 E = Node.Children.end();
2239 I != E; ++I) {
2240 printDebugInfo(*I, "\nChild: ");
2241 }
2242 }
2243 llvm::dbgs() << "\n";
2244}
2245
Daniel Jasperf7935112012-12-03 18:12:45 +00002246void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002247 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00002248 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00002249 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002250 if (CurrentLines == &Lines)
2251 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00002252 });
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002253 CurrentLines->push_back(std::move(*Line));
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002254 Line->Tokens.clear();
Krasimir Georgiev85c37042017-03-01 16:38:08 +00002255 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002256 Line->FirstStartColumn = 0;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002257 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002258 CurrentLines->append(
2259 std::make_move_iterator(PreprocessorDirectives.begin()),
2260 std::make_move_iterator(PreprocessorDirectives.end()));
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002261 PreprocessorDirectives.clear();
2262 }
Manuel Klimeke411aa82017-09-20 09:29:37 +00002263 // Disconnect the current token from the last token on the previous line.
2264 FormatTok->Previous = nullptr;
Daniel Jasperf7935112012-12-03 18:12:45 +00002265}
2266
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002267bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00002268
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002269bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002270 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
2271 FormatTok.NewlinesBefore > 0;
2272}
2273
Krasimir Georgiev91834222017-01-25 13:58:58 +00002274// Checks if \p FormatTok is a line comment that continues the line comment
2275// section on \p Line.
Krasimir Georgievea222a72017-05-22 10:07:56 +00002276static bool continuesLineCommentSection(const FormatToken &FormatTok,
2277 const UnwrappedLine &Line,
2278 llvm::Regex &CommentPragmasRegex) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002279 if (Line.Tokens.empty())
2280 return false;
Krasimir Georgiev84321612017-01-30 19:18:55 +00002281
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002282 StringRef IndentContent = FormatTok.TokenText;
2283 if (FormatTok.TokenText.startswith("//") ||
2284 FormatTok.TokenText.startswith("/*"))
2285 IndentContent = FormatTok.TokenText.substr(2);
2286 if (CommentPragmasRegex.match(IndentContent))
2287 return false;
2288
Krasimir Georgiev91834222017-01-25 13:58:58 +00002289 // If Line starts with a line comment, then FormatTok continues the comment
Krasimir Georgiev84321612017-01-30 19:18:55 +00002290 // section if its original column is greater or equal to the original start
Krasimir Georgiev91834222017-01-25 13:58:58 +00002291 // column of the line.
2292 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002293 // Define the min column token of a line as follows: if a line ends in '{' or
2294 // contains a '{' followed by a line comment, then the min column token is
2295 // that '{'. Otherwise, the min column token of the line is the first token of
2296 // the line.
2297 //
2298 // If Line starts with a token other than a line comment, then FormatTok
2299 // continues the comment section if its original column is greater than the
2300 // original start column of the min column token of the line.
Krasimir Georgiev91834222017-01-25 13:58:58 +00002301 //
2302 // For example, the second line comment continues the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002303 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002304 // // first line
2305 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002306 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002307 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002308 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002309 // // first line
2310 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002311 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002312 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002313 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002314 // int i; // first line
2315 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002316 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002317 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002318 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002319 // do { // first line
2320 // // second line
2321 // int i;
2322 // } while (true);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002323 //
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002324 // and:
2325 //
2326 // enum {
2327 // a, // first line
2328 // // second line
2329 // b
2330 // };
2331 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002332 // The second line comment doesn't continue the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002333 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002334 // // first line
2335 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002336 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002337 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002338 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002339 // int i; // first line
2340 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002341 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002342 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002343 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002344 // do { // first line
2345 // // second line
2346 // int i;
2347 // } while (true);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002348 //
2349 // and:
2350 //
2351 // enum {
2352 // a, // first line
2353 // // second line
2354 // };
Krasimir Georgiev84321612017-01-30 19:18:55 +00002355 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
2356
2357 // Scan for '{//'. If found, use the column of '{' as a min column for line
2358 // comment section continuation.
2359 const FormatToken *PreviousToken = nullptr;
Krasimir Georgievd86c25d2017-03-10 13:09:29 +00002360 for (const UnwrappedLineNode &Node : Line.Tokens) {
Krasimir Georgiev84321612017-01-30 19:18:55 +00002361 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
2362 isLineComment(*Node.Tok)) {
2363 MinColumnToken = PreviousToken;
2364 break;
2365 }
2366 PreviousToken = Node.Tok;
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002367
2368 // Grab the last newline preceding a token in this unwrapped line.
2369 if (Node.Tok->NewlinesBefore > 0) {
2370 MinColumnToken = Node.Tok;
2371 }
Krasimir Georgiev84321612017-01-30 19:18:55 +00002372 }
2373 if (PreviousToken && PreviousToken->is(tok::l_brace)) {
2374 MinColumnToken = PreviousToken;
2375 }
2376
Krasimir Georgievea222a72017-05-22 10:07:56 +00002377 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
2378 MinColumnToken);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002379}
2380
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002381void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
2382 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002383 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002384 I = CommentsBeforeNextToken.begin(),
2385 E = CommentsBeforeNextToken.end();
2386 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002387 // Line comments that belong to the same line comment section are put on the
2388 // same line since later we might want to reflow content between them.
Krasimir Georgiev753625b2017-01-31 13:32:38 +00002389 // Additional fine-grained breaking of line comment sections is controlled
2390 // by the class BreakableLineCommentSection in case it is desirable to keep
2391 // several line comment sections in the same unwrapped line.
2392 //
2393 // FIXME: Consider putting separate line comment sections as children to the
2394 // unwrapped line instead.
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002395 (*I)->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002396 continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002397 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002398 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002399 pushToken(*I);
2400 }
Daniel Jaspere60cba12015-05-13 11:35:53 +00002401 if (NewlineBeforeNext && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002402 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002403 CommentsBeforeNextToken.clear();
2404}
2405
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002406void UnwrappedLineParser::nextToken(int LevelDifference) {
Daniel Jasperf7935112012-12-03 18:12:45 +00002407 if (eof())
2408 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002409 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002410 pushToken(FormatTok);
Manuel Klimek89628f62017-09-20 09:51:03 +00002411 FormatToken *Previous = FormatTok;
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002412 if (Style.Language != FormatStyle::LK_JavaScript)
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002413 readToken(LevelDifference);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002414 else
2415 readTokenWithJavaScriptASI();
Manuel Klimeke411aa82017-09-20 09:29:37 +00002416 FormatTok->Previous = Previous;
Daniel Jasperb9a49902016-01-09 15:56:28 +00002417}
2418
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002419void UnwrappedLineParser::distributeComments(
2420 const SmallVectorImpl<FormatToken *> &Comments,
2421 const FormatToken *NextTok) {
2422 // Whether or not a line comment token continues a line is controlled by
Krasimir Georgievea222a72017-05-22 10:07:56 +00002423 // the method continuesLineCommentSection, with the following caveat:
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002424 //
2425 // Define a trail of Comments to be a nonempty proper postfix of Comments such
2426 // that each comment line from the trail is aligned with the next token, if
2427 // the next token exists. If a trail exists, the beginning of the maximal
2428 // trail is marked as a start of a new comment section.
2429 //
2430 // For example in this code:
2431 //
2432 // int a; // line about a
2433 // // line 1 about b
2434 // // line 2 about b
2435 // int b;
2436 //
2437 // the two lines about b form a maximal trail, so there are two sections, the
2438 // first one consisting of the single comment "// line about a" and the
2439 // second one consisting of the next two comments.
2440 if (Comments.empty())
2441 return;
2442 bool ShouldPushCommentsInCurrentLine = true;
2443 bool HasTrailAlignedWithNextToken = false;
2444 unsigned StartOfTrailAlignedWithNextToken = 0;
2445 if (NextTok) {
2446 // We are skipping the first element intentionally.
2447 for (unsigned i = Comments.size() - 1; i > 0; --i) {
2448 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
2449 HasTrailAlignedWithNextToken = true;
2450 StartOfTrailAlignedWithNextToken = i;
2451 }
2452 }
2453 }
2454 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
2455 FormatToken *FormatTok = Comments[i];
Manuel Klimek89628f62017-09-20 09:51:03 +00002456 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002457 FormatTok->ContinuesLineCommentSection = false;
2458 } else {
2459 FormatTok->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002460 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002461 }
2462 if (!FormatTok->ContinuesLineCommentSection &&
2463 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
2464 ShouldPushCommentsInCurrentLine = false;
2465 }
2466 if (ShouldPushCommentsInCurrentLine) {
2467 pushToken(FormatTok);
2468 } else {
2469 CommentsBeforeNextToken.push_back(FormatTok);
2470 }
2471 }
2472}
2473
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002474void UnwrappedLineParser::readToken(int LevelDifference) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002475 SmallVector<FormatToken *, 1> Comments;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002476 do {
2477 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00002478 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002479 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
2480 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002481 distributeComments(Comments, FormatTok);
2482 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002483 // If there is an unfinished unwrapped line, we flush the preprocessor
2484 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00002485 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002486 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002487 assert((LevelDifference >= 0 ||
2488 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2489 "LevelDifference makes Line->Level negative");
2490 Line->Level += LevelDifference;
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00002491 // Comments stored before the preprocessor directive need to be output
2492 // before the preprocessor directive, at the same level as the
2493 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002494 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002495 parsePPDirective();
2496 }
Manuel Klimek68b03042014-04-14 09:14:11 +00002497 while (FormatTok->Type == TT_ConflictStart ||
2498 FormatTok->Type == TT_ConflictEnd ||
2499 FormatTok->Type == TT_ConflictAlternative) {
2500 if (FormatTok->Type == TT_ConflictStart) {
2501 conditionalCompilationStart(/*Unreachable=*/false);
2502 } else if (FormatTok->Type == TT_ConflictAlternative) {
2503 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002504 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00002505 conditionalCompilationEnd();
2506 }
2507 FormatTok = Tokens->getNextToken();
2508 FormatTok->MustBreakBefore = true;
2509 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002510
Francois Ferranda98a95c2017-07-28 07:56:14 +00002511 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002512 !Line->InPPDirective) {
2513 continue;
2514 }
2515
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002516 if (!FormatTok->Tok.is(tok::comment)) {
2517 distributeComments(Comments, FormatTok);
2518 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002519 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002520 }
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002521
2522 Comments.push_back(FormatTok);
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002523 } while (!eof());
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002524
2525 distributeComments(Comments, nullptr);
2526 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002527}
2528
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002529void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002530 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002531 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002532 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002533 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00002534 }
Daniel Jasperf7935112012-12-03 18:12:45 +00002535}
2536
Daniel Jasper8d1832e2013-01-07 13:26:07 +00002537} // end namespace format
2538} // end namespace clang