[PATCH] D157568: [clang-format] Handle NamespaceMacro string arg for FixNamespaceComments

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 9 18:11:07 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.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157568

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


Index: clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
===================================================================
--- clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -40,8 +40,30 @@
         Code,
         /*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
   }
+
+  bool isFormatted(StringRef Code, const std::vector<tooling::Range> &Ranges,
+                   const FormatStyle &Style = getLLVMStyle()) const {
+    return clang::format::fixNamespaceEndComments(Style, Code, Ranges,
+                                                  "<stdin>")
+        .empty();
+  }
+
+  bool isFormatted(StringRef Code,
+                   const FormatStyle &Style = getLLVMStyle()) const {
+    return isFormatted(Code, {1, tooling::Range(0, Code.size())}, Style);
+  }
 };
 
+TEST_F(NamespaceEndCommentsFixerTest, IsFormatted) {
+  auto Style = getLLVMStyle();
+  Style.NamespaceMacros.push_back("SUITE");
+  EXPECT_TRUE(isFormatted("SUITE(\"foo\") {\n"
+                          "int i;\n"
+                          "int j;\n"
+                          "} // SUITE(\"foo\")",
+                          Style));
+}
+
 TEST_F(NamespaceEndCommentsFixerTest, AddsEndComment) {
   EXPECT_EQ("namespace {\n"
             "int i;\n"
Index: clang/lib/Format/NamespaceEndCommentsFixer.cpp
===================================================================
--- clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -174,7 +174,8 @@
                   llvm::Regex::IgnoreCase);
   static const llvm::Regex NamespaceMacroCommentPattern =
       llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
-                  "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*)\\)\\.? *(\\*/)?$",
+                  // NamespaceMacro arguments can also be string literals.
+                  "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*|\".+\")\\)\\.? *(\\*/)?$",
                   llvm::Regex::IgnoreCase);
 
   SmallVector<StringRef, 8> Groups;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157568.548841.patch
Type: text/x-patch
Size: 2049 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230810/53d76807/attachment.bin>


More information about the cfe-commits mailing list