[clang] [clang][transformer] Make `describe()` terser for `NamedDecl`s. (PR #108215)
Clement Courbet via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 11 05:52:28 PDT 2024
https://github.com/legrosbuffle created https://github.com/llvm/llvm-project/pull/108215
Right now `describe()`ing a `FunctionDecl` dups the whole code of the function. Dump only its name.
>From 42f1e67d2186fd90b8fc08206b33e077912039a1 Mon Sep 17 00:00:00 2001
From: Clement Courbet <courbet at google.com>
Date: Wed, 11 Sep 2024 12:35:14 +0000
Subject: [PATCH] [clang][transformer] Make `describe()` terser for
`NamedDecl`s.
Right now `describe()`ing a `FunctionDecl` dups the whole code of the
function. Dump only its name.
---
clang/lib/Tooling/Transformer/Stencil.cpp | 8 +++++++-
clang/unittests/Tooling/StencilTest.cpp | 22 ++++++++++++++++++++++
2 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Tooling/Transformer/Stencil.cpp b/clang/lib/Tooling/Transformer/Stencil.cpp
index bc4fa6e36057c1..223fb5a7689751 100644
--- a/clang/lib/Tooling/Transformer/Stencil.cpp
+++ b/clang/lib/Tooling/Transformer/Stencil.cpp
@@ -50,7 +50,13 @@ static Error printNode(StringRef Id, const MatchFinder::MatchResult &Match,
auto NodeOrErr = getNode(Match.Nodes, Id);
if (auto Err = NodeOrErr.takeError())
return Err;
- NodeOrErr->print(Os, PrintingPolicy(Match.Context->getLangOpts()));
+ const PrintingPolicy PP(Match.Context->getLangOpts());
+ if (const auto *ND = NodeOrErr->get<NamedDecl>()) {
+ // For NamedDecls, we can do a better job than printing the whole thing.
+ ND->getNameForDiagnostic(Os, PP, false);
+ } else {
+ NodeOrErr->print(Os, PP);
+ }
*Result += Output;
return Error::success();
}
diff --git a/clang/unittests/Tooling/StencilTest.cpp b/clang/unittests/Tooling/StencilTest.cpp
index 26257cf2ca3a5f..445912a53e8b62 100644
--- a/clang/unittests/Tooling/StencilTest.cpp
+++ b/clang/unittests/Tooling/StencilTest.cpp
@@ -565,6 +565,28 @@ TEST_F(StencilTest, DescribeAnonNamespaceType) {
HasValue(std::string(Expected)));
}
+TEST_F(StencilTest, DescribeFunction) {
+ std::string Snippet = "int F(); F();";
+ std::string Expected = "F";
+ auto StmtMatch = matchStmt(Snippet, callExpr(callee(namedDecl().bind("fn"))));
+ ASSERT_TRUE(StmtMatch);
+ EXPECT_THAT_EXPECTED(describe("fn")->eval(StmtMatch->Result),
+ HasValue(std::string(Expected)));
+}
+
+TEST_F(StencilTest, DescribeImplicitOperator) {
+ std::string Snippet = "struct Tag {}; [](Tag){};";
+ std::string Expected = "operator()";
+ auto StmtMatch = matchStmt(
+ Snippet,
+ stmt(hasDescendant(
+ cxxMethodDecl(hasParameter(0, hasType(namedDecl(hasName("Tag")))))
+ .bind("fn"))));
+ ASSERT_TRUE(StmtMatch);
+ EXPECT_THAT_EXPECTED(describe("fn")->eval(StmtMatch->Result),
+ HasValue(std::string(Expected)));
+}
+
TEST_F(StencilTest, RunOp) {
StringRef Id = "id";
auto SimpleFn = [Id](const MatchResult &R) {
More information about the cfe-commits
mailing list