[PATCH] D100450: [libTooling] Add smart pointer support to the `access` Stencil
Shu-Chun Weng via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 14 11:11:20 PDT 2021
This revision was automatically updated to reflect the committed changes.
Closed by commit rG1c5717225e89: [libTooling] Add smart pointer support to the `access` Stencil (authored by scw).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D100450/new/
https://reviews.llvm.org/D100450
Files:
clang/lib/Tooling/Transformer/Stencil.cpp
clang/unittests/Tooling/StencilTest.cpp
Index: clang/unittests/Tooling/StencilTest.cpp
===================================================================
--- clang/unittests/Tooling/StencilTest.cpp
+++ clang/unittests/Tooling/StencilTest.cpp
@@ -392,6 +392,37 @@
testExpr(Id, Snippet, access(Id, "field"), "x->field");
}
+TEST_F(StencilTest, AccessOpSmartPointer) {
+ StringRef Snippet = R"cc(
+ Smart x;
+ x;
+ )cc";
+ StringRef Id = "id";
+ testExpr(Id, Snippet, access(Id, "field"), "x->field");
+}
+
+TEST_F(StencilTest, AccessOpSmartPointerDereference) {
+ StringRef Snippet = R"cc(
+ Smart x;
+ *x;
+ )cc";
+ StringRef Id = "id";
+ testExpr(Id, Snippet, access(Id, "field"), "(*x).field");
+}
+
+TEST_F(StencilTest, AccessOpSmartPointerMemberCall) {
+ StringRef Snippet = R"cc(
+ Smart x;
+ x->Field;
+ )cc";
+ StringRef Id = "id";
+ auto StmtMatch =
+ matchStmt(Snippet, memberExpr(hasObjectExpression(expr().bind(Id))));
+ ASSERT_TRUE(StmtMatch);
+ EXPECT_THAT_EXPECTED(access(Id, "field")->eval(StmtMatch->Result),
+ HasValue("x->field"));
+}
+
TEST_F(StencilTest, AccessOpExplicitThis) {
using clang::ast_matchers::hasObjectExpression;
using clang::ast_matchers::memberExpr;
Index: clang/lib/Tooling/Transformer/Stencil.cpp
===================================================================
--- clang/lib/Tooling/Transformer/Stencil.cpp
+++ clang/lib/Tooling/Transformer/Stencil.cpp
@@ -323,10 +323,23 @@
return llvm::make_error<StringError>(errc::invalid_argument,
"Id not bound: " + Data.BaseId);
if (!E->isImplicitCXXThis()) {
- if (llvm::Optional<std::string> S =
- E->getType()->isAnyPointerType()
- ? tooling::buildArrow(*E, *Match.Context)
- : tooling::buildDot(*E, *Match.Context))
+ llvm::Optional<std::string> S;
+ if (E->getType()->isAnyPointerType() ||
+ isSmartPointerType(E->getType(), *Match.Context)) {
+ // Strip off any operator->. This can only occur inside an actual arrow
+ // member access, so we treat it as equivalent to an actual object
+ // expression.
+ if (const auto *OpCall = dyn_cast<clang::CXXOperatorCallExpr>(E)) {
+ if (OpCall->getOperator() == clang::OO_Arrow &&
+ OpCall->getNumArgs() == 1) {
+ E = OpCall->getArg(0);
+ }
+ }
+ S = tooling::buildArrow(*E, *Match.Context);
+ } else {
+ S = tooling::buildDot(*E, *Match.Context);
+ }
+ if (S.hasValue())
*Result += *S;
else
return llvm::make_error<StringError>(
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100450.337498.patch
Type: text/x-patch
Size: 2596 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210414/f2e3b656/attachment.bin>
More information about the cfe-commits
mailing list