[PATCH] D156065: [clang-format] Insert namespace comments with leading spaces

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 23 13:57:09 PDT 2023


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

Insert missing `namespace` comments with `SpacesBeforeTrailingComments` leading spaces.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156065

Files:
  clang/lib/Format/NamespaceEndCommentsFixer.cpp
  clang/unittests/Format/FormatTestComments.cpp
  clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp


Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
===================================================================
--- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -25,8 +25,10 @@
                           const FormatStyle &Style = getLLVMStyle()) {
     LLVM_DEBUG(llvm::errs() << "---\n");
     LLVM_DEBUG(llvm::errs() << Code << "\n\n");
+    FormatStyle S = Style;
+    S.SpacesBeforeTrailingComments = 0;
     tooling::Replacements Replaces =
-        clang::format::fixNamespaceEndComments(Style, Code, Ranges, "<stdin>");
+        clang::format::fixNamespaceEndComments(S, Code, Ranges, "<stdin>");
     auto Result = applyAllReplacements(Code, Replaces);
     EXPECT_TRUE(static_cast<bool>(Result));
     LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
Index: clang/unittests/Format/FormatTestComments.cpp
===================================================================
--- clang/unittests/Format/FormatTestComments.cpp
+++ clang/unittests/Format/FormatTestComments.cpp
@@ -3043,6 +3043,16 @@
                    "// comment",
                    Style));
 
+  verifyFormat("namespace ns {\n"
+               "int i;\n"
+               "int j;\n"
+               "} // namespace ns",
+               "namespace ns {\n"
+               "int i;\n"
+               "int j;\n"
+               "}",
+               Style);
+
   // Allow to keep 2 empty lines
   Style.MaxEmptyLinesToKeep = 2;
   EXPECT_EQ("// do not touch\n"
Index: clang/lib/Format/NamespaceEndCommentsFixer.cpp
===================================================================
--- clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -360,8 +360,12 @@
                               Style.SpacesInLineCommentPrefix.Minimum);
     if (!hasEndComment(EndCommentPrevTok)) {
       bool isShort = I - StartLineIndex <= Style.ShortNamespaceLines + 1;
-      if (!isShort)
-        addEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);
+      if (!isShort) {
+        addEndComment(EndCommentPrevTok,
+                      std::string(Style.SpacesBeforeTrailingComments, ' ') +
+                          EndCommentText,
+                      SourceMgr, &Fixes);
+      }
     } else if (!validEndComment(EndCommentPrevTok, NamespaceName,
                                 NamespaceTok)) {
       updateEndComment(EndCommentPrevTok, EndCommentText, SourceMgr, &Fixes);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156065.543324.patch
Type: text/x-patch
Size: 2507 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230723/43c4038d/attachment.bin>


More information about the cfe-commits mailing list