]> granicus.if.org Git - clang/commitdiff
clang-format: Properly reset nested AnnotatedLine structure.
authorDaniel Jasper <djasper@google.com>
Thu, 24 Oct 2013 15:23:11 +0000 (15:23 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 24 Oct 2013 15:23:11 +0000 (15:23 +0000)
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

lib/Format/TokenAnnotator.h
unittests/Format/FormatTest.cpp

index e51003bac822a71a3a84457f1ebfe1c88040d4b4..ca8b115415766b26e29cecc46e3b74443d8c2d5d 100644 (file)
@@ -56,6 +56,7 @@ public:
       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();
index 9b24f040d164acf2a2a67b5b5fd6d0d978358b4b..431940938dc98376cdef206df15879b92162c047 100644 (file)
@@ -2333,6 +2333,13 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
       "#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) {