blob: 80bfca644c9487daa20a5a4dd4768709eca44622 [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Jasperf7935112012-12-03 18:12:45 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// This file contains the implementation of the UnwrappedLineParser,
Daniel Jasperf7935112012-12-03 18:12:45 +000011/// which turns a stream of tokens into UnwrappedLines.
12///
Daniel Jasperf7935112012-12-03 18:12:45 +000013//===----------------------------------------------------------------------===//
14
Chandler Carruth4b417452013-01-19 08:09:44 +000015#include "UnwrappedLineParser.h"
Benjamin Kramer33335df2015-03-01 21:36:40 +000016#include "llvm/ADT/STLExtras.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000017#include "llvm/Support/Debug.h"
Benjamin Kramer53f5e892015-03-23 18:05:43 +000018#include "llvm/Support/raw_ostream.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000019
Martin Probst7e0f25b2017-11-25 09:19:42 +000020#include <algorithm>
21
Chandler Carruth10346662014-04-22 03:17:02 +000022#define DEBUG_TYPE "format-parser"
23
Daniel Jasperf7935112012-12-03 18:12:45 +000024namespace clang {
25namespace format {
26
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000027class FormatTokenSource {
28public:
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000029 virtual ~FormatTokenSource() {}
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000030 virtual FormatToken *getNextToken() = 0;
31
32 virtual unsigned getPosition() = 0;
33 virtual FormatToken *setPosition(unsigned Position) = 0;
34};
35
Craig Topper69665e12013-07-01 04:21:54 +000036namespace {
37
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000038class ScopedDeclarationState {
39public:
40 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
41 bool MustBeDeclaration)
42 : Line(Line), Stack(Stack) {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000043 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek39080572013-01-23 11:03:04 +000044 Stack.push_back(MustBeDeclaration);
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000045 }
46 ~ScopedDeclarationState() {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000047 Stack.pop_back();
Manuel Klimekc1237a82013-01-23 14:08:21 +000048 if (!Stack.empty())
49 Line.MustBeDeclaration = Stack.back();
50 else
51 Line.MustBeDeclaration = true;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000052 }
Daniel Jasper393564f2013-05-31 14:56:29 +000053
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000054private:
55 UnwrappedLine &Line;
56 std::vector<bool> &Stack;
57};
58
Krasimir Georgieva1c30932017-05-19 10:34:57 +000059static bool isLineComment(const FormatToken &FormatTok) {
Krasimir Georgiev410ed242017-11-10 12:50:09 +000060 return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith("/*");
Krasimir Georgieva1c30932017-05-19 10:34:57 +000061}
62
Krasimir Georgievea222a72017-05-22 10:07:56 +000063// Checks if \p FormatTok is a line comment that continues the line comment
64// \p Previous. The original column of \p MinColumnToken is used to determine
65// whether \p FormatTok is indented enough to the right to continue \p Previous.
66static bool continuesLineComment(const FormatToken &FormatTok,
67 const FormatToken *Previous,
68 const FormatToken *MinColumnToken) {
69 if (!Previous || !MinColumnToken)
70 return false;
71 unsigned MinContinueColumn =
72 MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
73 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
74 isLineComment(*Previous) &&
75 FormatTok.OriginalColumn >= MinContinueColumn;
76}
77
Manuel Klimek1abf7892013-01-04 23:34:14 +000078class ScopedMacroState : public FormatTokenSource {
79public:
80 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek20e0af62015-05-06 11:56:29 +000081 FormatToken *&ResetToken)
Manuel Klimek1abf7892013-01-04 23:34:14 +000082 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000083 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
Krasimir Georgieva1c30932017-05-19 10:34:57 +000084 Token(nullptr), PreviousToken(nullptr) {
David L. Jones5de22722018-06-15 06:08:54 +000085 FakeEOF.Tok.startToken();
86 FakeEOF.Tok.setKind(tok::eof);
Manuel Klimek1abf7892013-01-04 23:34:14 +000087 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000088 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000089 Line.InPPDirective = true;
90 }
91
Alexander Kornienko34eb2072015-04-11 02:00:23 +000092 ~ScopedMacroState() override {
Manuel Klimek1abf7892013-01-04 23:34:14 +000093 TokenSource = PreviousTokenSource;
94 ResetToken = Token;
95 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000096 Line.Level = PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +000097 }
98
Craig Topperfb6b25b2014-03-15 04:29:04 +000099 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +0000100 // The \c UnwrappedLineParser guards against this by never calling
101 // \c getNextToken() after it has encountered the first eof token.
102 assert(!eof());
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000103 PreviousToken = Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000104 Token = PreviousTokenSource->getNextToken();
105 if (eof())
David L. Jones5de22722018-06-15 06:08:54 +0000106 return &FakeEOF;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000107 return Token;
108 }
109
Craig Topperfb6b25b2014-03-15 04:29:04 +0000110 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +0000111
Craig Topperfb6b25b2014-03-15 04:29:04 +0000112 FormatToken *setPosition(unsigned Position) override {
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000113 PreviousToken = nullptr;
Manuel Klimekab419912013-05-23 09:41:43 +0000114 Token = PreviousTokenSource->setPosition(Position);
115 return Token;
116 }
117
Manuel Klimek1abf7892013-01-04 23:34:14 +0000118private:
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000119 bool eof() {
120 return Token && Token->HasUnescapedNewline &&
Krasimir Georgievea222a72017-05-22 10:07:56 +0000121 !continuesLineComment(*Token, PreviousToken,
122 /*MinColumnToken=*/PreviousToken);
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000123 }
Manuel Klimek1abf7892013-01-04 23:34:14 +0000124
David L. Jones5de22722018-06-15 06:08:54 +0000125 FormatToken FakeEOF;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000126 UnwrappedLine &Line;
127 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000128 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000129 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000130 FormatTokenSource *PreviousTokenSource;
131
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000132 FormatToken *Token;
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000133 FormatToken *PreviousToken;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000134};
135
Craig Topper69665e12013-07-01 04:21:54 +0000136} // end anonymous namespace
137
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000138class ScopedLineState {
139public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000140 ScopedLineState(UnwrappedLineParser &Parser,
141 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000142 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000143 if (SwitchToPreprocessorLines)
144 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000145 else if (!Parser.Line->Tokens.empty())
146 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000147 PreBlockLine = std::move(Parser.Line);
148 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000149 Parser.Line->Level = PreBlockLine->Level;
150 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000151 }
152
153 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000154 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000155 Parser.addUnwrappedLine();
156 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000157 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000158 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000159 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
160 Parser.MustBreakBeforeNextToken = true;
161 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000162 }
163
164private:
165 UnwrappedLineParser &Parser;
166
David Blaikieefb6eb22014-08-09 20:02:07 +0000167 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000168 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000169};
170
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000171class CompoundStatementIndenter {
172public:
173 CompoundStatementIndenter(UnwrappedLineParser *Parser,
174 const FormatStyle &Style, unsigned &LineLevel)
175 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000176 if (Style.BraceWrapping.AfterControlStatement)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000177 Parser->addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000178 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000179 ++LineLevel;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000180 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000181 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000182
183private:
184 unsigned &LineLevel;
185 unsigned OldLineLevel;
186};
187
Craig Topper69665e12013-07-01 04:21:54 +0000188namespace {
189
Manuel Klimekab419912013-05-23 09:41:43 +0000190class IndexedTokenSource : public FormatTokenSource {
191public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000192 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000193 : Tokens(Tokens), Position(-1) {}
194
Craig Topperfb6b25b2014-03-15 04:29:04 +0000195 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000196 ++Position;
197 return Tokens[Position];
198 }
199
Craig Topperfb6b25b2014-03-15 04:29:04 +0000200 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000201 assert(Position >= 0);
202 return Position;
203 }
204
Craig Topperfb6b25b2014-03-15 04:29:04 +0000205 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000206 Position = P;
207 return Tokens[Position];
208 }
209
Manuel Klimek71814b42013-10-11 21:25:45 +0000210 void reset() { Position = -1; }
211
Manuel Klimekab419912013-05-23 09:41:43 +0000212private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000213 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000214 int Position;
215};
216
Craig Topper69665e12013-07-01 04:21:54 +0000217} // end anonymous namespace
218
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000219UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000220 const AdditionalKeywords &Keywords,
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000221 unsigned FirstStartColumn,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000222 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000223 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000224 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000225 CurrentLines(&Lines), Style(Style), Keywords(Keywords),
226 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
Krasimir Georgievad47c902017-08-30 14:34:57 +0000227 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000228 IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
229 ? IG_Rejected
230 : IG_Inited),
231 IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000232
233void UnwrappedLineParser::reset() {
234 PPBranchLevel = -1;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000235 IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None
236 ? IG_Rejected
237 : IG_Inited;
238 IncludeGuardToken = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000239 Line.reset(new UnwrappedLine);
240 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000241 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000242 MustBreakBeforeNextToken = false;
243 PreprocessorDirectives.clear();
244 CurrentLines = &Lines;
245 DeclarationScopeStack.clear();
Manuel Klimek71814b42013-10-11 21:25:45 +0000246 PPStack.clear();
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000247 Line->FirstStartColumn = FirstStartColumn;
Manuel Klimek71814b42013-10-11 21:25:45 +0000248}
Daniel Jasperf7935112012-12-03 18:12:45 +0000249
Manuel Klimek20e0af62015-05-06 11:56:29 +0000250void UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000251 IndexedTokenSource TokenSource(AllTokens);
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000252 Line->FirstStartColumn = FirstStartColumn;
Manuel Klimek71814b42013-10-11 21:25:45 +0000253 do {
Nicola Zaghen3538b392018-05-15 13:30:56 +0000254 LLVM_DEBUG(llvm::dbgs() << "----\n");
Manuel Klimek71814b42013-10-11 21:25:45 +0000255 reset();
256 Tokens = &TokenSource;
257 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000258
Manuel Klimek71814b42013-10-11 21:25:45 +0000259 readToken();
260 parseFile();
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000261
262 // If we found an include guard then all preprocessor directives (other than
263 // the guard) are over-indented by one.
264 if (IncludeGuard == IG_Found)
265 for (auto &Line : Lines)
266 if (Line.InPPDirective && Line.Level > 0)
267 --Line.Level;
268
Manuel Klimek71814b42013-10-11 21:25:45 +0000269 // Create line with eof token.
270 pushToken(FormatTok);
271 addUnwrappedLine();
272
273 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
274 E = Lines.end();
275 I != E; ++I) {
276 Callback.consumeUnwrappedLine(*I);
277 }
278 Callback.finishRun();
279 Lines.clear();
280 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000281 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000282 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
283 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
284 }
285 if (!PPLevelBranchIndex.empty()) {
286 ++PPLevelBranchIndex.back();
287 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
288 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
289 }
290 } while (!PPLevelBranchIndex.empty());
Manuel Klimek1abf7892013-01-04 23:34:14 +0000291}
292
Manuel Klimek1a18c402013-04-12 14:13:36 +0000293void UnwrappedLineParser::parseFile() {
Daniel Jasper9326f912015-05-05 08:40:32 +0000294 // The top-level context in a file always has declarations, except for pre-
295 // processor directives and JavaScript files.
296 bool MustBeDeclaration =
297 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
298 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
299 MustBeDeclaration);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000300 if (Style.Language == FormatStyle::LK_TextProto)
301 parseBracedList();
302 else
303 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000304 // Make sure to format the remaining tokens.
Krasimir Georgiev0895f5e2018-06-25 11:08:24 +0000305 //
306 // LK_TextProto is special since its top-level is parsed as the body of a
307 // braced list, which does not necessarily have natural line separators such
308 // as a semicolon. Comments after the last entry that have been determined to
309 // not belong to that line, as in:
310 // key: value
311 // // endfile comment
312 // do not have a chance to be put on a line of their own until this point.
313 // Here we add this newline before end-of-file comments.
314 if (Style.Language == FormatStyle::LK_TextProto &&
315 !CommentsBeforeNextToken.empty())
316 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000317 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000318 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000319}
320
Manuel Klimek1a18c402013-04-12 14:13:36 +0000321void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000322 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000323 do {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000324 tok::TokenKind kind = FormatTok->Tok.getKind();
325 if (FormatTok->Type == TT_MacroBlockBegin) {
326 kind = tok::l_brace;
327 } else if (FormatTok->Type == TT_MacroBlockEnd) {
328 kind = tok::r_brace;
329 }
330
331 switch (kind) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000332 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000333 nextToken();
334 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000335 break;
336 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000337 // FIXME: Add parameter whether this can happen - if this happens, we must
338 // be in a non-declaration context.
Daniel Jasperb86e2722015-08-24 13:23:37 +0000339 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
340 continue;
Nico Weber9096fc02013-06-26 00:30:14 +0000341 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000342 addUnwrappedLine();
343 break;
344 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000345 if (HasOpeningBrace)
346 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000347 nextToken();
348 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000349 break;
Nico Weberc29f83b2018-01-23 16:30:56 +0000350 case tok::kw_default: {
351 unsigned StoredPosition = Tokens->getPosition();
Jonas Toth90d2aa22018-08-24 17:25:06 +0000352 FormatToken *Next;
353 do {
354 Next = Tokens->getNextToken();
355 } while (Next && Next->is(tok::comment));
Nico Weberc29f83b2018-01-23 16:30:56 +0000356 FormatTok = Tokens->setPosition(StoredPosition);
357 if (Next && Next->isNot(tok::colon)) {
358 // default not followed by ':' is not a case label; treat it like
359 // an identifier.
360 parseStructuralElement();
361 break;
362 }
363 // Else, if it is 'default:', fall through to the case handling.
Nico Weberf1add5e2018-01-24 01:47:22 +0000364 LLVM_FALLTHROUGH;
Nico Weberc29f83b2018-01-23 16:30:56 +0000365 }
Daniel Jasper516d7972013-07-25 11:31:57 +0000366 case tok::kw_case:
Manuel Klimek89628f62017-09-20 09:51:03 +0000367 if (Style.Language == FormatStyle::LK_JavaScript &&
368 Line->MustBeDeclaration) {
Martin Probstf785fd92017-08-04 17:07:15 +0000369 // A 'case: string' style field declaration.
370 parseStructuralElement();
371 break;
372 }
Daniel Jasper72407622013-09-02 08:26:29 +0000373 if (!SwitchLabelEncountered &&
374 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
375 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000376 SwitchLabelEncountered = true;
377 parseStructuralElement();
378 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000379 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000380 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000381 break;
382 }
383 } while (!eof());
384}
385
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000386void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
Manuel Klimekab419912013-05-23 09:41:43 +0000387 // We'll parse forward through the tokens until we hit
388 // a closing brace or eof - note that getNextToken() will
389 // parse macros, so this will magically work inside macro
390 // definitions, too.
391 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000392 FormatToken *Tok = FormatTok;
Manuel Klimek89628f62017-09-20 09:51:03 +0000393 const FormatToken *PrevTok = Tok->Previous;
Manuel Klimekab419912013-05-23 09:41:43 +0000394 // Keep a stack of positions of lbrace tokens. We will
395 // update information about whether an lbrace starts a
396 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000397 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000398 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000399 do {
Daniel Jaspereb65e912015-12-21 18:31:15 +0000400 // Get next non-comment token.
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000401 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000402 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000403 do {
404 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000405 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000406 } while (NextTok->is(tok::comment));
407
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000408 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000409 case tok::l_brace:
Martin Probst95ed8e72017-05-31 09:29:40 +0000410 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) {
Martin Probste8e27ca2017-11-25 09:33:47 +0000411 if (PrevTok->isOneOf(tok::colon, tok::less))
412 // A ':' indicates this code is in a type, or a braced list
413 // following a label in an object literal ({a: {b: 1}}).
414 // A '<' could be an object used in a comparison, but that is nonsense
415 // code (can never return true), so more likely it is a generic type
416 // argument (`X<{a: string; b: number}>`).
417 // The code below could be confused by semicolons between the
418 // individual members in a type member list, which would normally
419 // trigger BK_Block. In both cases, this must be parsed as an inline
420 // braced init.
Martin Probst95ed8e72017-05-31 09:29:40 +0000421 Tok->BlockKind = BK_BracedInit;
422 else if (PrevTok->is(tok::r_paren))
423 // `) { }` can only occur in function or method declarations in JS.
424 Tok->BlockKind = BK_Block;
425 } else {
Daniel Jasperb9a49902016-01-09 15:56:28 +0000426 Tok->BlockKind = BK_Unknown;
Martin Probst95ed8e72017-05-31 09:29:40 +0000427 }
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000428 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000429 break;
430 case tok::r_brace:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000431 if (LBraceStack.empty())
432 break;
433 if (LBraceStack.back()->BlockKind == BK_Unknown) {
434 bool ProbablyBracedList = false;
435 if (Style.Language == FormatStyle::LK_Proto) {
436 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
437 } else {
438 // Using OriginalColumn to distinguish between ObjC methods and
439 // binary operators is a bit hacky.
440 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
441 NextTok->OriginalColumn == 0;
Daniel Jasper91b032a2014-05-22 12:46:38 +0000442
Daniel Jasperb9a49902016-01-09 15:56:28 +0000443 // If there is a comma, semicolon or right paren after the closing
444 // brace, we assume this is a braced initializer list. Note that
445 // regardless how we mark inner braces here, we will overwrite the
446 // BlockKind later if we parse a braced list (where all blocks
447 // inside are by default braced lists), or when we explicitly detect
448 // blocks (for example while parsing lambdas).
Martin Probst95ed8e72017-05-31 09:29:40 +0000449 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
450 // braced list in JS.
Daniel Jasperb9a49902016-01-09 15:56:28 +0000451 ProbablyBracedList =
Daniel Jasperacffeb82016-03-05 18:34:26 +0000452 (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probste1e12a72016-08-19 14:35:01 +0000453 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
454 Keywords.kw_as)) ||
Martin Probstb7fb2672017-05-10 13:53:29 +0000455 (Style.isCpp() && NextTok->is(tok::l_paren)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000456 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
457 tok::r_paren, tok::r_square, tok::l_brace,
Manuel Klimekd0f3fe52018-04-11 14:51:54 +0000458 tok::ellipsis) ||
Daniel Jaspere4ada022016-12-13 10:05:03 +0000459 (NextTok->is(tok::identifier) &&
460 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000461 (NextTok->is(tok::semi) &&
462 (!ExpectClassBody || LBraceStack.size() != 1)) ||
463 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Manuel Klimekd0f3fe52018-04-11 14:51:54 +0000464 if (NextTok->is(tok::l_square)) {
465 // We can have an array subscript after a braced init
466 // list, but C++11 attributes are expected after blocks.
467 NextTok = Tokens->getNextToken();
468 ++ReadTokens;
469 ProbablyBracedList = NextTok->isNot(tok::l_square);
470 }
Manuel Klimekab419912013-05-23 09:41:43 +0000471 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000472 if (ProbablyBracedList) {
473 Tok->BlockKind = BK_BracedInit;
474 LBraceStack.back()->BlockKind = BK_BracedInit;
475 } else {
476 Tok->BlockKind = BK_Block;
477 LBraceStack.back()->BlockKind = BK_Block;
478 }
Manuel Klimekab419912013-05-23 09:41:43 +0000479 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000480 LBraceStack.pop_back();
Manuel Klimekab419912013-05-23 09:41:43 +0000481 break;
Francois Ferrand6f40e212018-10-02 16:37:51 +0000482 case tok::identifier:
483 if (!Tok->is(TT_StatementMacro))
484 break;
485 LLVM_FALLTHROUGH;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000486 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000487 case tok::semi:
488 case tok::kw_if:
489 case tok::kw_while:
490 case tok::kw_for:
491 case tok::kw_switch:
492 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000493 case tok::kw___try:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000494 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000495 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000496 break;
497 default:
498 break;
499 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000500 PrevTok = Tok;
Manuel Klimekab419912013-05-23 09:41:43 +0000501 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000502 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Daniel Jasperb9a49902016-01-09 15:56:28 +0000503
Manuel Klimekab419912013-05-23 09:41:43 +0000504 // Assume other blocks for all unclosed opening braces.
505 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000506 if (LBraceStack[i]->BlockKind == BK_Unknown)
507 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000508 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000509
Manuel Klimekab419912013-05-23 09:41:43 +0000510 FormatTok = Tokens->setPosition(StoredPosition);
511}
512
Francois Ferranda98a95c2017-07-28 07:56:14 +0000513template <class T>
514static inline void hash_combine(std::size_t &seed, const T &v) {
515 std::hash<T> hasher;
516 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
517}
518
519size_t UnwrappedLineParser::computePPHash() const {
520 size_t h = 0;
521 for (const auto &i : PPStack) {
522 hash_combine(h, size_t(i.Kind));
523 hash_combine(h, i.Line);
524 }
525 return h;
526}
527
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000528void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
529 bool MunchSemi) {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000530 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
531 "'{' or macro block token expected");
532 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
Daniel Jaspereb65e912015-12-21 18:31:15 +0000533 FormatTok->BlockKind = BK_Block;
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000534
Francois Ferranda98a95c2017-07-28 07:56:14 +0000535 size_t PPStartHash = computePPHash();
536
Daniel Jasper516d7972013-07-25 11:31:57 +0000537 unsigned InitialLevel = Line->Level;
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000538 nextToken(/*LevelDifference=*/AddLevel ? 1 : 0);
Daniel Jasperf7935112012-12-03 18:12:45 +0000539
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000540 if (MacroBlock && FormatTok->is(tok::l_paren))
541 parseParens();
542
Francois Ferranda98a95c2017-07-28 07:56:14 +0000543 size_t NbPreprocessorDirectives =
544 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000545 addUnwrappedLine();
Francois Ferranda98a95c2017-07-28 07:56:14 +0000546 size_t OpeningLineIndex =
547 CurrentLines->empty()
548 ? (UnwrappedLine::kInvalidIndex)
549 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
Daniel Jasperf7935112012-12-03 18:12:45 +0000550
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000551 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
552 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000553 if (AddLevel)
554 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000555 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000556
Marianne Mailhot-Sarrasin03137c62016-04-14 14:56:49 +0000557 if (eof())
558 return;
559
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000560 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
561 : !FormatTok->is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000562 Line->Level = InitialLevel;
Daniel Jaspereb65e912015-12-21 18:31:15 +0000563 FormatTok->BlockKind = BK_Block;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000564 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000565 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000566
Francois Ferranda98a95c2017-07-28 07:56:14 +0000567 size_t PPEndHash = computePPHash();
568
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000569 // Munch the closing brace.
570 nextToken(/*LevelDifference=*/AddLevel ? -1 : 0);
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000571
572 if (MacroBlock && FormatTok->is(tok::l_paren))
573 parseParens();
574
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000575 if (MunchSemi && FormatTok->Tok.is(tok::semi))
576 nextToken();
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000577 Line->Level = InitialLevel;
Francois Ferranda98a95c2017-07-28 07:56:14 +0000578
579 if (PPStartHash == PPEndHash) {
580 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
581 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
582 // Update the opening line to add the forward reference as well
Manuel Klimek0dddcf72018-04-23 09:34:26 +0000583 (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
Francois Ferranda98a95c2017-07-28 07:56:14 +0000584 CurrentLines->size() - 1;
585 }
Francois Ferrande56a8292017-06-14 12:29:47 +0000586 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000587}
588
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000589static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000590 // FIXME: Closure-library specific stuff should not be hard-coded but be
591 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000592 if (Line.Tokens.size() < 4)
593 return false;
594 auto I = Line.Tokens.begin();
595 if (I->Tok->TokenText != "goog")
596 return false;
597 ++I;
598 if (I->Tok->isNot(tok::period))
599 return false;
600 ++I;
601 if (I->Tok->TokenText != "scope")
602 return false;
603 ++I;
604 return I->Tok->is(tok::l_paren);
605}
606
Martin Probst101ec892017-05-09 20:04:09 +0000607static bool isIIFE(const UnwrappedLine &Line,
608 const AdditionalKeywords &Keywords) {
609 // Look for the start of an immediately invoked anonymous function.
610 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
611 // This is commonly done in JavaScript to create a new, anonymous scope.
612 // Example: (function() { ... })()
613 if (Line.Tokens.size() < 3)
614 return false;
615 auto I = Line.Tokens.begin();
616 if (I->Tok->isNot(tok::l_paren))
617 return false;
618 ++I;
619 if (I->Tok->isNot(Keywords.kw_function))
620 return false;
621 ++I;
622 return I->Tok->is(tok::l_paren);
623}
624
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000625static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
626 const FormatToken &InitialToken) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000627 if (InitialToken.is(tok::kw_namespace))
628 return Style.BraceWrapping.AfterNamespace;
629 if (InitialToken.is(tok::kw_class))
630 return Style.BraceWrapping.AfterClass;
631 if (InitialToken.is(tok::kw_union))
632 return Style.BraceWrapping.AfterUnion;
633 if (InitialToken.is(tok::kw_struct))
634 return Style.BraceWrapping.AfterStruct;
635 return false;
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000636}
637
Manuel Klimek516e0542013-09-04 13:25:30 +0000638void UnwrappedLineParser::parseChildBlock() {
639 FormatTok->BlockKind = BK_Block;
640 nextToken();
641 {
Manuel Klimek89628f62017-09-20 09:51:03 +0000642 bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript &&
643 (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
Manuel Klimek516e0542013-09-04 13:25:30 +0000644 ScopedLineState LineState(*this);
645 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
646 /*MustBeDeclaration=*/false);
Martin Probst101ec892017-05-09 20:04:09 +0000647 Line->Level += SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000648 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000649 flushComments(isOnNewLine(*FormatTok));
Martin Probst101ec892017-05-09 20:04:09 +0000650 Line->Level -= SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000651 }
652 nextToken();
653}
654
Daniel Jasperf7935112012-12-03 18:12:45 +0000655void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000656 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000657 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000658 nextToken();
659
Craig Topper2145bc02014-05-09 08:15:10 +0000660 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000661 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000662 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000663 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000664
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000665 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000666 case tok::pp_define:
667 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000668 return;
669 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000670 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000671 break;
672 case tok::pp_ifdef:
673 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000674 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000675 break;
676 case tok::pp_else:
677 parsePPElse();
678 break;
679 case tok::pp_elif:
680 parsePPElIf();
681 break;
682 case tok::pp_endif:
683 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000684 break;
685 default:
686 parsePPUnknown();
687 break;
688 }
689}
690
Manuel Klimek68b03042014-04-14 09:14:11 +0000691void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
Francois Ferranda98a95c2017-07-28 07:56:14 +0000692 size_t Line = CurrentLines->size();
693 if (CurrentLines == &PreprocessorDirectives)
694 Line += Lines.size();
695
696 if (Unreachable ||
697 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
698 PPStack.push_back({PP_Unreachable, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000699 else
Francois Ferranda98a95c2017-07-28 07:56:14 +0000700 PPStack.push_back({PP_Conditional, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000701}
702
Manuel Klimek68b03042014-04-14 09:14:11 +0000703void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000704 ++PPBranchLevel;
705 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
706 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
707 PPLevelBranchIndex.push_back(0);
708 PPLevelBranchCount.push_back(0);
709 }
710 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000711 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
712 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000713}
714
Manuel Klimek68b03042014-04-14 09:14:11 +0000715void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000716 if (!PPStack.empty())
717 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000718 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
719 if (!PPChainBranchIndex.empty())
720 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000721 conditionalCompilationCondition(
722 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
723 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000724}
725
Manuel Klimek68b03042014-04-14 09:14:11 +0000726void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000727 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
728 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
729 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000730 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
731 }
732 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000733 // Guard against #endif's without #if.
Krasimir Georgievad47c902017-08-30 14:34:57 +0000734 if (PPBranchLevel > -1)
Manuel Klimek14bd9172014-01-29 08:49:02 +0000735 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000736 if (!PPChainBranchIndex.empty())
737 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000738 if (!PPStack.empty())
739 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000740}
741
742void UnwrappedLineParser::parsePPIf(bool IfDef) {
Daniel Jasper62703eb2017-03-01 11:10:11 +0000743 bool IfNDef = FormatTok->is(tok::pp_ifndef);
Manuel Klimek68b03042014-04-14 09:14:11 +0000744 nextToken();
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000745 bool Unreachable = false;
746 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
747 Unreachable = true;
Daniel Jasper62703eb2017-03-01 11:10:11 +0000748 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000749 Unreachable = true;
750 conditionalCompilationStart(Unreachable);
Krasimir Georgievad47c902017-08-30 14:34:57 +0000751 FormatToken *IfCondition = FormatTok;
752 // If there's a #ifndef on the first line, and the only lines before it are
753 // comments, it could be an include guard.
754 bool MaybeIncludeGuard = IfNDef;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000755 if (IncludeGuard == IG_Inited && MaybeIncludeGuard)
Krasimir Georgievad47c902017-08-30 14:34:57 +0000756 for (auto &Line : Lines) {
757 if (!Line.Tokens.front().Tok->is(tok::comment)) {
758 MaybeIncludeGuard = false;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000759 IncludeGuard = IG_Rejected;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000760 break;
761 }
762 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000763 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000764 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000765 ++PPBranchLevel;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000766 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
767 IncludeGuard = IG_IfNdefed;
768 IncludeGuardToken = IfCondition;
769 }
Manuel Klimek68b03042014-04-14 09:14:11 +0000770}
771
772void UnwrappedLineParser::parsePPElse() {
Krasimir Georgievad47c902017-08-30 14:34:57 +0000773 // If a potential include guard has an #else, it's not an include guard.
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000774 if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
775 IncludeGuard = IG_Rejected;
Manuel Klimek68b03042014-04-14 09:14:11 +0000776 conditionalCompilationAlternative();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000777 if (PPBranchLevel > -1)
778 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000779 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000780 ++PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000781}
782
783void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
784
785void UnwrappedLineParser::parsePPEndIf() {
786 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000787 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000788 // If the #endif of a potential include guard is the last thing in the file,
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000789 // then we found an include guard.
Krasimir Georgievad47c902017-08-30 14:34:57 +0000790 unsigned TokenPosition = Tokens->getPosition();
791 FormatToken *PeekNext = AllTokens[TokenPosition];
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000792 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
793 PeekNext->is(tok::eof) &&
Daniel Jasper4df130f2017-09-04 13:33:52 +0000794 Style.IndentPPDirectives != FormatStyle::PPDIS_None)
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000795 IncludeGuard = IG_Found;
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000796}
797
Manuel Klimek1abf7892013-01-04 23:34:14 +0000798void UnwrappedLineParser::parsePPDefine() {
799 nextToken();
800
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000801 if (FormatTok->Tok.getKind() != tok::identifier) {
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000802 IncludeGuard = IG_Rejected;
803 IncludeGuardToken = nullptr;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000804 parsePPUnknown();
805 return;
806 }
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000807
808 if (IncludeGuard == IG_IfNdefed &&
809 IncludeGuardToken->TokenText == FormatTok->TokenText) {
810 IncludeGuard = IG_Defined;
811 IncludeGuardToken = nullptr;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000812 for (auto &Line : Lines) {
813 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000814 IncludeGuard = IG_Rejected;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000815 break;
816 }
817 }
818 }
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000819
Manuel Klimek1abf7892013-01-04 23:34:14 +0000820 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000821 if (FormatTok->Tok.getKind() == tok::l_paren &&
822 FormatTok->WhitespaceRange.getBegin() ==
823 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000824 parseParens();
825 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000826 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
827 Line->Level += PPBranchLevel + 1;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000828 addUnwrappedLine();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000829 ++Line->Level;
Manuel Klimek1b896292013-01-07 09:34:28 +0000830
831 // Errors during a preprocessor directive can only affect the layout of the
832 // preprocessor directive, and thus we ignore them. An alternative approach
833 // would be to use the same approach we use on the file level (no
834 // re-indentation if there was a structural error) within the macro
835 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000836 parseFile();
837}
838
839void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000840 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000841 nextToken();
842 } while (!eof());
Krasimir Georgievad47c902017-08-30 14:34:57 +0000843 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
844 Line->Level += PPBranchLevel + 1;
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000845 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000846}
847
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000848// Here we blacklist certain tokens that are not usually the first token in an
849// unwrapped line. This is used in attempt to distinguish macro calls without
850// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000851static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000852 // Semicolon can be a null-statement, l_square can be a start of a macro or
853 // a C++11 attribute, but this doesn't seem to be common.
854 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
855 Tok.isNot(tok::l_square) &&
856 // Tokens that can only be used as binary operators and a part of
857 // overloaded operator names.
858 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
859 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
860 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
861 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
862 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
863 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
864 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
865 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
866 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
867 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
868 Tok.isNot(tok::lesslessequal) &&
869 // Colon is used in labels, base class lists, initializer lists,
870 // range-based for loops, ternary operator, but should never be the
871 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000872 Tok.isNot(tok::colon) &&
873 // 'noexcept' is a trailing annotation.
874 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000875}
876
Martin Probst533965c2016-04-19 18:19:06 +0000877static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
878 const FormatToken *FormatTok) {
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000879 // FIXME: This returns true for C/C++ keywords like 'struct'.
880 return FormatTok->is(tok::identifier) &&
881 (FormatTok->Tok.getIdentifierInfo() == nullptr ||
Martin Probst3dbbefa2016-11-10 16:21:02 +0000882 !FormatTok->isOneOf(
883 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
884 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
885 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
886 Keywords.kw_let, Keywords.kw_var, tok::kw_const,
887 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
Manuel Klimek89628f62017-09-20 09:51:03 +0000888 Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws,
889 Keywords.kw_from));
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000890}
891
Martin Probst533965c2016-04-19 18:19:06 +0000892static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
893 const FormatToken *FormatTok) {
Martin Probstb9316ff2016-09-18 17:21:52 +0000894 return FormatTok->Tok.isLiteral() ||
895 FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
896 mustBeJSIdent(Keywords, FormatTok);
Martin Probst533965c2016-04-19 18:19:06 +0000897}
898
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000899// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
900// when encountered after a value (see mustBeJSIdentOrValue).
901static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
902 const FormatToken *FormatTok) {
903 return FormatTok->isOneOf(
Martin Probst5f8445b2016-04-24 22:05:09 +0000904 tok::kw_return, Keywords.kw_yield,
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000905 // conditionals
906 tok::kw_if, tok::kw_else,
907 // loops
908 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
909 // switch/case
910 tok::kw_switch, tok::kw_case,
911 // exceptions
912 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
913 // declaration
914 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
Martin Probst5f8445b2016-04-24 22:05:09 +0000915 Keywords.kw_async, Keywords.kw_function,
916 // import/export
917 Keywords.kw_import, tok::kw_export);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000918}
919
920// readTokenWithJavaScriptASI reads the next token and terminates the current
921// line if JavaScript Automatic Semicolon Insertion must
922// happen between the current token and the next token.
923//
924// This method is conservative - it cannot cover all edge cases of JavaScript,
925// but only aims to correctly handle certain well known cases. It *must not*
926// return true in speculative cases.
927void UnwrappedLineParser::readTokenWithJavaScriptASI() {
928 FormatToken *Previous = FormatTok;
929 readToken();
930 FormatToken *Next = FormatTok;
931
932 bool IsOnSameLine =
933 CommentsBeforeNextToken.empty()
934 ? Next->NewlinesBefore == 0
935 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
936 if (IsOnSameLine)
937 return;
938
939 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
Martin Probst717f6dc2016-10-21 05:11:38 +0000940 bool PreviousStartsTemplateExpr =
941 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${");
Martin Probst7e0f25b2017-11-25 09:19:42 +0000942 if (PreviousMustBeValue || Previous->is(tok::r_paren)) {
943 // If the line contains an '@' sign, the previous token might be an
944 // annotation, which can precede another identifier/value.
945 bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
946 [](UnwrappedLineNode &LineNode) {
947 return LineNode.Tok->is(tok::at);
948 }) != Line->Tokens.end();
949 if (HasAt)
Martin Probstbbffeac2016-04-11 07:35:57 +0000950 return;
951 }
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000952 if (Next->is(tok::exclaim) && PreviousMustBeValue)
Martin Probstd40bca42017-01-09 08:56:36 +0000953 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000954 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
Martin Probst717f6dc2016-10-21 05:11:38 +0000955 bool NextEndsTemplateExpr =
956 Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
957 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
958 (PreviousMustBeValue ||
959 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
960 tok::minusminus)))
Martin Probstd40bca42017-01-09 08:56:36 +0000961 return addUnwrappedLine();
Martin Probst0a19d432017-08-09 15:19:16 +0000962 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) &&
963 isJSDeclOrStmt(Keywords, Next))
Martin Probstd40bca42017-01-09 08:56:36 +0000964 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000965}
966
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000967void UnwrappedLineParser::parseStructuralElement() {
Daniel Jasper498f5582015-12-25 08:53:31 +0000968 assert(!FormatTok->is(tok::l_brace));
969 if (Style.Language == FormatStyle::LK_TableGen &&
970 FormatTok->is(tok::pp_include)) {
971 nextToken();
972 if (FormatTok->is(tok::string_literal))
973 nextToken();
974 addUnwrappedLine();
975 return;
976 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000977 switch (FormatTok->Tok.getKind()) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000978 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000979 nextToken();
980 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000981 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000982 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000983 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000984 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000985 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000986 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000987 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000988 break;
989 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000990 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000991 nextToken();
992 }
993 }
994 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000995 case tok::kw_namespace:
996 parseNamespace();
997 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000998 case tok::kw_public:
999 case tok::kw_protected:
1000 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +00001001 if (Style.Language == FormatStyle::LK_Java ||
1002 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001003 nextToken();
1004 else
1005 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +00001006 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001007 case tok::kw_if:
1008 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +00001009 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001010 case tok::kw_for:
1011 case tok::kw_while:
1012 parseForOrWhileLoop();
1013 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001014 case tok::kw_do:
1015 parseDoWhile();
1016 return;
1017 case tok::kw_switch:
Martin Probstf785fd92017-08-04 17:07:15 +00001018 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1019 // 'switch: string' field declaration.
1020 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001021 parseSwitch();
1022 return;
1023 case tok::kw_default:
Martin Probstf785fd92017-08-04 17:07:15 +00001024 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1025 // 'default: string' field declaration.
1026 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001027 nextToken();
Nico Weberc29f83b2018-01-23 16:30:56 +00001028 if (FormatTok->is(tok::colon)) {
1029 parseLabel();
1030 return;
1031 }
1032 // e.g. "default void f() {}" in a Java interface.
1033 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001034 case tok::kw_case:
Martin Probstf785fd92017-08-04 17:07:15 +00001035 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1036 // 'case: string' field declaration.
1037 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001038 parseCaseLabel();
1039 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001040 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +00001041 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +00001042 parseTryCatch();
1043 return;
Manuel Klimekae610d12013-01-21 14:32:05 +00001044 case tok::kw_extern:
1045 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001046 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +00001047 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001048 if (FormatTok->Tok.is(tok::l_brace)) {
Krasimir Georgievd6ce9372017-09-15 11:23:50 +00001049 if (Style.BraceWrapping.AfterExternBlock) {
1050 addUnwrappedLine();
1051 parseBlock(/*MustBeDeclaration=*/true);
1052 } else {
1053 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
1054 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001055 addUnwrappedLine();
1056 return;
1057 }
1058 }
Daniel Jaspere1e43192014-04-01 12:55:11 +00001059 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +00001060 case tok::kw_export:
1061 if (Style.Language == FormatStyle::LK_JavaScript) {
1062 parseJavaScriptEs6ImportExport();
1063 return;
1064 }
Sam McCall6f3778c2018-09-05 07:44:02 +00001065 if (!Style.isCpp())
1066 break;
1067 // Handle C++ "(inline|export) namespace".
1068 LLVM_FALLTHROUGH;
1069 case tok::kw_inline:
1070 nextToken();
1071 if (FormatTok->Tok.is(tok::kw_namespace)) {
1072 parseNamespace();
1073 return;
1074 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001075 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +00001076 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001077 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001078 parseForOrWhileLoop();
1079 return;
1080 }
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001081 if (FormatTok->is(TT_MacroBlockBegin)) {
1082 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
1083 /*MunchSemi=*/false);
1084 return;
1085 }
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001086 if (FormatTok->is(Keywords.kw_import)) {
1087 if (Style.Language == FormatStyle::LK_JavaScript) {
1088 parseJavaScriptEs6ImportExport();
1089 return;
1090 }
1091 if (Style.Language == FormatStyle::LK_Proto) {
1092 nextToken();
Daniel Jasper8b61d142016-06-20 20:39:53 +00001093 if (FormatTok->is(tok::kw_public))
1094 nextToken();
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001095 if (!FormatTok->is(tok::string_literal))
1096 return;
1097 nextToken();
1098 if (FormatTok->is(tok::semi))
1099 nextToken();
1100 addUnwrappedLine();
1101 return;
1102 }
Daniel Jasper354aa512015-02-19 16:07:32 +00001103 }
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001104 if (Style.isCpp() &&
Daniel Jasper72b33572017-03-31 12:04:37 +00001105 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
Daniel Jaspera00de632015-12-01 12:05:04 +00001106 Keywords.kw_slots, Keywords.kw_qslots)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001107 nextToken();
1108 if (FormatTok->is(tok::colon)) {
1109 nextToken();
1110 addUnwrappedLine();
Daniel Jasper31343832016-07-27 10:13:24 +00001111 return;
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001112 }
Daniel Jasper53395402015-04-07 15:04:40 +00001113 }
Francois Ferrand6f40e212018-10-02 16:37:51 +00001114 if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) {
1115 parseStatementMacro();
1116 return;
1117 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001118 // In all other cases, parse the declaration.
1119 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001120 default:
1121 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001122 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001123 do {
Manuel Klimeke411aa82017-09-20 09:29:37 +00001124 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001125 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +00001126 case tok::at:
1127 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001128 if (FormatTok->Tok.is(tok::l_brace)) {
1129 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001130 parseBracedList();
Nico Weberc068ff72018-01-23 17:10:25 +00001131 break;
Hans Wennborg749c1b52018-10-19 16:19:52 +00001132 } else if (Style.Language == FormatStyle::LK_Java &&
1133 FormatTok->is(Keywords.kw_interface)) {
1134 nextToken();
1135 break;
Nico Weberc068ff72018-01-23 17:10:25 +00001136 }
1137 switch (FormatTok->Tok.getObjCKeywordID()) {
1138 case tok::objc_public:
1139 case tok::objc_protected:
1140 case tok::objc_package:
1141 case tok::objc_private:
1142 return parseAccessSpecifier();
1143 case tok::objc_interface:
1144 case tok::objc_implementation:
1145 return parseObjCInterfaceOrImplementation();
1146 case tok::objc_protocol:
1147 if (parseObjCProtocol())
1148 return;
1149 break;
1150 case tok::objc_end:
1151 return; // Handled by the caller.
1152 case tok::objc_optional:
1153 case tok::objc_required:
1154 nextToken();
1155 addUnwrappedLine();
1156 return;
1157 case tok::objc_autoreleasepool:
1158 nextToken();
1159 if (FormatTok->Tok.is(tok::l_brace)) {
Francois Ferranda2484b22018-02-27 13:48:27 +00001160 if (Style.BraceWrapping.AfterControlStatement)
Nico Weberc068ff72018-01-23 17:10:25 +00001161 addUnwrappedLine();
1162 parseBlock(/*MustBeDeclaration=*/false);
1163 }
1164 addUnwrappedLine();
1165 return;
Francois Ferrandba91c3d2018-02-27 13:48:21 +00001166 case tok::objc_synchronized:
1167 nextToken();
1168 if (FormatTok->Tok.is(tok::l_paren))
1169 // Skip synchronization object
1170 parseParens();
1171 if (FormatTok->Tok.is(tok::l_brace)) {
Francois Ferranda2484b22018-02-27 13:48:27 +00001172 if (Style.BraceWrapping.AfterControlStatement)
Francois Ferrandba91c3d2018-02-27 13:48:21 +00001173 addUnwrappedLine();
1174 parseBlock(/*MustBeDeclaration=*/false);
1175 }
1176 addUnwrappedLine();
1177 return;
Nico Weberc068ff72018-01-23 17:10:25 +00001178 case tok::objc_try:
1179 // This branch isn't strictly necessary (the kw_try case below would
1180 // do this too after the tok::at is parsed above). But be explicit.
1181 parseTryCatch();
1182 return;
1183 default:
1184 break;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001185 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001186 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001187 case tok::kw_enum:
Daniel Jaspera7900ad2016-05-08 18:12:22 +00001188 // Ignore if this is part of "template <enum ...".
1189 if (Previous && Previous->is(tok::less)) {
1190 nextToken();
1191 break;
1192 }
1193
Daniel Jasper90cf3802015-06-17 09:44:02 +00001194 // parseEnum falls through and does not yet add an unwrapped line as an
1195 // enum definition can start a structural element.
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001196 if (!parseEnum())
1197 break;
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001198 // This only applies for C++.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001199 if (!Style.isCpp()) {
Daniel Jasper90cf3802015-06-17 09:44:02 +00001200 addUnwrappedLine();
1201 return;
1202 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001203 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001204 case tok::kw_typedef:
1205 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +00001206 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1207 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001208 parseEnum();
1209 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001210 case tok::kw_struct:
1211 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +00001212 case tok::kw_class:
Daniel Jasper910807d2015-06-12 04:52:02 +00001213 // parseRecord falls through and does not yet add an unwrapped line as a
1214 // record declaration or definition can start a structural element.
Manuel Klimeke01bab52013-01-15 13:38:33 +00001215 parseRecord();
Daniel Jasper910807d2015-06-12 04:52:02 +00001216 // This does not apply for Java and JavaScript.
1217 if (Style.Language == FormatStyle::LK_Java ||
1218 Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperd5ec65b2016-01-08 07:06:07 +00001219 if (FormatTok->is(tok::semi))
1220 nextToken();
Daniel Jasper910807d2015-06-12 04:52:02 +00001221 addUnwrappedLine();
1222 return;
1223 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001224 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +00001225 case tok::period:
1226 nextToken();
1227 // In Java, classes have an implicit static member "class".
1228 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
1229 FormatTok->is(tok::kw_class))
1230 nextToken();
Daniel Jasperba52fcb2015-09-28 14:29:45 +00001231 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
1232 FormatTok->Tok.getIdentifierInfo())
1233 // JavaScript only has pseudo keywords, all keywords are allowed to
1234 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1235 nextToken();
Daniel Jaspere5d74862014-11-26 08:17:08 +00001236 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001237 case tok::semi:
1238 nextToken();
1239 addUnwrappedLine();
1240 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001241 case tok::r_brace:
1242 addUnwrappedLine();
1243 return;
Daniel Jasperf7935112012-12-03 18:12:45 +00001244 case tok::l_paren:
1245 parseParens();
1246 break;
Daniel Jasper5af04a42015-10-07 03:43:10 +00001247 case tok::kw_operator:
1248 nextToken();
1249 if (FormatTok->isBinaryOperator())
1250 nextToken();
1251 break;
Manuel Klimek516e0542013-09-04 13:25:30 +00001252 case tok::caret:
1253 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +00001254 if (FormatTok->Tok.isAnyIdentifier() ||
1255 FormatTok->isSimpleTypeSpecifier())
1256 nextToken();
1257 if (FormatTok->is(tok::l_paren))
1258 parseParens();
1259 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +00001260 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +00001261 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001262 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001263 if (!tryToParseBracedList()) {
1264 // A block outside of parentheses must be the last part of a
1265 // structural element.
1266 // FIXME: Figure out cases where this is not true, and add projections
1267 // for them (the one we know is missing are lambdas).
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001268 if (Style.BraceWrapping.AfterFunction)
Manuel Klimekab419912013-05-23 09:41:43 +00001269 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +00001270 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +00001271 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001272 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +00001273 return;
1274 }
1275 // Otherwise this was a braced init list, and the structural
1276 // element continues.
1277 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001278 case tok::kw_try:
1279 // We arrive here when parsing function-try blocks.
Owen Pancb5ffbe2018-09-28 09:17:00 +00001280 if (Style.BraceWrapping.AfterFunction)
1281 addUnwrappedLine();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001282 parseTryCatch();
1283 return;
Daniel Jasper40e19212013-05-29 13:16:10 +00001284 case tok::identifier: {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001285 if (FormatTok->is(TT_MacroBlockEnd)) {
1286 addUnwrappedLine();
1287 return;
1288 }
1289
Martin Probst973ff792017-04-27 13:07:24 +00001290 // Function declarations (as opposed to function expressions) are parsed
1291 // on their own unwrapped line by continuing this loop. Function
1292 // expressions (functions that are not on their own line) must not create
1293 // a new unwrapped line, so they are special cased below.
1294 size_t TokenCount = Line->Tokens.size();
Daniel Jasper9326f912015-05-05 08:40:32 +00001295 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst973ff792017-04-27 13:07:24 +00001296 FormatTok->is(Keywords.kw_function) &&
1297 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1298 Keywords.kw_async)))) {
Daniel Jasper069e5f42014-05-20 11:14:57 +00001299 tryToParseJSFunction();
1300 break;
1301 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001302 if ((Style.Language == FormatStyle::LK_JavaScript ||
1303 Style.Language == FormatStyle::LK_Java) &&
1304 FormatTok->is(Keywords.kw_interface)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001305 if (Style.Language == FormatStyle::LK_JavaScript) {
1306 // In JavaScript/TypeScript, "interface" can be used as a standalone
1307 // identifier, e.g. in `var interface = 1;`. If "interface" is
1308 // followed by another identifier, it is very like to be an actual
1309 // interface declaration.
1310 unsigned StoredPosition = Tokens->getPosition();
1311 FormatToken *Next = Tokens->getNextToken();
1312 FormatTok = Tokens->setPosition(StoredPosition);
Martin Probst533965c2016-04-19 18:19:06 +00001313 if (Next && !mustBeJSIdent(Keywords, Next)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001314 nextToken();
1315 break;
1316 }
1317 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001318 parseRecord();
Daniel Jasper259188b2015-06-12 04:56:34 +00001319 addUnwrappedLine();
Daniel Jasper5c235c02015-07-06 14:26:04 +00001320 return;
Daniel Jasper9326f912015-05-05 08:40:32 +00001321 }
1322
Francois Ferrand6f40e212018-10-02 16:37:51 +00001323 if (Style.isCpp() && FormatTok->is(TT_StatementMacro)) {
1324 parseStatementMacro();
1325 return;
1326 }
1327
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001328 // See if the following token should start a new unwrapped line.
Daniel Jasper9326f912015-05-05 08:40:32 +00001329 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +00001330 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +00001331 if (Line->Tokens.size() == 1 &&
1332 // JS doesn't have macros, and within classes colons indicate fields,
1333 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +00001334 Style.Language != FormatStyle::LK_JavaScript) {
1335 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Daniel Jasper40609472016-04-06 15:02:46 +00001336 Line->Tokens.begin()->Tok->MustBreakBefore = true;
Alexander Kornienkode644272013-04-08 22:16:06 +00001337 parseLabel();
1338 return;
1339 }
Daniel Jasper680b09b2014-11-05 10:48:04 +00001340 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +00001341 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +00001342 bool FunctionLike = FormatTok->is(tok::l_paren);
1343 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +00001344 parseParens();
Daniel Jaspere60cba12015-05-13 11:35:53 +00001345
1346 bool FollowedByNewline =
1347 CommentsBeforeNextToken.empty()
1348 ? FormatTok->NewlinesBefore > 0
1349 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1350
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001351 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
Daniel Jasper680b09b2014-11-05 10:48:04 +00001352 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +00001353 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +00001354 return;
Alexander Kornienkode644272013-04-08 22:16:06 +00001355 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001356 }
1357 break;
Daniel Jasper40e19212013-05-29 13:16:10 +00001358 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001359 case tok::equal:
Manuel Klimek79e06082015-05-21 12:23:34 +00001360 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1361 // TT_JsFatArrow. The always start an expression or a child block if
1362 // followed by a curly.
1363 if (FormatTok->is(TT_JsFatArrow)) {
1364 nextToken();
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001365 if (FormatTok->is(tok::l_brace))
Manuel Klimek79e06082015-05-21 12:23:34 +00001366 parseChildBlock();
Manuel Klimek79e06082015-05-21 12:23:34 +00001367 break;
1368 }
1369
Daniel Jaspere25509f2012-12-17 11:29:41 +00001370 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001371 if (FormatTok->Tok.is(tok::l_brace)) {
1372 nextToken();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001373 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001374 } else if (Style.Language == FormatStyle::LK_Proto &&
Manuel Klimek89628f62017-09-20 09:51:03 +00001375 FormatTok->Tok.is(tok::less)) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001376 nextToken();
Krasimir Georgiev0b41fcb2017-06-27 13:58:41 +00001377 parseBracedList(/*ContinueOnSemicolons=*/false,
1378 /*ClosingBraceKind=*/tok::greater);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001379 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001380 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001381 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001382 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001383 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +00001384 case tok::kw_new:
1385 parseNew();
1386 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001387 default:
1388 nextToken();
1389 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001390 }
1391 } while (!eof());
1392}
1393
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001394bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001395 if (!Style.isCpp()) {
Daniel Jasper1feab0f2015-06-02 15:31:37 +00001396 nextToken();
1397 return false;
1398 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001399 assert(FormatTok->is(tok::l_square));
1400 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001401 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001402 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001403
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001404 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001405 if (FormatTok->isSimpleTypeSpecifier()) {
1406 nextToken();
1407 continue;
1408 }
Manuel Klimekffdeb592013-09-03 15:10:01 +00001409 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001410 case tok::l_brace:
1411 break;
1412 case tok::l_paren:
1413 parseParens();
1414 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +00001415 case tok::amp:
1416 case tok::star:
1417 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +00001418 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001419 case tok::less:
1420 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001421 case tok::identifier:
Daniel Jasper5eaa0092015-08-13 13:37:08 +00001422 case tok::numeric_constant:
Daniel Jasper1067ab02014-02-11 10:16:55 +00001423 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001424 case tok::kw_mutable:
Ben Hamilton4e442bb2019-01-30 13:54:32 +00001425 case tok::kw_noexcept:
Daniel Jasper81a20782014-03-10 10:02:02 +00001426 nextToken();
1427 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001428 case tok::arrow:
Ben Hamilton30b7d092019-02-08 15:55:18 +00001429 // This might or might not actually be a lambda arrow (this could be an
1430 // ObjC method invocation followed by a dereferencing arrow). We might
1431 // reset this back to TT_Unknown in TokenAnnotator.
Daniel Jasper6f2b88a2015-06-05 13:18:09 +00001432 FormatTok->Type = TT_LambdaArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001433 nextToken();
1434 break;
1435 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001436 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001437 }
1438 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001439 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +00001440 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001441 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001442}
1443
1444bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
Manuel Klimek89628f62017-09-20 09:51:03 +00001445 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001446 if (Previous &&
1447 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
Manuel Klimekd0f3fe52018-04-11 14:51:54 +00001448 tok::kw_delete, tok::l_square) ||
Manuel Klimek89628f62017-09-20 09:51:03 +00001449 FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() ||
1450 Previous->isSimpleTypeSpecifier())) {
Manuel Klimekffdeb592013-09-03 15:10:01 +00001451 nextToken();
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001452 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001453 }
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001454 nextToken();
Manuel Klimekd0f3fe52018-04-11 14:51:54 +00001455 if (FormatTok->is(tok::l_square)) {
1456 return false;
1457 }
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001458 parseSquare(/*LambdaIntroducer=*/true);
1459 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001460}
1461
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001462void UnwrappedLineParser::tryToParseJSFunction() {
Martin Probst409697e2016-05-29 14:41:07 +00001463 assert(FormatTok->is(Keywords.kw_function) ||
1464 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
Martin Probst5f8445b2016-04-24 22:05:09 +00001465 if (FormatTok->is(Keywords.kw_async))
1466 nextToken();
1467 // Consume "function".
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001468 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001469
Daniel Jasper71e50af2016-11-01 06:22:59 +00001470 // Consume * (generator function). Treat it like C++'s overloaded operators.
1471 if (FormatTok->is(tok::star)) {
1472 FormatTok->Type = TT_OverloadedOperator;
Martin Probst5f8445b2016-04-24 22:05:09 +00001473 nextToken();
Daniel Jasper71e50af2016-11-01 06:22:59 +00001474 }
Martin Probst5f8445b2016-04-24 22:05:09 +00001475
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001476 // Consume function name.
1477 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001478 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001479
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001480 if (FormatTok->isNot(tok::l_paren))
1481 return;
Manuel Klimek79e06082015-05-21 12:23:34 +00001482
1483 // Parse formal parameter list.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001484 parseParens();
Manuel Klimek79e06082015-05-21 12:23:34 +00001485
1486 if (FormatTok->is(tok::colon)) {
1487 // Parse a type definition.
1488 nextToken();
1489
1490 // Eat the type declaration. For braced inline object types, balance braces,
1491 // otherwise just parse until finding an l_brace for the function body.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001492 if (FormatTok->is(tok::l_brace))
1493 tryToParseBracedList();
1494 else
Martin Probstaf16c502017-01-04 13:36:43 +00001495 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
Manuel Klimek79e06082015-05-21 12:23:34 +00001496 nextToken();
Manuel Klimek79e06082015-05-21 12:23:34 +00001497 }
1498
Martin Probstaf16c502017-01-04 13:36:43 +00001499 if (FormatTok->is(tok::semi))
1500 return;
1501
Manuel Klimek79e06082015-05-21 12:23:34 +00001502 parseChildBlock();
1503}
1504
Daniel Jasper3c883d12015-05-18 14:49:19 +00001505bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001506 if (FormatTok->BlockKind == BK_Unknown)
Daniel Jasper3c883d12015-05-18 14:49:19 +00001507 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001508 assert(FormatTok->BlockKind != BK_Unknown);
1509 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001510 return false;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001511 nextToken();
Manuel Klimekab419912013-05-23 09:41:43 +00001512 parseBracedList();
1513 return true;
1514}
1515
Krasimir Georgievff747be2017-06-27 13:43:07 +00001516bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1517 tok::TokenKind ClosingBraceKind) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001518 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001519
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001520 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1521 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001522 do {
Manuel Klimek79e06082015-05-21 12:23:34 +00001523 if (Style.Language == FormatStyle::LK_JavaScript) {
Martin Probst409697e2016-05-29 14:41:07 +00001524 if (FormatTok->is(Keywords.kw_function) ||
1525 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001526 tryToParseJSFunction();
1527 continue;
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001528 }
1529 if (FormatTok->is(TT_JsFatArrow)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001530 nextToken();
1531 // Fat arrows can be followed by simple expressions or by child blocks
1532 // in curly braces.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001533 if (FormatTok->is(tok::l_brace)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001534 parseChildBlock();
1535 continue;
1536 }
1537 }
Martin Probst8e3eba02017-02-07 16:33:13 +00001538 if (FormatTok->is(tok::l_brace)) {
1539 // Could be a method inside of a braced list `{a() { return 1; }}`.
1540 if (tryToParseBracedList())
1541 continue;
1542 parseChildBlock();
1543 }
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001544 }
Krasimir Georgievff747be2017-06-27 13:43:07 +00001545 if (FormatTok->Tok.getKind() == ClosingBraceKind) {
1546 nextToken();
1547 return !HasError;
1548 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001549 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001550 case tok::caret:
1551 nextToken();
1552 if (FormatTok->is(tok::l_brace)) {
1553 parseChildBlock();
1554 }
1555 break;
1556 case tok::l_square:
1557 tryToParseLambda();
1558 break;
Daniel Jaspera87af7a2015-06-30 11:32:22 +00001559 case tok::l_paren:
1560 parseParens();
Daniel Jasperf46dec82015-03-31 14:34:15 +00001561 // JavaScript can just have free standing methods and getters/setters in
1562 // object literals. Detect them by a "{" following ")".
1563 if (Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperf46dec82015-03-31 14:34:15 +00001564 if (FormatTok->is(tok::l_brace))
1565 parseChildBlock();
1566 break;
1567 }
Daniel Jasperf46dec82015-03-31 14:34:15 +00001568 break;
Martin Probst8e3eba02017-02-07 16:33:13 +00001569 case tok::l_brace:
1570 // Assume there are no blocks inside a braced init list apart
1571 // from the ones we explicitly parse out (like lambdas).
1572 FormatTok->BlockKind = BK_BracedInit;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001573 nextToken();
Martin Probst8e3eba02017-02-07 16:33:13 +00001574 parseBracedList();
1575 break;
Krasimir Georgievfa4dbb62017-08-03 13:43:45 +00001576 case tok::less:
1577 if (Style.Language == FormatStyle::LK_Proto) {
1578 nextToken();
1579 parseBracedList(/*ContinueOnSemicolons=*/false,
1580 /*ClosingBraceKind=*/tok::greater);
1581 } else {
1582 nextToken();
1583 }
1584 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001585 case tok::semi:
Daniel Jasperb9a49902016-01-09 15:56:28 +00001586 // JavaScript (or more precisely TypeScript) can have semicolons in braced
1587 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1588 // used for error recovery if we have otherwise determined that this is
1589 // a braced list.
1590 if (Style.Language == FormatStyle::LK_JavaScript) {
1591 nextToken();
1592 break;
1593 }
Daniel Jasper015ed022013-09-13 09:20:45 +00001594 HasError = true;
1595 if (!ContinueOnSemicolons)
1596 return !HasError;
1597 nextToken();
1598 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001599 case tok::comma:
1600 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001601 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001602 default:
1603 nextToken();
1604 break;
1605 }
1606 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001607 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001608}
1609
Daniel Jasperf7935112012-12-03 18:12:45 +00001610void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001611 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001612 nextToken();
1613 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001614 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001615 case tok::l_paren:
1616 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001617 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1618 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001619 break;
1620 case tok::r_paren:
1621 nextToken();
1622 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001623 case tok::r_brace:
1624 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1625 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001626 case tok::l_square:
1627 tryToParseLambda();
1628 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001629 case tok::l_brace:
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001630 if (!tryToParseBracedList())
Manuel Klimekf017dc02013-09-04 13:34:14 +00001631 parseChildBlock();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001632 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001633 case tok::at:
1634 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001635 if (FormatTok->Tok.is(tok::l_brace)) {
1636 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001637 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001638 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001639 break;
Martin Probst1027fb82017-02-07 14:05:30 +00001640 case tok::kw_class:
1641 if (Style.Language == FormatStyle::LK_JavaScript)
1642 parseRecord(/*ParseAsExpr=*/true);
1643 else
1644 nextToken();
1645 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001646 case tok::identifier:
1647 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst409697e2016-05-29 14:41:07 +00001648 (FormatTok->is(Keywords.kw_function) ||
1649 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001650 tryToParseJSFunction();
1651 else
1652 nextToken();
1653 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001654 default:
1655 nextToken();
1656 break;
1657 }
1658 } while (!eof());
1659}
1660
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001661void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
1662 if (!LambdaIntroducer) {
1663 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1664 if (tryToParseLambda())
1665 return;
1666 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001667 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001668 switch (FormatTok->Tok.getKind()) {
1669 case tok::l_paren:
1670 parseParens();
1671 break;
1672 case tok::r_square:
1673 nextToken();
1674 return;
1675 case tok::r_brace:
1676 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1677 return;
1678 case tok::l_square:
1679 parseSquare();
1680 break;
1681 case tok::l_brace: {
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001682 if (!tryToParseBracedList())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001683 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001684 break;
1685 }
1686 case tok::at:
1687 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001688 if (FormatTok->Tok.is(tok::l_brace)) {
1689 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001690 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001691 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001692 break;
1693 default:
1694 nextToken();
1695 break;
1696 }
1697 } while (!eof());
1698}
1699
Daniel Jasperf7935112012-12-03 18:12:45 +00001700void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001701 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001702 nextToken();
Daniel Jasper6a7d5a72017-06-19 07:40:49 +00001703 if (FormatTok->Tok.is(tok::kw_constexpr))
1704 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001705 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001706 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001707 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001708 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001709 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001710 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001711 if (Style.BraceWrapping.BeforeElse)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001712 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001713 else
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001714 NeedsUnwrappedLine = true;
Daniel Jasperf7935112012-12-03 18:12:45 +00001715 } else {
1716 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001717 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001718 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001719 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001720 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001721 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001722 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001723 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001724 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001725 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001726 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001727 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001728 parseIfThenElse();
1729 } else {
1730 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001731 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001732 parseStructuralElement();
Daniel Jasper451544a2016-05-19 06:30:48 +00001733 if (FormatTok->is(tok::eof))
1734 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001735 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001736 }
1737 } else if (NeedsUnwrappedLine) {
1738 addUnwrappedLine();
1739 }
1740}
1741
Daniel Jasper04a71a42014-05-08 11:58:24 +00001742void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001743 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001744 nextToken();
1745 bool NeedsUnwrappedLine = false;
1746 if (FormatTok->is(tok::colon)) {
1747 // We are in a function try block, what comes is an initializer list.
1748 nextToken();
1749 while (FormatTok->is(tok::identifier)) {
1750 nextToken();
1751 if (FormatTok->is(tok::l_paren))
1752 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001753 if (FormatTok->is(tok::comma))
1754 nextToken();
1755 }
1756 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001757 // Parse try with resource.
1758 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1759 parseParens();
1760 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001761 if (FormatTok->is(tok::l_brace)) {
1762 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1763 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001764 if (Style.BraceWrapping.BeforeCatch) {
Daniel Jasper04a71a42014-05-08 11:58:24 +00001765 addUnwrappedLine();
1766 } else {
1767 NeedsUnwrappedLine = true;
1768 }
1769 } else if (!FormatTok->is(tok::kw_catch)) {
1770 // The C++ standard requires a compound-statement after a try.
1771 // If there's none, we try to assume there's a structuralElement
1772 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001773 addUnwrappedLine();
1774 ++Line->Level;
1775 parseStructuralElement();
1776 --Line->Level;
1777 }
Nico Weber33381f52015-02-07 01:57:32 +00001778 while (1) {
1779 if (FormatTok->is(tok::at))
1780 nextToken();
1781 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1782 tok::kw___finally) ||
1783 ((Style.Language == FormatStyle::LK_Java ||
1784 Style.Language == FormatStyle::LK_JavaScript) &&
1785 FormatTok->is(Keywords.kw_finally)) ||
1786 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1787 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1788 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001789 nextToken();
1790 while (FormatTok->isNot(tok::l_brace)) {
1791 if (FormatTok->is(tok::l_paren)) {
1792 parseParens();
1793 continue;
1794 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001795 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001796 return;
1797 nextToken();
1798 }
1799 NeedsUnwrappedLine = false;
1800 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1801 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001802 if (Style.BraceWrapping.BeforeCatch)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001803 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001804 else
Daniel Jasper04a71a42014-05-08 11:58:24 +00001805 NeedsUnwrappedLine = true;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001806 }
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001807 if (NeedsUnwrappedLine)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001808 addUnwrappedLine();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001809}
1810
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001811void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001812 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001813
1814 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001815 nextToken();
Saleem Abdulrasool328085f2015-10-30 05:07:56 +00001816 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001817 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001818 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001819 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001820 addUnwrappedLine();
1821
Daniel Jasper65ee3472013-07-31 23:16:02 +00001822 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1823 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1824 DeclarationScopeStack.size() > 1);
1825 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001826 // Munch the semicolon after a namespace. This is more common than one would
1827 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001828 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001829 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001830 addUnwrappedLine();
1831 }
1832 // FIXME: Add error handling.
1833}
1834
Daniel Jasper6acf5132015-03-12 14:44:29 +00001835void UnwrappedLineParser::parseNew() {
1836 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1837 nextToken();
1838 if (Style.Language != FormatStyle::LK_Java)
1839 return;
1840
1841 // In Java, we can parse everything up to the parens, which aren't optional.
1842 do {
1843 // There should not be a ;, { or } before the new's open paren.
1844 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1845 return;
1846
1847 // Consume the parens.
1848 if (FormatTok->is(tok::l_paren)) {
1849 parseParens();
1850
1851 // If there is a class body of an anonymous class, consume that as child.
1852 if (FormatTok->is(tok::l_brace))
1853 parseChildBlock();
1854 return;
1855 }
1856 nextToken();
1857 } while (!eof());
1858}
1859
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001860void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001861 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001862 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001863 nextToken();
Martin Probsta050f412017-05-18 21:19:29 +00001864 // JS' for await ( ...
Martin Probstbd49e322017-05-15 19:33:20 +00001865 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probsta050f412017-05-18 21:19:29 +00001866 FormatTok->is(Keywords.kw_await))
Martin Probstbd49e322017-05-15 19:33:20 +00001867 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001868 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001869 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001870 if (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);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001873 addUnwrappedLine();
1874 } else {
1875 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001876 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001877 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001878 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001879 }
1880}
1881
Daniel Jasperf7935112012-12-03 18:12:45 +00001882void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001883 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001884 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001885 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001886 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001887 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001888 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001889 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001890 } else {
1891 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001892 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001893 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001894 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001895 }
1896
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001897 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001898 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001899 addUnwrappedLine();
1900 return;
1901 }
1902
Daniel Jasperf7935112012-12-03 18:12:45 +00001903 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001904 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001905}
1906
1907void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001908 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001909 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001910 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001911 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001912 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001913 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001914 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001915 if (FormatTok->Tok.is(tok::kw_break)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001916 if (Style.BraceWrapping.AfterControlStatement)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001917 addUnwrappedLine();
1918 parseStructuralElement();
1919 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001920 addUnwrappedLine();
1921 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001922 if (FormatTok->is(tok::semi))
1923 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001924 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001925 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001926 Line->Level = OldLineLevel;
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001927 if (FormatTok->isNot(tok::l_brace)) {
Daniel Jasper40609472016-04-06 15:02:46 +00001928 parseStructuralElement();
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001929 addUnwrappedLine();
1930 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001931}
1932
1933void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001934 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001935 // FIXME: fix handling of complex expressions here.
1936 do {
1937 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001938 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001939 parseLabel();
1940}
1941
1942void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001943 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001944 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001945 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001946 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001947 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001948 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001949 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001950 addUnwrappedLine();
1951 } else {
1952 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001953 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001954 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001955 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001956 }
1957}
1958
1959void UnwrappedLineParser::parseAccessSpecifier() {
1960 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001961 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001962 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001963 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001964 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001965 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001966 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001967 addUnwrappedLine();
1968}
1969
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001970bool UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001971 // Won't be 'enum' for NS_ENUMs.
1972 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001973 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001974
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001975 // In TypeScript, "enum" can also be used as property name, e.g. in interface
1976 // declarations. An "enum" keyword followed by a colon would be a syntax
1977 // error and thus assume it is just an identifier.
Daniel Jasper87379302016-02-03 05:33:44 +00001978 if (Style.Language == FormatStyle::LK_JavaScript &&
1979 FormatTok->isOneOf(tok::colon, tok::question))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001980 return false;
1981
Daniel Jasper2b41a822013-08-20 12:42:50 +00001982 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001983 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1984 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001985
Daniel Jasper786a5502013-09-06 21:32:35 +00001986 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001987 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1988 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001989 nextToken();
1990 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001991 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001992 parseParens();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001993 if (FormatTok->is(tok::identifier)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001994 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001995 // If there are two identifiers in a row, this is likely an elaborate
1996 // return type. In Java, this can be "implements", etc.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001997 if (Style.isCpp() && FormatTok->is(tok::identifier))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001998 return false;
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001999 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00002000 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00002001
2002 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00002003 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002004 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00002005 FormatTok->BlockKind = BK_Block;
2006
2007 if (Style.Language == FormatStyle::LK_Java) {
2008 // Java enums are different.
2009 parseJavaEnumBody();
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002010 return true;
2011 }
2012 if (Style.Language == FormatStyle::LK_Proto) {
Daniel Jasperc6dd2732015-07-16 14:25:43 +00002013 parseBlock(/*MustBeDeclaration=*/true);
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002014 return true;
Manuel Klimek2cec0192013-01-21 19:17:52 +00002015 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00002016
2017 // Parse enum body.
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002018 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002019 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
2020 if (HasError) {
2021 if (FormatTok->is(tok::semi))
2022 nextToken();
2023 addUnwrappedLine();
2024 }
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002025 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00002026
Daniel Jasper90cf3802015-06-17 09:44:02 +00002027 // There is no addUnwrappedLine() here so that we fall through to parsing a
2028 // structural element afterwards. Thus, in "enum A {} n, m;",
Manuel Klimek2cec0192013-01-21 19:17:52 +00002029 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00002030}
2031
2032void UnwrappedLineParser::parseJavaEnumBody() {
2033 // Determine whether the enum is simple, i.e. does not have a semicolon or
2034 // constants with class bodies. Simple enums can be formatted like braced
2035 // lists, contracted to a single line, etc.
2036 unsigned StoredPosition = Tokens->getPosition();
2037 bool IsSimple = true;
2038 FormatToken *Tok = Tokens->getNextToken();
2039 while (Tok) {
2040 if (Tok->is(tok::r_brace))
2041 break;
2042 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
2043 IsSimple = false;
2044 break;
2045 }
2046 // FIXME: This will also mark enums with braces in the arguments to enum
2047 // constants as "not simple". This is probably fine in practice, though.
2048 Tok = Tokens->getNextToken();
2049 }
2050 FormatTok = Tokens->setPosition(StoredPosition);
2051
2052 if (IsSimple) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002053 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002054 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00002055 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002056 return;
2057 }
2058
2059 // Parse the body of a more complex enum.
2060 // First add a line for everything up to the "{".
2061 nextToken();
2062 addUnwrappedLine();
2063 ++Line->Level;
2064
2065 // Parse the enum constants.
2066 while (FormatTok) {
2067 if (FormatTok->is(tok::l_brace)) {
2068 // Parse the constant's class body.
2069 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2070 /*MunchSemi=*/false);
2071 } else if (FormatTok->is(tok::l_paren)) {
2072 parseParens();
2073 } else if (FormatTok->is(tok::comma)) {
2074 nextToken();
2075 addUnwrappedLine();
2076 } else if (FormatTok->is(tok::semi)) {
2077 nextToken();
2078 addUnwrappedLine();
2079 break;
2080 } else if (FormatTok->is(tok::r_brace)) {
2081 addUnwrappedLine();
2082 break;
2083 } else {
2084 nextToken();
2085 }
2086 }
2087
2088 // Parse the class body after the enum's ";" if any.
2089 parseLevel(/*HasOpeningBrace=*/true);
2090 nextToken();
2091 --Line->Level;
2092 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00002093}
2094
Martin Probst1027fb82017-02-07 14:05:30 +00002095void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00002096 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00002097 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00002098
Daniel Jasper04785d02015-05-06 14:03:02 +00002099 // The actual identifier can be a nested name specifier, and in macros
2100 // it is often token-pasted.
2101 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2102 tok::kw___attribute, tok::kw___declspec,
2103 tok::kw_alignas) ||
2104 ((Style.Language == FormatStyle::LK_Java ||
2105 Style.Language == FormatStyle::LK_JavaScript) &&
2106 FormatTok->isOneOf(tok::period, tok::comma))) {
Martin Probstcb870c52017-08-01 15:46:10 +00002107 if (Style.Language == FormatStyle::LK_JavaScript &&
2108 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
2109 // JavaScript/TypeScript supports inline object types in
2110 // extends/implements positions:
2111 // class Foo implements {bar: number} { }
2112 nextToken();
2113 if (FormatTok->is(tok::l_brace)) {
2114 tryToParseBracedList();
2115 continue;
2116 }
2117 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002118 bool IsNonMacroIdentifier =
2119 FormatTok->is(tok::identifier) &&
2120 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002121 nextToken();
2122 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00002123 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00002124 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00002125 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00002126
Daniel Jasper04785d02015-05-06 14:03:02 +00002127 // Note that parsing away template declarations here leads to incorrectly
2128 // accepting function declarations as record declarations.
2129 // In general, we cannot solve this problem. Consider:
2130 // class A<int> B() {}
2131 // which can be a function definition or a class definition when B() is a
2132 // macro. If we find enough real-world cases where this is a problem, we
2133 // can parse for the 'template' keyword in the beginning of the statement,
2134 // and thus rule out the record production in case there is no template
2135 // (this would still leave us with an ambiguity between template function
2136 // and class declarations).
Daniel Jasperadba2aa2015-05-18 12:52:00 +00002137 if (FormatTok->isOneOf(tok::colon, tok::less)) {
2138 while (!eof()) {
Daniel Jasper3c883d12015-05-18 14:49:19 +00002139 if (FormatTok->is(tok::l_brace)) {
2140 calculateBraceTypes(/*ExpectClassBody=*/true);
2141 if (!tryToParseBracedList())
2142 break;
2143 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002144 if (FormatTok->Tok.is(tok::semi))
2145 return;
2146 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002147 }
2148 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002149 if (FormatTok->Tok.is(tok::l_brace)) {
Martin Probst1027fb82017-02-07 14:05:30 +00002150 if (ParseAsExpr) {
2151 parseChildBlock();
2152 } else {
2153 if (ShouldBreakBeforeBrace(Style, InitialToken))
2154 addUnwrappedLine();
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002155
Martin Probst1027fb82017-02-07 14:05:30 +00002156 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2157 /*MunchSemi=*/false);
2158 }
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002159 }
Daniel Jasper90cf3802015-06-17 09:44:02 +00002160 // There is no addUnwrappedLine() here so that we fall through to parsing a
2161 // structural element afterwards. Thus, in "class A {} n, m;",
2162 // "} n, m;" will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00002163}
2164
Ben Hamilton707e68f2018-05-30 15:21:38 +00002165void UnwrappedLineParser::parseObjCMethod() {
2166 assert(FormatTok->Tok.isOneOf(tok::l_paren, tok::identifier) &&
2167 "'(' or identifier expected.");
2168 do {
2169 if (FormatTok->Tok.is(tok::semi)) {
2170 nextToken();
2171 addUnwrappedLine();
2172 return;
2173 } else if (FormatTok->Tok.is(tok::l_brace)) {
Ben Hamilton97034a32018-10-12 19:43:01 +00002174 if (Style.BraceWrapping.AfterFunction)
2175 addUnwrappedLine();
Ben Hamilton707e68f2018-05-30 15:21:38 +00002176 parseBlock(/*MustBeDeclaration=*/false);
2177 addUnwrappedLine();
2178 return;
2179 } else {
2180 nextToken();
2181 }
2182 } while (!eof());
2183}
2184
Nico Weber8696a8d2013-01-09 21:15:03 +00002185void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002186 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Ben Hamilton1462e842018-04-05 15:26:25 +00002187 do {
Nico Weber8696a8d2013-01-09 21:15:03 +00002188 nextToken();
Ben Hamilton1462e842018-04-05 15:26:25 +00002189 // Early exit in case someone forgot a close angle.
2190 if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2191 FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2192 return;
2193 } while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00002194 nextToken(); // Skip '>'.
2195}
2196
2197void UnwrappedLineParser::parseObjCUntilAtEnd() {
2198 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002199 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002200 nextToken();
2201 addUnwrappedLine();
2202 break;
2203 }
Daniel Jaspera15da302013-08-28 08:04:23 +00002204 if (FormatTok->is(tok::l_brace)) {
2205 parseBlock(/*MustBeDeclaration=*/false);
2206 // In ObjC interfaces, nothing should be following the "}".
2207 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00002208 } else if (FormatTok->is(tok::r_brace)) {
2209 // Ignore stray "}". parseStructuralElement doesn't consume them.
2210 nextToken();
2211 addUnwrappedLine();
Ben Hamilton707e68f2018-05-30 15:21:38 +00002212 } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
2213 nextToken();
2214 parseObjCMethod();
Daniel Jaspera15da302013-08-28 08:04:23 +00002215 } else {
2216 parseStructuralElement();
2217 }
Nico Weber8696a8d2013-01-09 21:15:03 +00002218 } while (!eof());
2219}
2220
Nico Weber2ce0ac52013-01-09 23:25:37 +00002221void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weberc068ff72018-01-23 17:10:25 +00002222 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
2223 FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
Nico Weber7eecf4b2013-01-09 20:25:35 +00002224 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002225 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00002226
Ben Hamilton1462e842018-04-05 15:26:25 +00002227 // @interface can be followed by a lightweight generic
2228 // specialization list, then either a base class or a category.
2229 if (FormatTok->Tok.is(tok::less)) {
2230 // Unlike protocol lists, generic parameterizations support
2231 // nested angles:
2232 //
2233 // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
2234 // NSObject <NSCopying, NSSecureCoding>
2235 //
2236 // so we need to count how many open angles we have left.
2237 unsigned NumOpenAngles = 1;
2238 do {
2239 nextToken();
2240 // Early exit in case someone forgot a close angle.
2241 if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2242 FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2243 break;
2244 if (FormatTok->Tok.is(tok::less))
2245 ++NumOpenAngles;
2246 else if (FormatTok->Tok.is(tok::greater)) {
2247 assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
2248 --NumOpenAngles;
2249 }
2250 } while (!eof() && NumOpenAngles != 0);
2251 nextToken(); // Skip '>'.
2252 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002253 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00002254 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002255 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002256 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00002257 // Skip category, if present.
2258 parseParens();
2259
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002260 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002261 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00002262
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002263 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00002264 if (Style.BraceWrapping.AfterObjCDeclaration)
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002265 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00002266 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002267 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00002268
2269 // With instance variables, this puts '}' on its own line. Without instance
2270 // variables, this ends the @interface line.
2271 addUnwrappedLine();
2272
Nico Weber8696a8d2013-01-09 21:15:03 +00002273 parseObjCUntilAtEnd();
2274}
Nico Weber7eecf4b2013-01-09 20:25:35 +00002275
Nico Weberc068ff72018-01-23 17:10:25 +00002276// Returns true for the declaration/definition form of @protocol,
2277// false for the expression form.
2278bool UnwrappedLineParser::parseObjCProtocol() {
2279 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
Nico Weber8696a8d2013-01-09 21:15:03 +00002280 nextToken();
Nico Weberc068ff72018-01-23 17:10:25 +00002281
2282 if (FormatTok->is(tok::l_paren))
2283 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
2284 return false;
2285
2286 // The definition/declaration form,
2287 // @protocol Foo
2288 // - (int)someMethod;
2289 // @end
2290
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002291 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00002292
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002293 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002294 parseObjCProtocolList();
2295
2296 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002297 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002298 nextToken();
Nico Weberc068ff72018-01-23 17:10:25 +00002299 addUnwrappedLine();
2300 return true;
Nico Weber8696a8d2013-01-09 21:15:03 +00002301 }
2302
2303 addUnwrappedLine();
2304 parseObjCUntilAtEnd();
Nico Weberc068ff72018-01-23 17:10:25 +00002305 return true;
Nico Weber7eecf4b2013-01-09 20:25:35 +00002306}
2307
Daniel Jasperfca735c2015-02-19 16:14:18 +00002308void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
Martin Probst053f1aa2016-04-19 14:55:37 +00002309 bool IsImport = FormatTok->is(Keywords.kw_import);
2310 assert(IsImport || FormatTok->is(tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00002311 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00002312
Daniel Jasperec05fc72015-05-11 09:14:50 +00002313 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002314 if (FormatTok->is(tok::kw_default))
2315 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00002316
Martin Probst5f8445b2016-04-24 22:05:09 +00002317 // Consume "async function", "function" and "default function", so that these
2318 // get parsed as free-standing JS functions, i.e. do not require a trailing
2319 // semicolon.
2320 if (FormatTok->is(Keywords.kw_async))
2321 nextToken();
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002322 if (FormatTok->is(Keywords.kw_function)) {
2323 nextToken();
2324 return;
2325 }
2326
Martin Probst053f1aa2016-04-19 14:55:37 +00002327 // For imports, `export *`, `export {...}`, consume the rest of the line up
2328 // to the terminating `;`. For everything else, just return and continue
2329 // parsing the structural element, i.e. the declaration or expression for
2330 // `export default`.
2331 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
2332 !FormatTok->isStringLiteral())
2333 return;
Daniel Jasperfca735c2015-02-19 16:14:18 +00002334
Martin Probstd40bca42017-01-09 08:56:36 +00002335 while (!eof()) {
2336 if (FormatTok->is(tok::semi))
2337 return;
Krasimir Georgiev112c2e92017-11-09 13:22:03 +00002338 if (Line->Tokens.empty()) {
Martin Probstd40bca42017-01-09 08:56:36 +00002339 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
2340 // import statement should terminate.
2341 return;
2342 }
Daniel Jasperefc1a832016-01-07 08:53:35 +00002343 if (FormatTok->is(tok::l_brace)) {
2344 FormatTok->BlockKind = BK_Block;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002345 nextToken();
Daniel Jasperefc1a832016-01-07 08:53:35 +00002346 parseBracedList();
2347 } else {
2348 nextToken();
2349 }
Daniel Jasper354aa512015-02-19 16:07:32 +00002350 }
2351}
2352
Francois Ferrand6f40e212018-10-02 16:37:51 +00002353void UnwrappedLineParser::parseStatementMacro()
2354{
2355 nextToken();
2356 if (FormatTok->is(tok::l_paren))
2357 parseParens();
2358 if (FormatTok->is(tok::semi))
2359 nextToken();
2360 addUnwrappedLine();
2361}
2362
Daniel Jasper3b203a62013-09-05 16:05:56 +00002363LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
2364 StringRef Prefix = "") {
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002365 llvm::dbgs() << Prefix << "Line(" << Line.Level
2366 << ", FSC=" << Line.FirstStartColumn << ")"
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002367 << (Line.InPPDirective ? " MACRO" : "") << ": ";
2368 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2369 E = Line.Tokens.end();
2370 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002371 llvm::dbgs() << I->Tok->Tok.getName() << "["
Manuel Klimek89628f62017-09-20 09:51:03 +00002372 << "T=" << I->Tok->Type << ", OC=" << I->Tok->OriginalColumn
2373 << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002374 }
2375 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2376 E = Line.Tokens.end();
2377 I != E; ++I) {
2378 const UnwrappedLineNode &Node = *I;
2379 for (SmallVectorImpl<UnwrappedLine>::const_iterator
2380 I = Node.Children.begin(),
2381 E = Node.Children.end();
2382 I != E; ++I) {
2383 printDebugInfo(*I, "\nChild: ");
2384 }
2385 }
2386 llvm::dbgs() << "\n";
2387}
2388
Daniel Jasperf7935112012-12-03 18:12:45 +00002389void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002390 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00002391 return;
Nicola Zaghen3538b392018-05-15 13:30:56 +00002392 LLVM_DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002393 if (CurrentLines == &Lines)
2394 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00002395 });
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002396 CurrentLines->push_back(std::move(*Line));
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002397 Line->Tokens.clear();
Krasimir Georgiev85c37042017-03-01 16:38:08 +00002398 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002399 Line->FirstStartColumn = 0;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002400 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002401 CurrentLines->append(
2402 std::make_move_iterator(PreprocessorDirectives.begin()),
2403 std::make_move_iterator(PreprocessorDirectives.end()));
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002404 PreprocessorDirectives.clear();
2405 }
Manuel Klimeke411aa82017-09-20 09:29:37 +00002406 // Disconnect the current token from the last token on the previous line.
2407 FormatTok->Previous = nullptr;
Daniel Jasperf7935112012-12-03 18:12:45 +00002408}
2409
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002410bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00002411
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002412bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002413 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
2414 FormatTok.NewlinesBefore > 0;
2415}
2416
Krasimir Georgiev91834222017-01-25 13:58:58 +00002417// Checks if \p FormatTok is a line comment that continues the line comment
2418// section on \p Line.
Krasimir Georgievea222a72017-05-22 10:07:56 +00002419static bool continuesLineCommentSection(const FormatToken &FormatTok,
2420 const UnwrappedLine &Line,
2421 llvm::Regex &CommentPragmasRegex) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002422 if (Line.Tokens.empty())
2423 return false;
Krasimir Georgiev84321612017-01-30 19:18:55 +00002424
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002425 StringRef IndentContent = FormatTok.TokenText;
2426 if (FormatTok.TokenText.startswith("//") ||
2427 FormatTok.TokenText.startswith("/*"))
2428 IndentContent = FormatTok.TokenText.substr(2);
2429 if (CommentPragmasRegex.match(IndentContent))
2430 return false;
2431
Krasimir Georgiev91834222017-01-25 13:58:58 +00002432 // If Line starts with a line comment, then FormatTok continues the comment
Krasimir Georgiev84321612017-01-30 19:18:55 +00002433 // section if its original column is greater or equal to the original start
Krasimir Georgiev91834222017-01-25 13:58:58 +00002434 // column of the line.
2435 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002436 // Define the min column token of a line as follows: if a line ends in '{' or
2437 // contains a '{' followed by a line comment, then the min column token is
2438 // that '{'. Otherwise, the min column token of the line is the first token of
2439 // the line.
2440 //
2441 // If Line starts with a token other than a line comment, then FormatTok
2442 // continues the comment section if its original column is greater than the
2443 // original start column of the min column token of the line.
Krasimir Georgiev91834222017-01-25 13:58:58 +00002444 //
2445 // For example, the second line comment continues the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002446 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002447 // // first line
2448 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002449 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002450 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002451 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002452 // // first line
2453 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002454 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002455 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002456 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002457 // int i; // first line
2458 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002459 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002460 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002461 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002462 // do { // first line
2463 // // second line
2464 // int i;
2465 // } while (true);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002466 //
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002467 // and:
2468 //
2469 // enum {
2470 // a, // first line
2471 // // second line
2472 // b
2473 // };
2474 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002475 // The second line comment doesn't continue the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002476 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002477 // // first line
2478 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002479 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002480 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002481 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002482 // int i; // first line
2483 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002484 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002485 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002486 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002487 // do { // first line
2488 // // second line
2489 // int i;
2490 // } while (true);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002491 //
2492 // and:
2493 //
2494 // enum {
2495 // a, // first line
2496 // // second line
2497 // };
Krasimir Georgiev84321612017-01-30 19:18:55 +00002498 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
2499
2500 // Scan for '{//'. If found, use the column of '{' as a min column for line
2501 // comment section continuation.
2502 const FormatToken *PreviousToken = nullptr;
Krasimir Georgievd86c25d2017-03-10 13:09:29 +00002503 for (const UnwrappedLineNode &Node : Line.Tokens) {
Krasimir Georgiev84321612017-01-30 19:18:55 +00002504 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
2505 isLineComment(*Node.Tok)) {
2506 MinColumnToken = PreviousToken;
2507 break;
2508 }
2509 PreviousToken = Node.Tok;
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002510
2511 // Grab the last newline preceding a token in this unwrapped line.
2512 if (Node.Tok->NewlinesBefore > 0) {
2513 MinColumnToken = Node.Tok;
2514 }
Krasimir Georgiev84321612017-01-30 19:18:55 +00002515 }
2516 if (PreviousToken && PreviousToken->is(tok::l_brace)) {
2517 MinColumnToken = PreviousToken;
2518 }
2519
Krasimir Georgievea222a72017-05-22 10:07:56 +00002520 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
2521 MinColumnToken);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002522}
2523
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002524void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
2525 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002526 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002527 I = CommentsBeforeNextToken.begin(),
2528 E = CommentsBeforeNextToken.end();
2529 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002530 // Line comments that belong to the same line comment section are put on the
2531 // same line since later we might want to reflow content between them.
Krasimir Georgiev753625b2017-01-31 13:32:38 +00002532 // Additional fine-grained breaking of line comment sections is controlled
2533 // by the class BreakableLineCommentSection in case it is desirable to keep
2534 // several line comment sections in the same unwrapped line.
2535 //
2536 // FIXME: Consider putting separate line comment sections as children to the
2537 // unwrapped line instead.
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002538 (*I)->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002539 continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002540 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002541 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002542 pushToken(*I);
2543 }
Daniel Jaspere60cba12015-05-13 11:35:53 +00002544 if (NewlineBeforeNext && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002545 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002546 CommentsBeforeNextToken.clear();
2547}
2548
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002549void UnwrappedLineParser::nextToken(int LevelDifference) {
Daniel Jasperf7935112012-12-03 18:12:45 +00002550 if (eof())
2551 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002552 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002553 pushToken(FormatTok);
Manuel Klimek89628f62017-09-20 09:51:03 +00002554 FormatToken *Previous = FormatTok;
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002555 if (Style.Language != FormatStyle::LK_JavaScript)
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002556 readToken(LevelDifference);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002557 else
2558 readTokenWithJavaScriptASI();
Manuel Klimeke411aa82017-09-20 09:29:37 +00002559 FormatTok->Previous = Previous;
Daniel Jasperb9a49902016-01-09 15:56:28 +00002560}
2561
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002562void UnwrappedLineParser::distributeComments(
2563 const SmallVectorImpl<FormatToken *> &Comments,
2564 const FormatToken *NextTok) {
2565 // Whether or not a line comment token continues a line is controlled by
Krasimir Georgievea222a72017-05-22 10:07:56 +00002566 // the method continuesLineCommentSection, with the following caveat:
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002567 //
2568 // Define a trail of Comments to be a nonempty proper postfix of Comments such
2569 // that each comment line from the trail is aligned with the next token, if
2570 // the next token exists. If a trail exists, the beginning of the maximal
2571 // trail is marked as a start of a new comment section.
2572 //
2573 // For example in this code:
2574 //
2575 // int a; // line about a
2576 // // line 1 about b
2577 // // line 2 about b
2578 // int b;
2579 //
2580 // the two lines about b form a maximal trail, so there are two sections, the
2581 // first one consisting of the single comment "// line about a" and the
2582 // second one consisting of the next two comments.
2583 if (Comments.empty())
2584 return;
2585 bool ShouldPushCommentsInCurrentLine = true;
2586 bool HasTrailAlignedWithNextToken = false;
2587 unsigned StartOfTrailAlignedWithNextToken = 0;
2588 if (NextTok) {
2589 // We are skipping the first element intentionally.
2590 for (unsigned i = Comments.size() - 1; i > 0; --i) {
2591 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
2592 HasTrailAlignedWithNextToken = true;
2593 StartOfTrailAlignedWithNextToken = i;
2594 }
2595 }
2596 }
2597 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
2598 FormatToken *FormatTok = Comments[i];
Manuel Klimek89628f62017-09-20 09:51:03 +00002599 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002600 FormatTok->ContinuesLineCommentSection = false;
2601 } else {
2602 FormatTok->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002603 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002604 }
2605 if (!FormatTok->ContinuesLineCommentSection &&
2606 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
2607 ShouldPushCommentsInCurrentLine = false;
2608 }
2609 if (ShouldPushCommentsInCurrentLine) {
2610 pushToken(FormatTok);
2611 } else {
2612 CommentsBeforeNextToken.push_back(FormatTok);
2613 }
2614 }
2615}
2616
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002617void UnwrappedLineParser::readToken(int LevelDifference) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002618 SmallVector<FormatToken *, 1> Comments;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002619 do {
2620 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00002621 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002622 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
2623 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002624 distributeComments(Comments, FormatTok);
2625 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002626 // If there is an unfinished unwrapped line, we flush the preprocessor
2627 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00002628 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002629 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002630 assert((LevelDifference >= 0 ||
2631 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2632 "LevelDifference makes Line->Level negative");
2633 Line->Level += LevelDifference;
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00002634 // Comments stored before the preprocessor directive need to be output
2635 // before the preprocessor directive, at the same level as the
2636 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002637 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002638 parsePPDirective();
2639 }
Manuel Klimek68b03042014-04-14 09:14:11 +00002640 while (FormatTok->Type == TT_ConflictStart ||
2641 FormatTok->Type == TT_ConflictEnd ||
2642 FormatTok->Type == TT_ConflictAlternative) {
2643 if (FormatTok->Type == TT_ConflictStart) {
2644 conditionalCompilationStart(/*Unreachable=*/false);
2645 } else if (FormatTok->Type == TT_ConflictAlternative) {
2646 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002647 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00002648 conditionalCompilationEnd();
2649 }
2650 FormatTok = Tokens->getNextToken();
2651 FormatTok->MustBreakBefore = true;
2652 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002653
Francois Ferranda98a95c2017-07-28 07:56:14 +00002654 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002655 !Line->InPPDirective) {
2656 continue;
2657 }
2658
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002659 if (!FormatTok->Tok.is(tok::comment)) {
2660 distributeComments(Comments, FormatTok);
2661 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002662 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002663 }
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002664
2665 Comments.push_back(FormatTok);
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002666 } while (!eof());
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002667
2668 distributeComments(Comments, nullptr);
2669 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002670}
2671
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002672void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002673 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002674 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002675 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002676 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00002677 }
Daniel Jasperf7935112012-12-03 18:12:45 +00002678}
2679
Daniel Jasper8d1832e2013-01-07 13:26:07 +00002680} // end namespace format
2681} // end namespace clang