[clang] [clang-format] Fix a bug in NamespaceEndCommentsFixer (PR #67422)

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 26 05:23:09 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

<details>
<summary>Changes</summary>

Fixed #<!-- -->67407.

---
Full diff: https://github.com/llvm/llvm-project/pull/67422.diff


2 Files Affected:

- (modified) clang/lib/Format/NamespaceEndCommentsFixer.cpp (+2-2) 
- (modified) clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp (+21-36) 


``````````diff
diff --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
index 4d3bd3b33f0f113..aed31db87495406 100644
--- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -170,7 +170,7 @@ bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName,
   // Valid namespace end comments don't need to be edited.
   static const llvm::Regex NamespaceCommentPattern =
       llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
-                  "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
+                  "namespace( +([a-zA-Z0-9:_ ]+))?\\.? *(\\*/)?$",
                   llvm::Regex::IgnoreCase);
   static const llvm::Regex NamespaceMacroCommentPattern =
       llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
@@ -189,7 +189,7 @@ bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName,
     // Comment does not match regex.
     return false;
   }
-  StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5] : "";
+  StringRef NamespaceNameInComment = Groups.size() > 5 ? Groups[5].rtrim() : "";
   // Anonymous namespace comments must not mention a namespace name.
   if (NamespaceName.empty() && !NamespaceNameInComment.empty())
     return false;
diff --git a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
index ec335c985ebba20..1ebcba551e4c9d2 100644
--- a/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
+++ b/clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
@@ -658,42 +658,27 @@ TEST_F(NamespaceEndCommentsFixerTest,
 }
 
 TEST_F(NamespaceEndCommentsFixerTest, KeepsValidEndComment) {
-  EXPECT_EQ("namespace {\n"
-            "int i;\n"
-            "} // end anonymous namespace",
-            fixNamespaceEndComments("namespace {\n"
-                                    "int i;\n"
-                                    "} // end anonymous namespace"));
-  EXPECT_EQ("namespace A {\n"
-            "int i;\n"
-            "} /* end of namespace A */",
-            fixNamespaceEndComments("namespace A {\n"
-                                    "int i;\n"
-                                    "} /* end of namespace A */"));
-  EXPECT_EQ("namespace A {\n"
-            "int i;\n"
-            "}   //   namespace A",
-            fixNamespaceEndComments("namespace A {\n"
-                                    "int i;\n"
-                                    "}   //   namespace A"));
-  EXPECT_EQ("namespace A::B {\n"
-            "int i;\n"
-            "} // end namespace A::B",
-            fixNamespaceEndComments("namespace A::B {\n"
-                                    "int i;\n"
-                                    "} // end namespace A::B"));
-  EXPECT_EQ("namespace A {\n"
-            "int i;\n"
-            "}; // end namespace A",
-            fixNamespaceEndComments("namespace A {\n"
-                                    "int i;\n"
-                                    "}; // end namespace A"));
-  EXPECT_EQ("namespace {\n"
-            "int i;\n"
-            "}; /* unnamed namespace */",
-            fixNamespaceEndComments("namespace {\n"
-                                    "int i;\n"
-                                    "}; /* unnamed namespace */"));
+  EXPECT_TRUE(isFormatted("namespace {\n"
+                          "int i;\n"
+                          "} // end anonymous namespace"));
+  EXPECT_TRUE(isFormatted("namespace A {\n"
+                          "int i;\n"
+                          "} /* end of namespace A */"));
+  EXPECT_TRUE(isFormatted("namespace A {\n"
+                          "int i;\n"
+                          "}   //   namespace A"));
+  EXPECT_TRUE(isFormatted("namespace A::B {\n"
+                          "int i;\n"
+                          "} // end namespace A::B"));
+  EXPECT_TRUE(isFormatted("namespace A {\n"
+                          "int i;\n"
+                          "}; // end namespace A"));
+  EXPECT_TRUE(isFormatted("namespace {\n"
+                          "int i;\n"
+                          "}; /* unnamed namespace */"));
+  EXPECT_TRUE(isFormatted("namespace a::inline b {\n"
+                          "int c;\n"
+                          "}; // namespace a::inline b"));
 }
 
 TEST_F(NamespaceEndCommentsFixerTest, KeepsValidMacroEndComment) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/67422


More information about the cfe-commits mailing list