blob: 8fd0e9433b63f8f978f11b59f25091255d6b0565 [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Jasperf7935112012-12-03 18:12:45 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file contains the implementation of the UnwrappedLineParser,
Daniel Jasperf7935112012-12-03 18:12:45 +000011/// which turns a stream of tokens into UnwrappedLines.
12///
Daniel Jasperf7935112012-12-03 18:12:45 +000013//===----------------------------------------------------------------------===//
14
Chandler Carruth4b417452013-01-19 08:09:44 +000015#include "UnwrappedLineParser.h"
Benjamin Kramer33335df2015-03-01 21:36:40 +000016#include "llvm/ADT/STLExtras.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000017#include "llvm/Support/Debug.h"
Benjamin Kramer53f5e892015-03-23 18:05:43 +000018#include "llvm/Support/raw_ostream.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000019
Martin Probst7e0f25b2017-11-25 09:19:42 +000020#include <algorithm>
21
Chandler Carruth10346662014-04-22 03:17:02 +000022#define DEBUG_TYPE "format-parser"
23
Daniel Jasperf7935112012-12-03 18:12:45 +000024namespace clang {
25namespace format {
26
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000027class FormatTokenSource {
28public:
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000029 virtual ~FormatTokenSource() {}
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000030 virtual FormatToken *getNextToken() = 0;
31
32 virtual unsigned getPosition() = 0;
33 virtual FormatToken *setPosition(unsigned Position) = 0;
34};
35
Craig Topper69665e12013-07-01 04:21:54 +000036namespace {
37
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000038class ScopedDeclarationState {
39public:
40 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
41 bool MustBeDeclaration)
42 : Line(Line), Stack(Stack) {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000043 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek39080572013-01-23 11:03:04 +000044 Stack.push_back(MustBeDeclaration);
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000045 }
46 ~ScopedDeclarationState() {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000047 Stack.pop_back();
Manuel Klimekc1237a82013-01-23 14:08:21 +000048 if (!Stack.empty())
49 Line.MustBeDeclaration = Stack.back();
50 else
51 Line.MustBeDeclaration = true;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000052 }
Daniel Jasper393564f2013-05-31 14:56:29 +000053
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000054private:
55 UnwrappedLine &Line;
56 std::vector<bool> &Stack;
57};
58
Krasimir Georgieva1c30932017-05-19 10:34:57 +000059static bool isLineComment(const FormatToken &FormatTok) {
Krasimir Georgiev410ed242017-11-10 12:50:09 +000060 return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith("/*");
Krasimir Georgieva1c30932017-05-19 10:34:57 +000061}
62
Krasimir Georgievea222a72017-05-22 10:07:56 +000063// Checks if \p FormatTok is a line comment that continues the line comment
64// \p Previous. The original column of \p MinColumnToken is used to determine
65// whether \p FormatTok is indented enough to the right to continue \p Previous.
66static bool continuesLineComment(const FormatToken &FormatTok,
67 const FormatToken *Previous,
68 const FormatToken *MinColumnToken) {
69 if (!Previous || !MinColumnToken)
70 return false;
71 unsigned MinContinueColumn =
72 MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
73 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
74 isLineComment(*Previous) &&
75 FormatTok.OriginalColumn >= MinContinueColumn;
76}
77
Manuel Klimek1abf7892013-01-04 23:34:14 +000078class ScopedMacroState : public FormatTokenSource {
79public:
80 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek20e0af62015-05-06 11:56:29 +000081 FormatToken *&ResetToken)
Manuel Klimek1abf7892013-01-04 23:34:14 +000082 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000083 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
Krasimir Georgieva1c30932017-05-19 10:34:57 +000084 Token(nullptr), PreviousToken(nullptr) {
David L. Jones5de22722018-06-15 06:08:54 +000085 FakeEOF.Tok.startToken();
86 FakeEOF.Tok.setKind(tok::eof);
Manuel Klimek1abf7892013-01-04 23:34:14 +000087 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000088 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000089 Line.InPPDirective = true;
90 }
91
Alexander Kornienko34eb2072015-04-11 02:00:23 +000092 ~ScopedMacroState() override {
Manuel Klimek1abf7892013-01-04 23:34:14 +000093 TokenSource = PreviousTokenSource;
94 ResetToken = Token;
95 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000096 Line.Level = PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +000097 }
98
Craig Topperfb6b25b2014-03-15 04:29:04 +000099 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +0000100 // The \c UnwrappedLineParser guards against this by never calling
101 // \c getNextToken() after it has encountered the first eof token.
102 assert(!eof());
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000103 PreviousToken = Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000104 Token = PreviousTokenSource->getNextToken();
105 if (eof())
David L. Jones5de22722018-06-15 06:08:54 +0000106 return &FakeEOF;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000107 return Token;
108 }
109
Craig Topperfb6b25b2014-03-15 04:29:04 +0000110 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +0000111
Craig Topperfb6b25b2014-03-15 04:29:04 +0000112 FormatToken *setPosition(unsigned Position) override {
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000113 PreviousToken = nullptr;
Manuel Klimekab419912013-05-23 09:41:43 +0000114 Token = PreviousTokenSource->setPosition(Position);
115 return Token;
116 }
117
Manuel Klimek1abf7892013-01-04 23:34:14 +0000118private:
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000119 bool eof() {
120 return Token && Token->HasUnescapedNewline &&
Krasimir Georgievea222a72017-05-22 10:07:56 +0000121 !continuesLineComment(*Token, PreviousToken,
122 /*MinColumnToken=*/PreviousToken);
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000123 }
Manuel Klimek1abf7892013-01-04 23:34:14 +0000124
David L. Jones5de22722018-06-15 06:08:54 +0000125 FormatToken FakeEOF;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000126 UnwrappedLine &Line;
127 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000128 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000129 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000130 FormatTokenSource *PreviousTokenSource;
131
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000132 FormatToken *Token;
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000133 FormatToken *PreviousToken;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000134};
135
Craig Topper69665e12013-07-01 04:21:54 +0000136} // end anonymous namespace
137
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000138class ScopedLineState {
139public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000140 ScopedLineState(UnwrappedLineParser &Parser,
141 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000142 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000143 if (SwitchToPreprocessorLines)
144 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000145 else if (!Parser.Line->Tokens.empty())
146 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000147 PreBlockLine = std::move(Parser.Line);
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +0000148 Parser.Line = std::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000149 Parser.Line->Level = PreBlockLine->Level;
150 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000151 }
152
153 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000154 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000155 Parser.addUnwrappedLine();
156 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000157 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000158 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000159 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
160 Parser.MustBreakBeforeNextToken = true;
161 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000162 }
163
164private:
165 UnwrappedLineParser &Parser;
166
David Blaikieefb6eb22014-08-09 20:02:07 +0000167 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000168 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000169};
170
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000171class CompoundStatementIndenter {
172public:
173 CompoundStatementIndenter(UnwrappedLineParser *Parser,
174 const FormatStyle &Style, unsigned &LineLevel)
Owen Pan806d5742019-04-08 23:36:25 +0000175 : CompoundStatementIndenter(Parser, LineLevel,
176 Style.BraceWrapping.AfterControlStatement,
Nico Weberff9f4b52019-07-29 13:26:48 +0000177 Style.BraceWrapping.IndentBraces) {}
Owen Pan806d5742019-04-08 23:36:25 +0000178 CompoundStatementIndenter(UnwrappedLineParser *Parser, unsigned &LineLevel,
179 bool WrapBrace, bool IndentBrace)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000180 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
Owen Pan806d5742019-04-08 23:36:25 +0000181 if (WrapBrace)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000182 Parser->addUnwrappedLine();
Owen Pan806d5742019-04-08 23:36:25 +0000183 if (IndentBrace)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000184 ++LineLevel;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000185 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000186 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000187
188private:
189 unsigned &LineLevel;
190 unsigned OldLineLevel;
191};
192
Craig Topper69665e12013-07-01 04:21:54 +0000193namespace {
194
Manuel Klimekab419912013-05-23 09:41:43 +0000195class IndexedTokenSource : public FormatTokenSource {
196public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000197 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000198 : Tokens(Tokens), Position(-1) {}
199
Craig Topperfb6b25b2014-03-15 04:29:04 +0000200 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000201 ++Position;
202 return Tokens[Position];
203 }
204
Craig Topperfb6b25b2014-03-15 04:29:04 +0000205 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000206 assert(Position >= 0);
207 return Position;
208 }
209
Craig Topperfb6b25b2014-03-15 04:29:04 +0000210 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000211 Position = P;
212 return Tokens[Position];
213 }
214
Manuel Klimek71814b42013-10-11 21:25:45 +0000215 void reset() { Position = -1; }
216
Manuel Klimekab419912013-05-23 09:41:43 +0000217private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000218 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000219 int Position;
220};
221
Craig Topper69665e12013-07-01 04:21:54 +0000222} // end anonymous namespace
223
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000224UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000225 const AdditionalKeywords &Keywords,
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000226 unsigned FirstStartColumn,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000227 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000228 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000229 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000230 CurrentLines(&Lines), Style(Style), Keywords(Keywords),
231 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
Krasimir Georgievad47c902017-08-30 14:34:57 +0000232 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000233 IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
234 ? IG_Rejected
235 : IG_Inited),
236 IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000237
238void UnwrappedLineParser::reset() {
239 PPBranchLevel = -1;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000240 IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None
241 ? IG_Rejected
242 : IG_Inited;
243 IncludeGuardToken = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000244 Line.reset(new UnwrappedLine);
245 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000246 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000247 MustBreakBeforeNextToken = false;
248 PreprocessorDirectives.clear();
249 CurrentLines = &Lines;
250 DeclarationScopeStack.clear();
Manuel Klimek71814b42013-10-11 21:25:45 +0000251 PPStack.clear();
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000252 Line->FirstStartColumn = FirstStartColumn;
Manuel Klimek71814b42013-10-11 21:25:45 +0000253}
Daniel Jasperf7935112012-12-03 18:12:45 +0000254
Manuel Klimek20e0af62015-05-06 11:56:29 +0000255void UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000256 IndexedTokenSource TokenSource(AllTokens);
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000257 Line->FirstStartColumn = FirstStartColumn;
Manuel Klimek71814b42013-10-11 21:25:45 +0000258 do {
Nicola Zaghen3538b392018-05-15 13:30:56 +0000259 LLVM_DEBUG(llvm::dbgs() << "----\n");
Manuel Klimek71814b42013-10-11 21:25:45 +0000260 reset();
261 Tokens = &TokenSource;
262 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000263
Manuel Klimek71814b42013-10-11 21:25:45 +0000264 readToken();
265 parseFile();
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000266
267 // If we found an include guard then all preprocessor directives (other than
268 // the guard) are over-indented by one.
269 if (IncludeGuard == IG_Found)
270 for (auto &Line : Lines)
271 if (Line.InPPDirective && Line.Level > 0)
272 --Line.Level;
273
Manuel Klimek71814b42013-10-11 21:25:45 +0000274 // Create line with eof token.
275 pushToken(FormatTok);
276 addUnwrappedLine();
277
278 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
279 E = Lines.end();
280 I != E; ++I) {
281 Callback.consumeUnwrappedLine(*I);
282 }
283 Callback.finishRun();
284 Lines.clear();
285 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000286 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000287 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
288 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
289 }
290 if (!PPLevelBranchIndex.empty()) {
291 ++PPLevelBranchIndex.back();
292 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
293 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
294 }
295 } while (!PPLevelBranchIndex.empty());
Manuel Klimek1abf7892013-01-04 23:34:14 +0000296}
297
Manuel Klimek1a18c402013-04-12 14:13:36 +0000298void UnwrappedLineParser::parseFile() {
Daniel Jasper9326f912015-05-05 08:40:32 +0000299 // The top-level context in a file always has declarations, except for pre-
300 // processor directives and JavaScript files.
301 bool MustBeDeclaration =
302 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
303 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
304 MustBeDeclaration);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000305 if (Style.Language == FormatStyle::LK_TextProto)
306 parseBracedList();
307 else
308 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000309 // Make sure to format the remaining tokens.
Krasimir Georgiev0895f5e2018-06-25 11:08:24 +0000310 //
311 // LK_TextProto is special since its top-level is parsed as the body of a
312 // braced list, which does not necessarily have natural line separators such
313 // as a semicolon. Comments after the last entry that have been determined to
314 // not belong to that line, as in:
315 // key: value
316 // // endfile comment
317 // do not have a chance to be put on a line of their own until this point.
318 // Here we add this newline before end-of-file comments.
319 if (Style.Language == FormatStyle::LK_TextProto &&
320 !CommentsBeforeNextToken.empty())
321 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000322 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000323 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000324}
325
Manuel Klimek1a18c402013-04-12 14:13:36 +0000326void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000327 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000328 do {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000329 tok::TokenKind kind = FormatTok->Tok.getKind();
330 if (FormatTok->Type == TT_MacroBlockBegin) {
331 kind = tok::l_brace;
332 } else if (FormatTok->Type == TT_MacroBlockEnd) {
333 kind = tok::r_brace;
334 }
335
336 switch (kind) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000337 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000338 nextToken();
339 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000340 break;
341 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000342 // FIXME: Add parameter whether this can happen - if this happens, we must
343 // be in a non-declaration context.
Daniel Jasperb86e2722015-08-24 13:23:37 +0000344 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
345 continue;
Nico Weber9096fc02013-06-26 00:30:14 +0000346 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000347 addUnwrappedLine();
348 break;
349 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000350 if (HasOpeningBrace)
351 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000352 nextToken();
353 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000354 break;
Nico Weberc29f83b2018-01-23 16:30:56 +0000355 case tok::kw_default: {
356 unsigned StoredPosition = Tokens->getPosition();
Jonas Toth90d2aa22018-08-24 17:25:06 +0000357 FormatToken *Next;
358 do {
359 Next = Tokens->getNextToken();
360 } while (Next && Next->is(tok::comment));
Nico Weberc29f83b2018-01-23 16:30:56 +0000361 FormatTok = Tokens->setPosition(StoredPosition);
362 if (Next && Next->isNot(tok::colon)) {
363 // default not followed by ':' is not a case label; treat it like
364 // an identifier.
365 parseStructuralElement();
366 break;
367 }
368 // Else, if it is 'default:', fall through to the case handling.
Nico Weberf1add5e2018-01-24 01:47:22 +0000369 LLVM_FALLTHROUGH;
Nico Weberc29f83b2018-01-23 16:30:56 +0000370 }
Daniel Jasper516d7972013-07-25 11:31:57 +0000371 case tok::kw_case:
Manuel Klimek89628f62017-09-20 09:51:03 +0000372 if (Style.Language == FormatStyle::LK_JavaScript &&
373 Line->MustBeDeclaration) {
Martin Probstf785fd92017-08-04 17:07:15 +0000374 // A 'case: string' style field declaration.
375 parseStructuralElement();
376 break;
377 }
Daniel Jasper72407622013-09-02 08:26:29 +0000378 if (!SwitchLabelEncountered &&
379 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
380 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000381 SwitchLabelEncountered = true;
382 parseStructuralElement();
383 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000384 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000385 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000386 break;
387 }
388 } while (!eof());
389}
390
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000391void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
Manuel Klimekab419912013-05-23 09:41:43 +0000392 // We'll parse forward through the tokens until we hit
393 // a closing brace or eof - note that getNextToken() will
394 // parse macros, so this will magically work inside macro
395 // definitions, too.
396 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000397 FormatToken *Tok = FormatTok;
Manuel Klimek89628f62017-09-20 09:51:03 +0000398 const FormatToken *PrevTok = Tok->Previous;
Manuel Klimekab419912013-05-23 09:41:43 +0000399 // Keep a stack of positions of lbrace tokens. We will
400 // update information about whether an lbrace starts a
401 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000402 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000403 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000404 do {
Daniel Jaspereb65e912015-12-21 18:31:15 +0000405 // Get next non-comment token.
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000406 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000407 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000408 do {
409 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000410 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000411 } while (NextTok->is(tok::comment));
412
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000413 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000414 case tok::l_brace:
Martin Probst95ed8e72017-05-31 09:29:40 +0000415 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) {
Martin Probste8e27ca2017-11-25 09:33:47 +0000416 if (PrevTok->isOneOf(tok::colon, tok::less))
417 // A ':' indicates this code is in a type, or a braced list
418 // following a label in an object literal ({a: {b: 1}}).
419 // A '<' could be an object used in a comparison, but that is nonsense
420 // code (can never return true), so more likely it is a generic type
421 // argument (`X<{a: string; b: number}>`).
422 // The code below could be confused by semicolons between the
423 // individual members in a type member list, which would normally
424 // trigger BK_Block. In both cases, this must be parsed as an inline
425 // braced init.
Martin Probst95ed8e72017-05-31 09:29:40 +0000426 Tok->BlockKind = BK_BracedInit;
427 else if (PrevTok->is(tok::r_paren))
428 // `) { }` can only occur in function or method declarations in JS.
429 Tok->BlockKind = BK_Block;
430 } else {
Daniel Jasperb9a49902016-01-09 15:56:28 +0000431 Tok->BlockKind = BK_Unknown;
Martin Probst95ed8e72017-05-31 09:29:40 +0000432 }
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000433 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000434 break;
435 case tok::r_brace:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000436 if (LBraceStack.empty())
437 break;
438 if (LBraceStack.back()->BlockKind == BK_Unknown) {
439 bool ProbablyBracedList = false;
440 if (Style.Language == FormatStyle::LK_Proto) {
441 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
442 } else {
443 // Using OriginalColumn to distinguish between ObjC methods and
444 // binary operators is a bit hacky.
445 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
446 NextTok->OriginalColumn == 0;
Daniel Jasper91b032a2014-05-22 12:46:38 +0000447
Daniel Jasperb9a49902016-01-09 15:56:28 +0000448 // If there is a comma, semicolon or right paren after the closing
449 // brace, we assume this is a braced initializer list. Note that
450 // regardless how we mark inner braces here, we will overwrite the
451 // BlockKind later if we parse a braced list (where all blocks
452 // inside are by default braced lists), or when we explicitly detect
453 // blocks (for example while parsing lambdas).
Martin Probst95ed8e72017-05-31 09:29:40 +0000454 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
455 // braced list in JS.
Daniel Jasperb9a49902016-01-09 15:56:28 +0000456 ProbablyBracedList =
Daniel Jasperacffeb82016-03-05 18:34:26 +0000457 (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probste1e12a72016-08-19 14:35:01 +0000458 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
459 Keywords.kw_as)) ||
Martin Probstb7fb2672017-05-10 13:53:29 +0000460 (Style.isCpp() && NextTok->is(tok::l_paren)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000461 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
462 tok::r_paren, tok::r_square, tok::l_brace,
Manuel Klimekd0f3fe52018-04-11 14:51:54 +0000463 tok::ellipsis) ||
Daniel Jaspere4ada022016-12-13 10:05:03 +0000464 (NextTok->is(tok::identifier) &&
465 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000466 (NextTok->is(tok::semi) &&
467 (!ExpectClassBody || LBraceStack.size() != 1)) ||
468 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Manuel Klimekd0f3fe52018-04-11 14:51:54 +0000469 if (NextTok->is(tok::l_square)) {
470 // We can have an array subscript after a braced init
471 // list, but C++11 attributes are expected after blocks.
472 NextTok = Tokens->getNextToken();
473 ++ReadTokens;
474 ProbablyBracedList = NextTok->isNot(tok::l_square);
475 }
Manuel Klimekab419912013-05-23 09:41:43 +0000476 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000477 if (ProbablyBracedList) {
478 Tok->BlockKind = BK_BracedInit;
479 LBraceStack.back()->BlockKind = BK_BracedInit;
480 } else {
481 Tok->BlockKind = BK_Block;
482 LBraceStack.back()->BlockKind = BK_Block;
483 }
Manuel Klimekab419912013-05-23 09:41:43 +0000484 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000485 LBraceStack.pop_back();
Manuel Klimekab419912013-05-23 09:41:43 +0000486 break;
Francois Ferrand6f40e212018-10-02 16:37:51 +0000487 case tok::identifier:
488 if (!Tok->is(TT_StatementMacro))
Paul Hoad5bcf99b2019-03-01 09:09:54 +0000489 break;
Francois Ferrand6f40e212018-10-02 16:37:51 +0000490 LLVM_FALLTHROUGH;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000491 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000492 case tok::semi:
493 case tok::kw_if:
494 case tok::kw_while:
495 case tok::kw_for:
496 case tok::kw_switch:
497 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000498 case tok::kw___try:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000499 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000500 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000501 break;
502 default:
503 break;
504 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000505 PrevTok = Tok;
Manuel Klimekab419912013-05-23 09:41:43 +0000506 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000507 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Daniel Jasperb9a49902016-01-09 15:56:28 +0000508
Manuel Klimekab419912013-05-23 09:41:43 +0000509 // Assume other blocks for all unclosed opening braces.
510 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000511 if (LBraceStack[i]->BlockKind == BK_Unknown)
512 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000513 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000514
Manuel Klimekab419912013-05-23 09:41:43 +0000515 FormatTok = Tokens->setPosition(StoredPosition);
516}
517
Francois Ferranda98a95c2017-07-28 07:56:14 +0000518template <class T>
519static inline void hash_combine(std::size_t &seed, const T &v) {
520 std::hash<T> hasher;
521 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
522}
523
524size_t UnwrappedLineParser::computePPHash() const {
525 size_t h = 0;
526 for (const auto &i : PPStack) {
527 hash_combine(h, size_t(i.Kind));
528 hash_combine(h, i.Line);
529 }
530 return h;
531}
532
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000533void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
534 bool MunchSemi) {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000535 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
536 "'{' or macro block token expected");
537 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
Daniel Jaspereb65e912015-12-21 18:31:15 +0000538 FormatTok->BlockKind = BK_Block;
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000539
Francois Ferranda98a95c2017-07-28 07:56:14 +0000540 size_t PPStartHash = computePPHash();
541
Daniel Jasper516d7972013-07-25 11:31:57 +0000542 unsigned InitialLevel = Line->Level;
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000543 nextToken(/*LevelDifference=*/AddLevel ? 1 : 0);
Daniel Jasperf7935112012-12-03 18:12:45 +0000544
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000545 if (MacroBlock && FormatTok->is(tok::l_paren))
546 parseParens();
547
Francois Ferranda98a95c2017-07-28 07:56:14 +0000548 size_t NbPreprocessorDirectives =
549 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000550 addUnwrappedLine();
Francois Ferranda98a95c2017-07-28 07:56:14 +0000551 size_t OpeningLineIndex =
552 CurrentLines->empty()
553 ? (UnwrappedLine::kInvalidIndex)
554 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
Daniel Jasperf7935112012-12-03 18:12:45 +0000555
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000556 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
557 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000558 if (AddLevel)
559 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000560 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000561
Marianne Mailhot-Sarrasin03137c62016-04-14 14:56:49 +0000562 if (eof())
563 return;
564
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000565 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
566 : !FormatTok->is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000567 Line->Level = InitialLevel;
Daniel Jaspereb65e912015-12-21 18:31:15 +0000568 FormatTok->BlockKind = BK_Block;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000569 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000570 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000571
Francois Ferranda98a95c2017-07-28 07:56:14 +0000572 size_t PPEndHash = computePPHash();
573
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000574 // Munch the closing brace.
575 nextToken(/*LevelDifference=*/AddLevel ? -1 : 0);
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000576
577 if (MacroBlock && FormatTok->is(tok::l_paren))
578 parseParens();
579
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000580 if (MunchSemi && FormatTok->Tok.is(tok::semi))
581 nextToken();
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000582 Line->Level = InitialLevel;
Francois Ferranda98a95c2017-07-28 07:56:14 +0000583
584 if (PPStartHash == PPEndHash) {
585 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
586 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
587 // Update the opening line to add the forward reference as well
Manuel Klimek0dddcf72018-04-23 09:34:26 +0000588 (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
Francois Ferranda98a95c2017-07-28 07:56:14 +0000589 CurrentLines->size() - 1;
590 }
Francois Ferrande56a8292017-06-14 12:29:47 +0000591 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000592}
593
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000594static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000595 // FIXME: Closure-library specific stuff should not be hard-coded but be
596 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000597 if (Line.Tokens.size() < 4)
598 return false;
599 auto I = Line.Tokens.begin();
600 if (I->Tok->TokenText != "goog")
601 return false;
602 ++I;
603 if (I->Tok->isNot(tok::period))
604 return false;
605 ++I;
606 if (I->Tok->TokenText != "scope")
607 return false;
608 ++I;
609 return I->Tok->is(tok::l_paren);
610}
611
Martin Probst101ec892017-05-09 20:04:09 +0000612static bool isIIFE(const UnwrappedLine &Line,
613 const AdditionalKeywords &Keywords) {
614 // Look for the start of an immediately invoked anonymous function.
615 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
616 // This is commonly done in JavaScript to create a new, anonymous scope.
617 // Example: (function() { ... })()
618 if (Line.Tokens.size() < 3)
619 return false;
620 auto I = Line.Tokens.begin();
621 if (I->Tok->isNot(tok::l_paren))
622 return false;
623 ++I;
624 if (I->Tok->isNot(Keywords.kw_function))
625 return false;
626 ++I;
627 return I->Tok->is(tok::l_paren);
628}
629
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000630static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
631 const FormatToken &InitialToken) {
Francois Ferrande8a301f2019-06-06 20:06:23 +0000632 if (InitialToken.isOneOf(tok::kw_namespace, TT_NamespaceMacro))
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000633 return Style.BraceWrapping.AfterNamespace;
634 if (InitialToken.is(tok::kw_class))
635 return Style.BraceWrapping.AfterClass;
636 if (InitialToken.is(tok::kw_union))
637 return Style.BraceWrapping.AfterUnion;
638 if (InitialToken.is(tok::kw_struct))
639 return Style.BraceWrapping.AfterStruct;
640 return false;
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000641}
642
Manuel Klimek516e0542013-09-04 13:25:30 +0000643void UnwrappedLineParser::parseChildBlock() {
644 FormatTok->BlockKind = BK_Block;
645 nextToken();
646 {
Manuel Klimek89628f62017-09-20 09:51:03 +0000647 bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript &&
648 (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
Manuel Klimek516e0542013-09-04 13:25:30 +0000649 ScopedLineState LineState(*this);
650 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
651 /*MustBeDeclaration=*/false);
Martin Probst101ec892017-05-09 20:04:09 +0000652 Line->Level += SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000653 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000654 flushComments(isOnNewLine(*FormatTok));
Martin Probst101ec892017-05-09 20:04:09 +0000655 Line->Level -= SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000656 }
657 nextToken();
658}
659
Daniel Jasperf7935112012-12-03 18:12:45 +0000660void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000661 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000662 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Paul Hoad701a0d72019-03-20 20:49:43 +0000663
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000664 nextToken();
665
Craig Topper2145bc02014-05-09 08:15:10 +0000666 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000667 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000668 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000669 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000670
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000671 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000672 case tok::pp_define:
673 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000674 return;
675 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000676 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000677 break;
678 case tok::pp_ifdef:
679 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000680 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000681 break;
682 case tok::pp_else:
683 parsePPElse();
684 break;
685 case tok::pp_elif:
686 parsePPElIf();
687 break;
688 case tok::pp_endif:
689 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000690 break;
691 default:
692 parsePPUnknown();
693 break;
694 }
695}
696
Manuel Klimek68b03042014-04-14 09:14:11 +0000697void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
Francois Ferranda98a95c2017-07-28 07:56:14 +0000698 size_t Line = CurrentLines->size();
699 if (CurrentLines == &PreprocessorDirectives)
700 Line += Lines.size();
701
702 if (Unreachable ||
703 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
704 PPStack.push_back({PP_Unreachable, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000705 else
Francois Ferranda98a95c2017-07-28 07:56:14 +0000706 PPStack.push_back({PP_Conditional, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000707}
708
Manuel Klimek68b03042014-04-14 09:14:11 +0000709void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000710 ++PPBranchLevel;
711 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
712 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
713 PPLevelBranchIndex.push_back(0);
714 PPLevelBranchCount.push_back(0);
715 }
716 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000717 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
718 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000719}
720
Manuel Klimek68b03042014-04-14 09:14:11 +0000721void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000722 if (!PPStack.empty())
723 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000724 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
725 if (!PPChainBranchIndex.empty())
726 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000727 conditionalCompilationCondition(
728 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
729 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000730}
731
Manuel Klimek68b03042014-04-14 09:14:11 +0000732void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000733 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
734 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
735 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000736 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
737 }
738 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000739 // Guard against #endif's without #if.
Krasimir Georgievad47c902017-08-30 14:34:57 +0000740 if (PPBranchLevel > -1)
Manuel Klimek14bd9172014-01-29 08:49:02 +0000741 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000742 if (!PPChainBranchIndex.empty())
743 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000744 if (!PPStack.empty())
745 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000746}
747
748void UnwrappedLineParser::parsePPIf(bool IfDef) {
Daniel Jasper62703eb2017-03-01 11:10:11 +0000749 bool IfNDef = FormatTok->is(tok::pp_ifndef);
Manuel Klimek68b03042014-04-14 09:14:11 +0000750 nextToken();
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000751 bool Unreachable = false;
752 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
753 Unreachable = true;
Daniel Jasper62703eb2017-03-01 11:10:11 +0000754 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000755 Unreachable = true;
756 conditionalCompilationStart(Unreachable);
Krasimir Georgievad47c902017-08-30 14:34:57 +0000757 FormatToken *IfCondition = FormatTok;
758 // If there's a #ifndef on the first line, and the only lines before it are
759 // comments, it could be an include guard.
760 bool MaybeIncludeGuard = IfNDef;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000761 if (IncludeGuard == IG_Inited && MaybeIncludeGuard)
Krasimir Georgievad47c902017-08-30 14:34:57 +0000762 for (auto &Line : Lines) {
763 if (!Line.Tokens.front().Tok->is(tok::comment)) {
764 MaybeIncludeGuard = false;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000765 IncludeGuard = IG_Rejected;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000766 break;
767 }
768 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000769 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000770 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000771 ++PPBranchLevel;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000772 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
773 IncludeGuard = IG_IfNdefed;
774 IncludeGuardToken = IfCondition;
775 }
Manuel Klimek68b03042014-04-14 09:14:11 +0000776}
777
778void UnwrappedLineParser::parsePPElse() {
Krasimir Georgievad47c902017-08-30 14:34:57 +0000779 // If a potential include guard has an #else, it's not an include guard.
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000780 if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
781 IncludeGuard = IG_Rejected;
Manuel Klimek68b03042014-04-14 09:14:11 +0000782 conditionalCompilationAlternative();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000783 if (PPBranchLevel > -1)
784 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000785 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000786 ++PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000787}
788
789void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
790
791void UnwrappedLineParser::parsePPEndIf() {
792 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000793 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000794 // If the #endif of a potential include guard is the last thing in the file,
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000795 // then we found an include guard.
Krasimir Georgievad47c902017-08-30 14:34:57 +0000796 unsigned TokenPosition = Tokens->getPosition();
797 FormatToken *PeekNext = AllTokens[TokenPosition];
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000798 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
799 PeekNext->is(tok::eof) &&
Daniel Jasper4df130f2017-09-04 13:33:52 +0000800 Style.IndentPPDirectives != FormatStyle::PPDIS_None)
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000801 IncludeGuard = IG_Found;
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000802}
803
Manuel Klimek1abf7892013-01-04 23:34:14 +0000804void UnwrappedLineParser::parsePPDefine() {
805 nextToken();
806
Owen Panfb73b79a2019-04-18 20:17:08 +0000807 if (!FormatTok->Tok.getIdentifierInfo()) {
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000808 IncludeGuard = IG_Rejected;
809 IncludeGuardToken = nullptr;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000810 parsePPUnknown();
811 return;
812 }
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000813
814 if (IncludeGuard == IG_IfNdefed &&
815 IncludeGuardToken->TokenText == FormatTok->TokenText) {
816 IncludeGuard = IG_Defined;
817 IncludeGuardToken = nullptr;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000818 for (auto &Line : Lines) {
819 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000820 IncludeGuard = IG_Rejected;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000821 break;
822 }
823 }
824 }
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000825
Manuel Klimek1abf7892013-01-04 23:34:14 +0000826 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000827 if (FormatTok->Tok.getKind() == tok::l_paren &&
828 FormatTok->WhitespaceRange.getBegin() ==
829 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000830 parseParens();
831 }
Paul Hoad701a0d72019-03-20 20:49:43 +0000832 if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
Krasimir Georgievad47c902017-08-30 14:34:57 +0000833 Line->Level += PPBranchLevel + 1;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000834 addUnwrappedLine();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000835 ++Line->Level;
Manuel Klimek1b896292013-01-07 09:34:28 +0000836
837 // Errors during a preprocessor directive can only affect the layout of the
838 // preprocessor directive, and thus we ignore them. An alternative approach
839 // would be to use the same approach we use on the file level (no
840 // re-indentation if there was a structural error) within the macro
841 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000842 parseFile();
843}
844
845void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000846 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000847 nextToken();
848 } while (!eof());
Paul Hoad701a0d72019-03-20 20:49:43 +0000849 if (Style.IndentPPDirectives != FormatStyle::PPDIS_None)
Krasimir Georgievad47c902017-08-30 14:34:57 +0000850 Line->Level += PPBranchLevel + 1;
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000851 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000852}
853
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000854// Here we blacklist certain tokens that are not usually the first token in an
855// unwrapped line. This is used in attempt to distinguish macro calls without
856// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000857static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000858 // Semicolon can be a null-statement, l_square can be a start of a macro or
859 // a C++11 attribute, but this doesn't seem to be common.
860 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
861 Tok.isNot(tok::l_square) &&
862 // Tokens that can only be used as binary operators and a part of
863 // overloaded operator names.
864 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
865 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
866 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
867 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
868 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
869 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
870 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
871 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
872 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
873 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
874 Tok.isNot(tok::lesslessequal) &&
875 // Colon is used in labels, base class lists, initializer lists,
876 // range-based for loops, ternary operator, but should never be the
877 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000878 Tok.isNot(tok::colon) &&
879 // 'noexcept' is a trailing annotation.
880 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000881}
882
Martin Probst533965c2016-04-19 18:19:06 +0000883static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
884 const FormatToken *FormatTok) {
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000885 // FIXME: This returns true for C/C++ keywords like 'struct'.
886 return FormatTok->is(tok::identifier) &&
887 (FormatTok->Tok.getIdentifierInfo() == nullptr ||
Martin Probst3dbbefa2016-11-10 16:21:02 +0000888 !FormatTok->isOneOf(
889 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
890 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
891 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
892 Keywords.kw_let, Keywords.kw_var, tok::kw_const,
893 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
Manuel Klimek89628f62017-09-20 09:51:03 +0000894 Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws,
895 Keywords.kw_from));
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000896}
897
Martin Probst533965c2016-04-19 18:19:06 +0000898static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
899 const FormatToken *FormatTok) {
Martin Probstb9316ff2016-09-18 17:21:52 +0000900 return FormatTok->Tok.isLiteral() ||
901 FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
902 mustBeJSIdent(Keywords, FormatTok);
Martin Probst533965c2016-04-19 18:19:06 +0000903}
904
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000905// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
906// when encountered after a value (see mustBeJSIdentOrValue).
907static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
908 const FormatToken *FormatTok) {
909 return FormatTok->isOneOf(
Martin Probst5f8445b2016-04-24 22:05:09 +0000910 tok::kw_return, Keywords.kw_yield,
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000911 // conditionals
912 tok::kw_if, tok::kw_else,
913 // loops
914 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
915 // switch/case
916 tok::kw_switch, tok::kw_case,
917 // exceptions
918 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
919 // declaration
920 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
Martin Probst5f8445b2016-04-24 22:05:09 +0000921 Keywords.kw_async, Keywords.kw_function,
922 // import/export
923 Keywords.kw_import, tok::kw_export);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000924}
925
926// readTokenWithJavaScriptASI reads the next token and terminates the current
927// line if JavaScript Automatic Semicolon Insertion must
928// happen between the current token and the next token.
929//
930// This method is conservative - it cannot cover all edge cases of JavaScript,
931// but only aims to correctly handle certain well known cases. It *must not*
932// return true in speculative cases.
933void UnwrappedLineParser::readTokenWithJavaScriptASI() {
934 FormatToken *Previous = FormatTok;
935 readToken();
936 FormatToken *Next = FormatTok;
937
938 bool IsOnSameLine =
939 CommentsBeforeNextToken.empty()
940 ? Next->NewlinesBefore == 0
941 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
942 if (IsOnSameLine)
943 return;
944
945 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
Martin Probst717f6dc2016-10-21 05:11:38 +0000946 bool PreviousStartsTemplateExpr =
947 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${");
Martin Probst7e0f25b2017-11-25 09:19:42 +0000948 if (PreviousMustBeValue || Previous->is(tok::r_paren)) {
949 // If the line contains an '@' sign, the previous token might be an
950 // annotation, which can precede another identifier/value.
951 bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
952 [](UnwrappedLineNode &LineNode) {
953 return LineNode.Tok->is(tok::at);
954 }) != Line->Tokens.end();
955 if (HasAt)
Martin Probstbbffeac2016-04-11 07:35:57 +0000956 return;
957 }
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000958 if (Next->is(tok::exclaim) && PreviousMustBeValue)
Martin Probstd40bca42017-01-09 08:56:36 +0000959 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000960 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
Martin Probst717f6dc2016-10-21 05:11:38 +0000961 bool NextEndsTemplateExpr =
962 Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
963 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
964 (PreviousMustBeValue ||
965 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
966 tok::minusminus)))
Martin Probstd40bca42017-01-09 08:56:36 +0000967 return addUnwrappedLine();
Martin Probst0a19d432017-08-09 15:19:16 +0000968 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) &&
969 isJSDeclOrStmt(Keywords, Next))
Martin Probstd40bca42017-01-09 08:56:36 +0000970 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000971}
972
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000973void UnwrappedLineParser::parseStructuralElement() {
Daniel Jasper498f5582015-12-25 08:53:31 +0000974 assert(!FormatTok->is(tok::l_brace));
975 if (Style.Language == FormatStyle::LK_TableGen &&
976 FormatTok->is(tok::pp_include)) {
977 nextToken();
978 if (FormatTok->is(tok::string_literal))
979 nextToken();
980 addUnwrappedLine();
981 return;
982 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000983 switch (FormatTok->Tok.getKind()) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000984 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000985 nextToken();
986 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000987 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000988 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000989 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000990 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000991 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000992 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000993 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000994 break;
995 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000996 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000997 nextToken();
998 }
999 }
1000 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001001 case tok::kw_namespace:
1002 parseNamespace();
1003 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001004 case tok::kw_public:
1005 case tok::kw_protected:
1006 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +00001007 if (Style.Language == FormatStyle::LK_Java ||
Paul Hoadcbb726d2019-03-21 13:09:22 +00001008 Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp())
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001009 nextToken();
1010 else
1011 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +00001012 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001013 case tok::kw_if:
1014 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +00001015 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001016 case tok::kw_for:
1017 case tok::kw_while:
1018 parseForOrWhileLoop();
1019 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001020 case tok::kw_do:
1021 parseDoWhile();
1022 return;
1023 case tok::kw_switch:
Martin Probstf785fd92017-08-04 17:07:15 +00001024 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1025 // 'switch: string' field declaration.
1026 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001027 parseSwitch();
1028 return;
1029 case tok::kw_default:
Martin Probstf785fd92017-08-04 17:07:15 +00001030 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1031 // 'default: string' field declaration.
1032 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001033 nextToken();
Nico Weberc29f83b2018-01-23 16:30:56 +00001034 if (FormatTok->is(tok::colon)) {
1035 parseLabel();
1036 return;
1037 }
1038 // e.g. "default void f() {}" in a Java interface.
1039 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001040 case tok::kw_case:
Martin Probstf785fd92017-08-04 17:07:15 +00001041 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1042 // 'case: string' field declaration.
1043 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001044 parseCaseLabel();
1045 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001046 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +00001047 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +00001048 parseTryCatch();
1049 return;
Manuel Klimekae610d12013-01-21 14:32:05 +00001050 case tok::kw_extern:
1051 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001052 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +00001053 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001054 if (FormatTok->Tok.is(tok::l_brace)) {
Krasimir Georgievd6ce9372017-09-15 11:23:50 +00001055 if (Style.BraceWrapping.AfterExternBlock) {
1056 addUnwrappedLine();
1057 parseBlock(/*MustBeDeclaration=*/true);
1058 } else {
1059 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
1060 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001061 addUnwrappedLine();
1062 return;
1063 }
1064 }
Daniel Jaspere1e43192014-04-01 12:55:11 +00001065 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +00001066 case tok::kw_export:
1067 if (Style.Language == FormatStyle::LK_JavaScript) {
1068 parseJavaScriptEs6ImportExport();
1069 return;
1070 }
Sam McCall6f3778c2018-09-05 07:44:02 +00001071 if (!Style.isCpp())
1072 break;
1073 // Handle C++ "(inline|export) namespace".
1074 LLVM_FALLTHROUGH;
1075 case tok::kw_inline:
1076 nextToken();
1077 if (FormatTok->Tok.is(tok::kw_namespace)) {
1078 parseNamespace();
1079 return;
1080 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001081 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +00001082 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001083 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001084 parseForOrWhileLoop();
1085 return;
1086 }
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001087 if (FormatTok->is(TT_MacroBlockBegin)) {
1088 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
1089 /*MunchSemi=*/false);
1090 return;
1091 }
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001092 if (FormatTok->is(Keywords.kw_import)) {
1093 if (Style.Language == FormatStyle::LK_JavaScript) {
1094 parseJavaScriptEs6ImportExport();
1095 return;
1096 }
1097 if (Style.Language == FormatStyle::LK_Proto) {
1098 nextToken();
Daniel Jasper8b61d142016-06-20 20:39:53 +00001099 if (FormatTok->is(tok::kw_public))
1100 nextToken();
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001101 if (!FormatTok->is(tok::string_literal))
1102 return;
1103 nextToken();
1104 if (FormatTok->is(tok::semi))
1105 nextToken();
1106 addUnwrappedLine();
1107 return;
1108 }
Daniel Jasper354aa512015-02-19 16:07:32 +00001109 }
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001110 if (Style.isCpp() &&
Daniel Jasper72b33572017-03-31 12:04:37 +00001111 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
Daniel Jaspera00de632015-12-01 12:05:04 +00001112 Keywords.kw_slots, Keywords.kw_qslots)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001113 nextToken();
1114 if (FormatTok->is(tok::colon)) {
1115 nextToken();
1116 addUnwrappedLine();
Daniel Jasper31343832016-07-27 10:13:24 +00001117 return;
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001118 }
Daniel Jasper53395402015-04-07 15:04:40 +00001119 }
Francois Ferrand6f40e212018-10-02 16:37:51 +00001120 if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) {
1121 parseStatementMacro();
1122 return;
1123 }
Francois Ferrande8a301f2019-06-06 20:06:23 +00001124 if (Style.isCpp() && FormatTok->is(TT_NamespaceMacro)) {
1125 parseNamespace();
1126 return;
1127 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001128 // In all other cases, parse the declaration.
1129 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001130 default:
1131 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001132 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001133 do {
Manuel Klimeke411aa82017-09-20 09:29:37 +00001134 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001135 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +00001136 case tok::at:
1137 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001138 if (FormatTok->Tok.is(tok::l_brace)) {
1139 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001140 parseBracedList();
Nico Weberc068ff72018-01-23 17:10:25 +00001141 break;
Hans Wennborg749c1b52018-10-19 16:19:52 +00001142 } else if (Style.Language == FormatStyle::LK_Java &&
1143 FormatTok->is(Keywords.kw_interface)) {
1144 nextToken();
1145 break;
Nico Weberc068ff72018-01-23 17:10:25 +00001146 }
1147 switch (FormatTok->Tok.getObjCKeywordID()) {
1148 case tok::objc_public:
1149 case tok::objc_protected:
1150 case tok::objc_package:
1151 case tok::objc_private:
1152 return parseAccessSpecifier();
1153 case tok::objc_interface:
1154 case tok::objc_implementation:
1155 return parseObjCInterfaceOrImplementation();
1156 case tok::objc_protocol:
1157 if (parseObjCProtocol())
1158 return;
1159 break;
1160 case tok::objc_end:
1161 return; // Handled by the caller.
1162 case tok::objc_optional:
1163 case tok::objc_required:
1164 nextToken();
1165 addUnwrappedLine();
1166 return;
1167 case tok::objc_autoreleasepool:
1168 nextToken();
1169 if (FormatTok->Tok.is(tok::l_brace)) {
Francois Ferranda2484b22018-02-27 13:48:27 +00001170 if (Style.BraceWrapping.AfterControlStatement)
Nico Weberc068ff72018-01-23 17:10:25 +00001171 addUnwrappedLine();
1172 parseBlock(/*MustBeDeclaration=*/false);
1173 }
1174 addUnwrappedLine();
1175 return;
Francois Ferrandba91c3d2018-02-27 13:48:21 +00001176 case tok::objc_synchronized:
1177 nextToken();
1178 if (FormatTok->Tok.is(tok::l_paren))
Paul Hoad5bcf99b2019-03-01 09:09:54 +00001179 // Skip synchronization object
1180 parseParens();
Francois Ferrandba91c3d2018-02-27 13:48:21 +00001181 if (FormatTok->Tok.is(tok::l_brace)) {
Francois Ferranda2484b22018-02-27 13:48:27 +00001182 if (Style.BraceWrapping.AfterControlStatement)
Francois Ferrandba91c3d2018-02-27 13:48:21 +00001183 addUnwrappedLine();
1184 parseBlock(/*MustBeDeclaration=*/false);
1185 }
1186 addUnwrappedLine();
1187 return;
Nico Weberc068ff72018-01-23 17:10:25 +00001188 case tok::objc_try:
1189 // This branch isn't strictly necessary (the kw_try case below would
1190 // do this too after the tok::at is parsed above). But be explicit.
1191 parseTryCatch();
1192 return;
1193 default:
1194 break;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001195 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001196 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001197 case tok::kw_enum:
Daniel Jaspera7900ad2016-05-08 18:12:22 +00001198 // Ignore if this is part of "template <enum ...".
1199 if (Previous && Previous->is(tok::less)) {
1200 nextToken();
1201 break;
1202 }
1203
Daniel Jasper90cf3802015-06-17 09:44:02 +00001204 // parseEnum falls through and does not yet add an unwrapped line as an
1205 // enum definition can start a structural element.
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001206 if (!parseEnum())
1207 break;
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001208 // This only applies for C++.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001209 if (!Style.isCpp()) {
Daniel Jasper90cf3802015-06-17 09:44:02 +00001210 addUnwrappedLine();
1211 return;
1212 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001213 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001214 case tok::kw_typedef:
1215 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +00001216 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
Ben Hamiltond9212ef2019-07-22 18:20:01 +00001217 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS,
Nico Weberff9f4b52019-07-29 13:26:48 +00001218 Keywords.kw_CF_CLOSED_ENUM,
1219 Keywords.kw_NS_CLOSED_ENUM))
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001220 parseEnum();
1221 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001222 case tok::kw_struct:
1223 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +00001224 case tok::kw_class:
Daniel Jasper910807d2015-06-12 04:52:02 +00001225 // parseRecord falls through and does not yet add an unwrapped line as a
1226 // record declaration or definition can start a structural element.
Manuel Klimeke01bab52013-01-15 13:38:33 +00001227 parseRecord();
Paul Hoadcbb726d2019-03-21 13:09:22 +00001228 // This does not apply for Java, JavaScript and C#.
Daniel Jasper910807d2015-06-12 04:52:02 +00001229 if (Style.Language == FormatStyle::LK_Java ||
Paul Hoadcbb726d2019-03-21 13:09:22 +00001230 Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp()) {
Daniel Jasperd5ec65b2016-01-08 07:06:07 +00001231 if (FormatTok->is(tok::semi))
1232 nextToken();
Daniel Jasper910807d2015-06-12 04:52:02 +00001233 addUnwrappedLine();
1234 return;
1235 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001236 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +00001237 case tok::period:
1238 nextToken();
1239 // In Java, classes have an implicit static member "class".
1240 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
1241 FormatTok->is(tok::kw_class))
1242 nextToken();
Daniel Jasperba52fcb2015-09-28 14:29:45 +00001243 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
1244 FormatTok->Tok.getIdentifierInfo())
1245 // JavaScript only has pseudo keywords, all keywords are allowed to
1246 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1247 nextToken();
Daniel Jaspere5d74862014-11-26 08:17:08 +00001248 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001249 case tok::semi:
1250 nextToken();
1251 addUnwrappedLine();
1252 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001253 case tok::r_brace:
1254 addUnwrappedLine();
1255 return;
Daniel Jasperf7935112012-12-03 18:12:45 +00001256 case tok::l_paren:
1257 parseParens();
1258 break;
Daniel Jasper5af04a42015-10-07 03:43:10 +00001259 case tok::kw_operator:
1260 nextToken();
1261 if (FormatTok->isBinaryOperator())
1262 nextToken();
1263 break;
Manuel Klimek516e0542013-09-04 13:25:30 +00001264 case tok::caret:
1265 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +00001266 if (FormatTok->Tok.isAnyIdentifier() ||
1267 FormatTok->isSimpleTypeSpecifier())
1268 nextToken();
1269 if (FormatTok->is(tok::l_paren))
1270 parseParens();
1271 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +00001272 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +00001273 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001274 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001275 if (!tryToParseBracedList()) {
1276 // A block outside of parentheses must be the last part of a
1277 // structural element.
1278 // FIXME: Figure out cases where this is not true, and add projections
1279 // for them (the one we know is missing are lambdas).
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001280 if (Style.BraceWrapping.AfterFunction)
Manuel Klimekab419912013-05-23 09:41:43 +00001281 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +00001282 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +00001283 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001284 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +00001285 return;
1286 }
1287 // Otherwise this was a braced init list, and the structural
1288 // element continues.
1289 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001290 case tok::kw_try:
1291 // We arrive here when parsing function-try blocks.
Owen Pancb5ffbe2018-09-28 09:17:00 +00001292 if (Style.BraceWrapping.AfterFunction)
1293 addUnwrappedLine();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001294 parseTryCatch();
1295 return;
Daniel Jasper40e19212013-05-29 13:16:10 +00001296 case tok::identifier: {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001297 if (FormatTok->is(TT_MacroBlockEnd)) {
1298 addUnwrappedLine();
1299 return;
1300 }
1301
Martin Probst973ff792017-04-27 13:07:24 +00001302 // Function declarations (as opposed to function expressions) are parsed
1303 // on their own unwrapped line by continuing this loop. Function
1304 // expressions (functions that are not on their own line) must not create
1305 // a new unwrapped line, so they are special cased below.
1306 size_t TokenCount = Line->Tokens.size();
Daniel Jasper9326f912015-05-05 08:40:32 +00001307 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst973ff792017-04-27 13:07:24 +00001308 FormatTok->is(Keywords.kw_function) &&
1309 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1310 Keywords.kw_async)))) {
Daniel Jasper069e5f42014-05-20 11:14:57 +00001311 tryToParseJSFunction();
1312 break;
1313 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001314 if ((Style.Language == FormatStyle::LK_JavaScript ||
1315 Style.Language == FormatStyle::LK_Java) &&
1316 FormatTok->is(Keywords.kw_interface)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001317 if (Style.Language == FormatStyle::LK_JavaScript) {
1318 // In JavaScript/TypeScript, "interface" can be used as a standalone
1319 // identifier, e.g. in `var interface = 1;`. If "interface" is
1320 // followed by another identifier, it is very like to be an actual
1321 // interface declaration.
1322 unsigned StoredPosition = Tokens->getPosition();
1323 FormatToken *Next = Tokens->getNextToken();
1324 FormatTok = Tokens->setPosition(StoredPosition);
Martin Probst533965c2016-04-19 18:19:06 +00001325 if (Next && !mustBeJSIdent(Keywords, Next)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001326 nextToken();
1327 break;
1328 }
1329 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001330 parseRecord();
Daniel Jasper259188b2015-06-12 04:56:34 +00001331 addUnwrappedLine();
Daniel Jasper5c235c02015-07-06 14:26:04 +00001332 return;
Daniel Jasper9326f912015-05-05 08:40:32 +00001333 }
1334
Francois Ferrand6f40e212018-10-02 16:37:51 +00001335 if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) {
1336 parseStatementMacro();
1337 return;
1338 }
1339
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001340 // See if the following token should start a new unwrapped line.
Daniel Jasper9326f912015-05-05 08:40:32 +00001341 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +00001342 nextToken();
Owen Pan945890a2019-05-01 15:03:41 +00001343
1344 // JS doesn't have macros, and within classes colons indicate fields, not
1345 // labels.
1346 if (Style.Language == FormatStyle::LK_JavaScript)
1347 break;
1348
1349 TokenCount = Line->Tokens.size();
1350 if (TokenCount == 1 ||
1351 (TokenCount == 2 && Line->Tokens.front().Tok->is(tok::comment))) {
Daniel Jasper676e5162015-04-07 14:36:33 +00001352 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Daniel Jasper40609472016-04-06 15:02:46 +00001353 Line->Tokens.begin()->Tok->MustBreakBefore = true;
Paul Hoad3867a2d2019-09-12 10:07:14 +00001354 parseLabel(!Style.IndentGotoLabels);
Alexander Kornienkode644272013-04-08 22:16:06 +00001355 return;
1356 }
Daniel Jasper680b09b2014-11-05 10:48:04 +00001357 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +00001358 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +00001359 bool FunctionLike = FormatTok->is(tok::l_paren);
1360 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +00001361 parseParens();
Daniel Jaspere60cba12015-05-13 11:35:53 +00001362
1363 bool FollowedByNewline =
1364 CommentsBeforeNextToken.empty()
1365 ? FormatTok->NewlinesBefore > 0
1366 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1367
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001368 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
Daniel Jasper680b09b2014-11-05 10:48:04 +00001369 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +00001370 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +00001371 return;
Alexander Kornienkode644272013-04-08 22:16:06 +00001372 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001373 }
1374 break;
Daniel Jasper40e19212013-05-29 13:16:10 +00001375 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001376 case tok::equal:
Manuel Klimek79e06082015-05-21 12:23:34 +00001377 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1378 // TT_JsFatArrow. The always start an expression or a child block if
1379 // followed by a curly.
1380 if (FormatTok->is(TT_JsFatArrow)) {
1381 nextToken();
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001382 if (FormatTok->is(tok::l_brace))
Manuel Klimek79e06082015-05-21 12:23:34 +00001383 parseChildBlock();
Manuel Klimek79e06082015-05-21 12:23:34 +00001384 break;
1385 }
1386
Daniel Jaspere25509f2012-12-17 11:29:41 +00001387 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001388 if (FormatTok->Tok.is(tok::l_brace)) {
1389 nextToken();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001390 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001391 } else if (Style.Language == FormatStyle::LK_Proto &&
Manuel Klimek89628f62017-09-20 09:51:03 +00001392 FormatTok->Tok.is(tok::less)) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001393 nextToken();
Krasimir Georgiev0b41fcb2017-06-27 13:58:41 +00001394 parseBracedList(/*ContinueOnSemicolons=*/false,
1395 /*ClosingBraceKind=*/tok::greater);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001396 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001397 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001398 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001399 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001400 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +00001401 case tok::kw_new:
1402 parseNew();
1403 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001404 default:
1405 nextToken();
1406 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001407 }
1408 } while (!eof());
1409}
1410
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001411bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001412 if (!Style.isCpp()) {
Daniel Jasper1feab0f2015-06-02 15:31:37 +00001413 nextToken();
1414 return false;
1415 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001416 assert(FormatTok->is(tok::l_square));
1417 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001418 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001419 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001420
Krasimir Georgievc416c522019-03-11 16:02:52 +00001421 bool SeenArrow = false;
1422
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001423 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001424 if (FormatTok->isSimpleTypeSpecifier()) {
1425 nextToken();
1426 continue;
1427 }
Manuel Klimekffdeb592013-09-03 15:10:01 +00001428 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001429 case tok::l_brace:
1430 break;
1431 case tok::l_paren:
1432 parseParens();
1433 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +00001434 case tok::amp:
1435 case tok::star:
1436 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +00001437 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001438 case tok::less:
1439 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001440 case tok::identifier:
Daniel Jasper5eaa0092015-08-13 13:37:08 +00001441 case tok::numeric_constant:
Daniel Jasper1067ab02014-02-11 10:16:55 +00001442 case tok::coloncolon:
Nico Weber41f4d682019-09-13 13:18:55 +00001443 case tok::kw_class:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001444 case tok::kw_mutable:
Ben Hamilton4e442bb2019-01-30 13:54:32 +00001445 case tok::kw_noexcept:
Nico Weber41f4d682019-09-13 13:18:55 +00001446 case tok::kw_template:
1447 case tok::kw_typename:
Krasimir Georgievc416c522019-03-11 16:02:52 +00001448 nextToken();
1449 break;
Jan Korous88e15142019-03-05 19:27:24 +00001450 // Specialization of a template with an integer parameter can contain
1451 // arithmetic, logical, comparison and ternary operators.
Krasimir Georgievc416c522019-03-11 16:02:52 +00001452 //
1453 // FIXME: This also accepts sequences of operators that are not in the scope
1454 // of a template argument list.
1455 //
1456 // In a C++ lambda a template type can only occur after an arrow. We use
1457 // this as an heuristic to distinguish between Objective-C expressions
1458 // followed by an `a->b` expression, such as:
1459 // ([obj func:arg] + a->b)
1460 // Otherwise the code below would parse as a lambda.
Nico Weber41f4d682019-09-13 13:18:55 +00001461 //
1462 // FIXME: This heuristic is incorrect for C++20 generic lambdas with
1463 // explicit template lists: []<bool b = true && false>(U &&u){}
Jan Korous88e15142019-03-05 19:27:24 +00001464 case tok::plus:
1465 case tok::minus:
1466 case tok::exclaim:
1467 case tok::tilde:
1468 case tok::slash:
1469 case tok::percent:
1470 case tok::lessless:
1471 case tok::pipe:
1472 case tok::pipepipe:
1473 case tok::ampamp:
1474 case tok::caret:
1475 case tok::equalequal:
1476 case tok::exclaimequal:
1477 case tok::greaterequal:
1478 case tok::lessequal:
1479 case tok::question:
1480 case tok::colon:
Paul Hoad10de3952019-03-05 22:20:25 +00001481 case tok::kw_true:
1482 case tok::kw_false:
Krasimir Georgievc416c522019-03-11 16:02:52 +00001483 if (SeenArrow) {
1484 nextToken();
1485 break;
1486 }
1487 return true;
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001488 case tok::arrow:
Ben Hamilton30b7d092019-02-08 15:55:18 +00001489 // This might or might not actually be a lambda arrow (this could be an
1490 // ObjC method invocation followed by a dereferencing arrow). We might
1491 // reset this back to TT_Unknown in TokenAnnotator.
Daniel Jasper6f2b88a2015-06-05 13:18:09 +00001492 FormatTok->Type = TT_LambdaArrow;
Krasimir Georgievc416c522019-03-11 16:02:52 +00001493 SeenArrow = true;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001494 nextToken();
1495 break;
1496 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001497 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001498 }
1499 }
Ronald Wamplera83e2db2019-03-26 20:18:14 +00001500 FormatTok->Type = TT_LambdaLBrace;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001501 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +00001502 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001503 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001504}
1505
1506bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
Manuel Klimek89628f62017-09-20 09:51:03 +00001507 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001508 if (Previous &&
1509 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
Manuel Klimekd0f3fe52018-04-11 14:51:54 +00001510 tok::kw_delete, tok::l_square) ||
Manuel Klimek89628f62017-09-20 09:51:03 +00001511 FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() ||
1512 Previous->isSimpleTypeSpecifier())) {
Manuel Klimekffdeb592013-09-03 15:10:01 +00001513 nextToken();
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001514 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001515 }
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001516 nextToken();
Manuel Klimekd0f3fe52018-04-11 14:51:54 +00001517 if (FormatTok->is(tok::l_square)) {
1518 return false;
1519 }
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001520 parseSquare(/*LambdaIntroducer=*/true);
1521 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001522}
1523
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001524void UnwrappedLineParser::tryToParseJSFunction() {
Martin Probst409697e2016-05-29 14:41:07 +00001525 assert(FormatTok->is(Keywords.kw_function) ||
1526 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
Martin Probst5f8445b2016-04-24 22:05:09 +00001527 if (FormatTok->is(Keywords.kw_async))
1528 nextToken();
1529 // Consume "function".
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001530 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001531
Daniel Jasper71e50af2016-11-01 06:22:59 +00001532 // Consume * (generator function). Treat it like C++'s overloaded operators.
1533 if (FormatTok->is(tok::star)) {
1534 FormatTok->Type = TT_OverloadedOperator;
Martin Probst5f8445b2016-04-24 22:05:09 +00001535 nextToken();
Daniel Jasper71e50af2016-11-01 06:22:59 +00001536 }
Martin Probst5f8445b2016-04-24 22:05:09 +00001537
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001538 // Consume function name.
1539 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001540 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001541
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001542 if (FormatTok->isNot(tok::l_paren))
1543 return;
Manuel Klimek79e06082015-05-21 12:23:34 +00001544
1545 // Parse formal parameter list.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001546 parseParens();
Manuel Klimek79e06082015-05-21 12:23:34 +00001547
1548 if (FormatTok->is(tok::colon)) {
1549 // Parse a type definition.
1550 nextToken();
1551
1552 // Eat the type declaration. For braced inline object types, balance braces,
1553 // otherwise just parse until finding an l_brace for the function body.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001554 if (FormatTok->is(tok::l_brace))
1555 tryToParseBracedList();
1556 else
Martin Probstaf16c502017-01-04 13:36:43 +00001557 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
Manuel Klimek79e06082015-05-21 12:23:34 +00001558 nextToken();
Manuel Klimek79e06082015-05-21 12:23:34 +00001559 }
1560
Martin Probstaf16c502017-01-04 13:36:43 +00001561 if (FormatTok->is(tok::semi))
1562 return;
1563
Manuel Klimek79e06082015-05-21 12:23:34 +00001564 parseChildBlock();
1565}
1566
Daniel Jasper3c883d12015-05-18 14:49:19 +00001567bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001568 if (FormatTok->BlockKind == BK_Unknown)
Daniel Jasper3c883d12015-05-18 14:49:19 +00001569 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001570 assert(FormatTok->BlockKind != BK_Unknown);
1571 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001572 return false;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001573 nextToken();
Manuel Klimekab419912013-05-23 09:41:43 +00001574 parseBracedList();
1575 return true;
1576}
1577
Krasimir Georgievff747be2017-06-27 13:43:07 +00001578bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1579 tok::TokenKind ClosingBraceKind) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001580 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001581
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001582 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1583 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001584 do {
Manuel Klimek79e06082015-05-21 12:23:34 +00001585 if (Style.Language == FormatStyle::LK_JavaScript) {
Martin Probst409697e2016-05-29 14:41:07 +00001586 if (FormatTok->is(Keywords.kw_function) ||
1587 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001588 tryToParseJSFunction();
1589 continue;
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001590 }
1591 if (FormatTok->is(TT_JsFatArrow)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001592 nextToken();
1593 // Fat arrows can be followed by simple expressions or by child blocks
1594 // in curly braces.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001595 if (FormatTok->is(tok::l_brace)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001596 parseChildBlock();
1597 continue;
1598 }
1599 }
Martin Probst8e3eba02017-02-07 16:33:13 +00001600 if (FormatTok->is(tok::l_brace)) {
1601 // Could be a method inside of a braced list `{a() { return 1; }}`.
1602 if (tryToParseBracedList())
1603 continue;
1604 parseChildBlock();
1605 }
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001606 }
Krasimir Georgievff747be2017-06-27 13:43:07 +00001607 if (FormatTok->Tok.getKind() == ClosingBraceKind) {
1608 nextToken();
1609 return !HasError;
1610 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001611 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001612 case tok::caret:
1613 nextToken();
1614 if (FormatTok->is(tok::l_brace)) {
1615 parseChildBlock();
1616 }
1617 break;
1618 case tok::l_square:
1619 tryToParseLambda();
1620 break;
Daniel Jaspera87af7a2015-06-30 11:32:22 +00001621 case tok::l_paren:
1622 parseParens();
Daniel Jasperf46dec82015-03-31 14:34:15 +00001623 // JavaScript can just have free standing methods and getters/setters in
1624 // object literals. Detect them by a "{" following ")".
1625 if (Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperf46dec82015-03-31 14:34:15 +00001626 if (FormatTok->is(tok::l_brace))
1627 parseChildBlock();
1628 break;
1629 }
Daniel Jasperf46dec82015-03-31 14:34:15 +00001630 break;
Martin Probst8e3eba02017-02-07 16:33:13 +00001631 case tok::l_brace:
1632 // Assume there are no blocks inside a braced init list apart
1633 // from the ones we explicitly parse out (like lambdas).
1634 FormatTok->BlockKind = BK_BracedInit;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001635 nextToken();
Martin Probst8e3eba02017-02-07 16:33:13 +00001636 parseBracedList();
1637 break;
Krasimir Georgievfa4dbb62017-08-03 13:43:45 +00001638 case tok::less:
1639 if (Style.Language == FormatStyle::LK_Proto) {
1640 nextToken();
1641 parseBracedList(/*ContinueOnSemicolons=*/false,
1642 /*ClosingBraceKind=*/tok::greater);
1643 } else {
1644 nextToken();
1645 }
1646 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001647 case tok::semi:
Daniel Jasperb9a49902016-01-09 15:56:28 +00001648 // JavaScript (or more precisely TypeScript) can have semicolons in braced
1649 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1650 // used for error recovery if we have otherwise determined that this is
1651 // a braced list.
1652 if (Style.Language == FormatStyle::LK_JavaScript) {
1653 nextToken();
1654 break;
1655 }
Daniel Jasper015ed022013-09-13 09:20:45 +00001656 HasError = true;
1657 if (!ContinueOnSemicolons)
1658 return !HasError;
1659 nextToken();
1660 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001661 case tok::comma:
1662 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001663 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001664 default:
1665 nextToken();
1666 break;
1667 }
1668 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001669 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001670}
1671
Daniel Jasperf7935112012-12-03 18:12:45 +00001672void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001673 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001674 nextToken();
1675 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001676 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001677 case tok::l_paren:
1678 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001679 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1680 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001681 break;
1682 case tok::r_paren:
1683 nextToken();
1684 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001685 case tok::r_brace:
1686 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1687 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001688 case tok::l_square:
1689 tryToParseLambda();
1690 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001691 case tok::l_brace:
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001692 if (!tryToParseBracedList())
Manuel Klimekf017dc02013-09-04 13:34:14 +00001693 parseChildBlock();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001694 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001695 case tok::at:
1696 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001697 if (FormatTok->Tok.is(tok::l_brace)) {
1698 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001699 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001700 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001701 break;
Martin Probst1027fb82017-02-07 14:05:30 +00001702 case tok::kw_class:
1703 if (Style.Language == FormatStyle::LK_JavaScript)
1704 parseRecord(/*ParseAsExpr=*/true);
1705 else
1706 nextToken();
1707 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001708 case tok::identifier:
1709 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst409697e2016-05-29 14:41:07 +00001710 (FormatTok->is(Keywords.kw_function) ||
1711 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001712 tryToParseJSFunction();
1713 else
1714 nextToken();
1715 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001716 default:
1717 nextToken();
1718 break;
1719 }
1720 } while (!eof());
1721}
1722
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001723void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
1724 if (!LambdaIntroducer) {
1725 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1726 if (tryToParseLambda())
1727 return;
1728 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001729 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001730 switch (FormatTok->Tok.getKind()) {
1731 case tok::l_paren:
1732 parseParens();
1733 break;
1734 case tok::r_square:
1735 nextToken();
1736 return;
1737 case tok::r_brace:
1738 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1739 return;
1740 case tok::l_square:
1741 parseSquare();
1742 break;
1743 case tok::l_brace: {
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001744 if (!tryToParseBracedList())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001745 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001746 break;
1747 }
1748 case tok::at:
1749 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001750 if (FormatTok->Tok.is(tok::l_brace)) {
1751 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001752 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001753 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001754 break;
1755 default:
1756 nextToken();
1757 break;
1758 }
1759 } while (!eof());
1760}
1761
Daniel Jasperf7935112012-12-03 18:12:45 +00001762void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001763 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001764 nextToken();
Nico Weber1361a4c2019-07-27 02:41:40 +00001765 if (FormatTok->Tok.isOneOf(tok::kw_constexpr, tok::identifier))
Daniel Jasper6a7d5a72017-06-19 07:40:49 +00001766 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001767 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001768 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001769 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001770 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001771 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001772 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001773 if (Style.BraceWrapping.BeforeElse)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001774 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001775 else
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001776 NeedsUnwrappedLine = true;
Daniel Jasperf7935112012-12-03 18:12:45 +00001777 } else {
1778 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001779 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001780 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001781 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001782 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001783 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001784 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001785 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001786 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001787 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001788 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001789 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001790 parseIfThenElse();
1791 } else {
1792 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001793 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001794 parseStructuralElement();
Daniel Jasper451544a2016-05-19 06:30:48 +00001795 if (FormatTok->is(tok::eof))
1796 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001797 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001798 }
1799 } else if (NeedsUnwrappedLine) {
1800 addUnwrappedLine();
1801 }
1802}
1803
Daniel Jasper04a71a42014-05-08 11:58:24 +00001804void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001805 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001806 nextToken();
1807 bool NeedsUnwrappedLine = false;
1808 if (FormatTok->is(tok::colon)) {
1809 // We are in a function try block, what comes is an initializer list.
1810 nextToken();
1811 while (FormatTok->is(tok::identifier)) {
1812 nextToken();
1813 if (FormatTok->is(tok::l_paren))
1814 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001815 if (FormatTok->is(tok::comma))
1816 nextToken();
1817 }
1818 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001819 // Parse try with resource.
1820 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1821 parseParens();
1822 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001823 if (FormatTok->is(tok::l_brace)) {
1824 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1825 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001826 if (Style.BraceWrapping.BeforeCatch) {
Daniel Jasper04a71a42014-05-08 11:58:24 +00001827 addUnwrappedLine();
1828 } else {
1829 NeedsUnwrappedLine = true;
1830 }
1831 } else if (!FormatTok->is(tok::kw_catch)) {
1832 // The C++ standard requires a compound-statement after a try.
1833 // If there's none, we try to assume there's a structuralElement
1834 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001835 addUnwrappedLine();
1836 ++Line->Level;
1837 parseStructuralElement();
1838 --Line->Level;
1839 }
Nico Weber33381f52015-02-07 01:57:32 +00001840 while (1) {
1841 if (FormatTok->is(tok::at))
1842 nextToken();
1843 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1844 tok::kw___finally) ||
1845 ((Style.Language == FormatStyle::LK_Java ||
1846 Style.Language == FormatStyle::LK_JavaScript) &&
1847 FormatTok->is(Keywords.kw_finally)) ||
1848 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1849 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1850 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001851 nextToken();
1852 while (FormatTok->isNot(tok::l_brace)) {
1853 if (FormatTok->is(tok::l_paren)) {
1854 parseParens();
1855 continue;
1856 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001857 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001858 return;
1859 nextToken();
1860 }
1861 NeedsUnwrappedLine = false;
1862 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1863 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001864 if (Style.BraceWrapping.BeforeCatch)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001865 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001866 else
Daniel Jasper04a71a42014-05-08 11:58:24 +00001867 NeedsUnwrappedLine = true;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001868 }
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001869 if (NeedsUnwrappedLine)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001870 addUnwrappedLine();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001871}
1872
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001873void UnwrappedLineParser::parseNamespace() {
Francois Ferrande8a301f2019-06-06 20:06:23 +00001874 assert(FormatTok->isOneOf(tok::kw_namespace, TT_NamespaceMacro) &&
1875 "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001876
1877 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001878 nextToken();
Francois Ferrande8a301f2019-06-06 20:06:23 +00001879 if (InitialToken.is(TT_NamespaceMacro)) {
1880 parseParens();
1881 } else {
Nico Weber37944132019-07-23 17:49:45 +00001882 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw_inline,
1883 tok::l_square)) {
1884 if (FormatTok->is(tok::l_square))
1885 parseSquare();
1886 else
1887 nextToken();
1888 }
Francois Ferrande8a301f2019-06-06 20:06:23 +00001889 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001890 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001891 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001892 addUnwrappedLine();
1893
Daniel Jasper65ee3472013-07-31 23:16:02 +00001894 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1895 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1896 DeclarationScopeStack.size() > 1);
1897 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001898 // Munch the semicolon after a namespace. This is more common than one would
1899 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001900 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001901 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001902 addUnwrappedLine();
1903 }
1904 // FIXME: Add error handling.
1905}
1906
Daniel Jasper6acf5132015-03-12 14:44:29 +00001907void UnwrappedLineParser::parseNew() {
1908 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1909 nextToken();
1910 if (Style.Language != FormatStyle::LK_Java)
1911 return;
1912
1913 // In Java, we can parse everything up to the parens, which aren't optional.
1914 do {
1915 // There should not be a ;, { or } before the new's open paren.
1916 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1917 return;
1918
1919 // Consume the parens.
1920 if (FormatTok->is(tok::l_paren)) {
1921 parseParens();
1922
1923 // If there is a class body of an anonymous class, consume that as child.
1924 if (FormatTok->is(tok::l_brace))
1925 parseChildBlock();
1926 return;
1927 }
1928 nextToken();
1929 } while (!eof());
1930}
1931
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001932void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001933 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001934 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001935 nextToken();
Martin Probsta050f412017-05-18 21:19:29 +00001936 // JS' for await ( ...
Martin Probstbd49e322017-05-15 19:33:20 +00001937 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probsta050f412017-05-18 21:19:29 +00001938 FormatTok->is(Keywords.kw_await))
Martin Probstbd49e322017-05-15 19:33:20 +00001939 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001940 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001941 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001942 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001943 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001944 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001945 addUnwrappedLine();
1946 } else {
1947 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001948 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001949 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001950 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001951 }
1952}
1953
Daniel Jasperf7935112012-12-03 18:12:45 +00001954void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001955 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001956 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001957 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001958 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001959 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001960 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001961 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001962 } else {
1963 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001964 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001965 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001966 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001967 }
1968
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001969 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001970 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001971 addUnwrappedLine();
1972 return;
1973 }
1974
Daniel Jasperf7935112012-12-03 18:12:45 +00001975 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001976 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001977}
1978
Paul Hoad3867a2d2019-09-12 10:07:14 +00001979void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001980 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001981 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001982 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001983 --Line->Level;
Paul Hoad3867a2d2019-09-12 10:07:14 +00001984 if (LeftAlignLabel)
1985 Line->Level = 0;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001986 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Owen Pan806d5742019-04-08 23:36:25 +00001987 CompoundStatementIndenter Indenter(this, Line->Level,
1988 Style.BraceWrapping.AfterCaseLabel,
1989 Style.BraceWrapping.IndentBraces);
Nico Weber9096fc02013-06-26 00:30:14 +00001990 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001991 if (FormatTok->Tok.is(tok::kw_break)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001992 if (Style.BraceWrapping.AfterControlStatement)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001993 addUnwrappedLine();
1994 parseStructuralElement();
1995 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001996 addUnwrappedLine();
1997 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001998 if (FormatTok->is(tok::semi))
1999 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00002000 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00002001 }
Manuel Klimek52b15152013-01-09 15:25:02 +00002002 Line->Level = OldLineLevel;
Daniel Jasper2cce7b72016-04-06 16:41:39 +00002003 if (FormatTok->isNot(tok::l_brace)) {
Daniel Jasper40609472016-04-06 15:02:46 +00002004 parseStructuralElement();
Daniel Jasper2cce7b72016-04-06 16:41:39 +00002005 addUnwrappedLine();
2006 }
Daniel Jasperf7935112012-12-03 18:12:45 +00002007}
2008
2009void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002010 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00002011 // FIXME: fix handling of complex expressions here.
2012 do {
2013 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002014 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00002015 parseLabel();
2016}
2017
2018void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002019 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00002020 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002021 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00002022 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002023 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00002024 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00002025 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00002026 addUnwrappedLine();
2027 } else {
2028 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00002029 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00002030 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00002031 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00002032 }
2033}
2034
2035void UnwrappedLineParser::parseAccessSpecifier() {
2036 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00002037 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00002038 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00002039 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00002040 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002041 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00002042 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00002043 addUnwrappedLine();
2044}
2045
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002046bool UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00002047 // Won't be 'enum' for NS_ENUMs.
2048 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00002049 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002050
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002051 // In TypeScript, "enum" can also be used as property name, e.g. in interface
2052 // declarations. An "enum" keyword followed by a colon would be a syntax
2053 // error and thus assume it is just an identifier.
Daniel Jasper87379302016-02-03 05:33:44 +00002054 if (Style.Language == FormatStyle::LK_JavaScript &&
2055 FormatTok->isOneOf(tok::colon, tok::question))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002056 return false;
2057
Paul Hoada87ba1c2019-03-23 14:24:30 +00002058 // In protobuf, "enum" can be used as a field name.
2059 if (Style.Language == FormatStyle::LK_Proto && FormatTok->is(tok::equal))
2060 return false;
2061
Daniel Jasper2b41a822013-08-20 12:42:50 +00002062 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002063 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
2064 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00002065
Daniel Jasper786a5502013-09-06 21:32:35 +00002066 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00002067 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
2068 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00002069 nextToken();
2070 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00002071 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00002072 parseParens();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00002073 if (FormatTok->is(tok::identifier)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00002074 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00002075 // If there are two identifiers in a row, this is likely an elaborate
2076 // return type. In Java, this can be "implements", etc.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00002077 if (Style.isCpp() && FormatTok->is(tok::identifier))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002078 return false;
Daniel Jasperb5a0b852015-06-19 08:17:32 +00002079 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00002080 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00002081
2082 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00002083 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002084 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00002085 FormatTok->BlockKind = BK_Block;
2086
2087 if (Style.Language == FormatStyle::LK_Java) {
2088 // Java enums are different.
2089 parseJavaEnumBody();
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002090 return true;
2091 }
2092 if (Style.Language == FormatStyle::LK_Proto) {
Daniel Jasperc6dd2732015-07-16 14:25:43 +00002093 parseBlock(/*MustBeDeclaration=*/true);
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002094 return true;
Manuel Klimek2cec0192013-01-21 19:17:52 +00002095 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00002096
2097 // Parse enum body.
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002098 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002099 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
2100 if (HasError) {
2101 if (FormatTok->is(tok::semi))
2102 nextToken();
2103 addUnwrappedLine();
2104 }
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002105 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00002106
Daniel Jasper90cf3802015-06-17 09:44:02 +00002107 // There is no addUnwrappedLine() here so that we fall through to parsing a
2108 // structural element afterwards. Thus, in "enum A {} n, m;",
Manuel Klimek2cec0192013-01-21 19:17:52 +00002109 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00002110}
2111
2112void UnwrappedLineParser::parseJavaEnumBody() {
2113 // Determine whether the enum is simple, i.e. does not have a semicolon or
2114 // constants with class bodies. Simple enums can be formatted like braced
2115 // lists, contracted to a single line, etc.
2116 unsigned StoredPosition = Tokens->getPosition();
2117 bool IsSimple = true;
2118 FormatToken *Tok = Tokens->getNextToken();
2119 while (Tok) {
2120 if (Tok->is(tok::r_brace))
2121 break;
2122 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
2123 IsSimple = false;
2124 break;
2125 }
2126 // FIXME: This will also mark enums with braces in the arguments to enum
2127 // constants as "not simple". This is probably fine in practice, though.
2128 Tok = Tokens->getNextToken();
2129 }
2130 FormatTok = Tokens->setPosition(StoredPosition);
2131
2132 if (IsSimple) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002133 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002134 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00002135 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002136 return;
2137 }
2138
2139 // Parse the body of a more complex enum.
2140 // First add a line for everything up to the "{".
2141 nextToken();
2142 addUnwrappedLine();
2143 ++Line->Level;
2144
2145 // Parse the enum constants.
2146 while (FormatTok) {
2147 if (FormatTok->is(tok::l_brace)) {
2148 // Parse the constant's class body.
2149 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2150 /*MunchSemi=*/false);
2151 } else if (FormatTok->is(tok::l_paren)) {
2152 parseParens();
2153 } else if (FormatTok->is(tok::comma)) {
2154 nextToken();
2155 addUnwrappedLine();
2156 } else if (FormatTok->is(tok::semi)) {
2157 nextToken();
2158 addUnwrappedLine();
2159 break;
2160 } else if (FormatTok->is(tok::r_brace)) {
2161 addUnwrappedLine();
2162 break;
2163 } else {
2164 nextToken();
2165 }
2166 }
2167
2168 // Parse the class body after the enum's ";" if any.
2169 parseLevel(/*HasOpeningBrace=*/true);
2170 nextToken();
2171 --Line->Level;
2172 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00002173}
2174
Martin Probst1027fb82017-02-07 14:05:30 +00002175void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00002176 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00002177 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00002178
Daniel Jasper04785d02015-05-06 14:03:02 +00002179 // The actual identifier can be a nested name specifier, and in macros
2180 // it is often token-pasted.
2181 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2182 tok::kw___attribute, tok::kw___declspec,
2183 tok::kw_alignas) ||
2184 ((Style.Language == FormatStyle::LK_Java ||
2185 Style.Language == FormatStyle::LK_JavaScript) &&
2186 FormatTok->isOneOf(tok::period, tok::comma))) {
Martin Probstcb870c52017-08-01 15:46:10 +00002187 if (Style.Language == FormatStyle::LK_JavaScript &&
2188 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
2189 // JavaScript/TypeScript supports inline object types in
2190 // extends/implements positions:
2191 // class Foo implements {bar: number} { }
2192 nextToken();
2193 if (FormatTok->is(tok::l_brace)) {
2194 tryToParseBracedList();
2195 continue;
2196 }
2197 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002198 bool IsNonMacroIdentifier =
2199 FormatTok->is(tok::identifier) &&
2200 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002201 nextToken();
2202 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00002203 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00002204 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00002205 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00002206
Daniel Jasper04785d02015-05-06 14:03:02 +00002207 // Note that parsing away template declarations here leads to incorrectly
2208 // accepting function declarations as record declarations.
2209 // In general, we cannot solve this problem. Consider:
2210 // class A<int> B() {}
2211 // which can be a function definition or a class definition when B() is a
2212 // macro. If we find enough real-world cases where this is a problem, we
2213 // can parse for the 'template' keyword in the beginning of the statement,
2214 // and thus rule out the record production in case there is no template
2215 // (this would still leave us with an ambiguity between template function
2216 // and class declarations).
Daniel Jasperadba2aa2015-05-18 12:52:00 +00002217 if (FormatTok->isOneOf(tok::colon, tok::less)) {
2218 while (!eof()) {
Daniel Jasper3c883d12015-05-18 14:49:19 +00002219 if (FormatTok->is(tok::l_brace)) {
2220 calculateBraceTypes(/*ExpectClassBody=*/true);
2221 if (!tryToParseBracedList())
2222 break;
2223 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002224 if (FormatTok->Tok.is(tok::semi))
2225 return;
2226 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002227 }
2228 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002229 if (FormatTok->Tok.is(tok::l_brace)) {
Martin Probst1027fb82017-02-07 14:05:30 +00002230 if (ParseAsExpr) {
2231 parseChildBlock();
2232 } else {
2233 if (ShouldBreakBeforeBrace(Style, InitialToken))
2234 addUnwrappedLine();
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002235
Martin Probst1027fb82017-02-07 14:05:30 +00002236 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2237 /*MunchSemi=*/false);
2238 }
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002239 }
Daniel Jasper90cf3802015-06-17 09:44:02 +00002240 // There is no addUnwrappedLine() here so that we fall through to parsing a
2241 // structural element afterwards. Thus, in "class A {} n, m;",
2242 // "} n, m;" will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00002243}
2244
Ben Hamilton707e68f2018-05-30 15:21:38 +00002245void UnwrappedLineParser::parseObjCMethod() {
2246 assert(FormatTok->Tok.isOneOf(tok::l_paren, tok::identifier) &&
2247 "'(' or identifier expected.");
2248 do {
2249 if (FormatTok->Tok.is(tok::semi)) {
2250 nextToken();
2251 addUnwrappedLine();
2252 return;
2253 } else if (FormatTok->Tok.is(tok::l_brace)) {
Ben Hamilton97034a32018-10-12 19:43:01 +00002254 if (Style.BraceWrapping.AfterFunction)
2255 addUnwrappedLine();
Ben Hamilton707e68f2018-05-30 15:21:38 +00002256 parseBlock(/*MustBeDeclaration=*/false);
2257 addUnwrappedLine();
2258 return;
2259 } else {
2260 nextToken();
2261 }
2262 } while (!eof());
2263}
2264
Nico Weber8696a8d2013-01-09 21:15:03 +00002265void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002266 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Ben Hamilton1462e842018-04-05 15:26:25 +00002267 do {
Nico Weber8696a8d2013-01-09 21:15:03 +00002268 nextToken();
Ben Hamilton1462e842018-04-05 15:26:25 +00002269 // Early exit in case someone forgot a close angle.
2270 if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2271 FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2272 return;
2273 } while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00002274 nextToken(); // Skip '>'.
2275}
2276
2277void UnwrappedLineParser::parseObjCUntilAtEnd() {
2278 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002279 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002280 nextToken();
2281 addUnwrappedLine();
2282 break;
2283 }
Daniel Jaspera15da302013-08-28 08:04:23 +00002284 if (FormatTok->is(tok::l_brace)) {
2285 parseBlock(/*MustBeDeclaration=*/false);
2286 // In ObjC interfaces, nothing should be following the "}".
2287 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00002288 } else if (FormatTok->is(tok::r_brace)) {
2289 // Ignore stray "}". parseStructuralElement doesn't consume them.
2290 nextToken();
2291 addUnwrappedLine();
Ben Hamilton707e68f2018-05-30 15:21:38 +00002292 } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
2293 nextToken();
2294 parseObjCMethod();
Daniel Jaspera15da302013-08-28 08:04:23 +00002295 } else {
2296 parseStructuralElement();
2297 }
Nico Weber8696a8d2013-01-09 21:15:03 +00002298 } while (!eof());
2299}
2300
Nico Weber2ce0ac52013-01-09 23:25:37 +00002301void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weberc068ff72018-01-23 17:10:25 +00002302 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
2303 FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
Nico Weber7eecf4b2013-01-09 20:25:35 +00002304 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002305 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00002306
Ben Hamilton1462e842018-04-05 15:26:25 +00002307 // @interface can be followed by a lightweight generic
2308 // specialization list, then either a base class or a category.
2309 if (FormatTok->Tok.is(tok::less)) {
2310 // Unlike protocol lists, generic parameterizations support
2311 // nested angles:
2312 //
2313 // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
2314 // NSObject <NSCopying, NSSecureCoding>
2315 //
2316 // so we need to count how many open angles we have left.
2317 unsigned NumOpenAngles = 1;
2318 do {
2319 nextToken();
2320 // Early exit in case someone forgot a close angle.
2321 if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2322 FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2323 break;
2324 if (FormatTok->Tok.is(tok::less))
2325 ++NumOpenAngles;
2326 else if (FormatTok->Tok.is(tok::greater)) {
2327 assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
2328 --NumOpenAngles;
2329 }
2330 } while (!eof() && NumOpenAngles != 0);
2331 nextToken(); // Skip '>'.
2332 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002333 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00002334 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002335 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002336 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00002337 // Skip category, if present.
2338 parseParens();
2339
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002340 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002341 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00002342
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002343 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00002344 if (Style.BraceWrapping.AfterObjCDeclaration)
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002345 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00002346 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002347 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00002348
2349 // With instance variables, this puts '}' on its own line. Without instance
2350 // variables, this ends the @interface line.
2351 addUnwrappedLine();
2352
Nico Weber8696a8d2013-01-09 21:15:03 +00002353 parseObjCUntilAtEnd();
2354}
Nico Weber7eecf4b2013-01-09 20:25:35 +00002355
Nico Weberc068ff72018-01-23 17:10:25 +00002356// Returns true for the declaration/definition form of @protocol,
2357// false for the expression form.
2358bool UnwrappedLineParser::parseObjCProtocol() {
2359 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
Nico Weber8696a8d2013-01-09 21:15:03 +00002360 nextToken();
Nico Weberc068ff72018-01-23 17:10:25 +00002361
2362 if (FormatTok->is(tok::l_paren))
2363 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
2364 return false;
2365
2366 // The definition/declaration form,
2367 // @protocol Foo
2368 // - (int)someMethod;
2369 // @end
2370
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002371 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00002372
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002373 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002374 parseObjCProtocolList();
2375
2376 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002377 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002378 nextToken();
Nico Weberc068ff72018-01-23 17:10:25 +00002379 addUnwrappedLine();
2380 return true;
Nico Weber8696a8d2013-01-09 21:15:03 +00002381 }
2382
2383 addUnwrappedLine();
2384 parseObjCUntilAtEnd();
Nico Weberc068ff72018-01-23 17:10:25 +00002385 return true;
Nico Weber7eecf4b2013-01-09 20:25:35 +00002386}
2387
Daniel Jasperfca735c2015-02-19 16:14:18 +00002388void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
Martin Probst053f1aa2016-04-19 14:55:37 +00002389 bool IsImport = FormatTok->is(Keywords.kw_import);
2390 assert(IsImport || FormatTok->is(tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00002391 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00002392
Daniel Jasperec05fc72015-05-11 09:14:50 +00002393 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002394 if (FormatTok->is(tok::kw_default))
2395 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00002396
Martin Probst5f8445b2016-04-24 22:05:09 +00002397 // Consume "async function", "function" and "default function", so that these
2398 // get parsed as free-standing JS functions, i.e. do not require a trailing
2399 // semicolon.
2400 if (FormatTok->is(Keywords.kw_async))
2401 nextToken();
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002402 if (FormatTok->is(Keywords.kw_function)) {
2403 nextToken();
2404 return;
2405 }
2406
Martin Probst053f1aa2016-04-19 14:55:37 +00002407 // For imports, `export *`, `export {...}`, consume the rest of the line up
2408 // to the terminating `;`. For everything else, just return and continue
2409 // parsing the structural element, i.e. the declaration or expression for
2410 // `export default`.
2411 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
2412 !FormatTok->isStringLiteral())
2413 return;
Daniel Jasperfca735c2015-02-19 16:14:18 +00002414
Martin Probstd40bca42017-01-09 08:56:36 +00002415 while (!eof()) {
2416 if (FormatTok->is(tok::semi))
2417 return;
Krasimir Georgiev112c2e92017-11-09 13:22:03 +00002418 if (Line->Tokens.empty()) {
Martin Probstd40bca42017-01-09 08:56:36 +00002419 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
2420 // import statement should terminate.
2421 return;
2422 }
Daniel Jasperefc1a832016-01-07 08:53:35 +00002423 if (FormatTok->is(tok::l_brace)) {
2424 FormatTok->BlockKind = BK_Block;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002425 nextToken();
Daniel Jasperefc1a832016-01-07 08:53:35 +00002426 parseBracedList();
2427 } else {
2428 nextToken();
2429 }
Daniel Jasper354aa512015-02-19 16:07:32 +00002430 }
2431}
2432
Paul Hoad5bcf99b2019-03-01 09:09:54 +00002433void UnwrappedLineParser::parseStatementMacro() {
Francois Ferrand6f40e212018-10-02 16:37:51 +00002434 nextToken();
2435 if (FormatTok->is(tok::l_paren))
2436 parseParens();
2437 if (FormatTok->is(tok::semi))
2438 nextToken();
2439 addUnwrappedLine();
2440}
2441
Daniel Jasper3b203a62013-09-05 16:05:56 +00002442LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
2443 StringRef Prefix = "") {
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002444 llvm::dbgs() << Prefix << "Line(" << Line.Level
2445 << ", FSC=" << Line.FirstStartColumn << ")"
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002446 << (Line.InPPDirective ? " MACRO" : "") << ": ";
2447 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2448 E = Line.Tokens.end();
2449 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002450 llvm::dbgs() << I->Tok->Tok.getName() << "["
Manuel Klimek89628f62017-09-20 09:51:03 +00002451 << "T=" << I->Tok->Type << ", OC=" << I->Tok->OriginalColumn
2452 << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002453 }
2454 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2455 E = Line.Tokens.end();
2456 I != E; ++I) {
2457 const UnwrappedLineNode &Node = *I;
2458 for (SmallVectorImpl<UnwrappedLine>::const_iterator
2459 I = Node.Children.begin(),
2460 E = Node.Children.end();
2461 I != E; ++I) {
2462 printDebugInfo(*I, "\nChild: ");
2463 }
2464 }
2465 llvm::dbgs() << "\n";
2466}
2467
Daniel Jasperf7935112012-12-03 18:12:45 +00002468void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002469 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00002470 return;
Nicola Zaghen3538b392018-05-15 13:30:56 +00002471 LLVM_DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002472 if (CurrentLines == &Lines)
2473 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00002474 });
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002475 CurrentLines->push_back(std::move(*Line));
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002476 Line->Tokens.clear();
Krasimir Georgiev85c37042017-03-01 16:38:08 +00002477 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002478 Line->FirstStartColumn = 0;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002479 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002480 CurrentLines->append(
2481 std::make_move_iterator(PreprocessorDirectives.begin()),
2482 std::make_move_iterator(PreprocessorDirectives.end()));
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002483 PreprocessorDirectives.clear();
2484 }
Manuel Klimeke411aa82017-09-20 09:29:37 +00002485 // Disconnect the current token from the last token on the previous line.
2486 FormatTok->Previous = nullptr;
Daniel Jasperf7935112012-12-03 18:12:45 +00002487}
2488
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002489bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00002490
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002491bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002492 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
2493 FormatTok.NewlinesBefore > 0;
2494}
2495
Krasimir Georgiev91834222017-01-25 13:58:58 +00002496// Checks if \p FormatTok is a line comment that continues the line comment
2497// section on \p Line.
Krasimir Georgievea222a72017-05-22 10:07:56 +00002498static bool continuesLineCommentSection(const FormatToken &FormatTok,
2499 const UnwrappedLine &Line,
2500 llvm::Regex &CommentPragmasRegex) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002501 if (Line.Tokens.empty())
2502 return false;
Krasimir Georgiev84321612017-01-30 19:18:55 +00002503
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002504 StringRef IndentContent = FormatTok.TokenText;
2505 if (FormatTok.TokenText.startswith("//") ||
2506 FormatTok.TokenText.startswith("/*"))
2507 IndentContent = FormatTok.TokenText.substr(2);
2508 if (CommentPragmasRegex.match(IndentContent))
2509 return false;
2510
Krasimir Georgiev91834222017-01-25 13:58:58 +00002511 // If Line starts with a line comment, then FormatTok continues the comment
Krasimir Georgiev84321612017-01-30 19:18:55 +00002512 // section if its original column is greater or equal to the original start
Krasimir Georgiev91834222017-01-25 13:58:58 +00002513 // column of the line.
2514 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002515 // Define the min column token of a line as follows: if a line ends in '{' or
2516 // contains a '{' followed by a line comment, then the min column token is
2517 // that '{'. Otherwise, the min column token of the line is the first token of
2518 // the line.
2519 //
2520 // If Line starts with a token other than a line comment, then FormatTok
2521 // continues the comment section if its original column is greater than the
2522 // original start column of the min column token of the line.
Krasimir Georgiev91834222017-01-25 13:58:58 +00002523 //
2524 // For example, the second line comment continues the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002525 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002526 // // first line
2527 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002528 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002529 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002530 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002531 // // first line
2532 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002533 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002534 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002535 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002536 // int i; // first line
2537 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002538 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002539 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002540 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002541 // do { // first line
2542 // // second line
2543 // int i;
2544 // } while (true);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002545 //
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002546 // and:
2547 //
2548 // enum {
2549 // a, // first line
2550 // // second line
2551 // b
2552 // };
2553 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002554 // The second line comment doesn't continue the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002555 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002556 // // first line
2557 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002558 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002559 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002560 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002561 // int i; // first line
2562 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002563 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002564 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002565 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002566 // do { // first line
2567 // // second line
2568 // int i;
2569 // } while (true);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002570 //
2571 // and:
2572 //
2573 // enum {
2574 // a, // first line
2575 // // second line
2576 // };
Krasimir Georgiev84321612017-01-30 19:18:55 +00002577 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
2578
2579 // Scan for '{//'. If found, use the column of '{' as a min column for line
2580 // comment section continuation.
2581 const FormatToken *PreviousToken = nullptr;
Krasimir Georgievd86c25d2017-03-10 13:09:29 +00002582 for (const UnwrappedLineNode &Node : Line.Tokens) {
Krasimir Georgiev84321612017-01-30 19:18:55 +00002583 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
2584 isLineComment(*Node.Tok)) {
2585 MinColumnToken = PreviousToken;
2586 break;
2587 }
2588 PreviousToken = Node.Tok;
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002589
2590 // Grab the last newline preceding a token in this unwrapped line.
2591 if (Node.Tok->NewlinesBefore > 0) {
2592 MinColumnToken = Node.Tok;
2593 }
Krasimir Georgiev84321612017-01-30 19:18:55 +00002594 }
2595 if (PreviousToken && PreviousToken->is(tok::l_brace)) {
2596 MinColumnToken = PreviousToken;
2597 }
2598
Krasimir Georgievea222a72017-05-22 10:07:56 +00002599 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
2600 MinColumnToken);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002601}
2602
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002603void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
2604 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002605 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002606 I = CommentsBeforeNextToken.begin(),
2607 E = CommentsBeforeNextToken.end();
2608 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002609 // Line comments that belong to the same line comment section are put on the
2610 // same line since later we might want to reflow content between them.
Krasimir Georgiev753625b2017-01-31 13:32:38 +00002611 // Additional fine-grained breaking of line comment sections is controlled
2612 // by the class BreakableLineCommentSection in case it is desirable to keep
2613 // several line comment sections in the same unwrapped line.
2614 //
2615 // FIXME: Consider putting separate line comment sections as children to the
2616 // unwrapped line instead.
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002617 (*I)->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002618 continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002619 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002620 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002621 pushToken(*I);
2622 }
Daniel Jaspere60cba12015-05-13 11:35:53 +00002623 if (NewlineBeforeNext && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002624 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002625 CommentsBeforeNextToken.clear();
2626}
2627
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002628void UnwrappedLineParser::nextToken(int LevelDifference) {
Daniel Jasperf7935112012-12-03 18:12:45 +00002629 if (eof())
2630 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002631 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002632 pushToken(FormatTok);
Manuel Klimek89628f62017-09-20 09:51:03 +00002633 FormatToken *Previous = FormatTok;
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002634 if (Style.Language != FormatStyle::LK_JavaScript)
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002635 readToken(LevelDifference);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002636 else
2637 readTokenWithJavaScriptASI();
Manuel Klimeke411aa82017-09-20 09:29:37 +00002638 FormatTok->Previous = Previous;
Daniel Jasperb9a49902016-01-09 15:56:28 +00002639}
2640
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002641void UnwrappedLineParser::distributeComments(
2642 const SmallVectorImpl<FormatToken *> &Comments,
2643 const FormatToken *NextTok) {
2644 // Whether or not a line comment token continues a line is controlled by
Krasimir Georgievea222a72017-05-22 10:07:56 +00002645 // the method continuesLineCommentSection, with the following caveat:
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002646 //
2647 // Define a trail of Comments to be a nonempty proper postfix of Comments such
2648 // that each comment line from the trail is aligned with the next token, if
2649 // the next token exists. If a trail exists, the beginning of the maximal
2650 // trail is marked as a start of a new comment section.
2651 //
2652 // For example in this code:
2653 //
2654 // int a; // line about a
2655 // // line 1 about b
2656 // // line 2 about b
2657 // int b;
2658 //
2659 // the two lines about b form a maximal trail, so there are two sections, the
2660 // first one consisting of the single comment "// line about a" and the
2661 // second one consisting of the next two comments.
2662 if (Comments.empty())
2663 return;
2664 bool ShouldPushCommentsInCurrentLine = true;
2665 bool HasTrailAlignedWithNextToken = false;
2666 unsigned StartOfTrailAlignedWithNextToken = 0;
2667 if (NextTok) {
2668 // We are skipping the first element intentionally.
2669 for (unsigned i = Comments.size() - 1; i > 0; --i) {
2670 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
2671 HasTrailAlignedWithNextToken = true;
2672 StartOfTrailAlignedWithNextToken = i;
2673 }
2674 }
2675 }
2676 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
2677 FormatToken *FormatTok = Comments[i];
Manuel Klimek89628f62017-09-20 09:51:03 +00002678 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002679 FormatTok->ContinuesLineCommentSection = false;
2680 } else {
2681 FormatTok->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002682 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002683 }
2684 if (!FormatTok->ContinuesLineCommentSection &&
2685 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
2686 ShouldPushCommentsInCurrentLine = false;
2687 }
2688 if (ShouldPushCommentsInCurrentLine) {
2689 pushToken(FormatTok);
2690 } else {
2691 CommentsBeforeNextToken.push_back(FormatTok);
2692 }
2693 }
2694}
2695
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002696void UnwrappedLineParser::readToken(int LevelDifference) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002697 SmallVector<FormatToken *, 1> Comments;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002698 do {
2699 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00002700 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002701 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
2702 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002703 distributeComments(Comments, FormatTok);
2704 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002705 // If there is an unfinished unwrapped line, we flush the preprocessor
2706 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00002707 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002708 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002709 assert((LevelDifference >= 0 ||
2710 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2711 "LevelDifference makes Line->Level negative");
2712 Line->Level += LevelDifference;
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00002713 // Comments stored before the preprocessor directive need to be output
2714 // before the preprocessor directive, at the same level as the
2715 // preprocessor directive, as we consider them to apply to the directive.
Paul Hoad701a0d72019-03-20 20:49:43 +00002716 if (Style.IndentPPDirectives == FormatStyle::PPDIS_BeforeHash &&
2717 PPBranchLevel > 0)
2718 Line->Level += PPBranchLevel;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002719 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002720 parsePPDirective();
2721 }
Manuel Klimek68b03042014-04-14 09:14:11 +00002722 while (FormatTok->Type == TT_ConflictStart ||
2723 FormatTok->Type == TT_ConflictEnd ||
2724 FormatTok->Type == TT_ConflictAlternative) {
2725 if (FormatTok->Type == TT_ConflictStart) {
2726 conditionalCompilationStart(/*Unreachable=*/false);
2727 } else if (FormatTok->Type == TT_ConflictAlternative) {
2728 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002729 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00002730 conditionalCompilationEnd();
2731 }
2732 FormatTok = Tokens->getNextToken();
2733 FormatTok->MustBreakBefore = true;
2734 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002735
Francois Ferranda98a95c2017-07-28 07:56:14 +00002736 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002737 !Line->InPPDirective) {
2738 continue;
2739 }
2740
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002741 if (!FormatTok->Tok.is(tok::comment)) {
2742 distributeComments(Comments, FormatTok);
2743 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002744 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002745 }
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002746
2747 Comments.push_back(FormatTok);
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002748 } while (!eof());
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002749
2750 distributeComments(Comments, nullptr);
2751 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002752}
2753
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002754void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002755 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002756 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002757 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002758 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00002759 }
Daniel Jasperf7935112012-12-03 18:12:45 +00002760}
2761
Daniel Jasper8d1832e2013-01-07 13:26:07 +00002762} // end namespace format
2763} // end namespace clang