Skip to content

Commit

Permalink
[libTooling] Add support for implicit this to buildAddressOf.
Browse files Browse the repository at this point in the history
Changes `buildAddressOf` to return `this` when given an implicit `this` expression.

Differential Revision: https://reviews.llvm.org/D105551
  • Loading branch information
ymand committed Jul 7, 2021
1 parent f5603aa commit d2e32fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/lib/Tooling/Transformer/SourceCodeBuilders.cpp
Expand Up @@ -93,6 +93,8 @@ tooling::buildDereference(const Expr &E, const ASTContext &Context) {

llvm::Optional<std::string> tooling::buildAddressOf(const Expr &E,
const ASTContext &Context) {
if (E.isImplicitCXXThis())
return std::string("this");
if (const auto *Op = dyn_cast<UnaryOperator>(&E))
if (Op->getOpcode() == UO_Deref) {
// Strip leading '*'.
Expand Down
18 changes: 18 additions & 0 deletions clang/unittests/Tooling/SourceCodeBuildersTest.cpp
Expand Up @@ -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>("expr"),
*StmtMatch->Result.Context),
ValueIs(std::string("this")));
}

TEST(SourceCodeBuildersTest, BuildDereferencePointer) {
testBuilder(buildDereference, "S *x; x;", "*x");
}
Expand Down

0 comments on commit d2e32fa

Please sign in to comment.