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-10 23:05:09 +08:00
|
|
|
#include "clang/Basic/Diagnostic.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,
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
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-01-10 23:05:09 +08:00
|
|
|
UnwrappedLineParser::UnwrappedLineParser(
|
|
|
|
clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
|
|
|
|
FormatTokenSource &Tokens, UnwrappedLineConsumer &Callback)
|
2013-01-18 22:04:34 +08:00
|
|
|
: Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
|
|
|
|
CurrentLines(&Lines), Diag(Diag), 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-16 20:31:12 +08:00
|
|
|
DEBUG(llvm::dbgs() << "----\n");
|
2013-01-05 07:34:14 +08:00
|
|
|
readToken();
|
2013-01-18 22:04:34 +08:00
|
|
|
bool Error = parseFile();
|
|
|
|
for (std::vector<UnwrappedLine>::iterator I = Lines.begin(),
|
|
|
|
E = Lines.end();
|
|
|
|
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-01-18 22:04:34 +08:00
|
|
|
return Error;
|
2013-01-05 07:34:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool UnwrappedLineParser::parseFile() {
|
2013-01-23 17:32:48 +08:00
|
|
|
ScopedDeclarationState DeclarationState(*Line, DeclarationScopeStack,
|
|
|
|
/*MustBeDeclaration=*/ true);
|
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.
|
2013-01-23 00:31:55 +08:00
|
|
|
flushComments(true);
|
2013-01-05 07:34:14 +08:00
|
|
|
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:
|
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.
|
|
|
|
Error |= parseBlock(/*MustBeDeclaration=*/ false);
|
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 {
|
2013-01-10 23:05:09 +08:00
|
|
|
Diag.Report(FormatTok.Tok.getLocation(),
|
|
|
|
Diag.getCustomDiagID(clang::DiagnosticsEngine::Error,
|
2013-01-12 00:03:45 +08:00
|
|
|
"unexpected '}'"));
|
2013-01-07 04:07:31 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-11 04:35:35 +08:00
|
|
|
bool UnwrappedLineParser::parseBlock(bool MustBeDeclaration,
|
|
|
|
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;
|
|
|
|
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-01-22 00:42:44 +08:00
|
|
|
return true;
|
2013-01-23 00:31:55 +08:00
|
|
|
}
|
2012-12-04 23:40:36 +08:00
|
|
|
|
2013-01-08 02:10:23 +08:00
|
|
|
nextToken(); // Munch the closing brace.
|
2013-01-23 00:31:55 +08:00
|
|
|
Line->Level -= AddLevels;
|
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) {
|
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-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-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();
|
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();
|
|
|
|
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;
|
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 {
|
|
|
|
++TokenNumber;
|
|
|
|
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-01-10 19:52:21 +08:00
|
|
|
// 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-01-23 17:32:48 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/ false);
|
2012-12-04 02:12:45 +08:00
|
|
|
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();
|
2013-01-10 19:52:21 +08:00
|
|
|
if (FormatTok.Tok.is(tok::l_brace)) {
|
|
|
|
parseBracedList();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!eof());
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnwrappedLineParser::parseBracedList() {
|
|
|
|
nextToken();
|
|
|
|
|
|
|
|
do {
|
|
|
|
switch (FormatTok.Tok.getKind()) {
|
|
|
|
case tok::l_brace:
|
|
|
|
parseBracedList();
|
2012-12-17 19:29:41 +08:00
|
|
|
break;
|
2013-01-10 19:52:21 +08:00
|
|
|
case tok::r_brace:
|
|
|
|
nextToken();
|
|
|
|
return;
|
2012-12-04 22:46:19 +08:00
|
|
|
default:
|
|
|
|
nextToken();
|
|
|
|
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();
|
|
|
|
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: {
|
|
|
|
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-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;
|
|
|
|
if (Line->Level > 0)
|
|
|
|
--Line->Level;
|
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);
|
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-02-06 23:57:54 +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-01-15 21:38:33 +08:00
|
|
|
while (FormatTok.Tok.isNot(tok::l_brace)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
if (FormatTok.Tok.is(tok::l_brace))
|
2013-01-23 17:32:48 +08:00
|
|
|
parseBlock(/*MustBeDeclaration=*/ true);
|
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();
|
|
|
|
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))
|
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();
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
bool UnwrappedLineParser::eof() const {
|
|
|
|
return FormatTok.Tok.is(tok::eof);
|
|
|
|
}
|
|
|
|
|
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) &&
|
|
|
|
((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) ||
|
|
|
|
FormatTok.IsFirst)) {
|
|
|
|
// If there is an unfinished unwrapped line, we flush the preprocessor
|
|
|
|
// directives only after that unwrapped line was finished later.
|
|
|
|
bool SwitchToPreprocessorLines = !Line->Tokens.empty() &&
|
|
|
|
CurrentLines == &Lines;
|
|
|
|
ScopedLineState BlockState(*this, SwitchToPreprocessorLines);
|
|
|
|
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
|