blob: fdf98839019565cc1b223a1c5d549de283eb74ec [file] [log] [blame]
Daniel Jasperf7935112012-12-03 18:12:45 +00001//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief This file contains the implementation of the UnwrappedLineParser,
12/// which turns a stream of tokens into UnwrappedLines.
13///
Daniel Jasperf7935112012-12-03 18:12:45 +000014//===----------------------------------------------------------------------===//
15
Chandler Carruth4b417452013-01-19 08:09:44 +000016#include "UnwrappedLineParser.h"
Benjamin Kramer33335df2015-03-01 21:36:40 +000017#include "llvm/ADT/STLExtras.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000018#include "llvm/Support/Debug.h"
Benjamin Kramer53f5e892015-03-23 18:05:43 +000019#include "llvm/Support/raw_ostream.h"
Manuel Klimekab3dc002013-01-16 12:31:12 +000020
Chandler Carruth10346662014-04-22 03:17:02 +000021#define DEBUG_TYPE "format-parser"
22
Daniel Jasperf7935112012-12-03 18:12:45 +000023namespace clang {
24namespace format {
25
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000026class FormatTokenSource {
27public:
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000028 virtual ~FormatTokenSource() {}
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000029 virtual FormatToken *getNextToken() = 0;
30
31 virtual unsigned getPosition() = 0;
32 virtual FormatToken *setPosition(unsigned Position) = 0;
33};
34
Craig Topper69665e12013-07-01 04:21:54 +000035namespace {
36
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000037class ScopedDeclarationState {
38public:
39 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
40 bool MustBeDeclaration)
41 : Line(Line), Stack(Stack) {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000042 Line.MustBeDeclaration = MustBeDeclaration;
Manuel Klimek39080572013-01-23 11:03:04 +000043 Stack.push_back(MustBeDeclaration);
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000044 }
45 ~ScopedDeclarationState() {
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000046 Stack.pop_back();
Manuel Klimekc1237a82013-01-23 14:08:21 +000047 if (!Stack.empty())
48 Line.MustBeDeclaration = Stack.back();
49 else
50 Line.MustBeDeclaration = true;
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000051 }
Daniel Jasper393564f2013-05-31 14:56:29 +000052
Manuel Klimek0a3a3c92013-01-23 09:32:48 +000053private:
54 UnwrappedLine &Line;
55 std::vector<bool> &Stack;
56};
57
Krasimir Georgieva1c30932017-05-19 10:34:57 +000058static bool isLineComment(const FormatToken &FormatTok) {
59 return FormatTok.is(tok::comment) &&
60 FormatTok.TokenText.startswith("//");
61}
62
Krasimir Georgievea222a72017-05-22 10:07:56 +000063// Checks if \p FormatTok is a line comment that continues the line comment
64// \p Previous. The original column of \p MinColumnToken is used to determine
65// whether \p FormatTok is indented enough to the right to continue \p Previous.
66static bool continuesLineComment(const FormatToken &FormatTok,
67 const FormatToken *Previous,
68 const FormatToken *MinColumnToken) {
69 if (!Previous || !MinColumnToken)
70 return false;
71 unsigned MinContinueColumn =
72 MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
73 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
74 isLineComment(*Previous) &&
75 FormatTok.OriginalColumn >= MinContinueColumn;
76}
77
Manuel Klimek1abf7892013-01-04 23:34:14 +000078class ScopedMacroState : public FormatTokenSource {
79public:
80 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek20e0af62015-05-06 11:56:29 +000081 FormatToken *&ResetToken)
Manuel Klimek1abf7892013-01-04 23:34:14 +000082 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000083 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
Krasimir Georgieva1c30932017-05-19 10:34:57 +000084 Token(nullptr), PreviousToken(nullptr) {
Manuel Klimek1abf7892013-01-04 23:34:14 +000085 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000086 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000087 Line.InPPDirective = true;
88 }
89
Alexander Kornienko34eb2072015-04-11 02:00:23 +000090 ~ScopedMacroState() override {
Manuel Klimek1abf7892013-01-04 23:34:14 +000091 TokenSource = PreviousTokenSource;
92 ResetToken = Token;
93 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000094 Line.Level = PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +000095 }
96
Craig Topperfb6b25b2014-03-15 04:29:04 +000097 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +000098 // The \c UnwrappedLineParser guards against this by never calling
99 // \c getNextToken() after it has encountered the first eof token.
100 assert(!eof());
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000101 PreviousToken = Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000102 Token = PreviousTokenSource->getNextToken();
103 if (eof())
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000104 return getFakeEOF();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000105 return Token;
106 }
107
Craig Topperfb6b25b2014-03-15 04:29:04 +0000108 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +0000109
Craig Topperfb6b25b2014-03-15 04:29:04 +0000110 FormatToken *setPosition(unsigned Position) override {
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000111 PreviousToken = nullptr;
Manuel Klimekab419912013-05-23 09:41:43 +0000112 Token = PreviousTokenSource->setPosition(Position);
113 return Token;
114 }
115
Manuel Klimek1abf7892013-01-04 23:34:14 +0000116private:
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000117 bool eof() {
118 return Token && Token->HasUnescapedNewline &&
Krasimir Georgievea222a72017-05-22 10:07:56 +0000119 !continuesLineComment(*Token, PreviousToken,
120 /*MinColumnToken=*/PreviousToken);
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000121 }
Manuel Klimek1abf7892013-01-04 23:34:14 +0000122
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000123 FormatToken *getFakeEOF() {
124 static bool EOFInitialized = false;
125 static FormatToken FormatTok;
126 if (!EOFInitialized) {
127 FormatTok.Tok.startToken();
128 FormatTok.Tok.setKind(tok::eof);
129 EOFInitialized = true;
130 }
131 return &FormatTok;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000132 }
133
134 UnwrappedLine &Line;
135 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000136 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000137 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000138 FormatTokenSource *PreviousTokenSource;
139
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000140 FormatToken *Token;
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000141 FormatToken *PreviousToken;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000142};
143
Craig Topper69665e12013-07-01 04:21:54 +0000144} // end anonymous namespace
145
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000146class ScopedLineState {
147public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000148 ScopedLineState(UnwrappedLineParser &Parser,
149 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000150 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000151 if (SwitchToPreprocessorLines)
152 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000153 else if (!Parser.Line->Tokens.empty())
154 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000155 PreBlockLine = std::move(Parser.Line);
156 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000157 Parser.Line->Level = PreBlockLine->Level;
158 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000159 }
160
161 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000162 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000163 Parser.addUnwrappedLine();
164 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000165 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000166 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000167 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
168 Parser.MustBreakBeforeNextToken = true;
169 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000170 }
171
172private:
173 UnwrappedLineParser &Parser;
174
David Blaikieefb6eb22014-08-09 20:02:07 +0000175 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000176 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000177};
178
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000179class CompoundStatementIndenter {
180public:
181 CompoundStatementIndenter(UnwrappedLineParser *Parser,
182 const FormatStyle &Style, unsigned &LineLevel)
183 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000184 if (Style.BraceWrapping.AfterControlStatement)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000185 Parser->addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000186 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000187 ++LineLevel;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000188 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000189 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000190
191private:
192 unsigned &LineLevel;
193 unsigned OldLineLevel;
194};
195
Craig Topper69665e12013-07-01 04:21:54 +0000196namespace {
197
Manuel Klimekab419912013-05-23 09:41:43 +0000198class IndexedTokenSource : public FormatTokenSource {
199public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000200 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000201 : Tokens(Tokens), Position(-1) {}
202
Craig Topperfb6b25b2014-03-15 04:29:04 +0000203 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000204 ++Position;
205 return Tokens[Position];
206 }
207
Craig Topperfb6b25b2014-03-15 04:29:04 +0000208 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000209 assert(Position >= 0);
210 return Position;
211 }
212
Craig Topperfb6b25b2014-03-15 04:29:04 +0000213 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000214 Position = P;
215 return Tokens[Position];
216 }
217
Manuel Klimek71814b42013-10-11 21:25:45 +0000218 void reset() { Position = -1; }
219
Manuel Klimekab419912013-05-23 09:41:43 +0000220private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000221 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000222 int Position;
223};
224
Craig Topper69665e12013-07-01 04:21:54 +0000225} // end anonymous namespace
226
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000227UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000228 const AdditionalKeywords &Keywords,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000229 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000230 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000231 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
Krasimir Georgiev00c5c722017-02-02 15:32:19 +0000232 CurrentLines(&Lines), Style(Style), Keywords(Keywords),
233 CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
Manuel Klimek20e0af62015-05-06 11:56:29 +0000234 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000235
236void UnwrappedLineParser::reset() {
237 PPBranchLevel = -1;
238 Line.reset(new UnwrappedLine);
239 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000240 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000241 MustBreakBeforeNextToken = false;
242 PreprocessorDirectives.clear();
243 CurrentLines = &Lines;
244 DeclarationScopeStack.clear();
Manuel Klimek71814b42013-10-11 21:25:45 +0000245 PPStack.clear();
246}
Daniel Jasperf7935112012-12-03 18:12:45 +0000247
Manuel Klimek20e0af62015-05-06 11:56:29 +0000248void UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000249 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimek71814b42013-10-11 21:25:45 +0000250 do {
251 DEBUG(llvm::dbgs() << "----\n");
252 reset();
253 Tokens = &TokenSource;
254 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000255
Manuel Klimek71814b42013-10-11 21:25:45 +0000256 readToken();
257 parseFile();
258 // Create line with eof token.
259 pushToken(FormatTok);
260 addUnwrappedLine();
261
262 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
263 E = Lines.end();
264 I != E; ++I) {
265 Callback.consumeUnwrappedLine(*I);
266 }
267 Callback.finishRun();
268 Lines.clear();
269 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000270 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000271 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
272 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
273 }
274 if (!PPLevelBranchIndex.empty()) {
275 ++PPLevelBranchIndex.back();
276 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
277 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
278 }
279 } while (!PPLevelBranchIndex.empty());
Manuel Klimek1abf7892013-01-04 23:34:14 +0000280}
281
Manuel Klimek1a18c402013-04-12 14:13:36 +0000282void UnwrappedLineParser::parseFile() {
Daniel Jasper9326f912015-05-05 08:40:32 +0000283 // The top-level context in a file always has declarations, except for pre-
284 // processor directives and JavaScript files.
285 bool MustBeDeclaration =
286 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
287 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
288 MustBeDeclaration);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000289 if (Style.Language == FormatStyle::LK_TextProto)
290 parseBracedList();
291 else
292 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000293 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000294 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000295 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000296}
297
Manuel Klimek1a18c402013-04-12 14:13:36 +0000298void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000299 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000300 do {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000301 tok::TokenKind kind = FormatTok->Tok.getKind();
302 if (FormatTok->Type == TT_MacroBlockBegin) {
303 kind = tok::l_brace;
304 } else if (FormatTok->Type == TT_MacroBlockEnd) {
305 kind = tok::r_brace;
306 }
307
308 switch (kind) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000309 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000310 nextToken();
311 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000312 break;
313 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000314 // FIXME: Add parameter whether this can happen - if this happens, we must
315 // be in a non-declaration context.
Daniel Jasperb86e2722015-08-24 13:23:37 +0000316 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
317 continue;
Nico Weber9096fc02013-06-26 00:30:14 +0000318 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000319 addUnwrappedLine();
320 break;
321 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000322 if (HasOpeningBrace)
323 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000324 nextToken();
325 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000326 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000327 case tok::kw_default:
328 case tok::kw_case:
Martin Probstf785fd92017-08-04 17:07:15 +0000329 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration) {
330 // A 'case: string' style field declaration.
331 parseStructuralElement();
332 break;
333 }
Daniel Jasper72407622013-09-02 08:26:29 +0000334 if (!SwitchLabelEncountered &&
335 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
336 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000337 SwitchLabelEncountered = true;
338 parseStructuralElement();
339 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000340 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000341 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000342 break;
343 }
344 } while (!eof());
345}
346
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000347void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
Manuel Klimekab419912013-05-23 09:41:43 +0000348 // We'll parse forward through the tokens until we hit
349 // a closing brace or eof - note that getNextToken() will
350 // parse macros, so this will magically work inside macro
351 // definitions, too.
352 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000353 FormatToken *Tok = FormatTok;
Daniel Jasperb9a49902016-01-09 15:56:28 +0000354 const FormatToken *PrevTok = getPreviousToken();
Manuel Klimekab419912013-05-23 09:41:43 +0000355 // Keep a stack of positions of lbrace tokens. We will
356 // update information about whether an lbrace starts a
357 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000358 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000359 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000360 do {
Daniel Jaspereb65e912015-12-21 18:31:15 +0000361 // Get next non-comment token.
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000362 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000363 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000364 do {
365 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000366 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000367 } while (NextTok->is(tok::comment));
368
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000369 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000370 case tok::l_brace:
Martin Probst95ed8e72017-05-31 09:29:40 +0000371 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) {
372 if (PrevTok->is(tok::colon))
373 // A colon indicates this code is in a type, or a braced list
374 // following a label in an object literal ({a: {b: 1}}). The code
375 // below could be confused by semicolons between the individual
376 // members in a type member list, which would normally trigger
377 // BK_Block. In both cases, this must be parsed as an inline braced
378 // init.
379 Tok->BlockKind = BK_BracedInit;
380 else if (PrevTok->is(tok::r_paren))
381 // `) { }` can only occur in function or method declarations in JS.
382 Tok->BlockKind = BK_Block;
383 } else {
Daniel Jasperb9a49902016-01-09 15:56:28 +0000384 Tok->BlockKind = BK_Unknown;
Martin Probst95ed8e72017-05-31 09:29:40 +0000385 }
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000386 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000387 break;
388 case tok::r_brace:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000389 if (LBraceStack.empty())
390 break;
391 if (LBraceStack.back()->BlockKind == BK_Unknown) {
392 bool ProbablyBracedList = false;
393 if (Style.Language == FormatStyle::LK_Proto) {
394 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
395 } else {
396 // Using OriginalColumn to distinguish between ObjC methods and
397 // binary operators is a bit hacky.
398 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
399 NextTok->OriginalColumn == 0;
Daniel Jasper91b032a2014-05-22 12:46:38 +0000400
Daniel Jasperb9a49902016-01-09 15:56:28 +0000401 // If there is a comma, semicolon or right paren after the closing
402 // brace, we assume this is a braced initializer list. Note that
403 // regardless how we mark inner braces here, we will overwrite the
404 // BlockKind later if we parse a braced list (where all blocks
405 // inside are by default braced lists), or when we explicitly detect
406 // blocks (for example while parsing lambdas).
Martin Probst95ed8e72017-05-31 09:29:40 +0000407 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
408 // braced list in JS.
Daniel Jasperb9a49902016-01-09 15:56:28 +0000409 ProbablyBracedList =
Daniel Jasperacffeb82016-03-05 18:34:26 +0000410 (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probste1e12a72016-08-19 14:35:01 +0000411 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
412 Keywords.kw_as)) ||
Martin Probstb7fb2672017-05-10 13:53:29 +0000413 (Style.isCpp() && NextTok->is(tok::l_paren)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000414 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
415 tok::r_paren, tok::r_square, tok::l_brace,
Martin Probstb7fb2672017-05-10 13:53:29 +0000416 tok::l_square, tok::ellipsis) ||
Daniel Jaspere4ada022016-12-13 10:05:03 +0000417 (NextTok->is(tok::identifier) &&
418 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000419 (NextTok->is(tok::semi) &&
420 (!ExpectClassBody || LBraceStack.size() != 1)) ||
421 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Manuel Klimekab419912013-05-23 09:41:43 +0000422 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000423 if (ProbablyBracedList) {
424 Tok->BlockKind = BK_BracedInit;
425 LBraceStack.back()->BlockKind = BK_BracedInit;
426 } else {
427 Tok->BlockKind = BK_Block;
428 LBraceStack.back()->BlockKind = BK_Block;
429 }
Manuel Klimekab419912013-05-23 09:41:43 +0000430 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000431 LBraceStack.pop_back();
Manuel Klimekab419912013-05-23 09:41:43 +0000432 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000433 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000434 case tok::semi:
435 case tok::kw_if:
436 case tok::kw_while:
437 case tok::kw_for:
438 case tok::kw_switch:
439 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000440 case tok::kw___try:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000441 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000442 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000443 break;
444 default:
445 break;
446 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000447 PrevTok = Tok;
Manuel Klimekab419912013-05-23 09:41:43 +0000448 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000449 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Daniel Jasperb9a49902016-01-09 15:56:28 +0000450
Manuel Klimekab419912013-05-23 09:41:43 +0000451 // Assume other blocks for all unclosed opening braces.
452 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000453 if (LBraceStack[i]->BlockKind == BK_Unknown)
454 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000455 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000456
Manuel Klimekab419912013-05-23 09:41:43 +0000457 FormatTok = Tokens->setPosition(StoredPosition);
458}
459
Francois Ferranda98a95c2017-07-28 07:56:14 +0000460template <class T>
461static inline void hash_combine(std::size_t &seed, const T &v) {
462 std::hash<T> hasher;
463 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
464}
465
466size_t UnwrappedLineParser::computePPHash() const {
467 size_t h = 0;
468 for (const auto &i : PPStack) {
469 hash_combine(h, size_t(i.Kind));
470 hash_combine(h, i.Line);
471 }
472 return h;
473}
474
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000475void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
476 bool MunchSemi) {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000477 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
478 "'{' or macro block token expected");
479 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
Daniel Jaspereb65e912015-12-21 18:31:15 +0000480 FormatTok->BlockKind = BK_Block;
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000481
Francois Ferranda98a95c2017-07-28 07:56:14 +0000482 size_t PPStartHash = computePPHash();
483
Daniel Jasper516d7972013-07-25 11:31:57 +0000484 unsigned InitialLevel = Line->Level;
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000485 nextToken(/*LevelDifference=*/AddLevel ? 1 : 0);
Daniel Jasperf7935112012-12-03 18:12:45 +0000486
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000487 if (MacroBlock && FormatTok->is(tok::l_paren))
488 parseParens();
489
Francois Ferranda98a95c2017-07-28 07:56:14 +0000490 size_t NbPreprocessorDirectives =
491 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000492 addUnwrappedLine();
Francois Ferranda98a95c2017-07-28 07:56:14 +0000493 size_t OpeningLineIndex =
494 CurrentLines->empty()
495 ? (UnwrappedLine::kInvalidIndex)
496 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
Daniel Jasperf7935112012-12-03 18:12:45 +0000497
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000498 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
499 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000500 if (AddLevel)
501 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000502 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000503
Marianne Mailhot-Sarrasin03137c62016-04-14 14:56:49 +0000504 if (eof())
505 return;
506
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000507 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
508 : !FormatTok->is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000509 Line->Level = InitialLevel;
Daniel Jaspereb65e912015-12-21 18:31:15 +0000510 FormatTok->BlockKind = BK_Block;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000511 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000512 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000513
Francois Ferranda98a95c2017-07-28 07:56:14 +0000514 size_t PPEndHash = computePPHash();
515
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000516 // Munch the closing brace.
517 nextToken(/*LevelDifference=*/AddLevel ? -1 : 0);
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000518
519 if (MacroBlock && FormatTok->is(tok::l_paren))
520 parseParens();
521
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000522 if (MunchSemi && FormatTok->Tok.is(tok::semi))
523 nextToken();
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000524 Line->Level = InitialLevel;
Francois Ferranda98a95c2017-07-28 07:56:14 +0000525
526 if (PPStartHash == PPEndHash) {
527 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
528 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
529 // Update the opening line to add the forward reference as well
530 (*CurrentLines)[OpeningLineIndex].MatchingOpeningBlockLineIndex =
531 CurrentLines->size() - 1;
532 }
Francois Ferrande56a8292017-06-14 12:29:47 +0000533 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000534}
535
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000536static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000537 // FIXME: Closure-library specific stuff should not be hard-coded but be
538 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000539 if (Line.Tokens.size() < 4)
540 return false;
541 auto I = Line.Tokens.begin();
542 if (I->Tok->TokenText != "goog")
543 return false;
544 ++I;
545 if (I->Tok->isNot(tok::period))
546 return false;
547 ++I;
548 if (I->Tok->TokenText != "scope")
549 return false;
550 ++I;
551 return I->Tok->is(tok::l_paren);
552}
553
Martin Probst101ec892017-05-09 20:04:09 +0000554static bool isIIFE(const UnwrappedLine &Line,
555 const AdditionalKeywords &Keywords) {
556 // Look for the start of an immediately invoked anonymous function.
557 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
558 // This is commonly done in JavaScript to create a new, anonymous scope.
559 // Example: (function() { ... })()
560 if (Line.Tokens.size() < 3)
561 return false;
562 auto I = Line.Tokens.begin();
563 if (I->Tok->isNot(tok::l_paren))
564 return false;
565 ++I;
566 if (I->Tok->isNot(Keywords.kw_function))
567 return false;
568 ++I;
569 return I->Tok->is(tok::l_paren);
570}
571
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000572static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
573 const FormatToken &InitialToken) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000574 if (InitialToken.is(tok::kw_namespace))
575 return Style.BraceWrapping.AfterNamespace;
576 if (InitialToken.is(tok::kw_class))
577 return Style.BraceWrapping.AfterClass;
578 if (InitialToken.is(tok::kw_union))
579 return Style.BraceWrapping.AfterUnion;
580 if (InitialToken.is(tok::kw_struct))
581 return Style.BraceWrapping.AfterStruct;
582 return false;
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000583}
584
Manuel Klimek516e0542013-09-04 13:25:30 +0000585void UnwrappedLineParser::parseChildBlock() {
586 FormatTok->BlockKind = BK_Block;
587 nextToken();
588 {
Martin Probst101ec892017-05-09 20:04:09 +0000589 bool SkipIndent =
590 (Style.Language == FormatStyle::LK_JavaScript &&
591 (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
Manuel Klimek516e0542013-09-04 13:25:30 +0000592 ScopedLineState LineState(*this);
593 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
594 /*MustBeDeclaration=*/false);
Martin Probst101ec892017-05-09 20:04:09 +0000595 Line->Level += SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000596 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000597 flushComments(isOnNewLine(*FormatTok));
Martin Probst101ec892017-05-09 20:04:09 +0000598 Line->Level -= SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000599 }
600 nextToken();
601}
602
Daniel Jasperf7935112012-12-03 18:12:45 +0000603void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000604 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000605 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000606 nextToken();
607
Craig Topper2145bc02014-05-09 08:15:10 +0000608 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000609 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000610 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000611 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000612
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000613 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000614 case tok::pp_define:
615 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000616 return;
617 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000618 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000619 break;
620 case tok::pp_ifdef:
621 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000622 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000623 break;
624 case tok::pp_else:
625 parsePPElse();
626 break;
627 case tok::pp_elif:
628 parsePPElIf();
629 break;
630 case tok::pp_endif:
631 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000632 break;
633 default:
634 parsePPUnknown();
635 break;
636 }
637}
638
Manuel Klimek68b03042014-04-14 09:14:11 +0000639void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
Francois Ferranda98a95c2017-07-28 07:56:14 +0000640 size_t Line = CurrentLines->size();
641 if (CurrentLines == &PreprocessorDirectives)
642 Line += Lines.size();
643
644 if (Unreachable ||
645 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
646 PPStack.push_back({PP_Unreachable, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000647 else
Francois Ferranda98a95c2017-07-28 07:56:14 +0000648 PPStack.push_back({PP_Conditional, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000649}
650
Manuel Klimek68b03042014-04-14 09:14:11 +0000651void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000652 ++PPBranchLevel;
653 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
654 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
655 PPLevelBranchIndex.push_back(0);
656 PPLevelBranchCount.push_back(0);
657 }
658 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000659 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
660 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000661}
662
Manuel Klimek68b03042014-04-14 09:14:11 +0000663void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000664 if (!PPStack.empty())
665 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000666 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
667 if (!PPChainBranchIndex.empty())
668 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000669 conditionalCompilationCondition(
670 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
671 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000672}
673
Manuel Klimek68b03042014-04-14 09:14:11 +0000674void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000675 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
676 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
677 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000678 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
679 }
680 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000681 // Guard against #endif's without #if.
682 if (PPBranchLevel > 0)
683 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000684 if (!PPChainBranchIndex.empty())
685 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000686 if (!PPStack.empty())
687 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000688}
689
690void UnwrappedLineParser::parsePPIf(bool IfDef) {
Daniel Jasper62703eb2017-03-01 11:10:11 +0000691 bool IfNDef = FormatTok->is(tok::pp_ifndef);
Manuel Klimek68b03042014-04-14 09:14:11 +0000692 nextToken();
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000693 bool Unreachable = false;
694 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
695 Unreachable = true;
Daniel Jasper62703eb2017-03-01 11:10:11 +0000696 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000697 Unreachable = true;
698 conditionalCompilationStart(Unreachable);
Manuel Klimek68b03042014-04-14 09:14:11 +0000699 parsePPUnknown();
700}
701
702void UnwrappedLineParser::parsePPElse() {
703 conditionalCompilationAlternative();
704 parsePPUnknown();
705}
706
707void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
708
709void UnwrappedLineParser::parsePPEndIf() {
710 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000711 parsePPUnknown();
712}
713
Manuel Klimek1abf7892013-01-04 23:34:14 +0000714void UnwrappedLineParser::parsePPDefine() {
715 nextToken();
716
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000717 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000718 parsePPUnknown();
719 return;
720 }
721 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000722 if (FormatTok->Tok.getKind() == tok::l_paren &&
723 FormatTok->WhitespaceRange.getBegin() ==
724 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000725 parseParens();
726 }
727 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000728 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000729
730 // Errors during a preprocessor directive can only affect the layout of the
731 // preprocessor directive, and thus we ignore them. An alternative approach
732 // would be to use the same approach we use on the file level (no
733 // re-indentation if there was a structural error) within the macro
734 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000735 parseFile();
736}
737
738void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000739 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000740 nextToken();
741 } while (!eof());
742 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000743}
744
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000745// Here we blacklist certain tokens that are not usually the first token in an
746// unwrapped line. This is used in attempt to distinguish macro calls without
747// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000748static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000749 // Semicolon can be a null-statement, l_square can be a start of a macro or
750 // a C++11 attribute, but this doesn't seem to be common.
751 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
752 Tok.isNot(tok::l_square) &&
753 // Tokens that can only be used as binary operators and a part of
754 // overloaded operator names.
755 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
756 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
757 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
758 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
759 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
760 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
761 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
762 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
763 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
764 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
765 Tok.isNot(tok::lesslessequal) &&
766 // Colon is used in labels, base class lists, initializer lists,
767 // range-based for loops, ternary operator, but should never be the
768 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000769 Tok.isNot(tok::colon) &&
770 // 'noexcept' is a trailing annotation.
771 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000772}
773
Martin Probst533965c2016-04-19 18:19:06 +0000774static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
775 const FormatToken *FormatTok) {
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000776 // FIXME: This returns true for C/C++ keywords like 'struct'.
777 return FormatTok->is(tok::identifier) &&
778 (FormatTok->Tok.getIdentifierInfo() == nullptr ||
Martin Probst3dbbefa2016-11-10 16:21:02 +0000779 !FormatTok->isOneOf(
780 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
781 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
782 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
783 Keywords.kw_let, Keywords.kw_var, tok::kw_const,
784 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
785 Keywords.kw_instanceof, Keywords.kw_interface,
Martin Probst93008f02017-07-18 14:00:19 +0000786 Keywords.kw_throws, Keywords.kw_from));
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000787}
788
Martin Probst533965c2016-04-19 18:19:06 +0000789static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
790 const FormatToken *FormatTok) {
Martin Probstb9316ff2016-09-18 17:21:52 +0000791 return FormatTok->Tok.isLiteral() ||
792 FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
793 mustBeJSIdent(Keywords, FormatTok);
Martin Probst533965c2016-04-19 18:19:06 +0000794}
795
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000796// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
797// when encountered after a value (see mustBeJSIdentOrValue).
798static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
799 const FormatToken *FormatTok) {
800 return FormatTok->isOneOf(
Martin Probst5f8445b2016-04-24 22:05:09 +0000801 tok::kw_return, Keywords.kw_yield,
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000802 // conditionals
803 tok::kw_if, tok::kw_else,
804 // loops
805 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
806 // switch/case
807 tok::kw_switch, tok::kw_case,
808 // exceptions
809 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
810 // declaration
811 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
Martin Probst5f8445b2016-04-24 22:05:09 +0000812 Keywords.kw_async, Keywords.kw_function,
813 // import/export
814 Keywords.kw_import, tok::kw_export);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000815}
816
817// readTokenWithJavaScriptASI reads the next token and terminates the current
818// line if JavaScript Automatic Semicolon Insertion must
819// happen between the current token and the next token.
820//
821// This method is conservative - it cannot cover all edge cases of JavaScript,
822// but only aims to correctly handle certain well known cases. It *must not*
823// return true in speculative cases.
824void UnwrappedLineParser::readTokenWithJavaScriptASI() {
825 FormatToken *Previous = FormatTok;
826 readToken();
827 FormatToken *Next = FormatTok;
828
829 bool IsOnSameLine =
830 CommentsBeforeNextToken.empty()
831 ? Next->NewlinesBefore == 0
832 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
833 if (IsOnSameLine)
834 return;
835
836 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
Martin Probst717f6dc2016-10-21 05:11:38 +0000837 bool PreviousStartsTemplateExpr =
838 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${");
Martin Probstbbffeac2016-04-11 07:35:57 +0000839 if (PreviousMustBeValue && Line && Line->Tokens.size() > 1) {
840 // If the token before the previous one is an '@', the previous token is an
841 // annotation and can precede another identifier/value.
Benjamin Kramer5ffc24e2016-04-11 12:19:19 +0000842 const FormatToken *PrePrevious = std::prev(Line->Tokens.end(), 2)->Tok;
Martin Probstbbffeac2016-04-11 07:35:57 +0000843 if (PrePrevious->is(tok::at))
844 return;
845 }
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000846 if (Next->is(tok::exclaim) && PreviousMustBeValue)
Martin Probstd40bca42017-01-09 08:56:36 +0000847 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000848 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
Martin Probst717f6dc2016-10-21 05:11:38 +0000849 bool NextEndsTemplateExpr =
850 Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
851 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
852 (PreviousMustBeValue ||
853 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
854 tok::minusminus)))
Martin Probstd40bca42017-01-09 08:56:36 +0000855 return addUnwrappedLine();
Martin Probst0a19d432017-08-09 15:19:16 +0000856 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) &&
857 isJSDeclOrStmt(Keywords, Next))
Martin Probstd40bca42017-01-09 08:56:36 +0000858 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000859}
860
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000861void UnwrappedLineParser::parseStructuralElement() {
Daniel Jasper498f5582015-12-25 08:53:31 +0000862 assert(!FormatTok->is(tok::l_brace));
863 if (Style.Language == FormatStyle::LK_TableGen &&
864 FormatTok->is(tok::pp_include)) {
865 nextToken();
866 if (FormatTok->is(tok::string_literal))
867 nextToken();
868 addUnwrappedLine();
869 return;
870 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000871 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000872 case tok::at:
873 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000874 if (FormatTok->Tok.is(tok::l_brace)) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000875 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +0000876 parseBracedList();
877 break;
878 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000879 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000880 case tok::objc_public:
881 case tok::objc_protected:
882 case tok::objc_package:
883 case tok::objc_private:
884 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000885 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000886 case tok::objc_implementation:
887 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000888 case tok::objc_protocol:
889 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000890 case tok::objc_end:
891 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000892 case tok::objc_optional:
893 case tok::objc_required:
894 nextToken();
895 addUnwrappedLine();
896 return;
Nico Weber45c48122015-06-28 01:06:16 +0000897 case tok::objc_autoreleasepool:
898 nextToken();
899 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000900 if (Style.BraceWrapping.AfterObjCDeclaration)
Nico Weber45c48122015-06-28 01:06:16 +0000901 addUnwrappedLine();
902 parseBlock(/*MustBeDeclaration=*/false);
903 }
904 addUnwrappedLine();
905 return;
Nico Weber33381f52015-02-07 01:57:32 +0000906 case tok::objc_try:
907 // This branch isn't strictly necessary (the kw_try case below would
908 // do this too after the tok::at is parsed above). But be explicit.
909 parseTryCatch();
910 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000911 default:
912 break;
913 }
914 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000915 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000916 nextToken();
917 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000918 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000919 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000920 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000921 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000922 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000923 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000924 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000925 break;
926 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000927 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000928 nextToken();
929 }
930 }
931 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000932 case tok::kw_namespace:
933 parseNamespace();
934 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000935 case tok::kw_inline:
936 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000937 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000938 parseNamespace();
939 return;
940 }
941 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000942 case tok::kw_public:
943 case tok::kw_protected:
944 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +0000945 if (Style.Language == FormatStyle::LK_Java ||
946 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000947 nextToken();
948 else
949 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000950 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000951 case tok::kw_if:
952 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000953 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000954 case tok::kw_for:
955 case tok::kw_while:
956 parseForOrWhileLoop();
957 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000958 case tok::kw_do:
959 parseDoWhile();
960 return;
961 case tok::kw_switch:
Martin Probstf785fd92017-08-04 17:07:15 +0000962 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
963 // 'switch: string' field declaration.
964 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000965 parseSwitch();
966 return;
967 case tok::kw_default:
Martin Probstf785fd92017-08-04 17:07:15 +0000968 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
969 // 'default: string' field declaration.
970 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000971 nextToken();
972 parseLabel();
973 return;
974 case tok::kw_case:
Martin Probstf785fd92017-08-04 17:07:15 +0000975 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
976 // 'case: string' field declaration.
977 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000978 parseCaseLabel();
979 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000980 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000981 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +0000982 parseTryCatch();
983 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000984 case tok::kw_extern:
985 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000986 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000987 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000988 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000989 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000990 addUnwrappedLine();
991 return;
992 }
993 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000994 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +0000995 case tok::kw_export:
996 if (Style.Language == FormatStyle::LK_JavaScript) {
997 parseJavaScriptEs6ImportExport();
998 return;
999 }
1000 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +00001001 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001002 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001003 parseForOrWhileLoop();
1004 return;
1005 }
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001006 if (FormatTok->is(TT_MacroBlockBegin)) {
1007 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
1008 /*MunchSemi=*/false);
1009 return;
1010 }
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001011 if (FormatTok->is(Keywords.kw_import)) {
1012 if (Style.Language == FormatStyle::LK_JavaScript) {
1013 parseJavaScriptEs6ImportExport();
1014 return;
1015 }
1016 if (Style.Language == FormatStyle::LK_Proto) {
1017 nextToken();
Daniel Jasper8b61d142016-06-20 20:39:53 +00001018 if (FormatTok->is(tok::kw_public))
1019 nextToken();
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001020 if (!FormatTok->is(tok::string_literal))
1021 return;
1022 nextToken();
1023 if (FormatTok->is(tok::semi))
1024 nextToken();
1025 addUnwrappedLine();
1026 return;
1027 }
Daniel Jasper354aa512015-02-19 16:07:32 +00001028 }
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001029 if (Style.isCpp() &&
Daniel Jasper72b33572017-03-31 12:04:37 +00001030 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
Daniel Jaspera00de632015-12-01 12:05:04 +00001031 Keywords.kw_slots, Keywords.kw_qslots)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001032 nextToken();
1033 if (FormatTok->is(tok::colon)) {
1034 nextToken();
1035 addUnwrappedLine();
Daniel Jasper31343832016-07-27 10:13:24 +00001036 return;
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001037 }
Daniel Jasper53395402015-04-07 15:04:40 +00001038 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001039 // In all other cases, parse the declaration.
1040 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001041 default:
1042 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001043 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001044 do {
Daniel Jaspera7900ad2016-05-08 18:12:22 +00001045 const FormatToken *Previous = getPreviousToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001046 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +00001047 case tok::at:
1048 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001049 if (FormatTok->Tok.is(tok::l_brace)) {
1050 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001051 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001052 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001053 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001054 case tok::kw_enum:
Daniel Jaspera7900ad2016-05-08 18:12:22 +00001055 // Ignore if this is part of "template <enum ...".
1056 if (Previous && Previous->is(tok::less)) {
1057 nextToken();
1058 break;
1059 }
1060
Daniel Jasper90cf3802015-06-17 09:44:02 +00001061 // parseEnum falls through and does not yet add an unwrapped line as an
1062 // enum definition can start a structural element.
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001063 if (!parseEnum())
1064 break;
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001065 // This only applies for C++.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001066 if (!Style.isCpp()) {
Daniel Jasper90cf3802015-06-17 09:44:02 +00001067 addUnwrappedLine();
1068 return;
1069 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001070 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001071 case tok::kw_typedef:
1072 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +00001073 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1074 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001075 parseEnum();
1076 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001077 case tok::kw_struct:
1078 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +00001079 case tok::kw_class:
Daniel Jasper910807d2015-06-12 04:52:02 +00001080 // parseRecord falls through and does not yet add an unwrapped line as a
1081 // record declaration or definition can start a structural element.
Manuel Klimeke01bab52013-01-15 13:38:33 +00001082 parseRecord();
Daniel Jasper910807d2015-06-12 04:52:02 +00001083 // This does not apply for Java and JavaScript.
1084 if (Style.Language == FormatStyle::LK_Java ||
1085 Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperd5ec65b2016-01-08 07:06:07 +00001086 if (FormatTok->is(tok::semi))
1087 nextToken();
Daniel Jasper910807d2015-06-12 04:52:02 +00001088 addUnwrappedLine();
1089 return;
1090 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001091 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +00001092 case tok::period:
1093 nextToken();
1094 // In Java, classes have an implicit static member "class".
1095 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
1096 FormatTok->is(tok::kw_class))
1097 nextToken();
Daniel Jasperba52fcb2015-09-28 14:29:45 +00001098 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
1099 FormatTok->Tok.getIdentifierInfo())
1100 // JavaScript only has pseudo keywords, all keywords are allowed to
1101 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1102 nextToken();
Daniel Jaspere5d74862014-11-26 08:17:08 +00001103 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001104 case tok::semi:
1105 nextToken();
1106 addUnwrappedLine();
1107 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001108 case tok::r_brace:
1109 addUnwrappedLine();
1110 return;
Daniel Jasperf7935112012-12-03 18:12:45 +00001111 case tok::l_paren:
1112 parseParens();
1113 break;
Daniel Jasper5af04a42015-10-07 03:43:10 +00001114 case tok::kw_operator:
1115 nextToken();
1116 if (FormatTok->isBinaryOperator())
1117 nextToken();
1118 break;
Manuel Klimek516e0542013-09-04 13:25:30 +00001119 case tok::caret:
1120 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +00001121 if (FormatTok->Tok.isAnyIdentifier() ||
1122 FormatTok->isSimpleTypeSpecifier())
1123 nextToken();
1124 if (FormatTok->is(tok::l_paren))
1125 parseParens();
1126 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +00001127 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +00001128 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001129 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001130 if (!tryToParseBracedList()) {
1131 // A block outside of parentheses must be the last part of a
1132 // structural element.
1133 // FIXME: Figure out cases where this is not true, and add projections
1134 // for them (the one we know is missing are lambdas).
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001135 if (Style.BraceWrapping.AfterFunction)
Manuel Klimekab419912013-05-23 09:41:43 +00001136 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +00001137 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +00001138 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001139 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +00001140 return;
1141 }
1142 // Otherwise this was a braced init list, and the structural
1143 // element continues.
1144 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001145 case tok::kw_try:
1146 // We arrive here when parsing function-try blocks.
1147 parseTryCatch();
1148 return;
Daniel Jasper40e19212013-05-29 13:16:10 +00001149 case tok::identifier: {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001150 if (FormatTok->is(TT_MacroBlockEnd)) {
1151 addUnwrappedLine();
1152 return;
1153 }
1154
Martin Probst973ff792017-04-27 13:07:24 +00001155 // Function declarations (as opposed to function expressions) are parsed
1156 // on their own unwrapped line by continuing this loop. Function
1157 // expressions (functions that are not on their own line) must not create
1158 // a new unwrapped line, so they are special cased below.
1159 size_t TokenCount = Line->Tokens.size();
Daniel Jasper9326f912015-05-05 08:40:32 +00001160 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst973ff792017-04-27 13:07:24 +00001161 FormatTok->is(Keywords.kw_function) &&
1162 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1163 Keywords.kw_async)))) {
Daniel Jasper069e5f42014-05-20 11:14:57 +00001164 tryToParseJSFunction();
1165 break;
1166 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001167 if ((Style.Language == FormatStyle::LK_JavaScript ||
1168 Style.Language == FormatStyle::LK_Java) &&
1169 FormatTok->is(Keywords.kw_interface)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001170 if (Style.Language == FormatStyle::LK_JavaScript) {
1171 // In JavaScript/TypeScript, "interface" can be used as a standalone
1172 // identifier, e.g. in `var interface = 1;`. If "interface" is
1173 // followed by another identifier, it is very like to be an actual
1174 // interface declaration.
1175 unsigned StoredPosition = Tokens->getPosition();
1176 FormatToken *Next = Tokens->getNextToken();
1177 FormatTok = Tokens->setPosition(StoredPosition);
Martin Probst533965c2016-04-19 18:19:06 +00001178 if (Next && !mustBeJSIdent(Keywords, Next)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001179 nextToken();
1180 break;
1181 }
1182 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001183 parseRecord();
Daniel Jasper259188b2015-06-12 04:56:34 +00001184 addUnwrappedLine();
Daniel Jasper5c235c02015-07-06 14:26:04 +00001185 return;
Daniel Jasper9326f912015-05-05 08:40:32 +00001186 }
1187
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001188 // See if the following token should start a new unwrapped line.
Daniel Jasper9326f912015-05-05 08:40:32 +00001189 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +00001190 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +00001191 if (Line->Tokens.size() == 1 &&
1192 // JS doesn't have macros, and within classes colons indicate fields,
1193 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +00001194 Style.Language != FormatStyle::LK_JavaScript) {
1195 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Daniel Jasper40609472016-04-06 15:02:46 +00001196 Line->Tokens.begin()->Tok->MustBreakBefore = true;
Alexander Kornienkode644272013-04-08 22:16:06 +00001197 parseLabel();
1198 return;
1199 }
Daniel Jasper680b09b2014-11-05 10:48:04 +00001200 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +00001201 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +00001202 bool FunctionLike = FormatTok->is(tok::l_paren);
1203 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +00001204 parseParens();
Daniel Jaspere60cba12015-05-13 11:35:53 +00001205
1206 bool FollowedByNewline =
1207 CommentsBeforeNextToken.empty()
1208 ? FormatTok->NewlinesBefore > 0
1209 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1210
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001211 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
Daniel Jasper680b09b2014-11-05 10:48:04 +00001212 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +00001213 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +00001214 return;
Alexander Kornienkode644272013-04-08 22:16:06 +00001215 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001216 }
1217 break;
Daniel Jasper40e19212013-05-29 13:16:10 +00001218 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001219 case tok::equal:
Manuel Klimek79e06082015-05-21 12:23:34 +00001220 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1221 // TT_JsFatArrow. The always start an expression or a child block if
1222 // followed by a curly.
1223 if (FormatTok->is(TT_JsFatArrow)) {
1224 nextToken();
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001225 if (FormatTok->is(tok::l_brace))
Manuel Klimek79e06082015-05-21 12:23:34 +00001226 parseChildBlock();
Manuel Klimek79e06082015-05-21 12:23:34 +00001227 break;
1228 }
1229
Daniel Jaspere25509f2012-12-17 11:29:41 +00001230 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001231 if (FormatTok->Tok.is(tok::l_brace)) {
1232 nextToken();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001233 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001234 } else if (Style.Language == FormatStyle::LK_Proto &&
1235 FormatTok->Tok.is(tok::less)) {
1236 nextToken();
Krasimir Georgiev0b41fcb2017-06-27 13:58:41 +00001237 parseBracedList(/*ContinueOnSemicolons=*/false,
1238 /*ClosingBraceKind=*/tok::greater);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001239 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001240 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001241 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001242 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001243 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +00001244 case tok::kw_new:
1245 parseNew();
1246 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001247 default:
1248 nextToken();
1249 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001250 }
1251 } while (!eof());
1252}
1253
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001254bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001255 if (!Style.isCpp()) {
Daniel Jasper1feab0f2015-06-02 15:31:37 +00001256 nextToken();
1257 return false;
1258 }
Daniel Jasperb9a49902016-01-09 15:56:28 +00001259 const FormatToken* Previous = getPreviousToken();
1260 if (Previous &&
1261 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1262 tok::kw_delete) ||
1263 Previous->closesScope() || Previous->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +00001264 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001265 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +00001266 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001267 assert(FormatTok->is(tok::l_square));
1268 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001269 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001270 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001271
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001272 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001273 if (FormatTok->isSimpleTypeSpecifier()) {
1274 nextToken();
1275 continue;
1276 }
Manuel Klimekffdeb592013-09-03 15:10:01 +00001277 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001278 case tok::l_brace:
1279 break;
1280 case tok::l_paren:
1281 parseParens();
1282 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +00001283 case tok::amp:
1284 case tok::star:
1285 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +00001286 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001287 case tok::less:
1288 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001289 case tok::identifier:
Daniel Jasper5eaa0092015-08-13 13:37:08 +00001290 case tok::numeric_constant:
Daniel Jasper1067ab02014-02-11 10:16:55 +00001291 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001292 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +00001293 nextToken();
1294 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001295 case tok::arrow:
Daniel Jasper6f2b88a2015-06-05 13:18:09 +00001296 FormatTok->Type = TT_LambdaArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001297 nextToken();
1298 break;
1299 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001300 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001301 }
1302 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001303 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +00001304 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001305 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001306}
1307
1308bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
1309 nextToken();
1310 if (FormatTok->is(tok::equal)) {
1311 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001312 if (FormatTok->is(tok::r_square)) {
1313 nextToken();
1314 return true;
1315 }
1316 if (FormatTok->isNot(tok::comma))
1317 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001318 nextToken();
1319 } else if (FormatTok->is(tok::amp)) {
1320 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001321 if (FormatTok->is(tok::r_square)) {
1322 nextToken();
1323 return true;
1324 }
Manuel Klimekffdeb592013-09-03 15:10:01 +00001325 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
1326 return false;
1327 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001328 if (FormatTok->is(tok::comma))
1329 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001330 } else if (FormatTok->is(tok::r_square)) {
1331 nextToken();
1332 return true;
1333 }
1334 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001335 if (FormatTok->is(tok::amp))
1336 nextToken();
1337 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
1338 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001339 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +00001340 if (FormatTok->is(tok::ellipsis))
1341 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001342 if (FormatTok->is(tok::comma)) {
1343 nextToken();
1344 } else if (FormatTok->is(tok::r_square)) {
1345 nextToken();
1346 return true;
1347 } else {
1348 return false;
1349 }
1350 } while (!eof());
1351 return false;
1352}
1353
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001354void UnwrappedLineParser::tryToParseJSFunction() {
Martin Probst409697e2016-05-29 14:41:07 +00001355 assert(FormatTok->is(Keywords.kw_function) ||
1356 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
Martin Probst5f8445b2016-04-24 22:05:09 +00001357 if (FormatTok->is(Keywords.kw_async))
1358 nextToken();
1359 // Consume "function".
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001360 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001361
Daniel Jasper71e50af2016-11-01 06:22:59 +00001362 // Consume * (generator function). Treat it like C++'s overloaded operators.
1363 if (FormatTok->is(tok::star)) {
1364 FormatTok->Type = TT_OverloadedOperator;
Martin Probst5f8445b2016-04-24 22:05:09 +00001365 nextToken();
Daniel Jasper71e50af2016-11-01 06:22:59 +00001366 }
Martin Probst5f8445b2016-04-24 22:05:09 +00001367
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001368 // Consume function name.
1369 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001370 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001371
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001372 if (FormatTok->isNot(tok::l_paren))
1373 return;
Manuel Klimek79e06082015-05-21 12:23:34 +00001374
1375 // Parse formal parameter list.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001376 parseParens();
Manuel Klimek79e06082015-05-21 12:23:34 +00001377
1378 if (FormatTok->is(tok::colon)) {
1379 // Parse a type definition.
1380 nextToken();
1381
1382 // Eat the type declaration. For braced inline object types, balance braces,
1383 // otherwise just parse until finding an l_brace for the function body.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001384 if (FormatTok->is(tok::l_brace))
1385 tryToParseBracedList();
1386 else
Martin Probstaf16c502017-01-04 13:36:43 +00001387 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
Manuel Klimek79e06082015-05-21 12:23:34 +00001388 nextToken();
Manuel Klimek79e06082015-05-21 12:23:34 +00001389 }
1390
Martin Probstaf16c502017-01-04 13:36:43 +00001391 if (FormatTok->is(tok::semi))
1392 return;
1393
Manuel Klimek79e06082015-05-21 12:23:34 +00001394 parseChildBlock();
1395}
1396
Daniel Jasper3c883d12015-05-18 14:49:19 +00001397bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001398 if (FormatTok->BlockKind == BK_Unknown)
Daniel Jasper3c883d12015-05-18 14:49:19 +00001399 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001400 assert(FormatTok->BlockKind != BK_Unknown);
1401 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001402 return false;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001403 nextToken();
Manuel Klimekab419912013-05-23 09:41:43 +00001404 parseBracedList();
1405 return true;
1406}
1407
Krasimir Georgievff747be2017-06-27 13:43:07 +00001408bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1409 tok::TokenKind ClosingBraceKind) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001410 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001411
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001412 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1413 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001414 do {
Manuel Klimek79e06082015-05-21 12:23:34 +00001415 if (Style.Language == FormatStyle::LK_JavaScript) {
Martin Probst409697e2016-05-29 14:41:07 +00001416 if (FormatTok->is(Keywords.kw_function) ||
1417 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001418 tryToParseJSFunction();
1419 continue;
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001420 }
1421 if (FormatTok->is(TT_JsFatArrow)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001422 nextToken();
1423 // Fat arrows can be followed by simple expressions or by child blocks
1424 // in curly braces.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001425 if (FormatTok->is(tok::l_brace)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001426 parseChildBlock();
1427 continue;
1428 }
1429 }
Martin Probst8e3eba02017-02-07 16:33:13 +00001430 if (FormatTok->is(tok::l_brace)) {
1431 // Could be a method inside of a braced list `{a() { return 1; }}`.
1432 if (tryToParseBracedList())
1433 continue;
1434 parseChildBlock();
1435 }
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001436 }
Krasimir Georgievff747be2017-06-27 13:43:07 +00001437 if (FormatTok->Tok.getKind() == ClosingBraceKind) {
1438 nextToken();
1439 return !HasError;
1440 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001441 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001442 case tok::caret:
1443 nextToken();
1444 if (FormatTok->is(tok::l_brace)) {
1445 parseChildBlock();
1446 }
1447 break;
1448 case tok::l_square:
1449 tryToParseLambda();
1450 break;
Daniel Jaspera87af7a2015-06-30 11:32:22 +00001451 case tok::l_paren:
1452 parseParens();
Daniel Jasperf46dec82015-03-31 14:34:15 +00001453 // JavaScript can just have free standing methods and getters/setters in
1454 // object literals. Detect them by a "{" following ")".
1455 if (Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperf46dec82015-03-31 14:34:15 +00001456 if (FormatTok->is(tok::l_brace))
1457 parseChildBlock();
1458 break;
1459 }
Daniel Jasperf46dec82015-03-31 14:34:15 +00001460 break;
Martin Probst8e3eba02017-02-07 16:33:13 +00001461 case tok::l_brace:
1462 // Assume there are no blocks inside a braced init list apart
1463 // from the ones we explicitly parse out (like lambdas).
1464 FormatTok->BlockKind = BK_BracedInit;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001465 nextToken();
Martin Probst8e3eba02017-02-07 16:33:13 +00001466 parseBracedList();
1467 break;
Krasimir Georgievfa4dbb62017-08-03 13:43:45 +00001468 case tok::less:
1469 if (Style.Language == FormatStyle::LK_Proto) {
1470 nextToken();
1471 parseBracedList(/*ContinueOnSemicolons=*/false,
1472 /*ClosingBraceKind=*/tok::greater);
1473 } else {
1474 nextToken();
1475 }
1476 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001477 case tok::semi:
Daniel Jasperb9a49902016-01-09 15:56:28 +00001478 // JavaScript (or more precisely TypeScript) can have semicolons in braced
1479 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1480 // used for error recovery if we have otherwise determined that this is
1481 // a braced list.
1482 if (Style.Language == FormatStyle::LK_JavaScript) {
1483 nextToken();
1484 break;
1485 }
Daniel Jasper015ed022013-09-13 09:20:45 +00001486 HasError = true;
1487 if (!ContinueOnSemicolons)
1488 return !HasError;
1489 nextToken();
1490 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001491 case tok::comma:
1492 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001493 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001494 default:
1495 nextToken();
1496 break;
1497 }
1498 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001499 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001500}
1501
Daniel Jasperf7935112012-12-03 18:12:45 +00001502void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001503 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001504 nextToken();
1505 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001506 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001507 case tok::l_paren:
1508 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001509 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1510 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001511 break;
1512 case tok::r_paren:
1513 nextToken();
1514 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001515 case tok::r_brace:
1516 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1517 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001518 case tok::l_square:
1519 tryToParseLambda();
1520 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001521 case tok::l_brace:
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001522 if (!tryToParseBracedList())
Manuel Klimekf017dc02013-09-04 13:34:14 +00001523 parseChildBlock();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001524 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001525 case tok::at:
1526 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001527 if (FormatTok->Tok.is(tok::l_brace)) {
1528 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001529 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001530 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001531 break;
Martin Probst1027fb82017-02-07 14:05:30 +00001532 case tok::kw_class:
1533 if (Style.Language == FormatStyle::LK_JavaScript)
1534 parseRecord(/*ParseAsExpr=*/true);
1535 else
1536 nextToken();
1537 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001538 case tok::identifier:
1539 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst409697e2016-05-29 14:41:07 +00001540 (FormatTok->is(Keywords.kw_function) ||
1541 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001542 tryToParseJSFunction();
1543 else
1544 nextToken();
1545 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001546 default:
1547 nextToken();
1548 break;
1549 }
1550 } while (!eof());
1551}
1552
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001553void UnwrappedLineParser::parseSquare() {
1554 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1555 if (tryToParseLambda())
1556 return;
1557 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001558 switch (FormatTok->Tok.getKind()) {
1559 case tok::l_paren:
1560 parseParens();
1561 break;
1562 case tok::r_square:
1563 nextToken();
1564 return;
1565 case tok::r_brace:
1566 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1567 return;
1568 case tok::l_square:
1569 parseSquare();
1570 break;
1571 case tok::l_brace: {
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001572 if (!tryToParseBracedList())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001573 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001574 break;
1575 }
1576 case tok::at:
1577 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001578 if (FormatTok->Tok.is(tok::l_brace)) {
1579 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001580 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001581 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001582 break;
1583 default:
1584 nextToken();
1585 break;
1586 }
1587 } while (!eof());
1588}
1589
Daniel Jasperf7935112012-12-03 18:12:45 +00001590void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001591 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001592 nextToken();
Daniel Jasper6a7d5a72017-06-19 07:40:49 +00001593 if (FormatTok->Tok.is(tok::kw_constexpr))
1594 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001595 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001596 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001597 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001598 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001599 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001600 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001601 if (Style.BraceWrapping.BeforeElse)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001602 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001603 else
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001604 NeedsUnwrappedLine = true;
Daniel Jasperf7935112012-12-03 18:12:45 +00001605 } else {
1606 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001607 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001608 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001609 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001610 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001611 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001612 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001613 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001614 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001615 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001616 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001617 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001618 parseIfThenElse();
1619 } else {
1620 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001621 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001622 parseStructuralElement();
Daniel Jasper451544a2016-05-19 06:30:48 +00001623 if (FormatTok->is(tok::eof))
1624 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001625 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001626 }
1627 } else if (NeedsUnwrappedLine) {
1628 addUnwrappedLine();
1629 }
1630}
1631
Daniel Jasper04a71a42014-05-08 11:58:24 +00001632void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001633 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001634 nextToken();
1635 bool NeedsUnwrappedLine = false;
1636 if (FormatTok->is(tok::colon)) {
1637 // We are in a function try block, what comes is an initializer list.
1638 nextToken();
1639 while (FormatTok->is(tok::identifier)) {
1640 nextToken();
1641 if (FormatTok->is(tok::l_paren))
1642 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001643 if (FormatTok->is(tok::comma))
1644 nextToken();
1645 }
1646 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001647 // Parse try with resource.
1648 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1649 parseParens();
1650 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001651 if (FormatTok->is(tok::l_brace)) {
1652 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1653 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001654 if (Style.BraceWrapping.BeforeCatch) {
Daniel Jasper04a71a42014-05-08 11:58:24 +00001655 addUnwrappedLine();
1656 } else {
1657 NeedsUnwrappedLine = true;
1658 }
1659 } else if (!FormatTok->is(tok::kw_catch)) {
1660 // The C++ standard requires a compound-statement after a try.
1661 // If there's none, we try to assume there's a structuralElement
1662 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001663 addUnwrappedLine();
1664 ++Line->Level;
1665 parseStructuralElement();
1666 --Line->Level;
1667 }
Nico Weber33381f52015-02-07 01:57:32 +00001668 while (1) {
1669 if (FormatTok->is(tok::at))
1670 nextToken();
1671 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1672 tok::kw___finally) ||
1673 ((Style.Language == FormatStyle::LK_Java ||
1674 Style.Language == FormatStyle::LK_JavaScript) &&
1675 FormatTok->is(Keywords.kw_finally)) ||
1676 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1677 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1678 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001679 nextToken();
1680 while (FormatTok->isNot(tok::l_brace)) {
1681 if (FormatTok->is(tok::l_paren)) {
1682 parseParens();
1683 continue;
1684 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001685 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001686 return;
1687 nextToken();
1688 }
1689 NeedsUnwrappedLine = false;
1690 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1691 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001692 if (Style.BraceWrapping.BeforeCatch)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001693 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001694 else
Daniel Jasper04a71a42014-05-08 11:58:24 +00001695 NeedsUnwrappedLine = true;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001696 }
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001697 if (NeedsUnwrappedLine)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001698 addUnwrappedLine();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001699}
1700
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001701void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001702 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001703
1704 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001705 nextToken();
Saleem Abdulrasool328085f2015-10-30 05:07:56 +00001706 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001707 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001708 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001709 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001710 addUnwrappedLine();
1711
Daniel Jasper65ee3472013-07-31 23:16:02 +00001712 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1713 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1714 DeclarationScopeStack.size() > 1);
1715 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001716 // Munch the semicolon after a namespace. This is more common than one would
1717 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001718 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001719 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001720 addUnwrappedLine();
1721 }
1722 // FIXME: Add error handling.
1723}
1724
Daniel Jasper6acf5132015-03-12 14:44:29 +00001725void UnwrappedLineParser::parseNew() {
1726 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1727 nextToken();
1728 if (Style.Language != FormatStyle::LK_Java)
1729 return;
1730
1731 // In Java, we can parse everything up to the parens, which aren't optional.
1732 do {
1733 // There should not be a ;, { or } before the new's open paren.
1734 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1735 return;
1736
1737 // Consume the parens.
1738 if (FormatTok->is(tok::l_paren)) {
1739 parseParens();
1740
1741 // If there is a class body of an anonymous class, consume that as child.
1742 if (FormatTok->is(tok::l_brace))
1743 parseChildBlock();
1744 return;
1745 }
1746 nextToken();
1747 } while (!eof());
1748}
1749
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001750void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001751 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001752 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001753 nextToken();
Martin Probsta050f412017-05-18 21:19:29 +00001754 // JS' for await ( ...
Martin Probstbd49e322017-05-15 19:33:20 +00001755 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probsta050f412017-05-18 21:19:29 +00001756 FormatTok->is(Keywords.kw_await))
Martin Probstbd49e322017-05-15 19:33:20 +00001757 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001758 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001759 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001760 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001761 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001762 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001763 addUnwrappedLine();
1764 } else {
1765 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001766 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001767 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001768 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001769 }
1770}
1771
Daniel Jasperf7935112012-12-03 18:12:45 +00001772void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001773 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001774 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001775 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001776 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001777 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001778 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001779 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001780 } else {
1781 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001782 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001783 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001784 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001785 }
1786
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001787 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001788 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001789 addUnwrappedLine();
1790 return;
1791 }
1792
Daniel Jasperf7935112012-12-03 18:12:45 +00001793 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001794 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001795}
1796
1797void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001798 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001799 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001800 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001801 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001802 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001803 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001804 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001805 if (FormatTok->Tok.is(tok::kw_break)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001806 if (Style.BraceWrapping.AfterControlStatement)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001807 addUnwrappedLine();
1808 parseStructuralElement();
1809 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001810 addUnwrappedLine();
1811 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001812 if (FormatTok->is(tok::semi))
1813 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001814 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001815 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001816 Line->Level = OldLineLevel;
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001817 if (FormatTok->isNot(tok::l_brace)) {
Daniel Jasper40609472016-04-06 15:02:46 +00001818 parseStructuralElement();
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001819 addUnwrappedLine();
1820 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001821}
1822
1823void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001824 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001825 // FIXME: fix handling of complex expressions here.
1826 do {
1827 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001828 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001829 parseLabel();
1830}
1831
1832void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001833 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001834 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001835 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001836 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001837 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001838 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001839 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001840 addUnwrappedLine();
1841 } else {
1842 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001843 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001844 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001845 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001846 }
1847}
1848
1849void UnwrappedLineParser::parseAccessSpecifier() {
1850 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001851 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001852 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001853 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001854 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001855 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001856 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001857 addUnwrappedLine();
1858}
1859
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001860bool UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001861 // Won't be 'enum' for NS_ENUMs.
1862 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001863 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001864
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001865 // In TypeScript, "enum" can also be used as property name, e.g. in interface
1866 // declarations. An "enum" keyword followed by a colon would be a syntax
1867 // error and thus assume it is just an identifier.
Daniel Jasper87379302016-02-03 05:33:44 +00001868 if (Style.Language == FormatStyle::LK_JavaScript &&
1869 FormatTok->isOneOf(tok::colon, tok::question))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001870 return false;
1871
Daniel Jasper2b41a822013-08-20 12:42:50 +00001872 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001873 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1874 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001875
Daniel Jasper786a5502013-09-06 21:32:35 +00001876 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001877 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1878 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001879 nextToken();
1880 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001881 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001882 parseParens();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001883 if (FormatTok->is(tok::identifier)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001884 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001885 // If there are two identifiers in a row, this is likely an elaborate
1886 // return type. In Java, this can be "implements", etc.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001887 if (Style.isCpp() && FormatTok->is(tok::identifier))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001888 return false;
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001889 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001890 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001891
1892 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001893 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001894 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00001895 FormatTok->BlockKind = BK_Block;
1896
1897 if (Style.Language == FormatStyle::LK_Java) {
1898 // Java enums are different.
1899 parseJavaEnumBody();
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001900 return true;
1901 }
1902 if (Style.Language == FormatStyle::LK_Proto) {
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001903 parseBlock(/*MustBeDeclaration=*/true);
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001904 return true;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001905 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001906
1907 // Parse enum body.
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001908 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001909 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1910 if (HasError) {
1911 if (FormatTok->is(tok::semi))
1912 nextToken();
1913 addUnwrappedLine();
1914 }
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001915 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00001916
Daniel Jasper90cf3802015-06-17 09:44:02 +00001917 // There is no addUnwrappedLine() here so that we fall through to parsing a
1918 // structural element afterwards. Thus, in "enum A {} n, m;",
Manuel Klimek2cec0192013-01-21 19:17:52 +00001919 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001920}
1921
1922void UnwrappedLineParser::parseJavaEnumBody() {
1923 // Determine whether the enum is simple, i.e. does not have a semicolon or
1924 // constants with class bodies. Simple enums can be formatted like braced
1925 // lists, contracted to a single line, etc.
1926 unsigned StoredPosition = Tokens->getPosition();
1927 bool IsSimple = true;
1928 FormatToken *Tok = Tokens->getNextToken();
1929 while (Tok) {
1930 if (Tok->is(tok::r_brace))
1931 break;
1932 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1933 IsSimple = false;
1934 break;
1935 }
1936 // FIXME: This will also mark enums with braces in the arguments to enum
1937 // constants as "not simple". This is probably fine in practice, though.
1938 Tok = Tokens->getNextToken();
1939 }
1940 FormatTok = Tokens->setPosition(StoredPosition);
1941
1942 if (IsSimple) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001943 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001944 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001945 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001946 return;
1947 }
1948
1949 // Parse the body of a more complex enum.
1950 // First add a line for everything up to the "{".
1951 nextToken();
1952 addUnwrappedLine();
1953 ++Line->Level;
1954
1955 // Parse the enum constants.
1956 while (FormatTok) {
1957 if (FormatTok->is(tok::l_brace)) {
1958 // Parse the constant's class body.
1959 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1960 /*MunchSemi=*/false);
1961 } else if (FormatTok->is(tok::l_paren)) {
1962 parseParens();
1963 } else if (FormatTok->is(tok::comma)) {
1964 nextToken();
1965 addUnwrappedLine();
1966 } else if (FormatTok->is(tok::semi)) {
1967 nextToken();
1968 addUnwrappedLine();
1969 break;
1970 } else if (FormatTok->is(tok::r_brace)) {
1971 addUnwrappedLine();
1972 break;
1973 } else {
1974 nextToken();
1975 }
1976 }
1977
1978 // Parse the class body after the enum's ";" if any.
1979 parseLevel(/*HasOpeningBrace=*/true);
1980 nextToken();
1981 --Line->Level;
1982 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001983}
1984
Martin Probst1027fb82017-02-07 14:05:30 +00001985void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001986 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001987 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00001988
Daniel Jasper04785d02015-05-06 14:03:02 +00001989 // The actual identifier can be a nested name specifier, and in macros
1990 // it is often token-pasted.
1991 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
1992 tok::kw___attribute, tok::kw___declspec,
1993 tok::kw_alignas) ||
1994 ((Style.Language == FormatStyle::LK_Java ||
1995 Style.Language == FormatStyle::LK_JavaScript) &&
1996 FormatTok->isOneOf(tok::period, tok::comma))) {
Martin Probstcb870c52017-08-01 15:46:10 +00001997 if (Style.Language == FormatStyle::LK_JavaScript &&
1998 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
1999 // JavaScript/TypeScript supports inline object types in
2000 // extends/implements positions:
2001 // class Foo implements {bar: number} { }
2002 nextToken();
2003 if (FormatTok->is(tok::l_brace)) {
2004 tryToParseBracedList();
2005 continue;
2006 }
2007 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002008 bool IsNonMacroIdentifier =
2009 FormatTok->is(tok::identifier) &&
2010 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002011 nextToken();
2012 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00002013 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00002014 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00002015 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00002016
Daniel Jasper04785d02015-05-06 14:03:02 +00002017 // Note that parsing away template declarations here leads to incorrectly
2018 // accepting function declarations as record declarations.
2019 // In general, we cannot solve this problem. Consider:
2020 // class A<int> B() {}
2021 // which can be a function definition or a class definition when B() is a
2022 // macro. If we find enough real-world cases where this is a problem, we
2023 // can parse for the 'template' keyword in the beginning of the statement,
2024 // and thus rule out the record production in case there is no template
2025 // (this would still leave us with an ambiguity between template function
2026 // and class declarations).
Daniel Jasperadba2aa2015-05-18 12:52:00 +00002027 if (FormatTok->isOneOf(tok::colon, tok::less)) {
2028 while (!eof()) {
Daniel Jasper3c883d12015-05-18 14:49:19 +00002029 if (FormatTok->is(tok::l_brace)) {
2030 calculateBraceTypes(/*ExpectClassBody=*/true);
2031 if (!tryToParseBracedList())
2032 break;
2033 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002034 if (FormatTok->Tok.is(tok::semi))
2035 return;
2036 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002037 }
2038 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002039 if (FormatTok->Tok.is(tok::l_brace)) {
Martin Probst1027fb82017-02-07 14:05:30 +00002040 if (ParseAsExpr) {
2041 parseChildBlock();
2042 } else {
2043 if (ShouldBreakBeforeBrace(Style, InitialToken))
2044 addUnwrappedLine();
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002045
Martin Probst1027fb82017-02-07 14:05:30 +00002046 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2047 /*MunchSemi=*/false);
2048 }
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002049 }
Daniel Jasper90cf3802015-06-17 09:44:02 +00002050 // There is no addUnwrappedLine() here so that we fall through to parsing a
2051 // structural element afterwards. Thus, in "class A {} n, m;",
2052 // "} n, m;" will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00002053}
2054
Nico Weber8696a8d2013-01-09 21:15:03 +00002055void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002056 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00002057 do
2058 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002059 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00002060 nextToken(); // Skip '>'.
2061}
2062
2063void UnwrappedLineParser::parseObjCUntilAtEnd() {
2064 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002065 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002066 nextToken();
2067 addUnwrappedLine();
2068 break;
2069 }
Daniel Jaspera15da302013-08-28 08:04:23 +00002070 if (FormatTok->is(tok::l_brace)) {
2071 parseBlock(/*MustBeDeclaration=*/false);
2072 // In ObjC interfaces, nothing should be following the "}".
2073 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00002074 } else if (FormatTok->is(tok::r_brace)) {
2075 // Ignore stray "}". parseStructuralElement doesn't consume them.
2076 nextToken();
2077 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00002078 } else {
2079 parseStructuralElement();
2080 }
Nico Weber8696a8d2013-01-09 21:15:03 +00002081 } while (!eof());
2082}
2083
Nico Weber2ce0ac52013-01-09 23:25:37 +00002084void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00002085 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002086 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00002087
2088 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002089 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00002090 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002091 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002092 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00002093 // Skip category, if present.
2094 parseParens();
2095
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002096 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002097 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00002098
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002099 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00002100 if (Style.BraceWrapping.AfterObjCDeclaration)
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002101 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00002102 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002103 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00002104
2105 // With instance variables, this puts '}' on its own line. Without instance
2106 // variables, this ends the @interface line.
2107 addUnwrappedLine();
2108
Nico Weber8696a8d2013-01-09 21:15:03 +00002109 parseObjCUntilAtEnd();
2110}
Nico Weber7eecf4b2013-01-09 20:25:35 +00002111
Nico Weber8696a8d2013-01-09 21:15:03 +00002112void UnwrappedLineParser::parseObjCProtocol() {
2113 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002114 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00002115
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002116 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002117 parseObjCProtocolList();
2118
2119 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002120 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002121 nextToken();
2122 return addUnwrappedLine();
2123 }
2124
2125 addUnwrappedLine();
2126 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00002127}
2128
Daniel Jasperfca735c2015-02-19 16:14:18 +00002129void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
Martin Probst053f1aa2016-04-19 14:55:37 +00002130 bool IsImport = FormatTok->is(Keywords.kw_import);
2131 assert(IsImport || FormatTok->is(tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00002132 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00002133
Daniel Jasperec05fc72015-05-11 09:14:50 +00002134 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002135 if (FormatTok->is(tok::kw_default))
2136 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00002137
Martin Probst5f8445b2016-04-24 22:05:09 +00002138 // Consume "async function", "function" and "default function", so that these
2139 // get parsed as free-standing JS functions, i.e. do not require a trailing
2140 // semicolon.
2141 if (FormatTok->is(Keywords.kw_async))
2142 nextToken();
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002143 if (FormatTok->is(Keywords.kw_function)) {
2144 nextToken();
2145 return;
2146 }
2147
Martin Probst053f1aa2016-04-19 14:55:37 +00002148 // For imports, `export *`, `export {...}`, consume the rest of the line up
2149 // to the terminating `;`. For everything else, just return and continue
2150 // parsing the structural element, i.e. the declaration or expression for
2151 // `export default`.
2152 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
2153 !FormatTok->isStringLiteral())
2154 return;
Daniel Jasperfca735c2015-02-19 16:14:18 +00002155
Martin Probstd40bca42017-01-09 08:56:36 +00002156 while (!eof()) {
2157 if (FormatTok->is(tok::semi))
2158 return;
2159 if (Line->Tokens.size() == 0) {
2160 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
2161 // import statement should terminate.
2162 return;
2163 }
Daniel Jasperefc1a832016-01-07 08:53:35 +00002164 if (FormatTok->is(tok::l_brace)) {
2165 FormatTok->BlockKind = BK_Block;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002166 nextToken();
Daniel Jasperefc1a832016-01-07 08:53:35 +00002167 parseBracedList();
2168 } else {
2169 nextToken();
2170 }
Daniel Jasper354aa512015-02-19 16:07:32 +00002171 }
2172}
2173
Daniel Jasper3b203a62013-09-05 16:05:56 +00002174LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
2175 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002176 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
2177 << (Line.InPPDirective ? " MACRO" : "") << ": ";
2178 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2179 E = Line.Tokens.end();
2180 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002181 llvm::dbgs() << I->Tok->Tok.getName() << "["
2182 << "T=" << I->Tok->Type
2183 << ", OC=" << I->Tok->OriginalColumn << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002184 }
2185 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2186 E = Line.Tokens.end();
2187 I != E; ++I) {
2188 const UnwrappedLineNode &Node = *I;
2189 for (SmallVectorImpl<UnwrappedLine>::const_iterator
2190 I = Node.Children.begin(),
2191 E = Node.Children.end();
2192 I != E; ++I) {
2193 printDebugInfo(*I, "\nChild: ");
2194 }
2195 }
2196 llvm::dbgs() << "\n";
2197}
2198
Daniel Jasperf7935112012-12-03 18:12:45 +00002199void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002200 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00002201 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00002202 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002203 if (CurrentLines == &Lines)
2204 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00002205 });
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002206 CurrentLines->push_back(std::move(*Line));
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002207 Line->Tokens.clear();
Krasimir Georgiev85c37042017-03-01 16:38:08 +00002208 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002209 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002210 CurrentLines->append(
2211 std::make_move_iterator(PreprocessorDirectives.begin()),
2212 std::make_move_iterator(PreprocessorDirectives.end()));
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002213 PreprocessorDirectives.clear();
2214 }
Daniel Jasperf7935112012-12-03 18:12:45 +00002215}
2216
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002217bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00002218
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002219bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002220 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
2221 FormatTok.NewlinesBefore > 0;
2222}
2223
Krasimir Georgiev91834222017-01-25 13:58:58 +00002224// Checks if \p FormatTok is a line comment that continues the line comment
2225// section on \p Line.
Krasimir Georgievea222a72017-05-22 10:07:56 +00002226static bool continuesLineCommentSection(const FormatToken &FormatTok,
2227 const UnwrappedLine &Line,
2228 llvm::Regex &CommentPragmasRegex) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002229 if (Line.Tokens.empty())
2230 return false;
Krasimir Georgiev84321612017-01-30 19:18:55 +00002231
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002232 StringRef IndentContent = FormatTok.TokenText;
2233 if (FormatTok.TokenText.startswith("//") ||
2234 FormatTok.TokenText.startswith("/*"))
2235 IndentContent = FormatTok.TokenText.substr(2);
2236 if (CommentPragmasRegex.match(IndentContent))
2237 return false;
2238
Krasimir Georgiev91834222017-01-25 13:58:58 +00002239 // If Line starts with a line comment, then FormatTok continues the comment
Krasimir Georgiev84321612017-01-30 19:18:55 +00002240 // section if its original column is greater or equal to the original start
Krasimir Georgiev91834222017-01-25 13:58:58 +00002241 // column of the line.
2242 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002243 // Define the min column token of a line as follows: if a line ends in '{' or
2244 // contains a '{' followed by a line comment, then the min column token is
2245 // that '{'. Otherwise, the min column token of the line is the first token of
2246 // the line.
2247 //
2248 // If Line starts with a token other than a line comment, then FormatTok
2249 // continues the comment section if its original column is greater than the
2250 // original start column of the min column token of the line.
Krasimir Georgiev91834222017-01-25 13:58:58 +00002251 //
2252 // For example, the second line comment continues the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002253 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002254 // // first line
2255 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002256 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002257 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002258 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002259 // // first line
2260 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002261 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002262 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002263 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002264 // int i; // first line
2265 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002266 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002267 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002268 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002269 // do { // first line
2270 // // second line
2271 // int i;
2272 // } while (true);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002273 //
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002274 // and:
2275 //
2276 // enum {
2277 // a, // first line
2278 // // second line
2279 // b
2280 // };
2281 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002282 // The second line comment doesn't continue the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002283 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002284 // // first line
2285 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002286 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002287 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002288 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002289 // int i; // first line
2290 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002291 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002292 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002293 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002294 // do { // first line
2295 // // second line
2296 // int i;
2297 // } while (true);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002298 //
2299 // and:
2300 //
2301 // enum {
2302 // a, // first line
2303 // // second line
2304 // };
Krasimir Georgiev84321612017-01-30 19:18:55 +00002305 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
2306
2307 // Scan for '{//'. If found, use the column of '{' as a min column for line
2308 // comment section continuation.
2309 const FormatToken *PreviousToken = nullptr;
Krasimir Georgievd86c25d2017-03-10 13:09:29 +00002310 for (const UnwrappedLineNode &Node : Line.Tokens) {
Krasimir Georgiev84321612017-01-30 19:18:55 +00002311 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
2312 isLineComment(*Node.Tok)) {
2313 MinColumnToken = PreviousToken;
2314 break;
2315 }
2316 PreviousToken = Node.Tok;
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002317
2318 // Grab the last newline preceding a token in this unwrapped line.
2319 if (Node.Tok->NewlinesBefore > 0) {
2320 MinColumnToken = Node.Tok;
2321 }
Krasimir Georgiev84321612017-01-30 19:18:55 +00002322 }
2323 if (PreviousToken && PreviousToken->is(tok::l_brace)) {
2324 MinColumnToken = PreviousToken;
2325 }
2326
Krasimir Georgievea222a72017-05-22 10:07:56 +00002327 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
2328 MinColumnToken);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002329}
2330
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002331void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
2332 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002333 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002334 I = CommentsBeforeNextToken.begin(),
2335 E = CommentsBeforeNextToken.end();
2336 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002337 // Line comments that belong to the same line comment section are put on the
2338 // same line since later we might want to reflow content between them.
Krasimir Georgiev753625b2017-01-31 13:32:38 +00002339 // Additional fine-grained breaking of line comment sections is controlled
2340 // by the class BreakableLineCommentSection in case it is desirable to keep
2341 // several line comment sections in the same unwrapped line.
2342 //
2343 // FIXME: Consider putting separate line comment sections as children to the
2344 // unwrapped line instead.
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002345 (*I)->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002346 continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002347 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002348 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002349 pushToken(*I);
2350 }
Daniel Jaspere60cba12015-05-13 11:35:53 +00002351 if (NewlineBeforeNext && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002352 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002353 CommentsBeforeNextToken.clear();
2354}
2355
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002356void UnwrappedLineParser::nextToken(int LevelDifference) {
Daniel Jasperf7935112012-12-03 18:12:45 +00002357 if (eof())
2358 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002359 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002360 pushToken(FormatTok);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002361 if (Style.Language != FormatStyle::LK_JavaScript)
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002362 readToken(LevelDifference);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002363 else
2364 readTokenWithJavaScriptASI();
Manuel Klimek1abf7892013-01-04 23:34:14 +00002365}
2366
Daniel Jasperb9a49902016-01-09 15:56:28 +00002367const FormatToken *UnwrappedLineParser::getPreviousToken() {
2368 // FIXME: This is a dirty way to access the previous token. Find a better
2369 // solution.
2370 if (!Line || Line->Tokens.empty())
2371 return nullptr;
2372 return Line->Tokens.back().Tok;
2373}
2374
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002375void UnwrappedLineParser::distributeComments(
2376 const SmallVectorImpl<FormatToken *> &Comments,
2377 const FormatToken *NextTok) {
2378 // Whether or not a line comment token continues a line is controlled by
Krasimir Georgievea222a72017-05-22 10:07:56 +00002379 // the method continuesLineCommentSection, with the following caveat:
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002380 //
2381 // Define a trail of Comments to be a nonempty proper postfix of Comments such
2382 // that each comment line from the trail is aligned with the next token, if
2383 // the next token exists. If a trail exists, the beginning of the maximal
2384 // trail is marked as a start of a new comment section.
2385 //
2386 // For example in this code:
2387 //
2388 // int a; // line about a
2389 // // line 1 about b
2390 // // line 2 about b
2391 // int b;
2392 //
2393 // the two lines about b form a maximal trail, so there are two sections, the
2394 // first one consisting of the single comment "// line about a" and the
2395 // second one consisting of the next two comments.
2396 if (Comments.empty())
2397 return;
2398 bool ShouldPushCommentsInCurrentLine = true;
2399 bool HasTrailAlignedWithNextToken = false;
2400 unsigned StartOfTrailAlignedWithNextToken = 0;
2401 if (NextTok) {
2402 // We are skipping the first element intentionally.
2403 for (unsigned i = Comments.size() - 1; i > 0; --i) {
2404 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
2405 HasTrailAlignedWithNextToken = true;
2406 StartOfTrailAlignedWithNextToken = i;
2407 }
2408 }
2409 }
2410 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
2411 FormatToken *FormatTok = Comments[i];
2412 if (HasTrailAlignedWithNextToken &&
2413 i == StartOfTrailAlignedWithNextToken) {
2414 FormatTok->ContinuesLineCommentSection = false;
2415 } else {
2416 FormatTok->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002417 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002418 }
2419 if (!FormatTok->ContinuesLineCommentSection &&
2420 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
2421 ShouldPushCommentsInCurrentLine = false;
2422 }
2423 if (ShouldPushCommentsInCurrentLine) {
2424 pushToken(FormatTok);
2425 } else {
2426 CommentsBeforeNextToken.push_back(FormatTok);
2427 }
2428 }
2429}
2430
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002431void UnwrappedLineParser::readToken(int LevelDifference) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002432 SmallVector<FormatToken *, 1> Comments;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002433 do {
2434 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00002435 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002436 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
2437 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002438 distributeComments(Comments, FormatTok);
2439 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002440 // If there is an unfinished unwrapped line, we flush the preprocessor
2441 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00002442 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002443 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002444 assert((LevelDifference >= 0 ||
2445 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2446 "LevelDifference makes Line->Level negative");
2447 Line->Level += LevelDifference;
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00002448 // Comments stored before the preprocessor directive need to be output
2449 // before the preprocessor directive, at the same level as the
2450 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002451 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002452 parsePPDirective();
2453 }
Manuel Klimek68b03042014-04-14 09:14:11 +00002454 while (FormatTok->Type == TT_ConflictStart ||
2455 FormatTok->Type == TT_ConflictEnd ||
2456 FormatTok->Type == TT_ConflictAlternative) {
2457 if (FormatTok->Type == TT_ConflictStart) {
2458 conditionalCompilationStart(/*Unreachable=*/false);
2459 } else if (FormatTok->Type == TT_ConflictAlternative) {
2460 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002461 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00002462 conditionalCompilationEnd();
2463 }
2464 FormatTok = Tokens->getNextToken();
2465 FormatTok->MustBreakBefore = true;
2466 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002467
Francois Ferranda98a95c2017-07-28 07:56:14 +00002468 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002469 !Line->InPPDirective) {
2470 continue;
2471 }
2472
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002473 if (!FormatTok->Tok.is(tok::comment)) {
2474 distributeComments(Comments, FormatTok);
2475 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002476 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002477 }
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002478
2479 Comments.push_back(FormatTok);
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002480 } while (!eof());
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002481
2482 distributeComments(Comments, nullptr);
2483 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002484}
2485
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002486void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002487 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002488 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002489 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002490 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00002491 }
Daniel Jasperf7935112012-12-03 18:12:45 +00002492}
2493
Daniel Jasper8d1832e2013-01-07 13:26:07 +00002494} // end namespace format
2495} // end namespace clang