]> granicus.if.org Git - clang/commitdiff
Fixes PR17617: Crash on joining short if statements.
authorManuel Klimek <klimek@google.com>
Mon, 21 Oct 2013 08:11:15 +0000 (08:11 +0000)
committerManuel Klimek <klimek@google.com>
Mon, 21 Oct 2013 08:11:15 +0000 (08:11 +0000)
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.

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

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

index 91f1b4678a14ace3d323dc3337c77e2f944cf136..e51003bac822a71a3a84457f1ebfe1c88040d4b4 100644 (file)
@@ -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() {
index 6a2d0d89b4cdd61e6a4ae5996c054e0ead12bdb7..a7de81b26782cd2ee278420846462966057eb056 100644 (file)
@@ -2329,6 +2329,19 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
                "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"