[PATCH] D120188: Fix extraneous whitespace addition in line comments on clang-format directives

Luis Penagos via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 19 10:40:09 PST 2022


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

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120188

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,12 @@
                "// line 2\n"
                "void f() {}\n");
 
+  EXPECT_EQ("// comment\n"
+            "// clang-format on\n",
+            format("//comment\n"
+                   "// clang-format on\n",
+                   getLLVMStyleWithColumns(20)));
+
   verifyFormat("void f() {\n"
                "  // Doesn't do anything\n"
                "}");
Index: clang/lib/Format/BreakableToken.cpp
===================================================================
--- clang/lib/Format/BreakableToken.cpp
+++ clang/lib/Format/BreakableToken.cpp
@@ -816,9 +816,10 @@
         assert(Lines[i].size() > IndentPrefix.size());
         const auto FirstNonSpace = Lines[i][IndentPrefix.size()];
         const auto AllowsSpaceChange =
-            SpacesInPrefix != 0 ||
-            (!NoSpaceBeforeFirstCommentChar() ||
-             (FirstNonSpace == '}' && FirstLineSpaceChange != 0));
+            (!LineTok || !switchesFormatting(*LineTok)) &&
+            (SpacesInPrefix != 0 ||
+             (!NoSpaceBeforeFirstCommentChar() ||
+              (FirstNonSpace == '}' && FirstLineSpaceChange != 0)));
 
         if (PrefixSpaceChange[i] > 0 && AllowsSpaceChange) {
           Prefix[i] = IndentPrefix.str();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120188.410090.patch
Type: text/x-patch
Size: 1454 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220219/389b1d95/attachment.bin>


More information about the cfe-commits mailing list