[clang] [clang][transformer] Make `describe()` terser for `NamedDecl`s. (PR #108215)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 11 05:52:58 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Clement Courbet (legrosbuffle)
<details>
<summary>Changes</summary>
Right now `describe()`ing a `FunctionDecl` dups the whole code of the function. Dump only its name.
---
Full diff: https://github.com/llvm/llvm-project/pull/108215.diff
2 Files Affected:
- (modified) clang/lib/Tooling/Transformer/Stencil.cpp (+7-1)
- (modified) clang/unittests/Tooling/StencilTest.cpp (+22)
``````````diff
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) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/108215
More information about the cfe-commits
mailing list