r226930 - clang-format: Fix another crasher caused by incomplete macro code.

Daniel Jasper djasper at google.com
Fri Jan 23 11:37:26 PST 2015


Author: djasper
Date: Fri Jan 23 13:37:25 2015
New Revision: 226930

URL: http://llvm.org/viewvc/llvm-project?rev=226930&view=rev
Log:
clang-format: Fix another crasher caused by incomplete macro code.

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.

Modified:
    cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp?rev=226930&r1=226929&r2=226930&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp Fri Jan 23 13:37:25 2015
@@ -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 Sma
         }
       }
     }
-    if (!DryRun) {
-      for (FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next) {
-        Tok->Finalized = true;
-      }
-    }
+    if (!DryRun)
+      markFinalized(TheLine.First);
     PreviousLine = *I;
   }
   PenaltyCache[CacheKey] = Penalty;

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=226930&r1=226929&r2=226930&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Jan 23 13:37:25 2015
@@ -2614,6 +2614,7 @@ TEST_F(FormatTest, MacroDefinitionsWithI
   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) {





More information about the cfe-commits mailing list