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);
continue;
}
- if (nonPPLineAffected(Line, &PreviousLineAffected))
+ if (nonPPLineAffected(Line, PreviousLine))
SomeLineAffected = true;
+ PreviousLine = Line;
++I;
}
return SomeLineAffected;
// 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());
// 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;
+
+ 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) {
+ if (SomeTokenAffected || SomeFirstChildAffected || LineMoved ||
+ IsContinuedComment) {
Line->Affected = true;
- *PreviousLineAffected = true;
SomeLineAffected = true;
- } else {
- *PreviousLineAffected = false;
}
return SomeLineAffected;
}
"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) {
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);",