blob: 3df7839b79b9365266c1c3c62e93ff4e5e221309 [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 }
Manuel Klimekae610d12013-01-21 14:32:05 +0000756 // In all other cases, parse the declaration.
757 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000758 default:
759 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000760 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000761 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000762 switch (FormatTok->Tok.getKind()) {
Nico Weber372d8dc2013-02-10 20:35:35 +0000763 case tok::at:
764 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000765 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +0000766 parseBracedList();
767 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000768 case tok::kw_enum:
769 parseEnum();
Manuel Klimek2cec0192013-01-21 19:17:52 +0000770 break;
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000771 case tok::kw_typedef:
772 nextToken();
Daniel Jasper31f6c542014-12-05 10:42:21 +0000773 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
774 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
Daniel Jaspera88f80a2014-01-30 14:38:37 +0000775 parseEnum();
776 break;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000777 case tok::kw_struct:
778 case tok::kw_union:
Manuel Klimek28cacc72013-01-07 18:10:23 +0000779 case tok::kw_class:
Manuel Klimeke01bab52013-01-15 13:38:33 +0000780 parseRecord();
781 // A record declaration or definition is always the start of a structural
782 // element.
783 break;
Daniel Jaspere5d74862014-11-26 08:17:08 +0000784 case tok::period:
785 nextToken();
786 // In Java, classes have an implicit static member "class".
787 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
788 FormatTok->is(tok::kw_class))
789 nextToken();
790 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000791 case tok::semi:
792 nextToken();
793 addUnwrappedLine();
794 return;
Alexander Kornienko1231e062013-01-16 11:43:46 +0000795 case tok::r_brace:
796 addUnwrappedLine();
797 return;
Daniel Jasperf7935112012-12-03 18:12:45 +0000798 case tok::l_paren:
799 parseParens();
800 break;
Manuel Klimek516e0542013-09-04 13:25:30 +0000801 case tok::caret:
802 nextToken();
Daniel Jasper395193c2014-03-28 07:48:59 +0000803 if (FormatTok->Tok.isAnyIdentifier() ||
804 FormatTok->isSimpleTypeSpecifier())
805 nextToken();
806 if (FormatTok->is(tok::l_paren))
807 parseParens();
808 if (FormatTok->is(tok::l_brace))
Manuel Klimek516e0542013-09-04 13:25:30 +0000809 parseChildBlock();
Manuel Klimek516e0542013-09-04 13:25:30 +0000810 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000811 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +0000812 if (!tryToParseBracedList()) {
813 // A block outside of parentheses must be the last part of a
814 // structural element.
815 // FIXME: Figure out cases where this is not true, and add projections
816 // for them (the one we know is missing are lambdas).
Alexander Kornienko3a33f022013-12-12 09:49:52 +0000817 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
Manuel Klimekab419912013-05-23 09:41:43 +0000818 addUnwrappedLine();
Alexander Kornienko3cfa9732013-11-20 16:33:05 +0000819 FormatTok->Type = TT_FunctionLBrace;
Nico Weber9096fc02013-06-26 00:30:14 +0000820 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +0000821 addUnwrappedLine();
Manuel Klimekab419912013-05-23 09:41:43 +0000822 return;
823 }
824 // Otherwise this was a braced init list, and the structural
825 // element continues.
826 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +0000827 case tok::kw_try:
828 // We arrive here when parsing function-try blocks.
829 parseTryCatch();
830 return;
Daniel Jasper40e19212013-05-29 13:16:10 +0000831 case tok::identifier: {
832 StringRef Text = FormatTok->TokenText;
Daniel Jasperad9eb0d2014-06-30 13:24:54 +0000833 // Parse function literal unless 'function' is the first token in a line
834 // in which case this should be treated as a free-standing function.
835 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
836 Line->Tokens.size() > 0) {
Daniel Jasper069e5f42014-05-20 11:14:57 +0000837 tryToParseJSFunction();
838 break;
839 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000840 nextToken();
Daniel Jasper83709082015-02-18 17:14:05 +0000841 if (Line->Tokens.size() == 1 &&
842 // JS doesn't have macros, and within classes colons indicate fields,
843 // not labels.
Daniel Jasper676e5162015-04-07 14:36:33 +0000844 Style.Language != FormatStyle::LK_JavaScript) {
845 if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
Alexander Kornienkode644272013-04-08 22:16:06 +0000846 parseLabel();
847 return;
848 }
Daniel Jasper680b09b2014-11-05 10:48:04 +0000849 // Recognize function-like macro usages without trailing semicolon as
Daniel Jasper83709082015-02-18 17:14:05 +0000850 // well as free-standing macros like Q_OBJECT.
Daniel Jasper680b09b2014-11-05 10:48:04 +0000851 bool FunctionLike = FormatTok->is(tok::l_paren);
852 if (FunctionLike)
Alexander Kornienkode644272013-04-08 22:16:06 +0000853 parseParens();
Daniel Jasper680b09b2014-11-05 10:48:04 +0000854 if (FormatTok->NewlinesBefore > 0 &&
855 (Text.size() >= 5 || FunctionLike) &&
856 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
Daniel Jasper40e19212013-05-29 13:16:10 +0000857 addUnwrappedLine();
Daniel Jasper41a0f782013-05-29 14:09:17 +0000858 return;
Alexander Kornienkode644272013-04-08 22:16:06 +0000859 }
Daniel Jasperf7935112012-12-03 18:12:45 +0000860 }
861 break;
Daniel Jasper40e19212013-05-29 13:16:10 +0000862 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000863 case tok::equal:
864 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +0000865 if (FormatTok->Tok.is(tok::l_brace)) {
Manuel Klimek8e07a1b2013-01-10 11:52:21 +0000866 parseBracedList();
867 }
Daniel Jaspere25509f2012-12-17 11:29:41 +0000868 break;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000869 case tok::l_square:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000870 parseSquare();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000871 break;
Daniel Jasper6acf5132015-03-12 14:44:29 +0000872 case tok::kw_new:
873 parseNew();
874 break;
Alexander Kornienkob7076a22012-12-04 14:46:19 +0000875 default:
876 nextToken();
877 break;
Daniel Jasperf7935112012-12-03 18:12:45 +0000878 }
879 } while (!eof());
880}
881
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000882bool UnwrappedLineParser::tryToParseLambda() {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000883 // FIXME: This is a dirty way to access the previous token. Find a better
884 // solution.
Daniel Jasperb4b99982013-09-06 21:25:51 +0000885 if (!Line->Tokens.empty() &&
Daniel Jasper80222262014-11-02 22:46:42 +0000886 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
887 tok::kw_new, tok::kw_delete) ||
Daniel Jaspera58dd5d2014-03-11 10:03:33 +0000888 Line->Tokens.back().Tok->closesScope() ||
Daniel Jasper6b70ec02014-01-22 17:01:47 +0000889 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000890 nextToken();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000891 return false;
Daniel Jasperbf02b2c12013-09-05 11:49:39 +0000892 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000893 assert(FormatTok->is(tok::l_square));
894 FormatToken &LSquare = *FormatTok;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000895 if (!tryToParseLambdaIntroducer())
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000896 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000897
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +0000898 while (FormatTok->isNot(tok::l_brace)) {
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000899 if (FormatTok->isSimpleTypeSpecifier()) {
900 nextToken();
901 continue;
902 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000903 switch (FormatTok->Tok.getKind()) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000904 case tok::l_brace:
905 break;
906 case tok::l_paren:
907 parseParens();
908 break;
Daniel Jasperbcb55ee2014-11-21 14:08:38 +0000909 case tok::amp:
910 case tok::star:
911 case tok::kw_const:
Daniel Jasper3431b752014-12-08 13:22:37 +0000912 case tok::comma:
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000913 case tok::less:
914 case tok::greater:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000915 case tok::identifier:
Daniel Jasper1067ab02014-02-11 10:16:55 +0000916 case tok::coloncolon:
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000917 case tok::kw_mutable:
Daniel Jasper81a20782014-03-10 10:02:02 +0000918 nextToken();
919 break;
Daniel Jaspercb51cf42014-01-16 09:11:55 +0000920 case tok::arrow:
Daniel Jasper81a20782014-03-10 10:02:02 +0000921 FormatTok->Type = TT_TrailingReturnArrow;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000922 nextToken();
923 break;
924 default:
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000925 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000926 }
927 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000928 LSquare.Type = TT_LambdaLSquare;
Manuel Klimek516e0542013-09-04 13:25:30 +0000929 parseChildBlock();
Daniel Jasperb88b25f2013-12-23 07:29:06 +0000930 return true;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000931}
932
933bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
934 nextToken();
935 if (FormatTok->is(tok::equal)) {
936 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000937 if (FormatTok->is(tok::r_square)) {
938 nextToken();
939 return true;
940 }
941 if (FormatTok->isNot(tok::comma))
942 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000943 nextToken();
944 } else if (FormatTok->is(tok::amp)) {
945 nextToken();
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000946 if (FormatTok->is(tok::r_square)) {
947 nextToken();
948 return true;
949 }
Manuel Klimekffdeb592013-09-03 15:10:01 +0000950 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
951 return false;
952 }
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000953 if (FormatTok->is(tok::comma))
954 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000955 } else if (FormatTok->is(tok::r_square)) {
956 nextToken();
957 return true;
958 }
959 do {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +0000960 if (FormatTok->is(tok::amp))
961 nextToken();
962 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
963 return false;
Manuel Klimekffdeb592013-09-03 15:10:01 +0000964 nextToken();
Daniel Jasperda18fd82014-06-10 06:39:03 +0000965 if (FormatTok->is(tok::ellipsis))
966 nextToken();
Manuel Klimekffdeb592013-09-03 15:10:01 +0000967 if (FormatTok->is(tok::comma)) {
968 nextToken();
969 } else if (FormatTok->is(tok::r_square)) {
970 nextToken();
971 return true;
972 } else {
973 return false;
974 }
975 } while (!eof());
976 return false;
977}
978
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000979void UnwrappedLineParser::tryToParseJSFunction() {
980 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000981
982 // Consume function name.
983 if (FormatTok->is(tok::identifier))
Daniel Jasperfca735c2015-02-19 16:14:18 +0000984 nextToken();
Daniel Jasper5217a8b2014-06-13 07:02:04 +0000985
Daniel Jasperc03e16a2014-05-08 09:25:39 +0000986 if (FormatTok->isNot(tok::l_paren))
987 return;
988 nextToken();
989 while (FormatTok->isNot(tok::l_brace)) {
990 // Err on the side of caution in order to avoid consuming the full file in
991 // case of incomplete code.
992 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
993 tok::comment))
994 return;
995 nextToken();
996 }
997 parseChildBlock();
998}
999
Manuel Klimekab419912013-05-23 09:41:43 +00001000bool UnwrappedLineParser::tryToParseBracedList() {
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001001 if (FormatTok->BlockKind == BK_Unknown)
Manuel Klimekab419912013-05-23 09:41:43 +00001002 calculateBraceTypes();
Daniel Jasperb1f74a82013-07-09 09:06:29 +00001003 assert(FormatTok->BlockKind != BK_Unknown);
1004 if (FormatTok->BlockKind == BK_Block)
Manuel Klimekab419912013-05-23 09:41:43 +00001005 return false;
1006 parseBracedList();
1007 return true;
1008}
1009
Daniel Jasper015ed022013-09-13 09:20:45 +00001010bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
1011 bool HasError = false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001012 nextToken();
1013
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001014 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
1015 // replace this by using parseAssigmentExpression() inside.
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001016 do {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001017 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001018 FormatTok->is(Keywords.kw_function)) {
Daniel Jasperc03e16a2014-05-08 09:25:39 +00001019 tryToParseJSFunction();
1020 continue;
1021 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001022 switch (FormatTok->Tok.getKind()) {
Manuel Klimek516e0542013-09-04 13:25:30 +00001023 case tok::caret:
1024 nextToken();
1025 if (FormatTok->is(tok::l_brace)) {
1026 parseChildBlock();
1027 }
1028 break;
1029 case tok::l_square:
1030 tryToParseLambda();
1031 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001032 case tok::l_brace:
Manuel Klimekbab25fd2013-09-04 08:20:47 +00001033 // Assume there are no blocks inside a braced init list apart
1034 // from the ones we explicitly parse out (like lambdas).
1035 FormatTok->BlockKind = BK_BracedInit;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001036 parseBracedList();
1037 break;
Daniel Jasperf46dec82015-03-31 14:34:15 +00001038 case tok::r_paren:
1039 // JavaScript can just have free standing methods and getters/setters in
1040 // object literals. Detect them by a "{" following ")".
1041 if (Style.Language == FormatStyle::LK_JavaScript) {
1042 nextToken();
1043 if (FormatTok->is(tok::l_brace))
1044 parseChildBlock();
1045 break;
1046 }
1047 nextToken();
1048 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001049 case tok::r_brace:
1050 nextToken();
Daniel Jasper015ed022013-09-13 09:20:45 +00001051 return !HasError;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001052 case tok::semi:
Daniel Jasper015ed022013-09-13 09:20:45 +00001053 HasError = true;
1054 if (!ContinueOnSemicolons)
1055 return !HasError;
1056 nextToken();
1057 break;
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001058 case tok::comma:
1059 nextToken();
Manuel Klimeka3ff45e2013-04-10 09:52:05 +00001060 break;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001061 default:
1062 nextToken();
1063 break;
1064 }
1065 } while (!eof());
Daniel Jasper015ed022013-09-13 09:20:45 +00001066 return false;
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001067}
1068
Daniel Jasperf7935112012-12-03 18:12:45 +00001069void UnwrappedLineParser::parseParens() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001070 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
Daniel Jasperf7935112012-12-03 18:12:45 +00001071 nextToken();
1072 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001073 switch (FormatTok->Tok.getKind()) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001074 case tok::l_paren:
1075 parseParens();
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001076 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1077 parseChildBlock();
Daniel Jasperf7935112012-12-03 18:12:45 +00001078 break;
1079 case tok::r_paren:
1080 nextToken();
1081 return;
Daniel Jasper393564f2013-05-31 14:56:29 +00001082 case tok::r_brace:
1083 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1084 return;
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001085 case tok::l_square:
1086 tryToParseLambda();
1087 break;
Daniel Jasper5f1fa852015-01-04 20:40:51 +00001088 case tok::l_brace:
Manuel Klimekab419912013-05-23 09:41:43 +00001089 if (!tryToParseBracedList()) {
Manuel Klimekf017dc02013-09-04 13:34:14 +00001090 parseChildBlock();
Manuel Klimekab419912013-05-23 09:41:43 +00001091 }
Manuel Klimek8e07a1b2013-01-10 11:52:21 +00001092 break;
Nico Weber372d8dc2013-02-10 20:35:35 +00001093 case tok::at:
1094 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001095 if (FormatTok->Tok.is(tok::l_brace))
Nico Weber372d8dc2013-02-10 20:35:35 +00001096 parseBracedList();
1097 break;
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001098 case tok::identifier:
1099 if (Style.Language == FormatStyle::LK_JavaScript &&
Daniel Jasperd0ec0d62014-11-04 12:41:02 +00001100 FormatTok->is(Keywords.kw_function))
Daniel Jasper3f69ba12014-09-05 08:42:27 +00001101 tryToParseJSFunction();
1102 else
1103 nextToken();
1104 break;
Daniel Jasperf7935112012-12-03 18:12:45 +00001105 default:
1106 nextToken();
1107 break;
1108 }
1109 } while (!eof());
1110}
1111
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001112void UnwrappedLineParser::parseSquare() {
1113 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1114 if (tryToParseLambda())
1115 return;
1116 do {
Daniel Jasperb88b25f2013-12-23 07:29:06 +00001117 switch (FormatTok->Tok.getKind()) {
1118 case tok::l_paren:
1119 parseParens();
1120 break;
1121 case tok::r_square:
1122 nextToken();
1123 return;
1124 case tok::r_brace:
1125 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1126 return;
1127 case tok::l_square:
1128 parseSquare();
1129 break;
1130 case tok::l_brace: {
1131 if (!tryToParseBracedList()) {
1132 parseChildBlock();
1133 }
1134 break;
1135 }
1136 case tok::at:
1137 nextToken();
1138 if (FormatTok->Tok.is(tok::l_brace))
1139 parseBracedList();
1140 break;
1141 default:
1142 nextToken();
1143 break;
1144 }
1145 } while (!eof());
1146}
1147
Daniel Jasperf7935112012-12-03 18:12:45 +00001148void UnwrappedLineParser::parseIfThenElse() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001149 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001150 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001151 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimekadededf2013-01-11 18:28:36 +00001152 parseParens();
Daniel Jasperf7935112012-12-03 18:12:45 +00001153 bool NeedsUnwrappedLine = false;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001154 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001155 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001156 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001157 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1158 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001159 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001160 } else {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001161 NeedsUnwrappedLine = true;
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001162 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001163 } else {
1164 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001165 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001166 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001167 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001168 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001169 if (FormatTok->Tok.is(tok::kw_else)) {
Daniel Jasperd9670872014-08-05 12:06:20 +00001170 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1171 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001172 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001173 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001174 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001175 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001176 addUnwrappedLine();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001177 } else if (FormatTok->Tok.is(tok::kw_if)) {
Daniel Jasperf7935112012-12-03 18:12:45 +00001178 parseIfThenElse();
1179 } else {
1180 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001181 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001182 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001183 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001184 }
1185 } else if (NeedsUnwrappedLine) {
1186 addUnwrappedLine();
1187 }
1188}
1189
Daniel Jasper04a71a42014-05-08 11:58:24 +00001190void UnwrappedLineParser::parseTryCatch() {
Nico Weberfac23712015-02-04 15:26:27 +00001191 assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
Daniel Jasper04a71a42014-05-08 11:58:24 +00001192 nextToken();
1193 bool NeedsUnwrappedLine = false;
1194 if (FormatTok->is(tok::colon)) {
1195 // We are in a function try block, what comes is an initializer list.
1196 nextToken();
1197 while (FormatTok->is(tok::identifier)) {
1198 nextToken();
1199 if (FormatTok->is(tok::l_paren))
1200 parseParens();
1201 else
1202 StructuralError = true;
1203 if (FormatTok->is(tok::comma))
1204 nextToken();
1205 }
1206 }
Daniel Jaspere189d462015-01-14 10:48:41 +00001207 // Parse try with resource.
1208 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1209 parseParens();
1210 }
Daniel Jasper04a71a42014-05-08 11:58:24 +00001211 if (FormatTok->is(tok::l_brace)) {
1212 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1213 parseBlock(/*MustBeDeclaration=*/false);
1214 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1215 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1216 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1217 addUnwrappedLine();
1218 } else {
1219 NeedsUnwrappedLine = true;
1220 }
1221 } else if (!FormatTok->is(tok::kw_catch)) {
1222 // The C++ standard requires a compound-statement after a try.
1223 // If there's none, we try to assume there's a structuralElement
1224 // and try to continue.
1225 StructuralError = true;
1226 addUnwrappedLine();
1227 ++Line->Level;
1228 parseStructuralElement();
1229 --Line->Level;
1230 }
Nico Weber33381f52015-02-07 01:57:32 +00001231 while (1) {
1232 if (FormatTok->is(tok::at))
1233 nextToken();
1234 if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
1235 tok::kw___finally) ||
1236 ((Style.Language == FormatStyle::LK_Java ||
1237 Style.Language == FormatStyle::LK_JavaScript) &&
1238 FormatTok->is(Keywords.kw_finally)) ||
1239 (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
1240 FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
1241 break;
Daniel Jasper04a71a42014-05-08 11:58:24 +00001242 nextToken();
1243 while (FormatTok->isNot(tok::l_brace)) {
1244 if (FormatTok->is(tok::l_paren)) {
1245 parseParens();
1246 continue;
1247 }
Daniel Jasper2bd7a642015-01-19 10:50:51 +00001248 if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
Daniel Jasper04a71a42014-05-08 11:58:24 +00001249 return;
1250 nextToken();
1251 }
1252 NeedsUnwrappedLine = false;
1253 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1254 parseBlock(/*MustBeDeclaration=*/false);
1255 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1256 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1257 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1258 addUnwrappedLine();
1259 } else {
1260 NeedsUnwrappedLine = true;
1261 }
1262 }
1263 if (NeedsUnwrappedLine) {
1264 addUnwrappedLine();
1265 }
1266}
1267
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001268void UnwrappedLineParser::parseNamespace() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001269 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001270
1271 const FormatToken &InitialToken = *FormatTok;
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001272 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001273 if (FormatTok->Tok.is(tok::identifier))
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001274 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001275 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001276 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001277 addUnwrappedLine();
1278
Daniel Jasper65ee3472013-07-31 23:16:02 +00001279 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1280 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1281 DeclarationScopeStack.size() > 1);
1282 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
Manuel Klimek046b9302013-02-06 16:08:09 +00001283 // Munch the semicolon after a namespace. This is more common than one would
1284 // think. Puttin the semicolon into its own line is very ugly.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001285 if (FormatTok->Tok.is(tok::semi))
Manuel Klimek046b9302013-02-06 16:08:09 +00001286 nextToken();
Alexander Kornienko578fdd82012-12-06 18:03:27 +00001287 addUnwrappedLine();
1288 }
1289 // FIXME: Add error handling.
1290}
1291
Daniel Jasper6acf5132015-03-12 14:44:29 +00001292void UnwrappedLineParser::parseNew() {
1293 assert(FormatTok->is(tok::kw_new) && "'new' expected");
1294 nextToken();
1295 if (Style.Language != FormatStyle::LK_Java)
1296 return;
1297
1298 // In Java, we can parse everything up to the parens, which aren't optional.
1299 do {
1300 // There should not be a ;, { or } before the new's open paren.
1301 if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
1302 return;
1303
1304 // Consume the parens.
1305 if (FormatTok->is(tok::l_paren)) {
1306 parseParens();
1307
1308 // If there is a class body of an anonymous class, consume that as child.
1309 if (FormatTok->is(tok::l_brace))
1310 parseChildBlock();
1311 return;
1312 }
1313 nextToken();
1314 } while (!eof());
1315}
1316
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001317void UnwrappedLineParser::parseForOrWhileLoop() {
Daniel Jaspere1e43192014-04-01 12:55:11 +00001318 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1319 FormatTok->IsForEachMacro) &&
1320 "'for', 'while' or foreach macro expected");
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001321 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001322 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001323 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001324 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001325 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001326 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001327 addUnwrappedLine();
1328 } else {
1329 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001330 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001331 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001332 --Line->Level;
Alexander Kornienko37d6c942012-12-05 15:06:06 +00001333 }
1334}
1335
Daniel Jasperf7935112012-12-03 18:12:45 +00001336void UnwrappedLineParser::parseDoWhile() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001337 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001338 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001339 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001340 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001341 parseBlock(/*MustBeDeclaration=*/false);
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001342 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1343 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001344 } else {
1345 addUnwrappedLine();
Manuel Klimek52b15152013-01-09 15:25:02 +00001346 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001347 parseStructuralElement();
Manuel Klimek52b15152013-01-09 15:25:02 +00001348 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001349 }
1350
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001351 // FIXME: Add error handling.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001352 if (!FormatTok->Tok.is(tok::kw_while)) {
Alexander Kornienko0ea8e102012-12-04 15:40:36 +00001353 addUnwrappedLine();
1354 return;
1355 }
1356
Daniel Jasperf7935112012-12-03 18:12:45 +00001357 nextToken();
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001358 parseStructuralElement();
Daniel Jasperf7935112012-12-03 18:12:45 +00001359}
1360
1361void UnwrappedLineParser::parseLabel() {
Daniel Jasperf7935112012-12-03 18:12:45 +00001362 nextToken();
Manuel Klimek52b15152013-01-09 15:25:02 +00001363 unsigned OldLineLevel = Line->Level;
Daniel Jaspera1275122013-03-20 10:23:53 +00001364 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
Manuel Klimek52b15152013-01-09 15:25:02 +00001365 --Line->Level;
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001366 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001367 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Nico Weber9096fc02013-06-26 00:30:14 +00001368 parseBlock(/*MustBeDeclaration=*/false);
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001369 if (FormatTok->Tok.is(tok::kw_break)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001370 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1371 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1372 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001373 addUnwrappedLine();
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001374 }
Manuel Klimekd3ed59a2013-08-02 21:31:59 +00001375 parseStructuralElement();
1376 }
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001377 addUnwrappedLine();
1378 } else {
1379 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001380 }
Manuel Klimek52b15152013-01-09 15:25:02 +00001381 Line->Level = OldLineLevel;
Daniel Jasperf7935112012-12-03 18:12:45 +00001382}
1383
1384void UnwrappedLineParser::parseCaseLabel() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001385 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001386 // FIXME: fix handling of complex expressions here.
1387 do {
1388 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001389 } while (!eof() && !FormatTok->Tok.is(tok::colon));
Daniel Jasperf7935112012-12-03 18:12:45 +00001390 parseLabel();
1391}
1392
1393void UnwrappedLineParser::parseSwitch() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001394 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
Daniel Jasperf7935112012-12-03 18:12:45 +00001395 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001396 if (FormatTok->Tok.is(tok::l_paren))
Manuel Klimek9fa8d552013-01-11 19:23:05 +00001397 parseParens();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001398 if (FormatTok->Tok.is(tok::l_brace)) {
Alexander Kornienko3a33f022013-12-12 09:49:52 +00001399 CompoundStatementIndenter Indenter(this, Style, Line->Level);
Daniel Jasper65ee3472013-07-31 23:16:02 +00001400 parseBlock(/*MustBeDeclaration=*/false);
Daniel Jasperf7935112012-12-03 18:12:45 +00001401 addUnwrappedLine();
1402 } else {
1403 addUnwrappedLine();
Daniel Jasper516d7972013-07-25 11:31:57 +00001404 ++Line->Level;
Manuel Klimek6b9eeba2013-01-07 14:56:16 +00001405 parseStructuralElement();
Daniel Jasper516d7972013-07-25 11:31:57 +00001406 --Line->Level;
Daniel Jasperf7935112012-12-03 18:12:45 +00001407 }
1408}
1409
1410void UnwrappedLineParser::parseAccessSpecifier() {
1411 nextToken();
Daniel Jasper84c47a12013-11-23 17:53:41 +00001412 // Understand Qt's slots.
Daniel Jasper1556b592013-11-29 08:51:56 +00001413 if (FormatTok->is(tok::identifier) &&
1414 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
Daniel Jasper84c47a12013-11-23 17:53:41 +00001415 nextToken();
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001416 // Otherwise, we don't know what it is, and we'd better keep the next token.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001417 if (FormatTok->Tok.is(tok::colon))
Alexander Kornienko2ca766f2012-12-10 16:34:48 +00001418 nextToken();
Daniel Jasperf7935112012-12-03 18:12:45 +00001419 addUnwrappedLine();
1420}
1421
1422void UnwrappedLineParser::parseEnum() {
Daniel Jasper6be0f552014-11-13 15:56:28 +00001423 // Won't be 'enum' for NS_ENUMs.
1424 if (FormatTok->Tok.is(tok::kw_enum))
Daniel Jasperccb68b42014-11-19 22:38:18 +00001425 nextToken();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001426
Daniel Jasper2b41a822013-08-20 12:42:50 +00001427 // Eat up enum class ...
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001428 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1429 nextToken();
Daniel Jasper786a5502013-09-06 21:32:35 +00001430 while (FormatTok->Tok.getIdentifierInfo() ||
Daniel Jasperccb68b42014-11-19 22:38:18 +00001431 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1432 tok::greater, tok::comma, tok::question)) {
Manuel Klimek2cec0192013-01-21 19:17:52 +00001433 nextToken();
1434 // We can have macros or attributes in between 'enum' and the enum name.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001435 if (FormatTok->is(tok::l_paren))
Alexander Kornienkob7076a22012-12-04 14:46:19 +00001436 parseParens();
Daniel Jasperccb68b42014-11-19 22:38:18 +00001437 if (FormatTok->is(tok::identifier))
Manuel Klimek2cec0192013-01-21 19:17:52 +00001438 nextToken();
1439 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001440
1441 // Just a declaration or something is wrong.
Daniel Jasperccb68b42014-11-19 22:38:18 +00001442 if (FormatTok->isNot(tok::l_brace))
Daniel Jasper6be0f552014-11-13 15:56:28 +00001443 return;
1444 FormatTok->BlockKind = BK_Block;
1445
1446 if (Style.Language == FormatStyle::LK_Java) {
1447 // Java enums are different.
1448 parseJavaEnumBody();
1449 return;
Manuel Klimek2cec0192013-01-21 19:17:52 +00001450 }
Daniel Jasper6be0f552014-11-13 15:56:28 +00001451
1452 // Parse enum body.
1453 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1454 if (HasError) {
1455 if (FormatTok->is(tok::semi))
1456 nextToken();
1457 addUnwrappedLine();
1458 }
1459
Manuel Klimek2cec0192013-01-21 19:17:52 +00001460 // We fall through to parsing a structural element afterwards, so that in
1461 // enum A {} n, m;
1462 // "} n, m;" will end up in one unwrapped line.
Daniel Jasper6be0f552014-11-13 15:56:28 +00001463}
1464
1465void UnwrappedLineParser::parseJavaEnumBody() {
1466 // Determine whether the enum is simple, i.e. does not have a semicolon or
1467 // constants with class bodies. Simple enums can be formatted like braced
1468 // lists, contracted to a single line, etc.
1469 unsigned StoredPosition = Tokens->getPosition();
1470 bool IsSimple = true;
1471 FormatToken *Tok = Tokens->getNextToken();
1472 while (Tok) {
1473 if (Tok->is(tok::r_brace))
1474 break;
1475 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1476 IsSimple = false;
1477 break;
1478 }
1479 // FIXME: This will also mark enums with braces in the arguments to enum
1480 // constants as "not simple". This is probably fine in practice, though.
1481 Tok = Tokens->getNextToken();
1482 }
1483 FormatTok = Tokens->setPosition(StoredPosition);
1484
1485 if (IsSimple) {
1486 parseBracedList();
Daniel Jasperdf2ff002014-11-02 22:31:39 +00001487 addUnwrappedLine();
Daniel Jasper6be0f552014-11-13 15:56:28 +00001488 return;
1489 }
1490
1491 // Parse the body of a more complex enum.
1492 // First add a line for everything up to the "{".
1493 nextToken();
1494 addUnwrappedLine();
1495 ++Line->Level;
1496
1497 // Parse the enum constants.
1498 while (FormatTok) {
1499 if (FormatTok->is(tok::l_brace)) {
1500 // Parse the constant's class body.
1501 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1502 /*MunchSemi=*/false);
1503 } else if (FormatTok->is(tok::l_paren)) {
1504 parseParens();
1505 } else if (FormatTok->is(tok::comma)) {
1506 nextToken();
1507 addUnwrappedLine();
1508 } else if (FormatTok->is(tok::semi)) {
1509 nextToken();
1510 addUnwrappedLine();
1511 break;
1512 } else if (FormatTok->is(tok::r_brace)) {
1513 addUnwrappedLine();
1514 break;
1515 } else {
1516 nextToken();
1517 }
1518 }
1519
1520 // Parse the class body after the enum's ";" if any.
1521 parseLevel(/*HasOpeningBrace=*/true);
1522 nextToken();
1523 --Line->Level;
1524 addUnwrappedLine();
Daniel Jasperf7935112012-12-03 18:12:45 +00001525}
1526
Manuel Klimeke01bab52013-01-15 13:38:33 +00001527void UnwrappedLineParser::parseRecord() {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001528 const FormatToken &InitialToken = *FormatTok;
Manuel Klimek28cacc72013-01-07 18:10:23 +00001529 nextToken();
Manuel Klimek45bf56c2014-07-31 07:19:30 +00001530 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1531 tok::kw___declspec, tok::kw_alignas)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001532 nextToken();
1533 // We can have macros or attributes in between 'class' and the class name.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001534 if (FormatTok->Tok.is(tok::l_paren)) {
Manuel Klimeke01bab52013-01-15 13:38:33 +00001535 parseParens();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001536 }
Manuel Klimekd2650902013-02-06 15:57:54 +00001537 // The actual identifier can be a nested name specifier, and in macros
1538 // it is often token-pasted.
Daniel Jasper50b4bd72014-11-02 19:16:41 +00001539 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1540 FormatTok->is(tok::hashhash) ||
1541 (Style.Language == FormatStyle::LK_Java &&
1542 FormatTok->isOneOf(tok::period, tok::comma)))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001543 nextToken();
1544
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001545 // Note that parsing away template declarations here leads to incorrectly
1546 // accepting function declarations as record declarations.
1547 // In general, we cannot solve this problem. Consider:
1548 // class A<int> B() {}
1549 // which can be a function definition or a class definition when B() is a
1550 // macro. If we find enough real-world cases where this is a problem, we
1551 // can parse for the 'template' keyword in the beginning of the statement,
1552 // and thus rule out the record production in case there is no template
1553 // (this would still leave us with an ambiguity between template function
1554 // and class declarations).
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001555 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1556 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1557 if (FormatTok->Tok.is(tok::semi))
Manuel Klimeke01bab52013-01-15 13:38:33 +00001558 return;
1559 nextToken();
1560 }
1561 }
1562 }
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001563 if (FormatTok->Tok.is(tok::l_brace)) {
Roman Kashitsyna043ced2014-08-11 12:18:01 +00001564 if (ShouldBreakBeforeBrace(Style, InitialToken))
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001565 addUnwrappedLine();
1566
Daniel Jasper240dfda2014-03-31 14:23:49 +00001567 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
Manuel Klimekb212f3b2013-10-12 22:46:56 +00001568 /*MunchSemi=*/false);
Manuel Klimeka8eb9142013-05-13 12:51:40 +00001569 }
Manuel Klimekcdee74d2013-01-21 13:58:54 +00001570 // We fall through to parsing a structural element afterwards, so
1571 // class A {} n, m;
1572 // will end up in one unwrapped line.
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001573 // This does not apply for Java.
Daniel Jasper6fa9ec72015-02-19 16:03:16 +00001574 if (Style.Language == FormatStyle::LK_Java ||
1575 Style.Language == FormatStyle::LK_JavaScript)
Daniel Jasperc58c70e2014-09-15 11:21:46 +00001576 addUnwrappedLine();
Manuel Klimek28cacc72013-01-07 18:10:23 +00001577}
1578
Nico Weber8696a8d2013-01-09 21:15:03 +00001579void UnwrappedLineParser::parseObjCProtocolList() {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001580 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
Nico Weber8696a8d2013-01-09 21:15:03 +00001581 do
1582 nextToken();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001583 while (!eof() && FormatTok->Tok.isNot(tok::greater));
Nico Weber8696a8d2013-01-09 21:15:03 +00001584 nextToken(); // Skip '>'.
1585}
1586
1587void UnwrappedLineParser::parseObjCUntilAtEnd() {
1588 do {
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001589 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001590 nextToken();
1591 addUnwrappedLine();
1592 break;
1593 }
Daniel Jaspera15da302013-08-28 08:04:23 +00001594 if (FormatTok->is(tok::l_brace)) {
1595 parseBlock(/*MustBeDeclaration=*/false);
1596 // In ObjC interfaces, nothing should be following the "}".
1597 addUnwrappedLine();
Benjamin Kramere21cb742014-01-08 15:59:42 +00001598 } else if (FormatTok->is(tok::r_brace)) {
1599 // Ignore stray "}". parseStructuralElement doesn't consume them.
1600 nextToken();
1601 addUnwrappedLine();
Daniel Jaspera15da302013-08-28 08:04:23 +00001602 } else {
1603 parseStructuralElement();
1604 }
Nico Weber8696a8d2013-01-09 21:15:03 +00001605 } while (!eof());
1606}
1607
Nico Weber2ce0ac52013-01-09 23:25:37 +00001608void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001609 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001610 nextToken(); // interface name
Nico Weber7eecf4b2013-01-09 20:25:35 +00001611
1612 // @interface can be followed by either a base class, or a category.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001613 if (FormatTok->Tok.is(tok::colon)) {
Nico Weber7eecf4b2013-01-09 20:25:35 +00001614 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001615 nextToken(); // base class name
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001616 } else if (FormatTok->Tok.is(tok::l_paren))
Nico Weber7eecf4b2013-01-09 20:25:35 +00001617 // Skip category, if present.
1618 parseParens();
1619
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001620 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001621 parseObjCProtocolList();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001622
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001623 if (FormatTok->Tok.is(tok::l_brace)) {
1624 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1625 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1626 addUnwrappedLine();
Nico Weber9096fc02013-06-26 00:30:14 +00001627 parseBlock(/*MustBeDeclaration=*/true);
Dinesh Dwivediea3aca82014-05-02 17:01:46 +00001628 }
Nico Weber7eecf4b2013-01-09 20:25:35 +00001629
1630 // With instance variables, this puts '}' on its own line. Without instance
1631 // variables, this ends the @interface line.
1632 addUnwrappedLine();
1633
Nico Weber8696a8d2013-01-09 21:15:03 +00001634 parseObjCUntilAtEnd();
1635}
Nico Weber7eecf4b2013-01-09 20:25:35 +00001636
Nico Weber8696a8d2013-01-09 21:15:03 +00001637void UnwrappedLineParser::parseObjCProtocol() {
1638 nextToken();
Daniel Jasperd1ae3582013-03-20 12:37:50 +00001639 nextToken(); // protocol name
Nico Weber8696a8d2013-01-09 21:15:03 +00001640
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001641 if (FormatTok->Tok.is(tok::less))
Nico Weber8696a8d2013-01-09 21:15:03 +00001642 parseObjCProtocolList();
1643
1644 // Check for protocol declaration.
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001645 if (FormatTok->Tok.is(tok::semi)) {
Nico Weber8696a8d2013-01-09 21:15:03 +00001646 nextToken();
1647 return addUnwrappedLine();
1648 }
1649
1650 addUnwrappedLine();
1651 parseObjCUntilAtEnd();
Nico Weber7eecf4b2013-01-09 20:25:35 +00001652}
1653
Daniel Jasperfca735c2015-02-19 16:14:18 +00001654void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
1655 assert(FormatTok->isOneOf(Keywords.kw_import, tok::kw_export));
Daniel Jasper354aa512015-02-19 16:07:32 +00001656 nextToken();
Daniel Jasperfca735c2015-02-19 16:14:18 +00001657
1658 if (FormatTok->isOneOf(tok::kw_const, tok::kw_class, Keywords.kw_function,
1659 Keywords.kw_var))
1660 return; // Fall through to parsing the corresponding structure.
1661
1662 if (FormatTok->is(tok::kw_default)) {
1663 nextToken(); // export default ..., fall through after eating 'default'.
1664 return;
1665 }
1666
Daniel Jasper354aa512015-02-19 16:07:32 +00001667 if (FormatTok->is(tok::l_brace)) {
1668 FormatTok->BlockKind = BK_Block;
1669 parseBracedList();
1670 }
Daniel Jasperfca735c2015-02-19 16:14:18 +00001671
Daniel Jasper354aa512015-02-19 16:07:32 +00001672 while (!eof() && FormatTok->isNot(tok::semi) &&
1673 FormatTok->isNot(tok::l_brace)) {
1674 nextToken();
1675 }
1676}
1677
Daniel Jasper3b203a62013-09-05 16:05:56 +00001678LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1679 StringRef Prefix = "") {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001680 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1681 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1682 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1683 E = Line.Tokens.end();
1684 I != E; ++I) {
Daniel Jasper9a8d48b2013-09-05 10:04:31 +00001685 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001686 }
1687 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1688 E = Line.Tokens.end();
1689 I != E; ++I) {
1690 const UnwrappedLineNode &Node = *I;
1691 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1692 I = Node.Children.begin(),
1693 E = Node.Children.end();
1694 I != E; ++I) {
1695 printDebugInfo(*I, "\nChild: ");
1696 }
1697 }
1698 llvm::dbgs() << "\n";
1699}
1700
Daniel Jasperf7935112012-12-03 18:12:45 +00001701void UnwrappedLineParser::addUnwrappedLine() {
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001702 if (Line->Tokens.empty())
Daniel Jasper7c85fde2013-01-08 14:56:18 +00001703 return;
Manuel Klimekab3dc002013-01-16 12:31:12 +00001704 DEBUG({
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001705 if (CurrentLines == &Lines)
1706 printDebugInfo(*Line);
Manuel Klimekab3dc002013-01-16 12:31:12 +00001707 });
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001708 CurrentLines->push_back(*Line);
Daniel Jasperdaffc0d2013-01-16 09:10:19 +00001709 Line->Tokens.clear();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001710 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001711 for (SmallVectorImpl<UnwrappedLine>::iterator
Daniel Jaspera79064a2013-03-01 18:11:39 +00001712 I = PreprocessorDirectives.begin(),
1713 E = PreprocessorDirectives.end();
Manuel Klimekd3b92fa2013-01-18 14:04:34 +00001714 I != E; ++I) {
1715 CurrentLines->push_back(*I);
1716 }
1717 PreprocessorDirectives.clear();
1718 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001719}
1720
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001721bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
Daniel Jasperf7935112012-12-03 18:12:45 +00001722
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001723bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001724 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1725 FormatTok.NewlinesBefore > 0;
1726}
1727
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001728void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1729 bool JustComments = Line->Tokens.empty();
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001730 for (SmallVectorImpl<FormatToken *>::const_iterator
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001731 I = CommentsBeforeNextToken.begin(),
1732 E = CommentsBeforeNextToken.end();
1733 I != E; ++I) {
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001734 if (isOnNewLine(**I) && JustComments) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001735 addUnwrappedLine();
1736 }
1737 pushToken(*I);
1738 }
1739 if (NewlineBeforeNext && JustComments) {
1740 addUnwrappedLine();
1741 }
1742 CommentsBeforeNextToken.clear();
1743}
1744
Daniel Jasperf7935112012-12-03 18:12:45 +00001745void UnwrappedLineParser::nextToken() {
1746 if (eof())
1747 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001748 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001749 pushToken(FormatTok);
Manuel Klimek1abf7892013-01-04 23:34:14 +00001750 readToken();
1751}
1752
1753void UnwrappedLineParser::readToken() {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001754 bool CommentsInCurrentLine = true;
1755 do {
1756 FormatTok = Tokens->getNextToken();
Alexander Kornienkoc2ee9cf2014-03-13 13:59:48 +00001757 assert(FormatTok);
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001758 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1759 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001760 // If there is an unfinished unwrapped line, we flush the preprocessor
1761 // directives only after that unwrapped line was finished later.
Daniel Jasper29d39d52015-02-08 09:34:49 +00001762 bool SwitchToPreprocessorLines = !Line->Tokens.empty();
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001763 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
Alexander Kornienkob1be9d62013-04-03 12:38:53 +00001764 // Comments stored before the preprocessor directive need to be output
1765 // before the preprocessor directive, at the same level as the
1766 // preprocessor directive, as we consider them to apply to the directive.
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001767 flushComments(isOnNewLine(*FormatTok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001768 parsePPDirective();
1769 }
Manuel Klimek68b03042014-04-14 09:14:11 +00001770 while (FormatTok->Type == TT_ConflictStart ||
1771 FormatTok->Type == TT_ConflictEnd ||
1772 FormatTok->Type == TT_ConflictAlternative) {
1773 if (FormatTok->Type == TT_ConflictStart) {
1774 conditionalCompilationStart(/*Unreachable=*/false);
1775 } else if (FormatTok->Type == TT_ConflictAlternative) {
1776 conditionalCompilationAlternative();
Daniel Jasperb05a81d2014-05-09 13:11:16 +00001777 } else if (FormatTok->Type == TT_ConflictEnd) {
Manuel Klimek68b03042014-04-14 09:14:11 +00001778 conditionalCompilationEnd();
1779 }
1780 FormatTok = Tokens->getNextToken();
1781 FormatTok->MustBreakBefore = true;
1782 }
Alexander Kornienkof2e02122013-05-24 18:24:24 +00001783
1784 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1785 !Line->InPPDirective) {
1786 continue;
1787 }
1788
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001789 if (!FormatTok->Tok.is(tok::comment))
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001790 return;
Manuel Klimek1fcbe672014-04-11 12:27:47 +00001791 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001792 CommentsInCurrentLine = false;
1793 }
1794 if (CommentsInCurrentLine) {
1795 pushToken(FormatTok);
1796 } else {
1797 CommentsBeforeNextToken.push_back(FormatTok);
1798 }
1799 } while (!eof());
1800}
1801
Manuel Klimek15dfe7a2013-05-28 11:55:06 +00001802void UnwrappedLineParser::pushToken(FormatToken *Tok) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001803 Line->Tokens.push_back(UnwrappedLineNode(Tok));
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001804 if (MustBreakBeforeNextToken) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +00001805 Line->Tokens.back().Tok->MustBreakBefore = true;
Manuel Klimekf92f7bc2013-01-22 16:31:55 +00001806 MustBreakBeforeNextToken = false;
Manuel Klimek1abf7892013-01-04 23:34:14 +00001807 }
Daniel Jasperf7935112012-12-03 18:12:45 +00001808}
1809
Daniel Jasper8d1832e2013-01-07 13:26:07 +00001810} // end namespace format
1811} // end namespace clang