]> granicus.if.org Git - clang/commitdiff
clang-format: Fix another crasher caused by incomplete macro code.
authorDaniel Jasper <djasper@google.com>
Fri, 23 Jan 2015 19:37:25 +0000 (19:37 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 23 Jan 2015 19:37:25 +0000 (19:37 +0000)
We did't properly mark all of an AnnotatedLine's children as finalized
and thus would reformat the same tokens in different branches of #if/#else
sequences leading to invalid replacements.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@226930 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/UnwrappedLineFormatter.cpp
unittests/Format/FormatTest.cpp

index ca66e7351641c7fd81d15f78d09fe85b67603e90..6c4aadc0f1c355f76c974162ab30b4bbef3b2e65 100644 (file)
@@ -309,6 +309,15 @@ private:
   ContinuationIndenter *Indenter;
 };
 
+
+static void markFinalized(FormatToken *Tok) {
+  for (; Tok; Tok = Tok->Next) {
+    Tok->Finalized = true;
+    for (AnnotatedLine *Child : Tok->Children)
+      markFinalized(Child->First);
+  }
+}
+
 } // namespace
 
 unsigned
@@ -442,11 +451,8 @@ UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
         }
       }
     }
-    if (!DryRun) {
-      for (FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next) {
-        Tok->Finalized = true;
-      }
-    }
+    if (!DryRun)
+      markFinalized(TheLine.First);
     PreviousLine = *I;
   }
   PenaltyCache[CacheKey] = Penalty;
index faf7c43688387350a8b7f1ec1a8513b022cd8540..65a7bff599ae1f1debfb67c85deccae360438ade 100644 (file)
@@ -2614,6 +2614,7 @@ TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
+  verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
 }
 
 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {