krasimir created this revision.
Herald added a project: All.
krasimir requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Follow-up from 
https://github.com/llvm/llvm-project/commit/36d13d3f8adb3d1a6bae71370afa23d11a94dc78.

Restore the old behavior in situations where we use # as comments and
long strings of #'s for comment sections.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122230

Files:
  clang/lib/Format/BreakableToken.cpp
  clang/unittests/Format/FormatTestTextProto.cpp


Index: clang/unittests/Format/FormatTestTextProto.cpp
===================================================================
--- clang/unittests/Format/FormatTestTextProto.cpp
+++ clang/unittests/Format/FormatTestTextProto.cpp
@@ -408,6 +408,23 @@
                    "dd: 100\n"
                    "#### another quadriple-hash comment\n",
                    Style));
+
+  // Ensure we support a common pattern for naming sections.
+  EXPECT_EQ("##############\n"
+            "# section name\n"
+            "##############",
+            format("##############\n"
+                   "# section name\n"
+                   "##############",
+                   Style));
+
+  EXPECT_EQ("///////////////\n"
+            "// section name\n"
+            "///////////////",
+            format("///////////////\n"
+                   "// section name\n"
+                   "///////////////",
+                   Style));
 }
 
 TEST_F(FormatTestTextProto, FormatsExtensions) {
Index: clang/lib/Format/BreakableToken.cpp
===================================================================
--- clang/lib/Format/BreakableToken.cpp
+++ clang/lib/Format/BreakableToken.cpp
@@ -785,7 +785,16 @@
                 Lines[i].substr(IndentPrefix.size(), FirstCharByteSize),
                 Encoding) != 1)
           return false;
-        if (FirstCommentChar == '#')
+        // In C-like comments, add a space before #. For example this is useful
+        // to preserve the relative indentation when commenting out code with
+        // #includes.
+        //
+        // In languages using # as the comment leader such as proto, don't
+        // add a space to support patterns like:
+        // #########
+        // # section
+        // #########
+        if (FirstCommentChar == '#' && !TokenText.startswith("#"))
           return false;
         return FirstCommentChar == '\\' || isPunctuation(FirstCommentChar) ||
                isHorizontalWhitespace(FirstCommentChar);


Index: clang/unittests/Format/FormatTestTextProto.cpp
===================================================================
--- clang/unittests/Format/FormatTestTextProto.cpp
+++ clang/unittests/Format/FormatTestTextProto.cpp
@@ -408,6 +408,23 @@
                    "dd: 100\n"
                    "#### another quadriple-hash comment\n",
                    Style));
+
+  // Ensure we support a common pattern for naming sections.
+  EXPECT_EQ("##############\n"
+            "# section name\n"
+            "##############",
+            format("##############\n"
+                   "# section name\n"
+                   "##############",
+                   Style));
+
+  EXPECT_EQ("///////////////\n"
+            "// section name\n"
+            "///////////////",
+            format("///////////////\n"
+                   "// section name\n"
+                   "///////////////",
+                   Style));
 }
 
 TEST_F(FormatTestTextProto, FormatsExtensions) {
Index: clang/lib/Format/BreakableToken.cpp
===================================================================
--- clang/lib/Format/BreakableToken.cpp
+++ clang/lib/Format/BreakableToken.cpp
@@ -785,7 +785,16 @@
                 Lines[i].substr(IndentPrefix.size(), FirstCharByteSize),
                 Encoding) != 1)
           return false;
-        if (FirstCommentChar == '#')
+        // In C-like comments, add a space before #. For example this is useful
+        // to preserve the relative indentation when commenting out code with
+        // #includes.
+        //
+        // In languages using # as the comment leader such as proto, don't
+        // add a space to support patterns like:
+        // #########
+        // # section
+        // #########
+        if (FirstCommentChar == '#' && !TokenText.startswith("#"))
           return false;
         return FirstCommentChar == '\\' || isPunctuation(FirstCommentChar) ||
                isHorizontalWhitespace(FirstCommentChar);
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D122230: [clang-... Krasimir Georgiev via Phabricator via cfe-commits

Reply via email to