[PATCH] D47577: [clang-format] Separate block comments with CRLF correctly

Attila via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu May 31 02:35:27 PDT 2018


Uran198 created this revision.
Uran198 added a reviewer: alexfh.
Herald added subscribers: cfe-commits, klimek.

When formatting the following string:

  "/*\r\n"
  " * Comment with\r\n"
  "    \r\n"
  " * blanks.\r\n"
  " */\r\n"

clang-format produced:

  "/*\r\n"
  " * Comment with\r\n"
  "    \r\r\n"
  " * blanks.\r\n"
  " */\r\n"

And when formatting

  "#define A(\\\r\n"
  "    x) /* \\\r\n"
  "a comment \\\r\n"
  "inside */ \\\r\n"
  "   f();"

with line length 17, clang-format produced:

  "#define A(      \\\r"
  "    x) /* \\     \\\r"
  "a comment     \\ \\\r"
  "inside */       \\\r"
  "   f();"

So in one case it added additional `\r` instead of replacing with the blank
line and in another it added additional newline escape character `\`.

After the change the result are respectively:

  "/*\r\n"
  " * Comment with\r\n"
  "\r\n"
  " * blanks
  .\r\
  n"
  " */\r\n"

and

  "#define A(x) /* \\\r\n"
  "  a comment     \\\r\n"
  "  inside */     \\\r\n"
  "  f();"


Repository:
  rC Clang

https://reviews.llvm.org/D47577

Files:
  lib/Format/BreakableToken.cpp
  unittests/Format/FormatTestComments.cpp


Index: unittests/Format/FormatTestComments.cpp
===================================================================
--- unittests/Format/FormatTestComments.cpp
+++ unittests/Format/FormatTestComments.cpp
@@ -472,6 +472,30 @@
                      "  int jjj; /*b*/");
 }
 
+TEST_F(FormatTestComments, BlockCommentsWithCLRF) {
+  EXPECT_EQ("/*\r\n"
+            " * Comment with\r\n"
+            "\r\n"
+            " * blanks.\r\n"
+            " */\r\n"
+            "void f() {}",
+            format("/* \r\n"
+                    " * Comment with\r\n"
+                    "     \r\n"
+                    " * blanks.\r\n"
+                    " */\r\n"
+                    "void f() {}"));
+  EXPECT_EQ("#define A(x) /* \\\r\n"
+            "  a comment     \\\r\n"
+            "  inside */     \\\r\n"
+            "  f();",
+            format("#define A(x) /* \\\r\n"
+                   "  a comment     \\\r\n"
+                   "  inside */     \\\r\n"
+                   "  f();",
+                   getLLVMStyleWithColumns(17)));
+}
+
 TEST_F(FormatTestComments, AlignsBlockComments) {
   EXPECT_EQ("/*\n"
             " * Really multi-line\n"
Index: lib/Format/BreakableToken.cpp
===================================================================
--- lib/Format/BreakableToken.cpp
+++ lib/Format/BreakableToken.cpp
@@ -323,7 +323,8 @@
 
   StringRef TokenText(Tok.TokenText);
   assert(TokenText.startswith("/*") && TokenText.endswith("*/"));
-  TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n");
+  TokenText.substr(2, TokenText.size() - 4)
+      .split(Lines, TokenText.count('\r') > 0 ? "\r\n" : "\n");
 
   int IndentDelta = StartColumn - OriginalStartColumn;
   Content.resize(Lines.size());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47577.149251.patch
Type: text/x-patch
Size: 1730 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180531/1df700c1/attachment.bin>


More information about the cfe-commits mailing list