Skip to content

Commit 512ceca

Browse files
authored
[clang][transformer] Make describe() terser for NamedDecls. (#108215)
Right now `describe()`ing a `FunctionDecl` dups the whole code of the function. Dump only its name.
1 parent 2a4992e commit 512ceca

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

clang/lib/Tooling/Transformer/Stencil.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ static Error printNode(StringRef Id, const MatchFinder::MatchResult &Match,
5050
auto NodeOrErr = getNode(Match.Nodes, Id);
5151
if (auto Err = NodeOrErr.takeError())
5252
return Err;
53-
NodeOrErr->print(Os, PrintingPolicy(Match.Context->getLangOpts()));
53+
const PrintingPolicy PP(Match.Context->getLangOpts());
54+
if (const auto *ND = NodeOrErr->get<NamedDecl>()) {
55+
// For NamedDecls, we can do a better job than printing the whole thing.
56+
ND->getNameForDiagnostic(Os, PP, false);
57+
} else {
58+
NodeOrErr->print(Os, PP);
59+
}
5460
*Result += Output;
5561
return Error::success();
5662
}

clang/unittests/Tooling/StencilTest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,28 @@ TEST_F(StencilTest, DescribeAnonNamespaceType) {
565565
HasValue(std::string(Expected)));
566566
}
567567

568+
TEST_F(StencilTest, DescribeFunction) {
569+
std::string Snippet = "int F(); F();";
570+
std::string Expected = "F";
571+
auto StmtMatch = matchStmt(Snippet, callExpr(callee(namedDecl().bind("fn"))));
572+
ASSERT_TRUE(StmtMatch);
573+
EXPECT_THAT_EXPECTED(describe("fn")->eval(StmtMatch->Result),
574+
HasValue(std::string(Expected)));
575+
}
576+
577+
TEST_F(StencilTest, DescribeImplicitOperator) {
578+
std::string Snippet = "struct Tag {}; [](Tag){};";
579+
std::string Expected = "operator()";
580+
auto StmtMatch = matchStmt(
581+
Snippet,
582+
stmt(hasDescendant(
583+
cxxMethodDecl(hasParameter(0, hasType(namedDecl(hasName("Tag")))))
584+
.bind("fn"))));
585+
ASSERT_TRUE(StmtMatch);
586+
EXPECT_THAT_EXPECTED(describe("fn")->eval(StmtMatch->Result),
587+
HasValue(std::string(Expected)));
588+
}
589+
568590
TEST_F(StencilTest, RunOp) {
569591
StringRef Id = "id";
570592
auto SimpleFn = [Id](const MatchResult &R) {

0 commit comments

Comments
 (0)