blob: 7afe183236a751dc5001b0edec3802a59e006898 [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:
28 virtual ~FormatTokenSource() {}
29 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
Manuel Klimek1abf7892013-01-04 23:34:14 +000058class ScopedMacroState : public FormatTokenSource {
59public:
60 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000061 FormatToken *&ResetToken, bool &StructuralError)
Manuel Klimek1abf7892013-01-04 23:34:14 +000062 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
Manuel Klimek1a18c402013-04-12 14:13:36 +000063 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
64 StructuralError(StructuralError),
Craig Topper2145bc02014-05-09 08:15:10 +000065 PreviousStructuralError(StructuralError), Token(nullptr) {
Manuel Klimek1abf7892013-01-04 23:34:14 +000066 TokenSource = this;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000067 Line.Level = 0;
Manuel Klimek1abf7892013-01-04 23:34:14 +000068 Line.InPPDirective = true;
69 }
70
71 ~ScopedMacroState() {
72 TokenSource = PreviousTokenSource;
73 ResetToken = Token;
74 Line.InPPDirective = false;
Manuel Klimekef2cfb12013-01-05 22:14:16 +000075 Line.Level = PreviousLineLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +000076 StructuralError = PreviousStructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +000077 }
78
Craig Topperfb6b25b2014-03-15 04:29:04 +000079 FormatToken *getNextToken() override {
Manuel Klimek78725712013-01-07 10:03:37 +000080 // The \c UnwrappedLineParser guards against this by never calling
81 // \c getNextToken() after it has encountered the first eof token.
82 assert(!eof());
Manuel Klimek1abf7892013-01-04 23:34:14 +000083 Token = PreviousTokenSource->getNextToken();
84 if (eof())
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000085 return getFakeEOF();
Manuel Klimek1abf7892013-01-04 23:34:14 +000086 return Token;
87 }
88
Craig Topperfb6b25b2014-03-15 04:29:04 +000089 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
Manuel Klimekab419912013-05-23 09:41:43 +000090
Craig Topperfb6b25b2014-03-15 04:29:04 +000091 FormatToken *setPosition(unsigned Position) override {
Manuel Klimekab419912013-05-23 09:41:43 +000092 Token = PreviousTokenSource->setPosition(Position);
93 return Token;
94 }
95
Manuel Klimek1abf7892013-01-04 23:34:14 +000096private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000097 bool eof() { return Token && Token->HasUnescapedNewline; }
Manuel Klimek1abf7892013-01-04 23:34:14 +000098
Manuel Klimek15dfe7a2013-05-28 11:55:06 +000099 FormatToken *getFakeEOF() {
100 static bool EOFInitialized = false;
101 static FormatToken FormatTok;
102 if (!EOFInitialized) {
103 FormatTok.Tok.startToken();
104 FormatTok.Tok.setKind(tok::eof);
105 EOFInitialized = true;
106 }
107 return &FormatTok;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000108 }
109
110 UnwrappedLine &Line;
111 FormatTokenSource *&TokenSource;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000112 FormatToken *&ResetToken;
Manuel Klimekef2cfb12013-01-05 22:14:16 +0000113 unsigned PreviousLineLevel;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000114 FormatTokenSource *PreviousTokenSource;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000115 bool &StructuralError;
116 bool PreviousStructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000117
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000118 FormatToken *Token;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000119};
120
Craig Topper69665e12013-07-01 04:21:54 +0000121} // end anonymous namespace
122
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000123class ScopedLineState {
124public:
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000125 ScopedLineState(UnwrappedLineParser &Parser,
126 bool SwitchToPreprocessorLines = false)
David Blaikieefb6eb22014-08-09 20:02:07 +0000127 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
Manuel Klimekd3b92fa2013-01-18 14:04:34 +0000128 if (SwitchToPreprocessorLines)
129 Parser.CurrentLines = &Parser.PreprocessorDirectives;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000130 else if (!Parser.Line->Tokens.empty())
131 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
David Blaikieefb6eb22014-08-09 20:02:07 +0000132 PreBlockLine = std::move(Parser.Line);
133 Parser.Line = llvm::make_unique<UnwrappedLine>();
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000134 Parser.Line->Level = PreBlockLine->Level;
135 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000136 }
137
138 ~ScopedLineState() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000139 if (!Parser.Line->Tokens.empty()) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000140 Parser.addUnwrappedLine();
141 }
Daniel Jasperdaffc0d2013-01-16 09:10:19 +0000142 assert(Parser.Line->Tokens.empty());
David Blaikieefb6eb22014-08-09 20:02:07 +0000143 Parser.Line = std::move(PreBlockLine);
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000144 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
145 Parser.MustBreakBeforeNextToken = true;
146 Parser.CurrentLines = OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000147 }
148
149private:
150 UnwrappedLineParser &Parser;
151
David Blaikieefb6eb22014-08-09 20:02:07 +0000152 std::unique_ptr<UnwrappedLine> PreBlockLine;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000153 SmallVectorImpl<UnwrappedLine> *OriginalLines;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000154};
155
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000156class CompoundStatementIndenter {
157public:
158 CompoundStatementIndenter(UnwrappedLineParser *Parser,
159 const FormatStyle &Style, unsigned &LineLevel)
160 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
161 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
162 Parser->addUnwrappedLine();
163 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
164 Parser->addUnwrappedLine();
165 ++LineLevel;
166 }
167 }
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000168 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000169
170private:
171 unsigned &LineLevel;
172 unsigned OldLineLevel;
173};
174
Craig Topper69665e12013-07-01 04:21:54 +0000175namespace {
176
Manuel Klimekab419912013-05-23 09:41:43 +0000177class IndexedTokenSource : public FormatTokenSource {
178public:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000179 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
Manuel Klimekab419912013-05-23 09:41:43 +0000180 : Tokens(Tokens), Position(-1) {}
181
Craig Topperfb6b25b2014-03-15 04:29:04 +0000182 FormatToken *getNextToken() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000183 ++Position;
184 return Tokens[Position];
185 }
186
Craig Topperfb6b25b2014-03-15 04:29:04 +0000187 unsigned getPosition() override {
Manuel Klimekab419912013-05-23 09:41:43 +0000188 assert(Position >= 0);
189 return Position;
190 }
191
Craig Topperfb6b25b2014-03-15 04:29:04 +0000192 FormatToken *setPosition(unsigned P) override {
Manuel Klimekab419912013-05-23 09:41:43 +0000193 Position = P;
194 return Tokens[Position];
195 }
196
Manuel Klimek71814b42013-10-11 21:25:45 +0000197 void reset() { Position = -1; }
198
Manuel Klimekab419912013-05-23 09:41:43 +0000199private:
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000200 ArrayRef<FormatToken *> Tokens;
Manuel Klimekab419912013-05-23 09:41:43 +0000201 int Position;
202};
203
Craig Topper69665e12013-07-01 04:21:54 +0000204} // end anonymous namespace
205
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000206UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000207 const AdditionalKeywords &Keywords,
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000208 ArrayRef<FormatToken *> Tokens,
Daniel Jasperd2ae41a2013-05-15 08:14:19 +0000209 UnwrappedLineConsumer &Callback)
Daniel Jasperb05a81d2014-05-09 13:11:16 +0000210 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
211 CurrentLines(&Lines), StructuralError(false), Style(Style),
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000212 Keywords(Keywords), Tokens(nullptr), Callback(Callback),
213 AllTokens(Tokens), PPBranchLevel(-1) {}
Manuel Klimek71814b42013-10-11 21:25:45 +0000214
215void UnwrappedLineParser::reset() {
216 PPBranchLevel = -1;
217 Line.reset(new UnwrappedLine);
218 CommentsBeforeNextToken.clear();
Craig Topper2145bc02014-05-09 08:15:10 +0000219 FormatTok = nullptr;
Manuel Klimek71814b42013-10-11 21:25:45 +0000220 MustBreakBeforeNextToken = false;
221 PreprocessorDirectives.clear();
222 CurrentLines = &Lines;
223 DeclarationScopeStack.clear();
224 StructuralError = false;
225 PPStack.clear();
226}
Daniel Jasperf7935112012-12-03 18:12:45 +0000227
Alexander Kornienko870f9eb2012-12-04 17:27:50 +0000228bool UnwrappedLineParser::parse() {
Manuel Klimekab419912013-05-23 09:41:43 +0000229 IndexedTokenSource TokenSource(AllTokens);
Manuel Klimek71814b42013-10-11 21:25:45 +0000230 do {
231 DEBUG(llvm::dbgs() << "----\n");
232 reset();
233 Tokens = &TokenSource;
234 TokenSource.reset();
Daniel Jaspera79064a2013-03-01 18:11:39 +0000235
Manuel Klimek71814b42013-10-11 21:25:45 +0000236 readToken();
237 parseFile();
238 // Create line with eof token.
239 pushToken(FormatTok);
240 addUnwrappedLine();
241
242 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
243 E = Lines.end();
244 I != E; ++I) {
245 Callback.consumeUnwrappedLine(*I);
246 }
247 Callback.finishRun();
248 Lines.clear();
249 while (!PPLevelBranchIndex.empty() &&
Daniel Jasper53bd1672013-10-12 13:32:56 +0000250 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000251 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
252 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
253 }
254 if (!PPLevelBranchIndex.empty()) {
255 ++PPLevelBranchIndex.back();
256 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
257 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
258 }
259 } while (!PPLevelBranchIndex.empty());
260
Manuel Klimek1a18c402013-04-12 14:13:36 +0000261 return StructuralError;
Manuel Klimek1abf7892013-01-04 23:34:14 +0000262}
263
Manuel Klimek1a18c402013-04-12 14:13:36 +0000264void UnwrappedLineParser::parseFile() {
Daniel Jasperdd9276e2013-03-22 16:55:40 +0000265 ScopedDeclarationState DeclarationState(
266 *Line, DeclarationScopeStack,
Daniel Jasperfca735c2015-02-19 16:14:18 +0000267 /*MustBeDeclaration=*/!Line->InPPDirective);
Nico Weber9096fc02013-06-26 00:30:14 +0000268 parseLevel(/*HasOpeningBrace=*/false);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000269 // Make sure to format the remaining tokens.
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000270 flushComments(true);
Manuel Klimek1abf7892013-01-04 23:34:14 +0000271 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000272}
273
Manuel Klimek1a18c402013-04-12 14:13:36 +0000274void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000275 bool SwitchLabelEncountered = false;
Daniel Jasperf7935112012-12-03 18:12:45 +0000276 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000277 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +0000278 case tok::comment:
Daniel Jaspere25509f2012-12-17 11:29:41 +0000279 nextToken();
280 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000281 break;
282 case tok::l_brace:
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000283 // FIXME: Add parameter whether this can happen - if this happens, we must
284 // be in a non-declaration context.
Nico Weber9096fc02013-06-26 00:30:14 +0000285 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +0000286 addUnwrappedLine();
287 break;
288 case tok::r_brace:
Manuel Klimek1a18c402013-04-12 14:13:36 +0000289 if (HasOpeningBrace)
290 return;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000291 StructuralError = true;
292 nextToken();
293 addUnwrappedLine();
Manuel Klimek1058d982013-01-06 20:07:31 +0000294 break;
Daniel Jasper516d7972013-07-25 11:31:57 +0000295 case tok::kw_default:
296 case tok::kw_case:
Daniel Jasper72407622013-09-02 08:26:29 +0000297 if (!SwitchLabelEncountered &&
298 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
299 ++Line->Level;
Daniel Jasper516d7972013-07-25 11:31:57 +0000300 SwitchLabelEncountered = true;
301 parseStructuralElement();
302 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000303 default:
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000304 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +0000305 break;
306 }
307 } while (!eof());
308}
309
Manuel Klimekab419912013-05-23 09:41:43 +0000310void UnwrappedLineParser::calculateBraceTypes() {
311 // We'll parse forward through the tokens until we hit
312 // a closing brace or eof - note that getNextToken() will
313 // parse macros, so this will magically work inside macro
314 // definitions, too.
315 unsigned StoredPosition = Tokens->getPosition();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000316 FormatToken *Tok = FormatTok;
Manuel Klimekab419912013-05-23 09:41:43 +0000317 // Keep a stack of positions of lbrace tokens. We will
318 // update information about whether an lbrace starts a
319 // braced init list or a different block during the loop.
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000320 SmallVector<FormatToken *, 8> LBraceStack;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000321 assert(Tok->Tok.is(tok::l_brace));
Manuel Klimekab419912013-05-23 09:41:43 +0000322 do {
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000323 // Get next none-comment token.
324 FormatToken *NextTok;
Daniel Jasperca7bd722013-07-01 16:43:38 +0000325 unsigned ReadTokens = 0;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000326 do {
327 NextTok = Tokens->getNextToken();
Daniel Jasperca7bd722013-07-01 16:43:38 +0000328 ++ReadTokens;
Daniel Jasper7f5d53e2013-07-01 09:15:46 +0000329 } while (NextTok->is(tok::comment));
330
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000331 switch (Tok->Tok.getKind()) {
Manuel Klimekab419912013-05-23 09:41:43 +0000332 case tok::l_brace:
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000333 LBraceStack.push_back(Tok);
Manuel Klimekab419912013-05-23 09:41:43 +0000334 break;
335 case tok::r_brace:
336 if (!LBraceStack.empty()) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000337 if (LBraceStack.back()->BlockKind == BK_Unknown) {
Daniel Jasper220c0d12014-04-10 07:27:12 +0000338 bool ProbablyBracedList = false;
339 if (Style.Language == FormatStyle::LK_Proto) {
340 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
341 } else {
Daniel Jasper91b032a2014-05-22 12:46:38 +0000342 // Using OriginalColumn to distinguish between ObjC methods and
343 // binary operators is a bit hacky.
344 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
345 NextTok->OriginalColumn == 0;
346
Daniel Jasper220c0d12014-04-10 07:27:12 +0000347 // If there is a comma, semicolon or right paren after the closing
348 // brace, we assume this is a braced initializer list. Note that
349 // regardless how we mark inner braces here, we will overwrite the
350 // BlockKind later if we parse a braced list (where all blocks
351 // inside are by default braced lists), or when we explicitly detect
352 // blocks (for example while parsing lambdas).
353 //
354 // We exclude + and - as they can be ObjC visibility modifiers.
355 ProbablyBracedList =
356 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
Daniel Jasper438059e2014-05-22 12:11:13 +0000357 tok::r_paren, tok::r_square, tok::l_brace,
Daniel Jasper65df5aa2014-08-04 14:51:02 +0000358 tok::l_paren, tok::ellipsis) ||
Daniel Jasper91b032a2014-05-22 12:46:38 +0000359 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
Daniel Jasper220c0d12014-04-10 07:27:12 +0000360 }
361 if (ProbablyBracedList) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000362 Tok->BlockKind = BK_BracedInit;
363 LBraceStack.back()->BlockKind = BK_BracedInit;
364 } else {
365 Tok->BlockKind = BK_Block;
366 LBraceStack.back()->BlockKind = BK_Block;
367 }
Manuel Klimekab419912013-05-23 09:41:43 +0000368 }
369 LBraceStack.pop_back();
370 }
371 break;
Daniel Jasperac7e34e2014-03-13 10:11:17 +0000372 case tok::at:
Manuel Klimekab419912013-05-23 09:41:43 +0000373 case tok::semi:
374 case tok::kw_if:
375 case tok::kw_while:
376 case tok::kw_for:
377 case tok::kw_switch:
378 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000379 case tok::kw___try:
Daniel Jasper393564f2013-05-31 14:56:29 +0000380 if (!LBraceStack.empty())
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000381 LBraceStack.back()->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000382 break;
383 default:
384 break;
385 }
386 Tok = NextTok;
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000387 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
Manuel Klimekab419912013-05-23 09:41:43 +0000388 // Assume other blocks for all unclosed opening braces.
389 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
Daniel Jasperb1f74a82013-07-09 09:06:29 +0000390 if (LBraceStack[i]->BlockKind == BK_Unknown)
391 LBraceStack[i]->BlockKind = BK_Block;
Manuel Klimekab419912013-05-23 09:41:43 +0000392 }
Manuel Klimekbab25fd2013-09-04 08:20:47 +0000393
Manuel Klimekab419912013-05-23 09:41:43 +0000394 FormatTok = Tokens->setPosition(StoredPosition);
395}
396
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000397void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
398 bool MunchSemi) {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000399 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
Daniel Jasper516d7972013-07-25 11:31:57 +0000400 unsigned InitialLevel = Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +0000401 nextToken();
402
Manuel Klimeka4fe1c12013-01-21 16:42:44 +0000403 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000404
Manuel Klimek0a3a3c92013-01-23 09:32:48 +0000405 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
406 MustBeDeclaration);
Daniel Jasper65ee3472013-07-31 23:16:02 +0000407 if (AddLevel)
408 ++Line->Level;
Nico Weber9096fc02013-06-26 00:30:14 +0000409 parseLevel(/*HasOpeningBrace=*/true);
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000410
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000411 if (!FormatTok->Tok.is(tok::r_brace)) {
Daniel Jasper516d7972013-07-25 11:31:57 +0000412 Line->Level = InitialLevel;
Manuel Klimek1a18c402013-04-12 14:13:36 +0000413 StructuralError = true;
414 return;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +0000415 }
Alexander Kornienko0ea8e102012-12-04 15:40:36 +0000416
Daniel Jasperd1ae3582013-03-20 12:37:50 +0000417 nextToken(); // Munch the closing brace.
Manuel Klimekb212f3b2013-10-12 22:46:56 +0000418 if (MunchSemi && FormatTok->Tok.is(tok::semi))
419 nextToken();
Daniel Jasper516d7972013-07-25 11:31:57 +0000420 Line->Level = InitialLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +0000421}
422
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000423static bool isGoogScope(const UnwrappedLine &Line) {
Daniel Jasper616de8642014-11-23 16:46:28 +0000424 // FIXME: Closure-library specific stuff should not be hard-coded but be
425 // configurable.
Daniel Jasper4a39c842014-05-06 13:54:10 +0000426 if (Line.Tokens.size() < 4)
427 return false;
428 auto I = Line.Tokens.begin();
429 if (I->Tok->TokenText != "goog")
430 return false;
431 ++I;
432 if (I->Tok->isNot(tok::period))
433 return false;
434 ++I;
435 if (I->Tok->TokenText != "scope")
436 return false;
437 ++I;
438 return I->Tok->is(tok::l_paren);
439}
440
Roman Kashitsyna043ced2014-08-11 12:18:01 +0000441static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
442 const FormatToken &InitialToken) {
443 switch (Style.BreakBeforeBraces) {
444 case FormatStyle::BS_Linux:
445 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
446 case FormatStyle::BS_Allman:
447 case FormatStyle::BS_GNU:
448 return true;
449 default:
450 return false;
451 }
452}
453
Manuel Klimek516e0542013-09-04 13:25:30 +0000454void UnwrappedLineParser::parseChildBlock() {
455 FormatTok->BlockKind = BK_Block;
456 nextToken();
457 {
Daniel Jasper4a39c842014-05-06 13:54:10 +0000458 bool GoogScope =
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000459 Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
Manuel Klimek516e0542013-09-04 13:25:30 +0000460 ScopedLineState LineState(*this);
461 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
462 /*MustBeDeclaration=*/false);
Daniel Jasper4a39c842014-05-06 13:54:10 +0000463 Line->Level += GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000464 parseLevel(/*HasOpeningBrace=*/true);
Daniel Jasper02c7bca2015-03-30 09:56:50 +0000465 flushComments(isOnNewLine(*FormatTok));
Daniel Jasper4a39c842014-05-06 13:54:10 +0000466 Line->Level -= GoogScope ? 0 : 1;
Manuel Klimek516e0542013-09-04 13:25:30 +0000467 }
468 nextToken();
469}
470
Daniel Jasperf7935112012-12-03 18:12:45 +0000471void UnwrappedLineParser::parsePPDirective() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000472 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
Manuel Klimek1a18c402013-04-12 14:13:36 +0000473 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000474 nextToken();
475
Craig Topper2145bc02014-05-09 08:15:10 +0000476 if (!FormatTok->Tok.getIdentifierInfo()) {
Manuel Klimek591b5802013-01-31 15:58:48 +0000477 parsePPUnknown();
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000478 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000479 }
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000480
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000481 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000482 case tok::pp_define:
483 parsePPDefine();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000484 return;
485 case tok::pp_if:
Manuel Klimek71814b42013-10-11 21:25:45 +0000486 parsePPIf(/*IfDef=*/false);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000487 break;
488 case tok::pp_ifdef:
489 case tok::pp_ifndef:
Manuel Klimek71814b42013-10-11 21:25:45 +0000490 parsePPIf(/*IfDef=*/true);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000491 break;
492 case tok::pp_else:
493 parsePPElse();
494 break;
495 case tok::pp_elif:
496 parsePPElIf();
497 break;
498 case tok::pp_endif:
499 parsePPEndIf();
Manuel Klimek1abf7892013-01-04 23:34:14 +0000500 break;
501 default:
502 parsePPUnknown();
503 break;
504 }
505}
506
Manuel Klimek68b03042014-04-14 09:14:11 +0000507void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
508 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000509 PPStack.push_back(PP_Unreachable);
510 else
511 PPStack.push_back(PP_Conditional);
512}
513
Manuel Klimek68b03042014-04-14 09:14:11 +0000514void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000515 ++PPBranchLevel;
516 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
517 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
518 PPLevelBranchIndex.push_back(0);
519 PPLevelBranchCount.push_back(0);
520 }
521 PPChainBranchIndex.push(0);
Manuel Klimek68b03042014-04-14 09:14:11 +0000522 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
523 conditionalCompilationCondition(Unreachable || Skip);
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000524}
525
Manuel Klimek68b03042014-04-14 09:14:11 +0000526void UnwrappedLineParser::conditionalCompilationAlternative() {
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000527 if (!PPStack.empty())
528 PPStack.pop_back();
Manuel Klimek71814b42013-10-11 21:25:45 +0000529 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
530 if (!PPChainBranchIndex.empty())
531 ++PPChainBranchIndex.top();
Manuel Klimek68b03042014-04-14 09:14:11 +0000532 conditionalCompilationCondition(
533 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
534 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000535}
536
Manuel Klimek68b03042014-04-14 09:14:11 +0000537void UnwrappedLineParser::conditionalCompilationEnd() {
Manuel Klimek71814b42013-10-11 21:25:45 +0000538 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
539 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
540 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
Manuel Klimek71814b42013-10-11 21:25:45 +0000541 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
542 }
543 }
Manuel Klimek14bd9172014-01-29 08:49:02 +0000544 // Guard against #endif's without #if.
545 if (PPBranchLevel > 0)
546 --PPBranchLevel;
Manuel Klimek71814b42013-10-11 21:25:45 +0000547 if (!PPChainBranchIndex.empty())
548 PPChainBranchIndex.pop();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000549 if (!PPStack.empty())
550 PPStack.pop_back();
Manuel Klimek68b03042014-04-14 09:14:11 +0000551}
552
553void UnwrappedLineParser::parsePPIf(bool IfDef) {
554 nextToken();
555 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
Daniel Jasper9d22bcc2015-01-19 10:51:05 +0000556 FormatTok->Tok.getLiteralData() != nullptr &&
Manuel Klimek68b03042014-04-14 09:14:11 +0000557 StringRef(FormatTok->Tok.getLiteralData(),
558 FormatTok->Tok.getLength()) == "0") ||
559 FormatTok->Tok.is(tok::kw_false);
560 conditionalCompilationStart(!IfDef && IsLiteralFalse);
561 parsePPUnknown();
562}
563
564void UnwrappedLineParser::parsePPElse() {
565 conditionalCompilationAlternative();
566 parsePPUnknown();
567}
568
569void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
570
571void UnwrappedLineParser::parsePPEndIf() {
572 conditionalCompilationEnd();
Alexander Kornienkof2e02122013-05-24 18:24:24 +0000573 parsePPUnknown();
574}
575
Manuel Klimek1abf7892013-01-04 23:34:14 +0000576void UnwrappedLineParser::parsePPDefine() {
577 nextToken();
578
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000579 if (FormatTok->Tok.getKind() != tok::identifier) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000580 parsePPUnknown();
581 return;
582 }
583 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000584 if (FormatTok->Tok.getKind() == tok::l_paren &&
585 FormatTok->WhitespaceRange.getBegin() ==
586 FormatTok->WhitespaceRange.getEnd()) {
Manuel Klimek1abf7892013-01-04 23:34:14 +0000587 parseParens();
588 }
589 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +0000590 Line->Level = 1;
Manuel Klimek1b896292013-01-07 09:34:28 +0000591
592 // Errors during a preprocessor directive can only affect the layout of the
593 // preprocessor directive, and thus we ignore them. An alternative approach
594 // would be to use the same approach we use on the file level (no
595 // re-indentation if there was a structural error) within the macro
596 // definition.
Manuel Klimek1abf7892013-01-04 23:34:14 +0000597 parseFile();
598}
599
600void UnwrappedLineParser::parsePPUnknown() {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000601 do {
Manuel Klimeka71e5d82013-01-02 16:30:12 +0000602 nextToken();
603 } while (!eof());
604 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +0000605}
606
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000607// Here we blacklist certain tokens that are not usually the first token in an
608// unwrapped line. This is used in attempt to distinguish macro calls without
609// trailing semicolons from other constructs split to several lines.
Benjamin Kramer8407df72015-03-09 16:47:52 +0000610static bool tokenCanStartNewLine(const clang::Token &Tok) {
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000611 // Semicolon can be a null-statement, l_square can be a start of a macro or
612 // a C++11 attribute, but this doesn't seem to be common.
613 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
614 Tok.isNot(tok::l_square) &&
615 // Tokens that can only be used as binary operators and a part of
616 // overloaded operator names.
617 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
618 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
619 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
620 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
621 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
622 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
623 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
624 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
625 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
626 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
627 Tok.isNot(tok::lesslessequal) &&
628 // Colon is used in labels, base class lists, initializer lists,
629 // range-based for loops, ternary operator, but should never be the
630 // first token in an unwrapped line.
Daniel Jasper5ebb2f32014-05-21 13:08:17 +0000631 Tok.isNot(tok::colon) &&
632 // 'noexcept' is a trailing annotation.
633 Tok.isNot(tok::kw_noexcept);
Alexander Kornienkoa04e5e22013-04-09 16:15:19 +0000634}
635
Manuel Klimek6b9eeba2013-01-07 14:56:16 +0000636void UnwrappedLineParser::parseStructuralElement() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000637 assert(!FormatTok->Tok.is(tok::l_brace));
638 switch (FormatTok->Tok.getKind()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000639 case tok::at:
640 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000641 if (FormatTok->Tok.is(tok::l_brace)) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000642 parseBracedList();
643 break;
644 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000645 switch (FormatTok->Tok.getObjCKeywordID()) {
Nico Weber04e9f1a2013-01-07 19:05:19 +0000646 case tok::objc_public:
647 case tok::objc_protected:
648 case tok::objc_package:
649 case tok::objc_private:
650 return parseAccessSpecifier();
Nico Weber7eecf4b2013-01-09 20:25:35 +0000651 case tok::objc_interface:
Nico Weber2ce0ac52013-01-09 23:25:37 +0000652 case tok::objc_implementation:
653 return parseObjCInterfaceOrImplementation();
Nico Weber8696a8d2013-01-09 21:15:03 +0000654 case tok::objc_protocol:
655 return parseObjCProtocol();
Nico Weberd8ffe752013-01-09 21:42:32 +0000656 case tok::objc_end:
657 return; // Handled by the caller.
Nico Weber51306d22013-01-10 00:25:19 +0000658 case tok::objc_optional:
659 case tok::objc_required:
660 nextToken();
661 addUnwrappedLine();
662 return;
Nico Weber33381f52015-02-07 01:57:32 +0000663 case tok::objc_try:
664 // This branch isn't strictly necessary (the kw_try case below would
665 // do this too after the tok::at is parsed above). But be explicit.
666 parseTryCatch();
667 return;
Nico Weber04e9f1a2013-01-07 19:05:19 +0000668 default:
669 break;
670 }
671 break;
Daniel Jasper8f463652014-08-26 23:15:12 +0000672 case tok::kw_asm:
Daniel Jasper8f463652014-08-26 23:15:12 +0000673 nextToken();
674 if (FormatTok->is(tok::l_brace)) {
Daniel Jasper2337f282015-01-12 10:14:56 +0000675 nextToken();
Daniel Jasper4429f142014-08-27 17:16:46 +0000676 while (FormatTok && FormatTok->isNot(tok::eof)) {
Daniel Jasper8f463652014-08-26 23:15:12 +0000677 if (FormatTok->is(tok::r_brace)) {
678 nextToken();
679 break;
680 }
Daniel Jasper2337f282015-01-12 10:14:56 +0000681 FormatTok->Finalized = true;
Daniel Jasper8f463652014-08-26 23:15:12 +0000682 nextToken();
683 }
684 }
685 break;
Alexander Kornienko578fdd82012-12-06 18:03:27 +0000686 case tok::kw_namespace:
687 parseNamespace();
688 return;
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000689 case tok::kw_inline:
690 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000691 if (FormatTok->Tok.is(tok::kw_namespace)) {
Dmitri Gribenko58d64e22012-12-30 21:27:25 +0000692 parseNamespace();
693 return;
694 }
695 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000696 case tok::kw_public:
697 case tok::kw_protected:
698 case tok::kw_private:
Daniel Jasper83709082015-02-18 17:14:05 +0000699 if (Style.Language == FormatStyle::LK_Java ||
700 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +0000701 nextToken();
702 else
703 parseAccessSpecifier();
Daniel Jasperf7935112012-12-03 18:12:45 +0000704 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000705 case tok::kw_if:
706 parseIfThenElse();
Daniel Jasperf7935112012-12-03 18:12:45 +0000707 return;
Alexander Kornienko37d6c942012-12-05 15:06:06 +0000708 case tok::kw_for:
709 case tok::kw_while:
710 parseForOrWhileLoop();
711 return;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000712 case tok::kw_do:
713 parseDoWhile();
714 return;
715 case tok::kw_switch:
716 parseSwitch();
717 return;
718 case tok::kw_default:
719 nextToken();
720 parseLabel();
721 return;
722 case tok::kw_case:
723 parseCaseLabel();
724 return;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000725 case tok::kw_try:
Nico Weberfac23712015-02-04 15:26:27 +0000726 case tok::kw___try:
Daniel Jasper04a71a42014-05-08 11:58:24 +0000727 parseTryCatch();
728 return;
Manuel Klimekae610d12013-01-21 14:32:05 +0000729 case tok::kw_extern:
730 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000731 if (FormatTok->Tok.is(tok::string_literal)) {
Manuel Klimekae610d12013-01-21 14:32:05 +0000732 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000733 if (FormatTok->Tok.is(tok::l_brace)) {
Daniel Jasper65ee3472013-07-31 23:16:02 +0000734 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
Manuel Klimekae610d12013-01-21 14:32:05 +0000735 addUnwrappedLine();
736 return;
737 }
738 }
Daniel Jaspere1e43192014-04-01 12:55:11 +0000739 break;
Daniel Jasperfca735c2015-02-19 16:14:18 +0000740 case tok::kw_export:
741 if (Style.Language == FormatStyle::LK_JavaScript) {
742 parseJavaScriptEs6ImportExport();
743 return;
744 }
745 break;
Daniel Jaspere1e43192014-04-01 12:55:11 +0000746 case tok::identifier:
747 if (FormatTok->IsForEachMacro) {
748 parseForOrWhileLoop();
749 return;
750 }
Daniel Jasper354aa512015-02-19 16:07:32 +0000751 if (Style.Language == FormatStyle::LK_JavaScript &&
752 FormatTok->is(Keywords.kw_import)) {
Daniel Jasperfca735c2015-02-19 16:14:18 +0000753 parseJavaScriptEs6ImportExport();
Daniel Jasper354aa512015-02-19 16:07:32 +0000754 return;
755 }
Daniel Jasper53395402015-04-07 15:04:40 +0000756 if (FormatTok->is(Keywords.kw_signals)) {
757 parseAccessSpecifier();
758 return;
759 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000760 // In all other cases, parse the declaration.
761 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000762 default:
763 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000764 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000765 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000766 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000767 case tok::at:
768 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000769 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000770 parseBracedList();
771 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000772 case tok::kw_enum:
773 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000774 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000775 case tok::kw_typedef:
776 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000777 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
778 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000779 parseEnum();
780 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000781 case tok::kw_struct:
782 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000783 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000784 parseRecord();
785 // A record declaration or definition is always the start of a structural
786 // element.
787 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000788 case tok::period:
789 nextToken();
790 // In Java, classes have an implicit static member "class".
791 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
792 FormatTok->is(tok::kw_class))
793 nextToken();
794 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000795 case tok::semi:
796 nextToken();
797 addUnwrappedLine();
798 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000799 case tok::r_brace:
800 addUnwrappedLine();
801 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000802 case tok::l_paren:
803 parseParens();
804 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000805 case tok::caret:
806 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000807 if (FormatTok->Tok.isAnyIdentifier() ||
808 FormatTok->isSimpleTypeSpecifier())
809 nextToken();
810 if (FormatTok->is(tok::l_paren))
811 parseParens();
812 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000813 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000814 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000815 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000816 if (!tryToParseBracedList()) {
817 // A block outside of parentheses must be the last part of a
818 // structural element.
819 // FIXME: Figure out cases where this is not true, and add projections
820 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000821 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000822 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000823 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000824 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000825 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000826 return;
827 }
828 // Otherwise this was a braced init list, and the structural
829 // element continues.
830 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000831 case tok::kw_try:
832 // We arrive here when parsing function-try blocks.
833 parseTryCatch();
834 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000835 case tok::identifier: {
836 StringRef Text = FormatTok->TokenText;
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000837 // Parse function literal unless 'function' is the first token in a line
838 // in which case this should be treated as a free-standing function.
839 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
840 Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000841 tryToParseJSFunction();
842 break;
843 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000844 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +0000845 if (Line->Tokens.size() == 1 &&
846 // JS doesn't have macros, and within classes colons indicate fields,
847 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +0000848 Style.Language != FormatStyle::LK_JavaScript) {
849 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000850 parseLabel();
851 return;
852 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000853 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +0000854 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +0000855 bool FunctionLike = FormatTok->is(tok::l_paren);
856 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000857 parseParens();
Daniel Jasper680b09b2014-11-05 10:48:04 +0000858 if (FormatTok->NewlinesBefore > 0 &&
859 (Text.size() >= 5 || FunctionLike) &&
860 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000861 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000862 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000863 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000864 }
865 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000866 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000867 case tok::equal:
868 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000869 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000870 parseBracedList();
871 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000872 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000873 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000874 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000875 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +0000876 case tok::kw_new:
877 parseNew();
878 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000879 default:
880 nextToken();
881 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000882 }
883 } while (!eof());
884}
885
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000886bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000887 // FIXME: This is a dirty way to access the previous token. Find a better
888 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000889 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000890 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
891 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000892 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000893 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000894 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000895 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000896 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000897 assert(FormatTok->is(tok::l_square));
898 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000899 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000900 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000901
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000902 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000903 if (FormatTok->isSimpleTypeSpecifier()) {
904 nextToken();
905 continue;
906 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000907 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000908 case tok::l_brace:
909 break;
910 case tok::l_paren:
911 parseParens();
912 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000913 case tok::amp:
914 case tok::star:
915 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000916 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000917 case tok::less:
918 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000919 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000920 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000921 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000922 nextToken();
923 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000924 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000925 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000926 nextToken();
927 break;
928 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000929 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000930 }
931 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000932 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000933 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000934 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000935}
936
937bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
938 nextToken();
939 if (FormatTok->is(tok::equal)) {
940 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000941 if (FormatTok->is(tok::r_square)) {
942 nextToken();
943 return true;
944 }
945 if (FormatTok->isNot(tok::comma))
946 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000947 nextToken();
948 } else if (FormatTok->is(tok::amp)) {
949 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000950 if (FormatTok->is(tok::r_square)) {
951 nextToken();
952 return true;
953 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000954 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
955 return false;
956 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000957 if (FormatTok->is(tok::comma))
958 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000959 } else if (FormatTok->is(tok::r_square)) {
960 nextToken();
961 return true;
962 }
963 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000964 if (FormatTok->is(tok::amp))
965 nextToken();
966 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
967 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000968 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000969 if (FormatTok->is(tok::ellipsis))
970 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000971 if (FormatTok->is(tok::comma)) {
972 nextToken();
973 } else if (FormatTok->is(tok::r_square)) {
974 nextToken();
975 return true;
976 } else {
977 return false;
978 }
979 } while (!eof());
980 return false;
981}
982
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000983void UnwrappedLineParser::tryToParseJSFunction() {
984 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000985
986 // Consume function name.
987 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +0000988 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000989
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000990 if (FormatTok->isNot(tok::l_paren))
991 return;
992 nextToken();
993 while (FormatTok->isNot(tok::l_brace)) {
994 // Err on the side of caution in order to avoid consuming the full file in
995 // case of incomplete code.
996 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
997 tok::comment))
998 return;
999 nextToken();
1000 }
1001 parseChildBlock();
1002}
1003
Manuel Klimekab419912013-05-23 09:41:43 +00001004bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001005 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +00001006 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001007 assert(FormatTok->BlockKind != BK_Unknown);
1008 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001009 return false;
1010 parseBracedList();
1011 return true;
1012}
1013
Daniel Jasper015ed022013-09-13 09:20:45 +00001014bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1015 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001016 nextToken();
1017
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001018 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1019 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001020 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001021 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001022 FormatTok->is(Keywords.kw_function)) {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001023 tryToParseJSFunction();
1024 continue;
1025 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001026 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001027 case tok::caret:
1028 nextToken();
1029 if (FormatTok->is(tok::l_brace)) {
1030 parseChildBlock();
1031 }
1032 break;
1033 case tok::l_square:
1034 tryToParseLambda();
1035 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001036 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001037 // Assume there are no blocks inside a braced init list apart
1038 // from the ones we explicitly parse out (like lambdas).
1039 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001040 parseBracedList();
1041 break;
Daniel Jasperf46dec82015-03-31 14:34:15 +00001042 case tok::r_paren:
1043 // JavaScript can just have free standing methods and getters/setters in
1044 // object literals. Detect them by a "{" following ")".
1045 if (Style.Language == FormatStyle::LK_JavaScript) {
1046 nextToken();
1047 if (FormatTok->is(tok::l_brace))
1048 parseChildBlock();
1049 break;
1050 }
1051 nextToken();
1052 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001053 case tok::r_brace:
1054 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001055 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001056 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001057 HasError = true;
1058 if (!ContinueOnSemicolons)
1059 return !HasError;
1060 nextToken();
1061 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001062 case tok::comma:
1063 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001064 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001065 default:
1066 nextToken();
1067 break;
1068 }
1069 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001070 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001071}
1072
Daniel Jasperf7935112012-12-03 18:12:45 +00001073void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001074 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001075 nextToken();
1076 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001077 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001078 case tok::l_paren:
1079 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001080 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1081 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001082 break;
1083 case tok::r_paren:
1084 nextToken();
1085 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001086 case tok::r_brace:
1087 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1088 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001089 case tok::l_square:
1090 tryToParseLambda();
1091 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001092 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001093 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001094 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001095 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001096 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001097 case tok::at:
1098 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001099 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001100 parseBracedList();
1101 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001102 case tok::identifier:
1103 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001104 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001105 tryToParseJSFunction();
1106 else
1107 nextToken();
1108 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001109 default:
1110 nextToken();
1111 break;
1112 }
1113 } while (!eof());
1114}
1115
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001116void UnwrappedLineParser::parseSquare() {
1117 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1118 if (tryToParseLambda())
1119 return;
1120 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001121 switch (FormatTok->Tok.getKind()) {
1122 case tok::l_paren:
1123 parseParens();
1124 break;
1125 case tok::r_square:
1126 nextToken();
1127 return;
1128 case tok::r_brace:
1129 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1130 return;
1131 case tok::l_square:
1132 parseSquare();
1133 break;
1134 case tok::l_brace: {
1135 if (!tryToParseBracedList()) {
1136 parseChildBlock();
1137 }
1138 break;
1139 }
1140 case tok::at:
1141 nextToken();
1142 if (FormatTok->Tok.is(tok::l_brace))
1143 parseBracedList();
1144 break;
1145 default:
1146 nextToken();
1147 break;
1148 }
1149 } while (!eof());
1150}
1151
Daniel Jasperf7935112012-12-03 18:12:45 +00001152void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001153 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001154 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001155 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001156 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001157 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001158 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001159 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001160 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001161 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1162 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001163 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001164 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001165 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001166 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001167 } else {
1168 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001169 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001170 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001171 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001172 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001173 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001174 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1175 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001176 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001177 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001178 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001179 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001180 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001181 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001182 parseIfThenElse();
1183 } else {
1184 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001185 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001186 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001187 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001188 }
1189 } else if (NeedsUnwrappedLine) {
1190 addUnwrappedLine();
1191 }
1192}
1193
Daniel Jasper04a71a42014-05-08 11:58:24 +00001194void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001195 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001196 nextToken();
1197 bool NeedsUnwrappedLine = false;
1198 if (FormatTok->is(tok::colon)) {
1199 // We are in a function try block, what comes is an initializer list.
1200 nextToken();
1201 while (FormatTok->is(tok::identifier)) {
1202 nextToken();
1203 if (FormatTok->is(tok::l_paren))
1204 parseParens();
1205 else
1206 StructuralError = true;
1207 if (FormatTok->is(tok::comma))
1208 nextToken();
1209 }
1210 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001211 // Parse try with resource.
1212 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1213 parseParens();
1214 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001215 if (FormatTok->is(tok::l_brace)) {
1216 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1217 parseBlock(/*MustBeDeclaration=*/false);
1218 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1219 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1220 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1221 addUnwrappedLine();
1222 } else {
1223 NeedsUnwrappedLine = true;
1224 }
1225 } else if (!FormatTok->is(tok::kw_catch)) {
1226 // The C++ standard requires a compound-statement after a try.
1227 // If there's none, we try to assume there's a structuralElement
1228 // and try to continue.
1229 StructuralError = true;
1230 addUnwrappedLine();
1231 ++Line->Level;
1232 parseStructuralElement();
1233 --Line->Level;
1234 }
Nico Weber33381f52015-02-07 01:57:32 +00001235 while (1) {
1236 if (FormatTok->is(tok::at))
1237 nextToken();
1238 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1239 tok::kw___finally) ||
1240 ((Style.Language == FormatStyle::LK_Java ||
1241 Style.Language == FormatStyle::LK_JavaScript) &&
1242 FormatTok->is(Keywords.kw_finally)) ||
1243 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1244 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1245 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001246 nextToken();
1247 while (FormatTok->isNot(tok::l_brace)) {
1248 if (FormatTok->is(tok::l_paren)) {
1249 parseParens();
1250 continue;
1251 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001252 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001253 return;
1254 nextToken();
1255 }
1256 NeedsUnwrappedLine = false;
1257 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1258 parseBlock(/*MustBeDeclaration=*/false);
1259 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1260 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1261 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1262 addUnwrappedLine();
1263 } else {
1264 NeedsUnwrappedLine = true;
1265 }
1266 }
1267 if (NeedsUnwrappedLine) {
1268 addUnwrappedLine();
1269 }
1270}
1271
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001272void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001273 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001274
1275 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001276 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001277 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001278 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001279 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001280 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001281 addUnwrappedLine();
1282
Daniel Jasper65ee3472013-07-31 23:16:02 +00001283 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1284 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1285 DeclarationScopeStack.size() > 1);
1286 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001287 // Munch the semicolon after a namespace. This is more common than one would
1288 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001289 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001290 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001291 addUnwrappedLine();
1292 }
1293 // FIXME: Add error handling.
1294}
1295
Daniel Jasper6acf5132015-03-12 14:44:29 +00001296void UnwrappedLineParser::parseNew() {
1297 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1298 nextToken();
1299 if (Style.Language != FormatStyle::LK_Java)
1300 return;
1301
1302 // In Java, we can parse everything up to the parens, which aren't optional.
1303 do {
1304 // There should not be a ;, { or } before the new's open paren.
1305 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1306 return;
1307
1308 // Consume the parens.
1309 if (FormatTok->is(tok::l_paren)) {
1310 parseParens();
1311
1312 // If there is a class body of an anonymous class, consume that as child.
1313 if (FormatTok->is(tok::l_brace))
1314 parseChildBlock();
1315 return;
1316 }
1317 nextToken();
1318 } while (!eof());
1319}
1320
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001321void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001322 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1323 FormatTok->IsForEachMacro) &&
1324 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001325 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001326 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001327 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001328 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001329 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001330 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001331 addUnwrappedLine();
1332 } else {
1333 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001334 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001335 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001336 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001337 }
1338}
1339
Daniel Jasperf7935112012-12-03 18:12:45 +00001340void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001341 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001342 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001343 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001344 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001345 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001346 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1347 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001348 } else {
1349 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001350 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001351 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001352 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001353 }
1354
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001355 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001356 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001357 addUnwrappedLine();
1358 return;
1359 }
1360
Daniel Jasperf7935112012-12-03 18:12:45 +00001361 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001362 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001363}
1364
1365void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001366 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001367 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001368 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001369 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001370 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001371 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001372 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001373 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001374 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1375 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1376 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001377 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001378 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001379 parseStructuralElement();
1380 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001381 addUnwrappedLine();
1382 } else {
1383 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001384 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001385 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001386}
1387
1388void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001389 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001390 // FIXME: fix handling of complex expressions here.
1391 do {
1392 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001393 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001394 parseLabel();
1395}
1396
1397void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001398 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001399 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001400 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001401 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001402 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001403 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001404 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001405 addUnwrappedLine();
1406 } else {
1407 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001408 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001409 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001410 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001411 }
1412}
1413
1414void UnwrappedLineParser::parseAccessSpecifier() {
1415 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001416 // Understand Qt's slots.
Daniel Jasper53395402015-04-07 15:04:40 +00001417 if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001418 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001419 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001420 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001421 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001422 addUnwrappedLine();
1423}
1424
1425void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001426 // Won't be 'enum' for NS_ENUMs.
1427 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001428 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001429
Daniel Jasper2b41a822013-08-20 12:42:50 +00001430 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001431 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1432 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001433 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001434 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1435 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001436 nextToken();
1437 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001438 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001439 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001440 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001441 nextToken();
1442 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001443
1444 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001445 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001446 return;
1447 FormatTok->BlockKind = BK_Block;
1448
1449 if (Style.Language == FormatStyle::LK_Java) {
1450 // Java enums are different.
1451 parseJavaEnumBody();
1452 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001453 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001454
1455 // Parse enum body.
1456 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1457 if (HasError) {
1458 if (FormatTok->is(tok::semi))
1459 nextToken();
1460 addUnwrappedLine();
1461 }
1462
Manuel Klimek2cec0192013-01-21 19:17:52 +00001463 // We fall through to parsing a structural element afterwards, so that in
1464 // enum A {} n, m;
1465 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001466}
1467
1468void UnwrappedLineParser::parseJavaEnumBody() {
1469 // Determine whether the enum is simple, i.e. does not have a semicolon or
1470 // constants with class bodies. Simple enums can be formatted like braced
1471 // lists, contracted to a single line, etc.
1472 unsigned StoredPosition = Tokens->getPosition();
1473 bool IsSimple = true;
1474 FormatToken *Tok = Tokens->getNextToken();
1475 while (Tok) {
1476 if (Tok->is(tok::r_brace))
1477 break;
1478 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1479 IsSimple = false;
1480 break;
1481 }
1482 // FIXME: This will also mark enums with braces in the arguments to enum
1483 // constants as "not simple". This is probably fine in practice, though.
1484 Tok = Tokens->getNextToken();
1485 }
1486 FormatTok = Tokens->setPosition(StoredPosition);
1487
1488 if (IsSimple) {
1489 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001490 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001491 return;
1492 }
1493
1494 // Parse the body of a more complex enum.
1495 // First add a line for everything up to the "{".
1496 nextToken();
1497 addUnwrappedLine();
1498 ++Line->Level;
1499
1500 // Parse the enum constants.
1501 while (FormatTok) {
1502 if (FormatTok->is(tok::l_brace)) {
1503 // Parse the constant's class body.
1504 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1505 /*MunchSemi=*/false);
1506 } else if (FormatTok->is(tok::l_paren)) {
1507 parseParens();
1508 } else if (FormatTok->is(tok::comma)) {
1509 nextToken();
1510 addUnwrappedLine();
1511 } else if (FormatTok->is(tok::semi)) {
1512 nextToken();
1513 addUnwrappedLine();
1514 break;
1515 } else if (FormatTok->is(tok::r_brace)) {
1516 addUnwrappedLine();
1517 break;
1518 } else {
1519 nextToken();
1520 }
1521 }
1522
1523 // Parse the class body after the enum's ";" if any.
1524 parseLevel(/*HasOpeningBrace=*/true);
1525 nextToken();
1526 --Line->Level;
1527 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001528}
1529
Manuel Klimeke01bab52013-01-15 13:38:33 +00001530void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001531 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001532 nextToken();
Manuel Klimek45bf56c2014-07-31 07:19:30 +00001533 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1534 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001535 nextToken();
1536 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001537 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001538 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001539 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001540 // The actual identifier can be a nested name specifier, and in macros
1541 // it is often token-pasted.
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001542 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1543 FormatTok->is(tok::hashhash) ||
1544 (Style.Language == FormatStyle::LK_Java &&
1545 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001546 nextToken();
1547
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001548 // Note that parsing away template declarations here leads to incorrectly
1549 // accepting function declarations as record declarations.
1550 // In general, we cannot solve this problem. Consider:
1551 // class A<int> B() {}
1552 // which can be a function definition or a class definition when B() is a
1553 // macro. If we find enough real-world cases where this is a problem, we
1554 // can parse for the 'template' keyword in the beginning of the statement,
1555 // and thus rule out the record production in case there is no template
1556 // (this would still leave us with an ambiguity between template function
1557 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001558 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1559 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1560 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001561 return;
1562 nextToken();
1563 }
1564 }
1565 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001566 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001567 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001568 addUnwrappedLine();
1569
Daniel Jasper240dfda2014-03-31 14:23:49 +00001570 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001571 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001572 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001573 // We fall through to parsing a structural element afterwards, so
1574 // class A {} n, m;
1575 // will end up in one unwrapped line.
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001576 // This does not apply for Java.
Daniel Jasper6fa9ec72015-02-19 16:03:16 +00001577 if (Style.Language == FormatStyle::LK_Java ||
1578 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001579 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001580}
1581
Nico Weber8696a8d2013-01-09 21:15:03 +00001582void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001583 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001584 do
1585 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001586 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001587 nextToken(); // Skip '>'.
1588}
1589
1590void UnwrappedLineParser::parseObjCUntilAtEnd() {
1591 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001592 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001593 nextToken();
1594 addUnwrappedLine();
1595 break;
1596 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001597 if (FormatTok->is(tok::l_brace)) {
1598 parseBlock(/*MustBeDeclaration=*/false);
1599 // In ObjC interfaces, nothing should be following the "}".
1600 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001601 } else if (FormatTok->is(tok::r_brace)) {
1602 // Ignore stray "}". parseStructuralElement doesn't consume them.
1603 nextToken();
1604 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001605 } else {
1606 parseStructuralElement();
1607 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001608 } while (!eof());
1609}
1610
Nico Weber2ce0ac52013-01-09 23:25:37 +00001611void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001612 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001613 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001614
1615 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001616 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001617 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001618 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001619 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001620 // Skip category, if present.
1621 parseParens();
1622
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001623 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001624 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001625
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001626 if (FormatTok->Tok.is(tok::l_brace)) {
1627 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1628 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1629 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001630 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001631 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001632
1633 // With instance variables, this puts '}' on its own line. Without instance
1634 // variables, this ends the @interface line.
1635 addUnwrappedLine();
1636
Nico Weber8696a8d2013-01-09 21:15:03 +00001637 parseObjCUntilAtEnd();
1638}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001639
Nico Weber8696a8d2013-01-09 21:15:03 +00001640void UnwrappedLineParser::parseObjCProtocol() {
1641 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001642 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001643
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001644 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001645 parseObjCProtocolList();
1646
1647 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001648 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001649 nextToken();
1650 return addUnwrappedLine();
1651 }
1652
1653 addUnwrappedLine();
1654 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001655}
1656
Daniel Jasperfca735c2015-02-19 16:14:18 +00001657void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1658 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00001659 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00001660
1661 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_function,
1662 Keywords.kw_var))
1663 return; // Fall through to parsing the corresponding structure.
1664
1665 if (FormatTok->is(tok::kw_default)) {
1666 nextToken(); // export default ..., fall through after eating 'default'.
1667 return;
1668 }
1669
Daniel Jasper354aa512015-02-19 16:07:32 +00001670 if (FormatTok->is(tok::l_brace)) {
1671 FormatTok->BlockKind = BK_Block;
1672 parseBracedList();
1673 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001674
Daniel Jasper354aa512015-02-19 16:07:32 +00001675 while (!eof() && FormatTok->isNot(tok::semi) &&
1676 FormatTok->isNot(tok::l_brace)) {
1677 nextToken();
1678 }
1679}
1680
Daniel Jasper3b203a62013-09-05 16:05:56 +00001681LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1682 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001683 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1684 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1685 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1686 E = Line.Tokens.end();
1687 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001688 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001689 }
1690 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1691 E = Line.Tokens.end();
1692 I != E; ++I) {
1693 const UnwrappedLineNode &Node = *I;
1694 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1695 I = Node.Children.begin(),
1696 E = Node.Children.end();
1697 I != E; ++I) {
1698 printDebugInfo(*I, "\nChild: ");
1699 }
1700 }
1701 llvm::dbgs() << "\n";
1702}
1703
Daniel Jasperf7935112012-12-03 18:12:45 +00001704void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001705 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001706 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001707 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001708 if (CurrentLines == &Lines)
1709 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001710 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001711 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001712 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001713 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001714 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001715 I = PreprocessorDirectives.begin(),
1716 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001717 I != E; ++I) {
1718 CurrentLines->push_back(*I);
1719 }
1720 PreprocessorDirectives.clear();
1721 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001722}
1723
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001724bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001725
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001726bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001727 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1728 FormatTok.NewlinesBefore > 0;
1729}
1730
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001731void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1732 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001733 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001734 I = CommentsBeforeNextToken.begin(),
1735 E = CommentsBeforeNextToken.end();
1736 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001737 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001738 addUnwrappedLine();
1739 }
1740 pushToken(*I);
1741 }
1742 if (NewlineBeforeNext && JustComments) {
1743 addUnwrappedLine();
1744 }
1745 CommentsBeforeNextToken.clear();
1746}
1747
Daniel Jasperf7935112012-12-03 18:12:45 +00001748void UnwrappedLineParser::nextToken() {
1749 if (eof())
1750 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001751 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001752 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001753 readToken();
1754}
1755
1756void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001757 bool CommentsInCurrentLine = true;
1758 do {
1759 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001760 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001761 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1762 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001763 // If there is an unfinished unwrapped line, we flush the preprocessor
1764 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001765 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001766 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001767 // Comments stored before the preprocessor directive need to be output
1768 // before the preprocessor directive, at the same level as the
1769 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001770 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001771 parsePPDirective();
1772 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001773 while (FormatTok->Type == TT_ConflictStart ||
1774 FormatTok->Type == TT_ConflictEnd ||
1775 FormatTok->Type == TT_ConflictAlternative) {
1776 if (FormatTok->Type == TT_ConflictStart) {
1777 conditionalCompilationStart(/*Unreachable=*/false);
1778 } else if (FormatTok->Type == TT_ConflictAlternative) {
1779 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001780 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001781 conditionalCompilationEnd();
1782 }
1783 FormatTok = Tokens->getNextToken();
1784 FormatTok->MustBreakBefore = true;
1785 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001786
1787 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1788 !Line->InPPDirective) {
1789 continue;
1790 }
1791
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001792 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001793 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001794 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001795 CommentsInCurrentLine = false;
1796 }
1797 if (CommentsInCurrentLine) {
1798 pushToken(FormatTok);
1799 } else {
1800 CommentsBeforeNextToken.push_back(FormatTok);
1801 }
1802 } while (!eof());
1803}
1804
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001805void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001806 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001807 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001808 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001809 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001810 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001811}
1812
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001813} // end namespace format
1814} // end namespace clang