[clang] 4866a6e - [clang][dataflow] Produce pointer values for callees of member operator calls.
Martin Braenne via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 24 00:12:21 PDT 2023
Author: Martin Braenne
Date: 2023-08-24T07:12:14Z
New Revision: 4866a6e1d3327a0499ef0e017a973a78d4e377bc
URL: https://github.com/llvm/llvm-project/commit/4866a6e1d3327a0499ef0e017a973a78d4e377bc
DIFF: https://github.com/llvm/llvm-project/commit/4866a6e1d3327a0499ef0e017a973a78d4e377bc.diff
LOG: [clang][dataflow] Produce pointer values for callees of member operator calls.
Calls to member operators are a special case in that their callees have pointer
type. The callees of non-operator non-static member functions are not pointers.
See the comments in the code for details.
This issue came up in the Crubit nullability check; the fact that we weren't
modeling the `PointerValue` caused non-convergence.
Reviewed By: ymandel, xazax.hun
Differential Revision: https://reviews.llvm.org/D158592
Added:
Modified:
clang/lib/Analysis/FlowSensitive/Transfer.cpp
clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/FlowSensitive/Transfer.cpp b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
index 8617cf4c8ca4a2..200a981584bb76 100644
--- a/clang/lib/Analysis/FlowSensitive/Transfer.cpp
+++ b/clang/lib/Analysis/FlowSensitive/Transfer.cpp
@@ -175,12 +175,16 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
const ValueDecl *VD = S->getDecl();
assert(VD != nullptr);
- // `DeclRefExpr`s to fields and non-static methods aren't glvalues, and
- // there's also no sensible `Value` we can assign to them, so skip them.
- if (isa<FieldDecl>(VD))
- return;
- if (auto *Method = dyn_cast<CXXMethodDecl>(VD);
- Method && !Method->isStatic())
+ // Some `DeclRefExpr`s aren't glvalues, so we can't associate them with a
+ // `StorageLocation`, and there's also no sensible `Value` that we can
+ // assign to them. Examples:
+ // - Non-static member variables
+ // - Non static member functions
+ // Note: Member operators are an exception to this, but apparently only
+ // if the `DeclRefExpr` is used within the callee of a
+ // `CXXOperatorCallExpr`. In other cases, for example when applying the
+ // address-of operator, the `DeclRefExpr` is a prvalue.
+ if (!S->isGLValue())
return;
auto *DeclLoc = Env.getStorageLocation(*VD);
diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
index 4c31de3c8085bd..ef28f2f233b844 100644
--- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
+++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
@@ -5588,6 +5588,59 @@ TEST(TransferTest, BuiltinFunctionModeled) {
});
}
+// Check that a callee of a member operator call is modeled as a `PointerValue`.
+// Member operator calls are unusual in that their callee is a pointer that
+// stems from a `FunctionToPointerDecay`. In calls to non-operator non-static
+// member functions, the callee is a `MemberExpr` (which does not have pointer
+// type).
+// We want to make sure that we produce a pointer value for the callee in this
+// specific scenario and that its storage location is durable (for convergence).
+TEST(TransferTest, MemberOperatorCallModelsPointerForCallee) {
+ std::string Code = R"(
+ struct S {
+ bool operator!=(S s);
+ };
+ void target() {
+ S s;
+ (void)(s != s);
+ (void)(s != s);
+ // [[p]]
+ }
+ )";
+ runDataflow(
+ Code,
+ [](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
+ ASTContext &ASTCtx) {
+ using ast_matchers::selectFirst;
+ using ast_matchers::match;
+ using ast_matchers::traverse;
+ using ast_matchers::cxxOperatorCallExpr;
+
+ const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
+
+ auto Matches = match(
+ traverse(TK_AsIs, cxxOperatorCallExpr().bind("call")), ASTCtx);
+
+ ASSERT_EQ(Matches.size(), 2);
+
+ auto *Call1 = Matches[0].getNodeAs<CXXOperatorCallExpr>("call");
+ auto *Call2 = Matches[1].getNodeAs<CXXOperatorCallExpr>("call");
+
+ ASSERT_THAT(Call1, NotNull());
+ ASSERT_THAT(Call2, NotNull());
+
+ EXPECT_EQ(cast<ImplicitCastExpr>(Call1->getCallee())->getCastKind(),
+ CK_FunctionToPointerDecay);
+ EXPECT_EQ(cast<ImplicitCastExpr>(Call2->getCallee())->getCastKind(),
+ CK_FunctionToPointerDecay);
+
+ auto *Ptr1 = cast<PointerValue>(Env.getValue(*Call1->getCallee()));
+ auto *Ptr2 = cast<PointerValue>(Env.getValue(*Call2->getCallee()));
+
+ ASSERT_EQ(&Ptr1->getPointeeLoc(), &Ptr2->getPointeeLoc());
+ });
+}
+
// Check that fields of anonymous records are modeled.
TEST(TransferTest, AnonymousStruct) {
std::string Code = R"(
More information about the cfe-commits
mailing list