clang-format: Properly reset nested AnnotatedLine structure.
This fixes llvm.org/PR17682.
Without this patch, the following code leads to invalid reads/writes:
DEBUG({
return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
});
#if a
#else
#endif
Because of the #if-#else structure, the code is formatted and annotated
twice and becauce of the nested block, the annotated lines form a
hierarchical structure. This structure was not properly reset between
runs.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@193352 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/TokenAnnotator.h b/lib/Format/TokenAnnotator.h
index e51003b..ca8b115 100644
--- a/lib/Format/TokenAnnotator.h
+++ b/lib/Format/TokenAnnotator.h
@@ -56,6 +56,7 @@
Current->Next = I->Tok;
I->Tok->Previous = Current;
Current = Current->Next;
+ Current->Children.clear();
for (SmallVectorImpl<UnwrappedLine>::const_iterator
I = Node.Children.begin(),
E = Node.Children.end();
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 9b24f04..4319409 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -2333,6 +2333,13 @@
"#if 1\n"
"#else\n"
"#endif\n");
+ verifyFormat("DEBUG({\n"
+ " return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
+ "});\n"
+ "#if a\n"
+ "#else\n"
+ "#endif");
}
TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {