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());


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());
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D47577: [clang... Attila via Phabricator via cfe-commits

Reply via email to