[clang] 063c42e - [clang-format] Handle NamespaceMacro string arg for FixNamespaceComments

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 16 10:46:00 PDT 2023


Author: Owen Pan
Date: 2023-08-16T10:45:54-07:00
New Revision: 063c42e919c01d7e64c1af5a10898fc84b06dfe8

URL: https://github.com/llvm/llvm-project/commit/063c42e919c01d7e64c1af5a10898fc84b06dfe8
DIFF: https://github.com/llvm/llvm-project/commit/063c42e919c01d7e64c1af5a10898fc84b06dfe8.diff

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

Fixes #63795.

Differential Revision: https://reviews.llvm.org/D157568

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
index 32c2592834555a..4d3bd3b33f0f11 100644
--- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -174,7 +174,7 @@ bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName,
                   llvm::Regex::IgnoreCase);
   static const llvm::Regex NamespaceMacroCommentPattern =
       llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
-                  "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*)\\)\\.? *(\\*/)?$",
+                  "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*|\".+\")\\)\\.? *(\\*/)?$",
                   llvm::Regex::IgnoreCase);
 
   SmallVector<StringRef, 8> Groups;

diff  --git a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
index 65876a3c668650..ec335c985ebba2 100644
--- a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -40,6 +40,18 @@ class NamespaceEndCommentsFixerTest : public ::testing::Test {
         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, AddsEndComment) {
@@ -688,48 +700,34 @@ TEST_F(NamespaceEndCommentsFixerTest, KeepsValidMacroEndComment) {
   FormatStyle Style = getLLVMStyle();
   Style.NamespaceMacros.push_back("TESTSUITE");
 
-  EXPECT_EQ("TESTSUITE() {\n"
-            "int i;\n"
-            "} // end anonymous TESTSUITE()",
-            fixNamespaceEndComments("TESTSUITE() {\n"
-                                    "int i;\n"
-                                    "} // end anonymous TESTSUITE()",
-                                    Style));
-  EXPECT_EQ("TESTSUITE(A) {\n"
-            "int i;\n"
-            "} /* end of TESTSUITE(A) */",
-            fixNamespaceEndComments("TESTSUITE(A) {\n"
-                                    "int i;\n"
-                                    "} /* end of TESTSUITE(A) */",
-                                    Style));
-  EXPECT_EQ("TESTSUITE(A) {\n"
-            "int i;\n"
-            "}   //   TESTSUITE(A)",
-            fixNamespaceEndComments("TESTSUITE(A) {\n"
-                                    "int i;\n"
-                                    "}   //   TESTSUITE(A)",
-                                    Style));
-  EXPECT_EQ("TESTSUITE(A::B) {\n"
-            "int i;\n"
-            "} // end TESTSUITE(A::B)",
-            fixNamespaceEndComments("TESTSUITE(A::B) {\n"
-                                    "int i;\n"
-                                    "} // end TESTSUITE(A::B)",
-                                    Style));
-  EXPECT_EQ("TESTSUITE(A) {\n"
-            "int i;\n"
-            "}; // end TESTSUITE(A)",
-            fixNamespaceEndComments("TESTSUITE(A) {\n"
-                                    "int i;\n"
-                                    "}; // end TESTSUITE(A)",
-                                    Style));
-  EXPECT_EQ("TESTSUITE() {\n"
-            "int i;\n"
-            "}; /* unnamed TESTSUITE() */",
-            fixNamespaceEndComments("TESTSUITE() {\n"
-                                    "int i;\n"
-                                    "}; /* unnamed TESTSUITE() */",
-                                    Style));
+  EXPECT_TRUE(isFormatted("TESTSUITE() {\n"
+                          "int i;\n"
+                          "} // end anonymous TESTSUITE()",
+                          Style));
+  EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n"
+                          "int i;\n"
+                          "} /* end of TESTSUITE(A) */",
+                          Style));
+  EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n"
+                          "int i;\n"
+                          "}   //   TESTSUITE(A)",
+                          Style));
+  EXPECT_TRUE(isFormatted("TESTSUITE(A::B) {\n"
+                          "int i;\n"
+                          "} // end TESTSUITE(A::B)",
+                          Style));
+  EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n"
+                          "int i;\n"
+                          "}; // end TESTSUITE(A)",
+                          Style));
+  EXPECT_TRUE(isFormatted("TESTSUITE() {\n"
+                          "int i;\n"
+                          "}; /* unnamed TESTSUITE() */",
+                          Style));
+  EXPECT_TRUE(isFormatted("TESTSUITE(\"foo\") {\n"
+                          "int i;\n"
+                          "} // TESTSUITE(\"foo\")",
+                          Style));
 }
 
 TEST_F(NamespaceEndCommentsFixerTest, UpdatesInvalidEndLineComment) {


        


More information about the cfe-commits mailing list