1 //===--- 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 ///
14 //===----------------------------------------------------------------------===//
15
16 #include "UnwrappedLineParser.h"
17 #include "llvm/Support/Debug.h"
18
19 #define DEBUG_TYPE "format-parser"
20
21 namespace clang {
22 namespace format {
23
24 class FormatTokenSource {
25 public:
~FormatTokenSource()26 virtual ~FormatTokenSource() {}
27 virtual FormatToken *getNextToken() = 0;
28
29 virtual unsigned getPosition() = 0;
30 virtual FormatToken *setPosition(unsigned Position) = 0;
31 };
32
33 namespace {
34
35 class ScopedDeclarationState {
36 public:
ScopedDeclarationState(UnwrappedLine & Line,std::vector<bool> & Stack,bool MustBeDeclaration)37 ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
38 bool MustBeDeclaration)
39 : Line(Line), Stack(Stack) {
40 Line.MustBeDeclaration = MustBeDeclaration;
41 Stack.push_back(MustBeDeclaration);
42 }
~ScopedDeclarationState()43 ~ScopedDeclarationState() {
44 Stack.pop_back();
45 if (!Stack.empty())
46 Line.MustBeDeclaration = Stack.back();
47 else
48 Line.MustBeDeclaration = true;
49 }
50
51 private:
52 UnwrappedLine &Line;
53 std::vector<bool> &Stack;
54 };
55
56 class ScopedMacroState : public FormatTokenSource {
57 public:
ScopedMacroState(UnwrappedLine & Line,FormatTokenSource * & TokenSource,FormatToken * & ResetToken,bool & StructuralError)58 ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
59 FormatToken *&ResetToken, bool &StructuralError)
60 : Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
61 PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
62 StructuralError(StructuralError),
63 PreviousStructuralError(StructuralError), Token(nullptr) {
64 TokenSource = this;
65 Line.Level = 0;
66 Line.InPPDirective = true;
67 }
68
~ScopedMacroState()69 ~ScopedMacroState() {
70 TokenSource = PreviousTokenSource;
71 ResetToken = Token;
72 Line.InPPDirective = false;
73 Line.Level = PreviousLineLevel;
74 StructuralError = PreviousStructuralError;
75 }
76
getNextToken()77 FormatToken *getNextToken() override {
78 // The \c UnwrappedLineParser guards against this by never calling
79 // \c getNextToken() after it has encountered the first eof token.
80 assert(!eof());
81 Token = PreviousTokenSource->getNextToken();
82 if (eof())
83 return getFakeEOF();
84 return Token;
85 }
86
getPosition()87 unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
88
setPosition(unsigned Position)89 FormatToken *setPosition(unsigned Position) override {
90 Token = PreviousTokenSource->setPosition(Position);
91 return Token;
92 }
93
94 private:
eof()95 bool eof() { return Token && Token->HasUnescapedNewline; }
96
getFakeEOF()97 FormatToken *getFakeEOF() {
98 static bool EOFInitialized = false;
99 static FormatToken FormatTok;
100 if (!EOFInitialized) {
101 FormatTok.Tok.startToken();
102 FormatTok.Tok.setKind(tok::eof);
103 EOFInitialized = true;
104 }
105 return &FormatTok;
106 }
107
108 UnwrappedLine &Line;
109 FormatTokenSource *&TokenSource;
110 FormatToken *&ResetToken;
111 unsigned PreviousLineLevel;
112 FormatTokenSource *PreviousTokenSource;
113 bool &StructuralError;
114 bool PreviousStructuralError;
115
116 FormatToken *Token;
117 };
118
119 } // end anonymous namespace
120
121 class ScopedLineState {
122 public:
ScopedLineState(UnwrappedLineParser & Parser,bool SwitchToPreprocessorLines=false)123 ScopedLineState(UnwrappedLineParser &Parser,
124 bool SwitchToPreprocessorLines = false)
125 : Parser(Parser), OriginalLines(Parser.CurrentLines) {
126 if (SwitchToPreprocessorLines)
127 Parser.CurrentLines = &Parser.PreprocessorDirectives;
128 else if (!Parser.Line->Tokens.empty())
129 Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
130 PreBlockLine = std::move(Parser.Line);
131 Parser.Line = llvm::make_unique<UnwrappedLine>();
132 Parser.Line->Level = PreBlockLine->Level;
133 Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
134 }
135
~ScopedLineState()136 ~ScopedLineState() {
137 if (!Parser.Line->Tokens.empty()) {
138 Parser.addUnwrappedLine();
139 }
140 assert(Parser.Line->Tokens.empty());
141 Parser.Line = std::move(PreBlockLine);
142 if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
143 Parser.MustBreakBeforeNextToken = true;
144 Parser.CurrentLines = OriginalLines;
145 }
146
147 private:
148 UnwrappedLineParser &Parser;
149
150 std::unique_ptr<UnwrappedLine> PreBlockLine;
151 SmallVectorImpl<UnwrappedLine> *OriginalLines;
152 };
153
154 class CompoundStatementIndenter {
155 public:
CompoundStatementIndenter(UnwrappedLineParser * Parser,const FormatStyle & Style,unsigned & LineLevel)156 CompoundStatementIndenter(UnwrappedLineParser *Parser,
157 const FormatStyle &Style, unsigned &LineLevel)
158 : LineLevel(LineLevel), OldLineLevel(LineLevel) {
159 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
160 Parser->addUnwrappedLine();
161 } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
162 Parser->addUnwrappedLine();
163 ++LineLevel;
164 }
165 }
~CompoundStatementIndenter()166 ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
167
168 private:
169 unsigned &LineLevel;
170 unsigned OldLineLevel;
171 };
172
173 namespace {
174
175 class IndexedTokenSource : public FormatTokenSource {
176 public:
IndexedTokenSource(ArrayRef<FormatToken * > Tokens)177 IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
178 : Tokens(Tokens), Position(-1) {}
179
getNextToken()180 FormatToken *getNextToken() override {
181 ++Position;
182 return Tokens[Position];
183 }
184
getPosition()185 unsigned getPosition() override {
186 assert(Position >= 0);
187 return Position;
188 }
189
setPosition(unsigned P)190 FormatToken *setPosition(unsigned P) override {
191 Position = P;
192 return Tokens[Position];
193 }
194
reset()195 void reset() { Position = -1; }
196
197 private:
198 ArrayRef<FormatToken *> Tokens;
199 int Position;
200 };
201
202 } // end anonymous namespace
203
UnwrappedLineParser(const FormatStyle & Style,const AdditionalKeywords & Keywords,ArrayRef<FormatToken * > Tokens,UnwrappedLineConsumer & Callback)204 UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
205 const AdditionalKeywords &Keywords,
206 ArrayRef<FormatToken *> Tokens,
207 UnwrappedLineConsumer &Callback)
208 : Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
209 CurrentLines(&Lines), StructuralError(false), Style(Style),
210 Keywords(Keywords), Tokens(nullptr), Callback(Callback),
211 AllTokens(Tokens), PPBranchLevel(-1) {}
212
reset()213 void UnwrappedLineParser::reset() {
214 PPBranchLevel = -1;
215 Line.reset(new UnwrappedLine);
216 CommentsBeforeNextToken.clear();
217 FormatTok = nullptr;
218 MustBreakBeforeNextToken = false;
219 PreprocessorDirectives.clear();
220 CurrentLines = &Lines;
221 DeclarationScopeStack.clear();
222 StructuralError = false;
223 PPStack.clear();
224 }
225
parse()226 bool UnwrappedLineParser::parse() {
227 IndexedTokenSource TokenSource(AllTokens);
228 do {
229 DEBUG(llvm::dbgs() << "----\n");
230 reset();
231 Tokens = &TokenSource;
232 TokenSource.reset();
233
234 readToken();
235 parseFile();
236 // Create line with eof token.
237 pushToken(FormatTok);
238 addUnwrappedLine();
239
240 for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
241 E = Lines.end();
242 I != E; ++I) {
243 Callback.consumeUnwrappedLine(*I);
244 }
245 Callback.finishRun();
246 Lines.clear();
247 while (!PPLevelBranchIndex.empty() &&
248 PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
249 PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
250 PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
251 }
252 if (!PPLevelBranchIndex.empty()) {
253 ++PPLevelBranchIndex.back();
254 assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
255 assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
256 }
257 } while (!PPLevelBranchIndex.empty());
258
259 return StructuralError;
260 }
261
parseFile()262 void UnwrappedLineParser::parseFile() {
263 ScopedDeclarationState DeclarationState(
264 *Line, DeclarationScopeStack,
265 /*MustBeDeclaration=*/ !Line->InPPDirective);
266 parseLevel(/*HasOpeningBrace=*/false);
267 // Make sure to format the remaining tokens.
268 flushComments(true);
269 addUnwrappedLine();
270 }
271
parseLevel(bool HasOpeningBrace)272 void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
273 bool SwitchLabelEncountered = false;
274 do {
275 switch (FormatTok->Tok.getKind()) {
276 case tok::comment:
277 nextToken();
278 addUnwrappedLine();
279 break;
280 case tok::l_brace:
281 // FIXME: Add parameter whether this can happen - if this happens, we must
282 // be in a non-declaration context.
283 parseBlock(/*MustBeDeclaration=*/false);
284 addUnwrappedLine();
285 break;
286 case tok::r_brace:
287 if (HasOpeningBrace)
288 return;
289 StructuralError = true;
290 nextToken();
291 addUnwrappedLine();
292 break;
293 case tok::kw_default:
294 case tok::kw_case:
295 if (!SwitchLabelEncountered &&
296 (Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
297 ++Line->Level;
298 SwitchLabelEncountered = true;
299 parseStructuralElement();
300 break;
301 default:
302 parseStructuralElement();
303 break;
304 }
305 } while (!eof());
306 }
307
calculateBraceTypes()308 void UnwrappedLineParser::calculateBraceTypes() {
309 // We'll parse forward through the tokens until we hit
310 // a closing brace or eof - note that getNextToken() will
311 // parse macros, so this will magically work inside macro
312 // definitions, too.
313 unsigned StoredPosition = Tokens->getPosition();
314 FormatToken *Tok = FormatTok;
315 // Keep a stack of positions of lbrace tokens. We will
316 // update information about whether an lbrace starts a
317 // braced init list or a different block during the loop.
318 SmallVector<FormatToken *, 8> LBraceStack;
319 assert(Tok->Tok.is(tok::l_brace));
320 do {
321 // Get next none-comment token.
322 FormatToken *NextTok;
323 unsigned ReadTokens = 0;
324 do {
325 NextTok = Tokens->getNextToken();
326 ++ReadTokens;
327 } while (NextTok->is(tok::comment));
328
329 switch (Tok->Tok.getKind()) {
330 case tok::l_brace:
331 LBraceStack.push_back(Tok);
332 break;
333 case tok::r_brace:
334 if (!LBraceStack.empty()) {
335 if (LBraceStack.back()->BlockKind == BK_Unknown) {
336 bool ProbablyBracedList = false;
337 if (Style.Language == FormatStyle::LK_Proto) {
338 ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
339 } else {
340 // Using OriginalColumn to distinguish between ObjC methods and
341 // binary operators is a bit hacky.
342 bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
343 NextTok->OriginalColumn == 0;
344
345 // If there is a comma, semicolon or right paren after the closing
346 // brace, we assume this is a braced initializer list. Note that
347 // regardless how we mark inner braces here, we will overwrite the
348 // BlockKind later if we parse a braced list (where all blocks
349 // inside are by default braced lists), or when we explicitly detect
350 // blocks (for example while parsing lambdas).
351 //
352 // We exclude + and - as they can be ObjC visibility modifiers.
353 ProbablyBracedList =
354 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
355 tok::r_paren, tok::r_square, tok::l_brace,
356 tok::l_paren, tok::ellipsis) ||
357 (NextTok->isBinaryOperator() && !NextIsObjCMethod);
358 }
359 if (ProbablyBracedList) {
360 Tok->BlockKind = BK_BracedInit;
361 LBraceStack.back()->BlockKind = BK_BracedInit;
362 } else {
363 Tok->BlockKind = BK_Block;
364 LBraceStack.back()->BlockKind = BK_Block;
365 }
366 }
367 LBraceStack.pop_back();
368 }
369 break;
370 case tok::at:
371 case tok::semi:
372 case tok::kw_if:
373 case tok::kw_while:
374 case tok::kw_for:
375 case tok::kw_switch:
376 case tok::kw_try:
377 if (!LBraceStack.empty())
378 LBraceStack.back()->BlockKind = BK_Block;
379 break;
380 default:
381 break;
382 }
383 Tok = NextTok;
384 } while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
385 // Assume other blocks for all unclosed opening braces.
386 for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
387 if (LBraceStack[i]->BlockKind == BK_Unknown)
388 LBraceStack[i]->BlockKind = BK_Block;
389 }
390
391 FormatTok = Tokens->setPosition(StoredPosition);
392 }
393
parseBlock(bool MustBeDeclaration,bool AddLevel,bool MunchSemi)394 void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
395 bool MunchSemi) {
396 assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
397 unsigned InitialLevel = Line->Level;
398 nextToken();
399
400 addUnwrappedLine();
401
402 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
403 MustBeDeclaration);
404 if (AddLevel)
405 ++Line->Level;
406 parseLevel(/*HasOpeningBrace=*/true);
407
408 if (!FormatTok->Tok.is(tok::r_brace)) {
409 Line->Level = InitialLevel;
410 StructuralError = true;
411 return;
412 }
413
414 nextToken(); // Munch the closing brace.
415 if (MunchSemi && FormatTok->Tok.is(tok::semi))
416 nextToken();
417 Line->Level = InitialLevel;
418 }
419
IsGoogScope(const UnwrappedLine & Line)420 static bool IsGoogScope(const UnwrappedLine &Line) {
421 // FIXME: Closure-library specific stuff should not be hard-coded but be
422 // configurable.
423 if (Line.Tokens.size() < 4)
424 return false;
425 auto I = Line.Tokens.begin();
426 if (I->Tok->TokenText != "goog")
427 return false;
428 ++I;
429 if (I->Tok->isNot(tok::period))
430 return false;
431 ++I;
432 if (I->Tok->TokenText != "scope")
433 return false;
434 ++I;
435 return I->Tok->is(tok::l_paren);
436 }
437
ShouldBreakBeforeBrace(const FormatStyle & Style,const FormatToken & InitialToken)438 static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
439 const FormatToken &InitialToken) {
440 switch (Style.BreakBeforeBraces) {
441 case FormatStyle::BS_Linux:
442 return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
443 case FormatStyle::BS_Allman:
444 case FormatStyle::BS_GNU:
445 return true;
446 default:
447 return false;
448 }
449 }
450
parseChildBlock()451 void UnwrappedLineParser::parseChildBlock() {
452 FormatTok->BlockKind = BK_Block;
453 nextToken();
454 {
455 bool GoogScope =
456 Style.Language == FormatStyle::LK_JavaScript && IsGoogScope(*Line);
457 ScopedLineState LineState(*this);
458 ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
459 /*MustBeDeclaration=*/false);
460 Line->Level += GoogScope ? 0 : 1;
461 parseLevel(/*HasOpeningBrace=*/true);
462 Line->Level -= GoogScope ? 0 : 1;
463 }
464 nextToken();
465 }
466
parsePPDirective()467 void UnwrappedLineParser::parsePPDirective() {
468 assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
469 ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
470 nextToken();
471
472 if (!FormatTok->Tok.getIdentifierInfo()) {
473 parsePPUnknown();
474 return;
475 }
476
477 switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
478 case tok::pp_define:
479 parsePPDefine();
480 return;
481 case tok::pp_if:
482 parsePPIf(/*IfDef=*/false);
483 break;
484 case tok::pp_ifdef:
485 case tok::pp_ifndef:
486 parsePPIf(/*IfDef=*/true);
487 break;
488 case tok::pp_else:
489 parsePPElse();
490 break;
491 case tok::pp_elif:
492 parsePPElIf();
493 break;
494 case tok::pp_endif:
495 parsePPEndIf();
496 break;
497 default:
498 parsePPUnknown();
499 break;
500 }
501 }
502
conditionalCompilationCondition(bool Unreachable)503 void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
504 if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
505 PPStack.push_back(PP_Unreachable);
506 else
507 PPStack.push_back(PP_Conditional);
508 }
509
conditionalCompilationStart(bool Unreachable)510 void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
511 ++PPBranchLevel;
512 assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
513 if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
514 PPLevelBranchIndex.push_back(0);
515 PPLevelBranchCount.push_back(0);
516 }
517 PPChainBranchIndex.push(0);
518 bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
519 conditionalCompilationCondition(Unreachable || Skip);
520 }
521
conditionalCompilationAlternative()522 void UnwrappedLineParser::conditionalCompilationAlternative() {
523 if (!PPStack.empty())
524 PPStack.pop_back();
525 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
526 if (!PPChainBranchIndex.empty())
527 ++PPChainBranchIndex.top();
528 conditionalCompilationCondition(
529 PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
530 PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
531 }
532
conditionalCompilationEnd()533 void UnwrappedLineParser::conditionalCompilationEnd() {
534 assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
535 if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
536 if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
537 PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
538 }
539 }
540 // Guard against #endif's without #if.
541 if (PPBranchLevel > 0)
542 --PPBranchLevel;
543 if (!PPChainBranchIndex.empty())
544 PPChainBranchIndex.pop();
545 if (!PPStack.empty())
546 PPStack.pop_back();
547 }
548
parsePPIf(bool IfDef)549 void UnwrappedLineParser::parsePPIf(bool IfDef) {
550 nextToken();
551 bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
552 StringRef(FormatTok->Tok.getLiteralData(),
553 FormatTok->Tok.getLength()) == "0") ||
554 FormatTok->Tok.is(tok::kw_false);
555 conditionalCompilationStart(!IfDef && IsLiteralFalse);
556 parsePPUnknown();
557 }
558
parsePPElse()559 void UnwrappedLineParser::parsePPElse() {
560 conditionalCompilationAlternative();
561 parsePPUnknown();
562 }
563
parsePPElIf()564 void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
565
parsePPEndIf()566 void UnwrappedLineParser::parsePPEndIf() {
567 conditionalCompilationEnd();
568 parsePPUnknown();
569 }
570
parsePPDefine()571 void UnwrappedLineParser::parsePPDefine() {
572 nextToken();
573
574 if (FormatTok->Tok.getKind() != tok::identifier) {
575 parsePPUnknown();
576 return;
577 }
578 nextToken();
579 if (FormatTok->Tok.getKind() == tok::l_paren &&
580 FormatTok->WhitespaceRange.getBegin() ==
581 FormatTok->WhitespaceRange.getEnd()) {
582 parseParens();
583 }
584 addUnwrappedLine();
585 Line->Level = 1;
586
587 // Errors during a preprocessor directive can only affect the layout of the
588 // preprocessor directive, and thus we ignore them. An alternative approach
589 // would be to use the same approach we use on the file level (no
590 // re-indentation if there was a structural error) within the macro
591 // definition.
592 parseFile();
593 }
594
parsePPUnknown()595 void UnwrappedLineParser::parsePPUnknown() {
596 do {
597 nextToken();
598 } while (!eof());
599 addUnwrappedLine();
600 }
601
602 // Here we blacklist certain tokens that are not usually the first token in an
603 // unwrapped line. This is used in attempt to distinguish macro calls without
604 // trailing semicolons from other constructs split to several lines.
tokenCanStartNewLine(clang::Token Tok)605 bool tokenCanStartNewLine(clang::Token Tok) {
606 // Semicolon can be a null-statement, l_square can be a start of a macro or
607 // a C++11 attribute, but this doesn't seem to be common.
608 return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
609 Tok.isNot(tok::l_square) &&
610 // Tokens that can only be used as binary operators and a part of
611 // overloaded operator names.
612 Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
613 Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
614 Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
615 Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
616 Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
617 Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
618 Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
619 Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
620 Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
621 Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
622 Tok.isNot(tok::lesslessequal) &&
623 // Colon is used in labels, base class lists, initializer lists,
624 // range-based for loops, ternary operator, but should never be the
625 // first token in an unwrapped line.
626 Tok.isNot(tok::colon) &&
627 // 'noexcept' is a trailing annotation.
628 Tok.isNot(tok::kw_noexcept);
629 }
630
parseStructuralElement()631 void UnwrappedLineParser::parseStructuralElement() {
632 assert(!FormatTok->Tok.is(tok::l_brace));
633 switch (FormatTok->Tok.getKind()) {
634 case tok::at:
635 nextToken();
636 if (FormatTok->Tok.is(tok::l_brace)) {
637 parseBracedList();
638 break;
639 }
640 switch (FormatTok->Tok.getObjCKeywordID()) {
641 case tok::objc_public:
642 case tok::objc_protected:
643 case tok::objc_package:
644 case tok::objc_private:
645 return parseAccessSpecifier();
646 case tok::objc_interface:
647 case tok::objc_implementation:
648 return parseObjCInterfaceOrImplementation();
649 case tok::objc_protocol:
650 return parseObjCProtocol();
651 case tok::objc_end:
652 return; // Handled by the caller.
653 case tok::objc_optional:
654 case tok::objc_required:
655 nextToken();
656 addUnwrappedLine();
657 return;
658 default:
659 break;
660 }
661 break;
662 case tok::kw_asm:
663 nextToken();
664 if (FormatTok->is(tok::l_brace)) {
665 nextToken();
666 while (FormatTok && FormatTok->isNot(tok::eof)) {
667 if (FormatTok->is(tok::r_brace)) {
668 nextToken();
669 break;
670 }
671 FormatTok->Finalized = true;
672 nextToken();
673 }
674 }
675 break;
676 case tok::kw_namespace:
677 parseNamespace();
678 return;
679 case tok::kw_inline:
680 nextToken();
681 if (FormatTok->Tok.is(tok::kw_namespace)) {
682 parseNamespace();
683 return;
684 }
685 break;
686 case tok::kw_public:
687 case tok::kw_protected:
688 case tok::kw_private:
689 if (Style.Language == FormatStyle::LK_Java)
690 nextToken();
691 else
692 parseAccessSpecifier();
693 return;
694 case tok::kw_if:
695 parseIfThenElse();
696 return;
697 case tok::kw_for:
698 case tok::kw_while:
699 parseForOrWhileLoop();
700 return;
701 case tok::kw_do:
702 parseDoWhile();
703 return;
704 case tok::kw_switch:
705 parseSwitch();
706 return;
707 case tok::kw_default:
708 nextToken();
709 parseLabel();
710 return;
711 case tok::kw_case:
712 parseCaseLabel();
713 return;
714 case tok::kw_try:
715 parseTryCatch();
716 return;
717 case tok::kw_extern:
718 nextToken();
719 if (FormatTok->Tok.is(tok::string_literal)) {
720 nextToken();
721 if (FormatTok->Tok.is(tok::l_brace)) {
722 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
723 addUnwrappedLine();
724 return;
725 }
726 }
727 break;
728 case tok::identifier:
729 if (FormatTok->IsForEachMacro) {
730 parseForOrWhileLoop();
731 return;
732 }
733 // In all other cases, parse the declaration.
734 break;
735 default:
736 break;
737 }
738 do {
739 switch (FormatTok->Tok.getKind()) {
740 case tok::at:
741 nextToken();
742 if (FormatTok->Tok.is(tok::l_brace))
743 parseBracedList();
744 break;
745 case tok::kw_enum:
746 parseEnum();
747 break;
748 case tok::kw_typedef:
749 nextToken();
750 if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
751 Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
752 parseEnum();
753 break;
754 case tok::kw_struct:
755 case tok::kw_union:
756 case tok::kw_class:
757 parseRecord();
758 // A record declaration or definition is always the start of a structural
759 // element.
760 break;
761 case tok::period:
762 nextToken();
763 // In Java, classes have an implicit static member "class".
764 if (Style.Language == FormatStyle::LK_Java && FormatTok &&
765 FormatTok->is(tok::kw_class))
766 nextToken();
767 break;
768 case tok::semi:
769 nextToken();
770 addUnwrappedLine();
771 return;
772 case tok::r_brace:
773 addUnwrappedLine();
774 return;
775 case tok::l_paren:
776 parseParens();
777 break;
778 case tok::caret:
779 nextToken();
780 if (FormatTok->Tok.isAnyIdentifier() ||
781 FormatTok->isSimpleTypeSpecifier())
782 nextToken();
783 if (FormatTok->is(tok::l_paren))
784 parseParens();
785 if (FormatTok->is(tok::l_brace))
786 parseChildBlock();
787 break;
788 case tok::l_brace:
789 if (!tryToParseBracedList()) {
790 // A block outside of parentheses must be the last part of a
791 // structural element.
792 // FIXME: Figure out cases where this is not true, and add projections
793 // for them (the one we know is missing are lambdas).
794 if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
795 addUnwrappedLine();
796 FormatTok->Type = TT_FunctionLBrace;
797 parseBlock(/*MustBeDeclaration=*/false);
798 addUnwrappedLine();
799 return;
800 }
801 // Otherwise this was a braced init list, and the structural
802 // element continues.
803 break;
804 case tok::kw_try:
805 // We arrive here when parsing function-try blocks.
806 parseTryCatch();
807 return;
808 case tok::identifier: {
809 StringRef Text = FormatTok->TokenText;
810 // Parse function literal unless 'function' is the first token in a line
811 // in which case this should be treated as a free-standing function.
812 if (Style.Language == FormatStyle::LK_JavaScript && Text == "function" &&
813 Line->Tokens.size() > 0) {
814 tryToParseJSFunction();
815 break;
816 }
817 nextToken();
818 if (Line->Tokens.size() == 1) {
819 if (FormatTok->Tok.is(tok::colon)) {
820 parseLabel();
821 return;
822 }
823 // Recognize function-like macro usages without trailing semicolon as
824 // well as free-standing macrose like Q_OBJECT.
825 bool FunctionLike = FormatTok->is(tok::l_paren);
826 if (FunctionLike)
827 parseParens();
828 if (FormatTok->NewlinesBefore > 0 &&
829 (Text.size() >= 5 || FunctionLike) &&
830 tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
831 addUnwrappedLine();
832 return;
833 }
834 }
835 break;
836 }
837 case tok::equal:
838 nextToken();
839 if (FormatTok->Tok.is(tok::l_brace)) {
840 parseBracedList();
841 }
842 break;
843 case tok::l_square:
844 parseSquare();
845 break;
846 default:
847 nextToken();
848 break;
849 }
850 } while (!eof());
851 }
852
tryToParseLambda()853 bool UnwrappedLineParser::tryToParseLambda() {
854 // FIXME: This is a dirty way to access the previous token. Find a better
855 // solution.
856 if (!Line->Tokens.empty() &&
857 (Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator,
858 tok::kw_new, tok::kw_delete) ||
859 Line->Tokens.back().Tok->closesScope() ||
860 Line->Tokens.back().Tok->isSimpleTypeSpecifier())) {
861 nextToken();
862 return false;
863 }
864 assert(FormatTok->is(tok::l_square));
865 FormatToken &LSquare = *FormatTok;
866 if (!tryToParseLambdaIntroducer())
867 return false;
868
869 while (FormatTok->isNot(tok::l_brace)) {
870 if (FormatTok->isSimpleTypeSpecifier()) {
871 nextToken();
872 continue;
873 }
874 switch (FormatTok->Tok.getKind()) {
875 case tok::l_brace:
876 break;
877 case tok::l_paren:
878 parseParens();
879 break;
880 case tok::amp:
881 case tok::star:
882 case tok::kw_const:
883 case tok::comma:
884 case tok::less:
885 case tok::greater:
886 case tok::identifier:
887 case tok::coloncolon:
888 case tok::kw_mutable:
889 nextToken();
890 break;
891 case tok::arrow:
892 FormatTok->Type = TT_TrailingReturnArrow;
893 nextToken();
894 break;
895 default:
896 return true;
897 }
898 }
899 LSquare.Type = TT_LambdaLSquare;
900 parseChildBlock();
901 return true;
902 }
903
tryToParseLambdaIntroducer()904 bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
905 nextToken();
906 if (FormatTok->is(tok::equal)) {
907 nextToken();
908 if (FormatTok->is(tok::r_square)) {
909 nextToken();
910 return true;
911 }
912 if (FormatTok->isNot(tok::comma))
913 return false;
914 nextToken();
915 } else if (FormatTok->is(tok::amp)) {
916 nextToken();
917 if (FormatTok->is(tok::r_square)) {
918 nextToken();
919 return true;
920 }
921 if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
922 return false;
923 }
924 if (FormatTok->is(tok::comma))
925 nextToken();
926 } else if (FormatTok->is(tok::r_square)) {
927 nextToken();
928 return true;
929 }
930 do {
931 if (FormatTok->is(tok::amp))
932 nextToken();
933 if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
934 return false;
935 nextToken();
936 if (FormatTok->is(tok::ellipsis))
937 nextToken();
938 if (FormatTok->is(tok::comma)) {
939 nextToken();
940 } else if (FormatTok->is(tok::r_square)) {
941 nextToken();
942 return true;
943 } else {
944 return false;
945 }
946 } while (!eof());
947 return false;
948 }
949
tryToParseJSFunction()950 void UnwrappedLineParser::tryToParseJSFunction() {
951 nextToken();
952
953 // Consume function name.
954 if (FormatTok->is(tok::identifier))
955 nextToken();
956
957 if (FormatTok->isNot(tok::l_paren))
958 return;
959 nextToken();
960 while (FormatTok->isNot(tok::l_brace)) {
961 // Err on the side of caution in order to avoid consuming the full file in
962 // case of incomplete code.
963 if (!FormatTok->isOneOf(tok::identifier, tok::comma, tok::r_paren,
964 tok::comment))
965 return;
966 nextToken();
967 }
968 parseChildBlock();
969 }
970
tryToParseBracedList()971 bool UnwrappedLineParser::tryToParseBracedList() {
972 if (FormatTok->BlockKind == BK_Unknown)
973 calculateBraceTypes();
974 assert(FormatTok->BlockKind != BK_Unknown);
975 if (FormatTok->BlockKind == BK_Block)
976 return false;
977 parseBracedList();
978 return true;
979 }
980
parseBracedList(bool ContinueOnSemicolons)981 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
982 bool HasError = false;
983 nextToken();
984
985 // FIXME: Once we have an expression parser in the UnwrappedLineParser,
986 // replace this by using parseAssigmentExpression() inside.
987 do {
988 if (Style.Language == FormatStyle::LK_JavaScript &&
989 FormatTok->is(Keywords.kw_function)) {
990 tryToParseJSFunction();
991 continue;
992 }
993 switch (FormatTok->Tok.getKind()) {
994 case tok::caret:
995 nextToken();
996 if (FormatTok->is(tok::l_brace)) {
997 parseChildBlock();
998 }
999 break;
1000 case tok::l_square:
1001 tryToParseLambda();
1002 break;
1003 case tok::l_brace:
1004 // Assume there are no blocks inside a braced init list apart
1005 // from the ones we explicitly parse out (like lambdas).
1006 FormatTok->BlockKind = BK_BracedInit;
1007 parseBracedList();
1008 break;
1009 case tok::r_brace:
1010 nextToken();
1011 return !HasError;
1012 case tok::semi:
1013 HasError = true;
1014 if (!ContinueOnSemicolons)
1015 return !HasError;
1016 nextToken();
1017 break;
1018 case tok::comma:
1019 nextToken();
1020 break;
1021 default:
1022 nextToken();
1023 break;
1024 }
1025 } while (!eof());
1026 return false;
1027 }
1028
parseParens()1029 void UnwrappedLineParser::parseParens() {
1030 assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
1031 nextToken();
1032 do {
1033 switch (FormatTok->Tok.getKind()) {
1034 case tok::l_paren:
1035 parseParens();
1036 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
1037 parseChildBlock();
1038 break;
1039 case tok::r_paren:
1040 nextToken();
1041 return;
1042 case tok::r_brace:
1043 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1044 return;
1045 case tok::l_square:
1046 tryToParseLambda();
1047 break;
1048 case tok::l_brace:
1049 if (!tryToParseBracedList()) {
1050 parseChildBlock();
1051 }
1052 break;
1053 case tok::at:
1054 nextToken();
1055 if (FormatTok->Tok.is(tok::l_brace))
1056 parseBracedList();
1057 break;
1058 case tok::identifier:
1059 if (Style.Language == FormatStyle::LK_JavaScript &&
1060 FormatTok->is(Keywords.kw_function))
1061 tryToParseJSFunction();
1062 else
1063 nextToken();
1064 break;
1065 default:
1066 nextToken();
1067 break;
1068 }
1069 } while (!eof());
1070 }
1071
parseSquare()1072 void UnwrappedLineParser::parseSquare() {
1073 assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
1074 if (tryToParseLambda())
1075 return;
1076 do {
1077 switch (FormatTok->Tok.getKind()) {
1078 case tok::l_paren:
1079 parseParens();
1080 break;
1081 case tok::r_square:
1082 nextToken();
1083 return;
1084 case tok::r_brace:
1085 // A "}" inside parenthesis is an error if there wasn't a matching "{".
1086 return;
1087 case tok::l_square:
1088 parseSquare();
1089 break;
1090 case tok::l_brace: {
1091 if (!tryToParseBracedList()) {
1092 parseChildBlock();
1093 }
1094 break;
1095 }
1096 case tok::at:
1097 nextToken();
1098 if (FormatTok->Tok.is(tok::l_brace))
1099 parseBracedList();
1100 break;
1101 default:
1102 nextToken();
1103 break;
1104 }
1105 } while (!eof());
1106 }
1107
parseIfThenElse()1108 void UnwrappedLineParser::parseIfThenElse() {
1109 assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
1110 nextToken();
1111 if (FormatTok->Tok.is(tok::l_paren))
1112 parseParens();
1113 bool NeedsUnwrappedLine = false;
1114 if (FormatTok->Tok.is(tok::l_brace)) {
1115 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1116 parseBlock(/*MustBeDeclaration=*/false);
1117 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1118 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
1119 addUnwrappedLine();
1120 } else {
1121 NeedsUnwrappedLine = true;
1122 }
1123 } else {
1124 addUnwrappedLine();
1125 ++Line->Level;
1126 parseStructuralElement();
1127 --Line->Level;
1128 }
1129 if (FormatTok->Tok.is(tok::kw_else)) {
1130 if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
1131 addUnwrappedLine();
1132 nextToken();
1133 if (FormatTok->Tok.is(tok::l_brace)) {
1134 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1135 parseBlock(/*MustBeDeclaration=*/false);
1136 addUnwrappedLine();
1137 } else if (FormatTok->Tok.is(tok::kw_if)) {
1138 parseIfThenElse();
1139 } else {
1140 addUnwrappedLine();
1141 ++Line->Level;
1142 parseStructuralElement();
1143 --Line->Level;
1144 }
1145 } else if (NeedsUnwrappedLine) {
1146 addUnwrappedLine();
1147 }
1148 }
1149
parseTryCatch()1150 void UnwrappedLineParser::parseTryCatch() {
1151 assert(FormatTok->is(tok::kw_try) && "'try' expected");
1152 nextToken();
1153 bool NeedsUnwrappedLine = false;
1154 if (FormatTok->is(tok::colon)) {
1155 // We are in a function try block, what comes is an initializer list.
1156 nextToken();
1157 while (FormatTok->is(tok::identifier)) {
1158 nextToken();
1159 if (FormatTok->is(tok::l_paren))
1160 parseParens();
1161 else
1162 StructuralError = true;
1163 if (FormatTok->is(tok::comma))
1164 nextToken();
1165 }
1166 }
1167 // Parse try with resource.
1168 if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
1169 parseParens();
1170 }
1171 if (FormatTok->is(tok::l_brace)) {
1172 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1173 parseBlock(/*MustBeDeclaration=*/false);
1174 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1175 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1176 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1177 addUnwrappedLine();
1178 } else {
1179 NeedsUnwrappedLine = true;
1180 }
1181 } else if (!FormatTok->is(tok::kw_catch)) {
1182 // The C++ standard requires a compound-statement after a try.
1183 // If there's none, we try to assume there's a structuralElement
1184 // and try to continue.
1185 StructuralError = true;
1186 addUnwrappedLine();
1187 ++Line->Level;
1188 parseStructuralElement();
1189 --Line->Level;
1190 }
1191 while (FormatTok->is(tok::kw_catch) ||
1192 ((Style.Language == FormatStyle::LK_Java ||
1193 Style.Language == FormatStyle::LK_JavaScript) &&
1194 FormatTok->is(Keywords.kw_finally))) {
1195 nextToken();
1196 while (FormatTok->isNot(tok::l_brace)) {
1197 if (FormatTok->is(tok::l_paren)) {
1198 parseParens();
1199 continue;
1200 }
1201 if (FormatTok->isOneOf(tok::semi, tok::r_brace))
1202 return;
1203 nextToken();
1204 }
1205 NeedsUnwrappedLine = false;
1206 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1207 parseBlock(/*MustBeDeclaration=*/false);
1208 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1209 Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
1210 Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
1211 addUnwrappedLine();
1212 } else {
1213 NeedsUnwrappedLine = true;
1214 }
1215 }
1216 if (NeedsUnwrappedLine) {
1217 addUnwrappedLine();
1218 }
1219 }
1220
parseNamespace()1221 void UnwrappedLineParser::parseNamespace() {
1222 assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
1223
1224 const FormatToken &InitialToken = *FormatTok;
1225 nextToken();
1226 if (FormatTok->Tok.is(tok::identifier))
1227 nextToken();
1228 if (FormatTok->Tok.is(tok::l_brace)) {
1229 if (ShouldBreakBeforeBrace(Style, InitialToken))
1230 addUnwrappedLine();
1231
1232 bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
1233 (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
1234 DeclarationScopeStack.size() > 1);
1235 parseBlock(/*MustBeDeclaration=*/true, AddLevel);
1236 // Munch the semicolon after a namespace. This is more common than one would
1237 // think. Puttin the semicolon into its own line is very ugly.
1238 if (FormatTok->Tok.is(tok::semi))
1239 nextToken();
1240 addUnwrappedLine();
1241 }
1242 // FIXME: Add error handling.
1243 }
1244
parseForOrWhileLoop()1245 void UnwrappedLineParser::parseForOrWhileLoop() {
1246 assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while) ||
1247 FormatTok->IsForEachMacro) &&
1248 "'for', 'while' or foreach macro expected");
1249 nextToken();
1250 if (FormatTok->Tok.is(tok::l_paren))
1251 parseParens();
1252 if (FormatTok->Tok.is(tok::l_brace)) {
1253 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1254 parseBlock(/*MustBeDeclaration=*/false);
1255 addUnwrappedLine();
1256 } else {
1257 addUnwrappedLine();
1258 ++Line->Level;
1259 parseStructuralElement();
1260 --Line->Level;
1261 }
1262 }
1263
parseDoWhile()1264 void UnwrappedLineParser::parseDoWhile() {
1265 assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
1266 nextToken();
1267 if (FormatTok->Tok.is(tok::l_brace)) {
1268 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1269 parseBlock(/*MustBeDeclaration=*/false);
1270 if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1271 addUnwrappedLine();
1272 } else {
1273 addUnwrappedLine();
1274 ++Line->Level;
1275 parseStructuralElement();
1276 --Line->Level;
1277 }
1278
1279 // FIXME: Add error handling.
1280 if (!FormatTok->Tok.is(tok::kw_while)) {
1281 addUnwrappedLine();
1282 return;
1283 }
1284
1285 nextToken();
1286 parseStructuralElement();
1287 }
1288
parseLabel()1289 void UnwrappedLineParser::parseLabel() {
1290 nextToken();
1291 unsigned OldLineLevel = Line->Level;
1292 if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
1293 --Line->Level;
1294 if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
1295 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1296 parseBlock(/*MustBeDeclaration=*/false);
1297 if (FormatTok->Tok.is(tok::kw_break)) {
1298 // "break;" after "}" on its own line only for BS_Allman and BS_GNU
1299 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1300 Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
1301 addUnwrappedLine();
1302 }
1303 parseStructuralElement();
1304 }
1305 addUnwrappedLine();
1306 } else {
1307 addUnwrappedLine();
1308 }
1309 Line->Level = OldLineLevel;
1310 }
1311
parseCaseLabel()1312 void UnwrappedLineParser::parseCaseLabel() {
1313 assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
1314 // FIXME: fix handling of complex expressions here.
1315 do {
1316 nextToken();
1317 } while (!eof() && !FormatTok->Tok.is(tok::colon));
1318 parseLabel();
1319 }
1320
parseSwitch()1321 void UnwrappedLineParser::parseSwitch() {
1322 assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
1323 nextToken();
1324 if (FormatTok->Tok.is(tok::l_paren))
1325 parseParens();
1326 if (FormatTok->Tok.is(tok::l_brace)) {
1327 CompoundStatementIndenter Indenter(this, Style, Line->Level);
1328 parseBlock(/*MustBeDeclaration=*/false);
1329 addUnwrappedLine();
1330 } else {
1331 addUnwrappedLine();
1332 ++Line->Level;
1333 parseStructuralElement();
1334 --Line->Level;
1335 }
1336 }
1337
parseAccessSpecifier()1338 void UnwrappedLineParser::parseAccessSpecifier() {
1339 nextToken();
1340 // Understand Qt's slots.
1341 if (FormatTok->is(tok::identifier) &&
1342 (FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
1343 nextToken();
1344 // Otherwise, we don't know what it is, and we'd better keep the next token.
1345 if (FormatTok->Tok.is(tok::colon))
1346 nextToken();
1347 addUnwrappedLine();
1348 }
1349
parseEnum()1350 void UnwrappedLineParser::parseEnum() {
1351 // Won't be 'enum' for NS_ENUMs.
1352 if (FormatTok->Tok.is(tok::kw_enum))
1353 nextToken();
1354
1355 // Eat up enum class ...
1356 if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
1357 nextToken();
1358 while (FormatTok->Tok.getIdentifierInfo() ||
1359 FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
1360 tok::greater, tok::comma, tok::question)) {
1361 nextToken();
1362 // We can have macros or attributes in between 'enum' and the enum name.
1363 if (FormatTok->is(tok::l_paren))
1364 parseParens();
1365 if (FormatTok->is(tok::identifier))
1366 nextToken();
1367 }
1368
1369 // Just a declaration or something is wrong.
1370 if (FormatTok->isNot(tok::l_brace))
1371 return;
1372 FormatTok->BlockKind = BK_Block;
1373
1374 if (Style.Language == FormatStyle::LK_Java) {
1375 // Java enums are different.
1376 parseJavaEnumBody();
1377 return;
1378 }
1379
1380 // Parse enum body.
1381 bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
1382 if (HasError) {
1383 if (FormatTok->is(tok::semi))
1384 nextToken();
1385 addUnwrappedLine();
1386 }
1387
1388 // We fall through to parsing a structural element afterwards, so that in
1389 // enum A {} n, m;
1390 // "} n, m;" will end up in one unwrapped line.
1391 }
1392
parseJavaEnumBody()1393 void UnwrappedLineParser::parseJavaEnumBody() {
1394 // Determine whether the enum is simple, i.e. does not have a semicolon or
1395 // constants with class bodies. Simple enums can be formatted like braced
1396 // lists, contracted to a single line, etc.
1397 unsigned StoredPosition = Tokens->getPosition();
1398 bool IsSimple = true;
1399 FormatToken *Tok = Tokens->getNextToken();
1400 while (Tok) {
1401 if (Tok->is(tok::r_brace))
1402 break;
1403 if (Tok->isOneOf(tok::l_brace, tok::semi)) {
1404 IsSimple = false;
1405 break;
1406 }
1407 // FIXME: This will also mark enums with braces in the arguments to enum
1408 // constants as "not simple". This is probably fine in practice, though.
1409 Tok = Tokens->getNextToken();
1410 }
1411 FormatTok = Tokens->setPosition(StoredPosition);
1412
1413 if (IsSimple) {
1414 parseBracedList();
1415 addUnwrappedLine();
1416 return;
1417 }
1418
1419 // Parse the body of a more complex enum.
1420 // First add a line for everything up to the "{".
1421 nextToken();
1422 addUnwrappedLine();
1423 ++Line->Level;
1424
1425 // Parse the enum constants.
1426 while (FormatTok) {
1427 if (FormatTok->is(tok::l_brace)) {
1428 // Parse the constant's class body.
1429 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1430 /*MunchSemi=*/false);
1431 } else if (FormatTok->is(tok::l_paren)) {
1432 parseParens();
1433 } else if (FormatTok->is(tok::comma)) {
1434 nextToken();
1435 addUnwrappedLine();
1436 } else if (FormatTok->is(tok::semi)) {
1437 nextToken();
1438 addUnwrappedLine();
1439 break;
1440 } else if (FormatTok->is(tok::r_brace)) {
1441 addUnwrappedLine();
1442 break;
1443 } else {
1444 nextToken();
1445 }
1446 }
1447
1448 // Parse the class body after the enum's ";" if any.
1449 parseLevel(/*HasOpeningBrace=*/true);
1450 nextToken();
1451 --Line->Level;
1452 addUnwrappedLine();
1453 }
1454
parseRecord()1455 void UnwrappedLineParser::parseRecord() {
1456 const FormatToken &InitialToken = *FormatTok;
1457 nextToken();
1458 if (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::kw___attribute,
1459 tok::kw___declspec, tok::kw_alignas)) {
1460 nextToken();
1461 // We can have macros or attributes in between 'class' and the class name.
1462 if (FormatTok->Tok.is(tok::l_paren)) {
1463 parseParens();
1464 }
1465 // The actual identifier can be a nested name specifier, and in macros
1466 // it is often token-pasted.
1467 while (FormatTok->is(tok::identifier) || FormatTok->is(tok::coloncolon) ||
1468 FormatTok->is(tok::hashhash) ||
1469 (Style.Language == FormatStyle::LK_Java &&
1470 FormatTok->isOneOf(tok::period, tok::comma)))
1471 nextToken();
1472
1473 // Note that parsing away template declarations here leads to incorrectly
1474 // accepting function declarations as record declarations.
1475 // In general, we cannot solve this problem. Consider:
1476 // class A<int> B() {}
1477 // which can be a function definition or a class definition when B() is a
1478 // macro. If we find enough real-world cases where this is a problem, we
1479 // can parse for the 'template' keyword in the beginning of the statement,
1480 // and thus rule out the record production in case there is no template
1481 // (this would still leave us with an ambiguity between template function
1482 // and class declarations).
1483 if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
1484 while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
1485 if (FormatTok->Tok.is(tok::semi))
1486 return;
1487 nextToken();
1488 }
1489 }
1490 }
1491 if (FormatTok->Tok.is(tok::l_brace)) {
1492 if (ShouldBreakBeforeBrace(Style, InitialToken))
1493 addUnwrappedLine();
1494
1495 parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
1496 /*MunchSemi=*/false);
1497 }
1498 // We fall through to parsing a structural element afterwards, so
1499 // class A {} n, m;
1500 // will end up in one unwrapped line.
1501 // This does not apply for Java.
1502 if (Style.Language == FormatStyle::LK_Java)
1503 addUnwrappedLine();
1504 }
1505
parseObjCProtocolList()1506 void UnwrappedLineParser::parseObjCProtocolList() {
1507 assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
1508 do
1509 nextToken();
1510 while (!eof() && FormatTok->Tok.isNot(tok::greater));
1511 nextToken(); // Skip '>'.
1512 }
1513
parseObjCUntilAtEnd()1514 void UnwrappedLineParser::parseObjCUntilAtEnd() {
1515 do {
1516 if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
1517 nextToken();
1518 addUnwrappedLine();
1519 break;
1520 }
1521 if (FormatTok->is(tok::l_brace)) {
1522 parseBlock(/*MustBeDeclaration=*/false);
1523 // In ObjC interfaces, nothing should be following the "}".
1524 addUnwrappedLine();
1525 } else if (FormatTok->is(tok::r_brace)) {
1526 // Ignore stray "}". parseStructuralElement doesn't consume them.
1527 nextToken();
1528 addUnwrappedLine();
1529 } else {
1530 parseStructuralElement();
1531 }
1532 } while (!eof());
1533 }
1534
parseObjCInterfaceOrImplementation()1535 void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
1536 nextToken();
1537 nextToken(); // interface name
1538
1539 // @interface can be followed by either a base class, or a category.
1540 if (FormatTok->Tok.is(tok::colon)) {
1541 nextToken();
1542 nextToken(); // base class name
1543 } else if (FormatTok->Tok.is(tok::l_paren))
1544 // Skip category, if present.
1545 parseParens();
1546
1547 if (FormatTok->Tok.is(tok::less))
1548 parseObjCProtocolList();
1549
1550 if (FormatTok->Tok.is(tok::l_brace)) {
1551 if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
1552 Style.BreakBeforeBraces == FormatStyle::BS_GNU)
1553 addUnwrappedLine();
1554 parseBlock(/*MustBeDeclaration=*/true);
1555 }
1556
1557 // With instance variables, this puts '}' on its own line. Without instance
1558 // variables, this ends the @interface line.
1559 addUnwrappedLine();
1560
1561 parseObjCUntilAtEnd();
1562 }
1563
parseObjCProtocol()1564 void UnwrappedLineParser::parseObjCProtocol() {
1565 nextToken();
1566 nextToken(); // protocol name
1567
1568 if (FormatTok->Tok.is(tok::less))
1569 parseObjCProtocolList();
1570
1571 // Check for protocol declaration.
1572 if (FormatTok->Tok.is(tok::semi)) {
1573 nextToken();
1574 return addUnwrappedLine();
1575 }
1576
1577 addUnwrappedLine();
1578 parseObjCUntilAtEnd();
1579 }
1580
printDebugInfo(const UnwrappedLine & Line,StringRef Prefix="")1581 LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
1582 StringRef Prefix = "") {
1583 llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
1584 << (Line.InPPDirective ? " MACRO" : "") << ": ";
1585 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1586 E = Line.Tokens.end();
1587 I != E; ++I) {
1588 llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
1589 }
1590 for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
1591 E = Line.Tokens.end();
1592 I != E; ++I) {
1593 const UnwrappedLineNode &Node = *I;
1594 for (SmallVectorImpl<UnwrappedLine>::const_iterator
1595 I = Node.Children.begin(),
1596 E = Node.Children.end();
1597 I != E; ++I) {
1598 printDebugInfo(*I, "\nChild: ");
1599 }
1600 }
1601 llvm::dbgs() << "\n";
1602 }
1603
addUnwrappedLine()1604 void UnwrappedLineParser::addUnwrappedLine() {
1605 if (Line->Tokens.empty())
1606 return;
1607 DEBUG({
1608 if (CurrentLines == &Lines)
1609 printDebugInfo(*Line);
1610 });
1611 CurrentLines->push_back(*Line);
1612 Line->Tokens.clear();
1613 if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
1614 for (SmallVectorImpl<UnwrappedLine>::iterator
1615 I = PreprocessorDirectives.begin(),
1616 E = PreprocessorDirectives.end();
1617 I != E; ++I) {
1618 CurrentLines->push_back(*I);
1619 }
1620 PreprocessorDirectives.clear();
1621 }
1622 }
1623
eof() const1624 bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
1625
isOnNewLine(const FormatToken & FormatTok)1626 bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
1627 return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
1628 FormatTok.NewlinesBefore > 0;
1629 }
1630
flushComments(bool NewlineBeforeNext)1631 void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
1632 bool JustComments = Line->Tokens.empty();
1633 for (SmallVectorImpl<FormatToken *>::const_iterator
1634 I = CommentsBeforeNextToken.begin(),
1635 E = CommentsBeforeNextToken.end();
1636 I != E; ++I) {
1637 if (isOnNewLine(**I) && JustComments) {
1638 addUnwrappedLine();
1639 }
1640 pushToken(*I);
1641 }
1642 if (NewlineBeforeNext && JustComments) {
1643 addUnwrappedLine();
1644 }
1645 CommentsBeforeNextToken.clear();
1646 }
1647
nextToken()1648 void UnwrappedLineParser::nextToken() {
1649 if (eof())
1650 return;
1651 flushComments(isOnNewLine(*FormatTok));
1652 pushToken(FormatTok);
1653 readToken();
1654 }
1655
readToken()1656 void UnwrappedLineParser::readToken() {
1657 bool CommentsInCurrentLine = true;
1658 do {
1659 FormatTok = Tokens->getNextToken();
1660 assert(FormatTok);
1661 while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
1662 (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
1663 // If there is an unfinished unwrapped line, we flush the preprocessor
1664 // directives only after that unwrapped line was finished later.
1665 bool SwitchToPreprocessorLines =
1666 !Line->Tokens.empty() && CurrentLines == &Lines;
1667 ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
1668 // Comments stored before the preprocessor directive need to be output
1669 // before the preprocessor directive, at the same level as the
1670 // preprocessor directive, as we consider them to apply to the directive.
1671 flushComments(isOnNewLine(*FormatTok));
1672 parsePPDirective();
1673 }
1674 while (FormatTok->Type == TT_ConflictStart ||
1675 FormatTok->Type == TT_ConflictEnd ||
1676 FormatTok->Type == TT_ConflictAlternative) {
1677 if (FormatTok->Type == TT_ConflictStart) {
1678 conditionalCompilationStart(/*Unreachable=*/false);
1679 } else if (FormatTok->Type == TT_ConflictAlternative) {
1680 conditionalCompilationAlternative();
1681 } else if (FormatTok->Type == TT_ConflictEnd) {
1682 conditionalCompilationEnd();
1683 }
1684 FormatTok = Tokens->getNextToken();
1685 FormatTok->MustBreakBefore = true;
1686 }
1687
1688 if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
1689 !Line->InPPDirective) {
1690 continue;
1691 }
1692
1693 if (!FormatTok->Tok.is(tok::comment))
1694 return;
1695 if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
1696 CommentsInCurrentLine = false;
1697 }
1698 if (CommentsInCurrentLine) {
1699 pushToken(FormatTok);
1700 } else {
1701 CommentsBeforeNextToken.push_back(FormatTok);
1702 }
1703 } while (!eof());
1704 }
1705
pushToken(FormatToken * Tok)1706 void UnwrappedLineParser::pushToken(FormatToken *Tok) {
1707 Line->Tokens.push_back(UnwrappedLineNode(Tok));
1708 if (MustBreakBeforeNextToken) {
1709 Line->Tokens.back().Tok->MustBreakBefore = true;
1710 MustBreakBeforeNextToken = false;
1711 }
1712 }
1713
1714 } // end namespace format
1715 } // end namespace clang
1716