2012-12-04 02:12:45 +08:00
|
|
|
//===--- UnwrappedLineParser.cpp - Format C++ code ------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
|
|
|
/// \brief This file contains the implementation of the UnwrappedLineParser,
|
|
|
|
/// which turns a stream of tokens into UnwrappedLines.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-19 16:09:44 +08:00
|
|
|
#include "UnwrappedLineParser.h"
|
2015-03-02 05:36:40 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-01-16 20:31:12 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2015-03-24 02:05:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2014-04-22 11:17:02 +08:00
|
|
|
#define DEBUG_TYPE "format-parser"
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
class FormatTokenSource {
|
|
|
|
public:
|
2015-10-20 21:23:58 +08:00
|
|
|
virtual ~FormatTokenSource() {}
|
2013-05-28 19:55:06 +08:00
|
|
|
virtual FormatToken *getNextToken() = 0;
|
|
|
|
|
|
|
|
virtual unsigned getPosition() = 0;
|
|
|
|
virtual FormatToken *setPosition(unsigned Position) = 0;
|
|
|
|
};
|
|
|
|
|
2013-07-01 12:21:54 +08:00
|
|
|
namespace {
|
|
|
|
|
2013-01-23 17:32:48 +08:00
|
|
|
class ScopedDeclarationState {
|
|
|
|
public:
|
|
|
|
ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
|
|
|
|
bool MustBeDeclaration)
|
|
|
|
: Line(Line), Stack(Stack) {
|
|
|
|
Line.MustBeDeclaration = MustBeDeclaration;
|
2013-01-23 19:03:04 +08:00
|
|
|
Stack.push_back(MustBeDeclaration);
|
2013-01-23 17:32:48 +08:00
|
|
|
}
|
|
|
|
~ScopedDeclarationState() {
|
|
|
|
Stack.pop_back();
|
2013-01-23 22:08:21 +08:00
|
|
|
if (!Stack.empty())
|
|
|
|
Line.MustBeDeclaration = Stack.back();
|
|
|
|
else
|
|
|
|
Line.MustBeDeclaration = true;
|
2013-01-23 17:32:48 +08:00
|
|
|
}
|
2013-05-31 22:56:29 +08:00
|
|
|
|
2013-01-23 17:32:48 +08:00
|
|
|
private:
|
|
|
|
UnwrappedLine &Line;
|
|
|
|
std::vector<bool> &Stack;
|
|
|
|
};
|
|
|
|
|
2013-01-05 07:34:14 +08:00
|
|
|
class ScopedMacroState : public FormatTokenSource {
|
|
|
|
public:
|
|
|
|
ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
|
2015-05-06 19:56:29 +08:00
|
|
|
FormatToken *&ResetToken)
|
2013-01-05 07:34:14 +08:00
|
|
|
: Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
|
2013-04-12 22:13:36 +08:00
|
|
|
PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource),
|
2015-05-06 19:56:29 +08:00
|
|
|
Token(nullptr) {
|
2013-01-05 07:34:14 +08:00
|
|
|
TokenSource = this;
|
2013-01-06 06:14:16 +08:00
|
|
|
Line.Level = 0;
|
2013-01-05 07:34:14 +08:00
|
|
|
Line.InPPDirective = true;
|
|
|
|
}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
~ScopedMacroState() override {
|
2013-01-05 07:34:14 +08:00
|
|
|
TokenSource = PreviousTokenSource;
|
|
|
|
ResetToken = Token;
|
|
|
|
Line.InPPDirective = false;
|
2013-01-06 06:14:16 +08:00
|
|
|
Line.Level = PreviousLineLevel;
|
2013-01-05 07:34:14 +08:00
|
|
|
}
|
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
FormatToken *getNextToken() override {
|
2013-01-07 18:03:37 +08:00
|
|
|
// The \c UnwrappedLineParser guards against this by never calling
|
|
|
|
// \c getNextToken() after it has encountered the first eof token.
|
|
|
|
assert(!eof());
|
2013-01-05 07:34:14 +08:00
|
|
|
Token = PreviousTokenSource->getNextToken();
|
|
|
|
if (eof())
|
2013-05-28 19:55:06 +08:00
|
|
|
return getFakeEOF();
|
2013-01-05 07:34:14 +08:00
|
|
|
return Token;
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
unsigned getPosition() override { return PreviousTokenSource->getPosition(); }
|
2013-05-23 17:41:43 +08:00
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
FormatToken *setPosition(unsigned Position) override {
|
2013-05-23 17:41:43 +08:00
|
|
|
Token = PreviousTokenSource->setPosition(Position);
|
|
|
|
return Token;
|
|
|
|
}
|
|
|
|
|
2013-01-05 07:34:14 +08:00
|
|
|
private:
|
2013-05-28 19:55:06 +08:00
|
|
|
bool eof() { return Token && Token->HasUnescapedNewline; }
|
|
|
|
|
|
|
|
FormatToken *getFakeEOF() {
|
|
|
|
static bool EOFInitialized = false;
|
|
|
|
static FormatToken FormatTok;
|
|
|
|
if (!EOFInitialized) {
|
|
|
|
FormatTok.Tok.startToken();
|
|
|
|
FormatTok.Tok.setKind(tok::eof);
|
|
|
|
EOFInitialized = true;
|
|
|
|
}
|
|
|
|
return &FormatTok;
|
2013-01-05 07:34:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UnwrappedLine &Line;
|
|
|
|
FormatTokenSource *&TokenSource;
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatToken *&ResetToken;
|
2013-01-06 06:14:16 +08:00
|
|
|
unsigned PreviousLineLevel;
|
2013-01-05 07:34:14 +08:00
|
|
|
FormatTokenSource *PreviousTokenSource;
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatToken *Token;
|
2013-01-05 07:34:14 +08:00
|
|
|
};
|
|
|
|
|
2013-07-01 12:21:54 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2013-01-10 19:52:21 +08:00
|
|
|
class ScopedLineState {
|
|
|
|
public:
|
2013-01-18 22:04:34 +08:00
|
|
|
ScopedLineState(UnwrappedLineParser &Parser,
|
|
|
|
bool SwitchToPreprocessorLines = false)
|
2014-08-10 04:02:07 +08:00
|
|
|
: Parser(Parser), OriginalLines(Parser.CurrentLines) {
|
2013-01-18 22:04:34 +08:00
|
|
|
if (SwitchToPreprocessorLines)
|
|
|
|
Parser.CurrentLines = &Parser.PreprocessorDirectives;
|
2013-09-05 17:29:45 +08:00
|
|
|
else if (!Parser.Line->Tokens.empty())
|
|
|
|
Parser.CurrentLines = &Parser.Line->Tokens.back().Children;
|
2014-08-10 04:02:07 +08:00
|
|
|
PreBlockLine = std::move(Parser.Line);
|
|
|
|
Parser.Line = llvm::make_unique<UnwrappedLine>();
|
2013-01-16 17:10:19 +08:00
|
|
|
Parser.Line->Level = PreBlockLine->Level;
|
|
|
|
Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
|
2013-01-10 19:52:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedLineState() {
|
2013-01-16 17:10:19 +08:00
|
|
|
if (!Parser.Line->Tokens.empty()) {
|
2013-01-10 19:52:21 +08:00
|
|
|
Parser.addUnwrappedLine();
|
|
|
|
}
|
2013-01-16 17:10:19 +08:00
|
|
|
assert(Parser.Line->Tokens.empty());
|
2014-08-10 04:02:07 +08:00
|
|
|
Parser.Line = std::move(PreBlockLine);
|
2013-09-05 17:29:45 +08:00
|
|
|
if (Parser.CurrentLines == &Parser.PreprocessorDirectives)
|
|
|
|
Parser.MustBreakBeforeNextToken = true;
|
|
|
|
Parser.CurrentLines = OriginalLines;
|
2013-01-10 19:52:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
UnwrappedLineParser &Parser;
|
|
|
|
|
2014-08-10 04:02:07 +08:00
|
|
|
std::unique_ptr<UnwrappedLine> PreBlockLine;
|
2013-09-05 17:29:45 +08:00
|
|
|
SmallVectorImpl<UnwrappedLine> *OriginalLines;
|
2013-01-10 19:52:21 +08:00
|
|
|
};
|
|
|
|
|
2013-12-12 17:49:52 +08:00
|
|
|
class CompoundStatementIndenter {
|
|
|
|
public:
|
|
|
|
CompoundStatementIndenter(UnwrappedLineParser *Parser,
|
|
|
|
const FormatStyle &Style, unsigned &LineLevel)
|
|
|
|
: LineLevel(LineLevel), OldLineLevel(LineLevel) {
|
2015-09-29 22:57:55 +08:00
|
|
|
if (Style.BraceWrapping.AfterControlStatement)
|
2013-12-12 17:49:52 +08:00
|
|
|
Parser->addUnwrappedLine();
|
2015-09-29 22:57:55 +08:00
|
|
|
if (Style.BraceWrapping.IndentBraces)
|
2013-12-12 17:49:52 +08:00
|
|
|
++LineLevel;
|
|
|
|
}
|
2014-05-09 21:11:16 +08:00
|
|
|
~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
|
2013-12-12 17:49:52 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned &LineLevel;
|
|
|
|
unsigned OldLineLevel;
|
|
|
|
};
|
|
|
|
|
2013-07-01 12:21:54 +08:00
|
|
|
namespace {
|
|
|
|
|
2013-05-23 17:41:43 +08:00
|
|
|
class IndexedTokenSource : public FormatTokenSource {
|
|
|
|
public:
|
2013-05-28 19:55:06 +08:00
|
|
|
IndexedTokenSource(ArrayRef<FormatToken *> Tokens)
|
2013-05-23 17:41:43 +08:00
|
|
|
: Tokens(Tokens), Position(-1) {}
|
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
FormatToken *getNextToken() override {
|
2013-05-23 17:41:43 +08:00
|
|
|
++Position;
|
|
|
|
return Tokens[Position];
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
unsigned getPosition() override {
|
2013-05-23 17:41:43 +08:00
|
|
|
assert(Position >= 0);
|
|
|
|
return Position;
|
|
|
|
}
|
|
|
|
|
2014-03-15 12:29:04 +08:00
|
|
|
FormatToken *setPosition(unsigned P) override {
|
2013-05-23 17:41:43 +08:00
|
|
|
Position = P;
|
|
|
|
return Tokens[Position];
|
|
|
|
}
|
|
|
|
|
2013-10-12 05:25:45 +08:00
|
|
|
void reset() { Position = -1; }
|
|
|
|
|
2013-05-23 17:41:43 +08:00
|
|
|
private:
|
2013-05-28 19:55:06 +08:00
|
|
|
ArrayRef<FormatToken *> Tokens;
|
2013-05-23 17:41:43 +08:00
|
|
|
int Position;
|
|
|
|
};
|
|
|
|
|
2013-07-01 12:21:54 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2013-05-15 16:14:19 +08:00
|
|
|
UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
|
2014-11-04 20:41:02 +08:00
|
|
|
const AdditionalKeywords &Keywords,
|
2013-05-28 19:55:06 +08:00
|
|
|
ArrayRef<FormatToken *> Tokens,
|
2013-05-15 16:14:19 +08:00
|
|
|
UnwrappedLineConsumer &Callback)
|
2014-05-09 21:11:16 +08:00
|
|
|
: Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
|
2015-05-06 19:56:29 +08:00
|
|
|
CurrentLines(&Lines), Style(Style), Keywords(Keywords), Tokens(nullptr),
|
|
|
|
Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
|
2013-10-12 05:25:45 +08:00
|
|
|
|
|
|
|
void UnwrappedLineParser::reset() {
|
|
|
|
PPBranchLevel = -1;
|
|
|
|
Line.reset(new UnwrappedLine);
|
|
|
|
CommentsBeforeNextToken.clear();
|
2014-05-09 16:15:10 +08:00
|
|
|
FormatTok = nullptr;
|
2013-10-12 05:25:45 +08:00
|
|
|
MustBreakBeforeNextToken = false;
|
|
|
|
PreprocessorDirectives.clear();
|
|
|
|
CurrentLines = &Lines;
|
|
|
|
DeclarationScopeStack.clear();
|
|
|
|
PPStack.clear();
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2015-05-06 19:56:29 +08:00
|
|
|
void UnwrappedLineParser::parse() {
|
2013-05-23 17:41:43 +08:00
|
|
|
IndexedTokenSource TokenSource(AllTokens);
|
2013-10-12 05:25:45 +08:00
|
|
|
do {
|
|
|
|
DEBUG(llvm::dbgs() << "----\n");
|
|
|
|
reset();
|
|
|
|
Tokens = &TokenSource;
|
|
|
|
TokenSource.reset();
|
|
|
|
|
|
|
|
readToken();
|
|
|
|
parseFile();
|
|
|
|
// Create line with eof token.
|
|
|
|
pushToken(FormatTok);
|
|
|
|
addUnwrappedLine();
|
|
|
|
|
|
|
|
for (SmallVectorImpl<UnwrappedLine>::iterator I = Lines.begin(),
|
|
|
|
E = Lines.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
Callback.consumeUnwrappedLine(*I);
|
|
|
|
}
|
|
|
|
Callback.finishRun();
|
|
|
|
Lines.clear();
|
|
|
|
while (!PPLevelBranchIndex.empty() &&
|
2013-10-12 21:32:56 +08:00
|
|
|
PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
|
2013-10-12 05:25:45 +08:00
|
|
|
PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
|
|
|
|
PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
|
|
|
|
}
|
|
|
|
if (!PPLevelBranchIndex.empty()) {
|
|
|
|
++PPLevelBranchIndex.back();
|
|
|
|
assert(PPLevelBranchIndex.size() == PPLevelBranchCount.size());
|
|
|
|
assert(PPLevelBranchIndex.back() <= PPLevelBranchCount.back());
|
|
|
|
}
|
|
|
|
} while (!PPLevelBranchIndex.empty());
|
2013-01-05 07:34:14 +08:00
|
|
|
}
|
|
|
|
|
2013-04-12 22:13:36 +08:00
|
|
|
void UnwrappedLineParser::parseFile() {
|
2015-05-05 16:40:32 +08:00
|
|
|
// The top-level context in a file always has declarations, except for pre-
|
|
|
|
// processor directives and JavaScript files.
|
|
|
|
bool MustBeDeclaration =
|
|
|
|
!Line->InPPDirective && Style.Language != FormatStyle::LK_JavaScript;
|
|
|
|
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
|
|
|
|
MustBeDeclaration);
|
2013-06-26 08:30:14 +08:00
|
|
|
parseLevel(/*HasOpeningBrace=*/false);
|
2013-01-05 07:34:14 +08:00
|
|
|
// Make sure to format the remaining tokens.
|
2013-01-23 00:31:55 +08:00
|
|
|
flushComments(true);
|
2013-01-05 07:34:14 +08:00
|
|
|
addUnwrappedLine();
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-04-12 22:13:36 +08:00
|
|
|
void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
|
2013-07-25 19:31:57 +08:00
|
|
|
bool SwitchLabelEncountered = false;
|
2012-12-04 02:12:45 +08:00
|
|
|
do {
|
2015-07-04 01:25:16 +08:00
|
|
|
tok::TokenKind kind = FormatTok->Tok.getKind();
|
|
|
|
if (FormatTok->Type == TT_MacroBlockBegin) {
|
|
|
|
kind = tok::l_brace;
|
|
|
|
} else if (FormatTok->Type == TT_MacroBlockEnd) {
|
|
|
|
kind = tok::r_brace;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (kind) {
|
2012-12-04 02:12:45 +08:00
|
|
|
case tok::comment:
|
2012-12-17 19:29:41 +08:00
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
2012-12-04 02:12:45 +08:00
|
|
|
break;
|
|
|
|
case tok::l_brace:
|
2013-01-23 17:32:48 +08:00
|
|
|
// FIXME: Add parameter whether this can happen - if this happens, we must
|
|
|
|
// be in a non-declaration context.
|
2015-08-24 21:23:37 +08:00
|
|
|
if (!FormatTok->is(TT_MacroBlockBegin) && tryToParseBracedList())
|
|
|
|
continue;
|
2013-06-26 08:30:14 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/false);
|
2012-12-04 02:12:45 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
break;
|
|
|
|
case tok::r_brace:
|
2013-04-12 22:13:36 +08:00
|
|
|
if (HasOpeningBrace)
|
|
|
|
return;
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
2013-01-07 04:07:31 +08:00
|
|
|
break;
|
2013-07-25 19:31:57 +08:00
|
|
|
case tok::kw_default:
|
|
|
|
case tok::kw_case:
|
2013-09-02 16:26:29 +08:00
|
|
|
if (!SwitchLabelEncountered &&
|
|
|
|
(Style.IndentCaseLabels || (Line->InPPDirective && Line->Level == 1)))
|
|
|
|
++Line->Level;
|
2013-07-25 19:31:57 +08:00
|
|
|
SwitchLabelEncountered = true;
|
|
|
|
parseStructuralElement();
|
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
default:
|
2013-01-07 22:56:16 +08:00
|
|
|
parseStructuralElement();
|
2012-12-04 02:12:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
2015-05-18 20:52:00 +08:00
|
|
|
void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
|
2013-05-23 17:41:43 +08:00
|
|
|
// We'll parse forward through the tokens until we hit
|
|
|
|
// a closing brace or eof - note that getNextToken() will
|
|
|
|
// parse macros, so this will magically work inside macro
|
|
|
|
// definitions, too.
|
|
|
|
unsigned StoredPosition = Tokens->getPosition();
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatToken *Tok = FormatTok;
|
2016-01-09 23:56:28 +08:00
|
|
|
const FormatToken *PrevTok = getPreviousToken();
|
2013-05-23 17:41:43 +08:00
|
|
|
// Keep a stack of positions of lbrace tokens. We will
|
|
|
|
// update information about whether an lbrace starts a
|
|
|
|
// braced init list or a different block during the loop.
|
2013-07-09 17:06:29 +08:00
|
|
|
SmallVector<FormatToken *, 8> LBraceStack;
|
2013-05-28 19:55:06 +08:00
|
|
|
assert(Tok->Tok.is(tok::l_brace));
|
2013-05-23 17:41:43 +08:00
|
|
|
do {
|
2015-12-22 02:31:15 +08:00
|
|
|
// Get next non-comment token.
|
2013-07-01 17:15:46 +08:00
|
|
|
FormatToken *NextTok;
|
2013-07-02 00:43:38 +08:00
|
|
|
unsigned ReadTokens = 0;
|
2013-07-01 17:15:46 +08:00
|
|
|
do {
|
|
|
|
NextTok = Tokens->getNextToken();
|
2013-07-02 00:43:38 +08:00
|
|
|
++ReadTokens;
|
2013-07-01 17:15:46 +08:00
|
|
|
} while (NextTok->is(tok::comment));
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
switch (Tok->Tok.getKind()) {
|
2013-05-23 17:41:43 +08:00
|
|
|
case tok::l_brace:
|
2016-01-09 23:56:28 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript && PrevTok &&
|
|
|
|
PrevTok->is(tok::colon))
|
|
|
|
// In TypeScript's TypeMemberLists, there can be semicolons between the
|
|
|
|
// individual members.
|
|
|
|
Tok->BlockKind = BK_BracedInit;
|
|
|
|
else
|
|
|
|
Tok->BlockKind = BK_Unknown;
|
2013-07-09 17:06:29 +08:00
|
|
|
LBraceStack.push_back(Tok);
|
2013-05-23 17:41:43 +08:00
|
|
|
break;
|
|
|
|
case tok::r_brace:
|
2016-01-09 23:56:28 +08:00
|
|
|
if (LBraceStack.empty())
|
|
|
|
break;
|
|
|
|
if (LBraceStack.back()->BlockKind == BK_Unknown) {
|
|
|
|
bool ProbablyBracedList = false;
|
|
|
|
if (Style.Language == FormatStyle::LK_Proto) {
|
|
|
|
ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
|
|
|
|
} else {
|
|
|
|
// Using OriginalColumn to distinguish between ObjC methods and
|
|
|
|
// binary operators is a bit hacky.
|
|
|
|
bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
|
|
|
|
NextTok->OriginalColumn == 0;
|
|
|
|
|
|
|
|
// If there is a comma, semicolon or right paren after the closing
|
|
|
|
// brace, we assume this is a braced initializer list. Note that
|
|
|
|
// regardless how we mark inner braces here, we will overwrite the
|
|
|
|
// BlockKind later if we parse a braced list (where all blocks
|
|
|
|
// inside are by default braced lists), or when we explicitly detect
|
|
|
|
// blocks (for example while parsing lambdas).
|
|
|
|
//
|
|
|
|
// We exclude + and - as they can be ObjC visibility modifiers.
|
|
|
|
ProbablyBracedList =
|
2016-03-06 02:34:26 +08:00
|
|
|
(Style.Language == FormatStyle::LK_JavaScript &&
|
|
|
|
NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in)) ||
|
2016-01-09 23:56:28 +08:00
|
|
|
NextTok->isOneOf(tok::comma, tok::period, tok::colon,
|
|
|
|
tok::r_paren, tok::r_square, tok::l_brace,
|
|
|
|
tok::l_square, tok::l_paren, tok::ellipsis) ||
|
|
|
|
(NextTok->is(tok::semi) &&
|
|
|
|
(!ExpectClassBody || LBraceStack.size() != 1)) ||
|
|
|
|
(NextTok->isBinaryOperator() && !NextIsObjCMethod);
|
|
|
|
}
|
|
|
|
if (ProbablyBracedList) {
|
|
|
|
Tok->BlockKind = BK_BracedInit;
|
|
|
|
LBraceStack.back()->BlockKind = BK_BracedInit;
|
|
|
|
} else {
|
|
|
|
Tok->BlockKind = BK_Block;
|
|
|
|
LBraceStack.back()->BlockKind = BK_Block;
|
2013-05-23 17:41:43 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-09 23:56:28 +08:00
|
|
|
LBraceStack.pop_back();
|
2013-05-23 17:41:43 +08:00
|
|
|
break;
|
2014-03-13 18:11:17 +08:00
|
|
|
case tok::at:
|
2013-05-23 17:41:43 +08:00
|
|
|
case tok::semi:
|
|
|
|
case tok::kw_if:
|
|
|
|
case tok::kw_while:
|
|
|
|
case tok::kw_for:
|
|
|
|
case tok::kw_switch:
|
|
|
|
case tok::kw_try:
|
2015-02-04 23:26:27 +08:00
|
|
|
case tok::kw___try:
|
2016-01-09 23:56:28 +08:00
|
|
|
if (!LBraceStack.empty() && LBraceStack.back()->BlockKind == BK_Unknown)
|
2013-07-09 17:06:29 +08:00
|
|
|
LBraceStack.back()->BlockKind = BK_Block;
|
2013-05-23 17:41:43 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-01-09 23:56:28 +08:00
|
|
|
PrevTok = Tok;
|
2013-05-23 17:41:43 +08:00
|
|
|
Tok = NextTok;
|
2013-09-04 16:20:47 +08:00
|
|
|
} while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
|
2016-01-09 23:56:28 +08:00
|
|
|
|
2013-05-23 17:41:43 +08:00
|
|
|
// Assume other blocks for all unclosed opening braces.
|
|
|
|
for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
|
2013-07-09 17:06:29 +08:00
|
|
|
if (LBraceStack[i]->BlockKind == BK_Unknown)
|
|
|
|
LBraceStack[i]->BlockKind = BK_Block;
|
2013-05-23 17:41:43 +08:00
|
|
|
}
|
2013-09-04 16:20:47 +08:00
|
|
|
|
2013-05-23 17:41:43 +08:00
|
|
|
FormatTok = Tokens->setPosition(StoredPosition);
|
|
|
|
}
|
|
|
|
|
2013-10-13 06:46:56 +08:00
|
|
|
void UnwrappedLineParser::parseBlock(bool MustBeDeclaration, bool AddLevel,
|
|
|
|
bool MunchSemi) {
|
2015-07-04 01:25:16 +08:00
|
|
|
assert(FormatTok->isOneOf(tok::l_brace, TT_MacroBlockBegin) &&
|
|
|
|
"'{' or macro block token expected");
|
|
|
|
const bool MacroBlock = FormatTok->is(TT_MacroBlockBegin);
|
2015-12-22 02:31:15 +08:00
|
|
|
FormatTok->BlockKind = BK_Block;
|
2015-07-04 01:25:16 +08:00
|
|
|
|
2013-07-25 19:31:57 +08:00
|
|
|
unsigned InitialLevel = Line->Level;
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
|
|
|
|
2015-07-04 01:25:16 +08:00
|
|
|
if (MacroBlock && FormatTok->is(tok::l_paren))
|
|
|
|
parseParens();
|
|
|
|
|
2013-01-22 00:42:44 +08:00
|
|
|
addUnwrappedLine();
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2013-01-23 17:32:48 +08:00
|
|
|
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
|
|
|
|
MustBeDeclaration);
|
2013-08-01 07:16:02 +08:00
|
|
|
if (AddLevel)
|
|
|
|
++Line->Level;
|
2013-06-26 08:30:14 +08:00
|
|
|
parseLevel(/*HasOpeningBrace=*/true);
|
2012-12-07 02:03:27 +08:00
|
|
|
|
2016-04-14 22:56:49 +08:00
|
|
|
if (eof())
|
|
|
|
return;
|
|
|
|
|
2015-07-04 01:25:16 +08:00
|
|
|
if (MacroBlock ? !FormatTok->is(TT_MacroBlockEnd)
|
|
|
|
: !FormatTok->is(tok::r_brace)) {
|
2013-07-25 19:31:57 +08:00
|
|
|
Line->Level = InitialLevel;
|
2015-12-22 02:31:15 +08:00
|
|
|
FormatTok->BlockKind = BK_Block;
|
2013-04-12 22:13:36 +08:00
|
|
|
return;
|
2013-01-23 00:31:55 +08:00
|
|
|
}
|
2012-12-04 23:40:36 +08:00
|
|
|
|
2013-03-20 20:37:50 +08:00
|
|
|
nextToken(); // Munch the closing brace.
|
2015-07-04 01:25:16 +08:00
|
|
|
|
|
|
|
if (MacroBlock && FormatTok->is(tok::l_paren))
|
|
|
|
parseParens();
|
|
|
|
|
2013-10-13 06:46:56 +08:00
|
|
|
if (MunchSemi && FormatTok->Tok.is(tok::semi))
|
|
|
|
nextToken();
|
2013-07-25 19:31:57 +08:00
|
|
|
Line->Level = InitialLevel;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2015-03-30 17:56:50 +08:00
|
|
|
static bool isGoogScope(const UnwrappedLine &Line) {
|
2014-11-24 00:46:28 +08:00
|
|
|
// FIXME: Closure-library specific stuff should not be hard-coded but be
|
|
|
|
// configurable.
|
2014-05-06 21:54:10 +08:00
|
|
|
if (Line.Tokens.size() < 4)
|
|
|
|
return false;
|
|
|
|
auto I = Line.Tokens.begin();
|
|
|
|
if (I->Tok->TokenText != "goog")
|
|
|
|
return false;
|
|
|
|
++I;
|
|
|
|
if (I->Tok->isNot(tok::period))
|
|
|
|
return false;
|
|
|
|
++I;
|
|
|
|
if (I->Tok->TokenText != "scope")
|
|
|
|
return false;
|
|
|
|
++I;
|
|
|
|
return I->Tok->is(tok::l_paren);
|
|
|
|
}
|
|
|
|
|
2014-08-11 20:18:01 +08:00
|
|
|
static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
|
|
|
|
const FormatToken &InitialToken) {
|
2015-09-29 22:57:55 +08:00
|
|
|
if (InitialToken.is(tok::kw_namespace))
|
|
|
|
return Style.BraceWrapping.AfterNamespace;
|
|
|
|
if (InitialToken.is(tok::kw_class))
|
|
|
|
return Style.BraceWrapping.AfterClass;
|
|
|
|
if (InitialToken.is(tok::kw_union))
|
|
|
|
return Style.BraceWrapping.AfterUnion;
|
|
|
|
if (InitialToken.is(tok::kw_struct))
|
|
|
|
return Style.BraceWrapping.AfterStruct;
|
|
|
|
return false;
|
2014-08-11 20:18:01 +08:00
|
|
|
}
|
|
|
|
|
2013-09-04 21:25:30 +08:00
|
|
|
void UnwrappedLineParser::parseChildBlock() {
|
|
|
|
FormatTok->BlockKind = BK_Block;
|
|
|
|
nextToken();
|
|
|
|
{
|
2014-05-06 21:54:10 +08:00
|
|
|
bool GoogScope =
|
2015-03-30 17:56:50 +08:00
|
|
|
Style.Language == FormatStyle::LK_JavaScript && isGoogScope(*Line);
|
2013-09-04 21:25:30 +08:00
|
|
|
ScopedLineState LineState(*this);
|
|
|
|
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
|
|
|
|
/*MustBeDeclaration=*/false);
|
2014-05-06 21:54:10 +08:00
|
|
|
Line->Level += GoogScope ? 0 : 1;
|
2013-09-04 21:25:30 +08:00
|
|
|
parseLevel(/*HasOpeningBrace=*/true);
|
2015-03-30 17:56:50 +08:00
|
|
|
flushComments(isOnNewLine(*FormatTok));
|
2014-05-06 21:54:10 +08:00
|
|
|
Line->Level -= GoogScope ? 0 : 1;
|
2013-09-04 21:25:30 +08:00
|
|
|
}
|
|
|
|
nextToken();
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
void UnwrappedLineParser::parsePPDirective() {
|
2013-05-28 19:55:06 +08:00
|
|
|
assert(FormatTok->Tok.is(tok::hash) && "'#' expected");
|
2015-05-06 19:56:29 +08:00
|
|
|
ScopedMacroState MacroState(*Line, Tokens, FormatTok);
|
2013-01-03 00:30:12 +08:00
|
|
|
nextToken();
|
|
|
|
|
2014-05-09 16:15:10 +08:00
|
|
|
if (!FormatTok->Tok.getIdentifierInfo()) {
|
2013-01-31 23:58:48 +08:00
|
|
|
parsePPUnknown();
|
2013-01-03 00:30:12 +08:00
|
|
|
return;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-03 00:30:12 +08:00
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
switch (FormatTok->Tok.getIdentifierInfo()->getPPKeywordID()) {
|
2013-01-05 07:34:14 +08:00
|
|
|
case tok::pp_define:
|
|
|
|
parsePPDefine();
|
2013-05-25 02:24:24 +08:00
|
|
|
return;
|
|
|
|
case tok::pp_if:
|
2013-10-12 05:25:45 +08:00
|
|
|
parsePPIf(/*IfDef=*/false);
|
2013-05-25 02:24:24 +08:00
|
|
|
break;
|
|
|
|
case tok::pp_ifdef:
|
|
|
|
case tok::pp_ifndef:
|
2013-10-12 05:25:45 +08:00
|
|
|
parsePPIf(/*IfDef=*/true);
|
2013-05-25 02:24:24 +08:00
|
|
|
break;
|
|
|
|
case tok::pp_else:
|
|
|
|
parsePPElse();
|
|
|
|
break;
|
|
|
|
case tok::pp_elif:
|
|
|
|
parsePPElIf();
|
|
|
|
break;
|
|
|
|
case tok::pp_endif:
|
|
|
|
parsePPEndIf();
|
2013-01-05 07:34:14 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
parsePPUnknown();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-14 17:14:11 +08:00
|
|
|
void UnwrappedLineParser::conditionalCompilationCondition(bool Unreachable) {
|
|
|
|
if (Unreachable || (!PPStack.empty() && PPStack.back() == PP_Unreachable))
|
2013-05-25 02:24:24 +08:00
|
|
|
PPStack.push_back(PP_Unreachable);
|
|
|
|
else
|
|
|
|
PPStack.push_back(PP_Conditional);
|
|
|
|
}
|
|
|
|
|
2014-04-14 17:14:11 +08:00
|
|
|
void UnwrappedLineParser::conditionalCompilationStart(bool Unreachable) {
|
2013-10-12 05:25:45 +08:00
|
|
|
++PPBranchLevel;
|
|
|
|
assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
|
|
|
|
if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
|
|
|
|
PPLevelBranchIndex.push_back(0);
|
|
|
|
PPLevelBranchCount.push_back(0);
|
|
|
|
}
|
|
|
|
PPChainBranchIndex.push(0);
|
2014-04-14 17:14:11 +08:00
|
|
|
bool Skip = PPLevelBranchIndex[PPBranchLevel] > 0;
|
|
|
|
conditionalCompilationCondition(Unreachable || Skip);
|
2013-05-25 02:24:24 +08:00
|
|
|
}
|
|
|
|
|
2014-04-14 17:14:11 +08:00
|
|
|
void UnwrappedLineParser::conditionalCompilationAlternative() {
|
2013-05-25 02:24:24 +08:00
|
|
|
if (!PPStack.empty())
|
|
|
|
PPStack.pop_back();
|
2013-10-12 05:25:45 +08:00
|
|
|
assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
|
|
|
|
if (!PPChainBranchIndex.empty())
|
|
|
|
++PPChainBranchIndex.top();
|
2014-04-14 17:14:11 +08:00
|
|
|
conditionalCompilationCondition(
|
|
|
|
PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
|
|
|
|
PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top());
|
2013-05-25 02:24:24 +08:00
|
|
|
}
|
|
|
|
|
2014-04-14 17:14:11 +08:00
|
|
|
void UnwrappedLineParser::conditionalCompilationEnd() {
|
2013-10-12 05:25:45 +08:00
|
|
|
assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
|
|
|
|
if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty()) {
|
|
|
|
if (PPChainBranchIndex.top() + 1 > PPLevelBranchCount[PPBranchLevel]) {
|
|
|
|
PPLevelBranchCount[PPBranchLevel] = PPChainBranchIndex.top() + 1;
|
|
|
|
}
|
|
|
|
}
|
2014-01-29 16:49:02 +08:00
|
|
|
// Guard against #endif's without #if.
|
|
|
|
if (PPBranchLevel > 0)
|
|
|
|
--PPBranchLevel;
|
2013-10-12 05:25:45 +08:00
|
|
|
if (!PPChainBranchIndex.empty())
|
|
|
|
PPChainBranchIndex.pop();
|
2013-05-25 02:24:24 +08:00
|
|
|
if (!PPStack.empty())
|
|
|
|
PPStack.pop_back();
|
2014-04-14 17:14:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parsePPIf(bool IfDef) {
|
|
|
|
nextToken();
|
|
|
|
bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
|
2015-01-19 18:51:05 +08:00
|
|
|
FormatTok->Tok.getLiteralData() != nullptr &&
|
2014-04-14 17:14:11 +08:00
|
|
|
StringRef(FormatTok->Tok.getLiteralData(),
|
|
|
|
FormatTok->Tok.getLength()) == "0") ||
|
|
|
|
FormatTok->Tok.is(tok::kw_false);
|
|
|
|
conditionalCompilationStart(!IfDef && IsLiteralFalse);
|
|
|
|
parsePPUnknown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parsePPElse() {
|
|
|
|
conditionalCompilationAlternative();
|
|
|
|
parsePPUnknown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parsePPEndIf() {
|
|
|
|
conditionalCompilationEnd();
|
2013-05-25 02:24:24 +08:00
|
|
|
parsePPUnknown();
|
|
|
|
}
|
|
|
|
|
2013-01-05 07:34:14 +08:00
|
|
|
void UnwrappedLineParser::parsePPDefine() {
|
|
|
|
nextToken();
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.getKind() != tok::identifier) {
|
2013-01-05 07:34:14 +08:00
|
|
|
parsePPUnknown();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.getKind() == tok::l_paren &&
|
|
|
|
FormatTok->WhitespaceRange.getBegin() ==
|
|
|
|
FormatTok->WhitespaceRange.getEnd()) {
|
2013-01-05 07:34:14 +08:00
|
|
|
parseParens();
|
|
|
|
}
|
|
|
|
addUnwrappedLine();
|
2013-01-09 23:25:02 +08:00
|
|
|
Line->Level = 1;
|
2013-01-07 17:34:28 +08:00
|
|
|
|
|
|
|
// Errors during a preprocessor directive can only affect the layout of the
|
|
|
|
// preprocessor directive, and thus we ignore them. An alternative approach
|
|
|
|
// would be to use the same approach we use on the file level (no
|
|
|
|
// re-indentation if there was a structural error) within the macro
|
|
|
|
// definition.
|
2013-01-05 07:34:14 +08:00
|
|
|
parseFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parsePPUnknown() {
|
2013-01-03 00:30:12 +08:00
|
|
|
do {
|
|
|
|
nextToken();
|
|
|
|
} while (!eof());
|
|
|
|
addUnwrappedLine();
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-04-10 00:15:19 +08:00
|
|
|
// Here we blacklist certain tokens that are not usually the first token in an
|
|
|
|
// unwrapped line. This is used in attempt to distinguish macro calls without
|
|
|
|
// trailing semicolons from other constructs split to several lines.
|
2015-03-10 00:47:52 +08:00
|
|
|
static bool tokenCanStartNewLine(const clang::Token &Tok) {
|
2013-04-10 00:15:19 +08:00
|
|
|
// Semicolon can be a null-statement, l_square can be a start of a macro or
|
|
|
|
// a C++11 attribute, but this doesn't seem to be common.
|
|
|
|
return Tok.isNot(tok::semi) && Tok.isNot(tok::l_brace) &&
|
|
|
|
Tok.isNot(tok::l_square) &&
|
|
|
|
// Tokens that can only be used as binary operators and a part of
|
|
|
|
// overloaded operator names.
|
|
|
|
Tok.isNot(tok::period) && Tok.isNot(tok::periodstar) &&
|
|
|
|
Tok.isNot(tok::arrow) && Tok.isNot(tok::arrowstar) &&
|
|
|
|
Tok.isNot(tok::less) && Tok.isNot(tok::greater) &&
|
|
|
|
Tok.isNot(tok::slash) && Tok.isNot(tok::percent) &&
|
|
|
|
Tok.isNot(tok::lessless) && Tok.isNot(tok::greatergreater) &&
|
|
|
|
Tok.isNot(tok::equal) && Tok.isNot(tok::plusequal) &&
|
|
|
|
Tok.isNot(tok::minusequal) && Tok.isNot(tok::starequal) &&
|
|
|
|
Tok.isNot(tok::slashequal) && Tok.isNot(tok::percentequal) &&
|
|
|
|
Tok.isNot(tok::ampequal) && Tok.isNot(tok::pipeequal) &&
|
|
|
|
Tok.isNot(tok::caretequal) && Tok.isNot(tok::greatergreaterequal) &&
|
|
|
|
Tok.isNot(tok::lesslessequal) &&
|
|
|
|
// Colon is used in labels, base class lists, initializer lists,
|
|
|
|
// range-based for loops, ternary operator, but should never be the
|
|
|
|
// first token in an unwrapped line.
|
2014-05-21 21:08:17 +08:00
|
|
|
Tok.isNot(tok::colon) &&
|
|
|
|
// 'noexcept' is a trailing annotation.
|
|
|
|
Tok.isNot(tok::kw_noexcept);
|
2013-04-10 00:15:19 +08:00
|
|
|
}
|
|
|
|
|
2016-04-20 02:19:06 +08:00
|
|
|
static bool mustBeJSIdent(const AdditionalKeywords &Keywords,
|
|
|
|
const FormatToken *FormatTok) {
|
2016-03-15 03:21:36 +08:00
|
|
|
// FIXME: This returns true for C/C++ keywords like 'struct'.
|
|
|
|
return FormatTok->is(tok::identifier) &&
|
|
|
|
(FormatTok->Tok.getIdentifierInfo() == nullptr ||
|
2016-06-01 23:22:47 +08:00
|
|
|
!FormatTok->isOneOf(Keywords.kw_in, Keywords.kw_of, Keywords.kw_as,
|
|
|
|
Keywords.kw_async, Keywords.kw_await,
|
|
|
|
Keywords.kw_yield, Keywords.kw_finally,
|
|
|
|
Keywords.kw_function, Keywords.kw_import,
|
|
|
|
Keywords.kw_is, Keywords.kw_let, Keywords.kw_var,
|
2016-03-15 03:21:36 +08:00
|
|
|
Keywords.kw_abstract, Keywords.kw_extends,
|
|
|
|
Keywords.kw_implements, Keywords.kw_instanceof,
|
|
|
|
Keywords.kw_interface, Keywords.kw_throws));
|
|
|
|
}
|
|
|
|
|
2016-04-20 02:19:06 +08:00
|
|
|
static bool mustBeJSIdentOrValue(const AdditionalKeywords &Keywords,
|
|
|
|
const FormatToken *FormatTok) {
|
|
|
|
return FormatTok->Tok.isLiteral() || mustBeJSIdent(Keywords, FormatTok);
|
|
|
|
}
|
|
|
|
|
2016-03-15 03:21:36 +08:00
|
|
|
// isJSDeclOrStmt returns true if |FormatTok| starts a declaration or statement
|
|
|
|
// when encountered after a value (see mustBeJSIdentOrValue).
|
|
|
|
static bool isJSDeclOrStmt(const AdditionalKeywords &Keywords,
|
|
|
|
const FormatToken *FormatTok) {
|
|
|
|
return FormatTok->isOneOf(
|
2016-04-25 06:05:09 +08:00
|
|
|
tok::kw_return, Keywords.kw_yield,
|
2016-03-15 03:21:36 +08:00
|
|
|
// conditionals
|
|
|
|
tok::kw_if, tok::kw_else,
|
|
|
|
// loops
|
|
|
|
tok::kw_for, tok::kw_while, tok::kw_do, tok::kw_continue, tok::kw_break,
|
|
|
|
// switch/case
|
|
|
|
tok::kw_switch, tok::kw_case,
|
|
|
|
// exceptions
|
|
|
|
tok::kw_throw, tok::kw_try, tok::kw_catch, Keywords.kw_finally,
|
|
|
|
// declaration
|
|
|
|
tok::kw_const, tok::kw_class, Keywords.kw_var, Keywords.kw_let,
|
2016-04-25 06:05:09 +08:00
|
|
|
Keywords.kw_async, Keywords.kw_function,
|
|
|
|
// import/export
|
|
|
|
Keywords.kw_import, tok::kw_export);
|
2016-03-15 03:21:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// readTokenWithJavaScriptASI reads the next token and terminates the current
|
|
|
|
// line if JavaScript Automatic Semicolon Insertion must
|
|
|
|
// happen between the current token and the next token.
|
|
|
|
//
|
|
|
|
// This method is conservative - it cannot cover all edge cases of JavaScript,
|
|
|
|
// but only aims to correctly handle certain well known cases. It *must not*
|
|
|
|
// return true in speculative cases.
|
|
|
|
void UnwrappedLineParser::readTokenWithJavaScriptASI() {
|
|
|
|
FormatToken *Previous = FormatTok;
|
|
|
|
readToken();
|
|
|
|
FormatToken *Next = FormatTok;
|
|
|
|
|
|
|
|
bool IsOnSameLine =
|
|
|
|
CommentsBeforeNextToken.empty()
|
|
|
|
? Next->NewlinesBefore == 0
|
|
|
|
: CommentsBeforeNextToken.front()->NewlinesBefore == 0;
|
|
|
|
if (IsOnSameLine)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous);
|
2016-04-11 15:35:57 +08:00
|
|
|
if (PreviousMustBeValue && Line && Line->Tokens.size() > 1) {
|
|
|
|
// If the token before the previous one is an '@', the previous token is an
|
|
|
|
// annotation and can precede another identifier/value.
|
2016-04-11 20:19:19 +08:00
|
|
|
const FormatToken *PrePrevious = std::prev(Line->Tokens.end(), 2)->Tok;
|
2016-04-11 15:35:57 +08:00
|
|
|
if (PrePrevious->is(tok::at))
|
|
|
|
return;
|
|
|
|
}
|
2016-03-15 03:21:36 +08:00
|
|
|
if (Next->is(tok::exclaim) && PreviousMustBeValue)
|
|
|
|
addUnwrappedLine();
|
|
|
|
bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next);
|
|
|
|
if (NextMustBeValue && (PreviousMustBeValue ||
|
|
|
|
Previous->isOneOf(tok::r_square, tok::r_paren,
|
|
|
|
tok::plusplus, tok::minusminus)))
|
|
|
|
addUnwrappedLine();
|
|
|
|
if (PreviousMustBeValue && isJSDeclOrStmt(Keywords, Next))
|
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
|
|
|
|
2013-01-07 22:56:16 +08:00
|
|
|
void UnwrappedLineParser::parseStructuralElement() {
|
2015-12-25 16:53:31 +08:00
|
|
|
assert(!FormatTok->is(tok::l_brace));
|
|
|
|
if (Style.Language == FormatStyle::LK_TableGen &&
|
|
|
|
FormatTok->is(tok::pp_include)) {
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok->is(tok::string_literal))
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
}
|
2013-05-28 19:55:06 +08:00
|
|
|
switch (FormatTok->Tok.getKind()) {
|
2013-01-08 03:05:19 +08:00
|
|
|
case tok::at:
|
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2013-02-11 04:35:35 +08:00
|
|
|
parseBracedList();
|
|
|
|
break;
|
|
|
|
}
|
2013-05-28 19:55:06 +08:00
|
|
|
switch (FormatTok->Tok.getObjCKeywordID()) {
|
2013-01-08 03:05:19 +08:00
|
|
|
case tok::objc_public:
|
|
|
|
case tok::objc_protected:
|
|
|
|
case tok::objc_package:
|
|
|
|
case tok::objc_private:
|
|
|
|
return parseAccessSpecifier();
|
2013-01-10 04:25:35 +08:00
|
|
|
case tok::objc_interface:
|
2013-01-10 07:25:37 +08:00
|
|
|
case tok::objc_implementation:
|
|
|
|
return parseObjCInterfaceOrImplementation();
|
2013-01-10 05:15:03 +08:00
|
|
|
case tok::objc_protocol:
|
|
|
|
return parseObjCProtocol();
|
2013-01-10 05:42:32 +08:00
|
|
|
case tok::objc_end:
|
|
|
|
return; // Handled by the caller.
|
2013-01-10 08:25:19 +08:00
|
|
|
case tok::objc_optional:
|
|
|
|
case tok::objc_required:
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
2015-06-28 09:06:16 +08:00
|
|
|
case tok::objc_autoreleasepool:
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2015-09-29 22:57:55 +08:00
|
|
|
if (Style.BraceWrapping.AfterObjCDeclaration)
|
2015-06-28 09:06:16 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
parseBlock(/*MustBeDeclaration=*/false);
|
|
|
|
}
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
2015-02-07 09:57:32 +08:00
|
|
|
case tok::objc_try:
|
|
|
|
// This branch isn't strictly necessary (the kw_try case below would
|
|
|
|
// do this too after the tok::at is parsed above). But be explicit.
|
|
|
|
parseTryCatch();
|
|
|
|
return;
|
2013-01-08 03:05:19 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2014-08-27 07:15:12 +08:00
|
|
|
case tok::kw_asm:
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok->is(tok::l_brace)) {
|
2015-05-10 16:42:04 +08:00
|
|
|
FormatTok->Type = TT_InlineASMBrace;
|
2015-01-12 18:14:56 +08:00
|
|
|
nextToken();
|
2014-08-28 01:16:46 +08:00
|
|
|
while (FormatTok && FormatTok->isNot(tok::eof)) {
|
2014-08-27 07:15:12 +08:00
|
|
|
if (FormatTok->is(tok::r_brace)) {
|
2015-05-10 16:42:04 +08:00
|
|
|
FormatTok->Type = TT_InlineASMBrace;
|
2014-08-27 07:15:12 +08:00
|
|
|
nextToken();
|
2015-05-11 19:59:46 +08:00
|
|
|
addUnwrappedLine();
|
2014-08-27 07:15:12 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-01-12 18:14:56 +08:00
|
|
|
FormatTok->Finalized = true;
|
2014-08-27 07:15:12 +08:00
|
|
|
nextToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2012-12-07 02:03:27 +08:00
|
|
|
case tok::kw_namespace:
|
|
|
|
parseNamespace();
|
|
|
|
return;
|
2012-12-31 05:27:25 +08:00
|
|
|
case tok::kw_inline:
|
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::kw_namespace)) {
|
2012-12-31 05:27:25 +08:00
|
|
|
parseNamespace();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2012-12-04 22:46:19 +08:00
|
|
|
case tok::kw_public:
|
|
|
|
case tok::kw_protected:
|
|
|
|
case tok::kw_private:
|
2015-02-19 01:14:05 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_Java ||
|
|
|
|
Style.Language == FormatStyle::LK_JavaScript)
|
2014-09-15 19:21:46 +08:00
|
|
|
nextToken();
|
|
|
|
else
|
|
|
|
parseAccessSpecifier();
|
2012-12-04 02:12:45 +08:00
|
|
|
return;
|
2012-12-04 22:46:19 +08:00
|
|
|
case tok::kw_if:
|
|
|
|
parseIfThenElse();
|
|
|
|
return;
|
2012-12-05 23:06:06 +08:00
|
|
|
case tok::kw_for:
|
|
|
|
case tok::kw_while:
|
|
|
|
parseForOrWhileLoop();
|
|
|
|
return;
|
2012-12-04 22:46:19 +08:00
|
|
|
case tok::kw_do:
|
|
|
|
parseDoWhile();
|
|
|
|
return;
|
|
|
|
case tok::kw_switch:
|
|
|
|
parseSwitch();
|
|
|
|
return;
|
|
|
|
case tok::kw_default:
|
|
|
|
nextToken();
|
|
|
|
parseLabel();
|
|
|
|
return;
|
|
|
|
case tok::kw_case:
|
|
|
|
parseCaseLabel();
|
2012-12-04 02:12:45 +08:00
|
|
|
return;
|
2014-05-08 19:58:24 +08:00
|
|
|
case tok::kw_try:
|
2015-02-04 23:26:27 +08:00
|
|
|
case tok::kw___try:
|
2014-05-08 19:58:24 +08:00
|
|
|
parseTryCatch();
|
|
|
|
return;
|
2013-01-21 22:32:05 +08:00
|
|
|
case tok::kw_extern:
|
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::string_literal)) {
|
2013-01-21 22:32:05 +08:00
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2013-08-01 07:16:02 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/false);
|
2013-01-21 22:32:05 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-04-01 20:55:11 +08:00
|
|
|
break;
|
2015-02-20 00:14:18 +08:00
|
|
|
case tok::kw_export:
|
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript) {
|
|
|
|
parseJavaScriptEs6ImportExport();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2014-04-01 20:55:11 +08:00
|
|
|
case tok::identifier:
|
2015-05-04 17:22:29 +08:00
|
|
|
if (FormatTok->is(TT_ForEachMacro)) {
|
2014-04-01 20:55:11 +08:00
|
|
|
parseForOrWhileLoop();
|
|
|
|
return;
|
|
|
|
}
|
2015-07-04 01:25:16 +08:00
|
|
|
if (FormatTok->is(TT_MacroBlockBegin)) {
|
|
|
|
parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/true,
|
|
|
|
/*MunchSemi=*/false);
|
|
|
|
return;
|
|
|
|
}
|
2016-06-21 02:20:38 +08:00
|
|
|
if (FormatTok->is(Keywords.kw_import)) {
|
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript) {
|
|
|
|
parseJavaScriptEs6ImportExport();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Style.Language == FormatStyle::LK_Proto) {
|
|
|
|
nextToken();
|
2016-06-21 04:39:53 +08:00
|
|
|
if (FormatTok->is(tok::kw_public))
|
|
|
|
nextToken();
|
2016-06-21 02:20:38 +08:00
|
|
|
if (!FormatTok->is(tok::string_literal))
|
|
|
|
return;
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok->is(tok::semi))
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
}
|
2015-02-20 00:07:32 +08:00
|
|
|
}
|
2015-12-01 20:05:04 +08:00
|
|
|
if (FormatTok->isOneOf(Keywords.kw_signals, Keywords.kw_qsignals,
|
|
|
|
Keywords.kw_slots, Keywords.kw_qslots)) {
|
2015-04-24 15:50:34 +08:00
|
|
|
nextToken();
|
|
|
|
if (FormatTok->is(tok::colon)) {
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
2015-04-07 23:04:40 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-01-21 22:32:05 +08:00
|
|
|
// In all other cases, parse the declaration.
|
|
|
|
break;
|
2012-12-04 22:46:19 +08:00
|
|
|
default:
|
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
do {
|
2016-05-09 02:12:22 +08:00
|
|
|
const FormatToken *Previous = getPreviousToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
switch (FormatTok->Tok.getKind()) {
|
2013-02-11 04:35:35 +08:00
|
|
|
case tok::at:
|
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace))
|
2013-02-11 04:35:35 +08:00
|
|
|
parseBracedList();
|
|
|
|
break;
|
2012-12-04 22:46:19 +08:00
|
|
|
case tok::kw_enum:
|
2016-05-09 02:12:22 +08:00
|
|
|
// Ignore if this is part of "template <enum ...".
|
|
|
|
if (Previous && Previous->is(tok::less)) {
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-06-17 17:44:02 +08:00
|
|
|
// parseEnum falls through and does not yet add an unwrapped line as an
|
|
|
|
// enum definition can start a structural element.
|
2015-12-29 16:54:23 +08:00
|
|
|
if (!parseEnum())
|
|
|
|
break;
|
2015-07-16 22:25:43 +08:00
|
|
|
// This only applies for C++.
|
|
|
|
if (Style.Language != FormatStyle::LK_Cpp) {
|
2015-06-17 17:44:02 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
}
|
2013-01-22 03:17:52 +08:00
|
|
|
break;
|
2014-01-30 22:38:37 +08:00
|
|
|
case tok::kw_typedef:
|
|
|
|
nextToken();
|
2014-12-05 18:42:21 +08:00
|
|
|
if (FormatTok->isOneOf(Keywords.kw_NS_ENUM, Keywords.kw_NS_OPTIONS,
|
|
|
|
Keywords.kw_CF_ENUM, Keywords.kw_CF_OPTIONS))
|
2014-01-30 22:38:37 +08:00
|
|
|
parseEnum();
|
|
|
|
break;
|
2013-01-16 19:43:46 +08:00
|
|
|
case tok::kw_struct:
|
|
|
|
case tok::kw_union:
|
2013-01-08 02:10:23 +08:00
|
|
|
case tok::kw_class:
|
2015-06-12 12:52:02 +08:00
|
|
|
// parseRecord falls through and does not yet add an unwrapped line as a
|
|
|
|
// record declaration or definition can start a structural element.
|
2013-01-15 21:38:33 +08:00
|
|
|
parseRecord();
|
2015-06-12 12:52:02 +08:00
|
|
|
// This does not apply for Java and JavaScript.
|
|
|
|
if (Style.Language == FormatStyle::LK_Java ||
|
|
|
|
Style.Language == FormatStyle::LK_JavaScript) {
|
2016-01-08 15:06:07 +08:00
|
|
|
if (FormatTok->is(tok::semi))
|
|
|
|
nextToken();
|
2015-06-12 12:52:02 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
}
|
2013-01-15 21:38:33 +08:00
|
|
|
break;
|
2014-11-26 16:17:08 +08:00
|
|
|
case tok::period:
|
|
|
|
nextToken();
|
|
|
|
// In Java, classes have an implicit static member "class".
|
|
|
|
if (Style.Language == FormatStyle::LK_Java && FormatTok &&
|
|
|
|
FormatTok->is(tok::kw_class))
|
|
|
|
nextToken();
|
2015-09-28 22:29:45 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
|
|
|
|
FormatTok->Tok.getIdentifierInfo())
|
|
|
|
// JavaScript only has pseudo keywords, all keywords are allowed to
|
|
|
|
// appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
|
|
|
|
nextToken();
|
2014-11-26 16:17:08 +08:00
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
case tok::semi:
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
2013-01-16 19:43:46 +08:00
|
|
|
case tok::r_brace:
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
2012-12-04 02:12:45 +08:00
|
|
|
case tok::l_paren:
|
|
|
|
parseParens();
|
|
|
|
break;
|
2015-10-07 11:43:10 +08:00
|
|
|
case tok::kw_operator:
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok->isBinaryOperator())
|
|
|
|
nextToken();
|
|
|
|
break;
|
2013-09-04 21:25:30 +08:00
|
|
|
case tok::caret:
|
|
|
|
nextToken();
|
2014-03-28 15:48:59 +08:00
|
|
|
if (FormatTok->Tok.isAnyIdentifier() ||
|
|
|
|
FormatTok->isSimpleTypeSpecifier())
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok->is(tok::l_paren))
|
|
|
|
parseParens();
|
|
|
|
if (FormatTok->is(tok::l_brace))
|
2013-09-04 21:25:30 +08:00
|
|
|
parseChildBlock();
|
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
case tok::l_brace:
|
2013-05-23 17:41:43 +08:00
|
|
|
if (!tryToParseBracedList()) {
|
|
|
|
// A block outside of parentheses must be the last part of a
|
|
|
|
// structural element.
|
|
|
|
// FIXME: Figure out cases where this is not true, and add projections
|
|
|
|
// for them (the one we know is missing are lambdas).
|
2015-09-29 22:57:55 +08:00
|
|
|
if (Style.BraceWrapping.AfterFunction)
|
2013-05-23 17:41:43 +08:00
|
|
|
addUnwrappedLine();
|
2013-11-21 00:33:05 +08:00
|
|
|
FormatTok->Type = TT_FunctionLBrace;
|
2013-06-26 08:30:14 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/false);
|
2013-05-13 20:51:40 +08:00
|
|
|
addUnwrappedLine();
|
2013-05-23 17:41:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Otherwise this was a braced init list, and the structural
|
|
|
|
// element continues.
|
|
|
|
break;
|
2014-05-08 19:58:24 +08:00
|
|
|
case tok::kw_try:
|
|
|
|
// We arrive here when parsing function-try blocks.
|
|
|
|
parseTryCatch();
|
|
|
|
return;
|
2013-05-29 21:16:10 +08:00
|
|
|
case tok::identifier: {
|
2015-07-04 01:25:16 +08:00
|
|
|
if (FormatTok->is(TT_MacroBlockEnd)) {
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
clang-format: [JS] support free-standing functions again.
This worked initially but was broken by r210887.
Before:
function outer1(a, b) {
function inner1(a, b) { return a; } inner1(a, b);
} function outer2(a, b) { function inner2(a, b) { return a; } inner2(a, b); }
After:
function outer1(a, b) {
function inner1(a, b) { return a; }
inner1(a, b);
}
function outer2(a, b) {
function inner2(a, b) { return a; }
inner2(a, b);
}
Thanks to Adam Strzelecki for working on this.
llvm-svn: 212038
2014-06-30 21:24:54 +08:00
|
|
|
// Parse function literal unless 'function' is the first token in a line
|
|
|
|
// in which case this should be treated as a free-standing function.
|
2015-05-05 16:40:32 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript &&
|
2016-05-29 22:41:07 +08:00
|
|
|
(FormatTok->is(Keywords.kw_function) ||
|
|
|
|
FormatTok->startsSequence(Keywords.kw_async,
|
|
|
|
Keywords.kw_function)) &&
|
2016-04-25 06:05:09 +08:00
|
|
|
Line->Tokens.size() > 0) {
|
2014-05-20 19:14:57 +08:00
|
|
|
tryToParseJSFunction();
|
|
|
|
break;
|
|
|
|
}
|
2015-05-05 16:40:32 +08:00
|
|
|
if ((Style.Language == FormatStyle::LK_JavaScript ||
|
|
|
|
Style.Language == FormatStyle::LK_Java) &&
|
|
|
|
FormatTok->is(Keywords.kw_interface)) {
|
2016-04-20 02:18:59 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript) {
|
|
|
|
// In JavaScript/TypeScript, "interface" can be used as a standalone
|
|
|
|
// identifier, e.g. in `var interface = 1;`. If "interface" is
|
|
|
|
// followed by another identifier, it is very like to be an actual
|
|
|
|
// interface declaration.
|
|
|
|
unsigned StoredPosition = Tokens->getPosition();
|
|
|
|
FormatToken *Next = Tokens->getNextToken();
|
|
|
|
FormatTok = Tokens->setPosition(StoredPosition);
|
2016-04-20 02:19:06 +08:00
|
|
|
if (Next && !mustBeJSIdent(Keywords, Next)) {
|
2016-04-20 02:18:59 +08:00
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-05 16:40:32 +08:00
|
|
|
parseRecord();
|
2015-06-12 12:56:34 +08:00
|
|
|
addUnwrappedLine();
|
2015-07-06 22:26:04 +08:00
|
|
|
return;
|
2015-05-05 16:40:32 +08:00
|
|
|
}
|
|
|
|
|
2016-03-15 03:21:36 +08:00
|
|
|
// See if the following token should start a new unwrapped line.
|
2015-05-05 16:40:32 +08:00
|
|
|
StringRef Text = FormatTok->TokenText;
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
2015-02-19 01:14:05 +08:00
|
|
|
if (Line->Tokens.size() == 1 &&
|
|
|
|
// JS doesn't have macros, and within classes colons indicate fields,
|
|
|
|
// not labels.
|
2015-04-07 22:36:33 +08:00
|
|
|
Style.Language != FormatStyle::LK_JavaScript) {
|
|
|
|
if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) {
|
2016-04-06 23:02:46 +08:00
|
|
|
Line->Tokens.begin()->Tok->MustBreakBefore = true;
|
2013-04-09 06:16:06 +08:00
|
|
|
parseLabel();
|
|
|
|
return;
|
|
|
|
}
|
2014-11-05 18:48:04 +08:00
|
|
|
// Recognize function-like macro usages without trailing semicolon as
|
2015-02-19 01:14:05 +08:00
|
|
|
// well as free-standing macros like Q_OBJECT.
|
2014-11-05 18:48:04 +08:00
|
|
|
bool FunctionLike = FormatTok->is(tok::l_paren);
|
|
|
|
if (FunctionLike)
|
2013-04-09 06:16:06 +08:00
|
|
|
parseParens();
|
2015-05-13 19:35:53 +08:00
|
|
|
|
|
|
|
bool FollowedByNewline =
|
|
|
|
CommentsBeforeNextToken.empty()
|
|
|
|
? FormatTok->NewlinesBefore > 0
|
|
|
|
: CommentsBeforeNextToken.front()->NewlinesBefore > 0;
|
|
|
|
|
2015-06-17 21:08:06 +08:00
|
|
|
if (FollowedByNewline && (Text.size() >= 5 || FunctionLike) &&
|
2014-11-05 18:48:04 +08:00
|
|
|
tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) {
|
2013-05-29 21:16:10 +08:00
|
|
|
addUnwrappedLine();
|
2013-05-29 22:09:17 +08:00
|
|
|
return;
|
2013-04-09 06:16:06 +08:00
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
break;
|
2013-05-29 21:16:10 +08:00
|
|
|
}
|
2012-12-17 19:29:41 +08:00
|
|
|
case tok::equal:
|
2015-05-21 20:23:34 +08:00
|
|
|
// Fat arrows (=>) have tok::TokenKind tok::equal but TokenType
|
|
|
|
// TT_JsFatArrow. The always start an expression or a child block if
|
|
|
|
// followed by a curly.
|
|
|
|
if (FormatTok->is(TT_JsFatArrow)) {
|
|
|
|
nextToken();
|
2015-05-31 16:51:54 +08:00
|
|
|
if (FormatTok->is(tok::l_brace))
|
2015-05-21 20:23:34 +08:00
|
|
|
parseChildBlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-12-17 19:29:41 +08:00
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2013-01-10 19:52:21 +08:00
|
|
|
parseBracedList();
|
|
|
|
}
|
|
|
|
break;
|
2013-09-03 23:10:01 +08:00
|
|
|
case tok::l_square:
|
2013-12-23 15:29:06 +08:00
|
|
|
parseSquare();
|
2013-09-03 23:10:01 +08:00
|
|
|
break;
|
2015-03-12 22:44:29 +08:00
|
|
|
case tok::kw_new:
|
|
|
|
parseNew();
|
|
|
|
break;
|
2013-01-10 19:52:21 +08:00
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:29:06 +08:00
|
|
|
bool UnwrappedLineParser::tryToParseLambda() {
|
2015-06-02 23:31:37 +08:00
|
|
|
if (Style.Language != FormatStyle::LK_Cpp) {
|
|
|
|
nextToken();
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-09 23:56:28 +08:00
|
|
|
const FormatToken* Previous = getPreviousToken();
|
|
|
|
if (Previous &&
|
|
|
|
(Previous->isOneOf(tok::identifier, tok::kw_operator, tok::kw_new,
|
|
|
|
tok::kw_delete) ||
|
|
|
|
Previous->closesScope() || Previous->isSimpleTypeSpecifier())) {
|
2013-09-05 19:49:39 +08:00
|
|
|
nextToken();
|
2013-12-23 15:29:06 +08:00
|
|
|
return false;
|
2013-09-05 19:49:39 +08:00
|
|
|
}
|
2013-09-05 17:29:45 +08:00
|
|
|
assert(FormatTok->is(tok::l_square));
|
|
|
|
FormatToken &LSquare = *FormatTok;
|
2013-09-05 18:04:31 +08:00
|
|
|
if (!tryToParseLambdaIntroducer())
|
2013-12-23 15:29:06 +08:00
|
|
|
return false;
|
2013-09-03 23:10:01 +08:00
|
|
|
|
2014-03-13 21:59:48 +08:00
|
|
|
while (FormatTok->isNot(tok::l_brace)) {
|
2014-01-16 17:11:55 +08:00
|
|
|
if (FormatTok->isSimpleTypeSpecifier()) {
|
|
|
|
nextToken();
|
|
|
|
continue;
|
|
|
|
}
|
2013-09-03 23:10:01 +08:00
|
|
|
switch (FormatTok->Tok.getKind()) {
|
2013-09-05 18:04:31 +08:00
|
|
|
case tok::l_brace:
|
|
|
|
break;
|
|
|
|
case tok::l_paren:
|
|
|
|
parseParens();
|
|
|
|
break;
|
2014-11-21 22:08:38 +08:00
|
|
|
case tok::amp:
|
|
|
|
case tok::star:
|
|
|
|
case tok::kw_const:
|
2014-12-08 21:22:37 +08:00
|
|
|
case tok::comma:
|
2014-01-16 17:11:55 +08:00
|
|
|
case tok::less:
|
|
|
|
case tok::greater:
|
2013-09-05 18:04:31 +08:00
|
|
|
case tok::identifier:
|
2015-08-13 21:37:08 +08:00
|
|
|
case tok::numeric_constant:
|
2014-02-11 18:16:55 +08:00
|
|
|
case tok::coloncolon:
|
2013-09-05 18:04:31 +08:00
|
|
|
case tok::kw_mutable:
|
2014-03-10 18:02:02 +08:00
|
|
|
nextToken();
|
|
|
|
break;
|
2014-01-16 17:11:55 +08:00
|
|
|
case tok::arrow:
|
2015-06-05 21:18:09 +08:00
|
|
|
FormatTok->Type = TT_LambdaArrow;
|
2013-09-05 18:04:31 +08:00
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
default:
|
2013-12-23 15:29:06 +08:00
|
|
|
return true;
|
2013-09-03 23:10:01 +08:00
|
|
|
}
|
|
|
|
}
|
2013-09-05 17:29:45 +08:00
|
|
|
LSquare.Type = TT_LambdaLSquare;
|
2013-09-04 21:25:30 +08:00
|
|
|
parseChildBlock();
|
2013-12-23 15:29:06 +08:00
|
|
|
return true;
|
2013-09-03 23:10:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UnwrappedLineParser::tryToParseLambdaIntroducer() {
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok->is(tok::equal)) {
|
|
|
|
nextToken();
|
2013-09-05 18:04:31 +08:00
|
|
|
if (FormatTok->is(tok::r_square)) {
|
|
|
|
nextToken();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (FormatTok->isNot(tok::comma))
|
|
|
|
return false;
|
2013-09-03 23:10:01 +08:00
|
|
|
nextToken();
|
|
|
|
} else if (FormatTok->is(tok::amp)) {
|
|
|
|
nextToken();
|
2013-09-05 18:04:31 +08:00
|
|
|
if (FormatTok->is(tok::r_square)) {
|
|
|
|
nextToken();
|
|
|
|
return true;
|
|
|
|
}
|
2013-09-03 23:10:01 +08:00
|
|
|
if (!FormatTok->isOneOf(tok::comma, tok::identifier)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-05 18:04:31 +08:00
|
|
|
if (FormatTok->is(tok::comma))
|
|
|
|
nextToken();
|
2013-09-03 23:10:01 +08:00
|
|
|
} else if (FormatTok->is(tok::r_square)) {
|
|
|
|
nextToken();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
do {
|
2013-09-05 18:04:31 +08:00
|
|
|
if (FormatTok->is(tok::amp))
|
|
|
|
nextToken();
|
|
|
|
if (!FormatTok->isOneOf(tok::identifier, tok::kw_this))
|
|
|
|
return false;
|
2013-09-03 23:10:01 +08:00
|
|
|
nextToken();
|
2014-06-10 14:39:03 +08:00
|
|
|
if (FormatTok->is(tok::ellipsis))
|
|
|
|
nextToken();
|
2013-09-03 23:10:01 +08:00
|
|
|
if (FormatTok->is(tok::comma)) {
|
|
|
|
nextToken();
|
|
|
|
} else if (FormatTok->is(tok::r_square)) {
|
|
|
|
nextToken();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-08 17:25:39 +08:00
|
|
|
void UnwrappedLineParser::tryToParseJSFunction() {
|
2016-05-29 22:41:07 +08:00
|
|
|
assert(FormatTok->is(Keywords.kw_function) ||
|
|
|
|
FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function));
|
2016-04-25 06:05:09 +08:00
|
|
|
if (FormatTok->is(Keywords.kw_async))
|
|
|
|
nextToken();
|
|
|
|
// Consume "function".
|
2014-05-08 17:25:39 +08:00
|
|
|
nextToken();
|
2014-06-13 15:02:04 +08:00
|
|
|
|
2016-04-25 06:05:09 +08:00
|
|
|
// Consume * (generator function).
|
|
|
|
if (FormatTok->is(tok::star))
|
|
|
|
nextToken();
|
|
|
|
|
2014-06-13 15:02:04 +08:00
|
|
|
// Consume function name.
|
|
|
|
if (FormatTok->is(tok::identifier))
|
2015-02-20 00:14:18 +08:00
|
|
|
nextToken();
|
2014-06-13 15:02:04 +08:00
|
|
|
|
2014-05-08 17:25:39 +08:00
|
|
|
if (FormatTok->isNot(tok::l_paren))
|
|
|
|
return;
|
2015-05-21 20:23:34 +08:00
|
|
|
|
|
|
|
// Parse formal parameter list.
|
2015-05-31 16:51:54 +08:00
|
|
|
parseParens();
|
2015-05-21 20:23:34 +08:00
|
|
|
|
|
|
|
if (FormatTok->is(tok::colon)) {
|
|
|
|
// Parse a type definition.
|
2014-05-08 17:25:39 +08:00
|
|
|
nextToken();
|
2015-05-21 20:23:34 +08:00
|
|
|
|
|
|
|
// Eat the type declaration. For braced inline object types, balance braces,
|
|
|
|
// otherwise just parse until finding an l_brace for the function body.
|
2015-05-31 16:51:54 +08:00
|
|
|
if (FormatTok->is(tok::l_brace))
|
|
|
|
tryToParseBracedList();
|
|
|
|
else
|
2015-06-17 21:08:06 +08:00
|
|
|
while (FormatTok->isNot(tok::l_brace) && !eof())
|
2015-05-21 20:23:34 +08:00
|
|
|
nextToken();
|
2014-05-08 17:25:39 +08:00
|
|
|
}
|
2015-05-21 20:23:34 +08:00
|
|
|
|
2014-05-08 17:25:39 +08:00
|
|
|
parseChildBlock();
|
|
|
|
}
|
|
|
|
|
2015-05-18 22:49:19 +08:00
|
|
|
bool UnwrappedLineParser::tryToParseBracedList() {
|
2013-07-09 17:06:29 +08:00
|
|
|
if (FormatTok->BlockKind == BK_Unknown)
|
2015-05-18 22:49:19 +08:00
|
|
|
calculateBraceTypes();
|
2013-07-09 17:06:29 +08:00
|
|
|
assert(FormatTok->BlockKind != BK_Unknown);
|
|
|
|
if (FormatTok->BlockKind == BK_Block)
|
2013-05-23 17:41:43 +08:00
|
|
|
return false;
|
|
|
|
parseBracedList();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-13 17:20:45 +08:00
|
|
|
bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
|
|
|
|
bool HasError = false;
|
2013-01-10 19:52:21 +08:00
|
|
|
nextToken();
|
|
|
|
|
2013-04-10 17:52:05 +08:00
|
|
|
// FIXME: Once we have an expression parser in the UnwrappedLineParser,
|
|
|
|
// replace this by using parseAssigmentExpression() inside.
|
2013-01-10 19:52:21 +08:00
|
|
|
do {
|
2015-05-21 20:23:34 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript) {
|
2016-05-29 22:41:07 +08:00
|
|
|
if (FormatTok->is(Keywords.kw_function) ||
|
|
|
|
FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)) {
|
2015-05-21 20:23:34 +08:00
|
|
|
tryToParseJSFunction();
|
|
|
|
continue;
|
2015-05-31 16:51:54 +08:00
|
|
|
}
|
|
|
|
if (FormatTok->is(TT_JsFatArrow)) {
|
2015-05-21 20:23:34 +08:00
|
|
|
nextToken();
|
|
|
|
// Fat arrows can be followed by simple expressions or by child blocks
|
|
|
|
// in curly braces.
|
2015-06-17 21:08:06 +08:00
|
|
|
if (FormatTok->is(tok::l_brace)) {
|
2015-05-21 20:23:34 +08:00
|
|
|
parseChildBlock();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 17:25:39 +08:00
|
|
|
}
|
2013-05-28 19:55:06 +08:00
|
|
|
switch (FormatTok->Tok.getKind()) {
|
2013-09-04 21:25:30 +08:00
|
|
|
case tok::caret:
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok->is(tok::l_brace)) {
|
|
|
|
parseChildBlock();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case tok::l_square:
|
|
|
|
tryToParseLambda();
|
|
|
|
break;
|
2013-01-10 19:52:21 +08:00
|
|
|
case tok::l_brace:
|
2013-09-04 16:20:47 +08:00
|
|
|
// Assume there are no blocks inside a braced init list apart
|
|
|
|
// from the ones we explicitly parse out (like lambdas).
|
|
|
|
FormatTok->BlockKind = BK_BracedInit;
|
2013-01-10 19:52:21 +08:00
|
|
|
parseBracedList();
|
2012-12-17 19:29:41 +08:00
|
|
|
break;
|
2015-06-30 19:32:22 +08:00
|
|
|
case tok::l_paren:
|
|
|
|
parseParens();
|
2015-03-31 22:34:15 +08:00
|
|
|
// JavaScript can just have free standing methods and getters/setters in
|
|
|
|
// object literals. Detect them by a "{" following ")".
|
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript) {
|
|
|
|
if (FormatTok->is(tok::l_brace))
|
|
|
|
parseChildBlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2013-01-10 19:52:21 +08:00
|
|
|
case tok::r_brace:
|
|
|
|
nextToken();
|
2013-09-13 17:20:45 +08:00
|
|
|
return !HasError;
|
2013-04-10 17:52:05 +08:00
|
|
|
case tok::semi:
|
2016-01-09 23:56:28 +08:00
|
|
|
// JavaScript (or more precisely TypeScript) can have semicolons in braced
|
|
|
|
// lists (in so-called TypeMemberLists). Thus, the semicolon cannot be
|
|
|
|
// used for error recovery if we have otherwise determined that this is
|
|
|
|
// a braced list.
|
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript) {
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
2013-09-13 17:20:45 +08:00
|
|
|
HasError = true;
|
|
|
|
if (!ContinueOnSemicolons)
|
|
|
|
return !HasError;
|
|
|
|
nextToken();
|
|
|
|
break;
|
2013-04-10 17:52:05 +08:00
|
|
|
case tok::comma:
|
|
|
|
nextToken();
|
|
|
|
break;
|
2012-12-04 22:46:19 +08:00
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
} while (!eof());
|
2013-09-13 17:20:45 +08:00
|
|
|
return false;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseParens() {
|
2013-05-28 19:55:06 +08:00
|
|
|
assert(FormatTok->Tok.is(tok::l_paren) && "'(' expected.");
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
|
|
|
do {
|
2013-05-28 19:55:06 +08:00
|
|
|
switch (FormatTok->Tok.getKind()) {
|
2012-12-04 02:12:45 +08:00
|
|
|
case tok::l_paren:
|
|
|
|
parseParens();
|
2015-01-05 04:40:51 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
|
|
|
|
parseChildBlock();
|
2012-12-04 02:12:45 +08:00
|
|
|
break;
|
|
|
|
case tok::r_paren:
|
|
|
|
nextToken();
|
|
|
|
return;
|
2013-05-31 22:56:29 +08:00
|
|
|
case tok::r_brace:
|
|
|
|
// A "}" inside parenthesis is an error if there wasn't a matching "{".
|
|
|
|
return;
|
2013-09-05 18:04:31 +08:00
|
|
|
case tok::l_square:
|
|
|
|
tryToParseLambda();
|
|
|
|
break;
|
2015-01-05 04:40:51 +08:00
|
|
|
case tok::l_brace:
|
2015-05-18 20:52:00 +08:00
|
|
|
if (!tryToParseBracedList())
|
2013-09-04 21:34:14 +08:00
|
|
|
parseChildBlock();
|
2013-01-10 19:52:21 +08:00
|
|
|
break;
|
2013-02-11 04:35:35 +08:00
|
|
|
case tok::at:
|
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace))
|
2013-02-11 04:35:35 +08:00
|
|
|
parseBracedList();
|
|
|
|
break;
|
2014-09-05 16:42:27 +08:00
|
|
|
case tok::identifier:
|
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript &&
|
2016-05-29 22:41:07 +08:00
|
|
|
(FormatTok->is(Keywords.kw_function) ||
|
|
|
|
FormatTok->startsSequence(Keywords.kw_async, Keywords.kw_function)))
|
2014-09-05 16:42:27 +08:00
|
|
|
tryToParseJSFunction();
|
|
|
|
else
|
|
|
|
nextToken();
|
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:29:06 +08:00
|
|
|
void UnwrappedLineParser::parseSquare() {
|
|
|
|
assert(FormatTok->Tok.is(tok::l_square) && "'[' expected.");
|
|
|
|
if (tryToParseLambda())
|
|
|
|
return;
|
|
|
|
do {
|
|
|
|
switch (FormatTok->Tok.getKind()) {
|
|
|
|
case tok::l_paren:
|
|
|
|
parseParens();
|
|
|
|
break;
|
|
|
|
case tok::r_square:
|
|
|
|
nextToken();
|
|
|
|
return;
|
|
|
|
case tok::r_brace:
|
|
|
|
// A "}" inside parenthesis is an error if there wasn't a matching "{".
|
|
|
|
return;
|
|
|
|
case tok::l_square:
|
|
|
|
parseSquare();
|
|
|
|
break;
|
|
|
|
case tok::l_brace: {
|
2015-05-18 20:52:00 +08:00
|
|
|
if (!tryToParseBracedList())
|
2013-12-23 15:29:06 +08:00
|
|
|
parseChildBlock();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case tok::at:
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok->Tok.is(tok::l_brace))
|
|
|
|
parseBracedList();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
void UnwrappedLineParser::parseIfThenElse() {
|
2013-05-28 19:55:06 +08:00
|
|
|
assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected");
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_paren))
|
2013-01-12 02:28:36 +08:00
|
|
|
parseParens();
|
2012-12-04 02:12:45 +08:00
|
|
|
bool NeedsUnwrappedLine = false;
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2013-12-12 17:49:52 +08:00
|
|
|
CompoundStatementIndenter Indenter(this, Style, Line->Level);
|
2013-06-26 08:30:14 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/false);
|
2015-09-29 22:57:55 +08:00
|
|
|
if (Style.BraceWrapping.BeforeElse)
|
2013-08-03 05:31:59 +08:00
|
|
|
addUnwrappedLine();
|
2015-09-29 22:57:55 +08:00
|
|
|
else
|
2013-08-03 05:31:59 +08:00
|
|
|
NeedsUnwrappedLine = true;
|
2012-12-04 02:12:45 +08:00
|
|
|
} else {
|
|
|
|
addUnwrappedLine();
|
2013-01-09 23:25:02 +08:00
|
|
|
++Line->Level;
|
2013-01-07 22:56:16 +08:00
|
|
|
parseStructuralElement();
|
2013-01-09 23:25:02 +08:00
|
|
|
--Line->Level;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::kw_else)) {
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2013-12-12 17:49:52 +08:00
|
|
|
CompoundStatementIndenter Indenter(this, Style, Line->Level);
|
2013-06-26 08:30:14 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/false);
|
2012-12-04 02:12:45 +08:00
|
|
|
addUnwrappedLine();
|
2013-05-28 19:55:06 +08:00
|
|
|
} else if (FormatTok->Tok.is(tok::kw_if)) {
|
2012-12-04 02:12:45 +08:00
|
|
|
parseIfThenElse();
|
|
|
|
} else {
|
|
|
|
addUnwrappedLine();
|
2013-01-09 23:25:02 +08:00
|
|
|
++Line->Level;
|
2013-01-07 22:56:16 +08:00
|
|
|
parseStructuralElement();
|
2016-05-19 14:30:48 +08:00
|
|
|
if (FormatTok->is(tok::eof))
|
|
|
|
addUnwrappedLine();
|
2013-01-09 23:25:02 +08:00
|
|
|
--Line->Level;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
} else if (NeedsUnwrappedLine) {
|
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-08 19:58:24 +08:00
|
|
|
void UnwrappedLineParser::parseTryCatch() {
|
2015-02-04 23:26:27 +08:00
|
|
|
assert(FormatTok->isOneOf(tok::kw_try, tok::kw___try) && "'try' expected");
|
2014-05-08 19:58:24 +08:00
|
|
|
nextToken();
|
|
|
|
bool NeedsUnwrappedLine = false;
|
|
|
|
if (FormatTok->is(tok::colon)) {
|
|
|
|
// We are in a function try block, what comes is an initializer list.
|
|
|
|
nextToken();
|
|
|
|
while (FormatTok->is(tok::identifier)) {
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok->is(tok::l_paren))
|
|
|
|
parseParens();
|
|
|
|
if (FormatTok->is(tok::comma))
|
|
|
|
nextToken();
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 18:48:41 +08:00
|
|
|
// Parse try with resource.
|
|
|
|
if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_paren)) {
|
|
|
|
parseParens();
|
|
|
|
}
|
2014-05-08 19:58:24 +08:00
|
|
|
if (FormatTok->is(tok::l_brace)) {
|
|
|
|
CompoundStatementIndenter Indenter(this, Style, Line->Level);
|
|
|
|
parseBlock(/*MustBeDeclaration=*/false);
|
2015-09-29 22:57:55 +08:00
|
|
|
if (Style.BraceWrapping.BeforeCatch) {
|
2014-05-08 19:58:24 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
} else {
|
|
|
|
NeedsUnwrappedLine = true;
|
|
|
|
}
|
|
|
|
} else if (!FormatTok->is(tok::kw_catch)) {
|
|
|
|
// The C++ standard requires a compound-statement after a try.
|
|
|
|
// If there's none, we try to assume there's a structuralElement
|
|
|
|
// and try to continue.
|
|
|
|
addUnwrappedLine();
|
|
|
|
++Line->Level;
|
|
|
|
parseStructuralElement();
|
|
|
|
--Line->Level;
|
|
|
|
}
|
2015-02-07 09:57:32 +08:00
|
|
|
while (1) {
|
|
|
|
if (FormatTok->is(tok::at))
|
|
|
|
nextToken();
|
|
|
|
if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
|
|
|
|
tok::kw___finally) ||
|
|
|
|
((Style.Language == FormatStyle::LK_Java ||
|
|
|
|
Style.Language == FormatStyle::LK_JavaScript) &&
|
|
|
|
FormatTok->is(Keywords.kw_finally)) ||
|
|
|
|
(FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
|
|
|
|
FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
|
|
|
|
break;
|
2014-05-08 19:58:24 +08:00
|
|
|
nextToken();
|
|
|
|
while (FormatTok->isNot(tok::l_brace)) {
|
|
|
|
if (FormatTok->is(tok::l_paren)) {
|
|
|
|
parseParens();
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-19 18:50:51 +08:00
|
|
|
if (FormatTok->isOneOf(tok::semi, tok::r_brace, tok::eof))
|
2014-05-08 19:58:24 +08:00
|
|
|
return;
|
|
|
|
nextToken();
|
|
|
|
}
|
|
|
|
NeedsUnwrappedLine = false;
|
|
|
|
CompoundStatementIndenter Indenter(this, Style, Line->Level);
|
|
|
|
parseBlock(/*MustBeDeclaration=*/false);
|
2015-09-29 22:57:55 +08:00
|
|
|
if (Style.BraceWrapping.BeforeCatch)
|
2014-05-08 19:58:24 +08:00
|
|
|
addUnwrappedLine();
|
2015-09-29 22:57:55 +08:00
|
|
|
else
|
2014-05-08 19:58:24 +08:00
|
|
|
NeedsUnwrappedLine = true;
|
|
|
|
}
|
2015-09-29 22:57:55 +08:00
|
|
|
if (NeedsUnwrappedLine)
|
2014-05-08 19:58:24 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
void UnwrappedLineParser::parseNamespace() {
|
2013-05-28 19:55:06 +08:00
|
|
|
assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
|
2014-08-11 20:18:01 +08:00
|
|
|
|
|
|
|
const FormatToken &InitialToken = *FormatTok;
|
2012-12-07 02:03:27 +08:00
|
|
|
nextToken();
|
2015-10-30 13:07:56 +08:00
|
|
|
while (FormatTok->isOneOf(tok::identifier, tok::coloncolon))
|
2012-12-07 02:03:27 +08:00
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2014-08-11 20:18:01 +08:00
|
|
|
if (ShouldBreakBeforeBrace(Style, InitialToken))
|
2013-05-13 20:51:40 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
|
2013-08-01 07:16:02 +08:00
|
|
|
bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
|
|
|
|
(Style.NamespaceIndentation == FormatStyle::NI_Inner &&
|
|
|
|
DeclarationScopeStack.size() > 1);
|
|
|
|
parseBlock(/*MustBeDeclaration=*/true, AddLevel);
|
2013-02-07 00:08:09 +08:00
|
|
|
// Munch the semicolon after a namespace. This is more common than one would
|
|
|
|
// think. Puttin the semicolon into its own line is very ugly.
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::semi))
|
2013-02-07 00:08:09 +08:00
|
|
|
nextToken();
|
2012-12-07 02:03:27 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
|
|
|
// FIXME: Add error handling.
|
|
|
|
}
|
|
|
|
|
2015-03-12 22:44:29 +08:00
|
|
|
void UnwrappedLineParser::parseNew() {
|
|
|
|
assert(FormatTok->is(tok::kw_new) && "'new' expected");
|
|
|
|
nextToken();
|
|
|
|
if (Style.Language != FormatStyle::LK_Java)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// In Java, we can parse everything up to the parens, which aren't optional.
|
|
|
|
do {
|
|
|
|
// There should not be a ;, { or } before the new's open paren.
|
|
|
|
if (FormatTok->isOneOf(tok::semi, tok::l_brace, tok::r_brace))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Consume the parens.
|
|
|
|
if (FormatTok->is(tok::l_paren)) {
|
|
|
|
parseParens();
|
|
|
|
|
|
|
|
// If there is a class body of an anonymous class, consume that as child.
|
|
|
|
if (FormatTok->is(tok::l_brace))
|
|
|
|
parseChildBlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nextToken();
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
2012-12-05 23:06:06 +08:00
|
|
|
void UnwrappedLineParser::parseForOrWhileLoop() {
|
2015-05-04 17:22:29 +08:00
|
|
|
assert(FormatTok->isOneOf(tok::kw_for, tok::kw_while, TT_ForEachMacro) &&
|
2014-04-01 20:55:11 +08:00
|
|
|
"'for', 'while' or foreach macro expected");
|
2012-12-05 23:06:06 +08:00
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_paren))
|
2013-01-12 03:23:05 +08:00
|
|
|
parseParens();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2013-12-12 17:49:52 +08:00
|
|
|
CompoundStatementIndenter Indenter(this, Style, Line->Level);
|
2013-06-26 08:30:14 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/false);
|
2012-12-05 23:06:06 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
} else {
|
|
|
|
addUnwrappedLine();
|
2013-01-09 23:25:02 +08:00
|
|
|
++Line->Level;
|
2013-01-07 22:56:16 +08:00
|
|
|
parseStructuralElement();
|
2013-01-09 23:25:02 +08:00
|
|
|
--Line->Level;
|
2012-12-05 23:06:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
void UnwrappedLineParser::parseDoWhile() {
|
2013-05-28 19:55:06 +08:00
|
|
|
assert(FormatTok->Tok.is(tok::kw_do) && "'do' expected");
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2013-12-12 17:49:52 +08:00
|
|
|
CompoundStatementIndenter Indenter(this, Style, Line->Level);
|
2013-06-26 08:30:14 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/false);
|
2015-09-29 22:57:55 +08:00
|
|
|
if (Style.BraceWrapping.IndentBraces)
|
2013-12-12 17:49:52 +08:00
|
|
|
addUnwrappedLine();
|
2012-12-04 02:12:45 +08:00
|
|
|
} else {
|
|
|
|
addUnwrappedLine();
|
2013-01-09 23:25:02 +08:00
|
|
|
++Line->Level;
|
2013-01-07 22:56:16 +08:00
|
|
|
parseStructuralElement();
|
2013-01-09 23:25:02 +08:00
|
|
|
--Line->Level;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2012-12-04 23:40:36 +08:00
|
|
|
// FIXME: Add error handling.
|
2013-05-28 19:55:06 +08:00
|
|
|
if (!FormatTok->Tok.is(tok::kw_while)) {
|
2012-12-04 23:40:36 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
2013-01-07 22:56:16 +08:00
|
|
|
parseStructuralElement();
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseLabel() {
|
|
|
|
nextToken();
|
2013-01-09 23:25:02 +08:00
|
|
|
unsigned OldLineLevel = Line->Level;
|
2013-03-20 18:23:53 +08:00
|
|
|
if (Line->Level > 1 || (!Line->InPPDirective && Line->Level > 0))
|
2013-01-09 23:25:02 +08:00
|
|
|
--Line->Level;
|
2013-05-28 19:55:06 +08:00
|
|
|
if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
|
2013-12-12 17:49:52 +08:00
|
|
|
CompoundStatementIndenter Indenter(this, Style, Line->Level);
|
2013-06-26 08:30:14 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/false);
|
2013-08-03 05:31:59 +08:00
|
|
|
if (FormatTok->Tok.is(tok::kw_break)) {
|
2015-09-29 22:57:55 +08:00
|
|
|
if (Style.BraceWrapping.AfterControlStatement)
|
2013-08-03 05:31:59 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
parseStructuralElement();
|
|
|
|
}
|
2013-12-12 17:49:52 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
} else {
|
2015-05-06 23:19:47 +08:00
|
|
|
if (FormatTok->is(tok::semi))
|
|
|
|
nextToken();
|
2013-12-12 17:49:52 +08:00
|
|
|
addUnwrappedLine();
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-09 23:25:02 +08:00
|
|
|
Line->Level = OldLineLevel;
|
2016-04-07 00:41:39 +08:00
|
|
|
if (FormatTok->isNot(tok::l_brace)) {
|
2016-04-06 23:02:46 +08:00
|
|
|
parseStructuralElement();
|
2016-04-07 00:41:39 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseCaseLabel() {
|
2013-05-28 19:55:06 +08:00
|
|
|
assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected");
|
2012-12-04 02:12:45 +08:00
|
|
|
// FIXME: fix handling of complex expressions here.
|
|
|
|
do {
|
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
} while (!eof() && !FormatTok->Tok.is(tok::colon));
|
2012-12-04 02:12:45 +08:00
|
|
|
parseLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseSwitch() {
|
2013-05-28 19:55:06 +08:00
|
|
|
assert(FormatTok->Tok.is(tok::kw_switch) && "'switch' expected");
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_paren))
|
2013-01-12 03:23:05 +08:00
|
|
|
parseParens();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2013-12-12 17:49:52 +08:00
|
|
|
CompoundStatementIndenter Indenter(this, Style, Line->Level);
|
2013-08-01 07:16:02 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/false);
|
2012-12-04 02:12:45 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
} else {
|
|
|
|
addUnwrappedLine();
|
2013-07-25 19:31:57 +08:00
|
|
|
++Line->Level;
|
2013-01-07 22:56:16 +08:00
|
|
|
parseStructuralElement();
|
2013-07-25 19:31:57 +08:00
|
|
|
--Line->Level;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseAccessSpecifier() {
|
|
|
|
nextToken();
|
2013-11-24 01:53:41 +08:00
|
|
|
// Understand Qt's slots.
|
2015-04-07 23:04:40 +08:00
|
|
|
if (FormatTok->isOneOf(Keywords.kw_slots, Keywords.kw_qslots))
|
2013-11-24 01:53:41 +08:00
|
|
|
nextToken();
|
2012-12-11 00:34:48 +08:00
|
|
|
// Otherwise, we don't know what it is, and we'd better keep the next token.
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::colon))
|
2012-12-11 00:34:48 +08:00
|
|
|
nextToken();
|
2012-12-04 02:12:45 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
|
|
|
|
2015-12-29 16:54:23 +08:00
|
|
|
bool UnwrappedLineParser::parseEnum() {
|
2014-11-13 23:56:28 +08:00
|
|
|
// Won't be 'enum' for NS_ENUMs.
|
|
|
|
if (FormatTok->Tok.is(tok::kw_enum))
|
2014-11-20 06:38:18 +08:00
|
|
|
nextToken();
|
2014-11-13 23:56:28 +08:00
|
|
|
|
2015-12-29 16:54:23 +08:00
|
|
|
// In TypeScript, "enum" can also be used as property name, e.g. in interface
|
|
|
|
// declarations. An "enum" keyword followed by a colon would be a syntax
|
|
|
|
// error and thus assume it is just an identifier.
|
2016-02-03 13:33:44 +08:00
|
|
|
if (Style.Language == FormatStyle::LK_JavaScript &&
|
|
|
|
FormatTok->isOneOf(tok::colon, tok::question))
|
2015-12-29 16:54:23 +08:00
|
|
|
return false;
|
|
|
|
|
2013-08-20 20:42:50 +08:00
|
|
|
// Eat up enum class ...
|
2014-05-09 21:11:16 +08:00
|
|
|
if (FormatTok->Tok.is(tok::kw_class) || FormatTok->Tok.is(tok::kw_struct))
|
|
|
|
nextToken();
|
2015-06-19 16:17:32 +08:00
|
|
|
|
2013-09-07 05:32:35 +08:00
|
|
|
while (FormatTok->Tok.getIdentifierInfo() ||
|
2014-11-20 06:38:18 +08:00
|
|
|
FormatTok->isOneOf(tok::colon, tok::coloncolon, tok::less,
|
|
|
|
tok::greater, tok::comma, tok::question)) {
|
2013-01-22 03:17:52 +08:00
|
|
|
nextToken();
|
|
|
|
// We can have macros or attributes in between 'enum' and the enum name.
|
2014-11-20 06:38:18 +08:00
|
|
|
if (FormatTok->is(tok::l_paren))
|
2012-12-04 22:46:19 +08:00
|
|
|
parseParens();
|
2015-06-19 16:17:32 +08:00
|
|
|
if (FormatTok->is(tok::identifier)) {
|
2012-12-04 22:46:19 +08:00
|
|
|
nextToken();
|
2015-06-19 16:17:32 +08:00
|
|
|
// If there are two identifiers in a row, this is likely an elaborate
|
|
|
|
// return type. In Java, this can be "implements", etc.
|
|
|
|
if (Style.Language == FormatStyle::LK_Cpp &&
|
|
|
|
FormatTok->is(tok::identifier))
|
2015-12-29 16:54:23 +08:00
|
|
|
return false;
|
2015-06-19 16:17:32 +08:00
|
|
|
}
|
2013-01-22 03:17:52 +08:00
|
|
|
}
|
2014-11-13 23:56:28 +08:00
|
|
|
|
|
|
|
// Just a declaration or something is wrong.
|
2014-11-20 06:38:18 +08:00
|
|
|
if (FormatTok->isNot(tok::l_brace))
|
2015-12-29 16:54:23 +08:00
|
|
|
return true;
|
2014-11-13 23:56:28 +08:00
|
|
|
FormatTok->BlockKind = BK_Block;
|
|
|
|
|
|
|
|
if (Style.Language == FormatStyle::LK_Java) {
|
|
|
|
// Java enums are different.
|
|
|
|
parseJavaEnumBody();
|
2015-12-29 16:54:23 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Style.Language == FormatStyle::LK_Proto) {
|
2015-07-16 22:25:43 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/true);
|
2015-12-29 16:54:23 +08:00
|
|
|
return true;
|
2013-01-22 03:17:52 +08:00
|
|
|
}
|
2014-11-13 23:56:28 +08:00
|
|
|
|
|
|
|
// Parse enum body.
|
|
|
|
bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
|
|
|
|
if (HasError) {
|
|
|
|
if (FormatTok->is(tok::semi))
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
2015-12-29 16:54:23 +08:00
|
|
|
return true;
|
2014-11-13 23:56:28 +08:00
|
|
|
|
2015-06-17 17:44:02 +08:00
|
|
|
// There is no addUnwrappedLine() here so that we fall through to parsing a
|
|
|
|
// structural element afterwards. Thus, in "enum A {} n, m;",
|
2013-01-22 03:17:52 +08:00
|
|
|
// "} n, m;" will end up in one unwrapped line.
|
2014-11-13 23:56:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseJavaEnumBody() {
|
|
|
|
// Determine whether the enum is simple, i.e. does not have a semicolon or
|
|
|
|
// constants with class bodies. Simple enums can be formatted like braced
|
|
|
|
// lists, contracted to a single line, etc.
|
|
|
|
unsigned StoredPosition = Tokens->getPosition();
|
|
|
|
bool IsSimple = true;
|
|
|
|
FormatToken *Tok = Tokens->getNextToken();
|
|
|
|
while (Tok) {
|
|
|
|
if (Tok->is(tok::r_brace))
|
|
|
|
break;
|
|
|
|
if (Tok->isOneOf(tok::l_brace, tok::semi)) {
|
|
|
|
IsSimple = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FIXME: This will also mark enums with braces in the arguments to enum
|
|
|
|
// constants as "not simple". This is probably fine in practice, though.
|
|
|
|
Tok = Tokens->getNextToken();
|
|
|
|
}
|
|
|
|
FormatTok = Tokens->setPosition(StoredPosition);
|
|
|
|
|
|
|
|
if (IsSimple) {
|
|
|
|
parseBracedList();
|
2014-11-03 06:31:39 +08:00
|
|
|
addUnwrappedLine();
|
2014-11-13 23:56:28 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the body of a more complex enum.
|
|
|
|
// First add a line for everything up to the "{".
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
++Line->Level;
|
|
|
|
|
|
|
|
// Parse the enum constants.
|
|
|
|
while (FormatTok) {
|
|
|
|
if (FormatTok->is(tok::l_brace)) {
|
|
|
|
// Parse the constant's class body.
|
|
|
|
parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
|
|
|
|
/*MunchSemi=*/false);
|
|
|
|
} else if (FormatTok->is(tok::l_paren)) {
|
|
|
|
parseParens();
|
|
|
|
} else if (FormatTok->is(tok::comma)) {
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
} else if (FormatTok->is(tok::semi)) {
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
break;
|
|
|
|
} else if (FormatTok->is(tok::r_brace)) {
|
|
|
|
addUnwrappedLine();
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
nextToken();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the class body after the enum's ";" if any.
|
|
|
|
parseLevel(/*HasOpeningBrace=*/true);
|
|
|
|
nextToken();
|
|
|
|
--Line->Level;
|
|
|
|
addUnwrappedLine();
|
2013-01-08 02:10:23 +08:00
|
|
|
}
|
|
|
|
|
2013-01-15 21:38:33 +08:00
|
|
|
void UnwrappedLineParser::parseRecord() {
|
2014-08-11 20:18:01 +08:00
|
|
|
const FormatToken &InitialToken = *FormatTok;
|
2013-01-08 02:10:23 +08:00
|
|
|
nextToken();
|
2015-05-06 22:03:02 +08:00
|
|
|
|
|
|
|
// The actual identifier can be a nested name specifier, and in macros
|
|
|
|
// it is often token-pasted.
|
|
|
|
while (FormatTok->isOneOf(tok::identifier, tok::coloncolon, tok::hashhash,
|
|
|
|
tok::kw___attribute, tok::kw___declspec,
|
|
|
|
tok::kw_alignas) ||
|
|
|
|
((Style.Language == FormatStyle::LK_Java ||
|
|
|
|
Style.Language == FormatStyle::LK_JavaScript) &&
|
|
|
|
FormatTok->isOneOf(tok::period, tok::comma))) {
|
|
|
|
bool IsNonMacroIdentifier =
|
|
|
|
FormatTok->is(tok::identifier) &&
|
|
|
|
FormatTok->TokenText != FormatTok->TokenText.upper();
|
2013-01-15 21:38:33 +08:00
|
|
|
nextToken();
|
|
|
|
// We can have macros or attributes in between 'class' and the class name.
|
2015-05-06 22:03:02 +08:00
|
|
|
if (!IsNonMacroIdentifier && FormatTok->Tok.is(tok::l_paren))
|
2013-01-15 21:38:33 +08:00
|
|
|
parseParens();
|
2015-05-06 22:03:02 +08:00
|
|
|
}
|
2013-01-15 21:38:33 +08:00
|
|
|
|
2015-05-06 22:03:02 +08:00
|
|
|
// Note that parsing away template declarations here leads to incorrectly
|
|
|
|
// accepting function declarations as record declarations.
|
|
|
|
// In general, we cannot solve this problem. Consider:
|
|
|
|
// class A<int> B() {}
|
|
|
|
// which can be a function definition or a class definition when B() is a
|
|
|
|
// macro. If we find enough real-world cases where this is a problem, we
|
|
|
|
// can parse for the 'template' keyword in the beginning of the statement,
|
|
|
|
// and thus rule out the record production in case there is no template
|
|
|
|
// (this would still leave us with an ambiguity between template function
|
|
|
|
// and class declarations).
|
2015-05-18 20:52:00 +08:00
|
|
|
if (FormatTok->isOneOf(tok::colon, tok::less)) {
|
|
|
|
while (!eof()) {
|
2015-05-18 22:49:19 +08:00
|
|
|
if (FormatTok->is(tok::l_brace)) {
|
|
|
|
calculateBraceTypes(/*ExpectClassBody=*/true);
|
|
|
|
if (!tryToParseBracedList())
|
|
|
|
break;
|
|
|
|
}
|
2015-05-06 22:03:02 +08:00
|
|
|
if (FormatTok->Tok.is(tok::semi))
|
|
|
|
return;
|
|
|
|
nextToken();
|
2013-01-08 02:10:23 +08:00
|
|
|
}
|
2013-01-15 21:38:33 +08:00
|
|
|
}
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2014-08-11 20:18:01 +08:00
|
|
|
if (ShouldBreakBeforeBrace(Style, InitialToken))
|
2013-05-13 20:51:40 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
|
2014-03-31 22:23:49 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
|
2013-10-13 06:46:56 +08:00
|
|
|
/*MunchSemi=*/false);
|
2013-05-13 20:51:40 +08:00
|
|
|
}
|
2015-06-17 17:44:02 +08:00
|
|
|
// There is no addUnwrappedLine() here so that we fall through to parsing a
|
|
|
|
// structural element afterwards. Thus, in "class A {} n, m;",
|
|
|
|
// "} n, m;" will end up in one unwrapped line.
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-10 05:15:03 +08:00
|
|
|
void UnwrappedLineParser::parseObjCProtocolList() {
|
2013-05-28 19:55:06 +08:00
|
|
|
assert(FormatTok->Tok.is(tok::less) && "'<' expected.");
|
2013-01-10 05:15:03 +08:00
|
|
|
do
|
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
while (!eof() && FormatTok->Tok.isNot(tok::greater));
|
2013-01-10 05:15:03 +08:00
|
|
|
nextToken(); // Skip '>'.
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseObjCUntilAtEnd() {
|
|
|
|
do {
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.isObjCAtKeyword(tok::objc_end)) {
|
2013-01-10 05:15:03 +08:00
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
break;
|
|
|
|
}
|
2013-08-28 16:04:23 +08:00
|
|
|
if (FormatTok->is(tok::l_brace)) {
|
|
|
|
parseBlock(/*MustBeDeclaration=*/false);
|
|
|
|
// In ObjC interfaces, nothing should be following the "}".
|
|
|
|
addUnwrappedLine();
|
2014-01-08 23:59:42 +08:00
|
|
|
} else if (FormatTok->is(tok::r_brace)) {
|
|
|
|
// Ignore stray "}". parseStructuralElement doesn't consume them.
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
2013-08-28 16:04:23 +08:00
|
|
|
} else {
|
|
|
|
parseStructuralElement();
|
|
|
|
}
|
2013-01-10 05:15:03 +08:00
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
2013-01-10 07:25:37 +08:00
|
|
|
void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
|
2013-01-10 04:25:35 +08:00
|
|
|
nextToken();
|
2013-03-20 20:37:50 +08:00
|
|
|
nextToken(); // interface name
|
2013-01-10 04:25:35 +08:00
|
|
|
|
|
|
|
// @interface can be followed by either a base class, or a category.
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::colon)) {
|
2013-01-10 04:25:35 +08:00
|
|
|
nextToken();
|
2013-03-20 20:37:50 +08:00
|
|
|
nextToken(); // base class name
|
2013-05-28 19:55:06 +08:00
|
|
|
} else if (FormatTok->Tok.is(tok::l_paren))
|
2013-01-10 04:25:35 +08:00
|
|
|
// Skip category, if present.
|
|
|
|
parseParens();
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::less))
|
2013-01-10 05:15:03 +08:00
|
|
|
parseObjCProtocolList();
|
2013-01-10 04:25:35 +08:00
|
|
|
|
2014-05-03 01:01:46 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2015-09-29 22:57:55 +08:00
|
|
|
if (Style.BraceWrapping.AfterObjCDeclaration)
|
2014-05-03 01:01:46 +08:00
|
|
|
addUnwrappedLine();
|
2013-06-26 08:30:14 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/true);
|
2014-05-03 01:01:46 +08:00
|
|
|
}
|
2013-01-10 04:25:35 +08:00
|
|
|
|
|
|
|
// With instance variables, this puts '}' on its own line. Without instance
|
|
|
|
// variables, this ends the @interface line.
|
|
|
|
addUnwrappedLine();
|
|
|
|
|
2013-01-10 05:15:03 +08:00
|
|
|
parseObjCUntilAtEnd();
|
|
|
|
}
|
2013-01-10 04:25:35 +08:00
|
|
|
|
2013-01-10 05:15:03 +08:00
|
|
|
void UnwrappedLineParser::parseObjCProtocol() {
|
|
|
|
nextToken();
|
2013-03-20 20:37:50 +08:00
|
|
|
nextToken(); // protocol name
|
2013-01-10 05:15:03 +08:00
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::less))
|
2013-01-10 05:15:03 +08:00
|
|
|
parseObjCProtocolList();
|
|
|
|
|
|
|
|
// Check for protocol declaration.
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::semi)) {
|
2013-01-10 05:15:03 +08:00
|
|
|
nextToken();
|
|
|
|
return addUnwrappedLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
addUnwrappedLine();
|
|
|
|
parseObjCUntilAtEnd();
|
2013-01-10 04:25:35 +08:00
|
|
|
}
|
|
|
|
|
2015-02-20 00:14:18 +08:00
|
|
|
void UnwrappedLineParser::parseJavaScriptEs6ImportExport() {
|
2016-04-19 22:55:37 +08:00
|
|
|
bool IsImport = FormatTok->is(Keywords.kw_import);
|
|
|
|
assert(IsImport || FormatTok->is(tok::kw_export));
|
2015-02-20 00:07:32 +08:00
|
|
|
nextToken();
|
2015-02-20 00:14:18 +08:00
|
|
|
|
2015-05-11 17:14:50 +08:00
|
|
|
// Consume the "default" in "export default class/function".
|
2015-05-11 17:03:10 +08:00
|
|
|
if (FormatTok->is(tok::kw_default))
|
|
|
|
nextToken();
|
2015-05-11 17:14:50 +08:00
|
|
|
|
2016-04-25 06:05:09 +08:00
|
|
|
// Consume "async function", "function" and "default function", so that these
|
|
|
|
// get parsed as free-standing JS functions, i.e. do not require a trailing
|
|
|
|
// semicolon.
|
|
|
|
if (FormatTok->is(Keywords.kw_async))
|
|
|
|
nextToken();
|
2015-05-11 17:03:10 +08:00
|
|
|
if (FormatTok->is(Keywords.kw_function)) {
|
|
|
|
nextToken();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-19 22:55:37 +08:00
|
|
|
// For imports, `export *`, `export {...}`, consume the rest of the line up
|
|
|
|
// to the terminating `;`. For everything else, just return and continue
|
|
|
|
// parsing the structural element, i.e. the declaration or expression for
|
|
|
|
// `export default`.
|
|
|
|
if (!IsImport && !FormatTok->isOneOf(tok::l_brace, tok::star) &&
|
|
|
|
!FormatTok->isStringLiteral())
|
|
|
|
return;
|
2015-02-20 00:14:18 +08:00
|
|
|
|
2016-01-07 16:53:35 +08:00
|
|
|
while (!eof() && FormatTok->isNot(tok::semi)) {
|
|
|
|
if (FormatTok->is(tok::l_brace)) {
|
|
|
|
FormatTok->BlockKind = BK_Block;
|
|
|
|
parseBracedList();
|
|
|
|
} else {
|
|
|
|
nextToken();
|
|
|
|
}
|
2015-02-20 00:07:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-06 00:05:56 +08:00
|
|
|
LLVM_ATTRIBUTE_UNUSED static void printDebugInfo(const UnwrappedLine &Line,
|
|
|
|
StringRef Prefix = "") {
|
2013-09-05 17:29:45 +08:00
|
|
|
llvm::dbgs() << Prefix << "Line(" << Line.Level << ")"
|
|
|
|
<< (Line.InPPDirective ? " MACRO" : "") << ": ";
|
|
|
|
for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
|
|
|
|
E = Line.Tokens.end();
|
|
|
|
I != E; ++I) {
|
2013-09-05 18:04:31 +08:00
|
|
|
llvm::dbgs() << I->Tok->Tok.getName() << "[" << I->Tok->Type << "] ";
|
2013-09-05 17:29:45 +08:00
|
|
|
}
|
|
|
|
for (std::list<UnwrappedLineNode>::const_iterator I = Line.Tokens.begin(),
|
|
|
|
E = Line.Tokens.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
const UnwrappedLineNode &Node = *I;
|
|
|
|
for (SmallVectorImpl<UnwrappedLine>::const_iterator
|
|
|
|
I = Node.Children.begin(),
|
|
|
|
E = Node.Children.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
printDebugInfo(*I, "\nChild: ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm::dbgs() << "\n";
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
void UnwrappedLineParser::addUnwrappedLine() {
|
2013-01-16 17:10:19 +08:00
|
|
|
if (Line->Tokens.empty())
|
2013-01-08 22:56:18 +08:00
|
|
|
return;
|
2013-01-16 20:31:12 +08:00
|
|
|
DEBUG({
|
2013-09-05 17:29:45 +08:00
|
|
|
if (CurrentLines == &Lines)
|
|
|
|
printDebugInfo(*Line);
|
2013-01-16 20:31:12 +08:00
|
|
|
});
|
2015-05-31 19:18:05 +08:00
|
|
|
CurrentLines->push_back(std::move(*Line));
|
2013-01-16 17:10:19 +08:00
|
|
|
Line->Tokens.clear();
|
2013-01-18 22:04:34 +08:00
|
|
|
if (CurrentLines == &Lines && !PreprocessorDirectives.empty()) {
|
2015-05-31 19:18:05 +08:00
|
|
|
CurrentLines->append(
|
|
|
|
std::make_move_iterator(PreprocessorDirectives.begin()),
|
|
|
|
std::make_move_iterator(PreprocessorDirectives.end()));
|
2013-01-18 22:04:34 +08:00
|
|
|
PreprocessorDirectives.clear();
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
bool UnwrappedLineParser::eof() const { return FormatTok->Tok.is(tok::eof); }
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2014-05-09 21:11:16 +08:00
|
|
|
bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
|
2014-04-11 20:27:47 +08:00
|
|
|
return (Line->InPPDirective || FormatTok.HasUnescapedNewline) &&
|
|
|
|
FormatTok.NewlinesBefore > 0;
|
|
|
|
}
|
|
|
|
|
2013-01-23 00:31:55 +08:00
|
|
|
void UnwrappedLineParser::flushComments(bool NewlineBeforeNext) {
|
|
|
|
bool JustComments = Line->Tokens.empty();
|
2013-05-28 19:55:06 +08:00
|
|
|
for (SmallVectorImpl<FormatToken *>::const_iterator
|
2013-01-23 00:31:55 +08:00
|
|
|
I = CommentsBeforeNextToken.begin(),
|
|
|
|
E = CommentsBeforeNextToken.end();
|
|
|
|
I != E; ++I) {
|
2015-05-13 19:35:53 +08:00
|
|
|
if (isOnNewLine(**I) && JustComments)
|
2013-01-23 00:31:55 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
pushToken(*I);
|
|
|
|
}
|
2015-05-13 19:35:53 +08:00
|
|
|
if (NewlineBeforeNext && JustComments)
|
2013-01-23 00:31:55 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
CommentsBeforeNextToken.clear();
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
void UnwrappedLineParser::nextToken() {
|
|
|
|
if (eof())
|
|
|
|
return;
|
2014-04-11 20:27:47 +08:00
|
|
|
flushComments(isOnNewLine(*FormatTok));
|
2013-01-23 00:31:55 +08:00
|
|
|
pushToken(FormatTok);
|
2016-03-15 03:21:36 +08:00
|
|
|
if (Style.Language != FormatStyle::LK_JavaScript)
|
|
|
|
readToken();
|
|
|
|
else
|
|
|
|
readTokenWithJavaScriptASI();
|
2013-01-05 07:34:14 +08:00
|
|
|
}
|
|
|
|
|
2016-01-09 23:56:28 +08:00
|
|
|
const FormatToken *UnwrappedLineParser::getPreviousToken() {
|
|
|
|
// FIXME: This is a dirty way to access the previous token. Find a better
|
|
|
|
// solution.
|
|
|
|
if (!Line || Line->Tokens.empty())
|
|
|
|
return nullptr;
|
|
|
|
return Line->Tokens.back().Tok;
|
|
|
|
}
|
|
|
|
|
2013-01-05 07:34:14 +08:00
|
|
|
void UnwrappedLineParser::readToken() {
|
2013-01-23 00:31:55 +08:00
|
|
|
bool CommentsInCurrentLine = true;
|
|
|
|
do {
|
|
|
|
FormatTok = Tokens->getNextToken();
|
2014-03-13 21:59:48 +08:00
|
|
|
assert(FormatTok);
|
2013-05-28 19:55:06 +08:00
|
|
|
while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
|
|
|
|
(FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
|
2013-01-23 00:31:55 +08:00
|
|
|
// If there is an unfinished unwrapped line, we flush the preprocessor
|
|
|
|
// directives only after that unwrapped line was finished later.
|
2015-02-08 17:34:49 +08:00
|
|
|
bool SwitchToPreprocessorLines = !Line->Tokens.empty();
|
2013-01-23 00:31:55 +08:00
|
|
|
ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
|
2013-04-03 20:38:53 +08:00
|
|
|
// Comments stored before the preprocessor directive need to be output
|
|
|
|
// before the preprocessor directive, at the same level as the
|
|
|
|
// preprocessor directive, as we consider them to apply to the directive.
|
2014-04-11 20:27:47 +08:00
|
|
|
flushComments(isOnNewLine(*FormatTok));
|
2013-01-23 00:31:55 +08:00
|
|
|
parsePPDirective();
|
|
|
|
}
|
2014-04-14 17:14:11 +08:00
|
|
|
while (FormatTok->Type == TT_ConflictStart ||
|
|
|
|
FormatTok->Type == TT_ConflictEnd ||
|
|
|
|
FormatTok->Type == TT_ConflictAlternative) {
|
|
|
|
if (FormatTok->Type == TT_ConflictStart) {
|
|
|
|
conditionalCompilationStart(/*Unreachable=*/false);
|
|
|
|
} else if (FormatTok->Type == TT_ConflictAlternative) {
|
|
|
|
conditionalCompilationAlternative();
|
2014-05-09 21:11:16 +08:00
|
|
|
} else if (FormatTok->Type == TT_ConflictEnd) {
|
2014-04-14 17:14:11 +08:00
|
|
|
conditionalCompilationEnd();
|
|
|
|
}
|
|
|
|
FormatTok = Tokens->getNextToken();
|
|
|
|
FormatTok->MustBreakBefore = true;
|
|
|
|
}
|
2013-05-25 02:24:24 +08:00
|
|
|
|
|
|
|
if (!PPStack.empty() && (PPStack.back() == PP_Unreachable) &&
|
|
|
|
!Line->InPPDirective) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
if (!FormatTok->Tok.is(tok::comment))
|
2013-01-23 00:31:55 +08:00
|
|
|
return;
|
2014-04-11 20:27:47 +08:00
|
|
|
if (isOnNewLine(*FormatTok) || FormatTok->IsFirst) {
|
2013-01-23 00:31:55 +08:00
|
|
|
CommentsInCurrentLine = false;
|
|
|
|
}
|
|
|
|
if (CommentsInCurrentLine) {
|
|
|
|
pushToken(FormatTok);
|
|
|
|
} else {
|
|
|
|
CommentsBeforeNextToken.push_back(FormatTok);
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
void UnwrappedLineParser::pushToken(FormatToken *Tok) {
|
2013-09-05 17:29:45 +08:00
|
|
|
Line->Tokens.push_back(UnwrappedLineNode(Tok));
|
2013-01-23 00:31:55 +08:00
|
|
|
if (MustBreakBeforeNextToken) {
|
2013-09-05 17:29:45 +08:00
|
|
|
Line->Tokens.back().Tok->MustBreakBefore = true;
|
2013-01-23 00:31:55 +08:00
|
|
|
MustBreakBeforeNextToken = false;
|
2013-01-05 07:34:14 +08:00
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-07 21:26:07 +08:00
|
|
|
} // end namespace format
|
|
|
|
} // end namespace clang
|