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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/lib/Format/NamespaceEndCommentsFixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)? *"
Expand All @@ -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;
Expand Down
57 changes: 21 additions & 36 deletions clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down