]> granicus.if.org Git - clang/commitdiff
clang-format: Fix child-formatting in macros.
authorDaniel Jasper <djasper@google.com>
Tue, 26 May 2015 07:03:42 +0000 (07:03 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 26 May 2015 07:03:42 +0000 (07:03 +0000)
This fixes a case where the column limit was incorrectly calculated
leading to a macro like this:

  #define A                                       \
    [] {                                          \
      xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \
          xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \
    }

exceeding the column limit.

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

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

index 9c8b5ba08eba488115f4fa78905087137b386aea..cbf8c6c922118eccb6455b0d444ba2c811e8775e 100644 (file)
@@ -934,10 +934,14 @@ UnwrappedLineFormatter::getColumnLimit(bool InPPDirective,
   // In preprocessor directives reserve two chars for trailing " \" if the
   // next line continues the preprocessor directive.
   bool ContinuesPPDirective =
-      InPPDirective && NextLine && NextLine->InPPDirective &&
-      // If there is an unescaped newline between this line and the next, the
-      // next line starts a new preprocessor directive.
-      !NextLine->First->HasUnescapedNewline;
+      InPPDirective &&
+      // If there is no next line, this is likely a child line and the parent
+      // continues the preprocessor directive.
+      (!NextLine ||
+       (NextLine->InPPDirective &&
+        // If there is an unescaped newline between this line and the next, the
+        // next line starts a new preprocessor directive.
+        !NextLine->First->HasUnescapedNewline));
   return Style.ColumnLimit - (ContinuesPPDirective ? 2 : 0);
 }
 
index 4684d1d746b5561cbebbba505ee88a308cf355db..5100c8e9304c975dcab142f26766a8e2e7b716dd 100644 (file)
@@ -3303,6 +3303,15 @@ TEST_F(FormatTest, FormatNestedBlocksInMacros) {
             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
                    "          {  int   i;  int  j;   })",
                    getGoogleStyle()));
+
+  EXPECT_EQ("#define A                                       \\\n"
+            "  [] {                                          \\\n"
+            "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
+            "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
+            "  }",
+            format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
+                   "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
+                   getGoogleStyle()));
 }
 
 TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {