clang-format: Extends formatted ranges to subsequent lines comments.

Before:
  int aaaa;     // This line is formatted.
                // The comment continues ..
                // .. here.

Before:
  int aaaa; // This line is formatted.
            // The comment continues ..
            // .. here.

This fixes llvm.org/PR17914.

llvm-svn: 195954
This commit is contained in:
Daniel Jasper 2013-11-29 09:27:43 +00:00
parent 1556b59338
commit 38c82408a7
2 changed files with 49 additions and 13 deletions

View File

@ -1275,7 +1275,7 @@ private:
bool computeAffectedLines(SmallVectorImpl<AnnotatedLine *>::iterator I,
SmallVectorImpl<AnnotatedLine *>::iterator E) {
bool SomeLineAffected = false;
bool PreviousLineAffected = false;
const AnnotatedLine *PreviousLine = NULL;
while (I != E) {
AnnotatedLine *Line = *I;
Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First);
@ -1299,9 +1299,10 @@ private:
continue;
}
if (nonPPLineAffected(Line, &PreviousLineAffected))
if (nonPPLineAffected(Line, PreviousLine))
SomeLineAffected = true;
PreviousLine = Line;
++I;
}
return SomeLineAffected;
@ -1309,7 +1310,8 @@ private:
// Determines whether 'Line' is affected by the SourceRanges given as input.
// Returns \c true if line or one if its children is affected.
bool nonPPLineAffected(AnnotatedLine *Line, bool *PreviousLineAffected) {
bool nonPPLineAffected(AnnotatedLine *Line,
const AnnotatedLine *PreviousLine) {
bool SomeLineAffected = false;
Line->ChildrenAffected =
computeAffectedLines(Line->Children.begin(), Line->Children.end());
@ -1340,14 +1342,18 @@ private:
// Was this line moved, i.e. has it previously been on the same line as an
// affected line?
bool LineMoved = *PreviousLineAffected && Line->First->NewlinesBefore == 0;
bool LineMoved = PreviousLine && PreviousLine->Affected &&
Line->First->NewlinesBefore == 0;
if (SomeTokenAffected || SomeFirstChildAffected || LineMoved) {
bool IsContinuedComment = Line->First->is(tok::comment) &&
Line->First->Next == NULL &&
Line->First->NewlinesBefore < 2 && PreviousLine &&
PreviousLine->Last->is(tok::comment);
if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
IsContinuedComment) {
Line->Affected = true;
*PreviousLineAffected = true;
SomeLineAffected = true;
} else {
*PreviousLineAffected = false;
}
return SomeLineAffected;
}

View File

@ -829,6 +829,36 @@ TEST_F(FormatTest, CanFormatCommentsLocally) {
"int b;\n"
"int c; // unrelated comment",
31, 0, getLLVMStyle()));
EXPECT_EQ("int a; // This\n"
" // is\n"
" // a",
format("int a; // This\n"
" // is\n"
" // a",
0, 0, getLLVMStyle()));
EXPECT_EQ("int a; // This\n"
" // is\n"
" // a\n"
"// This is b\n"
"int b;",
format("int a; // This\n"
" // is\n"
" // a\n"
"// This is b\n"
"int b;",
0, 0, getLLVMStyle()));
EXPECT_EQ("int a; // This\n"
" // is\n"
" // a\n"
"\n"
" // This is unrelated",
format("int a; // This\n"
" // is\n"
" // a\n"
"\n"
" // This is unrelated",
0, 0, getLLVMStyle()));
}
TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
@ -843,11 +873,11 @@ TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
TEST_F(FormatTest, UnderstandsBlockComments) {
verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
EXPECT_EQ(
"f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
" bbbbbbbbbbbbbbbbbbbbbbbbb);",
format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n/* Trailing comment for aa... */\n"
" bbbbbbbbbbbbbbbbbbbbbbbbb);"));
EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
" bbbbbbbbbbbbbbbbbbbbbbbbb);",
format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n"
"/* Trailing comment for aa... */\n"
" bbbbbbbbbbbbbbbbbbbbbbbbb);"));
EXPECT_EQ(
"f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",