blob: be7e2bbabac14ba79c959dd75ba643f56a59e507 [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file contains the implementation of the UnwrappedLineParser,
12/// which turns a stream of tokens into UnwrappedLines.
13///
Daniel Jasperf7935112012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Chandler Carruth4b417452013-01-19 08:09:44 +000016#include "UnwrappedLineParser.h"
Benjamin Kramer33335df2015-03-01 21:36:40 +000017#include "llvm/ADT/STLExtras.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000018#include "llvm/Support/Debug.h"
Benjamin Kramer53f5e892015-03-23 18:05:43 +000019#include "llvm/Support/raw_ostream.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000020
Martin Probst7e0f25b2017-11-25 09:19:42 +000021#include <algorithm>
22
Chandler Carruth10346662014-04-22 03:17:02 +000023#define DEBUG_TYPE "format-parser"
24
Daniel Jasperf7935112012-12-03 18:12:45 +000025namespace clang {
26namespace format {
27
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000028class FormatTokenSource {
29public:
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000030 virtual ~FormatTokenSource() {}
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000031 virtual FormatToken *getNextToken() = 0;
32
33 virtual unsigned getPosition() = 0;
34 virtual FormatToken *setPosition(unsigned Position) = 0;
35};
36
Craig Topper69665e12013-07-01 04:21:54 +000037namespace {
38
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000039class ScopedDeclarationState {
40public:
41 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
42 bool MustBeDeclaration)
43 : Line(Line), Stack(Stack) {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000044 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek39080572013-01-23 11:03:04 +000045 Stack.push_back(MustBeDeclaration);
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000046 }
47 ~ScopedDeclarationState() {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000048 Stack.pop_back();
Manuel Klimekc1237a82013-01-23 14:08:21 +000049 if (!Stack.empty())
50 Line.MustBeDeclaration = Stack.back();
51 else
52 Line.MustBeDeclaration = true;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000053 }
Daniel Jasper393564f2013-05-31 14:56:29 +000054
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000055private:
56 UnwrappedLine &Line;
57 std::vector<bool> &Stack;
58};
59
Krasimir Georgieva1c30932017-05-19 10:34:57 +000060static bool isLineComment(const FormatToken &FormatTok) {
Krasimir Georgiev410ed242017-11-10 12:50:09 +000061 return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith("/*");
Krasimir Georgieva1c30932017-05-19 10:34:57 +000062}
63
Krasimir Georgievea222a72017-05-22 10:07:56 +000064// Checks if \p FormatTok is a line comment that continues the line comment
65// \p Previous. The original column of \p MinColumnToken is used to determine
66// whether \p FormatTok is indented enough to the right to continue \p Previous.
67static bool continuesLineComment(const FormatToken &FormatTok,
68 const FormatToken *Previous,
69 const FormatToken *MinColumnToken) {
70 if (!Previous || !MinColumnToken)
71 return false;
72 unsigned MinContinueColumn =
73 MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
74 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
75 isLineComment(*Previous) &&
76 FormatTok.OriginalColumn >= MinContinueColumn;
77}
78
Manuel Klimek1abf7892013-01-04 23:34:14 +000079class ScopedMacroState : public FormatTokenSource {
80public:
81 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek20e0af62015-05-06 11:56:29 +000082 FormatToken *&ResetToken)
Manuel Klimek1abf7892013-01-04 23:34:14 +000083 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000084 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
Krasimir Georgieva1c30932017-05-19 10:34:57 +000085 Token(nullptr), PreviousToken(nullptr) {
Manuel Klimek1abf7892013-01-04 23:34:14 +000086 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000087 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000088 Line.InPPDirective = true;
89 }
90
Alexander Kornienko34eb2072015-04-11 02:00:23 +000091 ~ScopedMacroState() override {
Manuel Klimek1abf7892013-01-04 23:34:14 +000092 TokenSource = PreviousTokenSource;
93 ResetToken = Token;
94 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000095 Line.Level = PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +000096 }
97
Craig Topperfb6b25b2014-03-15 04:29:04 +000098 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +000099 // The \c UnwrappedLineParser guards against this by never calling
100 // \c getNextToken() after it has encountered the first eof token.
101 assert(!eof());
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000102 PreviousToken = Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000103 Token = PreviousTokenSource->getNextToken();
104 if (eof())
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000105 return getFakeEOF();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000106 return Token;
107 }
108
Craig Topperfb6b25b2014-03-15 04:29:04 +0000109 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +0000110
Craig Topperfb6b25b2014-03-15 04:29:04 +0000111 FormatToken *setPosition(unsigned Position) override {
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000112 PreviousToken = nullptr;
Manuel Klimekab419912013-05-23 09:41:43 +0000113 Token = PreviousTokenSource->setPosition(Position);
114 return Token;
115 }
116
Manuel Klimek1abf7892013-01-04 23:34:14 +0000117private:
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000118 bool eof() {
119 return Token && Token->HasUnescapedNewline &&
Krasimir Georgievea222a72017-05-22 10:07:56 +0000120 !continuesLineComment(*Token, PreviousToken,
121 /*MinColumnToken=*/PreviousToken);
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000122 }
Manuel Klimek1abf7892013-01-04 23:34:14 +0000123
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000124 FormatToken *getFakeEOF() {
125 static bool EOFInitialized = false;
126 static FormatToken FormatTok;
127 if (!EOFInitialized) {
128 FormatTok.Tok.startToken();
129 FormatTok.Tok.setKind(tok::eof);
130 EOFInitialized = true;
131 }
132 return &FormatTok;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000133 }
134
135 UnwrappedLine &Line;
136 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000137 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000138 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000139 FormatTokenSource *PreviousTokenSource;
140
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000141 FormatToken *Token;
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000142 FormatToken *PreviousToken;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000143};
144
Craig Topper69665e12013-07-01 04:21:54 +0000145} // end anonymous namespace
146
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000147class ScopedLineState {
148public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000149 ScopedLineState(UnwrappedLineParser &Parser,
150 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000151 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000152 if (SwitchToPreprocessorLines)
153 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000154 else if (!Parser.Line->Tokens.empty())
155 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000156 PreBlockLine = std::move(Parser.Line);
157 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000158 Parser.Line->Level = PreBlockLine->Level;
159 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000160 }
161
162 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000163 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000164 Parser.addUnwrappedLine();
165 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000166 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000167 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000168 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
169 Parser.MustBreakBeforeNextToken = true;
170 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000171 }
172
173private:
174 UnwrappedLineParser &Parser;
175
David Blaikieefb6eb22014-08-09 20:02:07 +0000176 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000177 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000178};
179
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000180class CompoundStatementIndenter {
181public:
182 CompoundStatementIndenter(UnwrappedLineParser *Parser,
183 const FormatStyle &Style, unsigned &LineLevel)
184 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000185 if (Style.BraceWrapping.AfterControlStatement)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000186 Parser->addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000187 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000188 ++LineLevel;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000189 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000190 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000191
192private:
193 unsigned &LineLevel;
194 unsigned OldLineLevel;
195};
196
Craig Topper69665e12013-07-01 04:21:54 +0000197namespace {
198
Manuel Klimekab419912013-05-23 09:41:43 +0000199class IndexedTokenSource : public FormatTokenSource {
200public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000201 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000202 : Tokens(Tokens), Position(-1) {}
203
Craig Topperfb6b25b2014-03-15 04:29:04 +0000204 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000205 ++Position;
206 return Tokens[Position];
207 }
208
Craig Topperfb6b25b2014-03-15 04:29:04 +0000209 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000210 assert(Position >= 0);
211 return Position;
212 }
213
Craig Topperfb6b25b2014-03-15 04:29:04 +0000214 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000215 Position = P;
216 return Tokens[Position];
217 }
218
Manuel Klimek71814b42013-10-11 21:25:45 +0000219 void reset() { Position = -1; }
220
Manuel Klimekab419912013-05-23 09:41:43 +0000221private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000222 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000223 int Position;
224};
225
Craig Topper69665e12013-07-01 04:21:54 +0000226} // end anonymous namespace
227
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000228UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000229 const AdditionalKeywords &Keywords,
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000230 unsigned FirstStartColumn,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000231 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000232 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000233 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000234 CurrentLines(&Lines), Style(Style), Keywords(Keywords),
235 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
Krasimir Georgievad47c902017-08-30 14:34:57 +0000236 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000237 IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
238 ? IG_Rejected
239 : IG_Inited),
240 IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000241
242void UnwrappedLineParser::reset() {
243 PPBranchLevel = -1;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000244 IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None
245 ? IG_Rejected
246 : IG_Inited;
247 IncludeGuardToken = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000248 Line.reset(new UnwrappedLine);
249 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000250 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000251 MustBreakBeforeNextToken = false;
252 PreprocessorDirectives.clear();
253 CurrentLines = &Lines;
254 DeclarationScopeStack.clear();
Manuel Klimek71814b42013-10-11 21:25:45 +0000255 PPStack.clear();
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000256 Line->FirstStartColumn = FirstStartColumn;
Manuel Klimek71814b42013-10-11 21:25:45 +0000257}
Daniel Jasperf7935112012-12-03 18:12:45 +0000258
Manuel Klimek20e0af62015-05-06 11:56:29 +0000259void UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000260 IndexedTokenSource TokenSource(AllTokens);
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000261 Line->FirstStartColumn = FirstStartColumn;
Manuel Klimek71814b42013-10-11 21:25:45 +0000262 do {
263 DEBUG(llvm::dbgs() << "----\n");
264 reset();
265 Tokens = &TokenSource;
266 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000267
Manuel Klimek71814b42013-10-11 21:25:45 +0000268 readToken();
269 parseFile();
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000270
271 // If we found an include guard then all preprocessor directives (other than
272 // the guard) are over-indented by one.
273 if (IncludeGuard == IG_Found)
274 for (auto &Line : Lines)
275 if (Line.InPPDirective && Line.Level > 0)
276 --Line.Level;
277
Manuel Klimek71814b42013-10-11 21:25:45 +0000278 // Create line with eof token.
279 pushToken(FormatTok);
280 addUnwrappedLine();
281
282 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
283 E = Lines.end();
284 I != E; ++I) {
285 Callback.consumeUnwrappedLine(*I);
286 }
287 Callback.finishRun();
288 Lines.clear();
289 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000290 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000291 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
292 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
293 }
294 if (!PPLevelBranchIndex.empty()) {
295 ++PPLevelBranchIndex.back();
296 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
297 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
298 }
299 } while (!PPLevelBranchIndex.empty());
Manuel Klimek1abf7892013-01-04 23:34:14 +0000300}
301
Manuel Klimek1a18c402013-04-12 14:13:36 +0000302void UnwrappedLineParser::parseFile() {
Daniel Jasper9326f912015-05-05 08:40:32 +0000303 // The top-level context in a file always has declarations, except for pre-
304 // processor directives and JavaScript files.
305 bool MustBeDeclaration =
306 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
307 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
308 MustBeDeclaration);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000309 if (Style.Language == FormatStyle::LK_TextProto)
310 parseBracedList();
311 else
312 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000313 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000314 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000315 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000316}
317
Manuel Klimek1a18c402013-04-12 14:13:36 +0000318void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000319 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000320 do {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000321 tok::TokenKind kind = FormatTok->Tok.getKind();
322 if (FormatTok->Type == TT_MacroBlockBegin) {
323 kind = tok::l_brace;
324 } else if (FormatTok->Type == TT_MacroBlockEnd) {
325 kind = tok::r_brace;
326 }
327
328 switch (kind) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000329 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000330 nextToken();
331 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000332 break;
333 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000334 // FIXME: Add parameter whether this can happen - if this happens, we must
335 // be in a non-declaration context.
Daniel Jasperb86e2722015-08-24 13:23:37 +0000336 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
337 continue;
Nico Weber9096fc02013-06-26 00:30:14 +0000338 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000339 addUnwrappedLine();
340 break;
341 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000342 if (HasOpeningBrace)
343 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000344 nextToken();
345 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000346 break;
Nico Weberc29f83b2018-01-23 16:30:56 +0000347 case tok::kw_default: {
348 unsigned StoredPosition = Tokens->getPosition();
349 FormatToken *Next = Tokens->getNextToken();
350 FormatTok = Tokens->setPosition(StoredPosition);
351 if (Next && Next->isNot(tok::colon)) {
352 // default not followed by ':' is not a case label; treat it like
353 // an identifier.
354 parseStructuralElement();
355 break;
356 }
357 // Else, if it is 'default:', fall through to the case handling.
Nico Weberf1add5e2018-01-24 01:47:22 +0000358 LLVM_FALLTHROUGH;
Nico Weberc29f83b2018-01-23 16:30:56 +0000359 }
Daniel Jasper516d7972013-07-25 11:31:57 +0000360 case tok::kw_case:
Manuel Klimek89628f62017-09-20 09:51:03 +0000361 if (Style.Language == FormatStyle::LK_JavaScript &&
362 Line->MustBeDeclaration) {
Martin Probstf785fd92017-08-04 17:07:15 +0000363 // A 'case: string' style field declaration.
364 parseStructuralElement();
365 break;
366 }
Daniel Jasper72407622013-09-02 08:26:29 +0000367 if (!SwitchLabelEncountered &&
368 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
369 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000370 SwitchLabelEncountered = true;
371 parseStructuralElement();
372 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000373 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000374 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000375 break;
376 }
377 } while (!eof());
378}
379
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000380void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
Manuel Klimekab419912013-05-23 09:41:43 +0000381 // We'll parse forward through the tokens until we hit
382 // a closing brace or eof - note that getNextToken() will
383 // parse macros, so this will magically work inside macro
384 // definitions, too.
385 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000386 FormatToken *Tok = FormatTok;
Manuel Klimek89628f62017-09-20 09:51:03 +0000387 const FormatToken *PrevTok = Tok->Previous;
Manuel Klimekab419912013-05-23 09:41:43 +0000388 // Keep a stack of positions of lbrace tokens. We will
389 // update information about whether an lbrace starts a
390 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000391 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000392 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000393 do {
Daniel Jaspereb65e912015-12-21 18:31:15 +0000394 // Get next non-comment token.
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000395 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000396 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000397 do {
398 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000399 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000400 } while (NextTok->is(tok::comment));
401
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000402 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000403 case tok::l_brace:
Martin Probst95ed8e72017-05-31 09:29:40 +0000404 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) {
Martin Probste8e27ca2017-11-25 09:33:47 +0000405 if (PrevTok->isOneOf(tok::colon, tok::less))
406 // A ':' indicates this code is in a type, or a braced list
407 // following a label in an object literal ({a: {b: 1}}).
408 // A '<' could be an object used in a comparison, but that is nonsense
409 // code (can never return true), so more likely it is a generic type
410 // argument (`X<{a: string; b: number}>`).
411 // The code below could be confused by semicolons between the
412 // individual members in a type member list, which would normally
413 // trigger BK_Block. In both cases, this must be parsed as an inline
414 // braced init.
Martin Probst95ed8e72017-05-31 09:29:40 +0000415 Tok->BlockKind = BK_BracedInit;
416 else if (PrevTok->is(tok::r_paren))
417 // `) { }` can only occur in function or method declarations in JS.
418 Tok->BlockKind = BK_Block;
419 } else {
Daniel Jasperb9a49902016-01-09 15:56:28 +0000420 Tok->BlockKind = BK_Unknown;
Martin Probst95ed8e72017-05-31 09:29:40 +0000421 }
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000422 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000423 break;
424 case tok::r_brace:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000425 if (LBraceStack.empty())
426 break;
427 if (LBraceStack.back()->BlockKind == BK_Unknown) {
428 bool ProbablyBracedList = false;
429 if (Style.Language == FormatStyle::LK_Proto) {
430 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
431 } else {
432 // Using OriginalColumn to distinguish between ObjC methods and
433 // binary operators is a bit hacky.
434 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
435 NextTok->OriginalColumn == 0;
Daniel Jasper91b032a2014-05-22 12:46:38 +0000436
Daniel Jasperb9a49902016-01-09 15:56:28 +0000437 // If there is a comma, semicolon or right paren after the closing
438 // brace, we assume this is a braced initializer list. Note that
439 // regardless how we mark inner braces here, we will overwrite the
440 // BlockKind later if we parse a braced list (where all blocks
441 // inside are by default braced lists), or when we explicitly detect
442 // blocks (for example while parsing lambdas).
Martin Probst95ed8e72017-05-31 09:29:40 +0000443 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
444 // braced list in JS.
Daniel Jasperb9a49902016-01-09 15:56:28 +0000445 ProbablyBracedList =
Daniel Jasperacffeb82016-03-05 18:34:26 +0000446 (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probste1e12a72016-08-19 14:35:01 +0000447 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
448 Keywords.kw_as)) ||
Martin Probstb7fb2672017-05-10 13:53:29 +0000449 (Style.isCpp() && NextTok->is(tok::l_paren)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000450 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
451 tok::r_paren, tok::r_square, tok::l_brace,
Martin Probstb7fb2672017-05-10 13:53:29 +0000452 tok::l_square, tok::ellipsis) ||
Daniel Jaspere4ada022016-12-13 10:05:03 +0000453 (NextTok->is(tok::identifier) &&
454 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000455 (NextTok->is(tok::semi) &&
456 (!ExpectClassBody || LBraceStack.size() != 1)) ||
457 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Manuel Klimekab419912013-05-23 09:41:43 +0000458 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000459 if (ProbablyBracedList) {
460 Tok->BlockKind = BK_BracedInit;
461 LBraceStack.back()->BlockKind = BK_BracedInit;
462 } else {
463 Tok->BlockKind = BK_Block;
464 LBraceStack.back()->BlockKind = BK_Block;
465 }
Manuel Klimekab419912013-05-23 09:41:43 +0000466 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000467 LBraceStack.pop_back();
Manuel Klimekab419912013-05-23 09:41:43 +0000468 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000469 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000470 case tok::semi:
471 case tok::kw_if:
472 case tok::kw_while:
473 case tok::kw_for:
474 case tok::kw_switch:
475 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000476 case tok::kw___try:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000477 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000478 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000479 break;
480 default:
481 break;
482 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000483 PrevTok = Tok;
Manuel Klimekab419912013-05-23 09:41:43 +0000484 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000485 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Daniel Jasperb9a49902016-01-09 15:56:28 +0000486
Manuel Klimekab419912013-05-23 09:41:43 +0000487 // Assume other blocks for all unclosed opening braces.
488 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000489 if (LBraceStack[i]->BlockKind == BK_Unknown)
490 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000491 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000492
Manuel Klimekab419912013-05-23 09:41:43 +0000493 FormatTok = Tokens->setPosition(StoredPosition);
494}
495
Francois Ferranda98a95c2017-07-28 07:56:14 +0000496template <class T>
497static inline void hash_combine(std::size_t &seed, const T &v) {
498 std::hash<T> hasher;
499 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
500}
501
502size_t UnwrappedLineParser::computePPHash() const {
503 size_t h = 0;
504 for (const auto &i : PPStack) {
505 hash_combine(h, size_t(i.Kind));
506 hash_combine(h, i.Line);
507 }
508 return h;
509}
510
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000511void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
512 bool MunchSemi) {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000513 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
514 "'{' or macro block token expected");
515 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
Daniel Jaspereb65e912015-12-21 18:31:15 +0000516 FormatTok->BlockKind = BK_Block;
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000517
Francois Ferranda98a95c2017-07-28 07:56:14 +0000518 size_t PPStartHash = computePPHash();
519
Daniel Jasper516d7972013-07-25 11:31:57 +0000520 unsigned InitialLevel = Line->Level;
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000521 nextToken(/*LevelDifference=*/AddLevel ? 1 : 0);
Daniel Jasperf7935112012-12-03 18:12:45 +0000522
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000523 if (MacroBlock && FormatTok->is(tok::l_paren))
524 parseParens();
525
Francois Ferranda98a95c2017-07-28 07:56:14 +0000526 size_t NbPreprocessorDirectives =
527 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000528 addUnwrappedLine();
Francois Ferranda98a95c2017-07-28 07:56:14 +0000529 size_t OpeningLineIndex =
530 CurrentLines->empty()
531 ? (UnwrappedLine::kInvalidIndex)
532 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
Daniel Jasperf7935112012-12-03 18:12:45 +0000533
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000534 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
535 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000536 if (AddLevel)
537 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000538 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000539
Marianne Mailhot-Sarrasin03137c62016-04-14 14:56:49 +0000540 if (eof())
541 return;
542
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000543 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
544 : !FormatTok->is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000545 Line->Level = InitialLevel;
Daniel Jaspereb65e912015-12-21 18:31:15 +0000546 FormatTok->BlockKind = BK_Block;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000547 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000548 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000549
Francois Ferranda98a95c2017-07-28 07:56:14 +0000550 size_t PPEndHash = computePPHash();
551
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000552 // Munch the closing brace.
553 nextToken(/*LevelDifference=*/AddLevel ? -1 : 0);
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000554
555 if (MacroBlock && FormatTok->is(tok::l_paren))
556 parseParens();
557
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000558 if (MunchSemi && FormatTok->Tok.is(tok::semi))
559 nextToken();
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000560 Line->Level = InitialLevel;
Francois Ferranda98a95c2017-07-28 07:56:14 +0000561
562 if (PPStartHash == PPEndHash) {
563 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
564 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
565 // Update the opening line to add the forward reference as well
566 (*CurrentLines)[OpeningLineIndex].MatchingOpeningBlockLineIndex =
567 CurrentLines->size() - 1;
568 }
Francois Ferrande56a8292017-06-14 12:29:47 +0000569 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000570}
571
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000572static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000573 // FIXME: Closure-library specific stuff should not be hard-coded but be
574 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000575 if (Line.Tokens.size() < 4)
576 return false;
577 auto I = Line.Tokens.begin();
578 if (I->Tok->TokenText != "goog")
579 return false;
580 ++I;
581 if (I->Tok->isNot(tok::period))
582 return false;
583 ++I;
584 if (I->Tok->TokenText != "scope")
585 return false;
586 ++I;
587 return I->Tok->is(tok::l_paren);
588}
589
Martin Probst101ec892017-05-09 20:04:09 +0000590static bool isIIFE(const UnwrappedLine &Line,
591 const AdditionalKeywords &Keywords) {
592 // Look for the start of an immediately invoked anonymous function.
593 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
594 // This is commonly done in JavaScript to create a new, anonymous scope.
595 // Example: (function() { ... })()
596 if (Line.Tokens.size() < 3)
597 return false;
598 auto I = Line.Tokens.begin();
599 if (I->Tok->isNot(tok::l_paren))
600 return false;
601 ++I;
602 if (I->Tok->isNot(Keywords.kw_function))
603 return false;
604 ++I;
605 return I->Tok->is(tok::l_paren);
606}
607
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000608static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
609 const FormatToken &InitialToken) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000610 if (InitialToken.is(tok::kw_namespace))
611 return Style.BraceWrapping.AfterNamespace;
612 if (InitialToken.is(tok::kw_class))
613 return Style.BraceWrapping.AfterClass;
614 if (InitialToken.is(tok::kw_union))
615 return Style.BraceWrapping.AfterUnion;
616 if (InitialToken.is(tok::kw_struct))
617 return Style.BraceWrapping.AfterStruct;
618 return false;
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000619}
620
Manuel Klimek516e0542013-09-04 13:25:30 +0000621void UnwrappedLineParser::parseChildBlock() {
622 FormatTok->BlockKind = BK_Block;
623 nextToken();
624 {
Manuel Klimek89628f62017-09-20 09:51:03 +0000625 bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript &&
626 (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
Manuel Klimek516e0542013-09-04 13:25:30 +0000627 ScopedLineState LineState(*this);
628 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
629 /*MustBeDeclaration=*/false);
Martin Probst101ec892017-05-09 20:04:09 +0000630 Line->Level += SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000631 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000632 flushComments(isOnNewLine(*FormatTok));
Martin Probst101ec892017-05-09 20:04:09 +0000633 Line->Level -= SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000634 }
635 nextToken();
636}
637
Daniel Jasperf7935112012-12-03 18:12:45 +0000638void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000639 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000640 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000641 nextToken();
642
Craig Topper2145bc02014-05-09 08:15:10 +0000643 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000644 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000645 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000646 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000647
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000648 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000649 case tok::pp_define:
650 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000651 return;
652 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000653 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000654 break;
655 case tok::pp_ifdef:
656 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000657 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000658 break;
659 case tok::pp_else:
660 parsePPElse();
661 break;
662 case tok::pp_elif:
663 parsePPElIf();
664 break;
665 case tok::pp_endif:
666 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000667 break;
668 default:
669 parsePPUnknown();
670 break;
671 }
672}
673
Manuel Klimek68b03042014-04-14 09:14:11 +0000674void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
Francois Ferranda98a95c2017-07-28 07:56:14 +0000675 size_t Line = CurrentLines->size();
676 if (CurrentLines == &PreprocessorDirectives)
677 Line += Lines.size();
678
679 if (Unreachable ||
680 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
681 PPStack.push_back({PP_Unreachable, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000682 else
Francois Ferranda98a95c2017-07-28 07:56:14 +0000683 PPStack.push_back({PP_Conditional, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000684}
685
Manuel Klimek68b03042014-04-14 09:14:11 +0000686void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000687 ++PPBranchLevel;
688 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
689 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
690 PPLevelBranchIndex.push_back(0);
691 PPLevelBranchCount.push_back(0);
692 }
693 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000694 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
695 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000696}
697
Manuel Klimek68b03042014-04-14 09:14:11 +0000698void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000699 if (!PPStack.empty())
700 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000701 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
702 if (!PPChainBranchIndex.empty())
703 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000704 conditionalCompilationCondition(
705 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
706 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000707}
708
Manuel Klimek68b03042014-04-14 09:14:11 +0000709void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000710 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
711 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
712 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000713 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
714 }
715 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000716 // Guard against #endif's without #if.
Krasimir Georgievad47c902017-08-30 14:34:57 +0000717 if (PPBranchLevel > -1)
Manuel Klimek14bd9172014-01-29 08:49:02 +0000718 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000719 if (!PPChainBranchIndex.empty())
720 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000721 if (!PPStack.empty())
722 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000723}
724
725void UnwrappedLineParser::parsePPIf(bool IfDef) {
Daniel Jasper62703eb2017-03-01 11:10:11 +0000726 bool IfNDef = FormatTok->is(tok::pp_ifndef);
Manuel Klimek68b03042014-04-14 09:14:11 +0000727 nextToken();
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000728 bool Unreachable = false;
729 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
730 Unreachable = true;
Daniel Jasper62703eb2017-03-01 11:10:11 +0000731 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000732 Unreachable = true;
733 conditionalCompilationStart(Unreachable);
Krasimir Georgievad47c902017-08-30 14:34:57 +0000734 FormatToken *IfCondition = FormatTok;
735 // If there's a #ifndef on the first line, and the only lines before it are
736 // comments, it could be an include guard.
737 bool MaybeIncludeGuard = IfNDef;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000738 if (IncludeGuard == IG_Inited && MaybeIncludeGuard)
Krasimir Georgievad47c902017-08-30 14:34:57 +0000739 for (auto &Line : Lines) {
740 if (!Line.Tokens.front().Tok->is(tok::comment)) {
741 MaybeIncludeGuard = false;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000742 IncludeGuard = IG_Rejected;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000743 break;
744 }
745 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000746 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000747 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000748 ++PPBranchLevel;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000749 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
750 IncludeGuard = IG_IfNdefed;
751 IncludeGuardToken = IfCondition;
752 }
Manuel Klimek68b03042014-04-14 09:14:11 +0000753}
754
755void UnwrappedLineParser::parsePPElse() {
Krasimir Georgievad47c902017-08-30 14:34:57 +0000756 // If a potential include guard has an #else, it's not an include guard.
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000757 if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
758 IncludeGuard = IG_Rejected;
Manuel Klimek68b03042014-04-14 09:14:11 +0000759 conditionalCompilationAlternative();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000760 if (PPBranchLevel > -1)
761 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000762 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000763 ++PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000764}
765
766void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
767
768void UnwrappedLineParser::parsePPEndIf() {
769 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000770 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000771 // If the #endif of a potential include guard is the last thing in the file,
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000772 // then we found an include guard.
Krasimir Georgievad47c902017-08-30 14:34:57 +0000773 unsigned TokenPosition = Tokens->getPosition();
774 FormatToken *PeekNext = AllTokens[TokenPosition];
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000775 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
776 PeekNext->is(tok::eof) &&
Daniel Jasper4df130f2017-09-04 13:33:52 +0000777 Style.IndentPPDirectives != FormatStyle::PPDIS_None)
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000778 IncludeGuard = IG_Found;
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000779}
780
Manuel Klimek1abf7892013-01-04 23:34:14 +0000781void UnwrappedLineParser::parsePPDefine() {
782 nextToken();
783
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000784 if (FormatTok->Tok.getKind() != tok::identifier) {
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000785 IncludeGuard = IG_Rejected;
786 IncludeGuardToken = nullptr;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000787 parsePPUnknown();
788 return;
789 }
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000790
791 if (IncludeGuard == IG_IfNdefed &&
792 IncludeGuardToken->TokenText == FormatTok->TokenText) {
793 IncludeGuard = IG_Defined;
794 IncludeGuardToken = nullptr;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000795 for (auto &Line : Lines) {
796 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000797 IncludeGuard = IG_Rejected;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000798 break;
799 }
800 }
801 }
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000802
Manuel Klimek1abf7892013-01-04 23:34:14 +0000803 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000804 if (FormatTok->Tok.getKind() == tok::l_paren &&
805 FormatTok->WhitespaceRange.getBegin() ==
806 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000807 parseParens();
808 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000809 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
810 Line->Level += PPBranchLevel + 1;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000811 addUnwrappedLine();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000812 ++Line->Level;
Manuel Klimek1b896292013-01-07 09:34:28 +0000813
814 // Errors during a preprocessor directive can only affect the layout of the
815 // preprocessor directive, and thus we ignore them. An alternative approach
816 // would be to use the same approach we use on the file level (no
817 // re-indentation if there was a structural error) within the macro
818 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000819 parseFile();
820}
821
822void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000823 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000824 nextToken();
825 } while (!eof());
Krasimir Georgievad47c902017-08-30 14:34:57 +0000826 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
827 Line->Level += PPBranchLevel + 1;
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000828 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000829}
830
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000831// Here we blacklist certain tokens that are not usually the first token in an
832// unwrapped line. This is used in attempt to distinguish macro calls without
833// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000834static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000835 // Semicolon can be a null-statement, l_square can be a start of a macro or
836 // a C++11 attribute, but this doesn't seem to be common.
837 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
838 Tok.isNot(tok::l_square) &&
839 // Tokens that can only be used as binary operators and a part of
840 // overloaded operator names.
841 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
842 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
843 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
844 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
845 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
846 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
847 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
848 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
849 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
850 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
851 Tok.isNot(tok::lesslessequal) &&
852 // Colon is used in labels, base class lists, initializer lists,
853 // range-based for loops, ternary operator, but should never be the
854 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000855 Tok.isNot(tok::colon) &&
856 // 'noexcept' is a trailing annotation.
857 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000858}
859
Martin Probst533965c2016-04-19 18:19:06 +0000860static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
861 const FormatToken *FormatTok) {
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000862 // FIXME: This returns true for C/C++ keywords like 'struct'.
863 return FormatTok->is(tok::identifier) &&
864 (FormatTok->Tok.getIdentifierInfo() == nullptr ||
Martin Probst3dbbefa2016-11-10 16:21:02 +0000865 !FormatTok->isOneOf(
866 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
867 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
868 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
869 Keywords.kw_let, Keywords.kw_var, tok::kw_const,
870 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
Manuel Klimek89628f62017-09-20 09:51:03 +0000871 Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws,
872 Keywords.kw_from));
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000873}
874
Martin Probst533965c2016-04-19 18:19:06 +0000875static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
876 const FormatToken *FormatTok) {
Martin Probstb9316ff2016-09-18 17:21:52 +0000877 return FormatTok->Tok.isLiteral() ||
878 FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
879 mustBeJSIdent(Keywords, FormatTok);
Martin Probst533965c2016-04-19 18:19:06 +0000880}
881
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000882// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
883// when encountered after a value (see mustBeJSIdentOrValue).
884static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
885 const FormatToken *FormatTok) {
886 return FormatTok->isOneOf(
Martin Probst5f8445b2016-04-24 22:05:09 +0000887 tok::kw_return, Keywords.kw_yield,
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000888 // conditionals
889 tok::kw_if, tok::kw_else,
890 // loops
891 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
892 // switch/case
893 tok::kw_switch, tok::kw_case,
894 // exceptions
895 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
896 // declaration
897 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
Martin Probst5f8445b2016-04-24 22:05:09 +0000898 Keywords.kw_async, Keywords.kw_function,
899 // import/export
900 Keywords.kw_import, tok::kw_export);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000901}
902
903// readTokenWithJavaScriptASI reads the next token and terminates the current
904// line if JavaScript Automatic Semicolon Insertion must
905// happen between the current token and the next token.
906//
907// This method is conservative - it cannot cover all edge cases of JavaScript,
908// but only aims to correctly handle certain well known cases. It *must not*
909// return true in speculative cases.
910void UnwrappedLineParser::readTokenWithJavaScriptASI() {
911 FormatToken *Previous = FormatTok;
912 readToken();
913 FormatToken *Next = FormatTok;
914
915 bool IsOnSameLine =
916 CommentsBeforeNextToken.empty()
917 ? Next->NewlinesBefore == 0
918 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
919 if (IsOnSameLine)
920 return;
921
922 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
Martin Probst717f6dc2016-10-21 05:11:38 +0000923 bool PreviousStartsTemplateExpr =
924 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${");
Martin Probst7e0f25b2017-11-25 09:19:42 +0000925 if (PreviousMustBeValue || Previous->is(tok::r_paren)) {
926 // If the line contains an '@' sign, the previous token might be an
927 // annotation, which can precede another identifier/value.
928 bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
929 [](UnwrappedLineNode &LineNode) {
930 return LineNode.Tok->is(tok::at);
931 }) != Line->Tokens.end();
932 if (HasAt)
Martin Probstbbffeac2016-04-11 07:35:57 +0000933 return;
934 }
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000935 if (Next->is(tok::exclaim) && PreviousMustBeValue)
Martin Probstd40bca42017-01-09 08:56:36 +0000936 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000937 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
Martin Probst717f6dc2016-10-21 05:11:38 +0000938 bool NextEndsTemplateExpr =
939 Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
940 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
941 (PreviousMustBeValue ||
942 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
943 tok::minusminus)))
Martin Probstd40bca42017-01-09 08:56:36 +0000944 return addUnwrappedLine();
Martin Probst0a19d432017-08-09 15:19:16 +0000945 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) &&
946 isJSDeclOrStmt(Keywords, Next))
Martin Probstd40bca42017-01-09 08:56:36 +0000947 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000948}
949
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000950void UnwrappedLineParser::parseStructuralElement() {
Daniel Jasper498f5582015-12-25 08:53:31 +0000951 assert(!FormatTok->is(tok::l_brace));
952 if (Style.Language == FormatStyle::LK_TableGen &&
953 FormatTok->is(tok::pp_include)) {
954 nextToken();
955 if (FormatTok->is(tok::string_literal))
956 nextToken();
957 addUnwrappedLine();
958 return;
959 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000960 switch (FormatTok->Tok.getKind()) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000961 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000962 nextToken();
963 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000964 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000965 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000966 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000967 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000968 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000969 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000970 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000971 break;
972 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000973 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000974 nextToken();
975 }
976 }
977 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000978 case tok::kw_namespace:
979 parseNamespace();
980 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000981 case tok::kw_inline:
982 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000983 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000984 parseNamespace();
985 return;
986 }
987 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000988 case tok::kw_public:
989 case tok::kw_protected:
990 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +0000991 if (Style.Language == FormatStyle::LK_Java ||
992 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000993 nextToken();
994 else
995 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000996 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000997 case tok::kw_if:
998 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000999 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001000 case tok::kw_for:
1001 case tok::kw_while:
1002 parseForOrWhileLoop();
1003 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001004 case tok::kw_do:
1005 parseDoWhile();
1006 return;
1007 case tok::kw_switch:
Martin Probstf785fd92017-08-04 17:07:15 +00001008 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1009 // 'switch: string' field declaration.
1010 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001011 parseSwitch();
1012 return;
1013 case tok::kw_default:
Martin Probstf785fd92017-08-04 17:07:15 +00001014 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1015 // 'default: string' field declaration.
1016 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001017 nextToken();
Nico Weberc29f83b2018-01-23 16:30:56 +00001018 if (FormatTok->is(tok::colon)) {
1019 parseLabel();
1020 return;
1021 }
1022 // e.g. "default void f() {}" in a Java interface.
1023 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001024 case tok::kw_case:
Martin Probstf785fd92017-08-04 17:07:15 +00001025 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1026 // 'case: string' field declaration.
1027 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001028 parseCaseLabel();
1029 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001030 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +00001031 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +00001032 parseTryCatch();
1033 return;
Manuel Klimekae610d12013-01-21 14:32:05 +00001034 case tok::kw_extern:
1035 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001036 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +00001037 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001038 if (FormatTok->Tok.is(tok::l_brace)) {
Krasimir Georgievd6ce9372017-09-15 11:23:50 +00001039 if (Style.BraceWrapping.AfterExternBlock) {
1040 addUnwrappedLine();
1041 parseBlock(/*MustBeDeclaration=*/true);
1042 } else {
1043 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
1044 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001045 addUnwrappedLine();
1046 return;
1047 }
1048 }
Daniel Jaspere1e43192014-04-01 12:55:11 +00001049 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +00001050 case tok::kw_export:
1051 if (Style.Language == FormatStyle::LK_JavaScript) {
1052 parseJavaScriptEs6ImportExport();
1053 return;
1054 }
1055 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +00001056 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001057 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001058 parseForOrWhileLoop();
1059 return;
1060 }
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001061 if (FormatTok->is(TT_MacroBlockBegin)) {
1062 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
1063 /*MunchSemi=*/false);
1064 return;
1065 }
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001066 if (FormatTok->is(Keywords.kw_import)) {
1067 if (Style.Language == FormatStyle::LK_JavaScript) {
1068 parseJavaScriptEs6ImportExport();
1069 return;
1070 }
1071 if (Style.Language == FormatStyle::LK_Proto) {
1072 nextToken();
Daniel Jasper8b61d142016-06-20 20:39:53 +00001073 if (FormatTok->is(tok::kw_public))
1074 nextToken();
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001075 if (!FormatTok->is(tok::string_literal))
1076 return;
1077 nextToken();
1078 if (FormatTok->is(tok::semi))
1079 nextToken();
1080 addUnwrappedLine();
1081 return;
1082 }
Daniel Jasper354aa512015-02-19 16:07:32 +00001083 }
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001084 if (Style.isCpp() &&
Daniel Jasper72b33572017-03-31 12:04:37 +00001085 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
Daniel Jaspera00de632015-12-01 12:05:04 +00001086 Keywords.kw_slots, Keywords.kw_qslots)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001087 nextToken();
1088 if (FormatTok->is(tok::colon)) {
1089 nextToken();
1090 addUnwrappedLine();
Daniel Jasper31343832016-07-27 10:13:24 +00001091 return;
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001092 }
Daniel Jasper53395402015-04-07 15:04:40 +00001093 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001094 // In all other cases, parse the declaration.
1095 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001096 default:
1097 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001098 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001099 do {
Manuel Klimeke411aa82017-09-20 09:29:37 +00001100 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001101 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +00001102 case tok::at:
1103 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001104 if (FormatTok->Tok.is(tok::l_brace)) {
1105 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001106 parseBracedList();
Nico Weberc068ff72018-01-23 17:10:25 +00001107 break;
1108 }
1109 switch (FormatTok->Tok.getObjCKeywordID()) {
1110 case tok::objc_public:
1111 case tok::objc_protected:
1112 case tok::objc_package:
1113 case tok::objc_private:
1114 return parseAccessSpecifier();
1115 case tok::objc_interface:
1116 case tok::objc_implementation:
1117 return parseObjCInterfaceOrImplementation();
1118 case tok::objc_protocol:
1119 if (parseObjCProtocol())
1120 return;
1121 break;
1122 case tok::objc_end:
1123 return; // Handled by the caller.
1124 case tok::objc_optional:
1125 case tok::objc_required:
1126 nextToken();
1127 addUnwrappedLine();
1128 return;
1129 case tok::objc_autoreleasepool:
1130 nextToken();
1131 if (FormatTok->Tok.is(tok::l_brace)) {
Francois Ferranda2484b22018-02-27 13:48:27 +00001132 if (Style.BraceWrapping.AfterControlStatement)
Nico Weberc068ff72018-01-23 17:10:25 +00001133 addUnwrappedLine();
1134 parseBlock(/*MustBeDeclaration=*/false);
1135 }
1136 addUnwrappedLine();
1137 return;
Francois Ferrandba91c3d2018-02-27 13:48:21 +00001138 case tok::objc_synchronized:
1139 nextToken();
1140 if (FormatTok->Tok.is(tok::l_paren))
1141 // Skip synchronization object
1142 parseParens();
1143 if (FormatTok->Tok.is(tok::l_brace)) {
Francois Ferranda2484b22018-02-27 13:48:27 +00001144 if (Style.BraceWrapping.AfterControlStatement)
Francois Ferrandba91c3d2018-02-27 13:48:21 +00001145 addUnwrappedLine();
1146 parseBlock(/*MustBeDeclaration=*/false);
1147 }
1148 addUnwrappedLine();
1149 return;
Nico Weberc068ff72018-01-23 17:10:25 +00001150 case tok::objc_try:
1151 // This branch isn't strictly necessary (the kw_try case below would
1152 // do this too after the tok::at is parsed above). But be explicit.
1153 parseTryCatch();
1154 return;
1155 default:
1156 break;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001157 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001158 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001159 case tok::kw_enum:
Daniel Jaspera7900ad2016-05-08 18:12:22 +00001160 // Ignore if this is part of "template <enum ...".
1161 if (Previous && Previous->is(tok::less)) {
1162 nextToken();
1163 break;
1164 }
1165
Daniel Jasper90cf3802015-06-17 09:44:02 +00001166 // parseEnum falls through and does not yet add an unwrapped line as an
1167 // enum definition can start a structural element.
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001168 if (!parseEnum())
1169 break;
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001170 // This only applies for C++.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001171 if (!Style.isCpp()) {
Daniel Jasper90cf3802015-06-17 09:44:02 +00001172 addUnwrappedLine();
1173 return;
1174 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001175 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001176 case tok::kw_typedef:
1177 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +00001178 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1179 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001180 parseEnum();
1181 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001182 case tok::kw_struct:
1183 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +00001184 case tok::kw_class:
Daniel Jasper910807d2015-06-12 04:52:02 +00001185 // parseRecord falls through and does not yet add an unwrapped line as a
1186 // record declaration or definition can start a structural element.
Manuel Klimeke01bab52013-01-15 13:38:33 +00001187 parseRecord();
Daniel Jasper910807d2015-06-12 04:52:02 +00001188 // This does not apply for Java and JavaScript.
1189 if (Style.Language == FormatStyle::LK_Java ||
1190 Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperd5ec65b2016-01-08 07:06:07 +00001191 if (FormatTok->is(tok::semi))
1192 nextToken();
Daniel Jasper910807d2015-06-12 04:52:02 +00001193 addUnwrappedLine();
1194 return;
1195 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001196 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +00001197 case tok::period:
1198 nextToken();
1199 // In Java, classes have an implicit static member "class".
1200 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
1201 FormatTok->is(tok::kw_class))
1202 nextToken();
Daniel Jasperba52fcb2015-09-28 14:29:45 +00001203 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
1204 FormatTok->Tok.getIdentifierInfo())
1205 // JavaScript only has pseudo keywords, all keywords are allowed to
1206 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1207 nextToken();
Daniel Jaspere5d74862014-11-26 08:17:08 +00001208 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001209 case tok::semi:
1210 nextToken();
1211 addUnwrappedLine();
1212 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001213 case tok::r_brace:
1214 addUnwrappedLine();
1215 return;
Daniel Jasperf7935112012-12-03 18:12:45 +00001216 case tok::l_paren:
1217 parseParens();
1218 break;
Daniel Jasper5af04a42015-10-07 03:43:10 +00001219 case tok::kw_operator:
1220 nextToken();
1221 if (FormatTok->isBinaryOperator())
1222 nextToken();
1223 break;
Manuel Klimek516e0542013-09-04 13:25:30 +00001224 case tok::caret:
1225 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +00001226 if (FormatTok->Tok.isAnyIdentifier() ||
1227 FormatTok->isSimpleTypeSpecifier())
1228 nextToken();
1229 if (FormatTok->is(tok::l_paren))
1230 parseParens();
1231 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +00001232 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +00001233 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001234 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001235 if (!tryToParseBracedList()) {
1236 // A block outside of parentheses must be the last part of a
1237 // structural element.
1238 // FIXME: Figure out cases where this is not true, and add projections
1239 // for them (the one we know is missing are lambdas).
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001240 if (Style.BraceWrapping.AfterFunction)
Manuel Klimekab419912013-05-23 09:41:43 +00001241 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +00001242 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +00001243 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001244 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +00001245 return;
1246 }
1247 // Otherwise this was a braced init list, and the structural
1248 // element continues.
1249 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001250 case tok::kw_try:
1251 // We arrive here when parsing function-try blocks.
1252 parseTryCatch();
1253 return;
Daniel Jasper40e19212013-05-29 13:16:10 +00001254 case tok::identifier: {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001255 if (FormatTok->is(TT_MacroBlockEnd)) {
1256 addUnwrappedLine();
1257 return;
1258 }
1259
Martin Probst973ff792017-04-27 13:07:24 +00001260 // Function declarations (as opposed to function expressions) are parsed
1261 // on their own unwrapped line by continuing this loop. Function
1262 // expressions (functions that are not on their own line) must not create
1263 // a new unwrapped line, so they are special cased below.
1264 size_t TokenCount = Line->Tokens.size();
Daniel Jasper9326f912015-05-05 08:40:32 +00001265 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst973ff792017-04-27 13:07:24 +00001266 FormatTok->is(Keywords.kw_function) &&
1267 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1268 Keywords.kw_async)))) {
Daniel Jasper069e5f42014-05-20 11:14:57 +00001269 tryToParseJSFunction();
1270 break;
1271 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001272 if ((Style.Language == FormatStyle::LK_JavaScript ||
1273 Style.Language == FormatStyle::LK_Java) &&
1274 FormatTok->is(Keywords.kw_interface)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001275 if (Style.Language == FormatStyle::LK_JavaScript) {
1276 // In JavaScript/TypeScript, "interface" can be used as a standalone
1277 // identifier, e.g. in `var interface = 1;`. If "interface" is
1278 // followed by another identifier, it is very like to be an actual
1279 // interface declaration.
1280 unsigned StoredPosition = Tokens->getPosition();
1281 FormatToken *Next = Tokens->getNextToken();
1282 FormatTok = Tokens->setPosition(StoredPosition);
Martin Probst533965c2016-04-19 18:19:06 +00001283 if (Next && !mustBeJSIdent(Keywords, Next)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001284 nextToken();
1285 break;
1286 }
1287 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001288 parseRecord();
Daniel Jasper259188b2015-06-12 04:56:34 +00001289 addUnwrappedLine();
Daniel Jasper5c235c02015-07-06 14:26:04 +00001290 return;
Daniel Jasper9326f912015-05-05 08:40:32 +00001291 }
1292
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001293 // See if the following token should start a new unwrapped line.
Daniel Jasper9326f912015-05-05 08:40:32 +00001294 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +00001295 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +00001296 if (Line->Tokens.size() == 1 &&
1297 // JS doesn't have macros, and within classes colons indicate fields,
1298 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +00001299 Style.Language != FormatStyle::LK_JavaScript) {
1300 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Daniel Jasper40609472016-04-06 15:02:46 +00001301 Line->Tokens.begin()->Tok->MustBreakBefore = true;
Alexander Kornienkode644272013-04-08 22:16:06 +00001302 parseLabel();
1303 return;
1304 }
Daniel Jasper680b09b2014-11-05 10:48:04 +00001305 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +00001306 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +00001307 bool FunctionLike = FormatTok->is(tok::l_paren);
1308 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +00001309 parseParens();
Daniel Jaspere60cba12015-05-13 11:35:53 +00001310
1311 bool FollowedByNewline =
1312 CommentsBeforeNextToken.empty()
1313 ? FormatTok->NewlinesBefore > 0
1314 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1315
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001316 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
Daniel Jasper680b09b2014-11-05 10:48:04 +00001317 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +00001318 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +00001319 return;
Alexander Kornienkode644272013-04-08 22:16:06 +00001320 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001321 }
1322 break;
Daniel Jasper40e19212013-05-29 13:16:10 +00001323 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001324 case tok::equal:
Manuel Klimek79e06082015-05-21 12:23:34 +00001325 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1326 // TT_JsFatArrow. The always start an expression or a child block if
1327 // followed by a curly.
1328 if (FormatTok->is(TT_JsFatArrow)) {
1329 nextToken();
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001330 if (FormatTok->is(tok::l_brace))
Manuel Klimek79e06082015-05-21 12:23:34 +00001331 parseChildBlock();
Manuel Klimek79e06082015-05-21 12:23:34 +00001332 break;
1333 }
1334
Daniel Jaspere25509f2012-12-17 11:29:41 +00001335 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001336 if (FormatTok->Tok.is(tok::l_brace)) {
1337 nextToken();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001338 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001339 } else if (Style.Language == FormatStyle::LK_Proto &&
Manuel Klimek89628f62017-09-20 09:51:03 +00001340 FormatTok->Tok.is(tok::less)) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001341 nextToken();
Krasimir Georgiev0b41fcb2017-06-27 13:58:41 +00001342 parseBracedList(/*ContinueOnSemicolons=*/false,
1343 /*ClosingBraceKind=*/tok::greater);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001344 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001345 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001346 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001347 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001348 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +00001349 case tok::kw_new:
1350 parseNew();
1351 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001352 default:
1353 nextToken();
1354 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001355 }
1356 } while (!eof());
1357}
1358
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001359bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001360 if (!Style.isCpp()) {
Daniel Jasper1feab0f2015-06-02 15:31:37 +00001361 nextToken();
1362 return false;
1363 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001364 assert(FormatTok->is(tok::l_square));
1365 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001366 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001367 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001368
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001369 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001370 if (FormatTok->isSimpleTypeSpecifier()) {
1371 nextToken();
1372 continue;
1373 }
Manuel Klimekffdeb592013-09-03 15:10:01 +00001374 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001375 case tok::l_brace:
1376 break;
1377 case tok::l_paren:
1378 parseParens();
1379 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +00001380 case tok::amp:
1381 case tok::star:
1382 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +00001383 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001384 case tok::less:
1385 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001386 case tok::identifier:
Daniel Jasper5eaa0092015-08-13 13:37:08 +00001387 case tok::numeric_constant:
Daniel Jasper1067ab02014-02-11 10:16:55 +00001388 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001389 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +00001390 nextToken();
1391 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001392 case tok::arrow:
Daniel Jasper6f2b88a2015-06-05 13:18:09 +00001393 FormatTok->Type = TT_LambdaArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001394 nextToken();
1395 break;
1396 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001397 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001398 }
1399 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001400 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +00001401 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001402 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001403}
1404
1405bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
Manuel Klimek89628f62017-09-20 09:51:03 +00001406 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001407 if (Previous &&
1408 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1409 tok::kw_delete) ||
Manuel Klimek89628f62017-09-20 09:51:03 +00001410 FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() ||
1411 Previous->isSimpleTypeSpecifier())) {
Manuel Klimekffdeb592013-09-03 15:10:01 +00001412 nextToken();
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001413 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001414 }
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001415 nextToken();
1416 parseSquare(/*LambdaIntroducer=*/true);
1417 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001418}
1419
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001420void UnwrappedLineParser::tryToParseJSFunction() {
Martin Probst409697e2016-05-29 14:41:07 +00001421 assert(FormatTok->is(Keywords.kw_function) ||
1422 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
Martin Probst5f8445b2016-04-24 22:05:09 +00001423 if (FormatTok->is(Keywords.kw_async))
1424 nextToken();
1425 // Consume "function".
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001426 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001427
Daniel Jasper71e50af2016-11-01 06:22:59 +00001428 // Consume * (generator function). Treat it like C++'s overloaded operators.
1429 if (FormatTok->is(tok::star)) {
1430 FormatTok->Type = TT_OverloadedOperator;
Martin Probst5f8445b2016-04-24 22:05:09 +00001431 nextToken();
Daniel Jasper71e50af2016-11-01 06:22:59 +00001432 }
Martin Probst5f8445b2016-04-24 22:05:09 +00001433
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001434 // Consume function name.
1435 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001436 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001437
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001438 if (FormatTok->isNot(tok::l_paren))
1439 return;
Manuel Klimek79e06082015-05-21 12:23:34 +00001440
1441 // Parse formal parameter list.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001442 parseParens();
Manuel Klimek79e06082015-05-21 12:23:34 +00001443
1444 if (FormatTok->is(tok::colon)) {
1445 // Parse a type definition.
1446 nextToken();
1447
1448 // Eat the type declaration. For braced inline object types, balance braces,
1449 // otherwise just parse until finding an l_brace for the function body.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001450 if (FormatTok->is(tok::l_brace))
1451 tryToParseBracedList();
1452 else
Martin Probstaf16c502017-01-04 13:36:43 +00001453 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
Manuel Klimek79e06082015-05-21 12:23:34 +00001454 nextToken();
Manuel Klimek79e06082015-05-21 12:23:34 +00001455 }
1456
Martin Probstaf16c502017-01-04 13:36:43 +00001457 if (FormatTok->is(tok::semi))
1458 return;
1459
Manuel Klimek79e06082015-05-21 12:23:34 +00001460 parseChildBlock();
1461}
1462
Daniel Jasper3c883d12015-05-18 14:49:19 +00001463bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001464 if (FormatTok->BlockKind == BK_Unknown)
Daniel Jasper3c883d12015-05-18 14:49:19 +00001465 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001466 assert(FormatTok->BlockKind != BK_Unknown);
1467 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001468 return false;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001469 nextToken();
Manuel Klimekab419912013-05-23 09:41:43 +00001470 parseBracedList();
1471 return true;
1472}
1473
Krasimir Georgievff747be2017-06-27 13:43:07 +00001474bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1475 tok::TokenKind ClosingBraceKind) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001476 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001477
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001478 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1479 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001480 do {
Manuel Klimek79e06082015-05-21 12:23:34 +00001481 if (Style.Language == FormatStyle::LK_JavaScript) {
Martin Probst409697e2016-05-29 14:41:07 +00001482 if (FormatTok->is(Keywords.kw_function) ||
1483 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001484 tryToParseJSFunction();
1485 continue;
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001486 }
1487 if (FormatTok->is(TT_JsFatArrow)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001488 nextToken();
1489 // Fat arrows can be followed by simple expressions or by child blocks
1490 // in curly braces.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001491 if (FormatTok->is(tok::l_brace)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001492 parseChildBlock();
1493 continue;
1494 }
1495 }
Martin Probst8e3eba02017-02-07 16:33:13 +00001496 if (FormatTok->is(tok::l_brace)) {
1497 // Could be a method inside of a braced list `{a() { return 1; }}`.
1498 if (tryToParseBracedList())
1499 continue;
1500 parseChildBlock();
1501 }
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001502 }
Krasimir Georgievff747be2017-06-27 13:43:07 +00001503 if (FormatTok->Tok.getKind() == ClosingBraceKind) {
1504 nextToken();
1505 return !HasError;
1506 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001507 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001508 case tok::caret:
1509 nextToken();
1510 if (FormatTok->is(tok::l_brace)) {
1511 parseChildBlock();
1512 }
1513 break;
1514 case tok::l_square:
1515 tryToParseLambda();
1516 break;
Daniel Jaspera87af7a2015-06-30 11:32:22 +00001517 case tok::l_paren:
1518 parseParens();
Daniel Jasperf46dec82015-03-31 14:34:15 +00001519 // JavaScript can just have free standing methods and getters/setters in
1520 // object literals. Detect them by a "{" following ")".
1521 if (Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperf46dec82015-03-31 14:34:15 +00001522 if (FormatTok->is(tok::l_brace))
1523 parseChildBlock();
1524 break;
1525 }
Daniel Jasperf46dec82015-03-31 14:34:15 +00001526 break;
Martin Probst8e3eba02017-02-07 16:33:13 +00001527 case tok::l_brace:
1528 // Assume there are no blocks inside a braced init list apart
1529 // from the ones we explicitly parse out (like lambdas).
1530 FormatTok->BlockKind = BK_BracedInit;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001531 nextToken();
Martin Probst8e3eba02017-02-07 16:33:13 +00001532 parseBracedList();
1533 break;
Krasimir Georgievfa4dbb62017-08-03 13:43:45 +00001534 case tok::less:
1535 if (Style.Language == FormatStyle::LK_Proto) {
1536 nextToken();
1537 parseBracedList(/*ContinueOnSemicolons=*/false,
1538 /*ClosingBraceKind=*/tok::greater);
1539 } else {
1540 nextToken();
1541 }
1542 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001543 case tok::semi:
Daniel Jasperb9a49902016-01-09 15:56:28 +00001544 // JavaScript (or more precisely TypeScript) can have semicolons in braced
1545 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1546 // used for error recovery if we have otherwise determined that this is
1547 // a braced list.
1548 if (Style.Language == FormatStyle::LK_JavaScript) {
1549 nextToken();
1550 break;
1551 }
Daniel Jasper015ed022013-09-13 09:20:45 +00001552 HasError = true;
1553 if (!ContinueOnSemicolons)
1554 return !HasError;
1555 nextToken();
1556 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001557 case tok::comma:
1558 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001559 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001560 default:
1561 nextToken();
1562 break;
1563 }
1564 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001565 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001566}
1567
Daniel Jasperf7935112012-12-03 18:12:45 +00001568void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001569 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001570 nextToken();
1571 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001572 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001573 case tok::l_paren:
1574 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001575 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1576 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001577 break;
1578 case tok::r_paren:
1579 nextToken();
1580 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001581 case tok::r_brace:
1582 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1583 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001584 case tok::l_square:
1585 tryToParseLambda();
1586 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001587 case tok::l_brace:
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001588 if (!tryToParseBracedList())
Manuel Klimekf017dc02013-09-04 13:34:14 +00001589 parseChildBlock();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001590 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001591 case tok::at:
1592 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001593 if (FormatTok->Tok.is(tok::l_brace)) {
1594 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001595 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001596 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001597 break;
Martin Probst1027fb82017-02-07 14:05:30 +00001598 case tok::kw_class:
1599 if (Style.Language == FormatStyle::LK_JavaScript)
1600 parseRecord(/*ParseAsExpr=*/true);
1601 else
1602 nextToken();
1603 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001604 case tok::identifier:
1605 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst409697e2016-05-29 14:41:07 +00001606 (FormatTok->is(Keywords.kw_function) ||
1607 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001608 tryToParseJSFunction();
1609 else
1610 nextToken();
1611 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001612 default:
1613 nextToken();
1614 break;
1615 }
1616 } while (!eof());
1617}
1618
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001619void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
1620 if (!LambdaIntroducer) {
1621 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1622 if (tryToParseLambda())
1623 return;
1624 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001625 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001626 switch (FormatTok->Tok.getKind()) {
1627 case tok::l_paren:
1628 parseParens();
1629 break;
1630 case tok::r_square:
1631 nextToken();
1632 return;
1633 case tok::r_brace:
1634 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1635 return;
1636 case tok::l_square:
1637 parseSquare();
1638 break;
1639 case tok::l_brace: {
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001640 if (!tryToParseBracedList())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001641 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001642 break;
1643 }
1644 case tok::at:
1645 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001646 if (FormatTok->Tok.is(tok::l_brace)) {
1647 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001648 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001649 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001650 break;
1651 default:
1652 nextToken();
1653 break;
1654 }
1655 } while (!eof());
1656}
1657
Daniel Jasperf7935112012-12-03 18:12:45 +00001658void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001659 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001660 nextToken();
Daniel Jasper6a7d5a72017-06-19 07:40:49 +00001661 if (FormatTok->Tok.is(tok::kw_constexpr))
1662 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001663 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001664 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001665 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001666 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001667 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001668 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001669 if (Style.BraceWrapping.BeforeElse)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001670 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001671 else
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001672 NeedsUnwrappedLine = true;
Daniel Jasperf7935112012-12-03 18:12:45 +00001673 } else {
1674 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001675 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001676 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001677 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001678 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001679 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001680 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001681 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001682 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001683 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001684 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001685 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001686 parseIfThenElse();
1687 } else {
1688 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001689 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001690 parseStructuralElement();
Daniel Jasper451544a2016-05-19 06:30:48 +00001691 if (FormatTok->is(tok::eof))
1692 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001693 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001694 }
1695 } else if (NeedsUnwrappedLine) {
1696 addUnwrappedLine();
1697 }
1698}
1699
Daniel Jasper04a71a42014-05-08 11:58:24 +00001700void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001701 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001702 nextToken();
1703 bool NeedsUnwrappedLine = false;
1704 if (FormatTok->is(tok::colon)) {
1705 // We are in a function try block, what comes is an initializer list.
1706 nextToken();
1707 while (FormatTok->is(tok::identifier)) {
1708 nextToken();
1709 if (FormatTok->is(tok::l_paren))
1710 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001711 if (FormatTok->is(tok::comma))
1712 nextToken();
1713 }
1714 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001715 // Parse try with resource.
1716 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1717 parseParens();
1718 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001719 if (FormatTok->is(tok::l_brace)) {
1720 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1721 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001722 if (Style.BraceWrapping.BeforeCatch) {
Daniel Jasper04a71a42014-05-08 11:58:24 +00001723 addUnwrappedLine();
1724 } else {
1725 NeedsUnwrappedLine = true;
1726 }
1727 } else if (!FormatTok->is(tok::kw_catch)) {
1728 // The C++ standard requires a compound-statement after a try.
1729 // If there's none, we try to assume there's a structuralElement
1730 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001731 addUnwrappedLine();
1732 ++Line->Level;
1733 parseStructuralElement();
1734 --Line->Level;
1735 }
Nico Weber33381f52015-02-07 01:57:32 +00001736 while (1) {
1737 if (FormatTok->is(tok::at))
1738 nextToken();
1739 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1740 tok::kw___finally) ||
1741 ((Style.Language == FormatStyle::LK_Java ||
1742 Style.Language == FormatStyle::LK_JavaScript) &&
1743 FormatTok->is(Keywords.kw_finally)) ||
1744 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1745 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1746 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001747 nextToken();
1748 while (FormatTok->isNot(tok::l_brace)) {
1749 if (FormatTok->is(tok::l_paren)) {
1750 parseParens();
1751 continue;
1752 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001753 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001754 return;
1755 nextToken();
1756 }
1757 NeedsUnwrappedLine = false;
1758 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1759 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001760 if (Style.BraceWrapping.BeforeCatch)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001761 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001762 else
Daniel Jasper04a71a42014-05-08 11:58:24 +00001763 NeedsUnwrappedLine = true;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001764 }
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001765 if (NeedsUnwrappedLine)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001766 addUnwrappedLine();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001767}
1768
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001769void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001770 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001771
1772 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001773 nextToken();
Saleem Abdulrasool328085f2015-10-30 05:07:56 +00001774 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001775 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001776 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001777 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001778 addUnwrappedLine();
1779
Daniel Jasper65ee3472013-07-31 23:16:02 +00001780 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1781 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1782 DeclarationScopeStack.size() > 1);
1783 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001784 // Munch the semicolon after a namespace. This is more common than one would
1785 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001786 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001787 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001788 addUnwrappedLine();
1789 }
1790 // FIXME: Add error handling.
1791}
1792
Daniel Jasper6acf5132015-03-12 14:44:29 +00001793void UnwrappedLineParser::parseNew() {
1794 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1795 nextToken();
1796 if (Style.Language != FormatStyle::LK_Java)
1797 return;
1798
1799 // In Java, we can parse everything up to the parens, which aren't optional.
1800 do {
1801 // There should not be a ;, { or } before the new's open paren.
1802 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1803 return;
1804
1805 // Consume the parens.
1806 if (FormatTok->is(tok::l_paren)) {
1807 parseParens();
1808
1809 // If there is a class body of an anonymous class, consume that as child.
1810 if (FormatTok->is(tok::l_brace))
1811 parseChildBlock();
1812 return;
1813 }
1814 nextToken();
1815 } while (!eof());
1816}
1817
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001818void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001819 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001820 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001821 nextToken();
Martin Probsta050f412017-05-18 21:19:29 +00001822 // JS' for await ( ...
Martin Probstbd49e322017-05-15 19:33:20 +00001823 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probsta050f412017-05-18 21:19:29 +00001824 FormatTok->is(Keywords.kw_await))
Martin Probstbd49e322017-05-15 19:33:20 +00001825 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001826 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001827 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001828 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001829 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001830 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001831 addUnwrappedLine();
1832 } else {
1833 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001834 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001835 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001836 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001837 }
1838}
1839
Daniel Jasperf7935112012-12-03 18:12:45 +00001840void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001841 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001842 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001843 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001844 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001845 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001846 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001847 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001848 } else {
1849 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001850 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001851 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001852 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001853 }
1854
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001855 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001856 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001857 addUnwrappedLine();
1858 return;
1859 }
1860
Daniel Jasperf7935112012-12-03 18:12:45 +00001861 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001862 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001863}
1864
1865void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001866 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001867 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001868 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001869 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001870 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001871 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001872 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001873 if (FormatTok->Tok.is(tok::kw_break)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001874 if (Style.BraceWrapping.AfterControlStatement)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001875 addUnwrappedLine();
1876 parseStructuralElement();
1877 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001878 addUnwrappedLine();
1879 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001880 if (FormatTok->is(tok::semi))
1881 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001882 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001883 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001884 Line->Level = OldLineLevel;
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001885 if (FormatTok->isNot(tok::l_brace)) {
Daniel Jasper40609472016-04-06 15:02:46 +00001886 parseStructuralElement();
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001887 addUnwrappedLine();
1888 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001889}
1890
1891void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001892 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001893 // FIXME: fix handling of complex expressions here.
1894 do {
1895 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001896 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001897 parseLabel();
1898}
1899
1900void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001901 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001902 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001903 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001904 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001905 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001906 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001907 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001908 addUnwrappedLine();
1909 } else {
1910 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001911 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001912 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001913 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001914 }
1915}
1916
1917void UnwrappedLineParser::parseAccessSpecifier() {
1918 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001919 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001920 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001921 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001922 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001923 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001924 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001925 addUnwrappedLine();
1926}
1927
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001928bool UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001929 // Won't be 'enum' for NS_ENUMs.
1930 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001931 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001932
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001933 // In TypeScript, "enum" can also be used as property name, e.g. in interface
1934 // declarations. An "enum" keyword followed by a colon would be a syntax
1935 // error and thus assume it is just an identifier.
Daniel Jasper87379302016-02-03 05:33:44 +00001936 if (Style.Language == FormatStyle::LK_JavaScript &&
1937 FormatTok->isOneOf(tok::colon, tok::question))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001938 return false;
1939
Daniel Jasper2b41a822013-08-20 12:42:50 +00001940 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001941 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1942 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001943
Daniel Jasper786a5502013-09-06 21:32:35 +00001944 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001945 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1946 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001947 nextToken();
1948 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001949 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001950 parseParens();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001951 if (FormatTok->is(tok::identifier)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001952 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001953 // If there are two identifiers in a row, this is likely an elaborate
1954 // return type. In Java, this can be "implements", etc.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001955 if (Style.isCpp() && FormatTok->is(tok::identifier))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001956 return false;
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001957 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001958 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001959
1960 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001961 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001962 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00001963 FormatTok->BlockKind = BK_Block;
1964
1965 if (Style.Language == FormatStyle::LK_Java) {
1966 // Java enums are different.
1967 parseJavaEnumBody();
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001968 return true;
1969 }
1970 if (Style.Language == FormatStyle::LK_Proto) {
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001971 parseBlock(/*MustBeDeclaration=*/true);
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001972 return true;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001973 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001974
1975 // Parse enum body.
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001976 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001977 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1978 if (HasError) {
1979 if (FormatTok->is(tok::semi))
1980 nextToken();
1981 addUnwrappedLine();
1982 }
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001983 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00001984
Daniel Jasper90cf3802015-06-17 09:44:02 +00001985 // There is no addUnwrappedLine() here so that we fall through to parsing a
1986 // structural element afterwards. Thus, in "enum A {} n, m;",
Manuel Klimek2cec0192013-01-21 19:17:52 +00001987 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001988}
1989
1990void UnwrappedLineParser::parseJavaEnumBody() {
1991 // Determine whether the enum is simple, i.e. does not have a semicolon or
1992 // constants with class bodies. Simple enums can be formatted like braced
1993 // lists, contracted to a single line, etc.
1994 unsigned StoredPosition = Tokens->getPosition();
1995 bool IsSimple = true;
1996 FormatToken *Tok = Tokens->getNextToken();
1997 while (Tok) {
1998 if (Tok->is(tok::r_brace))
1999 break;
2000 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
2001 IsSimple = false;
2002 break;
2003 }
2004 // FIXME: This will also mark enums with braces in the arguments to enum
2005 // constants as "not simple". This is probably fine in practice, though.
2006 Tok = Tokens->getNextToken();
2007 }
2008 FormatTok = Tokens->setPosition(StoredPosition);
2009
2010 if (IsSimple) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002011 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002012 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00002013 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002014 return;
2015 }
2016
2017 // Parse the body of a more complex enum.
2018 // First add a line for everything up to the "{".
2019 nextToken();
2020 addUnwrappedLine();
2021 ++Line->Level;
2022
2023 // Parse the enum constants.
2024 while (FormatTok) {
2025 if (FormatTok->is(tok::l_brace)) {
2026 // Parse the constant's class body.
2027 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2028 /*MunchSemi=*/false);
2029 } else if (FormatTok->is(tok::l_paren)) {
2030 parseParens();
2031 } else if (FormatTok->is(tok::comma)) {
2032 nextToken();
2033 addUnwrappedLine();
2034 } else if (FormatTok->is(tok::semi)) {
2035 nextToken();
2036 addUnwrappedLine();
2037 break;
2038 } else if (FormatTok->is(tok::r_brace)) {
2039 addUnwrappedLine();
2040 break;
2041 } else {
2042 nextToken();
2043 }
2044 }
2045
2046 // Parse the class body after the enum's ";" if any.
2047 parseLevel(/*HasOpeningBrace=*/true);
2048 nextToken();
2049 --Line->Level;
2050 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00002051}
2052
Martin Probst1027fb82017-02-07 14:05:30 +00002053void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00002054 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00002055 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00002056
Daniel Jasper04785d02015-05-06 14:03:02 +00002057 // The actual identifier can be a nested name specifier, and in macros
2058 // it is often token-pasted.
2059 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2060 tok::kw___attribute, tok::kw___declspec,
2061 tok::kw_alignas) ||
2062 ((Style.Language == FormatStyle::LK_Java ||
2063 Style.Language == FormatStyle::LK_JavaScript) &&
2064 FormatTok->isOneOf(tok::period, tok::comma))) {
Martin Probstcb870c52017-08-01 15:46:10 +00002065 if (Style.Language == FormatStyle::LK_JavaScript &&
2066 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
2067 // JavaScript/TypeScript supports inline object types in
2068 // extends/implements positions:
2069 // class Foo implements {bar: number} { }
2070 nextToken();
2071 if (FormatTok->is(tok::l_brace)) {
2072 tryToParseBracedList();
2073 continue;
2074 }
2075 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002076 bool IsNonMacroIdentifier =
2077 FormatTok->is(tok::identifier) &&
2078 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002079 nextToken();
2080 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00002081 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00002082 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00002083 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00002084
Daniel Jasper04785d02015-05-06 14:03:02 +00002085 // Note that parsing away template declarations here leads to incorrectly
2086 // accepting function declarations as record declarations.
2087 // In general, we cannot solve this problem. Consider:
2088 // class A<int> B() {}
2089 // which can be a function definition or a class definition when B() is a
2090 // macro. If we find enough real-world cases where this is a problem, we
2091 // can parse for the 'template' keyword in the beginning of the statement,
2092 // and thus rule out the record production in case there is no template
2093 // (this would still leave us with an ambiguity between template function
2094 // and class declarations).
Daniel Jasperadba2aa2015-05-18 12:52:00 +00002095 if (FormatTok->isOneOf(tok::colon, tok::less)) {
2096 while (!eof()) {
Daniel Jasper3c883d12015-05-18 14:49:19 +00002097 if (FormatTok->is(tok::l_brace)) {
2098 calculateBraceTypes(/*ExpectClassBody=*/true);
2099 if (!tryToParseBracedList())
2100 break;
2101 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002102 if (FormatTok->Tok.is(tok::semi))
2103 return;
2104 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002105 }
2106 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002107 if (FormatTok->Tok.is(tok::l_brace)) {
Martin Probst1027fb82017-02-07 14:05:30 +00002108 if (ParseAsExpr) {
2109 parseChildBlock();
2110 } else {
2111 if (ShouldBreakBeforeBrace(Style, InitialToken))
2112 addUnwrappedLine();
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002113
Martin Probst1027fb82017-02-07 14:05:30 +00002114 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2115 /*MunchSemi=*/false);
2116 }
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002117 }
Daniel Jasper90cf3802015-06-17 09:44:02 +00002118 // There is no addUnwrappedLine() here so that we fall through to parsing a
2119 // structural element afterwards. Thus, in "class A {} n, m;",
2120 // "} n, m;" will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00002121}
2122
Nico Weber8696a8d2013-01-09 21:15:03 +00002123void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002124 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Ben Hamilton1462e842018-04-05 15:26:25 +00002125 do {
Nico Weber8696a8d2013-01-09 21:15:03 +00002126 nextToken();
Ben Hamilton1462e842018-04-05 15:26:25 +00002127 // Early exit in case someone forgot a close angle.
2128 if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2129 FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2130 return;
2131 } while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00002132 nextToken(); // Skip '>'.
2133}
2134
2135void UnwrappedLineParser::parseObjCUntilAtEnd() {
2136 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002137 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002138 nextToken();
2139 addUnwrappedLine();
2140 break;
2141 }
Daniel Jaspera15da302013-08-28 08:04:23 +00002142 if (FormatTok->is(tok::l_brace)) {
2143 parseBlock(/*MustBeDeclaration=*/false);
2144 // In ObjC interfaces, nothing should be following the "}".
2145 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00002146 } else if (FormatTok->is(tok::r_brace)) {
2147 // Ignore stray "}". parseStructuralElement doesn't consume them.
2148 nextToken();
2149 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00002150 } else {
2151 parseStructuralElement();
2152 }
Nico Weber8696a8d2013-01-09 21:15:03 +00002153 } while (!eof());
2154}
2155
Nico Weber2ce0ac52013-01-09 23:25:37 +00002156void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weberc068ff72018-01-23 17:10:25 +00002157 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
2158 FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
Nico Weber7eecf4b2013-01-09 20:25:35 +00002159 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002160 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00002161
Ben Hamilton1462e842018-04-05 15:26:25 +00002162 // @interface can be followed by a lightweight generic
2163 // specialization list, then either a base class or a category.
2164 if (FormatTok->Tok.is(tok::less)) {
2165 // Unlike protocol lists, generic parameterizations support
2166 // nested angles:
2167 //
2168 // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
2169 // NSObject <NSCopying, NSSecureCoding>
2170 //
2171 // so we need to count how many open angles we have left.
2172 unsigned NumOpenAngles = 1;
2173 do {
2174 nextToken();
2175 // Early exit in case someone forgot a close angle.
2176 if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2177 FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2178 break;
2179 if (FormatTok->Tok.is(tok::less))
2180 ++NumOpenAngles;
2181 else if (FormatTok->Tok.is(tok::greater)) {
2182 assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
2183 --NumOpenAngles;
2184 }
2185 } while (!eof() && NumOpenAngles != 0);
2186 nextToken(); // Skip '>'.
2187 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002188 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00002189 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002190 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002191 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00002192 // Skip category, if present.
2193 parseParens();
2194
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002195 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002196 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00002197
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002198 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00002199 if (Style.BraceWrapping.AfterObjCDeclaration)
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002200 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00002201 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002202 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00002203
2204 // With instance variables, this puts '}' on its own line. Without instance
2205 // variables, this ends the @interface line.
2206 addUnwrappedLine();
2207
Nico Weber8696a8d2013-01-09 21:15:03 +00002208 parseObjCUntilAtEnd();
2209}
Nico Weber7eecf4b2013-01-09 20:25:35 +00002210
Nico Weberc068ff72018-01-23 17:10:25 +00002211// Returns true for the declaration/definition form of @protocol,
2212// false for the expression form.
2213bool UnwrappedLineParser::parseObjCProtocol() {
2214 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
Nico Weber8696a8d2013-01-09 21:15:03 +00002215 nextToken();
Nico Weberc068ff72018-01-23 17:10:25 +00002216
2217 if (FormatTok->is(tok::l_paren))
2218 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
2219 return false;
2220
2221 // The definition/declaration form,
2222 // @protocol Foo
2223 // - (int)someMethod;
2224 // @end
2225
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002226 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00002227
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002228 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002229 parseObjCProtocolList();
2230
2231 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002232 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002233 nextToken();
Nico Weberc068ff72018-01-23 17:10:25 +00002234 addUnwrappedLine();
2235 return true;
Nico Weber8696a8d2013-01-09 21:15:03 +00002236 }
2237
2238 addUnwrappedLine();
2239 parseObjCUntilAtEnd();
Nico Weberc068ff72018-01-23 17:10:25 +00002240 return true;
Nico Weber7eecf4b2013-01-09 20:25:35 +00002241}
2242
Daniel Jasperfca735c2015-02-19 16:14:18 +00002243void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
Martin Probst053f1aa2016-04-19 14:55:37 +00002244 bool IsImport = FormatTok->is(Keywords.kw_import);
2245 assert(IsImport || FormatTok->is(tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00002246 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00002247
Daniel Jasperec05fc72015-05-11 09:14:50 +00002248 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002249 if (FormatTok->is(tok::kw_default))
2250 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00002251
Martin Probst5f8445b2016-04-24 22:05:09 +00002252 // Consume "async function", "function" and "default function", so that these
2253 // get parsed as free-standing JS functions, i.e. do not require a trailing
2254 // semicolon.
2255 if (FormatTok->is(Keywords.kw_async))
2256 nextToken();
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002257 if (FormatTok->is(Keywords.kw_function)) {
2258 nextToken();
2259 return;
2260 }
2261
Martin Probst053f1aa2016-04-19 14:55:37 +00002262 // For imports, `export *`, `export {...}`, consume the rest of the line up
2263 // to the terminating `;`. For everything else, just return and continue
2264 // parsing the structural element, i.e. the declaration or expression for
2265 // `export default`.
2266 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
2267 !FormatTok->isStringLiteral())
2268 return;
Daniel Jasperfca735c2015-02-19 16:14:18 +00002269
Martin Probstd40bca42017-01-09 08:56:36 +00002270 while (!eof()) {
2271 if (FormatTok->is(tok::semi))
2272 return;
Krasimir Georgiev112c2e92017-11-09 13:22:03 +00002273 if (Line->Tokens.empty()) {
Martin Probstd40bca42017-01-09 08:56:36 +00002274 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
2275 // import statement should terminate.
2276 return;
2277 }
Daniel Jasperefc1a832016-01-07 08:53:35 +00002278 if (FormatTok->is(tok::l_brace)) {
2279 FormatTok->BlockKind = BK_Block;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002280 nextToken();
Daniel Jasperefc1a832016-01-07 08:53:35 +00002281 parseBracedList();
2282 } else {
2283 nextToken();
2284 }
Daniel Jasper354aa512015-02-19 16:07:32 +00002285 }
2286}
2287
Daniel Jasper3b203a62013-09-05 16:05:56 +00002288LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
2289 StringRef Prefix = "") {
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002290 llvm::dbgs() << Prefix << "Line(" << Line.Level
2291 << ", FSC=" << Line.FirstStartColumn << ")"
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002292 << (Line.InPPDirective ? " MACRO" : "") << ": ";
2293 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2294 E = Line.Tokens.end();
2295 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002296 llvm::dbgs() << I->Tok->Tok.getName() << "["
Manuel Klimek89628f62017-09-20 09:51:03 +00002297 << "T=" << I->Tok->Type << ", OC=" << I->Tok->OriginalColumn
2298 << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002299 }
2300 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2301 E = Line.Tokens.end();
2302 I != E; ++I) {
2303 const UnwrappedLineNode &Node = *I;
2304 for (SmallVectorImpl<UnwrappedLine>::const_iterator
2305 I = Node.Children.begin(),
2306 E = Node.Children.end();
2307 I != E; ++I) {
2308 printDebugInfo(*I, "\nChild: ");
2309 }
2310 }
2311 llvm::dbgs() << "\n";
2312}
2313
Daniel Jasperf7935112012-12-03 18:12:45 +00002314void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002315 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00002316 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00002317 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002318 if (CurrentLines == &Lines)
2319 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00002320 });
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002321 CurrentLines->push_back(std::move(*Line));
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002322 Line->Tokens.clear();
Krasimir Georgiev85c37042017-03-01 16:38:08 +00002323 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002324 Line->FirstStartColumn = 0;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002325 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002326 CurrentLines->append(
2327 std::make_move_iterator(PreprocessorDirectives.begin()),
2328 std::make_move_iterator(PreprocessorDirectives.end()));
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002329 PreprocessorDirectives.clear();
2330 }
Manuel Klimeke411aa82017-09-20 09:29:37 +00002331 // Disconnect the current token from the last token on the previous line.
2332 FormatTok->Previous = nullptr;
Daniel Jasperf7935112012-12-03 18:12:45 +00002333}
2334
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002335bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00002336
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002337bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002338 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
2339 FormatTok.NewlinesBefore > 0;
2340}
2341
Krasimir Georgiev91834222017-01-25 13:58:58 +00002342// Checks if \p FormatTok is a line comment that continues the line comment
2343// section on \p Line.
Krasimir Georgievea222a72017-05-22 10:07:56 +00002344static bool continuesLineCommentSection(const FormatToken &FormatTok,
2345 const UnwrappedLine &Line,
2346 llvm::Regex &CommentPragmasRegex) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002347 if (Line.Tokens.empty())
2348 return false;
Krasimir Georgiev84321612017-01-30 19:18:55 +00002349
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002350 StringRef IndentContent = FormatTok.TokenText;
2351 if (FormatTok.TokenText.startswith("//") ||
2352 FormatTok.TokenText.startswith("/*"))
2353 IndentContent = FormatTok.TokenText.substr(2);
2354 if (CommentPragmasRegex.match(IndentContent))
2355 return false;
2356
Krasimir Georgiev91834222017-01-25 13:58:58 +00002357 // If Line starts with a line comment, then FormatTok continues the comment
Krasimir Georgiev84321612017-01-30 19:18:55 +00002358 // section if its original column is greater or equal to the original start
Krasimir Georgiev91834222017-01-25 13:58:58 +00002359 // column of the line.
2360 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002361 // Define the min column token of a line as follows: if a line ends in '{' or
2362 // contains a '{' followed by a line comment, then the min column token is
2363 // that '{'. Otherwise, the min column token of the line is the first token of
2364 // the line.
2365 //
2366 // If Line starts with a token other than a line comment, then FormatTok
2367 // continues the comment section if its original column is greater than the
2368 // original start column of the min column token of the line.
Krasimir Georgiev91834222017-01-25 13:58:58 +00002369 //
2370 // For example, the second line comment continues the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002371 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002372 // // first line
2373 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002374 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002375 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002376 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002377 // // first line
2378 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002379 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002380 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002381 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002382 // int i; // first line
2383 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002384 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002385 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002386 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002387 // do { // first line
2388 // // second line
2389 // int i;
2390 // } while (true);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002391 //
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002392 // and:
2393 //
2394 // enum {
2395 // a, // first line
2396 // // second line
2397 // b
2398 // };
2399 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002400 // The second line comment doesn't continue the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002401 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002402 // // first line
2403 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002404 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002405 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002406 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002407 // int i; // first line
2408 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002409 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002410 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002411 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002412 // do { // first line
2413 // // second line
2414 // int i;
2415 // } while (true);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002416 //
2417 // and:
2418 //
2419 // enum {
2420 // a, // first line
2421 // // second line
2422 // };
Krasimir Georgiev84321612017-01-30 19:18:55 +00002423 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
2424
2425 // Scan for '{//'. If found, use the column of '{' as a min column for line
2426 // comment section continuation.
2427 const FormatToken *PreviousToken = nullptr;
Krasimir Georgievd86c25d2017-03-10 13:09:29 +00002428 for (const UnwrappedLineNode &Node : Line.Tokens) {
Krasimir Georgiev84321612017-01-30 19:18:55 +00002429 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
2430 isLineComment(*Node.Tok)) {
2431 MinColumnToken = PreviousToken;
2432 break;
2433 }
2434 PreviousToken = Node.Tok;
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002435
2436 // Grab the last newline preceding a token in this unwrapped line.
2437 if (Node.Tok->NewlinesBefore > 0) {
2438 MinColumnToken = Node.Tok;
2439 }
Krasimir Georgiev84321612017-01-30 19:18:55 +00002440 }
2441 if (PreviousToken && PreviousToken->is(tok::l_brace)) {
2442 MinColumnToken = PreviousToken;
2443 }
2444
Krasimir Georgievea222a72017-05-22 10:07:56 +00002445 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
2446 MinColumnToken);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002447}
2448
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002449void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
2450 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002451 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002452 I = CommentsBeforeNextToken.begin(),
2453 E = CommentsBeforeNextToken.end();
2454 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002455 // Line comments that belong to the same line comment section are put on the
2456 // same line since later we might want to reflow content between them.
Krasimir Georgiev753625b2017-01-31 13:32:38 +00002457 // Additional fine-grained breaking of line comment sections is controlled
2458 // by the class BreakableLineCommentSection in case it is desirable to keep
2459 // several line comment sections in the same unwrapped line.
2460 //
2461 // FIXME: Consider putting separate line comment sections as children to the
2462 // unwrapped line instead.
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002463 (*I)->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002464 continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002465 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002466 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002467 pushToken(*I);
2468 }
Daniel Jaspere60cba12015-05-13 11:35:53 +00002469 if (NewlineBeforeNext && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002470 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002471 CommentsBeforeNextToken.clear();
2472}
2473
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002474void UnwrappedLineParser::nextToken(int LevelDifference) {
Daniel Jasperf7935112012-12-03 18:12:45 +00002475 if (eof())
2476 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002477 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002478 pushToken(FormatTok);
Manuel Klimek89628f62017-09-20 09:51:03 +00002479 FormatToken *Previous = FormatTok;
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002480 if (Style.Language != FormatStyle::LK_JavaScript)
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002481 readToken(LevelDifference);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002482 else
2483 readTokenWithJavaScriptASI();
Manuel Klimeke411aa82017-09-20 09:29:37 +00002484 FormatTok->Previous = Previous;
Daniel Jasperb9a49902016-01-09 15:56:28 +00002485}
2486
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002487void UnwrappedLineParser::distributeComments(
2488 const SmallVectorImpl<FormatToken *> &Comments,
2489 const FormatToken *NextTok) {
2490 // Whether or not a line comment token continues a line is controlled by
Krasimir Georgievea222a72017-05-22 10:07:56 +00002491 // the method continuesLineCommentSection, with the following caveat:
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002492 //
2493 // Define a trail of Comments to be a nonempty proper postfix of Comments such
2494 // that each comment line from the trail is aligned with the next token, if
2495 // the next token exists. If a trail exists, the beginning of the maximal
2496 // trail is marked as a start of a new comment section.
2497 //
2498 // For example in this code:
2499 //
2500 // int a; // line about a
2501 // // line 1 about b
2502 // // line 2 about b
2503 // int b;
2504 //
2505 // the two lines about b form a maximal trail, so there are two sections, the
2506 // first one consisting of the single comment "// line about a" and the
2507 // second one consisting of the next two comments.
2508 if (Comments.empty())
2509 return;
2510 bool ShouldPushCommentsInCurrentLine = true;
2511 bool HasTrailAlignedWithNextToken = false;
2512 unsigned StartOfTrailAlignedWithNextToken = 0;
2513 if (NextTok) {
2514 // We are skipping the first element intentionally.
2515 for (unsigned i = Comments.size() - 1; i > 0; --i) {
2516 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
2517 HasTrailAlignedWithNextToken = true;
2518 StartOfTrailAlignedWithNextToken = i;
2519 }
2520 }
2521 }
2522 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
2523 FormatToken *FormatTok = Comments[i];
Manuel Klimek89628f62017-09-20 09:51:03 +00002524 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002525 FormatTok->ContinuesLineCommentSection = false;
2526 } else {
2527 FormatTok->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002528 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002529 }
2530 if (!FormatTok->ContinuesLineCommentSection &&
2531 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
2532 ShouldPushCommentsInCurrentLine = false;
2533 }
2534 if (ShouldPushCommentsInCurrentLine) {
2535 pushToken(FormatTok);
2536 } else {
2537 CommentsBeforeNextToken.push_back(FormatTok);
2538 }
2539 }
2540}
2541
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002542void UnwrappedLineParser::readToken(int LevelDifference) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002543 SmallVector<FormatToken *, 1> Comments;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002544 do {
2545 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00002546 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002547 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
2548 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002549 distributeComments(Comments, FormatTok);
2550 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002551 // If there is an unfinished unwrapped line, we flush the preprocessor
2552 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00002553 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002554 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002555 assert((LevelDifference >= 0 ||
2556 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2557 "LevelDifference makes Line->Level negative");
2558 Line->Level += LevelDifference;
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00002559 // Comments stored before the preprocessor directive need to be output
2560 // before the preprocessor directive, at the same level as the
2561 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002562 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002563 parsePPDirective();
2564 }
Manuel Klimek68b03042014-04-14 09:14:11 +00002565 while (FormatTok->Type == TT_ConflictStart ||
2566 FormatTok->Type == TT_ConflictEnd ||
2567 FormatTok->Type == TT_ConflictAlternative) {
2568 if (FormatTok->Type == TT_ConflictStart) {
2569 conditionalCompilationStart(/*Unreachable=*/false);
2570 } else if (FormatTok->Type == TT_ConflictAlternative) {
2571 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002572 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00002573 conditionalCompilationEnd();
2574 }
2575 FormatTok = Tokens->getNextToken();
2576 FormatTok->MustBreakBefore = true;
2577 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002578
Francois Ferranda98a95c2017-07-28 07:56:14 +00002579 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002580 !Line->InPPDirective) {
2581 continue;
2582 }
2583
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002584 if (!FormatTok->Tok.is(tok::comment)) {
2585 distributeComments(Comments, FormatTok);
2586 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002587 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002588 }
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002589
2590 Comments.push_back(FormatTok);
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002591 } while (!eof());
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002592
2593 distributeComments(Comments, nullptr);
2594 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002595}
2596
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002597void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002598 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002599 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002600 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002601 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00002602 }
Daniel Jasperf7935112012-12-03 18:12:45 +00002603}
2604
Daniel Jasper8d1832e2013-01-07 13:26:07 +00002605} // end namespace format
2606} // end namespace clang