[PATCH] D121451: [clang-format] Add space to comments starting with '#'.

Marek Kurdej via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 11 01:32:38 PST 2022


curdeius created this revision.
curdeius added reviewers: MyDeveloperDay, HazardyKnusperkeks, owenpan.
Herald added a project: All.
curdeius requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Fixes https://github.com/llvm/llvm-project/issues/35116.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D121451

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


Index: clang/unittests/Format/FormatTestComments.cpp
===================================================================
--- clang/unittests/Format/FormatTestComments.cpp
+++ clang/unittests/Format/FormatTestComments.cpp
@@ -91,6 +91,9 @@
                "// line 2\n"
                "void f() {}\n");
 
+  EXPECT_EQ("// comment\n", format("//comment\n"));
+  EXPECT_EQ("// #comment\n", format("//#comment\n"));
+
   EXPECT_EQ("// comment\n"
             "// clang-format on\n",
             format("//comment\n"
Index: clang/lib/Format/BreakableToken.cpp
===================================================================
--- clang/lib/Format/BreakableToken.cpp
+++ clang/lib/Format/BreakableToken.cpp
@@ -779,11 +779,14 @@
         const char FirstCommentChar = Lines[i][IndentPrefix.size()];
         const unsigned FirstCharByteSize =
             encoding::getCodePointNumBytes(FirstCommentChar, Encoding);
-        return encoding::columnWidth(
-                   Lines[i].substr(IndentPrefix.size(), FirstCharByteSize),
-                   Encoding) == 1 &&
-               (FirstCommentChar == '\\' || isPunctuation(FirstCommentChar) ||
-                isHorizontalWhitespace(FirstCommentChar));
+        if (encoding::columnWidth(
+                Lines[i].substr(IndentPrefix.size(), FirstCharByteSize),
+                Encoding) != 1)
+          return false;
+        if (FirstCommentChar == '#')
+          return false;
+        return FirstCommentChar == '\\' || isPunctuation(FirstCommentChar) ||
+               isHorizontalWhitespace(FirstCommentChar);
       };
 
       // On the first line of the comment section we calculate how many spaces


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121451.414613.patch
Type: text/x-patch
Size: 1668 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220311/ebc8ea94/attachment.bin>


More information about the cfe-commits mailing list