Skip to content

Commit 063c42e

Browse files
committed
[clang-format] Handle NamespaceMacro string arg for FixNamespaceComments
Fixes llvm#63795. Differential Revision: https://reviews.llvm.org/D157568
1 parent c9d0d15 commit 063c42e

File tree

2 files changed

+41
-43
lines changed

2 files changed

+41
-43
lines changed

clang/lib/Format/NamespaceEndCommentsFixer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName,
174174
llvm::Regex::IgnoreCase);
175175
static const llvm::Regex NamespaceMacroCommentPattern =
176176
llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
177-
"([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*)\\)\\.? *(\\*/)?$",
177+
"([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*|\".+\")\\)\\.? *(\\*/)?$",
178178
llvm::Regex::IgnoreCase);
179179

180180
SmallVector<StringRef, 8> Groups;

clang/unittests/Format/NamespaceEndCommentsFixerTest.cpp

+40-42
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ class NamespaceEndCommentsFixerTest : public ::testing::Test {
4040
Code,
4141
/*Ranges=*/{1, tooling::Range(0, Code.size())}, Style);
4242
}
43+
44+
bool isFormatted(StringRef Code, const std::vector<tooling::Range> &Ranges,
45+
const FormatStyle &Style = getLLVMStyle()) const {
46+
return clang::format::fixNamespaceEndComments(Style, Code, Ranges,
47+
"<stdin>")
48+
.empty();
49+
}
50+
51+
bool isFormatted(StringRef Code,
52+
const FormatStyle &Style = getLLVMStyle()) const {
53+
return isFormatted(Code, {1, tooling::Range(0, Code.size())}, Style);
54+
}
4355
};
4456

4557
TEST_F(NamespaceEndCommentsFixerTest, AddsEndComment) {
@@ -688,48 +700,34 @@ TEST_F(NamespaceEndCommentsFixerTest, KeepsValidMacroEndComment) {
688700
FormatStyle Style = getLLVMStyle();
689701
Style.NamespaceMacros.push_back("TESTSUITE");
690702

691-
EXPECT_EQ("TESTSUITE() {\n"
692-
"int i;\n"
693-
"} // end anonymous TESTSUITE()",
694-
fixNamespaceEndComments("TESTSUITE() {\n"
695-
"int i;\n"
696-
"} // end anonymous TESTSUITE()",
697-
Style));
698-
EXPECT_EQ("TESTSUITE(A) {\n"
699-
"int i;\n"
700-
"} /* end of TESTSUITE(A) */",
701-
fixNamespaceEndComments("TESTSUITE(A) {\n"
702-
"int i;\n"
703-
"} /* end of TESTSUITE(A) */",
704-
Style));
705-
EXPECT_EQ("TESTSUITE(A) {\n"
706-
"int i;\n"
707-
"} // TESTSUITE(A)",
708-
fixNamespaceEndComments("TESTSUITE(A) {\n"
709-
"int i;\n"
710-
"} // TESTSUITE(A)",
711-
Style));
712-
EXPECT_EQ("TESTSUITE(A::B) {\n"
713-
"int i;\n"
714-
"} // end TESTSUITE(A::B)",
715-
fixNamespaceEndComments("TESTSUITE(A::B) {\n"
716-
"int i;\n"
717-
"} // end TESTSUITE(A::B)",
718-
Style));
719-
EXPECT_EQ("TESTSUITE(A) {\n"
720-
"int i;\n"
721-
"}; // end TESTSUITE(A)",
722-
fixNamespaceEndComments("TESTSUITE(A) {\n"
723-
"int i;\n"
724-
"}; // end TESTSUITE(A)",
725-
Style));
726-
EXPECT_EQ("TESTSUITE() {\n"
727-
"int i;\n"
728-
"}; /* unnamed TESTSUITE() */",
729-
fixNamespaceEndComments("TESTSUITE() {\n"
730-
"int i;\n"
731-
"}; /* unnamed TESTSUITE() */",
732-
Style));
703+
EXPECT_TRUE(isFormatted("TESTSUITE() {\n"
704+
"int i;\n"
705+
"} // end anonymous TESTSUITE()",
706+
Style));
707+
EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n"
708+
"int i;\n"
709+
"} /* end of TESTSUITE(A) */",
710+
Style));
711+
EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n"
712+
"int i;\n"
713+
"} // TESTSUITE(A)",
714+
Style));
715+
EXPECT_TRUE(isFormatted("TESTSUITE(A::B) {\n"
716+
"int i;\n"
717+
"} // end TESTSUITE(A::B)",
718+
Style));
719+
EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n"
720+
"int i;\n"
721+
"}; // end TESTSUITE(A)",
722+
Style));
723+
EXPECT_TRUE(isFormatted("TESTSUITE() {\n"
724+
"int i;\n"
725+
"}; /* unnamed TESTSUITE() */",
726+
Style));
727+
EXPECT_TRUE(isFormatted("TESTSUITE(\"foo\") {\n"
728+
"int i;\n"
729+
"} // TESTSUITE(\"foo\")",
730+
Style));
733731
}
734732

735733
TEST_F(NamespaceEndCommentsFixerTest, UpdatesInvalidEndLineComment) {

0 commit comments

Comments
 (0)