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.
|
|
|
|
///
|
|
|
|
/// This is EXPERIMENTAL code under heavy development. It is not in a state yet,
|
|
|
|
/// where it can be used to format real code.
|
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "UnwrappedLineParser.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
2013-01-05 07:34:14 +08:00
|
|
|
class ScopedMacroState : public FormatTokenSource {
|
|
|
|
public:
|
|
|
|
ScopedMacroState(UnwrappedLine &Line, FormatTokenSource *&TokenSource,
|
|
|
|
FormatToken &ResetToken)
|
|
|
|
: Line(Line), TokenSource(TokenSource), ResetToken(ResetToken),
|
2013-01-06 06:14:16 +08:00
|
|
|
PreviousLineLevel(Line.Level), PreviousTokenSource(TokenSource) {
|
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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool eof() {
|
|
|
|
return Token.NewlinesBefore > 0 && Token.HasUnescapedNewline;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
FormatToken Token;
|
|
|
|
};
|
|
|
|
|
2012-12-08 00:15:44 +08:00
|
|
|
UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
|
|
|
|
FormatTokenSource &Tokens,
|
2012-12-04 02:12:45 +08:00
|
|
|
UnwrappedLineConsumer &Callback)
|
2013-01-09 23:25:02 +08:00
|
|
|
: Line(new UnwrappedLine), RootTokenInitialized(false),
|
|
|
|
LastInCurrentLine(NULL), MustBreakBeforeNextToken(false), Style(Style),
|
|
|
|
Tokens(&Tokens), Callback(Callback) {
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2012-12-05 01:27:50 +08:00
|
|
|
bool UnwrappedLineParser::parse() {
|
2013-01-05 07:34:14 +08:00
|
|
|
readToken();
|
|
|
|
return parseFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UnwrappedLineParser::parseFile() {
|
2013-01-07 04:07:31 +08:00
|
|
|
bool Error = parseLevel(/*HasOpeningBrace=*/false);
|
2013-01-05 07:34:14 +08:00
|
|
|
// Make sure to format the remaining tokens.
|
|
|
|
addUnwrappedLine();
|
|
|
|
return Error;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2013-01-07 04:07:31 +08:00
|
|
|
bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
|
2012-12-05 01:27:50 +08:00
|
|
|
bool Error = false;
|
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:
|
2012-12-05 01:27:50 +08:00
|
|
|
Error |= parseBlock();
|
2012-12-04 02:12:45 +08:00
|
|
|
addUnwrappedLine();
|
|
|
|
break;
|
|
|
|
case tok::r_brace:
|
2013-01-07 04:07:31 +08:00
|
|
|
if (HasOpeningBrace) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// Stray '}' is an error.
|
|
|
|
Error = true;
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
|
|
|
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());
|
2012-12-05 01:27:50 +08:00
|
|
|
return Error;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
2012-12-07 02:03:27 +08:00
|
|
|
bool UnwrappedLineParser::parseBlock(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();
|
|
|
|
|
|
|
|
addUnwrappedLine();
|
|
|
|
|
2013-01-09 23:25:02 +08:00
|
|
|
Line->Level += AddLevels;
|
2013-01-07 04:07:31 +08:00
|
|
|
parseLevel(/*HasOpeningBrace=*/true);
|
2013-01-09 23:25:02 +08:00
|
|
|
Line->Level -= AddLevels;
|
2012-12-07 02:03:27 +08:00
|
|
|
|
2012-12-04 23:40:36 +08:00
|
|
|
if (!FormatTok.Tok.is(tok::r_brace))
|
2012-12-05 01:27:50 +08:00
|
|
|
return true;
|
2012-12-04 23:40:36 +08:00
|
|
|
|
2013-01-08 02:10:23 +08:00
|
|
|
nextToken(); // Munch the closing brace.
|
2012-12-05 01:27:50 +08:00
|
|
|
return false;
|
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-01-09 23:25:02 +08:00
|
|
|
ScopedMacroState MacroState(*Line, Tokens, FormatTok);
|
2013-01-03 00:30:12 +08:00
|
|
|
nextToken();
|
|
|
|
|
|
|
|
if (FormatTok.Tok.getIdentifierInfo() == NULL) {
|
|
|
|
addUnwrappedLine();
|
|
|
|
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();
|
|
|
|
if (FormatTok.Tok.getKind() == tok::l_paren) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-12-17 19:29:41 +08:00
|
|
|
void UnwrappedLineParser::parseComments() {
|
2012-12-05 22:57:28 +08:00
|
|
|
// Consume leading line comments, e.g. for branches without compounds.
|
|
|
|
while (FormatTok.Tok.is(tok::comment)) {
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
}
|
2012-12-17 19:29:41 +08:00
|
|
|
}
|
|
|
|
|
2013-01-07 22:56:16 +08:00
|
|
|
void UnwrappedLineParser::parseStructuralElement() {
|
2012-12-17 19:29:41 +08:00
|
|
|
parseComments();
|
2012-12-05 22:57:28 +08:00
|
|
|
|
2012-12-31 05:27:25 +08:00
|
|
|
int TokenNumber = 0;
|
2012-12-04 22:46:19 +08:00
|
|
|
switch (FormatTok.Tok.getKind()) {
|
2013-01-08 03:05:19 +08:00
|
|
|
case tok::at:
|
|
|
|
nextToken();
|
|
|
|
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();
|
|
|
|
TokenNumber++;
|
|
|
|
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;
|
2012-12-04 22:46:19 +08:00
|
|
|
default:
|
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
++TokenNumber;
|
|
|
|
switch (FormatTok.Tok.getKind()) {
|
2012-12-04 22:46:19 +08:00
|
|
|
case tok::kw_enum:
|
|
|
|
parseEnum();
|
|
|
|
return;
|
2013-01-08 02:10:23 +08:00
|
|
|
case tok::kw_struct: // fallthrough
|
|
|
|
case tok::kw_class:
|
|
|
|
parseStructOrClass();
|
|
|
|
return;
|
2012-12-04 02:12:45 +08:00
|
|
|
case tok::semi:
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
case tok::l_paren:
|
|
|
|
parseParens();
|
|
|
|
break;
|
|
|
|
case tok::l_brace:
|
|
|
|
parseBlock();
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
2012-12-04 22:46:19 +08:00
|
|
|
case tok::identifier:
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
|
|
|
if (TokenNumber == 1 && FormatTok.Tok.is(tok::colon)) {
|
|
|
|
parseLabel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2012-12-17 19:29:41 +08:00
|
|
|
case tok::equal:
|
|
|
|
nextToken();
|
|
|
|
// Skip initializers as they will be formatted by a later step.
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace))
|
|
|
|
nextToken();
|
|
|
|
break;
|
2012-12-04 22:46:19 +08:00
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseIfThenElse() {
|
|
|
|
assert(FormatTok.Tok.is(tok::kw_if) && "'if' expected");
|
|
|
|
nextToken();
|
|
|
|
parseParens();
|
|
|
|
bool NeedsUnwrappedLine = false;
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
|
|
|
parseBlock();
|
|
|
|
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)) {
|
|
|
|
parseBlock();
|
|
|
|
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)) {
|
|
|
|
parseBlock(0);
|
|
|
|
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();
|
|
|
|
parseParens();
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
|
|
|
parseBlock();
|
|
|
|
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)) {
|
|
|
|
parseBlock();
|
|
|
|
} 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() {
|
|
|
|
// FIXME: remove all asserts.
|
|
|
|
assert(FormatTok.Tok.is(tok::colon) && "':' expected");
|
|
|
|
nextToken();
|
2013-01-09 23:25:02 +08:00
|
|
|
unsigned OldLineLevel = Line->Level;
|
|
|
|
if (Line->Level > 0)
|
|
|
|
--Line->Level;
|
2012-12-04 02:12:45 +08:00
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
|
|
|
parseBlock();
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
parseParens();
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
2012-12-07 02:03:27 +08:00
|
|
|
parseBlock(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() {
|
2012-12-04 22:46:19 +08:00
|
|
|
bool HasContents = false;
|
2012-12-04 02:12:45 +08:00
|
|
|
do {
|
2012-12-04 22:46:19 +08:00
|
|
|
switch (FormatTok.Tok.getKind()) {
|
|
|
|
case tok::l_brace:
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
2013-01-09 23:25:02 +08:00
|
|
|
++Line->Level;
|
2012-12-17 19:29:41 +08:00
|
|
|
parseComments();
|
2012-12-04 22:46:19 +08:00
|
|
|
break;
|
|
|
|
case tok::l_paren:
|
|
|
|
parseParens();
|
|
|
|
break;
|
|
|
|
case tok::comma:
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
2012-12-17 19:29:41 +08:00
|
|
|
parseComments();
|
2012-12-04 22:46:19 +08:00
|
|
|
break;
|
|
|
|
case tok::r_brace:
|
|
|
|
if (HasContents)
|
|
|
|
addUnwrappedLine();
|
2013-01-09 23:25:02 +08:00
|
|
|
--Line->Level;
|
2012-12-04 22:46:19 +08:00
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
case tok::semi:
|
2012-12-04 02:12:45 +08:00
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
2012-12-04 22:46:19 +08:00
|
|
|
default:
|
|
|
|
HasContents = true;
|
|
|
|
nextToken();
|
|
|
|
break;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
} while (!eof());
|
2013-01-08 02:10:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseStructOrClass() {
|
|
|
|
nextToken();
|
|
|
|
do {
|
|
|
|
switch (FormatTok.Tok.getKind()) {
|
|
|
|
case tok::l_brace:
|
|
|
|
// FIXME: Think about how to resolve the error handling here.
|
|
|
|
parseBlock();
|
|
|
|
parseStructuralElement();
|
|
|
|
return;
|
|
|
|
case tok::semi:
|
|
|
|
nextToken();
|
|
|
|
addUnwrappedLine();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
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();
|
|
|
|
nextToken(); // interface name
|
|
|
|
|
|
|
|
// @interface can be followed by either a base class, or a category.
|
|
|
|
if (FormatTok.Tok.is(tok::colon)) {
|
|
|
|
nextToken();
|
|
|
|
nextToken(); // base class name
|
|
|
|
} 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))
|
|
|
|
parseBlock();
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
nextToken(); // protocol name
|
|
|
|
|
|
|
|
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-08 22:56:18 +08:00
|
|
|
if (!RootTokenInitialized)
|
|
|
|
return;
|
2012-12-04 02:12:45 +08:00
|
|
|
// Consume trailing comments.
|
|
|
|
while (!eof() && FormatTok.NewlinesBefore == 0 &&
|
|
|
|
FormatTok.Tok.is(tok::comment)) {
|
|
|
|
nextToken();
|
|
|
|
}
|
2013-01-09 23:25:02 +08:00
|
|
|
Callback.consumeUnwrappedLine(*Line);
|
2013-01-08 22:56:18 +08:00
|
|
|
RootTokenInitialized = false;
|
2013-01-09 23:25:02 +08:00
|
|
|
LastInCurrentLine = NULL;
|
2012-12-04 02:12:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UnwrappedLineParser::eof() const {
|
|
|
|
return FormatTok.Tok.is(tok::eof);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::nextToken() {
|
|
|
|
if (eof())
|
|
|
|
return;
|
2013-01-08 22:56:18 +08:00
|
|
|
if (RootTokenInitialized) {
|
2013-01-09 23:25:02 +08:00
|
|
|
assert(LastInCurrentLine->Children.empty());
|
2013-01-08 22:56:18 +08:00
|
|
|
LastInCurrentLine->Children.push_back(FormatTok);
|
|
|
|
LastInCurrentLine = &LastInCurrentLine->Children.back();
|
|
|
|
} else {
|
2013-01-09 23:25:02 +08:00
|
|
|
Line->RootToken = FormatTok;
|
2013-01-08 22:56:18 +08:00
|
|
|
RootTokenInitialized = true;
|
2013-01-09 23:25:02 +08:00
|
|
|
LastInCurrentLine = &Line->RootToken;
|
|
|
|
}
|
|
|
|
if (MustBreakBeforeNextToken) {
|
|
|
|
LastInCurrentLine->MustBreakBefore = true;
|
|
|
|
MustBreakBeforeNextToken = false;
|
2013-01-08 22:56:18 +08:00
|
|
|
}
|
2013-01-05 07:34:14 +08:00
|
|
|
readToken();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::readToken() {
|
|
|
|
FormatTok = Tokens->getNextToken();
|
2013-01-09 23:25:02 +08:00
|
|
|
while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) &&
|
2013-01-06 06:56:06 +08:00
|
|
|
((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) ||
|
|
|
|
FormatTok.IsFirst)) {
|
2013-01-09 23:25:02 +08:00
|
|
|
UnwrappedLine* StoredLine = Line.take();
|
|
|
|
Line.reset(new UnwrappedLine(*StoredLine));
|
|
|
|
assert(LastInCurrentLine == NULL || LastInCurrentLine->Children.empty());
|
|
|
|
FormatToken *StoredLastInCurrentLine = LastInCurrentLine;
|
|
|
|
bool PreviousInitialized = RootTokenInitialized;
|
|
|
|
RootTokenInitialized = false;
|
|
|
|
LastInCurrentLine = NULL;
|
|
|
|
|
2013-01-05 07:34:14 +08:00
|
|
|
parsePPDirective();
|
2013-01-09 23:25:02 +08:00
|
|
|
|
|
|
|
assert(!RootTokenInitialized);
|
|
|
|
Line.reset(StoredLine);
|
|
|
|
RootTokenInitialized = PreviousInitialized;
|
|
|
|
LastInCurrentLine = StoredLastInCurrentLine;
|
|
|
|
assert(LastInCurrentLine == NULL || LastInCurrentLine->Children.empty());
|
|
|
|
MustBreakBeforeNextToken = true;
|
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
|