blob: 9243cd99cbbced61640929bbe01cbb9056aba745 [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) {
Manuel Klimek89628f62017-09-20 09:51:03 +000059 return FormatTok.is(tok::comment) && FormatTok.TokenText.startswith("//");
Krasimir Georgieva1c30932017-05-19 10:34:57 +000060}
61
Krasimir Georgievea222a72017-05-22 10:07:56 +000062// Checks if \p FormatTok is a line comment that continues the line comment
63// \p Previous. The original column of \p MinColumnToken is used to determine
64// whether \p FormatTok is indented enough to the right to continue \p Previous.
65static bool continuesLineComment(const FormatToken &FormatTok,
66 const FormatToken *Previous,
67 const FormatToken *MinColumnToken) {
68 if (!Previous || !MinColumnToken)
69 return false;
70 unsigned MinContinueColumn =
71 MinColumnToken->OriginalColumn + (isLineComment(*MinColumnToken) ? 0 : 1);
72 return isLineComment(FormatTok) && FormatTok.NewlinesBefore == 1 &&
73 isLineComment(*Previous) &&
74 FormatTok.OriginalColumn >= MinContinueColumn;
75}
76
Manuel Klimek1abf7892013-01-04 23:34:14 +000077class ScopedMacroState : public FormatTokenSource {
78public:
79 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek20e0af62015-05-06 11:56:29 +000080 FormatToken *&ResetToken)
Manuel Klimek1abf7892013-01-04 23:34:14 +000081 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000082 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
Krasimir Georgieva1c30932017-05-19 10:34:57 +000083 Token(nullptr), PreviousToken(nullptr) {
Manuel Klimek1abf7892013-01-04 23:34:14 +000084 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000085 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000086 Line.InPPDirective = true;
87 }
88
Alexander Kornienko34eb2072015-04-11 02:00:23 +000089 ~ScopedMacroState() override {
Manuel Klimek1abf7892013-01-04 23:34:14 +000090 TokenSource = PreviousTokenSource;
91 ResetToken = Token;
92 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000093 Line.Level = PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +000094 }
95
Craig Topperfb6b25b2014-03-15 04:29:04 +000096 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +000097 // The \c UnwrappedLineParser guards against this by never calling
98 // \c getNextToken() after it has encountered the first eof token.
99 assert(!eof());
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000100 PreviousToken = Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000101 Token = PreviousTokenSource->getNextToken();
102 if (eof())
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000103 return getFakeEOF();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000104 return Token;
105 }
106
Craig Topperfb6b25b2014-03-15 04:29:04 +0000107 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +0000108
Craig Topperfb6b25b2014-03-15 04:29:04 +0000109 FormatToken *setPosition(unsigned Position) override {
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000110 PreviousToken = nullptr;
Manuel Klimekab419912013-05-23 09:41:43 +0000111 Token = PreviousTokenSource->setPosition(Position);
112 return Token;
113 }
114
Manuel Klimek1abf7892013-01-04 23:34:14 +0000115private:
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000116 bool eof() {
117 return Token && Token->HasUnescapedNewline &&
Krasimir Georgievea222a72017-05-22 10:07:56 +0000118 !continuesLineComment(*Token, PreviousToken,
119 /*MinColumnToken=*/PreviousToken);
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000120 }
Manuel Klimek1abf7892013-01-04 23:34:14 +0000121
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000122 FormatToken *getFakeEOF() {
123 static bool EOFInitialized = false;
124 static FormatToken FormatTok;
125 if (!EOFInitialized) {
126 FormatTok.Tok.startToken();
127 FormatTok.Tok.setKind(tok::eof);
128 EOFInitialized = true;
129 }
130 return &FormatTok;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000131 }
132
133 UnwrappedLine &Line;
134 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000135 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000136 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000137 FormatTokenSource *PreviousTokenSource;
138
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000139 FormatToken *Token;
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000140 FormatToken *PreviousToken;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000141};
142
Craig Topper69665e12013-07-01 04:21:54 +0000143} // end anonymous namespace
144
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000145class ScopedLineState {
146public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000147 ScopedLineState(UnwrappedLineParser &Parser,
148 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000149 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000150 if (SwitchToPreprocessorLines)
151 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000152 else if (!Parser.Line->Tokens.empty())
153 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000154 PreBlockLine = std::move(Parser.Line);
155 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000156 Parser.Line->Level = PreBlockLine->Level;
157 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000158 }
159
160 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000161 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000162 Parser.addUnwrappedLine();
163 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000164 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000165 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000166 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
167 Parser.MustBreakBeforeNextToken = true;
168 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000169 }
170
171private:
172 UnwrappedLineParser &Parser;
173
David Blaikieefb6eb22014-08-09 20:02:07 +0000174 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000175 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000176};
177
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000178class CompoundStatementIndenter {
179public:
180 CompoundStatementIndenter(UnwrappedLineParser *Parser,
181 const FormatStyle &Style, unsigned &LineLevel)
182 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000183 if (Style.BraceWrapping.AfterControlStatement)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000184 Parser->addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000185 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000186 ++LineLevel;
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000187 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000188 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000189
190private:
191 unsigned &LineLevel;
192 unsigned OldLineLevel;
193};
194
Craig Topper69665e12013-07-01 04:21:54 +0000195namespace {
196
Manuel Klimekab419912013-05-23 09:41:43 +0000197class IndexedTokenSource : public FormatTokenSource {
198public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000199 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000200 : Tokens(Tokens), Position(-1) {}
201
Craig Topperfb6b25b2014-03-15 04:29:04 +0000202 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000203 ++Position;
204 return Tokens[Position];
205 }
206
Craig Topperfb6b25b2014-03-15 04:29:04 +0000207 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000208 assert(Position >= 0);
209 return Position;
210 }
211
Craig Topperfb6b25b2014-03-15 04:29:04 +0000212 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000213 Position = P;
214 return Tokens[Position];
215 }
216
Manuel Klimek71814b42013-10-11 21:25:45 +0000217 void reset() { Position = -1; }
218
Manuel Klimekab419912013-05-23 09:41:43 +0000219private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000220 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000221 int Position;
222};
223
Craig Topper69665e12013-07-01 04:21:54 +0000224} // end anonymous namespace
225
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000226UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000227 const AdditionalKeywords &Keywords,
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000228 unsigned FirstStartColumn,
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),
Krasimir Georgievad47c902017-08-30 14:34:57 +0000234 Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1),
235 IfNdefCondition(nullptr), FoundIncludeGuardStart(false),
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000236 IncludeGuardRejected(false), FirstStartColumn(FirstStartColumn) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000237
238void UnwrappedLineParser::reset() {
239 PPBranchLevel = -1;
Krasimir Georgievad47c902017-08-30 14:34:57 +0000240 IfNdefCondition = nullptr;
241 FoundIncludeGuardStart = false;
242 IncludeGuardRejected = false;
Manuel Klimek71814b42013-10-11 21:25:45 +0000243 Line.reset(new UnwrappedLine);
244 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000245 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000246 MustBreakBeforeNextToken = false;
247 PreprocessorDirectives.clear();
248 CurrentLines = &Lines;
249 DeclarationScopeStack.clear();
Manuel Klimek71814b42013-10-11 21:25:45 +0000250 PPStack.clear();
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000251 Line->FirstStartColumn = FirstStartColumn;
Manuel Klimek71814b42013-10-11 21:25:45 +0000252}
Daniel Jasperf7935112012-12-03 18:12:45 +0000253
Manuel Klimek20e0af62015-05-06 11:56:29 +0000254void UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000255 IndexedTokenSource TokenSource(AllTokens);
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +0000256 Line->FirstStartColumn = FirstStartColumn;
Manuel Klimek71814b42013-10-11 21:25:45 +0000257 do {
258 DEBUG(llvm::dbgs() << "----\n");
259 reset();
260 Tokens = &TokenSource;
261 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000262
Manuel Klimek71814b42013-10-11 21:25:45 +0000263 readToken();
264 parseFile();
265 // Create line with eof token.
266 pushToken(FormatTok);
267 addUnwrappedLine();
268
269 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
270 E = Lines.end();
271 I != E; ++I) {
272 Callback.consumeUnwrappedLine(*I);
273 }
274 Callback.finishRun();
275 Lines.clear();
276 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000277 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000278 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
279 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
280 }
281 if (!PPLevelBranchIndex.empty()) {
282 ++PPLevelBranchIndex.back();
283 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
284 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
285 }
286 } while (!PPLevelBranchIndex.empty());
Manuel Klimek1abf7892013-01-04 23:34:14 +0000287}
288
Manuel Klimek1a18c402013-04-12 14:13:36 +0000289void UnwrappedLineParser::parseFile() {
Daniel Jasper9326f912015-05-05 08:40:32 +0000290 // The top-level context in a file always has declarations, except for pre-
291 // processor directives and JavaScript files.
292 bool MustBeDeclaration =
293 !Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
294 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
295 MustBeDeclaration);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000296 if (Style.Language == FormatStyle::LK_TextProto)
297 parseBracedList();
298 else
299 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000300 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000301 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000302 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000303}
304
Manuel Klimek1a18c402013-04-12 14:13:36 +0000305void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000306 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000307 do {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000308 tok::TokenKind kind = FormatTok->Tok.getKind();
309 if (FormatTok->Type == TT_MacroBlockBegin) {
310 kind = tok::l_brace;
311 } else if (FormatTok->Type == TT_MacroBlockEnd) {
312 kind = tok::r_brace;
313 }
314
315 switch (kind) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000316 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000317 nextToken();
318 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000319 break;
320 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000321 // FIXME: Add parameter whether this can happen - if this happens, we must
322 // be in a non-declaration context.
Daniel Jasperb86e2722015-08-24 13:23:37 +0000323 if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
324 continue;
Nico Weber9096fc02013-06-26 00:30:14 +0000325 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000326 addUnwrappedLine();
327 break;
328 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000329 if (HasOpeningBrace)
330 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000331 nextToken();
332 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000333 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000334 case tok::kw_default:
335 case tok::kw_case:
Manuel Klimek89628f62017-09-20 09:51:03 +0000336 if (Style.Language == FormatStyle::LK_JavaScript &&
337 Line->MustBeDeclaration) {
Martin Probstf785fd92017-08-04 17:07:15 +0000338 // A 'case: string' style field declaration.
339 parseStructuralElement();
340 break;
341 }
Daniel Jasper72407622013-09-02 08:26:29 +0000342 if (!SwitchLabelEncountered &&
343 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
344 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000345 SwitchLabelEncountered = true;
346 parseStructuralElement();
347 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000348 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000349 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000350 break;
351 }
352 } while (!eof());
353}
354
Daniel Jasperadba2aa2015-05-18 12:52:00 +0000355void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
Manuel Klimekab419912013-05-23 09:41:43 +0000356 // We'll parse forward through the tokens until we hit
357 // a closing brace or eof - note that getNextToken() will
358 // parse macros, so this will magically work inside macro
359 // definitions, too.
360 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000361 FormatToken *Tok = FormatTok;
Manuel Klimek89628f62017-09-20 09:51:03 +0000362 const FormatToken *PrevTok = Tok->Previous;
Manuel Klimekab419912013-05-23 09:41:43 +0000363 // Keep a stack of positions of lbrace tokens. We will
364 // update information about whether an lbrace starts a
365 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000366 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000367 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000368 do {
Daniel Jaspereb65e912015-12-21 18:31:15 +0000369 // Get next non-comment token.
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000370 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000371 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000372 do {
373 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000374 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000375 } while (NextTok->is(tok::comment));
376
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000377 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000378 case tok::l_brace:
Martin Probst95ed8e72017-05-31 09:29:40 +0000379 if (Style.Language == FormatStyle::LK_JavaScript && PrevTok) {
380 if (PrevTok->is(tok::colon))
381 // A colon indicates this code is in a type, or a braced list
382 // following a label in an object literal ({a: {b: 1}}). The code
383 // below could be confused by semicolons between the individual
384 // members in a type member list, which would normally trigger
385 // BK_Block. In both cases, this must be parsed as an inline braced
386 // init.
387 Tok->BlockKind = BK_BracedInit;
388 else if (PrevTok->is(tok::r_paren))
389 // `) { }` can only occur in function or method declarations in JS.
390 Tok->BlockKind = BK_Block;
391 } else {
Daniel Jasperb9a49902016-01-09 15:56:28 +0000392 Tok->BlockKind = BK_Unknown;
Martin Probst95ed8e72017-05-31 09:29:40 +0000393 }
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000394 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000395 break;
396 case tok::r_brace:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000397 if (LBraceStack.empty())
398 break;
399 if (LBraceStack.back()->BlockKind == BK_Unknown) {
400 bool ProbablyBracedList = false;
401 if (Style.Language == FormatStyle::LK_Proto) {
402 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
403 } else {
404 // Using OriginalColumn to distinguish between ObjC methods and
405 // binary operators is a bit hacky.
406 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
407 NextTok->OriginalColumn == 0;
Daniel Jasper91b032a2014-05-22 12:46:38 +0000408
Daniel Jasperb9a49902016-01-09 15:56:28 +0000409 // If there is a comma, semicolon or right paren after the closing
410 // brace, we assume this is a braced initializer list. Note that
411 // regardless how we mark inner braces here, we will overwrite the
412 // BlockKind later if we parse a braced list (where all blocks
413 // inside are by default braced lists), or when we explicitly detect
414 // blocks (for example while parsing lambdas).
Martin Probst95ed8e72017-05-31 09:29:40 +0000415 // FIXME: Some of these do not apply to JS, e.g. "} {" can never be a
416 // braced list in JS.
Daniel Jasperb9a49902016-01-09 15:56:28 +0000417 ProbablyBracedList =
Daniel Jasperacffeb82016-03-05 18:34:26 +0000418 (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probste1e12a72016-08-19 14:35:01 +0000419 NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
420 Keywords.kw_as)) ||
Martin Probstb7fb2672017-05-10 13:53:29 +0000421 (Style.isCpp() && NextTok->is(tok::l_paren)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000422 NextTok->isOneOf(tok::comma, tok::period, tok::colon,
423 tok::r_paren, tok::r_square, tok::l_brace,
Martin Probstb7fb2672017-05-10 13:53:29 +0000424 tok::l_square, tok::ellipsis) ||
Daniel Jaspere4ada022016-12-13 10:05:03 +0000425 (NextTok->is(tok::identifier) &&
426 !PrevTok->isOneOf(tok::semi, tok::r_brace, tok::l_brace)) ||
Daniel Jasperb9a49902016-01-09 15:56:28 +0000427 (NextTok->is(tok::semi) &&
428 (!ExpectClassBody || LBraceStack.size() != 1)) ||
429 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Manuel Klimekab419912013-05-23 09:41:43 +0000430 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000431 if (ProbablyBracedList) {
432 Tok->BlockKind = BK_BracedInit;
433 LBraceStack.back()->BlockKind = BK_BracedInit;
434 } else {
435 Tok->BlockKind = BK_Block;
436 LBraceStack.back()->BlockKind = BK_Block;
437 }
Manuel Klimekab419912013-05-23 09:41:43 +0000438 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000439 LBraceStack.pop_back();
Manuel Klimekab419912013-05-23 09:41:43 +0000440 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000441 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000442 case tok::semi:
443 case tok::kw_if:
444 case tok::kw_while:
445 case tok::kw_for:
446 case tok::kw_switch:
447 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000448 case tok::kw___try:
Daniel Jasperb9a49902016-01-09 15:56:28 +0000449 if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000450 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000451 break;
452 default:
453 break;
454 }
Daniel Jasperb9a49902016-01-09 15:56:28 +0000455 PrevTok = Tok;
Manuel Klimekab419912013-05-23 09:41:43 +0000456 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000457 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Daniel Jasperb9a49902016-01-09 15:56:28 +0000458
Manuel Klimekab419912013-05-23 09:41:43 +0000459 // Assume other blocks for all unclosed opening braces.
460 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000461 if (LBraceStack[i]->BlockKind == BK_Unknown)
462 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000463 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000464
Manuel Klimekab419912013-05-23 09:41:43 +0000465 FormatTok = Tokens->setPosition(StoredPosition);
466}
467
Francois Ferranda98a95c2017-07-28 07:56:14 +0000468template <class T>
469static inline void hash_combine(std::size_t &seed, const T &v) {
470 std::hash<T> hasher;
471 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
472}
473
474size_t UnwrappedLineParser::computePPHash() const {
475 size_t h = 0;
476 for (const auto &i : PPStack) {
477 hash_combine(h, size_t(i.Kind));
478 hash_combine(h, i.Line);
479 }
480 return h;
481}
482
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000483void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
484 bool MunchSemi) {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000485 assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
486 "'{' or macro block token expected");
487 const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
Daniel Jaspereb65e912015-12-21 18:31:15 +0000488 FormatTok->BlockKind = BK_Block;
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000489
Francois Ferranda98a95c2017-07-28 07:56:14 +0000490 size_t PPStartHash = computePPHash();
491
Daniel Jasper516d7972013-07-25 11:31:57 +0000492 unsigned InitialLevel = Line->Level;
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000493 nextToken(/*LevelDifference=*/AddLevel ? 1 : 0);
Daniel Jasperf7935112012-12-03 18:12:45 +0000494
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000495 if (MacroBlock && FormatTok->is(tok::l_paren))
496 parseParens();
497
Francois Ferranda98a95c2017-07-28 07:56:14 +0000498 size_t NbPreprocessorDirectives =
499 CurrentLines == &Lines ? PreprocessorDirectives.size() : 0;
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000500 addUnwrappedLine();
Francois Ferranda98a95c2017-07-28 07:56:14 +0000501 size_t OpeningLineIndex =
502 CurrentLines->empty()
503 ? (UnwrappedLine::kInvalidIndex)
504 : (CurrentLines->size() - 1 - NbPreprocessorDirectives);
Daniel Jasperf7935112012-12-03 18:12:45 +0000505
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000506 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
507 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000508 if (AddLevel)
509 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000510 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000511
Marianne Mailhot-Sarrasin03137c62016-04-14 14:56:49 +0000512 if (eof())
513 return;
514
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000515 if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
516 : !FormatTok->is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000517 Line->Level = InitialLevel;
Daniel Jaspereb65e912015-12-21 18:31:15 +0000518 FormatTok->BlockKind = BK_Block;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000519 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000520 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000521
Francois Ferranda98a95c2017-07-28 07:56:14 +0000522 size_t PPEndHash = computePPHash();
523
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000524 // Munch the closing brace.
525 nextToken(/*LevelDifference=*/AddLevel ? -1 : 0);
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +0000526
527 if (MacroBlock && FormatTok->is(tok::l_paren))
528 parseParens();
529
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000530 if (MunchSemi && FormatTok->Tok.is(tok::semi))
531 nextToken();
Krasimir Georgiev3e051052017-07-24 14:51:59 +0000532 Line->Level = InitialLevel;
Francois Ferranda98a95c2017-07-28 07:56:14 +0000533
534 if (PPStartHash == PPEndHash) {
535 Line->MatchingOpeningBlockLineIndex = OpeningLineIndex;
536 if (OpeningLineIndex != UnwrappedLine::kInvalidIndex) {
537 // Update the opening line to add the forward reference as well
538 (*CurrentLines)[OpeningLineIndex].MatchingOpeningBlockLineIndex =
539 CurrentLines->size() - 1;
540 }
Francois Ferrande56a8292017-06-14 12:29:47 +0000541 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000542}
543
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000544static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000545 // FIXME: Closure-library specific stuff should not be hard-coded but be
546 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000547 if (Line.Tokens.size() < 4)
548 return false;
549 auto I = Line.Tokens.begin();
550 if (I->Tok->TokenText != "goog")
551 return false;
552 ++I;
553 if (I->Tok->isNot(tok::period))
554 return false;
555 ++I;
556 if (I->Tok->TokenText != "scope")
557 return false;
558 ++I;
559 return I->Tok->is(tok::l_paren);
560}
561
Martin Probst101ec892017-05-09 20:04:09 +0000562static bool isIIFE(const UnwrappedLine &Line,
563 const AdditionalKeywords &Keywords) {
564 // Look for the start of an immediately invoked anonymous function.
565 // https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
566 // This is commonly done in JavaScript to create a new, anonymous scope.
567 // Example: (function() { ... })()
568 if (Line.Tokens.size() < 3)
569 return false;
570 auto I = Line.Tokens.begin();
571 if (I->Tok->isNot(tok::l_paren))
572 return false;
573 ++I;
574 if (I->Tok->isNot(Keywords.kw_function))
575 return false;
576 ++I;
577 return I->Tok->is(tok::l_paren);
578}
579
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000580static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
581 const FormatToken &InitialToken) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000582 if (InitialToken.is(tok::kw_namespace))
583 return Style.BraceWrapping.AfterNamespace;
584 if (InitialToken.is(tok::kw_class))
585 return Style.BraceWrapping.AfterClass;
586 if (InitialToken.is(tok::kw_union))
587 return Style.BraceWrapping.AfterUnion;
588 if (InitialToken.is(tok::kw_struct))
589 return Style.BraceWrapping.AfterStruct;
590 return false;
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000591}
592
Manuel Klimek516e0542013-09-04 13:25:30 +0000593void UnwrappedLineParser::parseChildBlock() {
594 FormatTok->BlockKind = BK_Block;
595 nextToken();
596 {
Manuel Klimek89628f62017-09-20 09:51:03 +0000597 bool SkipIndent = (Style.Language == FormatStyle::LK_JavaScript &&
598 (isGoogScope(*Line) || isIIFE(*Line, Keywords)));
Manuel Klimek516e0542013-09-04 13:25:30 +0000599 ScopedLineState LineState(*this);
600 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
601 /*MustBeDeclaration=*/false);
Martin Probst101ec892017-05-09 20:04:09 +0000602 Line->Level += SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000603 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000604 flushComments(isOnNewLine(*FormatTok));
Martin Probst101ec892017-05-09 20:04:09 +0000605 Line->Level -= SkipIndent ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000606 }
607 nextToken();
608}
609
Daniel Jasperf7935112012-12-03 18:12:45 +0000610void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000611 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek20e0af62015-05-06 11:56:29 +0000612 ScopedMacroState MacroState(*Line, Tokens, FormatTok);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000613 nextToken();
614
Craig Topper2145bc02014-05-09 08:15:10 +0000615 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000616 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000617 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000618 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000619
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000620 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000621 case tok::pp_define:
622 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000623 return;
624 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000625 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000626 break;
627 case tok::pp_ifdef:
628 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000629 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000630 break;
631 case tok::pp_else:
632 parsePPElse();
633 break;
634 case tok::pp_elif:
635 parsePPElIf();
636 break;
637 case tok::pp_endif:
638 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000639 break;
640 default:
641 parsePPUnknown();
642 break;
643 }
644}
645
Manuel Klimek68b03042014-04-14 09:14:11 +0000646void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
Francois Ferranda98a95c2017-07-28 07:56:14 +0000647 size_t Line = CurrentLines->size();
648 if (CurrentLines == &PreprocessorDirectives)
649 Line += Lines.size();
650
651 if (Unreachable ||
652 (!PPStack.empty() && PPStack.back().Kind == PP_Unreachable))
653 PPStack.push_back({PP_Unreachable, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000654 else
Francois Ferranda98a95c2017-07-28 07:56:14 +0000655 PPStack.push_back({PP_Conditional, Line});
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000656}
657
Manuel Klimek68b03042014-04-14 09:14:11 +0000658void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000659 ++PPBranchLevel;
660 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
661 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
662 PPLevelBranchIndex.push_back(0);
663 PPLevelBranchCount.push_back(0);
664 }
665 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000666 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
667 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000668}
669
Manuel Klimek68b03042014-04-14 09:14:11 +0000670void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000671 if (!PPStack.empty())
672 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000673 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
674 if (!PPChainBranchIndex.empty())
675 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000676 conditionalCompilationCondition(
677 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
678 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000679}
680
Manuel Klimek68b03042014-04-14 09:14:11 +0000681void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000682 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
683 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
684 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000685 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
686 }
687 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000688 // Guard against #endif's without #if.
Krasimir Georgievad47c902017-08-30 14:34:57 +0000689 if (PPBranchLevel > -1)
Manuel Klimek14bd9172014-01-29 08:49:02 +0000690 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000691 if (!PPChainBranchIndex.empty())
692 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000693 if (!PPStack.empty())
694 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000695}
696
697void UnwrappedLineParser::parsePPIf(bool IfDef) {
Daniel Jasper62703eb2017-03-01 11:10:11 +0000698 bool IfNDef = FormatTok->is(tok::pp_ifndef);
Manuel Klimek68b03042014-04-14 09:14:11 +0000699 nextToken();
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000700 bool Unreachable = false;
701 if (!IfDef && (FormatTok->is(tok::kw_false) || FormatTok->TokenText == "0"))
702 Unreachable = true;
Daniel Jasper62703eb2017-03-01 11:10:11 +0000703 if (IfDef && !IfNDef && FormatTok->TokenText == "SWIG")
Daniel Jaspereab6cd42017-03-01 10:47:52 +0000704 Unreachable = true;
705 conditionalCompilationStart(Unreachable);
Krasimir Georgievad47c902017-08-30 14:34:57 +0000706 FormatToken *IfCondition = FormatTok;
707 // If there's a #ifndef on the first line, and the only lines before it are
708 // comments, it could be an include guard.
709 bool MaybeIncludeGuard = IfNDef;
710 if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard) {
711 for (auto &Line : Lines) {
712 if (!Line.Tokens.front().Tok->is(tok::comment)) {
713 MaybeIncludeGuard = false;
714 IncludeGuardRejected = true;
715 break;
716 }
717 }
718 }
719 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000720 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000721 ++PPBranchLevel;
722 if (!IncludeGuardRejected && !FoundIncludeGuardStart && MaybeIncludeGuard)
723 IfNdefCondition = IfCondition;
Manuel Klimek68b03042014-04-14 09:14:11 +0000724}
725
726void UnwrappedLineParser::parsePPElse() {
Krasimir Georgievad47c902017-08-30 14:34:57 +0000727 // If a potential include guard has an #else, it's not an include guard.
728 if (FoundIncludeGuardStart && PPBranchLevel == 0)
729 FoundIncludeGuardStart = false;
Manuel Klimek68b03042014-04-14 09:14:11 +0000730 conditionalCompilationAlternative();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000731 if (PPBranchLevel > -1)
732 --PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000733 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000734 ++PPBranchLevel;
Manuel Klimek68b03042014-04-14 09:14:11 +0000735}
736
737void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
738
739void UnwrappedLineParser::parsePPEndIf() {
740 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000741 parsePPUnknown();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000742 // If the #endif of a potential include guard is the last thing in the file,
743 // then we count it as a real include guard and subtract one from every
744 // preprocessor indent.
745 unsigned TokenPosition = Tokens->getPosition();
746 FormatToken *PeekNext = AllTokens[TokenPosition];
Daniel Jasper4df130f2017-09-04 13:33:52 +0000747 if (FoundIncludeGuardStart && PPBranchLevel == -1 && PeekNext->is(tok::eof) &&
748 Style.IndentPPDirectives != FormatStyle::PPDIS_None)
749 for (auto &Line : Lines)
Krasimir Georgievad47c902017-08-30 14:34:57 +0000750 if (Line.InPPDirective && Line.Level > 0)
751 --Line.Level;
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000752}
753
Manuel Klimek1abf7892013-01-04 23:34:14 +0000754void UnwrappedLineParser::parsePPDefine() {
755 nextToken();
756
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000757 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000758 parsePPUnknown();
759 return;
760 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000761 if (IfNdefCondition && IfNdefCondition->TokenText == FormatTok->TokenText) {
762 FoundIncludeGuardStart = true;
763 for (auto &Line : Lines) {
764 if (!Line.Tokens.front().Tok->isOneOf(tok::comment, tok::hash)) {
765 FoundIncludeGuardStart = false;
766 break;
767 }
768 }
769 }
770 IfNdefCondition = nullptr;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000771 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000772 if (FormatTok->Tok.getKind() == tok::l_paren &&
773 FormatTok->WhitespaceRange.getBegin() ==
774 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000775 parseParens();
776 }
Krasimir Georgievad47c902017-08-30 14:34:57 +0000777 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
778 Line->Level += PPBranchLevel + 1;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000779 addUnwrappedLine();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000780 ++Line->Level;
Manuel Klimek1b896292013-01-07 09:34:28 +0000781
782 // Errors during a preprocessor directive can only affect the layout of the
783 // preprocessor directive, and thus we ignore them. An alternative approach
784 // would be to use the same approach we use on the file level (no
785 // re-indentation if there was a structural error) within the macro
786 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000787 parseFile();
788}
789
790void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000791 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000792 nextToken();
793 } while (!eof());
Krasimir Georgievad47c902017-08-30 14:34:57 +0000794 if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash)
795 Line->Level += PPBranchLevel + 1;
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000796 addUnwrappedLine();
Krasimir Georgievad47c902017-08-30 14:34:57 +0000797 IfNdefCondition = nullptr;
Daniel Jasperf7935112012-12-03 18:12:45 +0000798}
799
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000800// Here we blacklist certain tokens that are not usually the first token in an
801// unwrapped line. This is used in attempt to distinguish macro calls without
802// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000803static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000804 // Semicolon can be a null-statement, l_square can be a start of a macro or
805 // a C++11 attribute, but this doesn't seem to be common.
806 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
807 Tok.isNot(tok::l_square) &&
808 // Tokens that can only be used as binary operators and a part of
809 // overloaded operator names.
810 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
811 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
812 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
813 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
814 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
815 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
816 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
817 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
818 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
819 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
820 Tok.isNot(tok::lesslessequal) &&
821 // Colon is used in labels, base class lists, initializer lists,
822 // range-based for loops, ternary operator, but should never be the
823 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000824 Tok.isNot(tok::colon) &&
825 // 'noexcept' is a trailing annotation.
826 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000827}
828
Martin Probst533965c2016-04-19 18:19:06 +0000829static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
830 const FormatToken *FormatTok) {
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000831 // FIXME: This returns true for C/C++ keywords like 'struct'.
832 return FormatTok->is(tok::identifier) &&
833 (FormatTok->Tok.getIdentifierInfo() == nullptr ||
Martin Probst3dbbefa2016-11-10 16:21:02 +0000834 !FormatTok->isOneOf(
835 Keywords.kw_in, Keywords.kw_of, Keywords.kw_as, Keywords.kw_async,
836 Keywords.kw_await, Keywords.kw_yield, Keywords.kw_finally,
837 Keywords.kw_function, Keywords.kw_import, Keywords.kw_is,
838 Keywords.kw_let, Keywords.kw_var, tok::kw_const,
839 Keywords.kw_abstract, Keywords.kw_extends, Keywords.kw_implements,
Manuel Klimek89628f62017-09-20 09:51:03 +0000840 Keywords.kw_instanceof, Keywords.kw_interface, Keywords.kw_throws,
841 Keywords.kw_from));
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000842}
843
Martin Probst533965c2016-04-19 18:19:06 +0000844static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
845 const FormatToken *FormatTok) {
Martin Probstb9316ff2016-09-18 17:21:52 +0000846 return FormatTok->Tok.isLiteral() ||
847 FormatTok->isOneOf(tok::kw_true, tok::kw_false) ||
848 mustBeJSIdent(Keywords, FormatTok);
Martin Probst533965c2016-04-19 18:19:06 +0000849}
850
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000851// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
852// when encountered after a value (see mustBeJSIdentOrValue).
853static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
854 const FormatToken *FormatTok) {
855 return FormatTok->isOneOf(
Martin Probst5f8445b2016-04-24 22:05:09 +0000856 tok::kw_return, Keywords.kw_yield,
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000857 // conditionals
858 tok::kw_if, tok::kw_else,
859 // loops
860 tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
861 // switch/case
862 tok::kw_switch, tok::kw_case,
863 // exceptions
864 tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
865 // declaration
866 tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
Martin Probst5f8445b2016-04-24 22:05:09 +0000867 Keywords.kw_async, Keywords.kw_function,
868 // import/export
869 Keywords.kw_import, tok::kw_export);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000870}
871
872// readTokenWithJavaScriptASI reads the next token and terminates the current
873// line if JavaScript Automatic Semicolon Insertion must
874// happen between the current token and the next token.
875//
876// This method is conservative - it cannot cover all edge cases of JavaScript,
877// but only aims to correctly handle certain well known cases. It *must not*
878// return true in speculative cases.
879void UnwrappedLineParser::readTokenWithJavaScriptASI() {
880 FormatToken *Previous = FormatTok;
881 readToken();
882 FormatToken *Next = FormatTok;
883
884 bool IsOnSameLine =
885 CommentsBeforeNextToken.empty()
886 ? Next->NewlinesBefore == 0
887 : CommentsBeforeNextToken.front()->NewlinesBefore == 0;
888 if (IsOnSameLine)
889 return;
890
891 bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
Martin Probst717f6dc2016-10-21 05:11:38 +0000892 bool PreviousStartsTemplateExpr =
893 Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${");
Martin Probstbbffeac2016-04-11 07:35:57 +0000894 if (PreviousMustBeValue && Line && Line->Tokens.size() > 1) {
895 // If the token before the previous one is an '@', the previous token is an
896 // annotation and can precede another identifier/value.
Benjamin Kramer5ffc24e2016-04-11 12:19:19 +0000897 const FormatToken *PrePrevious = std::prev(Line->Tokens.end(), 2)->Tok;
Martin Probstbbffeac2016-04-11 07:35:57 +0000898 if (PrePrevious->is(tok::at))
899 return;
900 }
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000901 if (Next->is(tok::exclaim) && PreviousMustBeValue)
Martin Probstd40bca42017-01-09 08:56:36 +0000902 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000903 bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
Martin Probst717f6dc2016-10-21 05:11:38 +0000904 bool NextEndsTemplateExpr =
905 Next->is(TT_TemplateString) && Next->TokenText.startswith("}");
906 if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr &&
907 (PreviousMustBeValue ||
908 Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus,
909 tok::minusminus)))
Martin Probstd40bca42017-01-09 08:56:36 +0000910 return addUnwrappedLine();
Martin Probst0a19d432017-08-09 15:19:16 +0000911 if ((PreviousMustBeValue || Previous->is(tok::r_paren)) &&
912 isJSDeclOrStmt(Keywords, Next))
Martin Probstd40bca42017-01-09 08:56:36 +0000913 return addUnwrappedLine();
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +0000914}
915
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000916void UnwrappedLineParser::parseStructuralElement() {
Daniel Jasper498f5582015-12-25 08:53:31 +0000917 assert(!FormatTok->is(tok::l_brace));
918 if (Style.Language == FormatStyle::LK_TableGen &&
919 FormatTok->is(tok::pp_include)) {
920 nextToken();
921 if (FormatTok->is(tok::string_literal))
922 nextToken();
923 addUnwrappedLine();
924 return;
925 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000926 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000927 case tok::at:
928 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000929 if (FormatTok->Tok.is(tok::l_brace)) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +0000930 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +0000931 parseBracedList();
932 break;
933 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000934 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000935 case tok::objc_public:
936 case tok::objc_protected:
937 case tok::objc_package:
938 case tok::objc_private:
939 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000940 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000941 case tok::objc_implementation:
942 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000943 case tok::objc_protocol:
944 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000945 case tok::objc_end:
946 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000947 case tok::objc_optional:
948 case tok::objc_required:
949 nextToken();
950 addUnwrappedLine();
951 return;
Nico Weber45c48122015-06-28 01:06:16 +0000952 case tok::objc_autoreleasepool:
953 nextToken();
954 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +0000955 if (Style.BraceWrapping.AfterObjCDeclaration)
Nico Weber45c48122015-06-28 01:06:16 +0000956 addUnwrappedLine();
957 parseBlock(/*MustBeDeclaration=*/false);
958 }
959 addUnwrappedLine();
960 return;
Nico Weber33381f52015-02-07 01:57:32 +0000961 case tok::objc_try:
962 // This branch isn't strictly necessary (the kw_try case below would
963 // do this too after the tok::at is parsed above). But be explicit.
964 parseTryCatch();
965 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000966 default:
967 break;
968 }
969 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000970 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000971 nextToken();
972 if (FormatTok->is(tok::l_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000973 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper2337f282015-01-12 10:14:56 +0000974 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000975 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000976 if (FormatTok->is(tok::r_brace)) {
Daniel Jasperc6366072015-05-10 08:42:04 +0000977 FormatTok->Type = TT_InlineASMBrace;
Daniel Jasper8f463652014-08-26 23:15:12 +0000978 nextToken();
Daniel Jasper790d4f92015-05-11 11:59:46 +0000979 addUnwrappedLine();
Daniel Jasper8f463652014-08-26 23:15:12 +0000980 break;
981 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000982 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000983 nextToken();
984 }
985 }
986 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000987 case tok::kw_namespace:
988 parseNamespace();
989 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000990 case tok::kw_inline:
991 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000992 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000993 parseNamespace();
994 return;
995 }
996 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000997 case tok::kw_public:
998 case tok::kw_protected:
999 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +00001000 if (Style.Language == FormatStyle::LK_Java ||
1001 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001002 nextToken();
1003 else
1004 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +00001005 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001006 case tok::kw_if:
1007 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +00001008 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001009 case tok::kw_for:
1010 case tok::kw_while:
1011 parseForOrWhileLoop();
1012 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001013 case tok::kw_do:
1014 parseDoWhile();
1015 return;
1016 case tok::kw_switch:
Martin Probstf785fd92017-08-04 17:07:15 +00001017 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1018 // 'switch: string' field declaration.
1019 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001020 parseSwitch();
1021 return;
1022 case tok::kw_default:
Martin Probstf785fd92017-08-04 17:07:15 +00001023 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1024 // 'default: string' field declaration.
1025 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001026 nextToken();
1027 parseLabel();
1028 return;
1029 case tok::kw_case:
Martin Probstf785fd92017-08-04 17:07:15 +00001030 if (Style.Language == FormatStyle::LK_JavaScript && Line->MustBeDeclaration)
1031 // 'case: string' field declaration.
1032 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001033 parseCaseLabel();
1034 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001035 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +00001036 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +00001037 parseTryCatch();
1038 return;
Manuel Klimekae610d12013-01-21 14:32:05 +00001039 case tok::kw_extern:
1040 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001041 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +00001042 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001043 if (FormatTok->Tok.is(tok::l_brace)) {
Krasimir Georgievd6ce9372017-09-15 11:23:50 +00001044 if (Style.BraceWrapping.AfterExternBlock) {
1045 addUnwrappedLine();
1046 parseBlock(/*MustBeDeclaration=*/true);
1047 } else {
1048 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
1049 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001050 addUnwrappedLine();
1051 return;
1052 }
1053 }
Daniel Jaspere1e43192014-04-01 12:55:11 +00001054 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +00001055 case tok::kw_export:
1056 if (Style.Language == FormatStyle::LK_JavaScript) {
1057 parseJavaScriptEs6ImportExport();
1058 return;
1059 }
1060 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +00001061 case tok::identifier:
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001062 if (FormatTok->is(TT_ForEachMacro)) {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001063 parseForOrWhileLoop();
1064 return;
1065 }
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001066 if (FormatTok->is(TT_MacroBlockBegin)) {
1067 parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
1068 /*MunchSemi=*/false);
1069 return;
1070 }
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001071 if (FormatTok->is(Keywords.kw_import)) {
1072 if (Style.Language == FormatStyle::LK_JavaScript) {
1073 parseJavaScriptEs6ImportExport();
1074 return;
1075 }
1076 if (Style.Language == FormatStyle::LK_Proto) {
1077 nextToken();
Daniel Jasper8b61d142016-06-20 20:39:53 +00001078 if (FormatTok->is(tok::kw_public))
1079 nextToken();
Daniel Jasper3d5a7d62016-06-20 18:20:38 +00001080 if (!FormatTok->is(tok::string_literal))
1081 return;
1082 nextToken();
1083 if (FormatTok->is(tok::semi))
1084 nextToken();
1085 addUnwrappedLine();
1086 return;
1087 }
Daniel Jasper354aa512015-02-19 16:07:32 +00001088 }
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001089 if (Style.isCpp() &&
Daniel Jasper72b33572017-03-31 12:04:37 +00001090 FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
Daniel Jaspera00de632015-12-01 12:05:04 +00001091 Keywords.kw_slots, Keywords.kw_qslots)) {
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001092 nextToken();
1093 if (FormatTok->is(tok::colon)) {
1094 nextToken();
1095 addUnwrappedLine();
Daniel Jasper31343832016-07-27 10:13:24 +00001096 return;
Daniel Jasperde0d1f32015-04-24 07:50:34 +00001097 }
Daniel Jasper53395402015-04-07 15:04:40 +00001098 }
Manuel Klimekae610d12013-01-21 14:32:05 +00001099 // In all other cases, parse the declaration.
1100 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001101 default:
1102 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001103 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001104 do {
Manuel Klimeke411aa82017-09-20 09:29:37 +00001105 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001106 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +00001107 case tok::at:
1108 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001109 if (FormatTok->Tok.is(tok::l_brace)) {
1110 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001111 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001112 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001113 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001114 case tok::kw_enum:
Daniel Jaspera7900ad2016-05-08 18:12:22 +00001115 // Ignore if this is part of "template <enum ...".
1116 if (Previous && Previous->is(tok::less)) {
1117 nextToken();
1118 break;
1119 }
1120
Daniel Jasper90cf3802015-06-17 09:44:02 +00001121 // parseEnum falls through and does not yet add an unwrapped line as an
1122 // enum definition can start a structural element.
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001123 if (!parseEnum())
1124 break;
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001125 // This only applies for C++.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001126 if (!Style.isCpp()) {
Daniel Jasper90cf3802015-06-17 09:44:02 +00001127 addUnwrappedLine();
1128 return;
1129 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001130 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001131 case tok::kw_typedef:
1132 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +00001133 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
1134 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +00001135 parseEnum();
1136 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001137 case tok::kw_struct:
1138 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +00001139 case tok::kw_class:
Daniel Jasper910807d2015-06-12 04:52:02 +00001140 // parseRecord falls through and does not yet add an unwrapped line as a
1141 // record declaration or definition can start a structural element.
Manuel Klimeke01bab52013-01-15 13:38:33 +00001142 parseRecord();
Daniel Jasper910807d2015-06-12 04:52:02 +00001143 // This does not apply for Java and JavaScript.
1144 if (Style.Language == FormatStyle::LK_Java ||
1145 Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperd5ec65b2016-01-08 07:06:07 +00001146 if (FormatTok->is(tok::semi))
1147 nextToken();
Daniel Jasper910807d2015-06-12 04:52:02 +00001148 addUnwrappedLine();
1149 return;
1150 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00001151 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +00001152 case tok::period:
1153 nextToken();
1154 // In Java, classes have an implicit static member "class".
1155 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
1156 FormatTok->is(tok::kw_class))
1157 nextToken();
Daniel Jasperba52fcb2015-09-28 14:29:45 +00001158 if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
1159 FormatTok->Tok.getIdentifierInfo())
1160 // JavaScript only has pseudo keywords, all keywords are allowed to
1161 // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
1162 nextToken();
Daniel Jaspere5d74862014-11-26 08:17:08 +00001163 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001164 case tok::semi:
1165 nextToken();
1166 addUnwrappedLine();
1167 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +00001168 case tok::r_brace:
1169 addUnwrappedLine();
1170 return;
Daniel Jasperf7935112012-12-03 18:12:45 +00001171 case tok::l_paren:
1172 parseParens();
1173 break;
Daniel Jasper5af04a42015-10-07 03:43:10 +00001174 case tok::kw_operator:
1175 nextToken();
1176 if (FormatTok->isBinaryOperator())
1177 nextToken();
1178 break;
Manuel Klimek516e0542013-09-04 13:25:30 +00001179 case tok::caret:
1180 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +00001181 if (FormatTok->Tok.isAnyIdentifier() ||
1182 FormatTok->isSimpleTypeSpecifier())
1183 nextToken();
1184 if (FormatTok->is(tok::l_paren))
1185 parseParens();
1186 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +00001187 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +00001188 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001189 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001190 if (!tryToParseBracedList()) {
1191 // A block outside of parentheses must be the last part of a
1192 // structural element.
1193 // FIXME: Figure out cases where this is not true, and add projections
1194 // for them (the one we know is missing are lambdas).
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001195 if (Style.BraceWrapping.AfterFunction)
Manuel Klimekab419912013-05-23 09:41:43 +00001196 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +00001197 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +00001198 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001199 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +00001200 return;
1201 }
1202 // Otherwise this was a braced init list, and the structural
1203 // element continues.
1204 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001205 case tok::kw_try:
1206 // We arrive here when parsing function-try blocks.
1207 parseTryCatch();
1208 return;
Daniel Jasper40e19212013-05-29 13:16:10 +00001209 case tok::identifier: {
Birunthan Mohanathasb001a0b2015-07-03 17:25:16 +00001210 if (FormatTok->is(TT_MacroBlockEnd)) {
1211 addUnwrappedLine();
1212 return;
1213 }
1214
Martin Probst973ff792017-04-27 13:07:24 +00001215 // Function declarations (as opposed to function expressions) are parsed
1216 // on their own unwrapped line by continuing this loop. Function
1217 // expressions (functions that are not on their own line) must not create
1218 // a new unwrapped line, so they are special cased below.
1219 size_t TokenCount = Line->Tokens.size();
Daniel Jasper9326f912015-05-05 08:40:32 +00001220 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst973ff792017-04-27 13:07:24 +00001221 FormatTok->is(Keywords.kw_function) &&
1222 (TokenCount > 1 || (TokenCount == 1 && !Line->Tokens.front().Tok->is(
1223 Keywords.kw_async)))) {
Daniel Jasper069e5f42014-05-20 11:14:57 +00001224 tryToParseJSFunction();
1225 break;
1226 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001227 if ((Style.Language == FormatStyle::LK_JavaScript ||
1228 Style.Language == FormatStyle::LK_Java) &&
1229 FormatTok->is(Keywords.kw_interface)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001230 if (Style.Language == FormatStyle::LK_JavaScript) {
1231 // In JavaScript/TypeScript, "interface" can be used as a standalone
1232 // identifier, e.g. in `var interface = 1;`. If "interface" is
1233 // followed by another identifier, it is very like to be an actual
1234 // interface declaration.
1235 unsigned StoredPosition = Tokens->getPosition();
1236 FormatToken *Next = Tokens->getNextToken();
1237 FormatTok = Tokens->setPosition(StoredPosition);
Martin Probst533965c2016-04-19 18:19:06 +00001238 if (Next && !mustBeJSIdent(Keywords, Next)) {
Martin Probst1e8261e2016-04-19 18:18:59 +00001239 nextToken();
1240 break;
1241 }
1242 }
Daniel Jasper9326f912015-05-05 08:40:32 +00001243 parseRecord();
Daniel Jasper259188b2015-06-12 04:56:34 +00001244 addUnwrappedLine();
Daniel Jasper5c235c02015-07-06 14:26:04 +00001245 return;
Daniel Jasper9326f912015-05-05 08:40:32 +00001246 }
1247
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00001248 // See if the following token should start a new unwrapped line.
Daniel Jasper9326f912015-05-05 08:40:32 +00001249 StringRef Text = FormatTok->TokenText;
Daniel Jasperf7935112012-12-03 18:12:45 +00001250 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +00001251 if (Line->Tokens.size() == 1 &&
1252 // JS doesn't have macros, and within classes colons indicate fields,
1253 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +00001254 Style.Language != FormatStyle::LK_JavaScript) {
1255 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Daniel Jasper40609472016-04-06 15:02:46 +00001256 Line->Tokens.begin()->Tok->MustBreakBefore = true;
Alexander Kornienkode644272013-04-08 22:16:06 +00001257 parseLabel();
1258 return;
1259 }
Daniel Jasper680b09b2014-11-05 10:48:04 +00001260 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +00001261 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +00001262 bool FunctionLike = FormatTok->is(tok::l_paren);
1263 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +00001264 parseParens();
Daniel Jaspere60cba12015-05-13 11:35:53 +00001265
1266 bool FollowedByNewline =
1267 CommentsBeforeNextToken.empty()
1268 ? FormatTok->NewlinesBefore > 0
1269 : CommentsBeforeNextToken.front()->NewlinesBefore > 0;
1270
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001271 if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
Daniel Jasper680b09b2014-11-05 10:48:04 +00001272 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +00001273 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +00001274 return;
Alexander Kornienkode644272013-04-08 22:16:06 +00001275 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001276 }
1277 break;
Daniel Jasper40e19212013-05-29 13:16:10 +00001278 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001279 case tok::equal:
Manuel Klimek79e06082015-05-21 12:23:34 +00001280 // Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
1281 // TT_JsFatArrow. The always start an expression or a child block if
1282 // followed by a curly.
1283 if (FormatTok->is(TT_JsFatArrow)) {
1284 nextToken();
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001285 if (FormatTok->is(tok::l_brace))
Manuel Klimek79e06082015-05-21 12:23:34 +00001286 parseChildBlock();
Manuel Klimek79e06082015-05-21 12:23:34 +00001287 break;
1288 }
1289
Daniel Jaspere25509f2012-12-17 11:29:41 +00001290 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001291 if (FormatTok->Tok.is(tok::l_brace)) {
1292 nextToken();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001293 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001294 } else if (Style.Language == FormatStyle::LK_Proto &&
Manuel Klimek89628f62017-09-20 09:51:03 +00001295 FormatTok->Tok.is(tok::less)) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001296 nextToken();
Krasimir Georgiev0b41fcb2017-06-27 13:58:41 +00001297 parseBracedList(/*ContinueOnSemicolons=*/false,
1298 /*ClosingBraceKind=*/tok::greater);
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001299 }
Daniel Jaspere25509f2012-12-17 11:29:41 +00001300 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001301 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001302 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +00001303 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +00001304 case tok::kw_new:
1305 parseNew();
1306 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001307 default:
1308 nextToken();
1309 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001310 }
1311 } while (!eof());
1312}
1313
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001314bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001315 if (!Style.isCpp()) {
Daniel Jasper1feab0f2015-06-02 15:31:37 +00001316 nextToken();
1317 return false;
1318 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001319 assert(FormatTok->is(tok::l_square));
1320 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001321 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001322 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001323
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001324 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001325 if (FormatTok->isSimpleTypeSpecifier()) {
1326 nextToken();
1327 continue;
1328 }
Manuel Klimekffdeb592013-09-03 15:10:01 +00001329 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001330 case tok::l_brace:
1331 break;
1332 case tok::l_paren:
1333 parseParens();
1334 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +00001335 case tok::amp:
1336 case tok::star:
1337 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +00001338 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001339 case tok::less:
1340 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001341 case tok::identifier:
Daniel Jasper5eaa0092015-08-13 13:37:08 +00001342 case tok::numeric_constant:
Daniel Jasper1067ab02014-02-11 10:16:55 +00001343 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001344 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +00001345 nextToken();
1346 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +00001347 case tok::arrow:
Daniel Jasper6f2b88a2015-06-05 13:18:09 +00001348 FormatTok->Type = TT_LambdaArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001349 nextToken();
1350 break;
1351 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001352 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001353 }
1354 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001355 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +00001356 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001357 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001358}
1359
1360bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
Manuel Klimek89628f62017-09-20 09:51:03 +00001361 const FormatToken *Previous = FormatTok->Previous;
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001362 if (Previous &&
1363 (Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
1364 tok::kw_delete) ||
Manuel Klimek89628f62017-09-20 09:51:03 +00001365 FormatTok->isCppStructuredBinding(Style) || Previous->closesScope() ||
1366 Previous->isSimpleTypeSpecifier())) {
Manuel Klimekffdeb592013-09-03 15:10:01 +00001367 nextToken();
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001368 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001369 }
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001370 nextToken();
1371 parseSquare(/*LambdaIntroducer=*/true);
1372 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +00001373}
1374
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001375void UnwrappedLineParser::tryToParseJSFunction() {
Martin Probst409697e2016-05-29 14:41:07 +00001376 assert(FormatTok->is(Keywords.kw_function) ||
1377 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
Martin Probst5f8445b2016-04-24 22:05:09 +00001378 if (FormatTok->is(Keywords.kw_async))
1379 nextToken();
1380 // Consume "function".
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001381 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001382
Daniel Jasper71e50af2016-11-01 06:22:59 +00001383 // Consume * (generator function). Treat it like C++'s overloaded operators.
1384 if (FormatTok->is(tok::star)) {
1385 FormatTok->Type = TT_OverloadedOperator;
Martin Probst5f8445b2016-04-24 22:05:09 +00001386 nextToken();
Daniel Jasper71e50af2016-11-01 06:22:59 +00001387 }
Martin Probst5f8445b2016-04-24 22:05:09 +00001388
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001389 // Consume function name.
1390 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +00001391 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +00001392
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001393 if (FormatTok->isNot(tok::l_paren))
1394 return;
Manuel Klimek79e06082015-05-21 12:23:34 +00001395
1396 // Parse formal parameter list.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001397 parseParens();
Manuel Klimek79e06082015-05-21 12:23:34 +00001398
1399 if (FormatTok->is(tok::colon)) {
1400 // Parse a type definition.
1401 nextToken();
1402
1403 // Eat the type declaration. For braced inline object types, balance braces,
1404 // otherwise just parse until finding an l_brace for the function body.
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001405 if (FormatTok->is(tok::l_brace))
1406 tryToParseBracedList();
1407 else
Martin Probstaf16c502017-01-04 13:36:43 +00001408 while (!FormatTok->isOneOf(tok::l_brace, tok::semi) && !eof())
Manuel Klimek79e06082015-05-21 12:23:34 +00001409 nextToken();
Manuel Klimek79e06082015-05-21 12:23:34 +00001410 }
1411
Martin Probstaf16c502017-01-04 13:36:43 +00001412 if (FormatTok->is(tok::semi))
1413 return;
1414
Manuel Klimek79e06082015-05-21 12:23:34 +00001415 parseChildBlock();
1416}
1417
Daniel Jasper3c883d12015-05-18 14:49:19 +00001418bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001419 if (FormatTok->BlockKind == BK_Unknown)
Daniel Jasper3c883d12015-05-18 14:49:19 +00001420 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001421 assert(FormatTok->BlockKind != BK_Unknown);
1422 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001423 return false;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001424 nextToken();
Manuel Klimekab419912013-05-23 09:41:43 +00001425 parseBracedList();
1426 return true;
1427}
1428
Krasimir Georgievff747be2017-06-27 13:43:07 +00001429bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
1430 tok::TokenKind ClosingBraceKind) {
Daniel Jasper015ed022013-09-13 09:20:45 +00001431 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001432
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001433 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1434 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001435 do {
Manuel Klimek79e06082015-05-21 12:23:34 +00001436 if (Style.Language == FormatStyle::LK_JavaScript) {
Martin Probst409697e2016-05-29 14:41:07 +00001437 if (FormatTok->is(Keywords.kw_function) ||
1438 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001439 tryToParseJSFunction();
1440 continue;
Daniel Jasperbe520bd2015-05-31 08:51:54 +00001441 }
1442 if (FormatTok->is(TT_JsFatArrow)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001443 nextToken();
1444 // Fat arrows can be followed by simple expressions or by child blocks
1445 // in curly braces.
Daniel Jaspere6fcf7d2015-06-17 13:08:06 +00001446 if (FormatTok->is(tok::l_brace)) {
Manuel Klimek79e06082015-05-21 12:23:34 +00001447 parseChildBlock();
1448 continue;
1449 }
1450 }
Martin Probst8e3eba02017-02-07 16:33:13 +00001451 if (FormatTok->is(tok::l_brace)) {
1452 // Could be a method inside of a braced list `{a() { return 1; }}`.
1453 if (tryToParseBracedList())
1454 continue;
1455 parseChildBlock();
1456 }
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001457 }
Krasimir Georgievff747be2017-06-27 13:43:07 +00001458 if (FormatTok->Tok.getKind() == ClosingBraceKind) {
1459 nextToken();
1460 return !HasError;
1461 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001462 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001463 case tok::caret:
1464 nextToken();
1465 if (FormatTok->is(tok::l_brace)) {
1466 parseChildBlock();
1467 }
1468 break;
1469 case tok::l_square:
1470 tryToParseLambda();
1471 break;
Daniel Jaspera87af7a2015-06-30 11:32:22 +00001472 case tok::l_paren:
1473 parseParens();
Daniel Jasperf46dec82015-03-31 14:34:15 +00001474 // JavaScript can just have free standing methods and getters/setters in
1475 // object literals. Detect them by a "{" following ")".
1476 if (Style.Language == FormatStyle::LK_JavaScript) {
Daniel Jasperf46dec82015-03-31 14:34:15 +00001477 if (FormatTok->is(tok::l_brace))
1478 parseChildBlock();
1479 break;
1480 }
Daniel Jasperf46dec82015-03-31 14:34:15 +00001481 break;
Martin Probst8e3eba02017-02-07 16:33:13 +00001482 case tok::l_brace:
1483 // Assume there are no blocks inside a braced init list apart
1484 // from the ones we explicitly parse out (like lambdas).
1485 FormatTok->BlockKind = BK_BracedInit;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001486 nextToken();
Martin Probst8e3eba02017-02-07 16:33:13 +00001487 parseBracedList();
1488 break;
Krasimir Georgievfa4dbb62017-08-03 13:43:45 +00001489 case tok::less:
1490 if (Style.Language == FormatStyle::LK_Proto) {
1491 nextToken();
1492 parseBracedList(/*ContinueOnSemicolons=*/false,
1493 /*ClosingBraceKind=*/tok::greater);
1494 } else {
1495 nextToken();
1496 }
1497 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001498 case tok::semi:
Daniel Jasperb9a49902016-01-09 15:56:28 +00001499 // JavaScript (or more precisely TypeScript) can have semicolons in braced
1500 // lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
1501 // used for error recovery if we have otherwise determined that this is
1502 // a braced list.
1503 if (Style.Language == FormatStyle::LK_JavaScript) {
1504 nextToken();
1505 break;
1506 }
Daniel Jasper015ed022013-09-13 09:20:45 +00001507 HasError = true;
1508 if (!ContinueOnSemicolons)
1509 return !HasError;
1510 nextToken();
1511 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001512 case tok::comma:
1513 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001514 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001515 default:
1516 nextToken();
1517 break;
1518 }
1519 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001520 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001521}
1522
Daniel Jasperf7935112012-12-03 18:12:45 +00001523void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001524 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001525 nextToken();
1526 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001527 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001528 case tok::l_paren:
1529 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001530 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1531 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001532 break;
1533 case tok::r_paren:
1534 nextToken();
1535 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001536 case tok::r_brace:
1537 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1538 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001539 case tok::l_square:
1540 tryToParseLambda();
1541 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001542 case tok::l_brace:
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001543 if (!tryToParseBracedList())
Manuel Klimekf017dc02013-09-04 13:34:14 +00001544 parseChildBlock();
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001545 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001546 case tok::at:
1547 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001548 if (FormatTok->Tok.is(tok::l_brace)) {
1549 nextToken();
Nico Weber372d8dc2013-02-10 20:35:35 +00001550 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001551 }
Nico Weber372d8dc2013-02-10 20:35:35 +00001552 break;
Martin Probst1027fb82017-02-07 14:05:30 +00001553 case tok::kw_class:
1554 if (Style.Language == FormatStyle::LK_JavaScript)
1555 parseRecord(/*ParseAsExpr=*/true);
1556 else
1557 nextToken();
1558 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001559 case tok::identifier:
1560 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probst409697e2016-05-29 14:41:07 +00001561 (FormatTok->is(Keywords.kw_function) ||
1562 FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001563 tryToParseJSFunction();
1564 else
1565 nextToken();
1566 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001567 default:
1568 nextToken();
1569 break;
1570 }
1571 } while (!eof());
1572}
1573
Manuel Klimek9f0a4e52017-09-19 09:59:30 +00001574void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) {
1575 if (!LambdaIntroducer) {
1576 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1577 if (tryToParseLambda())
1578 return;
1579 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001580 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001581 switch (FormatTok->Tok.getKind()) {
1582 case tok::l_paren:
1583 parseParens();
1584 break;
1585 case tok::r_square:
1586 nextToken();
1587 return;
1588 case tok::r_brace:
1589 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1590 return;
1591 case tok::l_square:
1592 parseSquare();
1593 break;
1594 case tok::l_brace: {
Daniel Jasperadba2aa2015-05-18 12:52:00 +00001595 if (!tryToParseBracedList())
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001596 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001597 break;
1598 }
1599 case tok::at:
1600 nextToken();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001601 if (FormatTok->Tok.is(tok::l_brace)) {
1602 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001603 parseBracedList();
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001604 }
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001605 break;
1606 default:
1607 nextToken();
1608 break;
1609 }
1610 } while (!eof());
1611}
1612
Daniel Jasperf7935112012-12-03 18:12:45 +00001613void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001614 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001615 nextToken();
Daniel Jasper6a7d5a72017-06-19 07:40:49 +00001616 if (FormatTok->Tok.is(tok::kw_constexpr))
1617 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001618 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001619 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001620 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001621 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001622 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001623 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001624 if (Style.BraceWrapping.BeforeElse)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001625 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001626 else
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001627 NeedsUnwrappedLine = true;
Daniel Jasperf7935112012-12-03 18:12:45 +00001628 } else {
1629 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001630 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001631 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001632 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001633 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001634 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001635 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001636 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001637 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001638 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001639 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001640 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001641 parseIfThenElse();
1642 } else {
1643 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001644 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001645 parseStructuralElement();
Daniel Jasper451544a2016-05-19 06:30:48 +00001646 if (FormatTok->is(tok::eof))
1647 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001648 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001649 }
1650 } else if (NeedsUnwrappedLine) {
1651 addUnwrappedLine();
1652 }
1653}
1654
Daniel Jasper04a71a42014-05-08 11:58:24 +00001655void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001656 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001657 nextToken();
1658 bool NeedsUnwrappedLine = false;
1659 if (FormatTok->is(tok::colon)) {
1660 // We are in a function try block, what comes is an initializer list.
1661 nextToken();
1662 while (FormatTok->is(tok::identifier)) {
1663 nextToken();
1664 if (FormatTok->is(tok::l_paren))
1665 parseParens();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001666 if (FormatTok->is(tok::comma))
1667 nextToken();
1668 }
1669 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001670 // Parse try with resource.
1671 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1672 parseParens();
1673 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001674 if (FormatTok->is(tok::l_brace)) {
1675 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1676 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001677 if (Style.BraceWrapping.BeforeCatch) {
Daniel Jasper04a71a42014-05-08 11:58:24 +00001678 addUnwrappedLine();
1679 } else {
1680 NeedsUnwrappedLine = true;
1681 }
1682 } else if (!FormatTok->is(tok::kw_catch)) {
1683 // The C++ standard requires a compound-statement after a try.
1684 // If there's none, we try to assume there's a structuralElement
1685 // and try to continue.
Daniel Jasper04a71a42014-05-08 11:58:24 +00001686 addUnwrappedLine();
1687 ++Line->Level;
1688 parseStructuralElement();
1689 --Line->Level;
1690 }
Nico Weber33381f52015-02-07 01:57:32 +00001691 while (1) {
1692 if (FormatTok->is(tok::at))
1693 nextToken();
1694 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1695 tok::kw___finally) ||
1696 ((Style.Language == FormatStyle::LK_Java ||
1697 Style.Language == FormatStyle::LK_JavaScript) &&
1698 FormatTok->is(Keywords.kw_finally)) ||
1699 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1700 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1701 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001702 nextToken();
1703 while (FormatTok->isNot(tok::l_brace)) {
1704 if (FormatTok->is(tok::l_paren)) {
1705 parseParens();
1706 continue;
1707 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001708 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001709 return;
1710 nextToken();
1711 }
1712 NeedsUnwrappedLine = false;
1713 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1714 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001715 if (Style.BraceWrapping.BeforeCatch)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001716 addUnwrappedLine();
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001717 else
Daniel Jasper04a71a42014-05-08 11:58:24 +00001718 NeedsUnwrappedLine = true;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001719 }
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001720 if (NeedsUnwrappedLine)
Daniel Jasper04a71a42014-05-08 11:58:24 +00001721 addUnwrappedLine();
Daniel Jasper04a71a42014-05-08 11:58:24 +00001722}
1723
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001724void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001725 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001726
1727 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001728 nextToken();
Saleem Abdulrasool328085f2015-10-30 05:07:56 +00001729 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001730 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001731 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001732 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001733 addUnwrappedLine();
1734
Daniel Jasper65ee3472013-07-31 23:16:02 +00001735 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1736 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1737 DeclarationScopeStack.size() > 1);
1738 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001739 // Munch the semicolon after a namespace. This is more common than one would
1740 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001741 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001742 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001743 addUnwrappedLine();
1744 }
1745 // FIXME: Add error handling.
1746}
1747
Daniel Jasper6acf5132015-03-12 14:44:29 +00001748void UnwrappedLineParser::parseNew() {
1749 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1750 nextToken();
1751 if (Style.Language != FormatStyle::LK_Java)
1752 return;
1753
1754 // In Java, we can parse everything up to the parens, which aren't optional.
1755 do {
1756 // There should not be a ;, { or } before the new's open paren.
1757 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1758 return;
1759
1760 // Consume the parens.
1761 if (FormatTok->is(tok::l_paren)) {
1762 parseParens();
1763
1764 // If there is a class body of an anonymous class, consume that as child.
1765 if (FormatTok->is(tok::l_brace))
1766 parseChildBlock();
1767 return;
1768 }
1769 nextToken();
1770 } while (!eof());
1771}
1772
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001773void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jasper66cb8c52015-05-04 09:22:29 +00001774 assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
Daniel Jaspere1e43192014-04-01 12:55:11 +00001775 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001776 nextToken();
Martin Probsta050f412017-05-18 21:19:29 +00001777 // JS' for await ( ...
Martin Probstbd49e322017-05-15 19:33:20 +00001778 if (Style.Language == FormatStyle::LK_JavaScript &&
Martin Probsta050f412017-05-18 21:19:29 +00001779 FormatTok->is(Keywords.kw_await))
Martin Probstbd49e322017-05-15 19:33:20 +00001780 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001781 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001782 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001783 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001784 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001785 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001786 addUnwrappedLine();
1787 } else {
1788 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001789 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001790 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001791 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001792 }
1793}
1794
Daniel Jasperf7935112012-12-03 18:12:45 +00001795void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001796 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001797 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001798 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001799 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001800 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001801 if (Style.BraceWrapping.IndentBraces)
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001802 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001803 } else {
1804 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001805 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001806 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001807 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001808 }
1809
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001810 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001811 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001812 addUnwrappedLine();
1813 return;
1814 }
1815
Daniel Jasperf7935112012-12-03 18:12:45 +00001816 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001817 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001818}
1819
1820void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001821 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001822 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001823 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001824 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001825 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001826 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001827 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001828 if (FormatTok->Tok.is(tok::kw_break)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00001829 if (Style.BraceWrapping.AfterControlStatement)
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001830 addUnwrappedLine();
1831 parseStructuralElement();
1832 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001833 addUnwrappedLine();
1834 } else {
Daniel Jasper1fe0d5c2015-05-06 15:19:47 +00001835 if (FormatTok->is(tok::semi))
1836 nextToken();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001837 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001838 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001839 Line->Level = OldLineLevel;
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001840 if (FormatTok->isNot(tok::l_brace)) {
Daniel Jasper40609472016-04-06 15:02:46 +00001841 parseStructuralElement();
Daniel Jasper2cce7b72016-04-06 16:41:39 +00001842 addUnwrappedLine();
1843 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001844}
1845
1846void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001847 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001848 // FIXME: fix handling of complex expressions here.
1849 do {
1850 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001851 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001852 parseLabel();
1853}
1854
1855void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001856 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001857 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001858 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001859 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001860 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001861 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001862 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001863 addUnwrappedLine();
1864 } else {
1865 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001866 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001867 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001868 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001869 }
1870}
1871
1872void UnwrappedLineParser::parseAccessSpecifier() {
1873 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001874 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001875 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001876 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001877 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001878 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001879 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001880 addUnwrappedLine();
1881}
1882
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001883bool UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001884 // Won't be 'enum' for NS_ENUMs.
1885 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001886 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001887
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001888 // In TypeScript, "enum" can also be used as property name, e.g. in interface
1889 // declarations. An "enum" keyword followed by a colon would be a syntax
1890 // error and thus assume it is just an identifier.
Daniel Jasper87379302016-02-03 05:33:44 +00001891 if (Style.Language == FormatStyle::LK_JavaScript &&
1892 FormatTok->isOneOf(tok::colon, tok::question))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001893 return false;
1894
Daniel Jasper2b41a822013-08-20 12:42:50 +00001895 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001896 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1897 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001898
Daniel Jasper786a5502013-09-06 21:32:35 +00001899 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001900 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1901 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001902 nextToken();
1903 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001904 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001905 parseParens();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001906 if (FormatTok->is(tok::identifier)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001907 nextToken();
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001908 // If there are two identifiers in a row, this is likely an elaborate
1909 // return type. In Java, this can be "implements", etc.
Daniel Jasper1dbc2102017-03-31 13:30:24 +00001910 if (Style.isCpp() && FormatTok->is(tok::identifier))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001911 return false;
Daniel Jasperb5a0b852015-06-19 08:17:32 +00001912 }
Manuel Klimek2cec0192013-01-21 19:17:52 +00001913 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001914
1915 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001916 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001917 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00001918 FormatTok->BlockKind = BK_Block;
1919
1920 if (Style.Language == FormatStyle::LK_Java) {
1921 // Java enums are different.
1922 parseJavaEnumBody();
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001923 return true;
1924 }
1925 if (Style.Language == FormatStyle::LK_Proto) {
Daniel Jasperc6dd2732015-07-16 14:25:43 +00001926 parseBlock(/*MustBeDeclaration=*/true);
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001927 return true;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001928 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001929
1930 // Parse enum body.
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001931 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001932 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1933 if (HasError) {
1934 if (FormatTok->is(tok::semi))
1935 nextToken();
1936 addUnwrappedLine();
1937 }
Daniel Jasper6f5a1932015-12-29 08:54:23 +00001938 return true;
Daniel Jasper6be0f552014-11-13 15:56:28 +00001939
Daniel Jasper90cf3802015-06-17 09:44:02 +00001940 // There is no addUnwrappedLine() here so that we fall through to parsing a
1941 // structural element afterwards. Thus, in "enum A {} n, m;",
Manuel Klimek2cec0192013-01-21 19:17:52 +00001942 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001943}
1944
1945void UnwrappedLineParser::parseJavaEnumBody() {
1946 // Determine whether the enum is simple, i.e. does not have a semicolon or
1947 // constants with class bodies. Simple enums can be formatted like braced
1948 // lists, contracted to a single line, etc.
1949 unsigned StoredPosition = Tokens->getPosition();
1950 bool IsSimple = true;
1951 FormatToken *Tok = Tokens->getNextToken();
1952 while (Tok) {
1953 if (Tok->is(tok::r_brace))
1954 break;
1955 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1956 IsSimple = false;
1957 break;
1958 }
1959 // FIXME: This will also mark enums with braces in the arguments to enum
1960 // constants as "not simple". This is probably fine in practice, though.
1961 Tok = Tokens->getNextToken();
1962 }
1963 FormatTok = Tokens->setPosition(StoredPosition);
1964
1965 if (IsSimple) {
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00001966 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001967 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001968 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001969 return;
1970 }
1971
1972 // Parse the body of a more complex enum.
1973 // First add a line for everything up to the "{".
1974 nextToken();
1975 addUnwrappedLine();
1976 ++Line->Level;
1977
1978 // Parse the enum constants.
1979 while (FormatTok) {
1980 if (FormatTok->is(tok::l_brace)) {
1981 // Parse the constant's class body.
1982 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1983 /*MunchSemi=*/false);
1984 } else if (FormatTok->is(tok::l_paren)) {
1985 parseParens();
1986 } else if (FormatTok->is(tok::comma)) {
1987 nextToken();
1988 addUnwrappedLine();
1989 } else if (FormatTok->is(tok::semi)) {
1990 nextToken();
1991 addUnwrappedLine();
1992 break;
1993 } else if (FormatTok->is(tok::r_brace)) {
1994 addUnwrappedLine();
1995 break;
1996 } else {
1997 nextToken();
1998 }
1999 }
2000
2001 // Parse the class body after the enum's ";" if any.
2002 parseLevel(/*HasOpeningBrace=*/true);
2003 nextToken();
2004 --Line->Level;
2005 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00002006}
2007
Martin Probst1027fb82017-02-07 14:05:30 +00002008void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00002009 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00002010 nextToken();
Daniel Jasper04785d02015-05-06 14:03:02 +00002011
Daniel Jasper04785d02015-05-06 14:03:02 +00002012 // The actual identifier can be a nested name specifier, and in macros
2013 // it is often token-pasted.
2014 while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
2015 tok::kw___attribute, tok::kw___declspec,
2016 tok::kw_alignas) ||
2017 ((Style.Language == FormatStyle::LK_Java ||
2018 Style.Language == FormatStyle::LK_JavaScript) &&
2019 FormatTok->isOneOf(tok::period, tok::comma))) {
Martin Probstcb870c52017-08-01 15:46:10 +00002020 if (Style.Language == FormatStyle::LK_JavaScript &&
2021 FormatTok->isOneOf(Keywords.kw_extends, Keywords.kw_implements)) {
2022 // JavaScript/TypeScript supports inline object types in
2023 // extends/implements positions:
2024 // class Foo implements {bar: number} { }
2025 nextToken();
2026 if (FormatTok->is(tok::l_brace)) {
2027 tryToParseBracedList();
2028 continue;
2029 }
2030 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002031 bool IsNonMacroIdentifier =
2032 FormatTok->is(tok::identifier) &&
2033 FormatTok->TokenText != FormatTok->TokenText.upper();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002034 nextToken();
2035 // We can have macros or attributes in between 'class' and the class name.
Daniel Jasper04785d02015-05-06 14:03:02 +00002036 if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
Manuel Klimeke01bab52013-01-15 13:38:33 +00002037 parseParens();
Daniel Jasper04785d02015-05-06 14:03:02 +00002038 }
Manuel Klimeke01bab52013-01-15 13:38:33 +00002039
Daniel Jasper04785d02015-05-06 14:03:02 +00002040 // Note that parsing away template declarations here leads to incorrectly
2041 // accepting function declarations as record declarations.
2042 // In general, we cannot solve this problem. Consider:
2043 // class A<int> B() {}
2044 // which can be a function definition or a class definition when B() is a
2045 // macro. If we find enough real-world cases where this is a problem, we
2046 // can parse for the 'template' keyword in the beginning of the statement,
2047 // and thus rule out the record production in case there is no template
2048 // (this would still leave us with an ambiguity between template function
2049 // and class declarations).
Daniel Jasperadba2aa2015-05-18 12:52:00 +00002050 if (FormatTok->isOneOf(tok::colon, tok::less)) {
2051 while (!eof()) {
Daniel Jasper3c883d12015-05-18 14:49:19 +00002052 if (FormatTok->is(tok::l_brace)) {
2053 calculateBraceTypes(/*ExpectClassBody=*/true);
2054 if (!tryToParseBracedList())
2055 break;
2056 }
Daniel Jasper04785d02015-05-06 14:03:02 +00002057 if (FormatTok->Tok.is(tok::semi))
2058 return;
2059 nextToken();
Manuel Klimeke01bab52013-01-15 13:38:33 +00002060 }
2061 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002062 if (FormatTok->Tok.is(tok::l_brace)) {
Martin Probst1027fb82017-02-07 14:05:30 +00002063 if (ParseAsExpr) {
2064 parseChildBlock();
2065 } else {
2066 if (ShouldBreakBeforeBrace(Style, InitialToken))
2067 addUnwrappedLine();
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002068
Martin Probst1027fb82017-02-07 14:05:30 +00002069 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
2070 /*MunchSemi=*/false);
2071 }
Manuel Klimeka8eb9142013-05-13 12:51:40 +00002072 }
Daniel Jasper90cf3802015-06-17 09:44:02 +00002073 // There is no addUnwrappedLine() here so that we fall through to parsing a
2074 // structural element afterwards. Thus, in "class A {} n, m;",
2075 // "} n, m;" will end up in one unwrapped line.
Manuel Klimek28cacc72013-01-07 18:10:23 +00002076}
2077
Nico Weber8696a8d2013-01-09 21:15:03 +00002078void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002079 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00002080 do
2081 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002082 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00002083 nextToken(); // Skip '>'.
2084}
2085
2086void UnwrappedLineParser::parseObjCUntilAtEnd() {
2087 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002088 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002089 nextToken();
2090 addUnwrappedLine();
2091 break;
2092 }
Daniel Jaspera15da302013-08-28 08:04:23 +00002093 if (FormatTok->is(tok::l_brace)) {
2094 parseBlock(/*MustBeDeclaration=*/false);
2095 // In ObjC interfaces, nothing should be following the "}".
2096 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00002097 } else if (FormatTok->is(tok::r_brace)) {
2098 // Ignore stray "}". parseStructuralElement doesn't consume them.
2099 nextToken();
2100 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00002101 } else {
2102 parseStructuralElement();
2103 }
Nico Weber8696a8d2013-01-09 21:15:03 +00002104 } while (!eof());
2105}
2106
Nico Weber2ce0ac52013-01-09 23:25:37 +00002107void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00002108 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002109 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00002110
2111 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002112 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00002113 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002114 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002115 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00002116 // Skip category, if present.
2117 parseParens();
2118
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002119 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002120 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00002121
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002122 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasperc1bc38e2015-09-29 14:57:55 +00002123 if (Style.BraceWrapping.AfterObjCDeclaration)
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002124 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00002125 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00002126 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00002127
2128 // With instance variables, this puts '}' on its own line. Without instance
2129 // variables, this ends the @interface line.
2130 addUnwrappedLine();
2131
Nico Weber8696a8d2013-01-09 21:15:03 +00002132 parseObjCUntilAtEnd();
2133}
Nico Weber7eecf4b2013-01-09 20:25:35 +00002134
Nico Weber8696a8d2013-01-09 21:15:03 +00002135void UnwrappedLineParser::parseObjCProtocol() {
2136 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00002137 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00002138
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002139 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00002140 parseObjCProtocolList();
2141
2142 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002143 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00002144 nextToken();
2145 return addUnwrappedLine();
2146 }
2147
2148 addUnwrappedLine();
2149 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00002150}
2151
Daniel Jasperfca735c2015-02-19 16:14:18 +00002152void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
Martin Probst053f1aa2016-04-19 14:55:37 +00002153 bool IsImport = FormatTok->is(Keywords.kw_import);
2154 assert(IsImport || FormatTok->is(tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00002155 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00002156
Daniel Jasperec05fc72015-05-11 09:14:50 +00002157 // Consume the "default" in "export default class/function".
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002158 if (FormatTok->is(tok::kw_default))
2159 nextToken();
Daniel Jasperec05fc72015-05-11 09:14:50 +00002160
Martin Probst5f8445b2016-04-24 22:05:09 +00002161 // Consume "async function", "function" and "default function", so that these
2162 // get parsed as free-standing JS functions, i.e. do not require a trailing
2163 // semicolon.
2164 if (FormatTok->is(Keywords.kw_async))
2165 nextToken();
Daniel Jasper668c7bb2015-05-11 09:03:10 +00002166 if (FormatTok->is(Keywords.kw_function)) {
2167 nextToken();
2168 return;
2169 }
2170
Martin Probst053f1aa2016-04-19 14:55:37 +00002171 // For imports, `export *`, `export {...}`, consume the rest of the line up
2172 // to the terminating `;`. For everything else, just return and continue
2173 // parsing the structural element, i.e. the declaration or expression for
2174 // `export default`.
2175 if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
2176 !FormatTok->isStringLiteral())
2177 return;
Daniel Jasperfca735c2015-02-19 16:14:18 +00002178
Martin Probstd40bca42017-01-09 08:56:36 +00002179 while (!eof()) {
2180 if (FormatTok->is(tok::semi))
2181 return;
2182 if (Line->Tokens.size() == 0) {
2183 // Common issue: Automatic Semicolon Insertion wrapped the line, so the
2184 // import statement should terminate.
2185 return;
2186 }
Daniel Jasperefc1a832016-01-07 08:53:35 +00002187 if (FormatTok->is(tok::l_brace)) {
2188 FormatTok->BlockKind = BK_Block;
Krasimir Georgiev26b144c2017-07-03 15:05:14 +00002189 nextToken();
Daniel Jasperefc1a832016-01-07 08:53:35 +00002190 parseBracedList();
2191 } else {
2192 nextToken();
2193 }
Daniel Jasper354aa512015-02-19 16:07:32 +00002194 }
2195}
2196
Daniel Jasper3b203a62013-09-05 16:05:56 +00002197LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
2198 StringRef Prefix = "") {
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002199 llvm::dbgs() << Prefix << "Line(" << Line.Level
2200 << ", FSC=" << Line.FirstStartColumn << ")"
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002201 << (Line.InPPDirective ? " MACRO" : "") << ": ";
2202 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2203 E = Line.Tokens.end();
2204 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002205 llvm::dbgs() << I->Tok->Tok.getName() << "["
Manuel Klimek89628f62017-09-20 09:51:03 +00002206 << "T=" << I->Tok->Type << ", OC=" << I->Tok->OriginalColumn
2207 << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002208 }
2209 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
2210 E = Line.Tokens.end();
2211 I != E; ++I) {
2212 const UnwrappedLineNode &Node = *I;
2213 for (SmallVectorImpl<UnwrappedLine>::const_iterator
2214 I = Node.Children.begin(),
2215 E = Node.Children.end();
2216 I != E; ++I) {
2217 printDebugInfo(*I, "\nChild: ");
2218 }
2219 }
2220 llvm::dbgs() << "\n";
2221}
2222
Daniel Jasperf7935112012-12-03 18:12:45 +00002223void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002224 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00002225 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00002226 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002227 if (CurrentLines == &Lines)
2228 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00002229 });
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002230 CurrentLines->push_back(std::move(*Line));
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00002231 Line->Tokens.clear();
Krasimir Georgiev85c37042017-03-01 16:38:08 +00002232 Line->MatchingOpeningBlockLineIndex = UnwrappedLine::kInvalidIndex;
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +00002233 Line->FirstStartColumn = 0;
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002234 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Benjamin Kramerc7551a42015-05-31 11:18:05 +00002235 CurrentLines->append(
2236 std::make_move_iterator(PreprocessorDirectives.begin()),
2237 std::make_move_iterator(PreprocessorDirectives.end()));
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00002238 PreprocessorDirectives.clear();
2239 }
Manuel Klimeke411aa82017-09-20 09:29:37 +00002240 // Disconnect the current token from the last token on the previous line.
2241 FormatTok->Previous = nullptr;
Daniel Jasperf7935112012-12-03 18:12:45 +00002242}
2243
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002244bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00002245
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002246bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002247 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
2248 FormatTok.NewlinesBefore > 0;
2249}
2250
Krasimir Georgiev91834222017-01-25 13:58:58 +00002251// Checks if \p FormatTok is a line comment that continues the line comment
2252// section on \p Line.
Krasimir Georgievea222a72017-05-22 10:07:56 +00002253static bool continuesLineCommentSection(const FormatToken &FormatTok,
2254 const UnwrappedLine &Line,
2255 llvm::Regex &CommentPragmasRegex) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002256 if (Line.Tokens.empty())
2257 return false;
Krasimir Georgiev84321612017-01-30 19:18:55 +00002258
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002259 StringRef IndentContent = FormatTok.TokenText;
2260 if (FormatTok.TokenText.startswith("//") ||
2261 FormatTok.TokenText.startswith("/*"))
2262 IndentContent = FormatTok.TokenText.substr(2);
2263 if (CommentPragmasRegex.match(IndentContent))
2264 return false;
2265
Krasimir Georgiev91834222017-01-25 13:58:58 +00002266 // If Line starts with a line comment, then FormatTok continues the comment
Krasimir Georgiev84321612017-01-30 19:18:55 +00002267 // section if its original column is greater or equal to the original start
Krasimir Georgiev91834222017-01-25 13:58:58 +00002268 // column of the line.
2269 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002270 // Define the min column token of a line as follows: if a line ends in '{' or
2271 // contains a '{' followed by a line comment, then the min column token is
2272 // that '{'. Otherwise, the min column token of the line is the first token of
2273 // the line.
2274 //
2275 // If Line starts with a token other than a line comment, then FormatTok
2276 // continues the comment section if its original column is greater than the
2277 // original start column of the min column token of the line.
Krasimir Georgiev91834222017-01-25 13:58:58 +00002278 //
2279 // For example, the second line comment continues the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002280 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002281 // // first line
2282 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002283 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002284 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002285 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002286 // // first line
2287 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002288 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002289 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002290 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002291 // int i; // first line
2292 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002293 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002294 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002295 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002296 // do { // first line
2297 // // second line
2298 // int i;
2299 // } while (true);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002300 //
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002301 // and:
2302 //
2303 // enum {
2304 // a, // first line
2305 // // second line
2306 // b
2307 // };
2308 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002309 // The second line comment doesn't continue the first in these cases:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002310 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002311 // // first line
2312 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002313 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002314 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002315 //
Krasimir Georgiev91834222017-01-25 13:58:58 +00002316 // int i; // first line
2317 // // second line
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002318 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002319 // and:
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002320 //
Krasimir Georgiev84321612017-01-30 19:18:55 +00002321 // do { // first line
2322 // // second line
2323 // int i;
2324 // } while (true);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002325 //
2326 // and:
2327 //
2328 // enum {
2329 // a, // first line
2330 // // second line
2331 // };
Krasimir Georgiev84321612017-01-30 19:18:55 +00002332 const FormatToken *MinColumnToken = Line.Tokens.front().Tok;
2333
2334 // Scan for '{//'. If found, use the column of '{' as a min column for line
2335 // comment section continuation.
2336 const FormatToken *PreviousToken = nullptr;
Krasimir Georgievd86c25d2017-03-10 13:09:29 +00002337 for (const UnwrappedLineNode &Node : Line.Tokens) {
Krasimir Georgiev84321612017-01-30 19:18:55 +00002338 if (PreviousToken && PreviousToken->is(tok::l_brace) &&
2339 isLineComment(*Node.Tok)) {
2340 MinColumnToken = PreviousToken;
2341 break;
2342 }
2343 PreviousToken = Node.Tok;
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002344
2345 // Grab the last newline preceding a token in this unwrapped line.
2346 if (Node.Tok->NewlinesBefore > 0) {
2347 MinColumnToken = Node.Tok;
2348 }
Krasimir Georgiev84321612017-01-30 19:18:55 +00002349 }
2350 if (PreviousToken && PreviousToken->is(tok::l_brace)) {
2351 MinColumnToken = PreviousToken;
2352 }
2353
Krasimir Georgievea222a72017-05-22 10:07:56 +00002354 return continuesLineComment(FormatTok, /*Previous=*/Line.Tokens.back().Tok,
2355 MinColumnToken);
Krasimir Georgiev91834222017-01-25 13:58:58 +00002356}
2357
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002358void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
2359 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002360 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002361 I = CommentsBeforeNextToken.begin(),
2362 E = CommentsBeforeNextToken.end();
2363 I != E; ++I) {
Krasimir Georgiev91834222017-01-25 13:58:58 +00002364 // Line comments that belong to the same line comment section are put on the
2365 // same line since later we might want to reflow content between them.
Krasimir Georgiev753625b2017-01-31 13:32:38 +00002366 // Additional fine-grained breaking of line comment sections is controlled
2367 // by the class BreakableLineCommentSection in case it is desirable to keep
2368 // several line comment sections in the same unwrapped line.
2369 //
2370 // FIXME: Consider putting separate line comment sections as children to the
2371 // unwrapped line instead.
Krasimir Georgiev00c5c722017-02-02 15:32:19 +00002372 (*I)->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002373 continuesLineCommentSection(**I, *Line, CommentPragmasRegex);
Krasimir Georgievb6ccd382017-02-02 14:36:50 +00002374 if (isOnNewLine(**I) && JustComments && !(*I)->ContinuesLineCommentSection)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002375 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002376 pushToken(*I);
2377 }
Daniel Jaspere60cba12015-05-13 11:35:53 +00002378 if (NewlineBeforeNext && JustComments)
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002379 addUnwrappedLine();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002380 CommentsBeforeNextToken.clear();
2381}
2382
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002383void UnwrappedLineParser::nextToken(int LevelDifference) {
Daniel Jasperf7935112012-12-03 18:12:45 +00002384 if (eof())
2385 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002386 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002387 pushToken(FormatTok);
Manuel Klimek89628f62017-09-20 09:51:03 +00002388 FormatToken *Previous = FormatTok;
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002389 if (Style.Language != FormatStyle::LK_JavaScript)
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002390 readToken(LevelDifference);
Daniel Jasper1dcbbcfc2016-03-14 19:21:36 +00002391 else
2392 readTokenWithJavaScriptASI();
Manuel Klimeke411aa82017-09-20 09:29:37 +00002393 FormatTok->Previous = Previous;
Daniel Jasperb9a49902016-01-09 15:56:28 +00002394}
2395
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002396void UnwrappedLineParser::distributeComments(
2397 const SmallVectorImpl<FormatToken *> &Comments,
2398 const FormatToken *NextTok) {
2399 // Whether or not a line comment token continues a line is controlled by
Krasimir Georgievea222a72017-05-22 10:07:56 +00002400 // the method continuesLineCommentSection, with the following caveat:
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002401 //
2402 // Define a trail of Comments to be a nonempty proper postfix of Comments such
2403 // that each comment line from the trail is aligned with the next token, if
2404 // the next token exists. If a trail exists, the beginning of the maximal
2405 // trail is marked as a start of a new comment section.
2406 //
2407 // For example in this code:
2408 //
2409 // int a; // line about a
2410 // // line 1 about b
2411 // // line 2 about b
2412 // int b;
2413 //
2414 // the two lines about b form a maximal trail, so there are two sections, the
2415 // first one consisting of the single comment "// line about a" and the
2416 // second one consisting of the next two comments.
2417 if (Comments.empty())
2418 return;
2419 bool ShouldPushCommentsInCurrentLine = true;
2420 bool HasTrailAlignedWithNextToken = false;
2421 unsigned StartOfTrailAlignedWithNextToken = 0;
2422 if (NextTok) {
2423 // We are skipping the first element intentionally.
2424 for (unsigned i = Comments.size() - 1; i > 0; --i) {
2425 if (Comments[i]->OriginalColumn == NextTok->OriginalColumn) {
2426 HasTrailAlignedWithNextToken = true;
2427 StartOfTrailAlignedWithNextToken = i;
2428 }
2429 }
2430 }
2431 for (unsigned i = 0, e = Comments.size(); i < e; ++i) {
2432 FormatToken *FormatTok = Comments[i];
Manuel Klimek89628f62017-09-20 09:51:03 +00002433 if (HasTrailAlignedWithNextToken && i == StartOfTrailAlignedWithNextToken) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002434 FormatTok->ContinuesLineCommentSection = false;
2435 } else {
2436 FormatTok->ContinuesLineCommentSection =
Krasimir Georgievea222a72017-05-22 10:07:56 +00002437 continuesLineCommentSection(*FormatTok, *Line, CommentPragmasRegex);
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002438 }
2439 if (!FormatTok->ContinuesLineCommentSection &&
2440 (isOnNewLine(*FormatTok) || FormatTok->IsFirst)) {
2441 ShouldPushCommentsInCurrentLine = false;
2442 }
2443 if (ShouldPushCommentsInCurrentLine) {
2444 pushToken(FormatTok);
2445 } else {
2446 CommentsBeforeNextToken.push_back(FormatTok);
2447 }
2448 }
2449}
2450
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002451void UnwrappedLineParser::readToken(int LevelDifference) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002452 SmallVector<FormatToken *, 1> Comments;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002453 do {
2454 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00002455 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002456 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
2457 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002458 distributeComments(Comments, FormatTok);
2459 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002460 // If there is an unfinished unwrapped line, we flush the preprocessor
2461 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00002462 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002463 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Krasimir Georgiev3e051052017-07-24 14:51:59 +00002464 assert((LevelDifference >= 0 ||
2465 static_cast<unsigned>(-LevelDifference) <= Line->Level) &&
2466 "LevelDifference makes Line->Level negative");
2467 Line->Level += LevelDifference;
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00002468 // Comments stored before the preprocessor directive need to be output
2469 // before the preprocessor directive, at the same level as the
2470 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00002471 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002472 parsePPDirective();
2473 }
Manuel Klimek68b03042014-04-14 09:14:11 +00002474 while (FormatTok->Type == TT_ConflictStart ||
2475 FormatTok->Type == TT_ConflictEnd ||
2476 FormatTok->Type == TT_ConflictAlternative) {
2477 if (FormatTok->Type == TT_ConflictStart) {
2478 conditionalCompilationStart(/*Unreachable=*/false);
2479 } else if (FormatTok->Type == TT_ConflictAlternative) {
2480 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00002481 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00002482 conditionalCompilationEnd();
2483 }
2484 FormatTok = Tokens->getNextToken();
2485 FormatTok->MustBreakBefore = true;
2486 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002487
Francois Ferranda98a95c2017-07-28 07:56:14 +00002488 if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) &&
Alexander Kornienkof2e02122013-05-24 18:24:24 +00002489 !Line->InPPDirective) {
2490 continue;
2491 }
2492
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002493 if (!FormatTok->Tok.is(tok::comment)) {
2494 distributeComments(Comments, FormatTok);
2495 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002496 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002497 }
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002498
2499 Comments.push_back(FormatTok);
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002500 } while (!eof());
Krasimir Georgievf62f9582017-02-08 10:30:44 +00002501
2502 distributeComments(Comments, nullptr);
2503 Comments.clear();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002504}
2505
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00002506void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002507 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002508 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00002509 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00002510 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00002511 }
Daniel Jasperf7935112012-12-03 18:12:45 +00002512}
2513
Daniel Jasper8d1832e2013-01-07 13:26:07 +00002514} // end namespace format
2515} // end namespace clang