[PATCH] D122230: [clang-format] don't break up #-style comment sections

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 22 07:03:04 PDT 2022


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122230.417286.patch
Type: text/x-patch
Size: 1973 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220322/3f5bc049/attachment.bin>


More information about the cfe-commits mailing list