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

Marek Kurdej via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 13 13:56:28 PDT 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rG36d13d3f8adb: [clang-format] Add space to comments starting with '#'. (authored by curdeius).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121451/new/

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"
@@ -3302,6 +3305,7 @@
             format(NoTextInComment, Style));
 
   Style.SpacesInLineCommentPrefix.Minimum = 0;
+  EXPECT_EQ("//#comment", format("//#comment", Style));
   EXPECT_EQ("//\n"
             "\n"
             "void foo() { //\n"
@@ -3310,6 +3314,7 @@
             format(NoTextInComment, Style));
 
   Style.SpacesInLineCommentPrefix.Minimum = 5;
+  EXPECT_EQ("//     #comment", format("//#comment", Style));
   EXPECT_EQ("//\n"
             "\n"
             "void foo() { //\n"
@@ -3463,6 +3468,7 @@
             format(Code, Style));
 
   Style.SpacesInLineCommentPrefix = {0, 0};
+  EXPECT_EQ("//#comment", format("//   #comment", Style));
   EXPECT_EQ("//Free comment without space\n"
             "\n"
             "//Free comment with 3 spaces\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.414953.patch
Type: text/x-patch
Size: 2439 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220313/ec236c1b/attachment.bin>


More information about the cfe-commits mailing list