Change the data structure used in clang-format.

This is a first step towards supporting more complex structures such
as #ifs inside unwrapped lines. This patch mostly converts the array-based
UnwrappedLine into a linked-list-based UnwrappedLine. Future changes will
allow multiple children for each Token turning the UnwrappedLine into a
tree.

No functional changes intended.

llvm-svn: 171856
This commit is contained in:
Daniel Jasper 2013-01-08 14:56:18 +00:00
parent 97059d4377
commit 7c85fde500
4 changed files with 316 additions and 299 deletions

File diff suppressed because it is too large Load Diff

View File

@ -74,7 +74,8 @@ private:
UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style,
FormatTokenSource &Tokens,
UnwrappedLineConsumer &Callback)
: Style(Style), Tokens(&Tokens), Callback(Callback) {
: RootTokenInitialized(false), Style(Style), Tokens(&Tokens),
Callback(Callback) {
}
bool UnwrappedLineParser::parse() {
@ -493,13 +494,15 @@ void UnwrappedLineParser::parseStructOrClass() {
}
void UnwrappedLineParser::addUnwrappedLine() {
if (!RootTokenInitialized)
return;
// Consume trailing comments.
while (!eof() && FormatTok.NewlinesBefore == 0 &&
FormatTok.Tok.is(tok::comment)) {
nextToken();
}
Callback.consumeUnwrappedLine(Line);
Line.Tokens.clear();
RootTokenInitialized = false;
}
bool UnwrappedLineParser::eof() const {
@ -509,7 +512,14 @@ bool UnwrappedLineParser::eof() const {
void UnwrappedLineParser::nextToken() {
if (eof())
return;
Line.Tokens.push_back(FormatTok);
if (RootTokenInitialized) {
LastInCurrentLine->Children.push_back(FormatTok);
LastInCurrentLine = &LastInCurrentLine->Children.back();
} else {
Line.RootToken = FormatTok;
RootTokenInitialized = true;
LastInCurrentLine = &Line.RootToken;
}
readToken();
}

View File

@ -24,6 +24,8 @@
#include "clang/Format/Format.h"
#include "clang/Lex/Lexer.h"
#include <vector>
namespace clang {
namespace format {
@ -65,6 +67,11 @@ struct FormatToken {
/// \brief Indicates that this is the first token.
bool IsFirst;
// FIXME: We currently assume that there is exactly one token in this vector
// except for the very last token that does not have any children.
/// \brief All tokens that logically follow this token.
std::vector<FormatToken> Children;
};
/// \brief An unwrapped line is a sequence of \c Token, that we would like to
@ -78,7 +85,7 @@ struct UnwrappedLine {
}
/// \brief The \c Token comprising this \c UnwrappedLine.
SmallVector<FormatToken, 16> Tokens;
FormatToken RootToken;
/// \brief The indent level of the \c UnwrappedLine.
unsigned Level;
@ -138,6 +145,8 @@ private:
// subtracted from beyond 0. Introduce a method to subtract from Line.Level
// and use that everywhere in the Parser.
UnwrappedLine Line;
bool RootTokenInitialized;
FormatToken *LastInCurrentLine;
FormatToken FormatTok;
const FormatStyle &Style;

View File

@ -537,8 +537,8 @@ TEST_F(FormatTest, LayoutRemainingTokens) {
}
TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) {
EXPECT_EQ("#define A \\\n b;",
format("#define A b;", 10, 2, getLLVMStyleWithColumns(11)));
EXPECT_EQ("# define A\\\n b;",
format("# define A b;", 11, 2, getLLVMStyleWithColumns(11)));
}
TEST_F(FormatTest, MacroDefinitionInsideStatement) {