From d2e32fa493a272c21dee2c6cbf52e501f9ee3908 Mon Sep 17 00:00:00 2001 From: Yitzhak Mandelbaum Date: Wed, 7 Jul 2021 14:26:06 +0000 Subject: [PATCH] [libTooling] Add support for implicit `this` to `buildAddressOf`. Changes `buildAddressOf` to return `this` when given an implicit `this` expression. Differential Revision: https://reviews.llvm.org/D105551 --- .../Tooling/Transformer/SourceCodeBuilders.cpp | 2 ++ .../Tooling/SourceCodeBuildersTest.cpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/clang/lib/Tooling/Transformer/SourceCodeBuilders.cpp b/clang/lib/Tooling/Transformer/SourceCodeBuilders.cpp index 56ec45e8fd1d..a1c99b60216b 100644 --- a/clang/lib/Tooling/Transformer/SourceCodeBuilders.cpp +++ b/clang/lib/Tooling/Transformer/SourceCodeBuilders.cpp @@ -93,6 +93,8 @@ tooling::buildDereference(const Expr &E, const ASTContext &Context) { llvm::Optional tooling::buildAddressOf(const Expr &E, const ASTContext &Context) { + if (E.isImplicitCXXThis()) + return std::string("this"); if (const auto *Op = dyn_cast(&E)) if (Op->getOpcode() == UO_Deref) { // Strip leading '*'. diff --git a/clang/unittests/Tooling/SourceCodeBuildersTest.cpp b/clang/unittests/Tooling/SourceCodeBuildersTest.cpp index b6f6aba63e2f..ce99d1e7f521 100644 --- a/clang/unittests/Tooling/SourceCodeBuildersTest.cpp +++ b/clang/unittests/Tooling/SourceCodeBuildersTest.cpp @@ -172,6 +172,24 @@ TEST(SourceCodeBuildersTest, BuildAddressOfBinaryOperation) { testBuilder(buildAddressOf, "S x; x + x;", "&(x + x)"); } +TEST(SourceCodeBuildersTest, BuildAddressOfImplicitThis) { + StringRef Snippet = R"cc( + struct Struct { + void foo() {} + void bar() { + foo(); + } + }; + )cc"; + auto StmtMatch = matchStmt( + Snippet, + cxxMemberCallExpr(onImplicitObjectArgument(cxxThisExpr().bind("expr")))); + ASSERT_TRUE(StmtMatch); + EXPECT_THAT(buildAddressOf(*StmtMatch->Result.Nodes.getNodeAs("expr"), + *StmtMatch->Result.Context), + ValueIs(std::string("this"))); +} + TEST(SourceCodeBuildersTest, BuildDereferencePointer) { testBuilder(buildDereference, "S *x; x;", "*x"); }