Skip to content

Commit

Permalink
[clang][dataflow] Model the fields that are accessed via inline acces…
Browse files Browse the repository at this point in the history
…sors (#66368)

So that the values that are accessed via such accessors can be analyzed
as a limited version of context-sensitive analysis. We can potentially
do this only when some option is set, but doing additional modeling like
this won't be expensive and intrusive, so we do it by default for now.
  • Loading branch information
kinu committed Sep 18, 2023
1 parent 75b76c4 commit 03be486
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
15 changes: 15 additions & 0 deletions clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ static void insertIfFunction(const Decl &D,
Funcs.insert(FD);
}

static MemberExpr *getMemberForAccessor(const CXXMemberCallExpr &C) {
auto *Body = dyn_cast_or_null<CompoundStmt>(C.getMethodDecl()->getBody());
if (!Body || Body->size() != 1)
return nullptr;
if (auto *RS = dyn_cast<ReturnStmt>(*Body->body_begin()))
return dyn_cast<MemberExpr>(RS->getRetValue()->IgnoreParenImpCasts());
return nullptr;
}

static void
getFieldsGlobalsAndFuncs(const Decl &D, FieldSet &Fields,
llvm::DenseSet<const VarDecl *> &Vars,
Expand Down Expand Up @@ -324,6 +333,12 @@ getFieldsGlobalsAndFuncs(const Stmt &S, FieldSet &Fields,
} else if (auto *E = dyn_cast<DeclRefExpr>(&S)) {
insertIfGlobal(*E->getDecl(), Vars);
insertIfFunction(*E->getDecl(), Funcs);
} else if (const auto *C = dyn_cast<CXXMemberCallExpr>(&S)) {
// If this is a method that returns a member variable but does nothing else,
// model the field of the return value.
if (MemberExpr *E = getMemberForAccessor(*C))
if (const auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()))
Fields.insert(FD);
} else if (auto *E = dyn_cast<MemberExpr>(&S)) {
// FIXME: should we be using `E->getFoundDecl()`?
const ValueDecl *VD = E->getMemberDecl();
Expand Down
49 changes: 49 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,55 @@ TEST(TransferTest, BaseClassInitializer) {
llvm::Succeeded());
}

TEST(TransferTest, StructModeledFieldsWithAccessor) {
std::string Code = R"(
class S {
int *Ptr;
int *PtrNonConst;
int Int;
int IntWithInc;
int IntNotAccessed;
int IntRef;
public:
int *getPtr() const { return Ptr; }
int *getPtrNonConst() { return PtrNonConst; }
int getInt(int i) const { return Int; }
int getWithInc(int i) { IntWithInc += i; return IntWithInc; }
int getIntNotAccessed() const { return IntNotAccessed; }
int getIntNoDefinition() const;
int &getIntRef() { return IntRef; }
};
void target() {
S s;
int *p1 = s.getPtr();
int *p2 = s.getPtrNonConst();
int i1 = s.getInt(1);
int i2 = s.getWithInc(1);
int i3 = s.getIntNoDefinition();
int &iref = s.getIntRef();
// [[p]]
}
)";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
const Environment &Env =
getEnvironmentAtAnnotation(Results, "p");
auto &SLoc = getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "s");
std::vector<const ValueDecl*> Fields;
for (auto [Field, _] : SLoc.children())
Fields.push_back(Field);
// Only the fields that have simple accessor methods (that have a
// single statement body that returns the member variable) should be
// modeled.
ASSERT_THAT(Fields, UnorderedElementsAre(
findValueDecl(ASTCtx, "Ptr"), findValueDecl(ASTCtx, "PtrNonConst"),
findValueDecl(ASTCtx, "Int"), findValueDecl(ASTCtx, "IntRef")));
});
}

TEST(TransferTest, StructModeledFieldsWithComplicatedInheritance) {
std::string Code = R"(
struct Base1 {
Expand Down

0 comments on commit 03be486

Please sign in to comment.