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-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
|
|
|
}
|
|
|
|
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-04-12 22:13:36 +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),
|
|
|
|
PreviousStructuralError(StructuralError) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
return createEOF();
|
|
|
|
return Token;
|
|
|
|
}
|
|
|
|
|
2013-05-23 17:41:43 +08:00
|
|
|
virtual unsigned getPosition() {
|
|
|
|
return PreviousTokenSource->getPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual FormatToken setPosition(unsigned Position) {
|
|
|
|
Token = PreviousTokenSource->setPosition(Position);
|
|
|
|
return Token;
|
|
|
|
}
|
|
|
|
|
2013-01-05 07:34:14 +08:00
|
|
|
private:
|
2013-04-09 06:16:06 +08:00
|
|
|
bool eof() { return Token.HasUnescapedNewline; }
|
2013-01-05 07:34:14 +08:00
|
|
|
|
|
|
|
FormatToken createEOF() {
|
|
|
|
FormatToken FormatTok;
|
|
|
|
FormatTok.Tok.startToken();
|
|
|
|
FormatTok.Tok.setKind(tok::eof);
|
|
|
|
return FormatTok;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnwrappedLine &Line;
|
|
|
|
FormatTokenSource *&TokenSource;
|
|
|
|
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
|
|
|
|
|
|
|
FormatToken Token;
|
|
|
|
};
|
|
|
|
|
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)
|
|
|
|
: Parser(Parser), SwitchToPreprocessorLines(SwitchToPreprocessorLines) {
|
|
|
|
if (SwitchToPreprocessorLines)
|
|
|
|
Parser.CurrentLines = &Parser.PreprocessorDirectives;
|
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);
|
|
|
|
Parser.MustBreakBeforeNextToken = true;
|
2013-01-18 22:04:34 +08:00
|
|
|
if (SwitchToPreprocessorLines)
|
|
|
|
Parser.CurrentLines = &Parser.Lines;
|
2013-01-10 19:52:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
UnwrappedLineParser &Parser;
|
2013-01-18 22:04:34 +08:00
|
|
|
const bool SwitchToPreprocessorLines;
|
2013-01-10 19:52:21 +08:00
|
|
|
|
|
|
|
UnwrappedLine *PreBlockLine;
|
|
|
|
};
|
|
|
|
|
2013-05-23 17:41:43 +08:00
|
|
|
class IndexedTokenSource : public FormatTokenSource {
|
|
|
|
public:
|
|
|
|
IndexedTokenSource(ArrayRef<FormatToken> Tokens)
|
|
|
|
: Tokens(Tokens), Position(-1) {}
|
|
|
|
|
|
|
|
virtual FormatToken getNextToken() {
|
|
|
|
++Position;
|
|
|
|
return Tokens[Position];
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual unsigned getPosition() {
|
|
|
|
assert(Position >= 0);
|
|
|
|
return Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual FormatToken setPosition(unsigned P) {
|
|
|
|
Position = P;
|
|
|
|
return Tokens[Position];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ArrayRef<FormatToken> Tokens;
|
|
|
|
int Position;
|
|
|
|
};
|
|
|
|
|
2013-05-15 16:14:19 +08:00
|
|
|
UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
|
|
|
|
FormatTokenSource &Tokens,
|
|
|
|
UnwrappedLineConsumer &Callback)
|
2013-01-18 22:04:34 +08:00
|
|
|
: Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
|
2013-05-15 16:14:19 +08:00
|
|
|
CurrentLines(&Lines), StructuralError(false), Style(Style),
|
2013-05-23 17:41:43 +08:00
|
|
|
Tokens(NULL), Callback(Callback) {
|
|
|
|
FormatToken Tok;
|
|
|
|
do {
|
|
|
|
Tok = Tokens.getNextToken();
|
|
|
|
AllTokens.push_back(Tok);
|
|
|
|
} while (Tok.Tok.isNot(tok::eof));
|
|
|
|
LBraces.resize(AllTokens.size(), BS_Unknown);
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
|
2012-12-05 01:27:50 +08:00
|
|
|
bool UnwrappedLineParser::parse() {
|
2013-01-16 20:31:12 +08:00
|
|
|
DEBUG(llvm::dbgs() << "----\n");
|
2013-05-23 17:41:43 +08:00
|
|
|
IndexedTokenSource TokenSource(AllTokens);
|
|
|
|
Tokens = &TokenSource;
|
2013-01-05 07:34:14 +08:00
|
|
|
readToken();
|
2013-04-12 22:13:36 +08:00
|
|
|
parseFile();
|
2013-03-20 20:37:50 +08:00
|
|
|
for (std::vector<UnwrappedLine>::iterator I = Lines.begin(), E = Lines.end();
|
2013-01-18 22:04:34 +08:00
|
|
|
I != E; ++I) {
|
|
|
|
Callback.consumeUnwrappedLine(*I);
|
|
|
|
}
|
2013-03-02 02:11:39 +08:00
|
|
|
|
|
|
|
// Create line with eof token.
|
|
|
|
pushToken(FormatTok);
|
|
|
|
Callback.consumeUnwrappedLine(*Line);
|
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-04-12 22:13:36 +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) {
|
2012-12-04 02:12:45 +08:00
|
|
|
do {
|
|
|
|
switch (FormatTok.Tok.getKind()) {
|
|
|
|
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-04-12 22:13:36 +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;
|
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;
|
|
|
|
FormatToken Tok = FormatTok;
|
|
|
|
// 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.
|
|
|
|
SmallVector<unsigned, 8> LBraceStack;
|
|
|
|
assert(Tok.Tok.is(tok::l_brace));
|
|
|
|
do {
|
|
|
|
FormatToken NextTok = Tokens->getNextToken();
|
|
|
|
switch (Tok.Tok.getKind()) {
|
|
|
|
case tok::l_brace:
|
|
|
|
LBraceStack.push_back(Position);
|
|
|
|
break;
|
|
|
|
case tok::r_brace:
|
|
|
|
if (!LBraceStack.empty()) {
|
|
|
|
if (LBraces[LBraceStack.back()] == BS_Unknown) {
|
|
|
|
// If there is a comma, semicolon or right paren after the closing
|
|
|
|
// brace, we assume this is a braced initializer list.
|
|
|
|
|
|
|
|
// FIXME: Note that this currently works only because we do not
|
|
|
|
// use the brace information while inside a braced init list.
|
|
|
|
// Thus, if the parent is a braced init list, we consider all
|
|
|
|
// brace blocks inside it braced init list. That works good enough
|
|
|
|
// for now, but we will need to fix it to correctly handle lambdas.
|
|
|
|
if (NextTok.Tok.is(tok::comma) || NextTok.Tok.is(tok::semi) ||
|
|
|
|
NextTok.Tok.is(tok::r_paren))
|
|
|
|
LBraces[LBraceStack.back()] = BS_BracedInit;
|
|
|
|
else
|
|
|
|
LBraces[LBraceStack.back()] = BS_Block;
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
if (!LBraceStack.empty())
|
|
|
|
LBraces[LBraceStack.back()] = BS_Block;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Tok = NextTok;
|
|
|
|
++Position;
|
|
|
|
} while (Tok.Tok.isNot(tok::eof));
|
|
|
|
// Assume other blocks for all unclosed opening braces.
|
|
|
|
for (unsigned i = 0, e = LBraceStack.size(); i != e; ++i) {
|
|
|
|
if (LBraces[LBraceStack[i]] == BS_Unknown)
|
|
|
|
LBraces[LBraceStack[i]] = BS_Block;
|
|
|
|
}
|
|
|
|
FormatTok = Tokens->setPosition(StoredPosition);
|
|
|
|
}
|
|
|
|
|
2013-04-12 22:13:36 +08:00
|
|
|
void UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
|
2013-02-11 04:35:35 +08:00
|
|
|
unsigned AddLevels) {
|
2012-12-07 01:49:17 +08:00
|
|
|
assert(FormatTok.Tok.is(tok::l_brace) && "'{' expected");
|
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-01-22 00:42:44 +08:00
|
|
|
Line->Level += AddLevels;
|
2013-03-20 20:37:50 +08:00
|
|
|
parseLevel(/*HasOpeningBrace=*/ true);
|
2012-12-07 02:03:27 +08:00
|
|
|
|
2013-01-23 00:31:55 +08:00
|
|
|
if (!FormatTok.Tok.is(tok::r_brace)) {
|
|
|
|
Line->Level -= AddLevels;
|
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-01-23 00:31:55 +08:00
|
|
|
Line->Level -= AddLevels;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parsePPDirective() {
|
2013-01-03 00:30:12 +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();
|
|
|
|
|
|
|
|
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-01-05 07:34:14 +08:00
|
|
|
switch (FormatTok.Tok.getIdentifierInfo()->getPPKeywordID()) {
|
|
|
|
case tok::pp_define:
|
|
|
|
parsePPDefine();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
parsePPUnknown();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parsePPDefine() {
|
|
|
|
nextToken();
|
|
|
|
|
|
|
|
if (FormatTok.Tok.getKind() != tok::identifier) {
|
|
|
|
parsePPUnknown();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nextToken();
|
2013-01-23 22:37:36 +08:00
|
|
|
if (FormatTok.Tok.getKind() == tok::l_paren &&
|
|
|
|
FormatTok.WhiteSpaceLength == 0) {
|
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-01-10 19:52:21 +08:00
|
|
|
assert(!FormatTok.Tok.is(tok::l_brace));
|
2012-12-04 22:46:19 +08:00
|
|
|
switch (FormatTok.Tok.getKind()) {
|
2013-01-08 03:05:19 +08:00
|
|
|
case tok::at:
|
|
|
|
nextToken();
|
2013-02-11 04:35:35 +08:00
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
|
|
|
parseBracedList();
|
|
|
|
break;
|
|
|
|
}
|
2013-01-08 03:05:19 +08:00
|
|
|
switch (FormatTok.Tok.getObjCKeywordID()) {
|
|
|
|
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();
|
|
|
|
if (FormatTok.Tok.is(tok::kw_namespace)) {
|
|
|
|
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();
|
|
|
|
if (FormatTok.Tok.is(tok::string_literal)) {
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
2013-01-23 17:32:48 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/ true, 0);
|
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 {
|
|
|
|
switch (FormatTok.Tok.getKind()) {
|
2013-02-11 04:35:35 +08:00
|
|
|
case tok::at:
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace))
|
|
|
|
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;
|
|
|
|
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).
|
|
|
|
if (Style.BreakBeforeBraces == FormatStyle::BS_Linux ||
|
|
|
|
Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
|
|
|
|
addUnwrappedLine();
|
|
|
|
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;
|
2012-12-04 22:46:19 +08:00
|
|
|
case tok::identifier:
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
2013-04-09 06:16:06 +08:00
|
|
|
if (Line->Tokens.size() == 1) {
|
|
|
|
if (FormatTok.Tok.is(tok::colon)) {
|
|
|
|
parseLabel();
|
|
|
|
return;
|
|
|
|
}
|
2013-04-10 00:15:19 +08:00
|
|
|
// Recognize function-like macro usages without trailing semicolon.
|
2013-04-09 06:16:06 +08:00
|
|
|
if (FormatTok.Tok.is(tok::l_paren)) {
|
|
|
|
parseParens();
|
2013-04-10 00:15:19 +08:00
|
|
|
if (FormatTok.HasUnescapedNewline &&
|
|
|
|
tokenCanStartNewLine(FormatTok.Tok)) {
|
2013-04-09 06:16:06 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-12-17 19:29:41 +08:00
|
|
|
case tok::equal:
|
|
|
|
nextToken();
|
2013-01-10 19:52:21 +08:00
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
|
|
|
parseBracedList();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
2013-05-23 17:41:43 +08:00
|
|
|
bool UnwrappedLineParser::tryToParseBracedList() {
|
|
|
|
if (LBraces[Tokens->getPosition()] == BS_Unknown)
|
|
|
|
calculateBraceTypes();
|
|
|
|
assert(LBraces[Tokens->getPosition()] != BS_Unknown);
|
|
|
|
if (LBraces[Tokens->getPosition()] == BS_Block)
|
|
|
|
return false;
|
|
|
|
parseBracedList();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-10 19:52:21 +08:00
|
|
|
void UnwrappedLineParser::parseBracedList() {
|
|
|
|
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.
|
|
|
|
bool StartOfExpression = true;
|
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-01-10 19:52:21 +08:00
|
|
|
switch (FormatTok.Tok.getKind()) {
|
|
|
|
case tok::l_brace:
|
2013-04-10 17:52:05 +08:00
|
|
|
if (!StartOfExpression) {
|
|
|
|
// Probably a missing closing brace. Bail out.
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
}
|
2013-01-10 19:52:21 +08:00
|
|
|
parseBracedList();
|
2013-04-10 17:52:05 +08:00
|
|
|
StartOfExpression = false;
|
2012-12-17 19:29:41 +08:00
|
|
|
break;
|
2013-01-10 19:52:21 +08:00
|
|
|
case tok::r_brace:
|
|
|
|
nextToken();
|
|
|
|
return;
|
2013-04-10 17:52:05 +08:00
|
|
|
case tok::semi:
|
|
|
|
// Probably a missing closing brace. Bail out.
|
|
|
|
return;
|
|
|
|
case tok::comma:
|
|
|
|
nextToken();
|
|
|
|
StartOfExpression = true;
|
|
|
|
break;
|
2012-12-04 22:46:19 +08:00
|
|
|
default:
|
|
|
|
nextToken();
|
2013-04-10 17:52:05 +08:00
|
|
|
StartOfExpression = false;
|
2012-12-04 22:46:19 +08:00
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
2013-01-21 18:07:49 +08:00
|
|
|
void UnwrappedLineParser::parseReturn() {
|
|
|
|
nextToken();
|
|
|
|
|
|
|
|
do {
|
|
|
|
switch (FormatTok.Tok.getKind()) {
|
|
|
|
case tok::l_brace:
|
|
|
|
parseBracedList();
|
2013-04-10 17:52:05 +08:00
|
|
|
if (FormatTok.Tok.isNot(tok::semi)) {
|
|
|
|
// 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;
|
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
2012-12-04 02:12:45 +08:00
|
|
|
void UnwrappedLineParser::parseParens() {
|
|
|
|
assert(FormatTok.Tok.is(tok::l_paren) && "'(' expected.");
|
|
|
|
nextToken();
|
|
|
|
do {
|
|
|
|
switch (FormatTok.Tok.getKind()) {
|
|
|
|
case tok::l_paren:
|
|
|
|
parseParens();
|
|
|
|
break;
|
|
|
|
case tok::r_paren:
|
|
|
|
nextToken();
|
|
|
|
return;
|
2013-02-10 12:38:23 +08:00
|
|
|
case tok::l_brace: {
|
2013-05-23 17:41:43 +08:00
|
|
|
if (!tryToParseBracedList()) {
|
|
|
|
nextToken();
|
|
|
|
ScopedLineState LineState(*this);
|
|
|
|
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
|
|
|
|
/*MustBeDeclaration=*/ false);
|
|
|
|
Line->Level += 1;
|
|
|
|
parseLevel(/*HasOpeningBrace=*/ true);
|
|
|
|
Line->Level -= 1;
|
|
|
|
}
|
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();
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace))
|
|
|
|
parseBracedList();
|
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseIfThenElse() {
|
|
|
|
assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected");
|
|
|
|
nextToken();
|
2013-01-12 02:28:36 +08:00
|
|
|
if (FormatTok.Tok.is(tok::l_paren))
|
|
|
|
parseParens();
|
2012-12-04 02:12:45 +08:00
|
|
|
bool NeedsUnwrappedLine = false;
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
2013-01-23 17:32:48 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/ false);
|
2012-12-04 02:12:45 +08:00
|
|
|
NeedsUnwrappedLine = true;
|
|
|
|
} 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
|
|
|
}
|
|
|
|
if (FormatTok.Tok.is(tok::kw_else)) {
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
2013-01-23 17:32:48 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/ false);
|
2012-12-04 02:12:45 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
} else if (FormatTok.Tok.is(tok::kw_if)) {
|
|
|
|
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() {
|
|
|
|
assert(FormatTok.Tok.is(tok::kw_namespace) && "'namespace' expected");
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok.Tok.is(tok::identifier))
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
2013-05-13 20:51:40 +08:00
|
|
|
if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
|
|
|
|
addUnwrappedLine();
|
|
|
|
|
2013-01-23 17:32:48 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/ true, 0);
|
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.
|
|
|
|
if (FormatTok.Tok.is(tok::semi))
|
|
|
|
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() {
|
|
|
|
assert((FormatTok.Tok.is(tok::kw_for) || FormatTok.Tok.is(tok::kw_while)) &&
|
|
|
|
"'for' or 'while' expected");
|
|
|
|
nextToken();
|
2013-01-12 03:23:05 +08:00
|
|
|
if (FormatTok.Tok.is(tok::l_paren))
|
|
|
|
parseParens();
|
2012-12-05 23:06:06 +08:00
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
2013-01-23 17:32:48 +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() {
|
|
|
|
assert(FormatTok.Tok.is(tok::kw_do) && "'do' expected");
|
|
|
|
nextToken();
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
2013-01-23 17:32:48 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/ false);
|
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.
|
|
|
|
if (!FormatTok.Tok.is(tok::kw_while)) {
|
|
|
|
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() {
|
2013-02-13 04:17:17 +08:00
|
|
|
if (FormatTok.Tok.isNot(tok::colon))
|
|
|
|
return;
|
2012-12-04 02:12:45 +08:00
|
|
|
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-03-20 02:33:58 +08:00
|
|
|
if (CommentsBeforeNextToken.empty() && FormatTok.Tok.is(tok::l_brace)) {
|
2013-01-23 17:32:48 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/ false);
|
2013-01-18 13:50:57 +08:00
|
|
|
if (FormatTok.Tok.is(tok::kw_break))
|
|
|
|
parseStructuralElement(); // "break;" after "}" goes on the same line.
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
addUnwrappedLine();
|
2013-01-09 23:25:02 +08:00
|
|
|
Line->Level = OldLineLevel;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseCaseLabel() {
|
|
|
|
assert(FormatTok.Tok.is(tok::kw_case) && "'case' expected");
|
|
|
|
// FIXME: fix handling of complex expressions here.
|
|
|
|
do {
|
|
|
|
nextToken();
|
|
|
|
} while (!eof() && !FormatTok.Tok.is(tok::colon));
|
|
|
|
parseLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseSwitch() {
|
|
|
|
assert(FormatTok.Tok.is(tok::kw_switch) && "'switch' expected");
|
|
|
|
nextToken();
|
2013-01-12 03:23:05 +08:00
|
|
|
if (FormatTok.Tok.is(tok::l_paren))
|
|
|
|
parseParens();
|
2012-12-04 02:12:45 +08:00
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
2013-01-23 17:32:48 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/ false, Style.IndentCaseLabels ? 2 : 1);
|
2012-12-04 02:12:45 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
} else {
|
|
|
|
addUnwrappedLine();
|
2013-01-09 23:25:02 +08:00
|
|
|
Line->Level += (Style.IndentCaseLabels ? 2 : 1);
|
2013-01-07 22:56:16 +08:00
|
|
|
parseStructuralElement();
|
2013-01-09 23:25:02 +08:00
|
|
|
Line->Level -= (Style.IndentCaseLabels ? 2 : 1);
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseAccessSpecifier() {
|
|
|
|
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.
|
|
|
|
if (FormatTok.Tok.is(tok::colon))
|
|
|
|
nextToken();
|
2012-12-04 02:12:45 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseEnum() {
|
2013-01-22 03:17:52 +08:00
|
|
|
nextToken();
|
|
|
|
if (FormatTok.Tok.is(tok::identifier) ||
|
|
|
|
FormatTok.Tok.is(tok::kw___attribute) ||
|
|
|
|
FormatTok.Tok.is(tok::kw___declspec)) {
|
|
|
|
nextToken();
|
|
|
|
// We can have macros or attributes in between 'enum' and the enum name.
|
|
|
|
if (FormatTok.Tok.is(tok::l_paren)) {
|
2012-12-04 22:46:19 +08:00
|
|
|
parseParens();
|
2013-01-22 03:17:52 +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
|
|
|
}
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
++Line->Level;
|
|
|
|
do {
|
|
|
|
switch (FormatTok.Tok.getKind()) {
|
|
|
|
case tok::l_paren:
|
|
|
|
parseParens();
|
|
|
|
break;
|
|
|
|
case tok::r_brace:
|
2012-12-04 22:46:19 +08:00
|
|
|
addUnwrappedLine();
|
2013-01-22 03:17:52 +08:00
|
|
|
nextToken();
|
|
|
|
--Line->Level;
|
|
|
|
return;
|
|
|
|
case tok::comma:
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
// 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-01-15 21:38:33 +08:00
|
|
|
if (FormatTok.Tok.is(tok::identifier) ||
|
|
|
|
FormatTok.Tok.is(tok::kw___attribute) ||
|
|
|
|
FormatTok.Tok.is(tok::kw___declspec)) {
|
|
|
|
nextToken();
|
|
|
|
// We can have macros or attributes in between 'class' and the class name.
|
|
|
|
if (FormatTok.Tok.is(tok::l_paren)) {
|
|
|
|
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-01-21 18:17:14 +08:00
|
|
|
while (FormatTok.Tok.is(tok::identifier) ||
|
2013-03-20 20:37:50 +08:00
|
|
|
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).
|
|
|
|
if (FormatTok.Tok.is(tok::colon) || FormatTok.Tok.is(tok::less)) {
|
2013-03-20 23:12:38 +08:00
|
|
|
while (!eof() && FormatTok.Tok.isNot(tok::l_brace)) {
|
2013-01-15 21:38:33 +08:00
|
|
|
if (FormatTok.Tok.is(tok::semi))
|
|
|
|
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-13 20:51:40 +08:00
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
|
|
|
if (Style.BreakBeforeBraces == FormatStyle::BS_Linux)
|
|
|
|
addUnwrappedLine();
|
|
|
|
|
2013-01-23 17:32:48 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/ true);
|
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() {
|
|
|
|
assert(FormatTok.Tok.is(tok::less) && "'<' expected.");
|
|
|
|
do
|
|
|
|
nextToken();
|
|
|
|
while (!eof() && FormatTok.Tok.isNot(tok::greater));
|
|
|
|
nextToken(); // Skip '>'.
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseObjCUntilAtEnd() {
|
|
|
|
do {
|
|
|
|
if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) {
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
parseStructuralElement();
|
|
|
|
} 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.
|
|
|
|
if (FormatTok.Tok.is(tok::colon)) {
|
|
|
|
nextToken();
|
2013-03-20 20:37:50 +08:00
|
|
|
nextToken(); // base class name
|
2013-01-10 04:25:35 +08:00
|
|
|
} else if (FormatTok.Tok.is(tok::l_paren))
|
|
|
|
// Skip category, if present.
|
|
|
|
parseParens();
|
|
|
|
|
2013-01-10 05:15:03 +08:00
|
|
|
if (FormatTok.Tok.is(tok::less))
|
|
|
|
parseObjCProtocolList();
|
2013-01-10 04:25:35 +08:00
|
|
|
|
|
|
|
// If instance variables are present, keep the '{' on the first line too.
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace))
|
2013-01-23 17:32:48 +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
|
|
|
|
|
|
|
if (FormatTok.Tok.is(tok::less))
|
|
|
|
parseObjCProtocolList();
|
|
|
|
|
|
|
|
// Check for protocol declaration.
|
|
|
|
if (FormatTok.Tok.is(tok::semi)) {
|
|
|
|
nextToken();
|
|
|
|
return addUnwrappedLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
addUnwrappedLine();
|
|
|
|
parseObjCUntilAtEnd();
|
2013-01-10 04:25:35 +08:00
|
|
|
}
|
|
|
|
|
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-02-11 20:33:24 +08:00
|
|
|
llvm::dbgs() << "Line(" << Line->Level << ")"
|
|
|
|
<< (Line->InPPDirective ? " MACRO" : "") << ": ";
|
2013-01-16 20:31:12 +08:00
|
|
|
for (std::list<FormatToken>::iterator I = Line->Tokens.begin(),
|
|
|
|
E = Line->Tokens.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
llvm::dbgs() << I->Tok.getName() << " ";
|
2013-01-16 17:10:19 +08:00
|
|
|
|
2013-01-16 20:31:12 +08:00
|
|
|
}
|
|
|
|
llvm::dbgs() << "\n";
|
|
|
|
});
|
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-03-02 02:11:39 +08:00
|
|
|
for (std::vector<UnwrappedLine>::iterator
|
|
|
|
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-03-20 20:37:50 +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();
|
|
|
|
for (SmallVectorImpl<FormatToken>::const_iterator
|
|
|
|
I = CommentsBeforeNextToken.begin(),
|
|
|
|
E = CommentsBeforeNextToken.end();
|
|
|
|
I != E; ++I) {
|
2013-02-07 00:40:56 +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-02-07 00:40:56 +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();
|
|
|
|
while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) &&
|
2013-04-09 06:16:06 +08:00
|
|
|
(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.
|
|
|
|
flushComments(FormatTok.NewlinesBefore > 0);
|
2013-01-23 00:31:55 +08:00
|
|
|
parsePPDirective();
|
|
|
|
}
|
|
|
|
if (!FormatTok.Tok.is(tok::comment))
|
|
|
|
return;
|
2013-02-07 00:40:56 +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());
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::pushToken(const FormatToken &Tok) {
|
|
|
|
Line->Tokens.push_back(Tok);
|
|
|
|
if (MustBreakBeforeNextToken) {
|
|
|
|
Line->Tokens.back().MustBreakBefore = true;
|
|
|
|
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
|