[PATCH] Correctly handle escaped newlines when the next token starts without a space.

Manuel Klimek klimek at google.com
Fri Apr 11 03:01:42 PDT 2014


Hi djasper,

We will need this to correctly handle conflict markers inside macro
definitions. Currently the tests as written fail directly when inserting
a space after the \\\n.

http://reviews.llvm.org/D3351

Files:
  lib/Format/Format.cpp
  unittests/Format/FormatTest.cpp

Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -1329,7 +1329,7 @@
     // FIXME: Add a more explicit test.
     while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' &&
            FormatTok->TokenText[1] == '\n') {
-      // FIXME: ++FormatTok->NewlinesBefore is missing...
+      ++FormatTok->NewlinesBefore;
       WhitespaceLength += 2;
       Column = 0;
       FormatTok->TokenText = FormatTok->TokenText.substr(2);
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -746,7 +746,7 @@
                    "int a;    // This is unrelated\n"
                    "};"));
 
-  EXPECT_EQ("int i; // single line trailing comment",
+  EXPECT_EQ("int i;\n// single line trailing comment",
             format("int i;\\\n// single line trailing comment"));
 
   verifyGoogleFormat("int a;  // Trailing comment.");
@@ -931,7 +931,7 @@
 TEST_F(FormatTest, UnderstandsBlockComments) {
   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
-  EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
+  EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n  /* Trailing comment for aa... */\n"
             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
                    "/* Trailing comment for aa... */\n"
@@ -1057,7 +1057,7 @@
             "// h i j k",
             format("// a b c d e f  g h i j k",
                    getLLVMStyleWithColumns(10)));
-  EXPECT_EQ("// a b c d\n"
+  EXPECT_EQ("\n// a b c d\n"
             "// e f  g\n"
             "// h i j k",
             format("\\\n// a b c d e f  g h i j k",
@@ -1246,7 +1246,7 @@
             " */",
             format("/* a b c d e f  g h i j k */",
                    getLLVMStyleWithColumns(10)));
-  EXPECT_EQ("/* a b c d\n"
+  EXPECT_EQ("\n/* a b c d\n"
             " * e f  g\n"
             " * h i j k\n"
             " */",
@@ -2462,11 +2462,11 @@
   EXPECT_EQ(
       "#define A \\\n  int i;  \\\n  int j;",
       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
-  EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
+  EXPECT_EQ("\ntemplate <class T> f();", format("\\\ntemplate <class T> f();"));
 }
 
 TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
-  EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
+  EXPECT_EQ("\n/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
 }
 
 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
@@ -6349,7 +6349,7 @@
   EXPECT_EQ("\"some text \"\n"
             "\"other\";",
             format("\"some text other\";", getLLVMStyleWithColumns(12)));
-  EXPECT_EQ("\"some text \"\n"
+  EXPECT_EQ("\n\"some text \"\n"
             "\"other\";",
             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
   EXPECT_EQ(
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3351.1.patch
Type: text/x-patch
Size: 3120 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140411/a27cf7eb/attachment.bin>


More information about the cfe-commits mailing list