r193071 - Fixes PR17617: Crash on joining short if statements.

Manuel Klimek klimek at google.com
Mon Oct 21 01:11:15 PDT 2013


Author: klimek
Date: Mon Oct 21 03:11:15 2013
New Revision: 193071

URL: http://llvm.org/viewvc/llvm-project?rev=193071&view=rev
Log:
Fixes PR17617: Crash on joining short if statements.

Now that we iterate on the formatting multiple times when we
have chains of preprocessor branches, we need to correctly reset
the token's previous and next pointer for the first / last token.

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.h
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.h?rev=193071&r1=193070&r2=193071&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.h (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.h Mon Oct 21 03:11:15 2013
@@ -43,6 +43,11 @@ public:
         MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
         StartsDefinition(false) {
     assert(!Line.Tokens.empty());
+
+    // Calculate Next and Previous for all tokens. Note that we must overwrite
+    // Next and Previous for every token, as previous formatting runs might have
+    // left them in a different state.
+    First->Previous = NULL;
     FormatToken *Current = First;
     for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
                                                       E = Line.Tokens.end();
@@ -60,6 +65,7 @@ public:
       }
     }
     Last = Current;
+    Last->Next = NULL;
   }
 
   ~AnnotatedLine() {

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=193071&r1=193070&r2=193071&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Oct 21 03:11:15 2013
@@ -2329,6 +2329,19 @@ TEST_F(FormatTest, LayoutStatementsAroun
                "int i;");
 }
 
+TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
+  FormatStyle SingleLine = getLLVMStyle();
+  SingleLine.AllowShortIfStatementsOnASingleLine = true;
+  verifyFormat(
+      "#if 0\n"
+      "#elif 1\n"
+      "#endif\n"
+      "void foo() {\n"
+      "  if (test) foo2();\n"
+      "}",
+      SingleLine);
+}
+
 TEST_F(FormatTest, LayoutBlockInsideParens) {
   EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );"));
   EXPECT_EQ("functionCall({\n"





More information about the cfe-commits mailing list