[clang] 512ceca - [clang][transformer] Make `describe()` terser for `NamedDecl`s. (#108215)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 11 08:02:51 PDT 2024
Author: Clement Courbet
Date: 2024-09-11T17:02:47+02:00
New Revision: 512cecad4c384c84b79fea050a755cb7e46c6ac5
URL: https://github.com/llvm/llvm-project/commit/512cecad4c384c84b79fea050a755cb7e46c6ac5
DIFF: https://github.com/llvm/llvm-project/commit/512cecad4c384c84b79fea050a755cb7e46c6ac5.diff
LOG: [clang][transformer] Make `describe()` terser for `NamedDecl`s. (#108215)
Right now `describe()`ing a `FunctionDecl` dups the whole code of the
function. Dump only its name.
Added:
Modified:
clang/lib/Tooling/Transformer/Stencil.cpp
clang/unittests/Tooling/StencilTest.cpp
Removed:
################################################################################
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