Alternative handling of comments adjacent to preprocessor directives.

Summary: Store comments in ScopedLineState

Reviewers: klimek, djasper

Reviewed By: klimek

CC: cfe-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D609

llvm-svn: 178537
This commit is contained in:
Alexander Kornienko 2013-04-02 13:04:06 +00:00
parent a922c7e90e
commit b5dad75e38
2 changed files with 12 additions and 1 deletions

View File

@ -100,6 +100,7 @@ public:
Parser.Line.reset(new UnwrappedLine());
Parser.Line->Level = PreBlockLine->Level;
Parser.Line->InPPDirective = PreBlockLine->InPPDirective;
Parser.CommentsBeforeNextToken.swap(CommentsBeforeNextToken);
}
~ScopedLineState() {
@ -111,6 +112,7 @@ public:
Parser.MustBreakBeforeNextToken = true;
if (SwitchToPreprocessorLines)
Parser.CurrentLines = &Parser.Lines;
Parser.CommentsBeforeNextToken.swap(CommentsBeforeNextToken);
}
private:
@ -118,6 +120,7 @@ private:
const bool SwitchToPreprocessorLines;
UnwrappedLine *PreBlockLine;
SmallVector<FormatToken, 1> CommentsBeforeNextToken;
};
UnwrappedLineParser::UnwrappedLineParser(
@ -822,7 +825,6 @@ void UnwrappedLineParser::readToken() {
while (!Line->InPPDirective && FormatTok.Tok.is(tok::hash) &&
((FormatTok.NewlinesBefore > 0 && FormatTok.HasUnescapedNewline) ||
FormatTok.IsFirst)) {
flushComments(FormatTok.NewlinesBefore > 0);
// If there is an unfinished unwrapped line, we flush the preprocessor
// directives only after that unwrapped line was finished later.
bool SwitchToPreprocessorLines =

View File

@ -708,6 +708,15 @@ TEST_F(FormatTest, SplitsLongCxxComments) {
getLLVMStyleWithColumns(20)));
}
TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
EXPECT_EQ("namespace {}\n// Test\n#define A",
format("namespace {}\n // Test\n#define A"));
EXPECT_EQ("namespace {}\n/* Test */\n#define A",
format("namespace {}\n /* Test */\n#define A"));
EXPECT_EQ("namespace {}\n/* Test */ #define A",
format("namespace {}\n /* Test */ #define A"));
}
TEST_F(FormatTest, SplitsLongLinesInComments) {
EXPECT_EQ("/* This is a long\n"
" * comment that\n"