Skip to content

[clang][dataflow] Model the fields that are accessed via inline accessors #66368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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