blob: e5afa1264abb5f5870ed7da4a77e224e224ce0b2 [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000011/// This file contains the implementation of the UnwrappedLineParser,
Daniel Jasperf7935112012-12-03 18:12:45 +000012/// which turns a stream of tokens into UnwrappedLines.
13///
Daniel Jasperf7935112012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Chandler Carruth4b417452013-01-19 08:09:44 +000016#include "UnwrappedLineParser.h"
Benjamin Kramer33335df2015-03-01 21:36:40 +000017#include "llvm/ADT/STLExtras.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000018#include "llvm/Support/Debug.h"
Benjamin Kramer53f5e892015-03-23 18:05:43 +000019#include "llvm/Support/raw_ostream.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000020
Martin Probst7e0f25b2017-11-25 09:19:42 +000021#include <algorithm>
22
Chandler Carruth10346662014-04-22 03:17:02 +000023#define DEBUG_TYPE "format-parser"
24
Daniel Jasperf7935112012-12-03 18:12:45 +000025namespace clang {
26namespace format {
27
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000028class FormatTokenSource {
29public:
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000030 virtual ~FormatTokenSource() {}
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000031 virtual FormatToken *getNextToken() = 0;
32
33 virtual unsigned getPosition() = 0;
34 virtual FormatToken *setPosition(unsigned Position) = 0;
35};
36
Craig Topper69665e12013-07-01 04:21:54 +000037namespace {
38
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000039class ScopedDeclarationState {
40public:
41 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
42 bool MustBeDeclaration)
43 : Line(Line), Stack(Stack) {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000044 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek39080572013-01-23 11:03:04 +000045 Stack.push_back(MustBeDeclaration);
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000046 }
47 ~ScopedDeclarationState() {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000048 Stack.pop_back();
Manuel Klimekc1237a82013-01-23 14:08:21 +000049 if (!Stack.empty())
50 Line.MustBeDeclaration = Stack.back();
51 else
52 Line.MustBeDeclaration = true;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000053 }
Daniel Jasper393564f2013-05-31 14:56:29 +000054
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000055private:
56 UnwrappedLine &Line;
57 std::vector<bool> &Stack;
58};
59
Krasimir Georgieva1c30932017-05-19 10:34:57 +000060static bool isLineComment(const FormatToken &FormatTok) {
Krasimir Georgiev410ed242017-11-10 12:50:09 +000061 return FormatTok.is(tok::comment) && !FormatTok.TokenText.startswith("/*");
Krasimir Georgieva1c30932017-05-19 10:34:57 +000062}
63
Krasimir Georgievea222a72017-05-22 10:07:56 +000064// Checks if \p FormatTok is a line comment that continues the line comment
65// \p Previous. The original column of \p MinColumnToken is used to determine
66// whether \p FormatTok is indented enough to the right to continue \p Previous.
67static bool continuesLineComment(const FormatToken &FormatTok,
68 const FormatToken *Previous,
69 const FormatToken *MinColumnToken) {
70 if (!Previous || !MinColumnToken)
71 return false;
72 unsigned MinContinueColumn =
73 MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
74 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
75 isLineComment(*Previous) &&
76 FormatTok.OriginalColumn >= MinContinueColumn;
77}
78
Manuel Klimek1abf7892013-01-04 23:34:14 +000079class ScopedMacroState : public FormatTokenSource {
80public:
81 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek20e0af62015-05-06 11:56:29 +000082 FormatToken *&ResetToken)
Manuel Klimek1abf7892013-01-04 23:34:14 +000083 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000084 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
Krasimir Georgieva1c30932017-05-19 10:34:57 +000085 Token(nullptr), PreviousToken(nullptr) {
David L. Jones5de22722018-06-15 06:08:54 +000086 FakeEOF.Tok.startToken();
87 FakeEOF.Tok.setKind(tok::eof);
Manuel Klimek1abf7892013-01-04 23:34:14 +000088 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000089 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000090 Line.InPPDirective = true;
91 }
92
Alexander Kornienko34eb2072015-04-11 02:00:23 +000093 ~ScopedMacroState() override {
Manuel Klimek1abf7892013-01-04 23:34:14 +000094 TokenSource = PreviousTokenSource;
95 ResetToken = Token;
96 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000097 Line.Level = PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +000098 }
99
Craig Topperfb6b25b2014-03-15 04:29:04 +0000100 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +0000101 // The \c UnwrappedLineParser guards against this by never calling
102 // \c getNextToken() after it has encountered the first eof token.
103 assert(!eof());
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000104 PreviousToken = Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000105 Token = PreviousTokenSource->getNextToken();
106 if (eof())
David L. Jones5de22722018-06-15 06:08:54 +0000107 return &FakeEOF;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000108 return Token;
109 }
110
Craig Topperfb6b25b2014-03-15 04:29:04 +0000111 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +0000112
Craig Topperfb6b25b2014-03-15 04:29:04 +0000113 FormatToken *setPosition(unsigned Position) override {
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000114 PreviousToken = nullptr;
Manuel Klimekab419912013-05-23 09:41:43 +0000115 Token = PreviousTokenSource->setPosition(Position);
116 return Token;
117 }
118
Manuel Klimek1abf7892013-01-04 23:34:14 +0000119private:
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000120 bool eof() {
121 return Token && Token->HasUnescapedNewline &&
Krasimir Georgievea222a72017-05-22 10:07:56 +0000122 !continuesLineComment(*Token, PreviousToken,
123 /*MinColumnToken=*/PreviousToken);
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000124 }
Manuel Klimek1abf7892013-01-04 23:34:14 +0000125
David L. Jones5de22722018-06-15 06:08:54 +0000126 FormatToken FakeEOF;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000127 UnwrappedLine &Line;
128 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000129 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000130 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000131 FormatTokenSource *PreviousTokenSource;
132
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000133 FormatToken *Token;
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000134 FormatToken *PreviousToken;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000135};
136
Craig Topper69665e12013-07-01 04:21:54 +0000137} // end anonymous namespace
138
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000139class ScopedLineState {
140public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000141 ScopedLineState(UnwrappedLineParser &Parser,
142 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000143 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000144 if (SwitchToPreprocessorLines)
145 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000146 else if (!Parser.Line->Tokens.empty())
147 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000148 PreBlockLine = std::move(Parser.Line);
149 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000150 Parser.Line->Level = PreBlockLine->Level;
151 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000152 }
153
154 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000155 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000156 Parser.addUnwrappedLine();
157 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000158 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000159 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000160 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
161 Parser.MustBreakBeforeNextToken = true;
162 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000163 }
164
165private:
166 UnwrappedLineParser &Parser;
167
David Blaikieefb6eb22014-08-09 20:02:07 +0000168 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000169 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000170};
171
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000172class CompoundStatementIndenter {
173public:
174 CompoundStatementIndenter(UnwrappedLineParser *Parser,
175 const FormatStyle &Style, unsigned &LineLevel)
176 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000177 if (Style.BraceWrapping.AfterControlStatement)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000178 Parser->addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000179 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000180 ++LineLevel;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000181 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000182 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000183
184private:
185 unsigned &LineLevel;
186 unsigned OldLineLevel;
187};
188
Craig Topper69665e12013-07-01 04:21:54 +0000189namespace {
190
Manuel Klimekab419912013-05-23 09:41:43 +0000191class IndexedTokenSource : public FormatTokenSource {
192public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000193 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000194 : Tokens(Tokens), Position(-1) {}
195
Craig Topperfb6b25b2014-03-15 04:29:04 +0000196 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000197 ++Position;
198 return Tokens[Position];
199 }
200
Craig Topperfb6b25b2014-03-15 04:29:04 +0000201 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000202 assert(Position >= 0);
203 return Position;
204 }
205
Craig Topperfb6b25b2014-03-15 04:29:04 +0000206 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000207 Position = P;
208 return Tokens[Position];
209 }
210
Manuel Klimek71814b42013-10-11 21:25:45 +0000211 void reset() { Position = -1; }
212
Manuel Klimekab419912013-05-23 09:41:43 +0000213private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000214 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000215 int Position;
216};
217
Craig Topper69665e12013-07-01 04:21:54 +0000218} // end anonymous namespace
219
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000220UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000221 const AdditionalKeywords &Keywords,
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000222 unsigned FirstStartColumn,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000223 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000224 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000225 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000226 CurrentLines(&Lines), Style(Style), Keywords(Keywords),
227 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
Krasimir Georgievad47c902017-08-30 14:34:57 +0000228 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000229 IncludeGuard(Style.IndentPPDirectives == FormatStyle::PPDIS_None
230 ? IG_Rejected
231 : IG_Inited),
232 IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000233
234void UnwrappedLineParser::reset() {
235 PPBranchLevel = -1;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000236 IncludeGuard = Style.IndentPPDirectives == FormatStyle::PPDIS_None
237 ? IG_Rejected
238 : IG_Inited;
239 IncludeGuardToken = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000240 Line.reset(new UnwrappedLine);
241 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000242 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000243 MustBreakBeforeNextToken = false;
244 PreprocessorDirectives.clear();
245 CurrentLines = &Lines;
246 DeclarationScopeStack.clear();
Manuel Klimek71814b42013-10-11 21:25:45 +0000247 PPStack.clear();
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000248 Line->FirstStartColumn = FirstStartColumn;
Manuel Klimek71814b42013-10-11 21:25:45 +0000249}
Daniel Jasperf7935112012-12-03 18:12:45 +0000250
Manuel Klimek20e0af62015-05-06 11:56:29 +0000251void UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000252 IndexedTokenSource TokenSource(AllTokens);
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000253 Line->FirstStartColumn = FirstStartColumn;
Manuel Klimek71814b42013-10-11 21:25:45 +0000254 do {
Nicola Zaghen3538b392018-05-15 13:30:56 +0000255 LLVM_DEBUG(llvm::dbgs() << "----\n");
Manuel Klimek71814b42013-10-11 21:25:45 +0000256 reset();
257 Tokens = &TokenSource;
258 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000259
Manuel Klimek71814b42013-10-11 21:25:45 +0000260 readToken();
261 parseFile();
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000262
263 // If we found an include guard then all preprocessor directives (other than
264 // the guard) are over-indented by one.
265 if (IncludeGuard == IG_Found)
266 for (auto &Line : Lines)
267 if (Line.InPPDirective && Line.Level > 0)
268 --Line.Level;
269
Manuel Klimek71814b42013-10-11 21:25:45 +0000270 // Create line with eof token.
271 pushToken(FormatTok);
272 addUnwrappedLine();
273
274 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
275 E = Lines.end();
276 I != E; ++I) {
277 Callback.consumeUnwrappedLine(*I);
278 }
279 Callback.finishRun();
280 Lines.clear();
281 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000282 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000283 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
284 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
285 }
286 if (!PPLevelBranchIndex.empty()) {
287 ++PPLevelBranchIndex.back();
288 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
289 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
290 }
291 } while (!PPLevelBranchIndex.empty());
Manuel Klimek1abf7892013-01-04 23:34:14 +0000292}
293
Manuel Klimek1a18c402013-04-12 14:13:36 +0000294void UnwrappedLineParser::parseFile() {
Daniel Jasper9326f912015-05-05 08:40:32 +0000295 // The top-level context in a file always has declarations, except for pre-
296 // processor directives and JavaScript files.
297 bool MustBeDeclaration =
298 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
299 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
300 MustBeDeclaration);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000301 if (Style.Language == FormatStyle::LK_TextProto)
302 parseBracedList();
303 else
304 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000305 // Make sure to format the remaining tokens.
Krasimir Georgiev0895f5e2018-06-25 11:08:24 +0000306 //
307 // LK_TextProto is special since its top-level is parsed as the body of a
308 // braced list, which does not necessarily have natural line separators such
309 // as a semicolon. Comments after the last entry that have been determined to
310 // not belong to that line, as in:
311 // key: value
312 // // endfile comment
313 // do not have a chance to be put on a line of their own until this point.
314 // Here we add this newline before end-of-file comments.
315 if (Style.Language == FormatStyle::LK_TextProto &&
316 !CommentsBeforeNextToken.empty())
317 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000318 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000319 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000320}
321
Manuel Klimek1a18c402013-04-12 14:13:36 +0000322void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000323 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000324 do {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000325 tok::TokenKind kind = FormatTok->Tok.getKind();
326 if (FormatTok->Type == TT_MacroBlockBegin) {
327 kind = tok::l_brace;
328 } else if (FormatTok->Type == TT_MacroBlockEnd) {
329 kind = tok::r_brace;
330 }
331
332 switch (kind) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000333 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000334 nextToken();
335 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000336 break;
337 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000338 // FIXME: Add parameter whether this can happen - if this happens, we must
339 // be in a non-declaration context.
Daniel Jasperb86e2722015-08-24 13:23:37 +0000340 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
341 continue;
Nico Weber9096fc02013-06-26 00:30:14 +0000342 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000343 addUnwrappedLine();
344 break;
345 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000346 if (HasOpeningBrace)
347 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000348 nextToken();
349 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000350 break;
Nico Weberc29f83b2018-01-23 16:30:56 +0000351 case tok::kw_default: {
352 unsigned StoredPosition = Tokens->getPosition();
353 FormatToken *Next = Tokens->getNextToken();
354 FormatTok = Tokens->setPosition(StoredPosition);
355 if (Next && Next->isNot(tok::colon)) {
356 // default not followed by ':' is not a case label; treat it like
357 // an identifier.
358 parseStructuralElement();
359 break;
360 }
361 // Else, if it is 'default:', fall through to the case handling.
Nico Weberf1add5e2018-01-24 01:47:22 +0000362 LLVM_FALLTHROUGH;
Nico Weberc29f83b2018-01-23 16:30:56 +0000363 }
Daniel Jasper516d7972013-07-25 11:31:57 +0000364 case tok::kw_case:
Manuel Klimek89628f62017-09-20 09:51:03 +0000365 if (Style.Language == FormatStyle::LK_JavaScript &&
366 Line->MustBeDeclaration) {
Martin Probstf785fd92017-08-04 17:07:15 +0000367 // A 'case: string' style field declaration.
368 parseStructuralElement();
369 break;
370 }
Daniel Jasper72407622013-09-02 08:26:29 +0000371 if (!SwitchLabelEncountered &&
372 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
373 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000374 SwitchLabelEncountered = true;
375 parseStructuralElement();
376 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000377 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000378 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000379 break;
380 }
381 } while (!eof());
382}
383
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000384void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
Manuel Klimekab419912013-05-23 09:41:43 +0000385 // We'll parse forward through the tokens until we hit
386 // a closing brace or eof - note that getNextToken() will
387 // parse macros, so this will magically work inside macro
388 // definitions, too.
389 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000390 FormatToken *Tok = FormatTok;
Manuel Klimek89628f62017-09-20 09:51:03 +0000391 const FormatToken *PrevTok = Tok->Previous;
Manuel Klimekab419912013-05-23 09:41:43 +0000392 // Keep a stack of positions of lbrace tokens. We will
393 // update information about whether an lbrace starts a
394 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000395 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000396 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000397 do {
Daniel Jaspereb65e912015-12-21 18:31:15 +0000398 // Get next non-comment token.
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000399 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000400 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000401 do {
402 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000403 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000404 } while (NextTok->is(tok::comment));
405
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000406 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000407 case tok::l_brace:
Martin Probst95ed8e72017-05-31 09:29:40 +0000408 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) {
Martin Probste8e27ca2017-11-25 09:33:47 +0000409 if (PrevTok->isOneOf(tok::colon, tok::less))
410 // A ':' indicates this code is in a type, or a braced list
411 // following a label in an object literal ({a: {b: 1}}).
412 // A '<' could be an object used in a comparison, but that is nonsense
413 // code (can never return true), so more likely it is a generic type
414 // argument (`X<{a: string; b: number}>`).
415 // The code below could be confused by semicolons between the
416 // individual members in a type member list, which would normally
417 // trigger BK_Block. In both cases, this must be parsed as an inline
418 // braced init.
Martin Probst95ed8e72017-05-31 09:29:40 +0000419 Tok->BlockKind = BK_BracedInit;
420 else if (PrevTok->is(tok::r_paren))
421 // `) { }` can only occur in function or method declarations in JS.
422 Tok->BlockKind = BK_Block;
423 } else {
Daniel Jasperb9a49902016-01-09 15:56:28 +0000424 Tok->BlockKind = BK_Unknown;
Martin Probst95ed8e72017-05-31 09:29:40 +0000425 }
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000426 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000427 break;
428 case tok::r_brace:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000429 if (LBraceStack.empty())
430 break;
431 if (LBraceStack.back()->BlockKind == BK_Unknown) {
432 bool ProbablyBracedList = false;
433 if (Style.Language == FormatStyle::LK_Proto) {
434 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
435 } else {
436 // Using OriginalColumn to distinguish between ObjC methods and
437 // binary operators is a bit hacky.
438 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
439 NextTok->OriginalColumn == 0;
Daniel Jasper91b032a2014-05-22 12:46:38 +0000440
Daniel Jasperb9a49902016-01-09 15:56:28 +0000441 // If there is a comma, semicolon or right paren after the closing
442 // brace, we assume this is a braced initializer list. Note that
443 // regardless how we mark inner braces here, we will overwrite the
444 // BlockKind later if we parse a braced list (where all blocks
445 // inside are by default braced lists), or when we explicitly detect
446 // blocks (for example while parsing lambdas).
Martin Probst95ed8e72017-05-31 09:29:40 +0000447 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
448 // braced list in JS.
Daniel Jasperb9a49902016-01-09 15:56:28 +0000449 ProbablyBracedList =
Daniel Jasperacffeb82016-03-05 18:34:26 +0000450 (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probste1e12a72016-08-19 14:35:01 +0000451 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
452 Keywords.kw_as)) ||
Martin Probstb7fb2672017-05-10 13:53:29 +0000453 (Style.isCpp() && NextTok->is(tok::l_paren)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000454 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
455 tok::r_paren, tok::r_square, tok::l_brace,
Manuel Klimekd0f3fe52018-04-11 14:51:54 +0000456 tok::ellipsis) ||
Daniel Jaspere4ada022016-12-13 10:05:03 +0000457 (NextTok->is(tok::identifier) &&
458 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000459 (NextTok->is(tok::semi) &&
460 (!ExpectClassBody || LBraceStack.size() != 1)) ||
461 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Manuel Klimekd0f3fe52018-04-11 14:51:54 +0000462 if (NextTok->is(tok::l_square)) {
463 // We can have an array subscript after a braced init
464 // list, but C++11 attributes are expected after blocks.
465 NextTok = Tokens->getNextToken();
466 ++ReadTokens;
467 ProbablyBracedList = NextTok->isNot(tok::l_square);
468 }
Manuel Klimekab419912013-05-23 09:41:43 +0000469 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000470 if (ProbablyBracedList) {
471 Tok->BlockKind = BK_BracedInit;
472 LBraceStack.back()->BlockKind = BK_BracedInit;
473 } else {
474 Tok->BlockKind = BK_Block;
475 LBraceStack.back()->BlockKind = BK_Block;
476 }
Manuel Klimekab419912013-05-23 09:41:43 +0000477 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000478 LBraceStack.pop_back();
Manuel Klimekab419912013-05-23 09:41:43 +0000479 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000480 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000481 case tok::semi:
482 case tok::kw_if:
483 case tok::kw_while:
484 case tok::kw_for:
485 case tok::kw_switch:
486 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000487 case tok::kw___try:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000488 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000489 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000490 break;
491 default:
492 break;
493 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000494 PrevTok = Tok;
Manuel Klimekab419912013-05-23 09:41:43 +0000495 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000496 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Daniel Jasperb9a49902016-01-09 15:56:28 +0000497
Manuel Klimekab419912013-05-23 09:41:43 +0000498 // Assume other blocks for all unclosed opening braces.
499 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000500 if (LBraceStack[i]->BlockKind == BK_Unknown)
501 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000502 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000503
Manuel Klimekab419912013-05-23 09:41:43 +0000504 FormatTok = Tokens->setPosition(StoredPosition);
505}
506
Francois Ferranda98a95c2017-07-28 07:56:14 +0000507template <class T>
508static inline void hash_combine(std::size_t &seed, const T &v) {
509 std::hash<T> hasher;
510 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
511}
512
513size_t UnwrappedLineParser::computePPHash() const {
514 size_t h = 0;
515 for (const auto &i : PPStack) {
516 hash_combine(h, size_t(i.Kind));
517 hash_combine(h, i.Line);
518 }
519 return h;
520}
521
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000522void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
523 bool MunchSemi) {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000524 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
525 "'{' or macro block token expected");
526 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
Daniel Jaspereb65e912015-12-21 18:31:15 +0000527 FormatTok->BlockKind = BK_Block;
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000528
Francois Ferranda98a95c2017-07-28 07:56:14 +0000529 size_t PPStartHash = computePPHash();
530
Daniel Jasper516d7972013-07-25 11:31:57 +0000531 unsigned InitialLevel = Line->Level;
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000532 nextToken(/*LevelDifference=*/AddLevel ? 1 : 0);
Daniel Jasperf7935112012-12-03 18:12:45 +0000533
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000534 if (MacroBlock && FormatTok->is(tok::l_paren))
535 parseParens();
536
Francois Ferranda98a95c2017-07-28 07:56:14 +0000537 size_t NbPreprocessorDirectives =
538 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000539 addUnwrappedLine();
Francois Ferranda98a95c2017-07-28 07:56:14 +0000540 size_t OpeningLineIndex =
541 CurrentLines->empty()
542 ? (UnwrappedLine::kInvalidIndex)
543 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
Daniel Jasperf7935112012-12-03 18:12:45 +0000544
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000545 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
546 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000547 if (AddLevel)
548 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000549 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000550
Marianne Mailhot-Sarrasin03137c62016-04-14 14:56:49 +0000551 if (eof())
552 return;
553
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000554 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
555 : !FormatTok->is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000556 Line->Level = InitialLevel;
Daniel Jaspereb65e912015-12-21 18:31:15 +0000557 FormatTok->BlockKind = BK_Block;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000558 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000559 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000560
Francois Ferranda98a95c2017-07-28 07:56:14 +0000561 size_t PPEndHash = computePPHash();
562
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000563 // Munch the closing brace.
564 nextToken(/*LevelDifference=*/AddLevel ? -1 : 0);
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000565
566 if (MacroBlock && FormatTok->is(tok::l_paren))
567 parseParens();
568
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000569 if (MunchSemi && FormatTok->Tok.is(tok::semi))
570 nextToken();
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000571 Line->Level = InitialLevel;
Francois Ferranda98a95c2017-07-28 07:56:14 +0000572
573 if (PPStartHash == PPEndHash) {
574 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
575 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
576 // Update the opening line to add the forward reference as well
Manuel Klimek0dddcf72018-04-23 09:34:26 +0000577 (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
Francois Ferranda98a95c2017-07-28 07:56:14 +0000578 CurrentLines->size() - 1;
579 }
Francois Ferrande56a8292017-06-14 12:29:47 +0000580 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000581}
582
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000583static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000584 // FIXME: Closure-library specific stuff should not be hard-coded but be
585 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000586 if (Line.Tokens.size() < 4)
587 return false;
588 auto I = Line.Tokens.begin();
589 if (I->Tok->TokenText != "goog")
590 return false;
591 ++I;
592 if (I->Tok->isNot(tok::period))
593 return false;
594 ++I;
595 if (I->Tok->TokenText != "scope")
596 return false;
597 ++I;
598 return I->Tok->is(tok::l_paren);
599}
600
Martin Probst101ec892017-05-09 20:04:09 +0000601static bool isIIFE(const UnwrappedLine &Line,
602 const AdditionalKeywords &Keywords) {
603 // Look for the start of an immediately invoked anonymous function.
604 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
605 // This is commonly done in JavaScript to create a new, anonymous scope.
606 // Example: (function() { ... })()
607 if (Line.Tokens.size() < 3)
608 return false;
609 auto I = Line.Tokens.begin();
610 if (I->Tok->isNot(tok::l_paren))
611 return false;
612 ++I;
613 if (I->Tok->isNot(Keywords.kw_function))
614 return false;
615 ++I;
616 return I->Tok->is(tok::l_paren);
617}
618
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000619static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
620 const FormatToken &InitialToken) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000621 if (InitialToken.is(tok::kw_namespace))
622 return Style.BraceWrapping.AfterNamespace;
623 if (InitialToken.is(tok::kw_class))
624 return Style.BraceWrapping.AfterClass;
625 if (InitialToken.is(tok::kw_union))
626 return Style.BraceWrapping.AfterUnion;
627 if (InitialToken.is(tok::kw_struct))
628 return Style.BraceWrapping.AfterStruct;
629 return false;
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000630}
631
Manuel Klimek516e0542013-09-04 13:25:30 +0000632void UnwrappedLineParser::parseChildBlock() {
633 FormatTok->BlockKind = BK_Block;
634 nextToken();
635 {
Manuel Klimek89628f62017-09-20 09:51:03 +0000636 bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript &&
637 (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
Manuel Klimek516e0542013-09-04 13:25:30 +0000638 ScopedLineState LineState(*this);
639 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
640 /*MustBeDeclaration=*/false);
Martin Probst101ec892017-05-09 20:04:09 +0000641 Line->Level += SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000642 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000643 flushComments(isOnNewLine(*FormatTok));
Martin Probst101ec892017-05-09 20:04:09 +0000644 Line->Level -= SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000645 }
646 nextToken();
647}
648
Daniel Jasperf7935112012-12-03 18:12:45 +0000649void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000650 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000651 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000652 nextToken();
653
Craig Topper2145bc02014-05-09 08:15:10 +0000654 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000655 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000656 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000657 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000658
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000659 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000660 case tok::pp_define:
661 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000662 return;
663 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000664 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000665 break;
666 case tok::pp_ifdef:
667 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000668 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000669 break;
670 case tok::pp_else:
671 parsePPElse();
672 break;
673 case tok::pp_elif:
674 parsePPElIf();
675 break;
676 case tok::pp_endif:
677 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000678 break;
679 default:
680 parsePPUnknown();
681 break;
682 }
683}
684
Manuel Klimek68b03042014-04-14 09:14:11 +0000685void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
Francois Ferranda98a95c2017-07-28 07:56:14 +0000686 size_t Line = CurrentLines->size();
687 if (CurrentLines == &PreprocessorDirectives)
688 Line += Lines.size();
689
690 if (Unreachable ||
691 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
692 PPStack.push_back({PP_Unreachable, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000693 else
Francois Ferranda98a95c2017-07-28 07:56:14 +0000694 PPStack.push_back({PP_Conditional, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000695}
696
Manuel Klimek68b03042014-04-14 09:14:11 +0000697void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000698 ++PPBranchLevel;
699 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
700 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
701 PPLevelBranchIndex.push_back(0);
702 PPLevelBranchCount.push_back(0);
703 }
704 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000705 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
706 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000707}
708
Manuel Klimek68b03042014-04-14 09:14:11 +0000709void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000710 if (!PPStack.empty())
711 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000712 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
713 if (!PPChainBranchIndex.empty())
714 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000715 conditionalCompilationCondition(
716 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
717 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000718}
719
Manuel Klimek68b03042014-04-14 09:14:11 +0000720void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000721 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
722 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
723 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000724 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
725 }
726 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000727 // Guard against #endif's without #if.
Krasimir Georgievad47c902017-08-30 14:34:57 +0000728 if (PPBranchLevel > -1)
Manuel Klimek14bd9172014-01-29 08:49:02 +0000729 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000730 if (!PPChainBranchIndex.empty())
731 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000732 if (!PPStack.empty())
733 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000734}
735
736void UnwrappedLineParser::parsePPIf(bool IfDef) {
Daniel Jasper62703eb2017-03-01 11:10:11 +0000737 bool IfNDef = FormatTok->is(tok::pp_ifndef);
Manuel Klimek68b03042014-04-14 09:14:11 +0000738 nextToken();
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000739 bool Unreachable = false;
740 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
741 Unreachable = true;
Daniel Jasper62703eb2017-03-01 11:10:11 +0000742 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000743 Unreachable = true;
744 conditionalCompilationStart(Unreachable);
Krasimir Georgievad47c902017-08-30 14:34:57 +0000745 FormatToken *IfCondition = FormatTok;
746 // If there's a #ifndef on the first line, and the only lines before it are
747 // comments, it could be an include guard.
748 bool MaybeIncludeGuard = IfNDef;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000749 if (IncludeGuard == IG_Inited && MaybeIncludeGuard)
Krasimir Georgievad47c902017-08-30 14:34:57 +0000750 for (auto &Line : Lines) {
751 if (!Line.Tokens.front().Tok->is(tok::comment)) {
752 MaybeIncludeGuard = false;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000753 IncludeGuard = IG_Rejected;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000754 break;
755 }
756 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000757 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000758 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000759 ++PPBranchLevel;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000760 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
761 IncludeGuard = IG_IfNdefed;
762 IncludeGuardToken = IfCondition;
763 }
Manuel Klimek68b03042014-04-14 09:14:11 +0000764}
765
766void UnwrappedLineParser::parsePPElse() {
Krasimir Georgievad47c902017-08-30 14:34:57 +0000767 // If a potential include guard has an #else, it's not an include guard.
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000768 if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
769 IncludeGuard = IG_Rejected;
Manuel Klimek68b03042014-04-14 09:14:11 +0000770 conditionalCompilationAlternative();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000771 if (PPBranchLevel > -1)
772 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000773 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000774 ++PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000775}
776
777void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
778
779void UnwrappedLineParser::parsePPEndIf() {
780 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000781 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000782 // If the #endif of a potential include guard is the last thing in the file,
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000783 // then we found an include guard.
Krasimir Georgievad47c902017-08-30 14:34:57 +0000784 unsigned TokenPosition = Tokens->getPosition();
785 FormatToken *PeekNext = AllTokens[TokenPosition];
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000786 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
787 PeekNext->is(tok::eof) &&
Daniel Jasper4df130f2017-09-04 13:33:52 +0000788 Style.IndentPPDirectives != FormatStyle::PPDIS_None)
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000789 IncludeGuard = IG_Found;
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000790}
791
Manuel Klimek1abf7892013-01-04 23:34:14 +0000792void UnwrappedLineParser::parsePPDefine() {
793 nextToken();
794
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000795 if (FormatTok->Tok.getKind() != tok::identifier) {
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000796 IncludeGuard = IG_Rejected;
797 IncludeGuardToken = nullptr;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000798 parsePPUnknown();
799 return;
800 }
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000801
802 if (IncludeGuard == IG_IfNdefed &&
803 IncludeGuardToken->TokenText == FormatTok->TokenText) {
804 IncludeGuard = IG_Defined;
805 IncludeGuardToken = nullptr;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000806 for (auto &Line : Lines) {
807 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000808 IncludeGuard = IG_Rejected;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000809 break;
810 }
811 }
812 }
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000813
Manuel Klimek1abf7892013-01-04 23:34:14 +0000814 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000815 if (FormatTok->Tok.getKind() == tok::l_paren &&
816 FormatTok->WhitespaceRange.getBegin() ==
817 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000818 parseParens();
819 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000820 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
821 Line->Level += PPBranchLevel + 1;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000822 addUnwrappedLine();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000823 ++Line->Level;
Manuel Klimek1b896292013-01-07 09:34:28 +0000824
825 // Errors during a preprocessor directive can only affect the layout of the
826 // preprocessor directive, and thus we ignore them. An alternative approach
827 // would be to use the same approach we use on the file level (no
828 // re-indentation if there was a structural error) within the macro
829 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000830 parseFile();
831}
832
833void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000834 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000835 nextToken();
836 } while (!eof());
Krasimir Georgievad47c902017-08-30 14:34:57 +0000837 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
838 Line->Level += PPBranchLevel + 1;
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000839 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000840}
841
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000842// Here we blacklist certain tokens that are not usually the first token in an
843// unwrapped line. This is used in attempt to distinguish macro calls without
844// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000845static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000846 // Semicolon can be a null-statement, l_square can be a start of a macro or
847 // a C++11 attribute, but this doesn't seem to be common.
848 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
849 Tok.isNot(tok::l_square) &&
850 // Tokens that can only be used as binary operators and a part of
851 // overloaded operator names.
852 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
853 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
854 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
855 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
856 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
857 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
858 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
859 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
860 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
861 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
862 Tok.isNot(tok::lesslessequal) &&
863 // Colon is used in labels, base class lists, initializer lists,
864 // range-based for loops, ternary operator, but should never be the
865 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000866 Tok.isNot(tok::colon) &&
867 // 'noexcept' is a trailing annotation.
868 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000869}
870
Martin Probst533965c2016-04-19 18:19:06 +0000871static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
872 const FormatToken *FormatTok) {
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000873 // FIXME: This returns true for C/C++ keywords like 'struct'.
874 return FormatTok->is(tok::identifier) &&
875 (FormatTok->Tok.getIdentifierInfo() == nullptr ||
Martin Probst3dbbefa2016-11-10 16:21:02 +0000876 !FormatTok->isOneOf(
877 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
878 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
879 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
880 Keywords.kw_let, Keywords.kw_var, tok::kw_const,
881 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
Manuel Klimek89628f62017-09-20 09:51:03 +0000882 Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws,
883 Keywords.kw_from));
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000884}
885
Martin Probst533965c2016-04-19 18:19:06 +0000886static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
887 const FormatToken *FormatTok) {
Martin Probstb9316ff2016-09-18 17:21:52 +0000888 return FormatTok->Tok.isLiteral() ||
889 FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
890 mustBeJSIdent(Keywords, FormatTok);
Martin Probst533965c2016-04-19 18:19:06 +0000891}
892
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000893// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
894// when encountered after a value (see mustBeJSIdentOrValue).
895static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
896 const FormatToken *FormatTok) {
897 return FormatTok->isOneOf(
Martin Probst5f8445b2016-04-24 22:05:09 +0000898 tok::kw_return, Keywords.kw_yield,
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000899 // conditionals
900 tok::kw_if, tok::kw_else,
901 // loops
902 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
903 // switch/case
904 tok::kw_switch, tok::kw_case,
905 // exceptions
906 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
907 // declaration
908 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
Martin Probst5f8445b2016-04-24 22:05:09 +0000909 Keywords.kw_async, Keywords.kw_function,
910 // import/export
911 Keywords.kw_import, tok::kw_export);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000912}
913
914// readTokenWithJavaScriptASI reads the next token and terminates the current
915// line if JavaScript Automatic Semicolon Insertion must
916// happen between the current token and the next token.
917//
918// This method is conservative - it cannot cover all edge cases of JavaScript,
919// but only aims to correctly handle certain well known cases. It *must not*
920// return true in speculative cases.
921void UnwrappedLineParser::readTokenWithJavaScriptASI() {
922 FormatToken *Previous = FormatTok;
923 readToken();
924 FormatToken *Next = FormatTok;
925
926 bool IsOnSameLine =
927 CommentsBeforeNextToken.empty()
928 ? Next->NewlinesBefore == 0
929 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
930 if (IsOnSameLine)
931 return;
932
933 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
Martin Probst717f6dc2016-10-21 05:11:38 +0000934 bool PreviousStartsTemplateExpr =
935 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${");
Martin Probst7e0f25b2017-11-25 09:19:42 +0000936 if (PreviousMustBeValue || Previous->is(tok::r_paren)) {
937 // If the line contains an '@' sign, the previous token might be an
938 // annotation, which can precede another identifier/value.
939 bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
940 [](UnwrappedLineNode &LineNode) {
941 return LineNode.Tok->is(tok::at);
942 }) != Line->Tokens.end();
943 if (HasAt)
Martin Probstbbffeac2016-04-11 07:35:57 +0000944 return;
945 }
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000946 if (Next->is(tok::exclaim) && PreviousMustBeValue)
Martin Probstd40bca42017-01-09 08:56:36 +0000947 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000948 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
Martin Probst717f6dc2016-10-21 05:11:38 +0000949 bool NextEndsTemplateExpr =
950 Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
951 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
952 (PreviousMustBeValue ||
953 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
954 tok::minusminus)))
Martin Probstd40bca42017-01-09 08:56:36 +0000955 return addUnwrappedLine();
Martin Probst0a19d432017-08-09 15:19:16 +0000956 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) &&
957 isJSDeclOrStmt(Keywords, Next))
Martin Probstd40bca42017-01-09 08:56:36 +0000958 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000959}
960
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000961void UnwrappedLineParser::parseStructuralElement() {
Daniel Jasper498f5582015-12-25 08:53:31 +0000962 assert(!FormatTok->is(tok::l_brace));
963 if (Style.Language == FormatStyle::LK_TableGen &&
964 FormatTok->is(tok::pp_include)) {
965 nextToken();
966 if (FormatTok->is(tok::string_literal))
967 nextToken();
968 addUnwrappedLine();
969 return;
970 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000971 switch (FormatTok->Tok.getKind()) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000972 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000973 nextToken();
974 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000975 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000976 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000977 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000978 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000979 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000980 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000981 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000982 break;
983 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000984 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000985 nextToken();
986 }
987 }
988 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000989 case tok::kw_namespace:
990 parseNamespace();
991 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000992 case tok::kw_inline:
993 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000994 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000995 parseNamespace();
996 return;
997 }
998 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000999 case tok::kw_public:
1000 case tok::kw_protected:
1001 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +00001002 if (Style.Language == FormatStyle::LK_Java ||
1003 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001004 nextToken();
1005 else
1006 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +00001007 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001008 case tok::kw_if:
1009 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +00001010 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001011 case tok::kw_for:
1012 case tok::kw_while:
1013 parseForOrWhileLoop();
1014 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001015 case tok::kw_do:
1016 parseDoWhile();
1017 return;
1018 case tok::kw_switch:
Martin Probstf785fd92017-08-04 17:07:15 +00001019 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1020 // 'switch: string' field declaration.
1021 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001022 parseSwitch();
1023 return;
1024 case tok::kw_default:
Martin Probstf785fd92017-08-04 17:07:15 +00001025 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1026 // 'default: string' field declaration.
1027 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001028 nextToken();
Nico Weberc29f83b2018-01-23 16:30:56 +00001029 if (FormatTok->is(tok::colon)) {
1030 parseLabel();
1031 return;
1032 }
1033 // e.g. "default void f() {}" in a Java interface.
1034 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001035 case tok::kw_case:
Martin Probstf785fd92017-08-04 17:07:15 +00001036 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1037 // 'case: string' field declaration.
1038 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001039 parseCaseLabel();
1040 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001041 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +00001042 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +00001043 parseTryCatch();
1044 return;
Manuel Klimekae610d12013-01-21 14:32:05 +00001045 case tok::kw_extern:
1046 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001047 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +00001048 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001049 if (FormatTok->Tok.is(tok::l_brace)) {
Krasimir Georgievd6ce9372017-09-15 11:23:50 +00001050 if (Style.BraceWrapping.AfterExternBlock) {
1051 addUnwrappedLine();
1052 parseBlock(/*MustBeDeclaration=*/true);
1053 } else {
1054 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
1055 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001056 addUnwrappedLine();
1057 return;
1058 }
1059 }
Daniel Jaspere1e43192014-04-01 12:55:11 +00001060 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +00001061 case tok::kw_export:
1062 if (Style.Language == FormatStyle::LK_JavaScript) {
1063 parseJavaScriptEs6ImportExport();
1064 return;
1065 }
1066 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +00001067 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001068 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001069 parseForOrWhileLoop();
1070 return;
1071 }
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001072 if (FormatTok->is(TT_MacroBlockBegin)) {
1073 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
1074 /*MunchSemi=*/false);
1075 return;
1076 }
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001077 if (FormatTok->is(Keywords.kw_import)) {
1078 if (Style.Language == FormatStyle::LK_JavaScript) {
1079 parseJavaScriptEs6ImportExport();
1080 return;
1081 }
1082 if (Style.Language == FormatStyle::LK_Proto) {
1083 nextToken();
Daniel Jasper8b61d142016-06-20 20:39:53 +00001084 if (FormatTok->is(tok::kw_public))
1085 nextToken();
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001086 if (!FormatTok->is(tok::string_literal))
1087 return;
1088 nextToken();
1089 if (FormatTok->is(tok::semi))
1090 nextToken();
1091 addUnwrappedLine();
1092 return;
1093 }
Daniel Jasper354aa512015-02-19 16:07:32 +00001094 }
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001095 if (Style.isCpp() &&
Daniel Jasper72b33572017-03-31 12:04:37 +00001096 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
Daniel Jaspera00de632015-12-01 12:05:04 +00001097 Keywords.kw_slots, Keywords.kw_qslots)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001098 nextToken();
1099 if (FormatTok->is(tok::colon)) {
1100 nextToken();
1101 addUnwrappedLine();
Daniel Jasper31343832016-07-27 10:13:24 +00001102 return;
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001103 }
Daniel Jasper53395402015-04-07 15:04:40 +00001104 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001105 // In all other cases, parse the declaration.
1106 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001107 default:
1108 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001109 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001110 do {
Manuel Klimeke411aa82017-09-20 09:29:37 +00001111 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001112 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +00001113 case tok::at:
1114 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001115 if (FormatTok->Tok.is(tok::l_brace)) {
1116 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001117 parseBracedList();
Nico Weberc068ff72018-01-23 17:10:25 +00001118 break;
1119 }
1120 switch (FormatTok->Tok.getObjCKeywordID()) {
1121 case tok::objc_public:
1122 case tok::objc_protected:
1123 case tok::objc_package:
1124 case tok::objc_private:
1125 return parseAccessSpecifier();
1126 case tok::objc_interface:
1127 case tok::objc_implementation:
1128 return parseObjCInterfaceOrImplementation();
1129 case tok::objc_protocol:
1130 if (parseObjCProtocol())
1131 return;
1132 break;
1133 case tok::objc_end:
1134 return; // Handled by the caller.
1135 case tok::objc_optional:
1136 case tok::objc_required:
1137 nextToken();
1138 addUnwrappedLine();
1139 return;
1140 case tok::objc_autoreleasepool:
1141 nextToken();
1142 if (FormatTok->Tok.is(tok::l_brace)) {
Francois Ferranda2484b22018-02-27 13:48:27 +00001143 if (Style.BraceWrapping.AfterControlStatement)
Nico Weberc068ff72018-01-23 17:10:25 +00001144 addUnwrappedLine();
1145 parseBlock(/*MustBeDeclaration=*/false);
1146 }
1147 addUnwrappedLine();
1148 return;
Francois Ferrandba91c3d2018-02-27 13:48:21 +00001149 case tok::objc_synchronized:
1150 nextToken();
1151 if (FormatTok->Tok.is(tok::l_paren))
1152 // Skip synchronization object
1153 parseParens();
1154 if (FormatTok->Tok.is(tok::l_brace)) {
Francois Ferranda2484b22018-02-27 13:48:27 +00001155 if (Style.BraceWrapping.AfterControlStatement)
Francois Ferrandba91c3d2018-02-27 13:48:21 +00001156 addUnwrappedLine();
1157 parseBlock(/*MustBeDeclaration=*/false);
1158 }
1159 addUnwrappedLine();
1160 return;
Nico Weberc068ff72018-01-23 17:10:25 +00001161 case tok::objc_try:
1162 // This branch isn't strictly necessary (the kw_try case below would
1163 // do this too after the tok::at is parsed above). But be explicit.
1164 parseTryCatch();
1165 return;
1166 default:
1167 break;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001168 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001169 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001170 case tok::kw_enum:
Daniel Jaspera7900ad2016-05-08 18:12:22 +00001171 // Ignore if this is part of "template <enum ...".
1172 if (Previous && Previous->is(tok::less)) {
1173 nextToken();
1174 break;
1175 }
1176
Daniel Jasper90cf3802015-06-17 09:44:02 +00001177 // parseEnum falls through and does not yet add an unwrapped line as an
1178 // enum definition can start a structural element.
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001179 if (!parseEnum())
1180 break;
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001181 // This only applies for C++.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001182 if (!Style.isCpp()) {
Daniel Jasper90cf3802015-06-17 09:44:02 +00001183 addUnwrappedLine();
1184 return;
1185 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001186 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001187 case tok::kw_typedef:
1188 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +00001189 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1190 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001191 parseEnum();
1192 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001193 case tok::kw_struct:
1194 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +00001195 case tok::kw_class:
Daniel Jasper910807d2015-06-12 04:52:02 +00001196 // parseRecord falls through and does not yet add an unwrapped line as a
1197 // record declaration or definition can start a structural element.
Manuel Klimeke01bab52013-01-15 13:38:33 +00001198 parseRecord();
Daniel Jasper910807d2015-06-12 04:52:02 +00001199 // This does not apply for Java and JavaScript.
1200 if (Style.Language == FormatStyle::LK_Java ||
1201 Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperd5ec65b2016-01-08 07:06:07 +00001202 if (FormatTok->is(tok::semi))
1203 nextToken();
Daniel Jasper910807d2015-06-12 04:52:02 +00001204 addUnwrappedLine();
1205 return;
1206 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001207 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +00001208 case tok::period:
1209 nextToken();
1210 // In Java, classes have an implicit static member "class".
1211 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
1212 FormatTok->is(tok::kw_class))
1213 nextToken();
Daniel Jasperba52fcb2015-09-28 14:29:45 +00001214 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
1215 FormatTok->Tok.getIdentifierInfo())
1216 // JavaScript only has pseudo keywords, all keywords are allowed to
1217 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1218 nextToken();
Daniel Jaspere5d74862014-11-26 08:17:08 +00001219 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001220 case tok::semi:
1221 nextToken();
1222 addUnwrappedLine();
1223 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001224 case tok::r_brace:
1225 addUnwrappedLine();
1226 return;
Daniel Jasperf7935112012-12-03 18:12:45 +00001227 case tok::l_paren:
1228 parseParens();
1229 break;
Daniel Jasper5af04a42015-10-07 03:43:10 +00001230 case tok::kw_operator:
1231 nextToken();
1232 if (FormatTok->isBinaryOperator())
1233 nextToken();
1234 break;
Manuel Klimek516e0542013-09-04 13:25:30 +00001235 case tok::caret:
1236 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +00001237 if (FormatTok->Tok.isAnyIdentifier() ||
1238 FormatTok->isSimpleTypeSpecifier())
1239 nextToken();
1240 if (FormatTok->is(tok::l_paren))
1241 parseParens();
1242 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +00001243 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +00001244 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001245 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001246 if (!tryToParseBracedList()) {
1247 // A block outside of parentheses must be the last part of a
1248 // structural element.
1249 // FIXME: Figure out cases where this is not true, and add projections
1250 // for them (the one we know is missing are lambdas).
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001251 if (Style.BraceWrapping.AfterFunction)
Manuel Klimekab419912013-05-23 09:41:43 +00001252 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +00001253 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +00001254 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001255 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +00001256 return;
1257 }
1258 // Otherwise this was a braced init list, and the structural
1259 // element continues.
1260 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001261 case tok::kw_try:
1262 // We arrive here when parsing function-try blocks.
1263 parseTryCatch();
1264 return;
Daniel Jasper40e19212013-05-29 13:16:10 +00001265 case tok::identifier: {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001266 if (FormatTok->is(TT_MacroBlockEnd)) {
1267 addUnwrappedLine();
1268 return;
1269 }
1270
Martin Probst973ff792017-04-27 13:07:24 +00001271 // Function declarations (as opposed to function expressions) are parsed
1272 // on their own unwrapped line by continuing this loop. Function
1273 // expressions (functions that are not on their own line) must not create
1274 // a new unwrapped line, so they are special cased below.
1275 size_t TokenCount = Line->Tokens.size();
Daniel Jasper9326f912015-05-05 08:40:32 +00001276 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst973ff792017-04-27 13:07:24 +00001277 FormatTok->is(Keywords.kw_function) &&
1278 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1279 Keywords.kw_async)))) {
Daniel Jasper069e5f42014-05-20 11:14:57 +00001280 tryToParseJSFunction();
1281 break;
1282 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001283 if ((Style.Language == FormatStyle::LK_JavaScript ||
1284 Style.Language == FormatStyle::LK_Java) &&
1285 FormatTok->is(Keywords.kw_interface)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001286 if (Style.Language == FormatStyle::LK_JavaScript) {
1287 // In JavaScript/TypeScript, "interface" can be used as a standalone
1288 // identifier, e.g. in `var interface = 1;`. If "interface" is
1289 // followed by another identifier, it is very like to be an actual
1290 // interface declaration.
1291 unsigned StoredPosition = Tokens->getPosition();
1292 FormatToken *Next = Tokens->getNextToken();
1293 FormatTok = Tokens->setPosition(StoredPosition);
Martin Probst533965c2016-04-19 18:19:06 +00001294 if (Next && !mustBeJSIdent(Keywords, Next)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001295 nextToken();
1296 break;
1297 }
1298 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001299 parseRecord();
Daniel Jasper259188b2015-06-12 04:56:34 +00001300 addUnwrappedLine();
Daniel Jasper5c235c02015-07-06 14:26:04 +00001301 return;
Daniel Jasper9326f912015-05-05 08:40:32 +00001302 }
1303
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001304 // See if the following token should start a new unwrapped line.
Daniel Jasper9326f912015-05-05 08:40:32 +00001305 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +00001306 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +00001307 if (Line->Tokens.size() == 1 &&
1308 // JS doesn't have macros, and within classes colons indicate fields,
1309 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +00001310 Style.Language != FormatStyle::LK_JavaScript) {
1311 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Daniel Jasper40609472016-04-06 15:02:46 +00001312 Line->Tokens.begin()->Tok->MustBreakBefore = true;
Alexander Kornienkode644272013-04-08 22:16:06 +00001313 parseLabel();
1314 return;
1315 }
Daniel Jasper680b09b2014-11-05 10:48:04 +00001316 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +00001317 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +00001318 bool FunctionLike = FormatTok->is(tok::l_paren);
1319 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +00001320 parseParens();
Daniel Jaspere60cba12015-05-13 11:35:53 +00001321
1322 bool FollowedByNewline =
1323 CommentsBeforeNextToken.empty()
1324 ? FormatTok->NewlinesBefore > 0
1325 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1326
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001327 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
Daniel Jasper680b09b2014-11-05 10:48:04 +00001328 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +00001329 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +00001330 return;
Alexander Kornienkode644272013-04-08 22:16:06 +00001331 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001332 }
1333 break;
Daniel Jasper40e19212013-05-29 13:16:10 +00001334 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001335 case tok::equal:
Manuel Klimek79e06082015-05-21 12:23:34 +00001336 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1337 // TT_JsFatArrow. The always start an expression or a child block if
1338 // followed by a curly.
1339 if (FormatTok->is(TT_JsFatArrow)) {
1340 nextToken();
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001341 if (FormatTok->is(tok::l_brace))
Manuel Klimek79e06082015-05-21 12:23:34 +00001342 parseChildBlock();
Manuel Klimek79e06082015-05-21 12:23:34 +00001343 break;
1344 }
1345
Daniel Jaspere25509f2012-12-17 11:29:41 +00001346 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001347 if (FormatTok->Tok.is(tok::l_brace)) {
1348 nextToken();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001349 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001350 } else if (Style.Language == FormatStyle::LK_Proto &&
Manuel Klimek89628f62017-09-20 09:51:03 +00001351 FormatTok->Tok.is(tok::less)) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001352 nextToken();
Krasimir Georgiev0b41fcb2017-06-27 13:58:41 +00001353 parseBracedList(/*ContinueOnSemicolons=*/false,
1354 /*ClosingBraceKind=*/tok::greater);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001355 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001356 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001357 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001358 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001359 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +00001360 case tok::kw_new:
1361 parseNew();
1362 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001363 default:
1364 nextToken();
1365 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001366 }
1367 } while (!eof());
1368}
1369
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001370bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001371 if (!Style.isCpp()) {
Daniel Jasper1feab0f2015-06-02 15:31:37 +00001372 nextToken();
1373 return false;
1374 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001375 assert(FormatTok->is(tok::l_square));
1376 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001377 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001378 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001379
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001380 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001381 if (FormatTok->isSimpleTypeSpecifier()) {
1382 nextToken();
1383 continue;
1384 }
Manuel Klimekffdeb592013-09-03 15:10:01 +00001385 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001386 case tok::l_brace:
1387 break;
1388 case tok::l_paren:
1389 parseParens();
1390 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +00001391 case tok::amp:
1392 case tok::star:
1393 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +00001394 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001395 case tok::less:
1396 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001397 case tok::identifier:
Daniel Jasper5eaa0092015-08-13 13:37:08 +00001398 case tok::numeric_constant:
Daniel Jasper1067ab02014-02-11 10:16:55 +00001399 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001400 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +00001401 nextToken();
1402 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001403 case tok::arrow:
Daniel Jasper6f2b88a2015-06-05 13:18:09 +00001404 FormatTok->Type = TT_LambdaArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001405 nextToken();
1406 break;
1407 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001408 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001409 }
1410 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001411 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +00001412 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001413 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001414}
1415
1416bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
Manuel Klimek89628f62017-09-20 09:51:03 +00001417 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001418 if (Previous &&
1419 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
Manuel Klimekd0f3fe52018-04-11 14:51:54 +00001420 tok::kw_delete, tok::l_square) ||
Manuel Klimek89628f62017-09-20 09:51:03 +00001421 FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() ||
1422 Previous->isSimpleTypeSpecifier())) {
Manuel Klimekffdeb592013-09-03 15:10:01 +00001423 nextToken();
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001424 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001425 }
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001426 nextToken();
Manuel Klimekd0f3fe52018-04-11 14:51:54 +00001427 if (FormatTok->is(tok::l_square)) {
1428 return false;
1429 }
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001430 parseSquare(/*LambdaIntroducer=*/true);
1431 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001432}
1433
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001434void UnwrappedLineParser::tryToParseJSFunction() {
Martin Probst409697e2016-05-29 14:41:07 +00001435 assert(FormatTok->is(Keywords.kw_function) ||
1436 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
Martin Probst5f8445b2016-04-24 22:05:09 +00001437 if (FormatTok->is(Keywords.kw_async))
1438 nextToken();
1439 // Consume "function".
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001440 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001441
Daniel Jasper71e50af2016-11-01 06:22:59 +00001442 // Consume * (generator function). Treat it like C++'s overloaded operators.
1443 if (FormatTok->is(tok::star)) {
1444 FormatTok->Type = TT_OverloadedOperator;
Martin Probst5f8445b2016-04-24 22:05:09 +00001445 nextToken();
Daniel Jasper71e50af2016-11-01 06:22:59 +00001446 }
Martin Probst5f8445b2016-04-24 22:05:09 +00001447
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001448 // Consume function name.
1449 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001450 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001451
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001452 if (FormatTok->isNot(tok::l_paren))
1453 return;
Manuel Klimek79e06082015-05-21 12:23:34 +00001454
1455 // Parse formal parameter list.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001456 parseParens();
Manuel Klimek79e06082015-05-21 12:23:34 +00001457
1458 if (FormatTok->is(tok::colon)) {
1459 // Parse a type definition.
1460 nextToken();
1461
1462 // Eat the type declaration. For braced inline object types, balance braces,
1463 // otherwise just parse until finding an l_brace for the function body.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001464 if (FormatTok->is(tok::l_brace))
1465 tryToParseBracedList();
1466 else
Martin Probstaf16c502017-01-04 13:36:43 +00001467 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
Manuel Klimek79e06082015-05-21 12:23:34 +00001468 nextToken();
Manuel Klimek79e06082015-05-21 12:23:34 +00001469 }
1470
Martin Probstaf16c502017-01-04 13:36:43 +00001471 if (FormatTok->is(tok::semi))
1472 return;
1473
Manuel Klimek79e06082015-05-21 12:23:34 +00001474 parseChildBlock();
1475}
1476
Daniel Jasper3c883d12015-05-18 14:49:19 +00001477bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001478 if (FormatTok->BlockKind == BK_Unknown)
Daniel Jasper3c883d12015-05-18 14:49:19 +00001479 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001480 assert(FormatTok->BlockKind != BK_Unknown);
1481 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001482 return false;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001483 nextToken();
Manuel Klimekab419912013-05-23 09:41:43 +00001484 parseBracedList();
1485 return true;
1486}
1487
Krasimir Georgievff747be2017-06-27 13:43:07 +00001488bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1489 tok::TokenKind ClosingBraceKind) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001490 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001491
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001492 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1493 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001494 do {
Manuel Klimek79e06082015-05-21 12:23:34 +00001495 if (Style.Language == FormatStyle::LK_JavaScript) {
Martin Probst409697e2016-05-29 14:41:07 +00001496 if (FormatTok->is(Keywords.kw_function) ||
1497 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001498 tryToParseJSFunction();
1499 continue;
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001500 }
1501 if (FormatTok->is(TT_JsFatArrow)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001502 nextToken();
1503 // Fat arrows can be followed by simple expressions or by child blocks
1504 // in curly braces.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001505 if (FormatTok->is(tok::l_brace)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001506 parseChildBlock();
1507 continue;
1508 }
1509 }
Martin Probst8e3eba02017-02-07 16:33:13 +00001510 if (FormatTok->is(tok::l_brace)) {
1511 // Could be a method inside of a braced list `{a() { return 1; }}`.
1512 if (tryToParseBracedList())
1513 continue;
1514 parseChildBlock();
1515 }
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001516 }
Krasimir Georgievff747be2017-06-27 13:43:07 +00001517 if (FormatTok->Tok.getKind() == ClosingBraceKind) {
1518 nextToken();
1519 return !HasError;
1520 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001521 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001522 case tok::caret:
1523 nextToken();
1524 if (FormatTok->is(tok::l_brace)) {
1525 parseChildBlock();
1526 }
1527 break;
1528 case tok::l_square:
1529 tryToParseLambda();
1530 break;
Daniel Jaspera87af7a2015-06-30 11:32:22 +00001531 case tok::l_paren:
1532 parseParens();
Daniel Jasperf46dec82015-03-31 14:34:15 +00001533 // JavaScript can just have free standing methods and getters/setters in
1534 // object literals. Detect them by a "{" following ")".
1535 if (Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperf46dec82015-03-31 14:34:15 +00001536 if (FormatTok->is(tok::l_brace))
1537 parseChildBlock();
1538 break;
1539 }
Daniel Jasperf46dec82015-03-31 14:34:15 +00001540 break;
Martin Probst8e3eba02017-02-07 16:33:13 +00001541 case tok::l_brace:
1542 // Assume there are no blocks inside a braced init list apart
1543 // from the ones we explicitly parse out (like lambdas).
1544 FormatTok->BlockKind = BK_BracedInit;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001545 nextToken();
Martin Probst8e3eba02017-02-07 16:33:13 +00001546 parseBracedList();
1547 break;
Krasimir Georgievfa4dbb62017-08-03 13:43:45 +00001548 case tok::less:
1549 if (Style.Language == FormatStyle::LK_Proto) {
1550 nextToken();
1551 parseBracedList(/*ContinueOnSemicolons=*/false,
1552 /*ClosingBraceKind=*/tok::greater);
1553 } else {
1554 nextToken();
1555 }
1556 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001557 case tok::semi:
Daniel Jasperb9a49902016-01-09 15:56:28 +00001558 // JavaScript (or more precisely TypeScript) can have semicolons in braced
1559 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1560 // used for error recovery if we have otherwise determined that this is
1561 // a braced list.
1562 if (Style.Language == FormatStyle::LK_JavaScript) {
1563 nextToken();
1564 break;
1565 }
Daniel Jasper015ed022013-09-13 09:20:45 +00001566 HasError = true;
1567 if (!ContinueOnSemicolons)
1568 return !HasError;
1569 nextToken();
1570 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001571 case tok::comma:
1572 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001573 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001574 default:
1575 nextToken();
1576 break;
1577 }
1578 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001579 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001580}
1581
Daniel Jasperf7935112012-12-03 18:12:45 +00001582void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001583 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001584 nextToken();
1585 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001586 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001587 case tok::l_paren:
1588 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001589 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1590 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001591 break;
1592 case tok::r_paren:
1593 nextToken();
1594 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001595 case tok::r_brace:
1596 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1597 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001598 case tok::l_square:
1599 tryToParseLambda();
1600 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001601 case tok::l_brace:
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001602 if (!tryToParseBracedList())
Manuel Klimekf017dc02013-09-04 13:34:14 +00001603 parseChildBlock();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001604 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001605 case tok::at:
1606 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001607 if (FormatTok->Tok.is(tok::l_brace)) {
1608 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001609 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001610 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001611 break;
Martin Probst1027fb82017-02-07 14:05:30 +00001612 case tok::kw_class:
1613 if (Style.Language == FormatStyle::LK_JavaScript)
1614 parseRecord(/*ParseAsExpr=*/true);
1615 else
1616 nextToken();
1617 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001618 case tok::identifier:
1619 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst409697e2016-05-29 14:41:07 +00001620 (FormatTok->is(Keywords.kw_function) ||
1621 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001622 tryToParseJSFunction();
1623 else
1624 nextToken();
1625 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001626 default:
1627 nextToken();
1628 break;
1629 }
1630 } while (!eof());
1631}
1632
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001633void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
1634 if (!LambdaIntroducer) {
1635 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1636 if (tryToParseLambda())
1637 return;
1638 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001639 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001640 switch (FormatTok->Tok.getKind()) {
1641 case tok::l_paren:
1642 parseParens();
1643 break;
1644 case tok::r_square:
1645 nextToken();
1646 return;
1647 case tok::r_brace:
1648 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1649 return;
1650 case tok::l_square:
1651 parseSquare();
1652 break;
1653 case tok::l_brace: {
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001654 if (!tryToParseBracedList())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001655 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001656 break;
1657 }
1658 case tok::at:
1659 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001660 if (FormatTok->Tok.is(tok::l_brace)) {
1661 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001662 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001663 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001664 break;
1665 default:
1666 nextToken();
1667 break;
1668 }
1669 } while (!eof());
1670}
1671
Daniel Jasperf7935112012-12-03 18:12:45 +00001672void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001673 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001674 nextToken();
Daniel Jasper6a7d5a72017-06-19 07:40:49 +00001675 if (FormatTok->Tok.is(tok::kw_constexpr))
1676 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001677 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001678 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001679 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001680 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001681 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001682 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001683 if (Style.BraceWrapping.BeforeElse)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001684 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001685 else
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001686 NeedsUnwrappedLine = true;
Daniel Jasperf7935112012-12-03 18:12:45 +00001687 } else {
1688 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001689 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001690 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001691 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001692 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001693 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001694 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001695 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001696 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001697 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001698 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001699 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001700 parseIfThenElse();
1701 } else {
1702 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001703 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001704 parseStructuralElement();
Daniel Jasper451544a2016-05-19 06:30:48 +00001705 if (FormatTok->is(tok::eof))
1706 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001707 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001708 }
1709 } else if (NeedsUnwrappedLine) {
1710 addUnwrappedLine();
1711 }
1712}
1713
Daniel Jasper04a71a42014-05-08 11:58:24 +00001714void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001715 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001716 nextToken();
1717 bool NeedsUnwrappedLine = false;
1718 if (FormatTok->is(tok::colon)) {
1719 // We are in a function try block, what comes is an initializer list.
1720 nextToken();
1721 while (FormatTok->is(tok::identifier)) {
1722 nextToken();
1723 if (FormatTok->is(tok::l_paren))
1724 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001725 if (FormatTok->is(tok::comma))
1726 nextToken();
1727 }
1728 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001729 // Parse try with resource.
1730 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1731 parseParens();
1732 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001733 if (FormatTok->is(tok::l_brace)) {
1734 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1735 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001736 if (Style.BraceWrapping.BeforeCatch) {
Daniel Jasper04a71a42014-05-08 11:58:24 +00001737 addUnwrappedLine();
1738 } else {
1739 NeedsUnwrappedLine = true;
1740 }
1741 } else if (!FormatTok->is(tok::kw_catch)) {
1742 // The C++ standard requires a compound-statement after a try.
1743 // If there's none, we try to assume there's a structuralElement
1744 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001745 addUnwrappedLine();
1746 ++Line->Level;
1747 parseStructuralElement();
1748 --Line->Level;
1749 }
Nico Weber33381f52015-02-07 01:57:32 +00001750 while (1) {
1751 if (FormatTok->is(tok::at))
1752 nextToken();
1753 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1754 tok::kw___finally) ||
1755 ((Style.Language == FormatStyle::LK_Java ||
1756 Style.Language == FormatStyle::LK_JavaScript) &&
1757 FormatTok->is(Keywords.kw_finally)) ||
1758 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1759 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1760 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001761 nextToken();
1762 while (FormatTok->isNot(tok::l_brace)) {
1763 if (FormatTok->is(tok::l_paren)) {
1764 parseParens();
1765 continue;
1766 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001767 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001768 return;
1769 nextToken();
1770 }
1771 NeedsUnwrappedLine = false;
1772 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1773 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001774 if (Style.BraceWrapping.BeforeCatch)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001775 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001776 else
Daniel Jasper04a71a42014-05-08 11:58:24 +00001777 NeedsUnwrappedLine = true;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001778 }
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001779 if (NeedsUnwrappedLine)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001780 addUnwrappedLine();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001781}
1782
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001783void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001784 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001785
1786 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001787 nextToken();
Saleem Abdulrasool328085f2015-10-30 05:07:56 +00001788 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001789 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001790 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001791 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001792 addUnwrappedLine();
1793
Daniel Jasper65ee3472013-07-31 23:16:02 +00001794 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1795 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1796 DeclarationScopeStack.size() > 1);
1797 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001798 // Munch the semicolon after a namespace. This is more common than one would
1799 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001800 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001801 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001802 addUnwrappedLine();
1803 }
1804 // FIXME: Add error handling.
1805}
1806
Daniel Jasper6acf5132015-03-12 14:44:29 +00001807void UnwrappedLineParser::parseNew() {
1808 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1809 nextToken();
1810 if (Style.Language != FormatStyle::LK_Java)
1811 return;
1812
1813 // In Java, we can parse everything up to the parens, which aren't optional.
1814 do {
1815 // There should not be a ;, { or } before the new's open paren.
1816 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1817 return;
1818
1819 // Consume the parens.
1820 if (FormatTok->is(tok::l_paren)) {
1821 parseParens();
1822
1823 // If there is a class body of an anonymous class, consume that as child.
1824 if (FormatTok->is(tok::l_brace))
1825 parseChildBlock();
1826 return;
1827 }
1828 nextToken();
1829 } while (!eof());
1830}
1831
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001832void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001833 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001834 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001835 nextToken();
Martin Probsta050f412017-05-18 21:19:29 +00001836 // JS' for await ( ...
Martin Probstbd49e322017-05-15 19:33:20 +00001837 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probsta050f412017-05-18 21:19:29 +00001838 FormatTok->is(Keywords.kw_await))
Martin Probstbd49e322017-05-15 19:33:20 +00001839 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001840 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001841 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001842 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001843 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001844 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001845 addUnwrappedLine();
1846 } else {
1847 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001848 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001849 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001850 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001851 }
1852}
1853
Daniel Jasperf7935112012-12-03 18:12:45 +00001854void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001855 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001856 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001857 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001858 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001859 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001860 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001861 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001862 } else {
1863 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001864 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001865 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001866 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001867 }
1868
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001869 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001870 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001871 addUnwrappedLine();
1872 return;
1873 }
1874
Daniel Jasperf7935112012-12-03 18:12:45 +00001875 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001876 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001877}
1878
1879void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001880 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001881 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001882 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001883 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001884 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001885 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001886 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001887 if (FormatTok->Tok.is(tok::kw_break)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001888 if (Style.BraceWrapping.AfterControlStatement)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001889 addUnwrappedLine();
1890 parseStructuralElement();
1891 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001892 addUnwrappedLine();
1893 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001894 if (FormatTok->is(tok::semi))
1895 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001896 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001897 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001898 Line->Level = OldLineLevel;
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001899 if (FormatTok->isNot(tok::l_brace)) {
Daniel Jasper40609472016-04-06 15:02:46 +00001900 parseStructuralElement();
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001901 addUnwrappedLine();
1902 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001903}
1904
1905void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001906 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001907 // FIXME: fix handling of complex expressions here.
1908 do {
1909 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001910 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001911 parseLabel();
1912}
1913
1914void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001915 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001916 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001917 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001918 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001919 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001920 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001921 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001922 addUnwrappedLine();
1923 } else {
1924 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001925 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001926 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001927 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001928 }
1929}
1930
1931void UnwrappedLineParser::parseAccessSpecifier() {
1932 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001933 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001934 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001935 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001936 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001937 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001938 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001939 addUnwrappedLine();
1940}
1941
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001942bool UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001943 // Won't be 'enum' for NS_ENUMs.
1944 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001945 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001946
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001947 // In TypeScript, "enum" can also be used as property name, e.g. in interface
1948 // declarations. An "enum" keyword followed by a colon would be a syntax
1949 // error and thus assume it is just an identifier.
Daniel Jasper87379302016-02-03 05:33:44 +00001950 if (Style.Language == FormatStyle::LK_JavaScript &&
1951 FormatTok->isOneOf(tok::colon, tok::question))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001952 return false;
1953
Daniel Jasper2b41a822013-08-20 12:42:50 +00001954 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001955 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1956 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001957
Daniel Jasper786a5502013-09-06 21:32:35 +00001958 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001959 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1960 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001961 nextToken();
1962 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001963 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001964 parseParens();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001965 if (FormatTok->is(tok::identifier)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001966 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001967 // If there are two identifiers in a row, this is likely an elaborate
1968 // return type. In Java, this can be "implements", etc.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001969 if (Style.isCpp() && FormatTok->is(tok::identifier))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001970 return false;
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001971 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001972 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001973
1974 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001975 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001976 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00001977 FormatTok->BlockKind = BK_Block;
1978
1979 if (Style.Language == FormatStyle::LK_Java) {
1980 // Java enums are different.
1981 parseJavaEnumBody();
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001982 return true;
1983 }
1984 if (Style.Language == FormatStyle::LK_Proto) {
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001985 parseBlock(/*MustBeDeclaration=*/true);
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001986 return true;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001987 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001988
1989 // Parse enum body.
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001990 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001991 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1992 if (HasError) {
1993 if (FormatTok->is(tok::semi))
1994 nextToken();
1995 addUnwrappedLine();
1996 }
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001997 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00001998
Daniel Jasper90cf3802015-06-17 09:44:02 +00001999 // There is no addUnwrappedLine() here so that we fall through to parsing a
2000 // structural element afterwards. Thus, in "enum A {} n, m;",
Manuel Klimek2cec0192013-01-21 19:17:52 +00002001 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00002002}
2003
2004void UnwrappedLineParser::parseJavaEnumBody() {
2005 // Determine whether the enum is simple, i.e. does not have a semicolon or
2006 // constants with class bodies. Simple enums can be formatted like braced
2007 // lists, contracted to a single line, etc.
2008 unsigned StoredPosition = Tokens->getPosition();
2009 bool IsSimple = true;
2010 FormatToken *Tok = Tokens->getNextToken();
2011 while (Tok) {
2012 if (Tok->is(tok::r_brace))
2013 break;
2014 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
2015 IsSimple = false;
2016 break;
2017 }
2018 // FIXME: This will also mark enums with braces in the arguments to enum
2019 // constants as "not simple". This is probably fine in practice, though.
2020 Tok = Tokens->getNextToken();
2021 }
2022 FormatTok = Tokens->setPosition(StoredPosition);
2023
2024 if (IsSimple) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002025 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002026 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00002027 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002028 return;
2029 }
2030
2031 // Parse the body of a more complex enum.
2032 // First add a line for everything up to the "{".
2033 nextToken();
2034 addUnwrappedLine();
2035 ++Line->Level;
2036
2037 // Parse the enum constants.
2038 while (FormatTok) {
2039 if (FormatTok->is(tok::l_brace)) {
2040 // Parse the constant's class body.
2041 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2042 /*MunchSemi=*/false);
2043 } else if (FormatTok->is(tok::l_paren)) {
2044 parseParens();
2045 } else if (FormatTok->is(tok::comma)) {
2046 nextToken();
2047 addUnwrappedLine();
2048 } else if (FormatTok->is(tok::semi)) {
2049 nextToken();
2050 addUnwrappedLine();
2051 break;
2052 } else if (FormatTok->is(tok::r_brace)) {
2053 addUnwrappedLine();
2054 break;
2055 } else {
2056 nextToken();
2057 }
2058 }
2059
2060 // Parse the class body after the enum's ";" if any.
2061 parseLevel(/*HasOpeningBrace=*/true);
2062 nextToken();
2063 --Line->Level;
2064 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00002065}
2066
Martin Probst1027fb82017-02-07 14:05:30 +00002067void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00002068 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00002069 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00002070
Daniel Jasper04785d02015-05-06 14:03:02 +00002071 // The actual identifier can be a nested name specifier, and in macros
2072 // it is often token-pasted.
2073 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2074 tok::kw___attribute, tok::kw___declspec,
2075 tok::kw_alignas) ||
2076 ((Style.Language == FormatStyle::LK_Java ||
2077 Style.Language == FormatStyle::LK_JavaScript) &&
2078 FormatTok->isOneOf(tok::period, tok::comma))) {
Martin Probstcb870c52017-08-01 15:46:10 +00002079 if (Style.Language == FormatStyle::LK_JavaScript &&
2080 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
2081 // JavaScript/TypeScript supports inline object types in
2082 // extends/implements positions:
2083 // class Foo implements {bar: number} { }
2084 nextToken();
2085 if (FormatTok->is(tok::l_brace)) {
2086 tryToParseBracedList();
2087 continue;
2088 }
2089 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002090 bool IsNonMacroIdentifier =
2091 FormatTok->is(tok::identifier) &&
2092 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002093 nextToken();
2094 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00002095 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00002096 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00002097 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00002098
Daniel Jasper04785d02015-05-06 14:03:02 +00002099 // Note that parsing away template declarations here leads to incorrectly
2100 // accepting function declarations as record declarations.
2101 // In general, we cannot solve this problem. Consider:
2102 // class A<int> B() {}
2103 // which can be a function definition or a class definition when B() is a
2104 // macro. If we find enough real-world cases where this is a problem, we
2105 // can parse for the 'template' keyword in the beginning of the statement,
2106 // and thus rule out the record production in case there is no template
2107 // (this would still leave us with an ambiguity between template function
2108 // and class declarations).
Daniel Jasperadba2aa2015-05-18 12:52:00 +00002109 if (FormatTok->isOneOf(tok::colon, tok::less)) {
2110 while (!eof()) {
Daniel Jasper3c883d12015-05-18 14:49:19 +00002111 if (FormatTok->is(tok::l_brace)) {
2112 calculateBraceTypes(/*ExpectClassBody=*/true);
2113 if (!tryToParseBracedList())
2114 break;
2115 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002116 if (FormatTok->Tok.is(tok::semi))
2117 return;
2118 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002119 }
2120 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002121 if (FormatTok->Tok.is(tok::l_brace)) {
Martin Probst1027fb82017-02-07 14:05:30 +00002122 if (ParseAsExpr) {
2123 parseChildBlock();
2124 } else {
2125 if (ShouldBreakBeforeBrace(Style, InitialToken))
2126 addUnwrappedLine();
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002127
Martin Probst1027fb82017-02-07 14:05:30 +00002128 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2129 /*MunchSemi=*/false);
2130 }
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002131 }
Daniel Jasper90cf3802015-06-17 09:44:02 +00002132 // There is no addUnwrappedLine() here so that we fall through to parsing a
2133 // structural element afterwards. Thus, in "class A {} n, m;",
2134 // "} n, m;" will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00002135}
2136
Ben Hamilton707e68f2018-05-30 15:21:38 +00002137void UnwrappedLineParser::parseObjCMethod() {
2138 assert(FormatTok->Tok.isOneOf(tok::l_paren, tok::identifier) &&
2139 "'(' or identifier expected.");
2140 do {
2141 if (FormatTok->Tok.is(tok::semi)) {
2142 nextToken();
2143 addUnwrappedLine();
2144 return;
2145 } else if (FormatTok->Tok.is(tok::l_brace)) {
2146 parseBlock(/*MustBeDeclaration=*/false);
2147 addUnwrappedLine();
2148 return;
2149 } else {
2150 nextToken();
2151 }
2152 } while (!eof());
2153}
2154
Nico Weber8696a8d2013-01-09 21:15:03 +00002155void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002156 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Ben Hamilton1462e842018-04-05 15:26:25 +00002157 do {
Nico Weber8696a8d2013-01-09 21:15:03 +00002158 nextToken();
Ben Hamilton1462e842018-04-05 15:26:25 +00002159 // Early exit in case someone forgot a close angle.
2160 if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2161 FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2162 return;
2163 } while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00002164 nextToken(); // Skip '>'.
2165}
2166
2167void UnwrappedLineParser::parseObjCUntilAtEnd() {
2168 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002169 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002170 nextToken();
2171 addUnwrappedLine();
2172 break;
2173 }
Daniel Jaspera15da302013-08-28 08:04:23 +00002174 if (FormatTok->is(tok::l_brace)) {
2175 parseBlock(/*MustBeDeclaration=*/false);
2176 // In ObjC interfaces, nothing should be following the "}".
2177 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00002178 } else if (FormatTok->is(tok::r_brace)) {
2179 // Ignore stray "}". parseStructuralElement doesn't consume them.
2180 nextToken();
2181 addUnwrappedLine();
Ben Hamilton707e68f2018-05-30 15:21:38 +00002182 } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
2183 nextToken();
2184 parseObjCMethod();
Daniel Jaspera15da302013-08-28 08:04:23 +00002185 } else {
2186 parseStructuralElement();
2187 }
Nico Weber8696a8d2013-01-09 21:15:03 +00002188 } while (!eof());
2189}
2190
Nico Weber2ce0ac52013-01-09 23:25:37 +00002191void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weberc068ff72018-01-23 17:10:25 +00002192 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
2193 FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
Nico Weber7eecf4b2013-01-09 20:25:35 +00002194 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002195 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00002196
Ben Hamilton1462e842018-04-05 15:26:25 +00002197 // @interface can be followed by a lightweight generic
2198 // specialization list, then either a base class or a category.
2199 if (FormatTok->Tok.is(tok::less)) {
2200 // Unlike protocol lists, generic parameterizations support
2201 // nested angles:
2202 //
2203 // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
2204 // NSObject <NSCopying, NSSecureCoding>
2205 //
2206 // so we need to count how many open angles we have left.
2207 unsigned NumOpenAngles = 1;
2208 do {
2209 nextToken();
2210 // Early exit in case someone forgot a close angle.
2211 if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2212 FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2213 break;
2214 if (FormatTok->Tok.is(tok::less))
2215 ++NumOpenAngles;
2216 else if (FormatTok->Tok.is(tok::greater)) {
2217 assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
2218 --NumOpenAngles;
2219 }
2220 } while (!eof() && NumOpenAngles != 0);
2221 nextToken(); // Skip '>'.
2222 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002223 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00002224 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002225 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002226 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00002227 // Skip category, if present.
2228 parseParens();
2229
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002230 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002231 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00002232
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002233 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00002234 if (Style.BraceWrapping.AfterObjCDeclaration)
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002235 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00002236 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002237 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00002238
2239 // With instance variables, this puts '}' on its own line. Without instance
2240 // variables, this ends the @interface line.
2241 addUnwrappedLine();
2242
Nico Weber8696a8d2013-01-09 21:15:03 +00002243 parseObjCUntilAtEnd();
2244}
Nico Weber7eecf4b2013-01-09 20:25:35 +00002245
Nico Weberc068ff72018-01-23 17:10:25 +00002246// Returns true for the declaration/definition form of @protocol,
2247// false for the expression form.
2248bool UnwrappedLineParser::parseObjCProtocol() {
2249 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
Nico Weber8696a8d2013-01-09 21:15:03 +00002250 nextToken();
Nico Weberc068ff72018-01-23 17:10:25 +00002251
2252 if (FormatTok->is(tok::l_paren))
2253 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
2254 return false;
2255
2256 // The definition/declaration form,
2257 // @protocol Foo
2258 // - (int)someMethod;
2259 // @end
2260
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002261 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00002262
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002263 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002264 parseObjCProtocolList();
2265
2266 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002267 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002268 nextToken();
Nico Weberc068ff72018-01-23 17:10:25 +00002269 addUnwrappedLine();
2270 return true;
Nico Weber8696a8d2013-01-09 21:15:03 +00002271 }
2272
2273 addUnwrappedLine();
2274 parseObjCUntilAtEnd();
Nico Weberc068ff72018-01-23 17:10:25 +00002275 return true;
Nico Weber7eecf4b2013-01-09 20:25:35 +00002276}
2277
Daniel Jasperfca735c2015-02-19 16:14:18 +00002278void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
Martin Probst053f1aa2016-04-19 14:55:37 +00002279 bool IsImport = FormatTok->is(Keywords.kw_import);
2280 assert(IsImport || FormatTok->is(tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00002281 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00002282
Daniel Jasperec05fc72015-05-11 09:14:50 +00002283 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002284 if (FormatTok->is(tok::kw_default))
2285 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00002286
Martin Probst5f8445b2016-04-24 22:05:09 +00002287 // Consume "async function", "function" and "default function", so that these
2288 // get parsed as free-standing JS functions, i.e. do not require a trailing
2289 // semicolon.
2290 if (FormatTok->is(Keywords.kw_async))
2291 nextToken();
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002292 if (FormatTok->is(Keywords.kw_function)) {
2293 nextToken();
2294 return;
2295 }
2296
Martin Probst053f1aa2016-04-19 14:55:37 +00002297 // For imports, `export *`, `export {...}`, consume the rest of the line up
2298 // to the terminating `;`. For everything else, just return and continue
2299 // parsing the structural element, i.e. the declaration or expression for
2300 // `export default`.
2301 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
2302 !FormatTok->isStringLiteral())
2303 return;
Daniel Jasperfca735c2015-02-19 16:14:18 +00002304
Martin Probstd40bca42017-01-09 08:56:36 +00002305 while (!eof()) {
2306 if (FormatTok->is(tok::semi))
2307 return;
Krasimir Georgiev112c2e92017-11-09 13:22:03 +00002308 if (Line->Tokens.empty()) {
Martin Probstd40bca42017-01-09 08:56:36 +00002309 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
2310 // import statement should terminate.
2311 return;
2312 }
Daniel Jasperefc1a832016-01-07 08:53:35 +00002313 if (FormatTok->is(tok::l_brace)) {
2314 FormatTok->BlockKind = BK_Block;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002315 nextToken();
Daniel Jasperefc1a832016-01-07 08:53:35 +00002316 parseBracedList();
2317 } else {
2318 nextToken();
2319 }
Daniel Jasper354aa512015-02-19 16:07:32 +00002320 }
2321}
2322
Daniel Jasper3b203a62013-09-05 16:05:56 +00002323LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
2324 StringRef Prefix = "") {
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002325 llvm::dbgs() << Prefix << "Line(" << Line.Level
2326 << ", FSC=" << Line.FirstStartColumn << ")"
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002327 << (Line.InPPDirective ? " MACRO" : "") << ": ";
2328 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2329 E = Line.Tokens.end();
2330 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002331 llvm::dbgs() << I->Tok->Tok.getName() << "["
Manuel Klimek89628f62017-09-20 09:51:03 +00002332 << "T=" << I->Tok->Type << ", OC=" << I->Tok->OriginalColumn
2333 << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002334 }
2335 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2336 E = Line.Tokens.end();
2337 I != E; ++I) {
2338 const UnwrappedLineNode &Node = *I;
2339 for (SmallVectorImpl<UnwrappedLine>::const_iterator
2340 I = Node.Children.begin(),
2341 E = Node.Children.end();
2342 I != E; ++I) {
2343 printDebugInfo(*I, "\nChild: ");
2344 }
2345 }
2346 llvm::dbgs() << "\n";
2347}
2348
Daniel Jasperf7935112012-12-03 18:12:45 +00002349void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002350 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00002351 return;
Nicola Zaghen3538b392018-05-15 13:30:56 +00002352 LLVM_DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002353 if (CurrentLines == &Lines)
2354 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00002355 });
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002356 CurrentLines->push_back(std::move(*Line));
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002357 Line->Tokens.clear();
Krasimir Georgiev85c37042017-03-01 16:38:08 +00002358 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002359 Line->FirstStartColumn = 0;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002360 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002361 CurrentLines->append(
2362 std::make_move_iterator(PreprocessorDirectives.begin()),
2363 std::make_move_iterator(PreprocessorDirectives.end()));
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002364 PreprocessorDirectives.clear();
2365 }
Manuel Klimeke411aa82017-09-20 09:29:37 +00002366 // Disconnect the current token from the last token on the previous line.
2367 FormatTok->Previous = nullptr;
Daniel Jasperf7935112012-12-03 18:12:45 +00002368}
2369
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002370bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00002371
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002372bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002373 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
2374 FormatTok.NewlinesBefore > 0;
2375}
2376
Krasimir Georgiev91834222017-01-25 13:58:58 +00002377// Checks if \p FormatTok is a line comment that continues the line comment
2378// section on \p Line.
Krasimir Georgievea222a72017-05-22 10:07:56 +00002379static bool continuesLineCommentSection(const FormatToken &FormatTok,
2380 const UnwrappedLine &Line,
2381 llvm::Regex &CommentPragmasRegex) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002382 if (Line.Tokens.empty())
2383 return false;
Krasimir Georgiev84321612017-01-30 19:18:55 +00002384
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002385 StringRef IndentContent = FormatTok.TokenText;
2386 if (FormatTok.TokenText.startswith("//") ||
2387 FormatTok.TokenText.startswith("/*"))
2388 IndentContent = FormatTok.TokenText.substr(2);
2389 if (CommentPragmasRegex.match(IndentContent))
2390 return false;
2391
Krasimir Georgiev91834222017-01-25 13:58:58 +00002392 // If Line starts with a line comment, then FormatTok continues the comment
Krasimir Georgiev84321612017-01-30 19:18:55 +00002393 // section if its original column is greater or equal to the original start
Krasimir Georgiev91834222017-01-25 13:58:58 +00002394 // column of the line.
2395 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002396 // Define the min column token of a line as follows: if a line ends in '{' or
2397 // contains a '{' followed by a line comment, then the min column token is
2398 // that '{'. Otherwise, the min column token of the line is the first token of
2399 // the line.
2400 //
2401 // If Line starts with a token other than a line comment, then FormatTok
2402 // continues the comment section if its original column is greater than the
2403 // original start column of the min column token of the line.
Krasimir Georgiev91834222017-01-25 13:58:58 +00002404 //
2405 // For example, the second line comment continues the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002406 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002407 // // first line
2408 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002409 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002410 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002411 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002412 // // first line
2413 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002414 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002415 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002416 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002417 // int i; // first line
2418 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002419 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002420 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002421 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002422 // do { // first line
2423 // // second line
2424 // int i;
2425 // } while (true);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002426 //
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002427 // and:
2428 //
2429 // enum {
2430 // a, // first line
2431 // // second line
2432 // b
2433 // };
2434 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002435 // The second line comment doesn't continue the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002436 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002437 // // first line
2438 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002439 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002440 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002441 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002442 // int i; // first line
2443 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002444 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002445 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002446 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002447 // do { // first line
2448 // // second line
2449 // int i;
2450 // } while (true);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002451 //
2452 // and:
2453 //
2454 // enum {
2455 // a, // first line
2456 // // second line
2457 // };
Krasimir Georgiev84321612017-01-30 19:18:55 +00002458 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
2459
2460 // Scan for '{//'. If found, use the column of '{' as a min column for line
2461 // comment section continuation.
2462 const FormatToken *PreviousToken = nullptr;
Krasimir Georgievd86c25d2017-03-10 13:09:29 +00002463 for (const UnwrappedLineNode &Node : Line.Tokens) {
Krasimir Georgiev84321612017-01-30 19:18:55 +00002464 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
2465 isLineComment(*Node.Tok)) {
2466 MinColumnToken = PreviousToken;
2467 break;
2468 }
2469 PreviousToken = Node.Tok;
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002470
2471 // Grab the last newline preceding a token in this unwrapped line.
2472 if (Node.Tok->NewlinesBefore > 0) {
2473 MinColumnToken = Node.Tok;
2474 }
Krasimir Georgiev84321612017-01-30 19:18:55 +00002475 }
2476 if (PreviousToken && PreviousToken->is(tok::l_brace)) {
2477 MinColumnToken = PreviousToken;
2478 }
2479
Krasimir Georgievea222a72017-05-22 10:07:56 +00002480 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
2481 MinColumnToken);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002482}
2483
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002484void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
2485 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002486 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002487 I = CommentsBeforeNextToken.begin(),
2488 E = CommentsBeforeNextToken.end();
2489 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002490 // Line comments that belong to the same line comment section are put on the
2491 // same line since later we might want to reflow content between them.
Krasimir Georgiev753625b2017-01-31 13:32:38 +00002492 // Additional fine-grained breaking of line comment sections is controlled
2493 // by the class BreakableLineCommentSection in case it is desirable to keep
2494 // several line comment sections in the same unwrapped line.
2495 //
2496 // FIXME: Consider putting separate line comment sections as children to the
2497 // unwrapped line instead.
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002498 (*I)->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002499 continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002500 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002501 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002502 pushToken(*I);
2503 }
Daniel Jaspere60cba12015-05-13 11:35:53 +00002504 if (NewlineBeforeNext && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002505 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002506 CommentsBeforeNextToken.clear();
2507}
2508
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002509void UnwrappedLineParser::nextToken(int LevelDifference) {
Daniel Jasperf7935112012-12-03 18:12:45 +00002510 if (eof())
2511 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002512 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002513 pushToken(FormatTok);
Manuel Klimek89628f62017-09-20 09:51:03 +00002514 FormatToken *Previous = FormatTok;
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002515 if (Style.Language != FormatStyle::LK_JavaScript)
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002516 readToken(LevelDifference);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002517 else
2518 readTokenWithJavaScriptASI();
Manuel Klimeke411aa82017-09-20 09:29:37 +00002519 FormatTok->Previous = Previous;
Daniel Jasperb9a49902016-01-09 15:56:28 +00002520}
2521
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002522void UnwrappedLineParser::distributeComments(
2523 const SmallVectorImpl<FormatToken *> &Comments,
2524 const FormatToken *NextTok) {
2525 // Whether or not a line comment token continues a line is controlled by
Krasimir Georgievea222a72017-05-22 10:07:56 +00002526 // the method continuesLineCommentSection, with the following caveat:
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002527 //
2528 // Define a trail of Comments to be a nonempty proper postfix of Comments such
2529 // that each comment line from the trail is aligned with the next token, if
2530 // the next token exists. If a trail exists, the beginning of the maximal
2531 // trail is marked as a start of a new comment section.
2532 //
2533 // For example in this code:
2534 //
2535 // int a; // line about a
2536 // // line 1 about b
2537 // // line 2 about b
2538 // int b;
2539 //
2540 // the two lines about b form a maximal trail, so there are two sections, the
2541 // first one consisting of the single comment "// line about a" and the
2542 // second one consisting of the next two comments.
2543 if (Comments.empty())
2544 return;
2545 bool ShouldPushCommentsInCurrentLine = true;
2546 bool HasTrailAlignedWithNextToken = false;
2547 unsigned StartOfTrailAlignedWithNextToken = 0;
2548 if (NextTok) {
2549 // We are skipping the first element intentionally.
2550 for (unsigned i = Comments.size() - 1; i > 0; --i) {
2551 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
2552 HasTrailAlignedWithNextToken = true;
2553 StartOfTrailAlignedWithNextToken = i;
2554 }
2555 }
2556 }
2557 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
2558 FormatToken *FormatTok = Comments[i];
Manuel Klimek89628f62017-09-20 09:51:03 +00002559 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002560 FormatTok->ContinuesLineCommentSection = false;
2561 } else {
2562 FormatTok->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002563 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002564 }
2565 if (!FormatTok->ContinuesLineCommentSection &&
2566 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
2567 ShouldPushCommentsInCurrentLine = false;
2568 }
2569 if (ShouldPushCommentsInCurrentLine) {
2570 pushToken(FormatTok);
2571 } else {
2572 CommentsBeforeNextToken.push_back(FormatTok);
2573 }
2574 }
2575}
2576
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002577void UnwrappedLineParser::readToken(int LevelDifference) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002578 SmallVector<FormatToken *, 1> Comments;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002579 do {
2580 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00002581 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002582 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
2583 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002584 distributeComments(Comments, FormatTok);
2585 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002586 // If there is an unfinished unwrapped line, we flush the preprocessor
2587 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00002588 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002589 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002590 assert((LevelDifference >= 0 ||
2591 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2592 "LevelDifference makes Line->Level negative");
2593 Line->Level += LevelDifference;
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00002594 // Comments stored before the preprocessor directive need to be output
2595 // before the preprocessor directive, at the same level as the
2596 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002597 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002598 parsePPDirective();
2599 }
Manuel Klimek68b03042014-04-14 09:14:11 +00002600 while (FormatTok->Type == TT_ConflictStart ||
2601 FormatTok->Type == TT_ConflictEnd ||
2602 FormatTok->Type == TT_ConflictAlternative) {
2603 if (FormatTok->Type == TT_ConflictStart) {
2604 conditionalCompilationStart(/*Unreachable=*/false);
2605 } else if (FormatTok->Type == TT_ConflictAlternative) {
2606 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002607 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00002608 conditionalCompilationEnd();
2609 }
2610 FormatTok = Tokens->getNextToken();
2611 FormatTok->MustBreakBefore = true;
2612 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002613
Francois Ferranda98a95c2017-07-28 07:56:14 +00002614 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002615 !Line->InPPDirective) {
2616 continue;
2617 }
2618
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002619 if (!FormatTok->Tok.is(tok::comment)) {
2620 distributeComments(Comments, FormatTok);
2621 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002622 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002623 }
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002624
2625 Comments.push_back(FormatTok);
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002626 } while (!eof());
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002627
2628 distributeComments(Comments, nullptr);
2629 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002630}
2631
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002632void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002633 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002634 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002635 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002636 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00002637 }
Daniel Jasperf7935112012-12-03 18:12:45 +00002638}
2639
Daniel Jasper8d1832e2013-01-07 13:26:07 +00002640} // end namespace format
2641} // end namespace clang