clang-format: Don't remove newline if macro ends in access specifier.
authorDaniel Jasper <djasper@google.com>
Mon, 9 Mar 2015 08:13:55 +0000 (08:13 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 9 Mar 2015 08:13:55 +0000 (08:13 +0000)
I.e.:

  #define A public:

  // The new line before this line would be removed.
  int a;

llvm-svn: 231636

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

index 8e6809f..71e8b3f 100644 (file)
@@ -512,7 +512,8 @@ void UnwrappedLineFormatter::formatFirstToken(FormatToken &RootToken,
     ++Newlines;
 
   // Remove empty lines after access specifiers.
-  if (PreviousLine && PreviousLine->First->isAccessSpecifier())
+  if (PreviousLine && PreviousLine->First->isAccessSpecifier() &&
+      (!PreviousLine->InPPDirective || !RootToken.HasUnescapedNewline))
     Newlines = std::min(1u, Newlines);
 
   Whitespaces->replaceWhitespace(RootToken, Newlines, IndentLevel, Indent,
index fcfe977..7221ec8 100644 (file)
@@ -1930,6 +1930,30 @@ TEST_F(FormatTest, SeparatesLogicalBlocks) {
                    "\n"
                    "  void f();\n"
                    "};"));
+
+  // Even ensure proper spacing inside macros.
+  EXPECT_EQ("#define B     \\\n"
+            "  class A {   \\\n"
+            "   protected: \\\n"
+            "   public:    \\\n"
+            "    void f(); \\\n"
+            "  };",
+            format("#define B     \\\n"
+                   "  class A {   \\\n"
+                   "   protected: \\\n"
+                   "              \\\n"
+                   "   public:    \\\n"
+                   "              \\\n"
+                   "    void f(); \\\n"
+                   "  };",
+                   getGoogleStyle()));
+  // But don't remove empty lines after macros ending in access specifiers.
+  EXPECT_EQ("#define A private:\n"
+            "\n"
+            "int i;",
+            format("#define A         private:\n"
+                   "\n"
+                   "int              i;"));
 }
 
 TEST_F(FormatTest, FormatsClasses) {