Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2023
Merged

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Sep 26, 2023

Fixed #67407.

@llvmbot
Copy link
Member

llvmbot commented Sep 26, 2023

@llvm/pr-subscribers-clang-format

Changes

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

@owenca owenca merged commit f2c97ff into llvm:main Sep 26, 2023
@owenca owenca deleted the 67407 branch September 26, 2023 21:22
legrosbuffle pushed a commit to legrosbuffle/llvm-project that referenced this pull request Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-format] does not handle well concatenated namespaces closing comment if inline namespace is used
3 participants