blob: 752357c1affb39100e7fae8d77361f678765f882 [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();
Jonas Toth90d2aa22018-08-24 17:25:06 +0000353 FormatToken *Next;
354 do {
355 Next = Tokens->getNextToken();
356 } while (Next && Next->is(tok::comment));
Nico Weberc29f83b2018-01-23 16:30:56 +0000357 FormatTok = Tokens->setPosition(StoredPosition);
358 if (Next && Next->isNot(tok::colon)) {
359 // default not followed by ':' is not a case label; treat it like
360 // an identifier.
361 parseStructuralElement();
362 break;
363 }
364 // Else, if it is 'default:', fall through to the case handling.
Nico Weberf1add5e2018-01-24 01:47:22 +0000365 LLVM_FALLTHROUGH;
Nico Weberc29f83b2018-01-23 16:30:56 +0000366 }
Daniel Jasper516d7972013-07-25 11:31:57 +0000367 case tok::kw_case:
Manuel Klimek89628f62017-09-20 09:51:03 +0000368 if (Style.Language == FormatStyle::LK_JavaScript &&
369 Line->MustBeDeclaration) {
Martin Probstf785fd92017-08-04 17:07:15 +0000370 // A 'case: string' style field declaration.
371 parseStructuralElement();
372 break;
373 }
Daniel Jasper72407622013-09-02 08:26:29 +0000374 if (!SwitchLabelEncountered &&
375 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
376 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000377 SwitchLabelEncountered = true;
378 parseStructuralElement();
379 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000380 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000381 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000382 break;
383 }
384 } while (!eof());
385}
386
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000387void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
Manuel Klimekab419912013-05-23 09:41:43 +0000388 // We'll parse forward through the tokens until we hit
389 // a closing brace or eof - note that getNextToken() will
390 // parse macros, so this will magically work inside macro
391 // definitions, too.
392 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000393 FormatToken *Tok = FormatTok;
Manuel Klimek89628f62017-09-20 09:51:03 +0000394 const FormatToken *PrevTok = Tok->Previous;
Manuel Klimekab419912013-05-23 09:41:43 +0000395 // Keep a stack of positions of lbrace tokens. We will
396 // update information about whether an lbrace starts a
397 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000398 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000399 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000400 do {
Daniel Jaspereb65e912015-12-21 18:31:15 +0000401 // Get next non-comment token.
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000402 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000403 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000404 do {
405 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000406 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000407 } while (NextTok->is(tok::comment));
408
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000409 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000410 case tok::l_brace:
Martin Probst95ed8e72017-05-31 09:29:40 +0000411 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) {
Martin Probste8e27ca2017-11-25 09:33:47 +0000412 if (PrevTok->isOneOf(tok::colon, tok::less))
413 // A ':' indicates this code is in a type, or a braced list
414 // following a label in an object literal ({a: {b: 1}}).
415 // A '<' could be an object used in a comparison, but that is nonsense
416 // code (can never return true), so more likely it is a generic type
417 // argument (`X<{a: string; b: number}>`).
418 // The code below could be confused by semicolons between the
419 // individual members in a type member list, which would normally
420 // trigger BK_Block. In both cases, this must be parsed as an inline
421 // braced init.
Martin Probst95ed8e72017-05-31 09:29:40 +0000422 Tok->BlockKind = BK_BracedInit;
423 else if (PrevTok->is(tok::r_paren))
424 // `) { }` can only occur in function or method declarations in JS.
425 Tok->BlockKind = BK_Block;
426 } else {
Daniel Jasperb9a49902016-01-09 15:56:28 +0000427 Tok->BlockKind = BK_Unknown;
Martin Probst95ed8e72017-05-31 09:29:40 +0000428 }
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000429 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000430 break;
431 case tok::r_brace:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000432 if (LBraceStack.empty())
433 break;
434 if (LBraceStack.back()->BlockKind == BK_Unknown) {
435 bool ProbablyBracedList = false;
436 if (Style.Language == FormatStyle::LK_Proto) {
437 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
438 } else {
439 // Using OriginalColumn to distinguish between ObjC methods and
440 // binary operators is a bit hacky.
441 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
442 NextTok->OriginalColumn == 0;
Daniel Jasper91b032a2014-05-22 12:46:38 +0000443
Daniel Jasperb9a49902016-01-09 15:56:28 +0000444 // If there is a comma, semicolon or right paren after the closing
445 // brace, we assume this is a braced initializer list. Note that
446 // regardless how we mark inner braces here, we will overwrite the
447 // BlockKind later if we parse a braced list (where all blocks
448 // inside are by default braced lists), or when we explicitly detect
449 // blocks (for example while parsing lambdas).
Martin Probst95ed8e72017-05-31 09:29:40 +0000450 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
451 // braced list in JS.
Daniel Jasperb9a49902016-01-09 15:56:28 +0000452 ProbablyBracedList =
Daniel Jasperacffeb82016-03-05 18:34:26 +0000453 (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probste1e12a72016-08-19 14:35:01 +0000454 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
455 Keywords.kw_as)) ||
Martin Probstb7fb2672017-05-10 13:53:29 +0000456 (Style.isCpp() && NextTok->is(tok::l_paren)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000457 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
458 tok::r_paren, tok::r_square, tok::l_brace,
Manuel Klimekd0f3fe52018-04-11 14:51:54 +0000459 tok::ellipsis) ||
Daniel Jaspere4ada022016-12-13 10:05:03 +0000460 (NextTok->is(tok::identifier) &&
461 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000462 (NextTok->is(tok::semi) &&
463 (!ExpectClassBody || LBraceStack.size() != 1)) ||
464 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Manuel Klimekd0f3fe52018-04-11 14:51:54 +0000465 if (NextTok->is(tok::l_square)) {
466 // We can have an array subscript after a braced init
467 // list, but C++11 attributes are expected after blocks.
468 NextTok = Tokens->getNextToken();
469 ++ReadTokens;
470 ProbablyBracedList = NextTok->isNot(tok::l_square);
471 }
Manuel Klimekab419912013-05-23 09:41:43 +0000472 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000473 if (ProbablyBracedList) {
474 Tok->BlockKind = BK_BracedInit;
475 LBraceStack.back()->BlockKind = BK_BracedInit;
476 } else {
477 Tok->BlockKind = BK_Block;
478 LBraceStack.back()->BlockKind = BK_Block;
479 }
Manuel Klimekab419912013-05-23 09:41:43 +0000480 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000481 LBraceStack.pop_back();
Manuel Klimekab419912013-05-23 09:41:43 +0000482 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000483 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000484 case tok::semi:
485 case tok::kw_if:
486 case tok::kw_while:
487 case tok::kw_for:
488 case tok::kw_switch:
489 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000490 case tok::kw___try:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000491 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000492 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000493 break;
494 default:
495 break;
496 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000497 PrevTok = Tok;
Manuel Klimekab419912013-05-23 09:41:43 +0000498 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000499 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Daniel Jasperb9a49902016-01-09 15:56:28 +0000500
Manuel Klimekab419912013-05-23 09:41:43 +0000501 // Assume other blocks for all unclosed opening braces.
502 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000503 if (LBraceStack[i]->BlockKind == BK_Unknown)
504 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000505 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000506
Manuel Klimekab419912013-05-23 09:41:43 +0000507 FormatTok = Tokens->setPosition(StoredPosition);
508}
509
Francois Ferranda98a95c2017-07-28 07:56:14 +0000510template <class T>
511static inline void hash_combine(std::size_t &seed, const T &v) {
512 std::hash<T> hasher;
513 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
514}
515
516size_t UnwrappedLineParser::computePPHash() const {
517 size_t h = 0;
518 for (const auto &i : PPStack) {
519 hash_combine(h, size_t(i.Kind));
520 hash_combine(h, i.Line);
521 }
522 return h;
523}
524
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000525void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
526 bool MunchSemi) {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000527 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
528 "'{' or macro block token expected");
529 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
Daniel Jaspereb65e912015-12-21 18:31:15 +0000530 FormatTok->BlockKind = BK_Block;
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000531
Francois Ferranda98a95c2017-07-28 07:56:14 +0000532 size_t PPStartHash = computePPHash();
533
Daniel Jasper516d7972013-07-25 11:31:57 +0000534 unsigned InitialLevel = Line->Level;
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000535 nextToken(/*LevelDifference=*/AddLevel ? 1 : 0);
Daniel Jasperf7935112012-12-03 18:12:45 +0000536
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000537 if (MacroBlock && FormatTok->is(tok::l_paren))
538 parseParens();
539
Francois Ferranda98a95c2017-07-28 07:56:14 +0000540 size_t NbPreprocessorDirectives =
541 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000542 addUnwrappedLine();
Francois Ferranda98a95c2017-07-28 07:56:14 +0000543 size_t OpeningLineIndex =
544 CurrentLines->empty()
545 ? (UnwrappedLine::kInvalidIndex)
546 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
Daniel Jasperf7935112012-12-03 18:12:45 +0000547
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000548 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
549 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000550 if (AddLevel)
551 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000552 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000553
Marianne Mailhot-Sarrasin03137c62016-04-14 14:56:49 +0000554 if (eof())
555 return;
556
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000557 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
558 : !FormatTok->is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000559 Line->Level = InitialLevel;
Daniel Jaspereb65e912015-12-21 18:31:15 +0000560 FormatTok->BlockKind = BK_Block;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000561 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000562 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000563
Francois Ferranda98a95c2017-07-28 07:56:14 +0000564 size_t PPEndHash = computePPHash();
565
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000566 // Munch the closing brace.
567 nextToken(/*LevelDifference=*/AddLevel ? -1 : 0);
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000568
569 if (MacroBlock && FormatTok->is(tok::l_paren))
570 parseParens();
571
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000572 if (MunchSemi && FormatTok->Tok.is(tok::semi))
573 nextToken();
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000574 Line->Level = InitialLevel;
Francois Ferranda98a95c2017-07-28 07:56:14 +0000575
576 if (PPStartHash == PPEndHash) {
577 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
578 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
579 // Update the opening line to add the forward reference as well
Manuel Klimek0dddcf72018-04-23 09:34:26 +0000580 (*CurrentLines)[OpeningLineIndex].MatchingClosingBlockLineIndex =
Francois Ferranda98a95c2017-07-28 07:56:14 +0000581 CurrentLines->size() - 1;
582 }
Francois Ferrande56a8292017-06-14 12:29:47 +0000583 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000584}
585
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000586static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000587 // FIXME: Closure-library specific stuff should not be hard-coded but be
588 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000589 if (Line.Tokens.size() < 4)
590 return false;
591 auto I = Line.Tokens.begin();
592 if (I->Tok->TokenText != "goog")
593 return false;
594 ++I;
595 if (I->Tok->isNot(tok::period))
596 return false;
597 ++I;
598 if (I->Tok->TokenText != "scope")
599 return false;
600 ++I;
601 return I->Tok->is(tok::l_paren);
602}
603
Martin Probst101ec892017-05-09 20:04:09 +0000604static bool isIIFE(const UnwrappedLine &Line,
605 const AdditionalKeywords &Keywords) {
606 // Look for the start of an immediately invoked anonymous function.
607 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
608 // This is commonly done in JavaScript to create a new, anonymous scope.
609 // Example: (function() { ... })()
610 if (Line.Tokens.size() < 3)
611 return false;
612 auto I = Line.Tokens.begin();
613 if (I->Tok->isNot(tok::l_paren))
614 return false;
615 ++I;
616 if (I->Tok->isNot(Keywords.kw_function))
617 return false;
618 ++I;
619 return I->Tok->is(tok::l_paren);
620}
621
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000622static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
623 const FormatToken &InitialToken) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000624 if (InitialToken.is(tok::kw_namespace))
625 return Style.BraceWrapping.AfterNamespace;
626 if (InitialToken.is(tok::kw_class))
627 return Style.BraceWrapping.AfterClass;
628 if (InitialToken.is(tok::kw_union))
629 return Style.BraceWrapping.AfterUnion;
630 if (InitialToken.is(tok::kw_struct))
631 return Style.BraceWrapping.AfterStruct;
632 return false;
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000633}
634
Manuel Klimek516e0542013-09-04 13:25:30 +0000635void UnwrappedLineParser::parseChildBlock() {
636 FormatTok->BlockKind = BK_Block;
637 nextToken();
638 {
Manuel Klimek89628f62017-09-20 09:51:03 +0000639 bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript &&
640 (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
Manuel Klimek516e0542013-09-04 13:25:30 +0000641 ScopedLineState LineState(*this);
642 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
643 /*MustBeDeclaration=*/false);
Martin Probst101ec892017-05-09 20:04:09 +0000644 Line->Level += SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000645 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000646 flushComments(isOnNewLine(*FormatTok));
Martin Probst101ec892017-05-09 20:04:09 +0000647 Line->Level -= SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000648 }
649 nextToken();
650}
651
Daniel Jasperf7935112012-12-03 18:12:45 +0000652void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000653 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000654 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000655 nextToken();
656
Craig Topper2145bc02014-05-09 08:15:10 +0000657 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000658 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000659 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000660 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000661
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000662 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000663 case tok::pp_define:
664 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000665 return;
666 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000667 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000668 break;
669 case tok::pp_ifdef:
670 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000671 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000672 break;
673 case tok::pp_else:
674 parsePPElse();
675 break;
676 case tok::pp_elif:
677 parsePPElIf();
678 break;
679 case tok::pp_endif:
680 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000681 break;
682 default:
683 parsePPUnknown();
684 break;
685 }
686}
687
Manuel Klimek68b03042014-04-14 09:14:11 +0000688void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
Francois Ferranda98a95c2017-07-28 07:56:14 +0000689 size_t Line = CurrentLines->size();
690 if (CurrentLines == &PreprocessorDirectives)
691 Line += Lines.size();
692
693 if (Unreachable ||
694 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
695 PPStack.push_back({PP_Unreachable, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000696 else
Francois Ferranda98a95c2017-07-28 07:56:14 +0000697 PPStack.push_back({PP_Conditional, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000698}
699
Manuel Klimek68b03042014-04-14 09:14:11 +0000700void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000701 ++PPBranchLevel;
702 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
703 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
704 PPLevelBranchIndex.push_back(0);
705 PPLevelBranchCount.push_back(0);
706 }
707 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000708 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
709 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000710}
711
Manuel Klimek68b03042014-04-14 09:14:11 +0000712void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000713 if (!PPStack.empty())
714 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000715 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
716 if (!PPChainBranchIndex.empty())
717 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000718 conditionalCompilationCondition(
719 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
720 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000721}
722
Manuel Klimek68b03042014-04-14 09:14:11 +0000723void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000724 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
725 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
726 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000727 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
728 }
729 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000730 // Guard against #endif's without #if.
Krasimir Georgievad47c902017-08-30 14:34:57 +0000731 if (PPBranchLevel > -1)
Manuel Klimek14bd9172014-01-29 08:49:02 +0000732 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000733 if (!PPChainBranchIndex.empty())
734 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000735 if (!PPStack.empty())
736 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000737}
738
739void UnwrappedLineParser::parsePPIf(bool IfDef) {
Daniel Jasper62703eb2017-03-01 11:10:11 +0000740 bool IfNDef = FormatTok->is(tok::pp_ifndef);
Manuel Klimek68b03042014-04-14 09:14:11 +0000741 nextToken();
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000742 bool Unreachable = false;
743 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
744 Unreachable = true;
Daniel Jasper62703eb2017-03-01 11:10:11 +0000745 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000746 Unreachable = true;
747 conditionalCompilationStart(Unreachable);
Krasimir Georgievad47c902017-08-30 14:34:57 +0000748 FormatToken *IfCondition = FormatTok;
749 // If there's a #ifndef on the first line, and the only lines before it are
750 // comments, it could be an include guard.
751 bool MaybeIncludeGuard = IfNDef;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000752 if (IncludeGuard == IG_Inited && MaybeIncludeGuard)
Krasimir Georgievad47c902017-08-30 14:34:57 +0000753 for (auto &Line : Lines) {
754 if (!Line.Tokens.front().Tok->is(tok::comment)) {
755 MaybeIncludeGuard = false;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000756 IncludeGuard = IG_Rejected;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000757 break;
758 }
759 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000760 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000761 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000762 ++PPBranchLevel;
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000763 if (IncludeGuard == IG_Inited && MaybeIncludeGuard) {
764 IncludeGuard = IG_IfNdefed;
765 IncludeGuardToken = IfCondition;
766 }
Manuel Klimek68b03042014-04-14 09:14:11 +0000767}
768
769void UnwrappedLineParser::parsePPElse() {
Krasimir Georgievad47c902017-08-30 14:34:57 +0000770 // If a potential include guard has an #else, it's not an include guard.
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000771 if (IncludeGuard == IG_Defined && PPBranchLevel == 0)
772 IncludeGuard = IG_Rejected;
Manuel Klimek68b03042014-04-14 09:14:11 +0000773 conditionalCompilationAlternative();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000774 if (PPBranchLevel > -1)
775 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000776 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000777 ++PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000778}
779
780void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
781
782void UnwrappedLineParser::parsePPEndIf() {
783 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000784 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000785 // If the #endif of a potential include guard is the last thing in the file,
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000786 // then we found an include guard.
Krasimir Georgievad47c902017-08-30 14:34:57 +0000787 unsigned TokenPosition = Tokens->getPosition();
788 FormatToken *PeekNext = AllTokens[TokenPosition];
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000789 if (IncludeGuard == IG_Defined && PPBranchLevel == -1 &&
790 PeekNext->is(tok::eof) &&
Daniel Jasper4df130f2017-09-04 13:33:52 +0000791 Style.IndentPPDirectives != FormatStyle::PPDIS_None)
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000792 IncludeGuard = IG_Found;
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000793}
794
Manuel Klimek1abf7892013-01-04 23:34:14 +0000795void UnwrappedLineParser::parsePPDefine() {
796 nextToken();
797
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000798 if (FormatTok->Tok.getKind() != tok::identifier) {
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000799 IncludeGuard = IG_Rejected;
800 IncludeGuardToken = nullptr;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000801 parsePPUnknown();
802 return;
803 }
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000804
805 if (IncludeGuard == IG_IfNdefed &&
806 IncludeGuardToken->TokenText == FormatTok->TokenText) {
807 IncludeGuard = IG_Defined;
808 IncludeGuardToken = nullptr;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000809 for (auto &Line : Lines) {
810 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000811 IncludeGuard = IG_Rejected;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000812 break;
813 }
814 }
815 }
Mark Zeren1c3afaf2018-02-05 15:59:00 +0000816
Manuel Klimek1abf7892013-01-04 23:34:14 +0000817 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000818 if (FormatTok->Tok.getKind() == tok::l_paren &&
819 FormatTok->WhitespaceRange.getBegin() ==
820 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000821 parseParens();
822 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000823 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
824 Line->Level += PPBranchLevel + 1;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000825 addUnwrappedLine();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000826 ++Line->Level;
Manuel Klimek1b896292013-01-07 09:34:28 +0000827
828 // Errors during a preprocessor directive can only affect the layout of the
829 // preprocessor directive, and thus we ignore them. An alternative approach
830 // would be to use the same approach we use on the file level (no
831 // re-indentation if there was a structural error) within the macro
832 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000833 parseFile();
834}
835
836void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000837 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000838 nextToken();
839 } while (!eof());
Krasimir Georgievad47c902017-08-30 14:34:57 +0000840 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
841 Line->Level += PPBranchLevel + 1;
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000842 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000843}
844
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000845// Here we blacklist certain tokens that are not usually the first token in an
846// unwrapped line. This is used in attempt to distinguish macro calls without
847// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000848static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000849 // Semicolon can be a null-statement, l_square can be a start of a macro or
850 // a C++11 attribute, but this doesn't seem to be common.
851 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
852 Tok.isNot(tok::l_square) &&
853 // Tokens that can only be used as binary operators and a part of
854 // overloaded operator names.
855 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
856 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
857 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
858 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
859 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
860 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
861 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
862 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
863 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
864 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
865 Tok.isNot(tok::lesslessequal) &&
866 // Colon is used in labels, base class lists, initializer lists,
867 // range-based for loops, ternary operator, but should never be the
868 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000869 Tok.isNot(tok::colon) &&
870 // 'noexcept' is a trailing annotation.
871 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000872}
873
Martin Probst533965c2016-04-19 18:19:06 +0000874static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
875 const FormatToken *FormatTok) {
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000876 // FIXME: This returns true for C/C++ keywords like 'struct'.
877 return FormatTok->is(tok::identifier) &&
878 (FormatTok->Tok.getIdentifierInfo() == nullptr ||
Martin Probst3dbbefa2016-11-10 16:21:02 +0000879 !FormatTok->isOneOf(
880 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
881 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
882 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
883 Keywords.kw_let, Keywords.kw_var, tok::kw_const,
884 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
Manuel Klimek89628f62017-09-20 09:51:03 +0000885 Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws,
886 Keywords.kw_from));
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000887}
888
Martin Probst533965c2016-04-19 18:19:06 +0000889static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
890 const FormatToken *FormatTok) {
Martin Probstb9316ff2016-09-18 17:21:52 +0000891 return FormatTok->Tok.isLiteral() ||
892 FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
893 mustBeJSIdent(Keywords, FormatTok);
Martin Probst533965c2016-04-19 18:19:06 +0000894}
895
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000896// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
897// when encountered after a value (see mustBeJSIdentOrValue).
898static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
899 const FormatToken *FormatTok) {
900 return FormatTok->isOneOf(
Martin Probst5f8445b2016-04-24 22:05:09 +0000901 tok::kw_return, Keywords.kw_yield,
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000902 // conditionals
903 tok::kw_if, tok::kw_else,
904 // loops
905 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
906 // switch/case
907 tok::kw_switch, tok::kw_case,
908 // exceptions
909 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
910 // declaration
911 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
Martin Probst5f8445b2016-04-24 22:05:09 +0000912 Keywords.kw_async, Keywords.kw_function,
913 // import/export
914 Keywords.kw_import, tok::kw_export);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000915}
916
917// readTokenWithJavaScriptASI reads the next token and terminates the current
918// line if JavaScript Automatic Semicolon Insertion must
919// happen between the current token and the next token.
920//
921// This method is conservative - it cannot cover all edge cases of JavaScript,
922// but only aims to correctly handle certain well known cases. It *must not*
923// return true in speculative cases.
924void UnwrappedLineParser::readTokenWithJavaScriptASI() {
925 FormatToken *Previous = FormatTok;
926 readToken();
927 FormatToken *Next = FormatTok;
928
929 bool IsOnSameLine =
930 CommentsBeforeNextToken.empty()
931 ? Next->NewlinesBefore == 0
932 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
933 if (IsOnSameLine)
934 return;
935
936 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
Martin Probst717f6dc2016-10-21 05:11:38 +0000937 bool PreviousStartsTemplateExpr =
938 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${");
Martin Probst7e0f25b2017-11-25 09:19:42 +0000939 if (PreviousMustBeValue || Previous->is(tok::r_paren)) {
940 // If the line contains an '@' sign, the previous token might be an
941 // annotation, which can precede another identifier/value.
942 bool HasAt = std::find_if(Line->Tokens.begin(), Line->Tokens.end(),
943 [](UnwrappedLineNode &LineNode) {
944 return LineNode.Tok->is(tok::at);
945 }) != Line->Tokens.end();
946 if (HasAt)
Martin Probstbbffeac2016-04-11 07:35:57 +0000947 return;
948 }
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000949 if (Next->is(tok::exclaim) && PreviousMustBeValue)
Martin Probstd40bca42017-01-09 08:56:36 +0000950 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000951 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
Martin Probst717f6dc2016-10-21 05:11:38 +0000952 bool NextEndsTemplateExpr =
953 Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
954 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
955 (PreviousMustBeValue ||
956 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
957 tok::minusminus)))
Martin Probstd40bca42017-01-09 08:56:36 +0000958 return addUnwrappedLine();
Martin Probst0a19d432017-08-09 15:19:16 +0000959 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) &&
960 isJSDeclOrStmt(Keywords, Next))
Martin Probstd40bca42017-01-09 08:56:36 +0000961 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000962}
963
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000964void UnwrappedLineParser::parseStructuralElement() {
Daniel Jasper498f5582015-12-25 08:53:31 +0000965 assert(!FormatTok->is(tok::l_brace));
966 if (Style.Language == FormatStyle::LK_TableGen &&
967 FormatTok->is(tok::pp_include)) {
968 nextToken();
969 if (FormatTok->is(tok::string_literal))
970 nextToken();
971 addUnwrappedLine();
972 return;
973 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000974 switch (FormatTok->Tok.getKind()) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000975 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000976 nextToken();
977 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000978 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000979 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000980 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000981 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000982 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000983 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000984 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000985 break;
986 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000987 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000988 nextToken();
989 }
990 }
991 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000992 case tok::kw_namespace:
993 parseNamespace();
994 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000995 case tok::kw_public:
996 case tok::kw_protected:
997 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +0000998 if (Style.Language == FormatStyle::LK_Java ||
999 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001000 nextToken();
1001 else
1002 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +00001003 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001004 case tok::kw_if:
1005 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +00001006 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001007 case tok::kw_for:
1008 case tok::kw_while:
1009 parseForOrWhileLoop();
1010 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001011 case tok::kw_do:
1012 parseDoWhile();
1013 return;
1014 case tok::kw_switch:
Martin Probstf785fd92017-08-04 17:07:15 +00001015 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1016 // 'switch: string' field declaration.
1017 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001018 parseSwitch();
1019 return;
1020 case tok::kw_default:
Martin Probstf785fd92017-08-04 17:07:15 +00001021 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1022 // 'default: string' field declaration.
1023 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001024 nextToken();
Nico Weberc29f83b2018-01-23 16:30:56 +00001025 if (FormatTok->is(tok::colon)) {
1026 parseLabel();
1027 return;
1028 }
1029 // e.g. "default void f() {}" in a Java interface.
1030 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001031 case tok::kw_case:
Martin Probstf785fd92017-08-04 17:07:15 +00001032 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1033 // 'case: string' field declaration.
1034 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001035 parseCaseLabel();
1036 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001037 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +00001038 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +00001039 parseTryCatch();
1040 return;
Manuel Klimekae610d12013-01-21 14:32:05 +00001041 case tok::kw_extern:
1042 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001043 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +00001044 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001045 if (FormatTok->Tok.is(tok::l_brace)) {
Krasimir Georgievd6ce9372017-09-15 11:23:50 +00001046 if (Style.BraceWrapping.AfterExternBlock) {
1047 addUnwrappedLine();
1048 parseBlock(/*MustBeDeclaration=*/true);
1049 } else {
1050 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
1051 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001052 addUnwrappedLine();
1053 return;
1054 }
1055 }
Daniel Jaspere1e43192014-04-01 12:55:11 +00001056 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +00001057 case tok::kw_export:
1058 if (Style.Language == FormatStyle::LK_JavaScript) {
1059 parseJavaScriptEs6ImportExport();
1060 return;
1061 }
Sam McCall6f3778c2018-09-05 07:44:02 +00001062 if (!Style.isCpp())
1063 break;
1064 // Handle C++ "(inline|export) namespace".
1065 LLVM_FALLTHROUGH;
1066 case tok::kw_inline:
1067 nextToken();
1068 if (FormatTok->Tok.is(tok::kw_namespace)) {
1069 parseNamespace();
1070 return;
1071 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001072 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +00001073 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001074 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001075 parseForOrWhileLoop();
1076 return;
1077 }
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001078 if (FormatTok->is(TT_MacroBlockBegin)) {
1079 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
1080 /*MunchSemi=*/false);
1081 return;
1082 }
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001083 if (FormatTok->is(Keywords.kw_import)) {
1084 if (Style.Language == FormatStyle::LK_JavaScript) {
1085 parseJavaScriptEs6ImportExport();
1086 return;
1087 }
1088 if (Style.Language == FormatStyle::LK_Proto) {
1089 nextToken();
Daniel Jasper8b61d142016-06-20 20:39:53 +00001090 if (FormatTok->is(tok::kw_public))
1091 nextToken();
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001092 if (!FormatTok->is(tok::string_literal))
1093 return;
1094 nextToken();
1095 if (FormatTok->is(tok::semi))
1096 nextToken();
1097 addUnwrappedLine();
1098 return;
1099 }
Daniel Jasper354aa512015-02-19 16:07:32 +00001100 }
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001101 if (Style.isCpp() &&
Daniel Jasper72b33572017-03-31 12:04:37 +00001102 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
Daniel Jaspera00de632015-12-01 12:05:04 +00001103 Keywords.kw_slots, Keywords.kw_qslots)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001104 nextToken();
1105 if (FormatTok->is(tok::colon)) {
1106 nextToken();
1107 addUnwrappedLine();
Daniel Jasper31343832016-07-27 10:13:24 +00001108 return;
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001109 }
Daniel Jasper53395402015-04-07 15:04:40 +00001110 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001111 // In all other cases, parse the declaration.
1112 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001113 default:
1114 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001115 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001116 do {
Manuel Klimeke411aa82017-09-20 09:29:37 +00001117 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001118 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +00001119 case tok::at:
1120 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001121 if (FormatTok->Tok.is(tok::l_brace)) {
1122 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001123 parseBracedList();
Nico Weberc068ff72018-01-23 17:10:25 +00001124 break;
1125 }
1126 switch (FormatTok->Tok.getObjCKeywordID()) {
1127 case tok::objc_public:
1128 case tok::objc_protected:
1129 case tok::objc_package:
1130 case tok::objc_private:
1131 return parseAccessSpecifier();
1132 case tok::objc_interface:
1133 case tok::objc_implementation:
1134 return parseObjCInterfaceOrImplementation();
1135 case tok::objc_protocol:
1136 if (parseObjCProtocol())
1137 return;
1138 break;
1139 case tok::objc_end:
1140 return; // Handled by the caller.
1141 case tok::objc_optional:
1142 case tok::objc_required:
1143 nextToken();
1144 addUnwrappedLine();
1145 return;
1146 case tok::objc_autoreleasepool:
1147 nextToken();
1148 if (FormatTok->Tok.is(tok::l_brace)) {
Francois Ferranda2484b22018-02-27 13:48:27 +00001149 if (Style.BraceWrapping.AfterControlStatement)
Nico Weberc068ff72018-01-23 17:10:25 +00001150 addUnwrappedLine();
1151 parseBlock(/*MustBeDeclaration=*/false);
1152 }
1153 addUnwrappedLine();
1154 return;
Francois Ferrandba91c3d2018-02-27 13:48:21 +00001155 case tok::objc_synchronized:
1156 nextToken();
1157 if (FormatTok->Tok.is(tok::l_paren))
1158 // Skip synchronization object
1159 parseParens();
1160 if (FormatTok->Tok.is(tok::l_brace)) {
Francois Ferranda2484b22018-02-27 13:48:27 +00001161 if (Style.BraceWrapping.AfterControlStatement)
Francois Ferrandba91c3d2018-02-27 13:48:21 +00001162 addUnwrappedLine();
1163 parseBlock(/*MustBeDeclaration=*/false);
1164 }
1165 addUnwrappedLine();
1166 return;
Nico Weberc068ff72018-01-23 17:10:25 +00001167 case tok::objc_try:
1168 // This branch isn't strictly necessary (the kw_try case below would
1169 // do this too after the tok::at is parsed above). But be explicit.
1170 parseTryCatch();
1171 return;
1172 default:
1173 break;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001174 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001175 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001176 case tok::kw_enum:
Daniel Jaspera7900ad2016-05-08 18:12:22 +00001177 // Ignore if this is part of "template <enum ...".
1178 if (Previous && Previous->is(tok::less)) {
1179 nextToken();
1180 break;
1181 }
1182
Daniel Jasper90cf3802015-06-17 09:44:02 +00001183 // parseEnum falls through and does not yet add an unwrapped line as an
1184 // enum definition can start a structural element.
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001185 if (!parseEnum())
1186 break;
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001187 // This only applies for C++.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001188 if (!Style.isCpp()) {
Daniel Jasper90cf3802015-06-17 09:44:02 +00001189 addUnwrappedLine();
1190 return;
1191 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001192 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001193 case tok::kw_typedef:
1194 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +00001195 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1196 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001197 parseEnum();
1198 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001199 case tok::kw_struct:
1200 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +00001201 case tok::kw_class:
Daniel Jasper910807d2015-06-12 04:52:02 +00001202 // parseRecord falls through and does not yet add an unwrapped line as a
1203 // record declaration or definition can start a structural element.
Manuel Klimeke01bab52013-01-15 13:38:33 +00001204 parseRecord();
Daniel Jasper910807d2015-06-12 04:52:02 +00001205 // This does not apply for Java and JavaScript.
1206 if (Style.Language == FormatStyle::LK_Java ||
1207 Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperd5ec65b2016-01-08 07:06:07 +00001208 if (FormatTok->is(tok::semi))
1209 nextToken();
Daniel Jasper910807d2015-06-12 04:52:02 +00001210 addUnwrappedLine();
1211 return;
1212 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001213 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +00001214 case tok::period:
1215 nextToken();
1216 // In Java, classes have an implicit static member "class".
1217 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
1218 FormatTok->is(tok::kw_class))
1219 nextToken();
Daniel Jasperba52fcb2015-09-28 14:29:45 +00001220 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
1221 FormatTok->Tok.getIdentifierInfo())
1222 // JavaScript only has pseudo keywords, all keywords are allowed to
1223 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1224 nextToken();
Daniel Jaspere5d74862014-11-26 08:17:08 +00001225 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001226 case tok::semi:
1227 nextToken();
1228 addUnwrappedLine();
1229 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001230 case tok::r_brace:
1231 addUnwrappedLine();
1232 return;
Daniel Jasperf7935112012-12-03 18:12:45 +00001233 case tok::l_paren:
1234 parseParens();
1235 break;
Daniel Jasper5af04a42015-10-07 03:43:10 +00001236 case tok::kw_operator:
1237 nextToken();
1238 if (FormatTok->isBinaryOperator())
1239 nextToken();
1240 break;
Manuel Klimek516e0542013-09-04 13:25:30 +00001241 case tok::caret:
1242 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +00001243 if (FormatTok->Tok.isAnyIdentifier() ||
1244 FormatTok->isSimpleTypeSpecifier())
1245 nextToken();
1246 if (FormatTok->is(tok::l_paren))
1247 parseParens();
1248 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +00001249 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +00001250 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001251 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001252 if (!tryToParseBracedList()) {
1253 // A block outside of parentheses must be the last part of a
1254 // structural element.
1255 // FIXME: Figure out cases where this is not true, and add projections
1256 // for them (the one we know is missing are lambdas).
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001257 if (Style.BraceWrapping.AfterFunction)
Manuel Klimekab419912013-05-23 09:41:43 +00001258 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +00001259 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +00001260 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001261 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +00001262 return;
1263 }
1264 // Otherwise this was a braced init list, and the structural
1265 // element continues.
1266 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001267 case tok::kw_try:
1268 // We arrive here when parsing function-try blocks.
1269 parseTryCatch();
1270 return;
Daniel Jasper40e19212013-05-29 13:16:10 +00001271 case tok::identifier: {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001272 if (FormatTok->is(TT_MacroBlockEnd)) {
1273 addUnwrappedLine();
1274 return;
1275 }
1276
Martin Probst973ff792017-04-27 13:07:24 +00001277 // Function declarations (as opposed to function expressions) are parsed
1278 // on their own unwrapped line by continuing this loop. Function
1279 // expressions (functions that are not on their own line) must not create
1280 // a new unwrapped line, so they are special cased below.
1281 size_t TokenCount = Line->Tokens.size();
Daniel Jasper9326f912015-05-05 08:40:32 +00001282 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst973ff792017-04-27 13:07:24 +00001283 FormatTok->is(Keywords.kw_function) &&
1284 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1285 Keywords.kw_async)))) {
Daniel Jasper069e5f42014-05-20 11:14:57 +00001286 tryToParseJSFunction();
1287 break;
1288 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001289 if ((Style.Language == FormatStyle::LK_JavaScript ||
1290 Style.Language == FormatStyle::LK_Java) &&
1291 FormatTok->is(Keywords.kw_interface)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001292 if (Style.Language == FormatStyle::LK_JavaScript) {
1293 // In JavaScript/TypeScript, "interface" can be used as a standalone
1294 // identifier, e.g. in `var interface = 1;`. If "interface" is
1295 // followed by another identifier, it is very like to be an actual
1296 // interface declaration.
1297 unsigned StoredPosition = Tokens->getPosition();
1298 FormatToken *Next = Tokens->getNextToken();
1299 FormatTok = Tokens->setPosition(StoredPosition);
Martin Probst533965c2016-04-19 18:19:06 +00001300 if (Next && !mustBeJSIdent(Keywords, Next)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001301 nextToken();
1302 break;
1303 }
1304 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001305 parseRecord();
Daniel Jasper259188b2015-06-12 04:56:34 +00001306 addUnwrappedLine();
Daniel Jasper5c235c02015-07-06 14:26:04 +00001307 return;
Daniel Jasper9326f912015-05-05 08:40:32 +00001308 }
1309
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001310 // See if the following token should start a new unwrapped line.
Daniel Jasper9326f912015-05-05 08:40:32 +00001311 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +00001312 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +00001313 if (Line->Tokens.size() == 1 &&
1314 // JS doesn't have macros, and within classes colons indicate fields,
1315 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +00001316 Style.Language != FormatStyle::LK_JavaScript) {
1317 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Daniel Jasper40609472016-04-06 15:02:46 +00001318 Line->Tokens.begin()->Tok->MustBreakBefore = true;
Alexander Kornienkode644272013-04-08 22:16:06 +00001319 parseLabel();
1320 return;
1321 }
Daniel Jasper680b09b2014-11-05 10:48:04 +00001322 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +00001323 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +00001324 bool FunctionLike = FormatTok->is(tok::l_paren);
1325 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +00001326 parseParens();
Daniel Jaspere60cba12015-05-13 11:35:53 +00001327
1328 bool FollowedByNewline =
1329 CommentsBeforeNextToken.empty()
1330 ? FormatTok->NewlinesBefore > 0
1331 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1332
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001333 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
Daniel Jasper680b09b2014-11-05 10:48:04 +00001334 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +00001335 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +00001336 return;
Alexander Kornienkode644272013-04-08 22:16:06 +00001337 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001338 }
1339 break;
Daniel Jasper40e19212013-05-29 13:16:10 +00001340 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001341 case tok::equal:
Manuel Klimek79e06082015-05-21 12:23:34 +00001342 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1343 // TT_JsFatArrow. The always start an expression or a child block if
1344 // followed by a curly.
1345 if (FormatTok->is(TT_JsFatArrow)) {
1346 nextToken();
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001347 if (FormatTok->is(tok::l_brace))
Manuel Klimek79e06082015-05-21 12:23:34 +00001348 parseChildBlock();
Manuel Klimek79e06082015-05-21 12:23:34 +00001349 break;
1350 }
1351
Daniel Jaspere25509f2012-12-17 11:29:41 +00001352 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001353 if (FormatTok->Tok.is(tok::l_brace)) {
1354 nextToken();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001355 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001356 } else if (Style.Language == FormatStyle::LK_Proto &&
Manuel Klimek89628f62017-09-20 09:51:03 +00001357 FormatTok->Tok.is(tok::less)) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001358 nextToken();
Krasimir Georgiev0b41fcb2017-06-27 13:58:41 +00001359 parseBracedList(/*ContinueOnSemicolons=*/false,
1360 /*ClosingBraceKind=*/tok::greater);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001361 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001362 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001363 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001364 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001365 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +00001366 case tok::kw_new:
1367 parseNew();
1368 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001369 default:
1370 nextToken();
1371 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001372 }
1373 } while (!eof());
1374}
1375
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001376bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001377 if (!Style.isCpp()) {
Daniel Jasper1feab0f2015-06-02 15:31:37 +00001378 nextToken();
1379 return false;
1380 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001381 assert(FormatTok->is(tok::l_square));
1382 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001383 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001384 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001385
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001386 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001387 if (FormatTok->isSimpleTypeSpecifier()) {
1388 nextToken();
1389 continue;
1390 }
Manuel Klimekffdeb592013-09-03 15:10:01 +00001391 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001392 case tok::l_brace:
1393 break;
1394 case tok::l_paren:
1395 parseParens();
1396 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +00001397 case tok::amp:
1398 case tok::star:
1399 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +00001400 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001401 case tok::less:
1402 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001403 case tok::identifier:
Daniel Jasper5eaa0092015-08-13 13:37:08 +00001404 case tok::numeric_constant:
Daniel Jasper1067ab02014-02-11 10:16:55 +00001405 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001406 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +00001407 nextToken();
1408 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001409 case tok::arrow:
Daniel Jasper6f2b88a2015-06-05 13:18:09 +00001410 FormatTok->Type = TT_LambdaArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001411 nextToken();
1412 break;
1413 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001414 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001415 }
1416 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001417 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +00001418 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001419 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001420}
1421
1422bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
Manuel Klimek89628f62017-09-20 09:51:03 +00001423 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001424 if (Previous &&
1425 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
Manuel Klimekd0f3fe52018-04-11 14:51:54 +00001426 tok::kw_delete, tok::l_square) ||
Manuel Klimek89628f62017-09-20 09:51:03 +00001427 FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() ||
1428 Previous->isSimpleTypeSpecifier())) {
Manuel Klimekffdeb592013-09-03 15:10:01 +00001429 nextToken();
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001430 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001431 }
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001432 nextToken();
Manuel Klimekd0f3fe52018-04-11 14:51:54 +00001433 if (FormatTok->is(tok::l_square)) {
1434 return false;
1435 }
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001436 parseSquare(/*LambdaIntroducer=*/true);
1437 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001438}
1439
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001440void UnwrappedLineParser::tryToParseJSFunction() {
Martin Probst409697e2016-05-29 14:41:07 +00001441 assert(FormatTok->is(Keywords.kw_function) ||
1442 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
Martin Probst5f8445b2016-04-24 22:05:09 +00001443 if (FormatTok->is(Keywords.kw_async))
1444 nextToken();
1445 // Consume "function".
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001446 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001447
Daniel Jasper71e50af2016-11-01 06:22:59 +00001448 // Consume * (generator function). Treat it like C++'s overloaded operators.
1449 if (FormatTok->is(tok::star)) {
1450 FormatTok->Type = TT_OverloadedOperator;
Martin Probst5f8445b2016-04-24 22:05:09 +00001451 nextToken();
Daniel Jasper71e50af2016-11-01 06:22:59 +00001452 }
Martin Probst5f8445b2016-04-24 22:05:09 +00001453
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001454 // Consume function name.
1455 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001456 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001457
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001458 if (FormatTok->isNot(tok::l_paren))
1459 return;
Manuel Klimek79e06082015-05-21 12:23:34 +00001460
1461 // Parse formal parameter list.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001462 parseParens();
Manuel Klimek79e06082015-05-21 12:23:34 +00001463
1464 if (FormatTok->is(tok::colon)) {
1465 // Parse a type definition.
1466 nextToken();
1467
1468 // Eat the type declaration. For braced inline object types, balance braces,
1469 // otherwise just parse until finding an l_brace for the function body.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001470 if (FormatTok->is(tok::l_brace))
1471 tryToParseBracedList();
1472 else
Martin Probstaf16c502017-01-04 13:36:43 +00001473 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
Manuel Klimek79e06082015-05-21 12:23:34 +00001474 nextToken();
Manuel Klimek79e06082015-05-21 12:23:34 +00001475 }
1476
Martin Probstaf16c502017-01-04 13:36:43 +00001477 if (FormatTok->is(tok::semi))
1478 return;
1479
Manuel Klimek79e06082015-05-21 12:23:34 +00001480 parseChildBlock();
1481}
1482
Daniel Jasper3c883d12015-05-18 14:49:19 +00001483bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001484 if (FormatTok->BlockKind == BK_Unknown)
Daniel Jasper3c883d12015-05-18 14:49:19 +00001485 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001486 assert(FormatTok->BlockKind != BK_Unknown);
1487 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001488 return false;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001489 nextToken();
Manuel Klimekab419912013-05-23 09:41:43 +00001490 parseBracedList();
1491 return true;
1492}
1493
Krasimir Georgievff747be2017-06-27 13:43:07 +00001494bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1495 tok::TokenKind ClosingBraceKind) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001496 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001497
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001498 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1499 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001500 do {
Manuel Klimek79e06082015-05-21 12:23:34 +00001501 if (Style.Language == FormatStyle::LK_JavaScript) {
Martin Probst409697e2016-05-29 14:41:07 +00001502 if (FormatTok->is(Keywords.kw_function) ||
1503 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001504 tryToParseJSFunction();
1505 continue;
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001506 }
1507 if (FormatTok->is(TT_JsFatArrow)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001508 nextToken();
1509 // Fat arrows can be followed by simple expressions or by child blocks
1510 // in curly braces.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001511 if (FormatTok->is(tok::l_brace)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001512 parseChildBlock();
1513 continue;
1514 }
1515 }
Martin Probst8e3eba02017-02-07 16:33:13 +00001516 if (FormatTok->is(tok::l_brace)) {
1517 // Could be a method inside of a braced list `{a() { return 1; }}`.
1518 if (tryToParseBracedList())
1519 continue;
1520 parseChildBlock();
1521 }
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001522 }
Krasimir Georgievff747be2017-06-27 13:43:07 +00001523 if (FormatTok->Tok.getKind() == ClosingBraceKind) {
1524 nextToken();
1525 return !HasError;
1526 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001527 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001528 case tok::caret:
1529 nextToken();
1530 if (FormatTok->is(tok::l_brace)) {
1531 parseChildBlock();
1532 }
1533 break;
1534 case tok::l_square:
1535 tryToParseLambda();
1536 break;
Daniel Jaspera87af7a2015-06-30 11:32:22 +00001537 case tok::l_paren:
1538 parseParens();
Daniel Jasperf46dec82015-03-31 14:34:15 +00001539 // JavaScript can just have free standing methods and getters/setters in
1540 // object literals. Detect them by a "{" following ")".
1541 if (Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperf46dec82015-03-31 14:34:15 +00001542 if (FormatTok->is(tok::l_brace))
1543 parseChildBlock();
1544 break;
1545 }
Daniel Jasperf46dec82015-03-31 14:34:15 +00001546 break;
Martin Probst8e3eba02017-02-07 16:33:13 +00001547 case tok::l_brace:
1548 // Assume there are no blocks inside a braced init list apart
1549 // from the ones we explicitly parse out (like lambdas).
1550 FormatTok->BlockKind = BK_BracedInit;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001551 nextToken();
Martin Probst8e3eba02017-02-07 16:33:13 +00001552 parseBracedList();
1553 break;
Krasimir Georgievfa4dbb62017-08-03 13:43:45 +00001554 case tok::less:
1555 if (Style.Language == FormatStyle::LK_Proto) {
1556 nextToken();
1557 parseBracedList(/*ContinueOnSemicolons=*/false,
1558 /*ClosingBraceKind=*/tok::greater);
1559 } else {
1560 nextToken();
1561 }
1562 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001563 case tok::semi:
Daniel Jasperb9a49902016-01-09 15:56:28 +00001564 // JavaScript (or more precisely TypeScript) can have semicolons in braced
1565 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1566 // used for error recovery if we have otherwise determined that this is
1567 // a braced list.
1568 if (Style.Language == FormatStyle::LK_JavaScript) {
1569 nextToken();
1570 break;
1571 }
Daniel Jasper015ed022013-09-13 09:20:45 +00001572 HasError = true;
1573 if (!ContinueOnSemicolons)
1574 return !HasError;
1575 nextToken();
1576 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001577 case tok::comma:
1578 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001579 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001580 default:
1581 nextToken();
1582 break;
1583 }
1584 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001585 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001586}
1587
Daniel Jasperf7935112012-12-03 18:12:45 +00001588void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001589 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001590 nextToken();
1591 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001592 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001593 case tok::l_paren:
1594 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001595 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1596 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001597 break;
1598 case tok::r_paren:
1599 nextToken();
1600 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001601 case tok::r_brace:
1602 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1603 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001604 case tok::l_square:
1605 tryToParseLambda();
1606 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001607 case tok::l_brace:
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001608 if (!tryToParseBracedList())
Manuel Klimekf017dc02013-09-04 13:34:14 +00001609 parseChildBlock();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001610 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001611 case tok::at:
1612 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001613 if (FormatTok->Tok.is(tok::l_brace)) {
1614 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001615 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001616 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001617 break;
Martin Probst1027fb82017-02-07 14:05:30 +00001618 case tok::kw_class:
1619 if (Style.Language == FormatStyle::LK_JavaScript)
1620 parseRecord(/*ParseAsExpr=*/true);
1621 else
1622 nextToken();
1623 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001624 case tok::identifier:
1625 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst409697e2016-05-29 14:41:07 +00001626 (FormatTok->is(Keywords.kw_function) ||
1627 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001628 tryToParseJSFunction();
1629 else
1630 nextToken();
1631 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001632 default:
1633 nextToken();
1634 break;
1635 }
1636 } while (!eof());
1637}
1638
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001639void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
1640 if (!LambdaIntroducer) {
1641 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1642 if (tryToParseLambda())
1643 return;
1644 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001645 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001646 switch (FormatTok->Tok.getKind()) {
1647 case tok::l_paren:
1648 parseParens();
1649 break;
1650 case tok::r_square:
1651 nextToken();
1652 return;
1653 case tok::r_brace:
1654 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1655 return;
1656 case tok::l_square:
1657 parseSquare();
1658 break;
1659 case tok::l_brace: {
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001660 if (!tryToParseBracedList())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001661 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001662 break;
1663 }
1664 case tok::at:
1665 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001666 if (FormatTok->Tok.is(tok::l_brace)) {
1667 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001668 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001669 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001670 break;
1671 default:
1672 nextToken();
1673 break;
1674 }
1675 } while (!eof());
1676}
1677
Daniel Jasperf7935112012-12-03 18:12:45 +00001678void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001679 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001680 nextToken();
Daniel Jasper6a7d5a72017-06-19 07:40:49 +00001681 if (FormatTok->Tok.is(tok::kw_constexpr))
1682 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001683 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001684 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001685 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001686 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001687 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001688 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001689 if (Style.BraceWrapping.BeforeElse)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001690 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001691 else
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001692 NeedsUnwrappedLine = true;
Daniel Jasperf7935112012-12-03 18:12:45 +00001693 } else {
1694 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001695 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001696 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001697 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001698 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001699 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001700 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001701 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001702 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001703 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001704 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001705 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001706 parseIfThenElse();
1707 } else {
1708 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001709 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001710 parseStructuralElement();
Daniel Jasper451544a2016-05-19 06:30:48 +00001711 if (FormatTok->is(tok::eof))
1712 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001713 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001714 }
1715 } else if (NeedsUnwrappedLine) {
1716 addUnwrappedLine();
1717 }
1718}
1719
Daniel Jasper04a71a42014-05-08 11:58:24 +00001720void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001721 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001722 nextToken();
1723 bool NeedsUnwrappedLine = false;
1724 if (FormatTok->is(tok::colon)) {
1725 // We are in a function try block, what comes is an initializer list.
1726 nextToken();
1727 while (FormatTok->is(tok::identifier)) {
1728 nextToken();
1729 if (FormatTok->is(tok::l_paren))
1730 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001731 if (FormatTok->is(tok::comma))
1732 nextToken();
1733 }
1734 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001735 // Parse try with resource.
1736 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1737 parseParens();
1738 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001739 if (FormatTok->is(tok::l_brace)) {
1740 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1741 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001742 if (Style.BraceWrapping.BeforeCatch) {
Daniel Jasper04a71a42014-05-08 11:58:24 +00001743 addUnwrappedLine();
1744 } else {
1745 NeedsUnwrappedLine = true;
1746 }
1747 } else if (!FormatTok->is(tok::kw_catch)) {
1748 // The C++ standard requires a compound-statement after a try.
1749 // If there's none, we try to assume there's a structuralElement
1750 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001751 addUnwrappedLine();
1752 ++Line->Level;
1753 parseStructuralElement();
1754 --Line->Level;
1755 }
Nico Weber33381f52015-02-07 01:57:32 +00001756 while (1) {
1757 if (FormatTok->is(tok::at))
1758 nextToken();
1759 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1760 tok::kw___finally) ||
1761 ((Style.Language == FormatStyle::LK_Java ||
1762 Style.Language == FormatStyle::LK_JavaScript) &&
1763 FormatTok->is(Keywords.kw_finally)) ||
1764 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1765 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1766 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001767 nextToken();
1768 while (FormatTok->isNot(tok::l_brace)) {
1769 if (FormatTok->is(tok::l_paren)) {
1770 parseParens();
1771 continue;
1772 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001773 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001774 return;
1775 nextToken();
1776 }
1777 NeedsUnwrappedLine = false;
1778 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1779 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001780 if (Style.BraceWrapping.BeforeCatch)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001781 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001782 else
Daniel Jasper04a71a42014-05-08 11:58:24 +00001783 NeedsUnwrappedLine = true;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001784 }
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001785 if (NeedsUnwrappedLine)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001786 addUnwrappedLine();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001787}
1788
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001789void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001790 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001791
1792 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001793 nextToken();
Saleem Abdulrasool328085f2015-10-30 05:07:56 +00001794 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001795 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001796 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001797 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001798 addUnwrappedLine();
1799
Daniel Jasper65ee3472013-07-31 23:16:02 +00001800 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1801 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1802 DeclarationScopeStack.size() > 1);
1803 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001804 // Munch the semicolon after a namespace. This is more common than one would
1805 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001806 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001807 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001808 addUnwrappedLine();
1809 }
1810 // FIXME: Add error handling.
1811}
1812
Daniel Jasper6acf5132015-03-12 14:44:29 +00001813void UnwrappedLineParser::parseNew() {
1814 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1815 nextToken();
1816 if (Style.Language != FormatStyle::LK_Java)
1817 return;
1818
1819 // In Java, we can parse everything up to the parens, which aren't optional.
1820 do {
1821 // There should not be a ;, { or } before the new's open paren.
1822 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1823 return;
1824
1825 // Consume the parens.
1826 if (FormatTok->is(tok::l_paren)) {
1827 parseParens();
1828
1829 // If there is a class body of an anonymous class, consume that as child.
1830 if (FormatTok->is(tok::l_brace))
1831 parseChildBlock();
1832 return;
1833 }
1834 nextToken();
1835 } while (!eof());
1836}
1837
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001838void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001839 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001840 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001841 nextToken();
Martin Probsta050f412017-05-18 21:19:29 +00001842 // JS' for await ( ...
Martin Probstbd49e322017-05-15 19:33:20 +00001843 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probsta050f412017-05-18 21:19:29 +00001844 FormatTok->is(Keywords.kw_await))
Martin Probstbd49e322017-05-15 19:33:20 +00001845 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001846 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001847 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001848 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001849 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001850 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001851 addUnwrappedLine();
1852 } else {
1853 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001854 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001855 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001856 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001857 }
1858}
1859
Daniel Jasperf7935112012-12-03 18:12:45 +00001860void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001861 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001862 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001863 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001864 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001865 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001866 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001867 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001868 } else {
1869 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001870 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001871 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001872 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001873 }
1874
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001875 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001876 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001877 addUnwrappedLine();
1878 return;
1879 }
1880
Daniel Jasperf7935112012-12-03 18:12:45 +00001881 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001882 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001883}
1884
1885void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001886 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001887 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001888 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001889 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001890 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001891 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001892 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001893 if (FormatTok->Tok.is(tok::kw_break)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001894 if (Style.BraceWrapping.AfterControlStatement)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001895 addUnwrappedLine();
1896 parseStructuralElement();
1897 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001898 addUnwrappedLine();
1899 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001900 if (FormatTok->is(tok::semi))
1901 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001902 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001903 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001904 Line->Level = OldLineLevel;
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001905 if (FormatTok->isNot(tok::l_brace)) {
Daniel Jasper40609472016-04-06 15:02:46 +00001906 parseStructuralElement();
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001907 addUnwrappedLine();
1908 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001909}
1910
1911void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001912 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001913 // FIXME: fix handling of complex expressions here.
1914 do {
1915 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001916 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001917 parseLabel();
1918}
1919
1920void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001921 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001922 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001923 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001924 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001925 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001926 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001927 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001928 addUnwrappedLine();
1929 } else {
1930 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001931 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001932 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001933 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001934 }
1935}
1936
1937void UnwrappedLineParser::parseAccessSpecifier() {
1938 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001939 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001940 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001941 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001942 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001943 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001944 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001945 addUnwrappedLine();
1946}
1947
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001948bool UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001949 // Won't be 'enum' for NS_ENUMs.
1950 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001951 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001952
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001953 // In TypeScript, "enum" can also be used as property name, e.g. in interface
1954 // declarations. An "enum" keyword followed by a colon would be a syntax
1955 // error and thus assume it is just an identifier.
Daniel Jasper87379302016-02-03 05:33:44 +00001956 if (Style.Language == FormatStyle::LK_JavaScript &&
1957 FormatTok->isOneOf(tok::colon, tok::question))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001958 return false;
1959
Daniel Jasper2b41a822013-08-20 12:42:50 +00001960 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001961 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1962 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001963
Daniel Jasper786a5502013-09-06 21:32:35 +00001964 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001965 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1966 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001967 nextToken();
1968 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001969 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001970 parseParens();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001971 if (FormatTok->is(tok::identifier)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001972 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001973 // If there are two identifiers in a row, this is likely an elaborate
1974 // return type. In Java, this can be "implements", etc.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001975 if (Style.isCpp() && FormatTok->is(tok::identifier))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001976 return false;
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001977 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001978 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001979
1980 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001981 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001982 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00001983 FormatTok->BlockKind = BK_Block;
1984
1985 if (Style.Language == FormatStyle::LK_Java) {
1986 // Java enums are different.
1987 parseJavaEnumBody();
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001988 return true;
1989 }
1990 if (Style.Language == FormatStyle::LK_Proto) {
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001991 parseBlock(/*MustBeDeclaration=*/true);
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001992 return true;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001993 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001994
1995 // Parse enum body.
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001996 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001997 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1998 if (HasError) {
1999 if (FormatTok->is(tok::semi))
2000 nextToken();
2001 addUnwrappedLine();
2002 }
Daniel Jasper6f5a1932015-12-29 08:54:23 +00002003 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00002004
Daniel Jasper90cf3802015-06-17 09:44:02 +00002005 // There is no addUnwrappedLine() here so that we fall through to parsing a
2006 // structural element afterwards. Thus, in "enum A {} n, m;",
Manuel Klimek2cec0192013-01-21 19:17:52 +00002007 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00002008}
2009
2010void UnwrappedLineParser::parseJavaEnumBody() {
2011 // Determine whether the enum is simple, i.e. does not have a semicolon or
2012 // constants with class bodies. Simple enums can be formatted like braced
2013 // lists, contracted to a single line, etc.
2014 unsigned StoredPosition = Tokens->getPosition();
2015 bool IsSimple = true;
2016 FormatToken *Tok = Tokens->getNextToken();
2017 while (Tok) {
2018 if (Tok->is(tok::r_brace))
2019 break;
2020 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
2021 IsSimple = false;
2022 break;
2023 }
2024 // FIXME: This will also mark enums with braces in the arguments to enum
2025 // constants as "not simple". This is probably fine in practice, though.
2026 Tok = Tokens->getNextToken();
2027 }
2028 FormatTok = Tokens->setPosition(StoredPosition);
2029
2030 if (IsSimple) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002031 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002032 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00002033 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00002034 return;
2035 }
2036
2037 // Parse the body of a more complex enum.
2038 // First add a line for everything up to the "{".
2039 nextToken();
2040 addUnwrappedLine();
2041 ++Line->Level;
2042
2043 // Parse the enum constants.
2044 while (FormatTok) {
2045 if (FormatTok->is(tok::l_brace)) {
2046 // Parse the constant's class body.
2047 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2048 /*MunchSemi=*/false);
2049 } else if (FormatTok->is(tok::l_paren)) {
2050 parseParens();
2051 } else if (FormatTok->is(tok::comma)) {
2052 nextToken();
2053 addUnwrappedLine();
2054 } else if (FormatTok->is(tok::semi)) {
2055 nextToken();
2056 addUnwrappedLine();
2057 break;
2058 } else if (FormatTok->is(tok::r_brace)) {
2059 addUnwrappedLine();
2060 break;
2061 } else {
2062 nextToken();
2063 }
2064 }
2065
2066 // Parse the class body after the enum's ";" if any.
2067 parseLevel(/*HasOpeningBrace=*/true);
2068 nextToken();
2069 --Line->Level;
2070 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00002071}
2072
Martin Probst1027fb82017-02-07 14:05:30 +00002073void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00002074 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00002075 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00002076
Daniel Jasper04785d02015-05-06 14:03:02 +00002077 // The actual identifier can be a nested name specifier, and in macros
2078 // it is often token-pasted.
2079 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2080 tok::kw___attribute, tok::kw___declspec,
2081 tok::kw_alignas) ||
2082 ((Style.Language == FormatStyle::LK_Java ||
2083 Style.Language == FormatStyle::LK_JavaScript) &&
2084 FormatTok->isOneOf(tok::period, tok::comma))) {
Martin Probstcb870c52017-08-01 15:46:10 +00002085 if (Style.Language == FormatStyle::LK_JavaScript &&
2086 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
2087 // JavaScript/TypeScript supports inline object types in
2088 // extends/implements positions:
2089 // class Foo implements {bar: number} { }
2090 nextToken();
2091 if (FormatTok->is(tok::l_brace)) {
2092 tryToParseBracedList();
2093 continue;
2094 }
2095 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002096 bool IsNonMacroIdentifier =
2097 FormatTok->is(tok::identifier) &&
2098 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002099 nextToken();
2100 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00002101 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00002102 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00002103 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00002104
Daniel Jasper04785d02015-05-06 14:03:02 +00002105 // Note that parsing away template declarations here leads to incorrectly
2106 // accepting function declarations as record declarations.
2107 // In general, we cannot solve this problem. Consider:
2108 // class A<int> B() {}
2109 // which can be a function definition or a class definition when B() is a
2110 // macro. If we find enough real-world cases where this is a problem, we
2111 // can parse for the 'template' keyword in the beginning of the statement,
2112 // and thus rule out the record production in case there is no template
2113 // (this would still leave us with an ambiguity between template function
2114 // and class declarations).
Daniel Jasperadba2aa2015-05-18 12:52:00 +00002115 if (FormatTok->isOneOf(tok::colon, tok::less)) {
2116 while (!eof()) {
Daniel Jasper3c883d12015-05-18 14:49:19 +00002117 if (FormatTok->is(tok::l_brace)) {
2118 calculateBraceTypes(/*ExpectClassBody=*/true);
2119 if (!tryToParseBracedList())
2120 break;
2121 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002122 if (FormatTok->Tok.is(tok::semi))
2123 return;
2124 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002125 }
2126 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002127 if (FormatTok->Tok.is(tok::l_brace)) {
Martin Probst1027fb82017-02-07 14:05:30 +00002128 if (ParseAsExpr) {
2129 parseChildBlock();
2130 } else {
2131 if (ShouldBreakBeforeBrace(Style, InitialToken))
2132 addUnwrappedLine();
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002133
Martin Probst1027fb82017-02-07 14:05:30 +00002134 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2135 /*MunchSemi=*/false);
2136 }
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002137 }
Daniel Jasper90cf3802015-06-17 09:44:02 +00002138 // There is no addUnwrappedLine() here so that we fall through to parsing a
2139 // structural element afterwards. Thus, in "class A {} n, m;",
2140 // "} n, m;" will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00002141}
2142
Ben Hamilton707e68f2018-05-30 15:21:38 +00002143void UnwrappedLineParser::parseObjCMethod() {
2144 assert(FormatTok->Tok.isOneOf(tok::l_paren, tok::identifier) &&
2145 "'(' or identifier expected.");
2146 do {
2147 if (FormatTok->Tok.is(tok::semi)) {
2148 nextToken();
2149 addUnwrappedLine();
2150 return;
2151 } else if (FormatTok->Tok.is(tok::l_brace)) {
2152 parseBlock(/*MustBeDeclaration=*/false);
2153 addUnwrappedLine();
2154 return;
2155 } else {
2156 nextToken();
2157 }
2158 } while (!eof());
2159}
2160
Nico Weber8696a8d2013-01-09 21:15:03 +00002161void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002162 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Ben Hamilton1462e842018-04-05 15:26:25 +00002163 do {
Nico Weber8696a8d2013-01-09 21:15:03 +00002164 nextToken();
Ben Hamilton1462e842018-04-05 15:26:25 +00002165 // Early exit in case someone forgot a close angle.
2166 if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2167 FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2168 return;
2169 } while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00002170 nextToken(); // Skip '>'.
2171}
2172
2173void UnwrappedLineParser::parseObjCUntilAtEnd() {
2174 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002175 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002176 nextToken();
2177 addUnwrappedLine();
2178 break;
2179 }
Daniel Jaspera15da302013-08-28 08:04:23 +00002180 if (FormatTok->is(tok::l_brace)) {
2181 parseBlock(/*MustBeDeclaration=*/false);
2182 // In ObjC interfaces, nothing should be following the "}".
2183 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00002184 } else if (FormatTok->is(tok::r_brace)) {
2185 // Ignore stray "}". parseStructuralElement doesn't consume them.
2186 nextToken();
2187 addUnwrappedLine();
Ben Hamilton707e68f2018-05-30 15:21:38 +00002188 } else if (FormatTok->isOneOf(tok::minus, tok::plus)) {
2189 nextToken();
2190 parseObjCMethod();
Daniel Jaspera15da302013-08-28 08:04:23 +00002191 } else {
2192 parseStructuralElement();
2193 }
Nico Weber8696a8d2013-01-09 21:15:03 +00002194 } while (!eof());
2195}
2196
Nico Weber2ce0ac52013-01-09 23:25:37 +00002197void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weberc068ff72018-01-23 17:10:25 +00002198 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_interface ||
2199 FormatTok->Tok.getObjCKeywordID() == tok::objc_implementation);
Nico Weber7eecf4b2013-01-09 20:25:35 +00002200 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002201 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00002202
Ben Hamilton1462e842018-04-05 15:26:25 +00002203 // @interface can be followed by a lightweight generic
2204 // specialization list, then either a base class or a category.
2205 if (FormatTok->Tok.is(tok::less)) {
2206 // Unlike protocol lists, generic parameterizations support
2207 // nested angles:
2208 //
2209 // @interface Foo<ValueType : id <NSCopying, NSSecureCoding>> :
2210 // NSObject <NSCopying, NSSecureCoding>
2211 //
2212 // so we need to count how many open angles we have left.
2213 unsigned NumOpenAngles = 1;
2214 do {
2215 nextToken();
2216 // Early exit in case someone forgot a close angle.
2217 if (FormatTok->isOneOf(tok::semi, tok::l_brace) ||
2218 FormatTok->Tok.isObjCAtKeyword(tok::objc_end))
2219 break;
2220 if (FormatTok->Tok.is(tok::less))
2221 ++NumOpenAngles;
2222 else if (FormatTok->Tok.is(tok::greater)) {
2223 assert(NumOpenAngles > 0 && "'>' makes NumOpenAngles negative");
2224 --NumOpenAngles;
2225 }
2226 } while (!eof() && NumOpenAngles != 0);
2227 nextToken(); // Skip '>'.
2228 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002229 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00002230 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002231 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002232 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00002233 // Skip category, if present.
2234 parseParens();
2235
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002236 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002237 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00002238
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002239 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00002240 if (Style.BraceWrapping.AfterObjCDeclaration)
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002241 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00002242 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002243 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00002244
2245 // With instance variables, this puts '}' on its own line. Without instance
2246 // variables, this ends the @interface line.
2247 addUnwrappedLine();
2248
Nico Weber8696a8d2013-01-09 21:15:03 +00002249 parseObjCUntilAtEnd();
2250}
Nico Weber7eecf4b2013-01-09 20:25:35 +00002251
Nico Weberc068ff72018-01-23 17:10:25 +00002252// Returns true for the declaration/definition form of @protocol,
2253// false for the expression form.
2254bool UnwrappedLineParser::parseObjCProtocol() {
2255 assert(FormatTok->Tok.getObjCKeywordID() == tok::objc_protocol);
Nico Weber8696a8d2013-01-09 21:15:03 +00002256 nextToken();
Nico Weberc068ff72018-01-23 17:10:25 +00002257
2258 if (FormatTok->is(tok::l_paren))
2259 // The expression form of @protocol, e.g. "Protocol* p = @protocol(foo);".
2260 return false;
2261
2262 // The definition/declaration form,
2263 // @protocol Foo
2264 // - (int)someMethod;
2265 // @end
2266
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002267 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00002268
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002269 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002270 parseObjCProtocolList();
2271
2272 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002273 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002274 nextToken();
Nico Weberc068ff72018-01-23 17:10:25 +00002275 addUnwrappedLine();
2276 return true;
Nico Weber8696a8d2013-01-09 21:15:03 +00002277 }
2278
2279 addUnwrappedLine();
2280 parseObjCUntilAtEnd();
Nico Weberc068ff72018-01-23 17:10:25 +00002281 return true;
Nico Weber7eecf4b2013-01-09 20:25:35 +00002282}
2283
Daniel Jasperfca735c2015-02-19 16:14:18 +00002284void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
Martin Probst053f1aa2016-04-19 14:55:37 +00002285 bool IsImport = FormatTok->is(Keywords.kw_import);
2286 assert(IsImport || FormatTok->is(tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00002287 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00002288
Daniel Jasperec05fc72015-05-11 09:14:50 +00002289 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002290 if (FormatTok->is(tok::kw_default))
2291 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00002292
Martin Probst5f8445b2016-04-24 22:05:09 +00002293 // Consume "async function", "function" and "default function", so that these
2294 // get parsed as free-standing JS functions, i.e. do not require a trailing
2295 // semicolon.
2296 if (FormatTok->is(Keywords.kw_async))
2297 nextToken();
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002298 if (FormatTok->is(Keywords.kw_function)) {
2299 nextToken();
2300 return;
2301 }
2302
Martin Probst053f1aa2016-04-19 14:55:37 +00002303 // For imports, `export *`, `export {...}`, consume the rest of the line up
2304 // to the terminating `;`. For everything else, just return and continue
2305 // parsing the structural element, i.e. the declaration or expression for
2306 // `export default`.
2307 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
2308 !FormatTok->isStringLiteral())
2309 return;
Daniel Jasperfca735c2015-02-19 16:14:18 +00002310
Martin Probstd40bca42017-01-09 08:56:36 +00002311 while (!eof()) {
2312 if (FormatTok->is(tok::semi))
2313 return;
Krasimir Georgiev112c2e92017-11-09 13:22:03 +00002314 if (Line->Tokens.empty()) {
Martin Probstd40bca42017-01-09 08:56:36 +00002315 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
2316 // import statement should terminate.
2317 return;
2318 }
Daniel Jasperefc1a832016-01-07 08:53:35 +00002319 if (FormatTok->is(tok::l_brace)) {
2320 FormatTok->BlockKind = BK_Block;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002321 nextToken();
Daniel Jasperefc1a832016-01-07 08:53:35 +00002322 parseBracedList();
2323 } else {
2324 nextToken();
2325 }
Daniel Jasper354aa512015-02-19 16:07:32 +00002326 }
2327}
2328
Daniel Jasper3b203a62013-09-05 16:05:56 +00002329LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
2330 StringRef Prefix = "") {
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002331 llvm::dbgs() << Prefix << "Line(" << Line.Level
2332 << ", FSC=" << Line.FirstStartColumn << ")"
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002333 << (Line.InPPDirective ? " MACRO" : "") << ": ";
2334 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2335 E = Line.Tokens.end();
2336 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002337 llvm::dbgs() << I->Tok->Tok.getName() << "["
Manuel Klimek89628f62017-09-20 09:51:03 +00002338 << "T=" << I->Tok->Type << ", OC=" << I->Tok->OriginalColumn
2339 << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002340 }
2341 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2342 E = Line.Tokens.end();
2343 I != E; ++I) {
2344 const UnwrappedLineNode &Node = *I;
2345 for (SmallVectorImpl<UnwrappedLine>::const_iterator
2346 I = Node.Children.begin(),
2347 E = Node.Children.end();
2348 I != E; ++I) {
2349 printDebugInfo(*I, "\nChild: ");
2350 }
2351 }
2352 llvm::dbgs() << "\n";
2353}
2354
Daniel Jasperf7935112012-12-03 18:12:45 +00002355void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002356 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00002357 return;
Nicola Zaghen3538b392018-05-15 13:30:56 +00002358 LLVM_DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002359 if (CurrentLines == &Lines)
2360 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00002361 });
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002362 CurrentLines->push_back(std::move(*Line));
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002363 Line->Tokens.clear();
Krasimir Georgiev85c37042017-03-01 16:38:08 +00002364 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002365 Line->FirstStartColumn = 0;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002366 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002367 CurrentLines->append(
2368 std::make_move_iterator(PreprocessorDirectives.begin()),
2369 std::make_move_iterator(PreprocessorDirectives.end()));
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002370 PreprocessorDirectives.clear();
2371 }
Manuel Klimeke411aa82017-09-20 09:29:37 +00002372 // Disconnect the current token from the last token on the previous line.
2373 FormatTok->Previous = nullptr;
Daniel Jasperf7935112012-12-03 18:12:45 +00002374}
2375
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002376bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00002377
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002378bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002379 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
2380 FormatTok.NewlinesBefore > 0;
2381}
2382
Krasimir Georgiev91834222017-01-25 13:58:58 +00002383// Checks if \p FormatTok is a line comment that continues the line comment
2384// section on \p Line.
Krasimir Georgievea222a72017-05-22 10:07:56 +00002385static bool continuesLineCommentSection(const FormatToken &FormatTok,
2386 const UnwrappedLine &Line,
2387 llvm::Regex &CommentPragmasRegex) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002388 if (Line.Tokens.empty())
2389 return false;
Krasimir Georgiev84321612017-01-30 19:18:55 +00002390
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002391 StringRef IndentContent = FormatTok.TokenText;
2392 if (FormatTok.TokenText.startswith("//") ||
2393 FormatTok.TokenText.startswith("/*"))
2394 IndentContent = FormatTok.TokenText.substr(2);
2395 if (CommentPragmasRegex.match(IndentContent))
2396 return false;
2397
Krasimir Georgiev91834222017-01-25 13:58:58 +00002398 // If Line starts with a line comment, then FormatTok continues the comment
Krasimir Georgiev84321612017-01-30 19:18:55 +00002399 // section if its original column is greater or equal to the original start
Krasimir Georgiev91834222017-01-25 13:58:58 +00002400 // column of the line.
2401 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002402 // Define the min column token of a line as follows: if a line ends in '{' or
2403 // contains a '{' followed by a line comment, then the min column token is
2404 // that '{'. Otherwise, the min column token of the line is the first token of
2405 // the line.
2406 //
2407 // If Line starts with a token other than a line comment, then FormatTok
2408 // continues the comment section if its original column is greater than the
2409 // original start column of the min column token of the line.
Krasimir Georgiev91834222017-01-25 13:58:58 +00002410 //
2411 // For example, the second line comment continues the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002412 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002413 // // first line
2414 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002415 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002416 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002417 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002418 // // first line
2419 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002420 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002421 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002422 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002423 // int i; // first line
2424 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002425 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002426 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002427 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002428 // do { // first line
2429 // // second line
2430 // int i;
2431 // } while (true);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002432 //
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002433 // and:
2434 //
2435 // enum {
2436 // a, // first line
2437 // // second line
2438 // b
2439 // };
2440 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002441 // The second line comment doesn't continue the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002442 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002443 // // first line
2444 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002445 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002446 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002447 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002448 // int i; // first line
2449 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002450 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002451 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002452 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002453 // do { // first line
2454 // // second line
2455 // int i;
2456 // } while (true);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002457 //
2458 // and:
2459 //
2460 // enum {
2461 // a, // first line
2462 // // second line
2463 // };
Krasimir Georgiev84321612017-01-30 19:18:55 +00002464 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
2465
2466 // Scan for '{//'. If found, use the column of '{' as a min column for line
2467 // comment section continuation.
2468 const FormatToken *PreviousToken = nullptr;
Krasimir Georgievd86c25d2017-03-10 13:09:29 +00002469 for (const UnwrappedLineNode &Node : Line.Tokens) {
Krasimir Georgiev84321612017-01-30 19:18:55 +00002470 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
2471 isLineComment(*Node.Tok)) {
2472 MinColumnToken = PreviousToken;
2473 break;
2474 }
2475 PreviousToken = Node.Tok;
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002476
2477 // Grab the last newline preceding a token in this unwrapped line.
2478 if (Node.Tok->NewlinesBefore > 0) {
2479 MinColumnToken = Node.Tok;
2480 }
Krasimir Georgiev84321612017-01-30 19:18:55 +00002481 }
2482 if (PreviousToken && PreviousToken->is(tok::l_brace)) {
2483 MinColumnToken = PreviousToken;
2484 }
2485
Krasimir Georgievea222a72017-05-22 10:07:56 +00002486 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
2487 MinColumnToken);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002488}
2489
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002490void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
2491 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002492 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002493 I = CommentsBeforeNextToken.begin(),
2494 E = CommentsBeforeNextToken.end();
2495 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002496 // Line comments that belong to the same line comment section are put on the
2497 // same line since later we might want to reflow content between them.
Krasimir Georgiev753625b2017-01-31 13:32:38 +00002498 // Additional fine-grained breaking of line comment sections is controlled
2499 // by the class BreakableLineCommentSection in case it is desirable to keep
2500 // several line comment sections in the same unwrapped line.
2501 //
2502 // FIXME: Consider putting separate line comment sections as children to the
2503 // unwrapped line instead.
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002504 (*I)->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002505 continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002506 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002507 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002508 pushToken(*I);
2509 }
Daniel Jaspere60cba12015-05-13 11:35:53 +00002510 if (NewlineBeforeNext && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002511 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002512 CommentsBeforeNextToken.clear();
2513}
2514
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002515void UnwrappedLineParser::nextToken(int LevelDifference) {
Daniel Jasperf7935112012-12-03 18:12:45 +00002516 if (eof())
2517 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002518 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002519 pushToken(FormatTok);
Manuel Klimek89628f62017-09-20 09:51:03 +00002520 FormatToken *Previous = FormatTok;
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002521 if (Style.Language != FormatStyle::LK_JavaScript)
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002522 readToken(LevelDifference);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002523 else
2524 readTokenWithJavaScriptASI();
Manuel Klimeke411aa82017-09-20 09:29:37 +00002525 FormatTok->Previous = Previous;
Daniel Jasperb9a49902016-01-09 15:56:28 +00002526}
2527
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002528void UnwrappedLineParser::distributeComments(
2529 const SmallVectorImpl<FormatToken *> &Comments,
2530 const FormatToken *NextTok) {
2531 // Whether or not a line comment token continues a line is controlled by
Krasimir Georgievea222a72017-05-22 10:07:56 +00002532 // the method continuesLineCommentSection, with the following caveat:
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002533 //
2534 // Define a trail of Comments to be a nonempty proper postfix of Comments such
2535 // that each comment line from the trail is aligned with the next token, if
2536 // the next token exists. If a trail exists, the beginning of the maximal
2537 // trail is marked as a start of a new comment section.
2538 //
2539 // For example in this code:
2540 //
2541 // int a; // line about a
2542 // // line 1 about b
2543 // // line 2 about b
2544 // int b;
2545 //
2546 // the two lines about b form a maximal trail, so there are two sections, the
2547 // first one consisting of the single comment "// line about a" and the
2548 // second one consisting of the next two comments.
2549 if (Comments.empty())
2550 return;
2551 bool ShouldPushCommentsInCurrentLine = true;
2552 bool HasTrailAlignedWithNextToken = false;
2553 unsigned StartOfTrailAlignedWithNextToken = 0;
2554 if (NextTok) {
2555 // We are skipping the first element intentionally.
2556 for (unsigned i = Comments.size() - 1; i > 0; --i) {
2557 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
2558 HasTrailAlignedWithNextToken = true;
2559 StartOfTrailAlignedWithNextToken = i;
2560 }
2561 }
2562 }
2563 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
2564 FormatToken *FormatTok = Comments[i];
Manuel Klimek89628f62017-09-20 09:51:03 +00002565 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002566 FormatTok->ContinuesLineCommentSection = false;
2567 } else {
2568 FormatTok->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002569 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002570 }
2571 if (!FormatTok->ContinuesLineCommentSection &&
2572 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
2573 ShouldPushCommentsInCurrentLine = false;
2574 }
2575 if (ShouldPushCommentsInCurrentLine) {
2576 pushToken(FormatTok);
2577 } else {
2578 CommentsBeforeNextToken.push_back(FormatTok);
2579 }
2580 }
2581}
2582
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002583void UnwrappedLineParser::readToken(int LevelDifference) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002584 SmallVector<FormatToken *, 1> Comments;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002585 do {
2586 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00002587 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002588 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
2589 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002590 distributeComments(Comments, FormatTok);
2591 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002592 // If there is an unfinished unwrapped line, we flush the preprocessor
2593 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00002594 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002595 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002596 assert((LevelDifference >= 0 ||
2597 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2598 "LevelDifference makes Line->Level negative");
2599 Line->Level += LevelDifference;
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00002600 // Comments stored before the preprocessor directive need to be output
2601 // before the preprocessor directive, at the same level as the
2602 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002603 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002604 parsePPDirective();
2605 }
Manuel Klimek68b03042014-04-14 09:14:11 +00002606 while (FormatTok->Type == TT_ConflictStart ||
2607 FormatTok->Type == TT_ConflictEnd ||
2608 FormatTok->Type == TT_ConflictAlternative) {
2609 if (FormatTok->Type == TT_ConflictStart) {
2610 conditionalCompilationStart(/*Unreachable=*/false);
2611 } else if (FormatTok->Type == TT_ConflictAlternative) {
2612 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002613 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00002614 conditionalCompilationEnd();
2615 }
2616 FormatTok = Tokens->getNextToken();
2617 FormatTok->MustBreakBefore = true;
2618 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002619
Francois Ferranda98a95c2017-07-28 07:56:14 +00002620 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002621 !Line->InPPDirective) {
2622 continue;
2623 }
2624
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002625 if (!FormatTok->Tok.is(tok::comment)) {
2626 distributeComments(Comments, FormatTok);
2627 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002628 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002629 }
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002630
2631 Comments.push_back(FormatTok);
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002632 } while (!eof());
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002633
2634 distributeComments(Comments, nullptr);
2635 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002636}
2637
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002638void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002639 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002640 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002641 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002642 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00002643 }
Daniel Jasperf7935112012-12-03 18:12:45 +00002644}
2645
Daniel Jasper8d1832e2013-01-07 13:26:07 +00002646} // end namespace format
2647} // end namespace clang