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

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 26 05:21:55 PDT 2023


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

Fixed #67407.

>From 1c74983f9d2a16ab26d64624448df89e5fff87c4 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 26 Sep 2023 05:17:25 -0700
Subject: [PATCH] [clang-format] Fix a bug in NamespaceEndCommentsFixer

Fixed #67407.
---
 .../lib/Format/NamespaceEndCommentsFixer.cpp  |  4 +-
 .../Format/NamespaceEndCommentsFixerTest.cpp  | 57 +++++++------------
 2 files changed, 23 insertions(+), 38 deletions(-)

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) {



More information about the cfe-commits mailing list