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-16 20:31:12 +08:00
|
|
|
#define DEBUG_TYPE "format-parser"
|
|
|
|
|
2013-01-19 16:09:44 +08:00
|
|
|
#include "UnwrappedLineParser.h"
|
2013-01-16 20:31:12 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-12-04 02:12:45 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
class FormatTokenSource {
|
|
|
|
public:
|
|
|
|
virtual ~FormatTokenSource() {}
|
|
|
|
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,
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatToken *&ResetToken, bool &StructuralError)
|
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),
|
|
|
|
StructuralError(StructuralError),
|
2013-05-28 19:55:06 +08:00
|
|
|
PreviousStructuralError(StructuralError), Token(NULL) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedMacroState() {
|
|
|
|
TokenSource = PreviousTokenSource;
|
|
|
|
ResetToken = Token;
|
|
|
|
Line.InPPDirective = false;
|
2013-01-06 06:14:16 +08:00
|
|
|
Line.Level = PreviousLineLevel;
|
2013-04-12 22:13:36 +08:00
|
|
|
StructuralError = PreviousStructuralError;
|
2013-01-05 07:34:14 +08:00
|
|
|
}
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
virtual FormatToken *getNextToken() {
|
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;
|
|
|
|
}
|
|
|
|
|
2013-05-31 22:56:29 +08:00
|
|
|
virtual unsigned getPosition() { return PreviousTokenSource->getPosition(); }
|
2013-05-23 17:41:43 +08:00
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
virtual FormatToken *setPosition(unsigned Position) {
|
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-04-12 22:13:36 +08:00
|
|
|
bool &StructuralError;
|
|
|
|
bool PreviousStructuralError;
|
2013-01-05 07:34:14 +08:00
|
|
|
|
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)
|
2013-09-05 17:29:45 +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;
|
2013-01-10 19:52:21 +08:00
|
|
|
PreBlockLine = Parser.Line.take();
|
2013-01-16 17:10:19 +08:00
|
|
|
Parser.Line.reset(new UnwrappedLine());
|
|
|
|
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());
|
2013-01-10 19:52:21 +08:00
|
|
|
Parser.Line.reset(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;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
|
|
|
|
Parser->addUnwrappedLine();
|
|
|
|
} else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
|
|
|
|
Parser->addUnwrappedLine();
|
|
|
|
++LineLevel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~CompoundStatementIndenter() {
|
|
|
|
LineLevel = OldLineLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {}
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
virtual FormatToken *getNextToken() {
|
2013-05-23 17:41:43 +08:00
|
|
|
++Position;
|
|
|
|
return Tokens[Position];
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual unsigned getPosition() {
|
|
|
|
assert(Position >= 0);
|
|
|
|
return Position;
|
|
|
|
}
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
virtual FormatToken *setPosition(unsigned P) {
|
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,
|
2013-05-28 19:55:06 +08:00
|
|
|
ArrayRef<FormatToken *> Tokens,
|
2013-05-15 16:14:19 +08:00
|
|
|
UnwrappedLineConsumer &Callback)
|
2013-01-18 22:04:34 +08:00
|
|
|
: Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
|
2013-05-28 19:55:06 +08:00
|
|
|
CurrentLines(&Lines), StructuralError(false), Style(Style), Tokens(NULL),
|
2013-10-12 05:25:45 +08:00
|
|
|
Callback(Callback), AllTokens(Tokens), PPBranchLevel(-1) {}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::reset() {
|
|
|
|
PPBranchLevel = -1;
|
|
|
|
Line.reset(new UnwrappedLine);
|
|
|
|
CommentsBeforeNextToken.clear();
|
|
|
|
FormatTok = NULL;
|
|
|
|
MustBreakBeforeNextToken = false;
|
|
|
|
PreprocessorDirectives.clear();
|
|
|
|
CurrentLines = &Lines;
|
|
|
|
DeclarationScopeStack.clear();
|
|
|
|
StructuralError = false;
|
|
|
|
PPStack.clear();
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2012-12-05 01:27:50 +08:00
|
|
|
bool 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-03-02 02:11:39 +08:00
|
|
|
|
2013-04-12 22:13:36 +08:00
|
|
|
return StructuralError;
|
2013-01-05 07:34:14 +08:00
|
|
|
}
|
|
|
|
|
2013-04-12 22:13:36 +08:00
|
|
|
void UnwrappedLineParser::parseFile() {
|
2013-03-23 00:55:40 +08:00
|
|
|
ScopedDeclarationState DeclarationState(
|
|
|
|
*Line, DeclarationScopeStack,
|
|
|
|
/*MustBeDeclaration=*/ !Line->InPPDirective);
|
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 {
|
2013-05-28 19:55:06 +08:00
|
|
|
switch (FormatTok->Tok.getKind()) {
|
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.
|
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;
|
|
|
|
StructuralError = true;
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2013-05-23 17:41:43 +08:00
|
|
|
void UnwrappedLineParser::calculateBraceTypes() {
|
|
|
|
// 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();
|
|
|
|
unsigned Position = StoredPosition;
|
2013-05-28 19:55:06 +08:00
|
|
|
FormatToken *Tok = FormatTok;
|
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 {
|
2013-07-01 17:15:46 +08:00
|
|
|
// Get next none-comment token.
|
|
|
|
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:
|
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:
|
|
|
|
if (!LBraceStack.empty()) {
|
2013-07-09 17:06:29 +08:00
|
|
|
if (LBraceStack.back()->BlockKind == BK_Unknown) {
|
2013-05-23 17:41:43 +08:00
|
|
|
// If there is a comma, semicolon or right paren after the closing
|
2013-09-04 16:20:47 +08:00
|
|
|
// 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).
|
2013-08-28 15:50:37 +08:00
|
|
|
//
|
|
|
|
// We exclude + and - as they can be ObjC visibility modifiers.
|
2013-11-22 15:26:53 +08:00
|
|
|
if (NextTok->isOneOf(tok::comma, tok::semi, tok::r_paren, tok::period,
|
2013-09-13 18:55:31 +08:00
|
|
|
tok::r_square, tok::l_brace, tok::colon) ||
|
2013-08-28 15:50:37 +08:00
|
|
|
(NextTok->isBinaryOperator() &&
|
|
|
|
!NextTok->isOneOf(tok::plus, tok::minus))) {
|
2013-07-09 17:06:29 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
LBraceStack.pop_back();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case tok::semi:
|
|
|
|
case tok::kw_if:
|
|
|
|
case tok::kw_while:
|
|
|
|
case tok::kw_for:
|
|
|
|
case tok::kw_switch:
|
|
|
|
case tok::kw_try:
|
2013-05-31 22:56:29 +08:00
|
|
|
if (!LBraceStack.empty())
|
2013-07-09 17:06:29 +08:00
|
|
|
LBraceStack.back()->BlockKind = BK_Block;
|
2013-05-23 17:41:43 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Tok = NextTok;
|
2013-07-02 00:43:38 +08:00
|
|
|
Position += ReadTokens;
|
2013-09-04 16:20:47 +08:00
|
|
|
} while (Tok->Tok.isNot(tok::eof) && !LBraceStack.empty());
|
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) {
|
2013-05-28 19:55:06 +08:00
|
|
|
assert(FormatTok->Tok.is(tok::l_brace) && "'{' expected");
|
2013-07-25 19:31:57 +08:00
|
|
|
unsigned InitialLevel = Line->Level;
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
|
|
|
|
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
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
if (!FormatTok->Tok.is(tok::r_brace)) {
|
2013-07-25 19:31:57 +08:00
|
|
|
Line->Level = InitialLevel;
|
2013-04-12 22:13:36 +08:00
|
|
|
StructuralError = true;
|
|
|
|
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.
|
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
|
|
|
}
|
|
|
|
|
2013-09-04 21:25:30 +08:00
|
|
|
void UnwrappedLineParser::parseChildBlock() {
|
|
|
|
FormatTok->BlockKind = BK_Block;
|
|
|
|
nextToken();
|
|
|
|
{
|
|
|
|
ScopedLineState LineState(*this);
|
|
|
|
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
|
|
|
|
/*MustBeDeclaration=*/false);
|
|
|
|
Line->Level += 1;
|
|
|
|
parseLevel(/*HasOpeningBrace=*/true);
|
|
|
|
Line->Level -= 1;
|
|
|
|
}
|
|
|
|
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");
|
2013-04-12 22:13:36 +08:00
|
|
|
ScopedMacroState MacroState(*Line, Tokens, FormatTok, StructuralError);
|
2013-01-03 00:30:12 +08:00
|
|
|
nextToken();
|
|
|
|
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.getIdentifierInfo() == NULL) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-25 02:24:24 +08:00
|
|
|
void UnwrappedLineParser::pushPPConditional() {
|
|
|
|
if (!PPStack.empty() && PPStack.back() == PP_Unreachable)
|
|
|
|
PPStack.push_back(PP_Unreachable);
|
|
|
|
else
|
|
|
|
PPStack.push_back(PP_Conditional);
|
|
|
|
}
|
|
|
|
|
2013-10-12 05:25:45 +08:00
|
|
|
void UnwrappedLineParser::parsePPIf(bool IfDef) {
|
|
|
|
++PPBranchLevel;
|
|
|
|
assert(PPBranchLevel >= 0 && PPBranchLevel <= (int)PPLevelBranchIndex.size());
|
|
|
|
if (PPBranchLevel == (int)PPLevelBranchIndex.size()) {
|
|
|
|
PPLevelBranchIndex.push_back(0);
|
|
|
|
PPLevelBranchCount.push_back(0);
|
|
|
|
}
|
|
|
|
PPChainBranchIndex.push(0);
|
2013-05-25 02:24:24 +08:00
|
|
|
nextToken();
|
2013-10-12 05:25:45 +08:00
|
|
|
bool IsLiteralFalse = (FormatTok->Tok.isLiteral() &&
|
|
|
|
StringRef(FormatTok->Tok.getLiteralData(),
|
|
|
|
FormatTok->Tok.getLength()) == "0") ||
|
|
|
|
FormatTok->Tok.is(tok::kw_false);
|
|
|
|
if ((!IfDef && IsLiteralFalse) || PPLevelBranchIndex[PPBranchLevel] > 0) {
|
2013-05-25 02:24:24 +08:00
|
|
|
PPStack.push_back(PP_Unreachable);
|
|
|
|
} else {
|
|
|
|
pushPPConditional();
|
|
|
|
}
|
|
|
|
parsePPUnknown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parsePPElse() {
|
|
|
|
if (!PPStack.empty())
|
|
|
|
PPStack.pop_back();
|
2013-10-12 05:25:45 +08:00
|
|
|
assert(PPBranchLevel < (int)PPLevelBranchIndex.size());
|
|
|
|
if (!PPChainBranchIndex.empty())
|
|
|
|
++PPChainBranchIndex.top();
|
|
|
|
if (PPBranchLevel >= 0 && !PPChainBranchIndex.empty() &&
|
|
|
|
PPLevelBranchIndex[PPBranchLevel] != PPChainBranchIndex.top()) {
|
|
|
|
PPStack.push_back(PP_Unreachable);
|
|
|
|
} else {
|
|
|
|
pushPPConditional();
|
|
|
|
}
|
2013-05-25 02:24:24 +08:00
|
|
|
parsePPUnknown();
|
|
|
|
}
|
|
|
|
|
2013-05-31 22:56:29 +08:00
|
|
|
void UnwrappedLineParser::parsePPElIf() { parsePPElse(); }
|
2013-05-25 02:24:24 +08:00
|
|
|
|
|
|
|
void UnwrappedLineParser::parsePPEndIf() {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--PPBranchLevel;
|
|
|
|
if (!PPChainBranchIndex.empty())
|
|
|
|
PPChainBranchIndex.pop();
|
2013-05-25 02:24:24 +08:00
|
|
|
if (!PPStack.empty())
|
|
|
|
PPStack.pop_back();
|
|
|
|
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.
|
|
|
|
bool tokenCanStartNewLine(clang::Token Tok) {
|
|
|
|
// 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.
|
|
|
|
Tok.isNot(tok::colon);
|
|
|
|
}
|
|
|
|
|
2013-01-07 22:56:16 +08:00
|
|
|
void UnwrappedLineParser::parseStructuralElement() {
|
2013-05-28 19:55:06 +08:00
|
|
|
assert(!FormatTok->Tok.is(tok::l_brace));
|
|
|
|
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;
|
2013-01-08 03:05:19 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
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:
|
2012-12-04 02:12:45 +08:00
|
|
|
parseAccessSpecifier();
|
|
|
|
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;
|
2013-01-21 18:07:49 +08:00
|
|
|
case tok::kw_return:
|
|
|
|
parseReturn();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 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 {
|
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:
|
|
|
|
parseEnum();
|
2013-01-22 03:17:52 +08:00
|
|
|
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:
|
2013-01-15 21:38:33 +08:00
|
|
|
parseRecord();
|
|
|
|
// A record declaration or definition is always the start of a structural
|
|
|
|
// element.
|
|
|
|
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;
|
2013-09-04 21:25:30 +08:00
|
|
|
case tok::caret:
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok->is(tok::l_brace)) {
|
|
|
|
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).
|
2013-12-12 17:49:52 +08:00
|
|
|
if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
|
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;
|
2013-05-29 21:16:10 +08:00
|
|
|
case tok::identifier: {
|
|
|
|
StringRef Text = FormatTok->TokenText;
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
2013-04-09 06:16:06 +08:00
|
|
|
if (Line->Tokens.size() == 1) {
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::colon)) {
|
2013-04-09 06:16:06 +08:00
|
|
|
parseLabel();
|
|
|
|
return;
|
|
|
|
}
|
2013-04-10 00:15:19 +08:00
|
|
|
// Recognize function-like macro usages without trailing semicolon.
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_paren)) {
|
2013-04-09 06:16:06 +08:00
|
|
|
parseParens();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->HasUnescapedNewline &&
|
|
|
|
tokenCanStartNewLine(FormatTok->Tok)) {
|
2013-04-09 06:16:06 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
}
|
2013-05-29 21:16:10 +08:00
|
|
|
} else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 &&
|
|
|
|
Text == Text.upper()) {
|
|
|
|
// Recognize free-standing macros like Q_OBJECT.
|
|
|
|
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:
|
|
|
|
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:
|
|
|
|
tryToParseLambda();
|
|
|
|
break;
|
2013-01-10 19:52:21 +08:00
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
2013-09-03 23:10:01 +08:00
|
|
|
void UnwrappedLineParser::tryToParseLambda() {
|
2013-09-05 19:49:39 +08:00
|
|
|
// FIXME: This is a dirty way to access the previous token. Find a better
|
|
|
|
// solution.
|
2013-09-07 05:25:51 +08:00
|
|
|
if (!Line->Tokens.empty() &&
|
|
|
|
Line->Tokens.back().Tok->isOneOf(tok::identifier, tok::kw_operator)) {
|
2013-09-05 19:49:39 +08:00
|
|
|
nextToken();
|
|
|
|
return;
|
|
|
|
}
|
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-09-03 23:10:01 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
while (FormatTok->isNot(tok::l_brace)) {
|
|
|
|
switch (FormatTok->Tok.getKind()) {
|
2013-09-05 18:04:31 +08:00
|
|
|
case tok::l_brace:
|
|
|
|
break;
|
|
|
|
case tok::l_paren:
|
|
|
|
parseParens();
|
|
|
|
break;
|
|
|
|
case tok::identifier:
|
|
|
|
case tok::kw_mutable:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
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-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();
|
|
|
|
if (FormatTok->is(tok::comma)) {
|
|
|
|
nextToken();
|
|
|
|
} else if (FormatTok->is(tok::r_square)) {
|
|
|
|
nextToken();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-23 17:41:43 +08:00
|
|
|
bool UnwrappedLineParser::tryToParseBracedList() {
|
2013-07-09 17:06:29 +08:00
|
|
|
if (FormatTok->BlockKind == BK_Unknown)
|
2013-05-23 17:41:43 +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 {
|
2013-04-10 17:52:05 +08:00
|
|
|
// FIXME: When we start to support lambdas, we'll want to parse them away
|
|
|
|
// here, otherwise our bail-out scenarios below break. The better solution
|
|
|
|
// might be to just implement a more or less complete expression parser.
|
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;
|
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:
|
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
|
|
|
}
|
|
|
|
|
2013-01-21 18:07:49 +08:00
|
|
|
void UnwrappedLineParser::parseReturn() {
|
|
|
|
nextToken();
|
|
|
|
|
|
|
|
do {
|
2013-05-28 19:55:06 +08:00
|
|
|
switch (FormatTok->Tok.getKind()) {
|
2013-01-21 18:07:49 +08:00
|
|
|
case tok::l_brace:
|
|
|
|
parseBracedList();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.isNot(tok::semi)) {
|
2013-04-10 17:52:05 +08:00
|
|
|
// Assume missing ';'.
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
}
|
2013-01-21 18:07:49 +08:00
|
|
|
break;
|
|
|
|
case tok::l_paren:
|
|
|
|
parseParens();
|
|
|
|
break;
|
|
|
|
case tok::r_brace:
|
|
|
|
// Assume missing ';'.
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
case tok::semi:
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
2013-09-03 23:10:01 +08:00
|
|
|
case tok::l_square:
|
|
|
|
tryToParseLambda();
|
|
|
|
break;
|
2013-01-21 18:07:49 +08:00
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
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;
|
2013-02-10 12:38:23 +08:00
|
|
|
case tok::l_brace: {
|
2013-05-23 17:41:43 +08:00
|
|
|
if (!tryToParseBracedList()) {
|
2013-09-04 21:34:14 +08:00
|
|
|
parseChildBlock();
|
2013-05-23 17:41:43 +08:00
|
|
|
}
|
2013-01-10 19:52:21 +08:00
|
|
|
break;
|
2013-02-10 12:38:23 +08:00
|
|
|
}
|
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 02:12:45 +08:00
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-12-12 17:49:52 +08:00
|
|
|
if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
|
|
|
|
Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
|
2013-08-03 05:31:59 +08:00
|
|
|
addUnwrappedLine();
|
2013-12-12 17:49:52 +08:00
|
|
|
} else {
|
2013-08-03 05:31:59 +08:00
|
|
|
NeedsUnwrappedLine = true;
|
2013-12-12 17:49:52 +08:00
|
|
|
}
|
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();
|
2013-01-09 23:25:02 +08:00
|
|
|
--Line->Level;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
} else if (NeedsUnwrappedLine) {
|
|
|
|
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");
|
2012-12-07 02:03:27 +08:00
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::identifier))
|
2012-12-07 02:03:27 +08:00
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2013-08-03 05:31:59 +08:00
|
|
|
if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
|
2013-12-12 17:49:52 +08:00
|
|
|
Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
|
|
|
|
Style.BreakBeforeBraces == FormatStyle::BS_GNU)
|
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.
|
|
|
|
}
|
|
|
|
|
2012-12-05 23:06:06 +08:00
|
|
|
void UnwrappedLineParser::parseForOrWhileLoop() {
|
2013-05-28 19:55:06 +08:00
|
|
|
assert((FormatTok->Tok.is(tok::kw_for) || FormatTok->Tok.is(tok::kw_while)) &&
|
2012-12-05 23:06:06 +08:00
|
|
|
"'for' or 'while' expected");
|
|
|
|
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);
|
2013-12-12 17:49:52 +08:00
|
|
|
if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
|
|
|
|
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)) {
|
2013-12-12 17:49:52 +08:00
|
|
|
// "break;" after "}" on its own line only for BS_Allman and BS_GNU
|
|
|
|
if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
|
|
|
|
Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
|
2013-08-03 05:31:59 +08:00
|
|
|
addUnwrappedLine();
|
2013-12-12 17:49:52 +08:00
|
|
|
}
|
2013-08-03 05:31:59 +08:00
|
|
|
parseStructuralElement();
|
|
|
|
}
|
2013-12-12 17:49:52 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
} else {
|
|
|
|
addUnwrappedLine();
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
2013-01-09 23:25:02 +08:00
|
|
|
Line->Level = OldLineLevel;
|
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.
|
2013-11-29 16:51:56 +08:00
|
|
|
if (FormatTok->is(tok::identifier) &&
|
|
|
|
(FormatTok->TokenText == "slots" || FormatTok->TokenText == "Q_SLOTS"))
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseEnum() {
|
2013-01-22 03:17:52 +08:00
|
|
|
nextToken();
|
2013-08-20 20:42:50 +08:00
|
|
|
// Eat up enum class ...
|
|
|
|
if (FormatTok->Tok.is(tok::kw_class) ||
|
|
|
|
FormatTok->Tok.is(tok::kw_struct))
|
|
|
|
nextToken();
|
2013-09-07 05:32:35 +08:00
|
|
|
while (FormatTok->Tok.getIdentifierInfo() ||
|
|
|
|
FormatTok->isOneOf(tok::colon, tok::coloncolon)) {
|
2013-01-22 03:17:52 +08:00
|
|
|
nextToken();
|
|
|
|
// We can have macros or attributes in between 'enum' and the enum name.
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_paren)) {
|
2012-12-04 22:46:19 +08:00
|
|
|
parseParens();
|
2013-01-22 03:17:52 +08:00
|
|
|
}
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::identifier))
|
2012-12-04 22:46:19 +08:00
|
|
|
nextToken();
|
2013-01-22 03:17:52 +08:00
|
|
|
}
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace)) {
|
2013-09-13 17:20:45 +08:00
|
|
|
FormatTok->BlockKind = BK_Block;
|
|
|
|
bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
|
|
|
|
if (HasError) {
|
|
|
|
if (FormatTok->is(tok::semi))
|
2013-01-22 03:17:52 +08:00
|
|
|
nextToken();
|
2013-09-13 17:20:45 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
2013-01-22 03:17:52 +08:00
|
|
|
}
|
|
|
|
// We fall through to parsing a structural element afterwards, so that in
|
|
|
|
// enum A {} n, m;
|
|
|
|
// "} n, m;" will end up in one unwrapped line.
|
2013-01-08 02:10:23 +08:00
|
|
|
}
|
|
|
|
|
2013-01-15 21:38:33 +08:00
|
|
|
void UnwrappedLineParser::parseRecord() {
|
2013-01-08 02:10:23 +08:00
|
|
|
nextToken();
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::identifier) ||
|
|
|
|
FormatTok->Tok.is(tok::kw___attribute) ||
|
2013-10-07 17:15:41 +08:00
|
|
|
FormatTok->Tok.is(tok::kw___declspec) ||
|
|
|
|
FormatTok->Tok.is(tok::kw_alignas)) {
|
2013-01-15 21:38:33 +08:00
|
|
|
nextToken();
|
|
|
|
// We can have macros or attributes in between 'class' and the class name.
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_paren)) {
|
2013-01-15 21:38:33 +08:00
|
|
|
parseParens();
|
|
|
|
}
|
2013-02-06 23:57:54 +08:00
|
|
|
// The actual identifier can be a nested name specifier, and in macros
|
|
|
|
// it is often token-pasted.
|
2013-05-28 19:55:06 +08:00
|
|
|
while (FormatTok->Tok.is(tok::identifier) ||
|
|
|
|
FormatTok->Tok.is(tok::coloncolon) ||
|
|
|
|
FormatTok->Tok.is(tok::hashhash))
|
2013-01-12 02:13:04 +08:00
|
|
|
nextToken();
|
2013-01-15 21:38:33 +08:00
|
|
|
|
2013-01-21 21:58:54 +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).
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::colon) || FormatTok->Tok.is(tok::less)) {
|
|
|
|
while (!eof() && FormatTok->Tok.isNot(tok::l_brace)) {
|
|
|
|
if (FormatTok->Tok.is(tok::semi))
|
2013-01-15 21:38:33 +08:00
|
|
|
return;
|
|
|
|
nextToken();
|
2013-01-12 02:13:04 +08:00
|
|
|
}
|
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)) {
|
2013-08-03 05:31:59 +08:00
|
|
|
if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
|
2013-12-12 17:49:52 +08:00
|
|
|
Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
|
|
|
|
Style.BreakBeforeBraces == FormatStyle::BS_GNU)
|
2013-05-13 20:51:40 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
|
2013-10-13 06:46:56 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/true, /*Addlevel=*/true,
|
|
|
|
/*MunchSemi=*/false);
|
2013-05-13 20:51:40 +08:00
|
|
|
}
|
2013-01-21 21:58:54 +08:00
|
|
|
// We fall through to parsing a structural element afterwards, so
|
|
|
|
// class A {} 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();
|
|
|
|
} 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
|
|
|
|
|
|
|
// If instance variables are present, keep the '{' on the first line too.
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->Tok.is(tok::l_brace))
|
2013-06-26 08:30:14 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/true);
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|
2013-01-18 22:04:34 +08:00
|
|
|
CurrentLines->push_back(*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()) {
|
2013-09-05 17:29:45 +08:00
|
|
|
for (SmallVectorImpl<UnwrappedLine>::iterator
|
2013-03-02 02:11:39 +08:00
|
|
|
I = PreprocessorDirectives.begin(),
|
|
|
|
E = PreprocessorDirectives.end();
|
2013-01-18 22:04:34 +08:00
|
|
|
I != E; ++I) {
|
|
|
|
CurrentLines->push_back(*I);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
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) {
|
2013-05-28 19:55:06 +08:00
|
|
|
if ((*I)->NewlinesBefore && JustComments) {
|
2013-01-23 00:31:55 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
|
|
|
pushToken(*I);
|
|
|
|
}
|
|
|
|
if (NewlineBeforeNext && JustComments) {
|
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
|
|
|
CommentsBeforeNextToken.clear();
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
void UnwrappedLineParser::nextToken() {
|
|
|
|
if (eof())
|
|
|
|
return;
|
2013-05-28 19:55:06 +08:00
|
|
|
flushComments(FormatTok->NewlinesBefore > 0);
|
2013-01-23 00:31:55 +08:00
|
|
|
pushToken(FormatTok);
|
2013-01-05 07:34:14 +08:00
|
|
|
readToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::readToken() {
|
2013-01-23 00:31:55 +08:00
|
|
|
bool CommentsInCurrentLine = true;
|
|
|
|
do {
|
|
|
|
FormatTok = Tokens->getNextToken();
|
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.
|
2013-03-20 20:37:50 +08:00
|
|
|
bool SwitchToPreprocessorLines =
|
|
|
|
!Line->Tokens.empty() && CurrentLines == &Lines;
|
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.
|
2013-05-28 19:55:06 +08:00
|
|
|
flushComments(FormatTok->NewlinesBefore > 0);
|
2013-01-23 00:31:55 +08:00
|
|
|
parsePPDirective();
|
|
|
|
}
|
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;
|
2013-05-28 19:55:06 +08:00
|
|
|
if (FormatTok->NewlinesBefore > 0 || 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
|