Skip to content

Commit 4a0a47b

Browse files
committed
Addressed comments
1 parent 62403de commit 4a0a47b

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,12 @@ static void insertIfFunction(const Decl &D,
288288
Funcs.insert(FD);
289289
}
290290

291-
static Expr *getRetValueFromSingleReturnStmtMethod(const CXXMemberCallExpr &C) {
292-
auto *D = cast_or_null<CXXMethodDecl>(C.getMethodDecl()->getDefinition());
293-
if (!D)
291+
static MemberExpr *getMemberForAccessor(const CXXMemberCallExpr &C) {
292+
auto *Body = dyn_cast_or_null<CompoundStmt>(C.getMethodDecl()->getBody());
293+
if (!Body || Body->size() != 1)
294294
return nullptr;
295-
auto *S = cast<CompoundStmt>(D->getBody());
296-
if (S->size() != 1)
297-
return nullptr;
298-
if (auto *RS = dyn_cast<ReturnStmt>(*S->body_begin()))
299-
return RS->getRetValue()->IgnoreParenImpCasts();
295+
if (auto *RS = dyn_cast<ReturnStmt>(*Body->body_begin()))
296+
return dyn_cast<MemberExpr>(RS->getRetValue()->IgnoreParenImpCasts());
300297
return nullptr;
301298
}
302299

@@ -339,9 +336,9 @@ getFieldsGlobalsAndFuncs(const Stmt &S, FieldSet &Fields,
339336
} else if (const auto *C = dyn_cast<CXXMemberCallExpr>(&S)) {
340337
// If this is a method that returns a member variable but does nothing else,
341338
// model the field of the return value.
342-
if (MemberExpr *E = dyn_cast_or_null<MemberExpr>(
343-
getRetValueFromSingleReturnStmtMethod(*C)))
344-
getFieldsGlobalsAndFuncs(*E, Fields, Vars, Funcs);
339+
if (MemberExpr *E = getMemberForAccessor(*C))
340+
if (const auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl()))
341+
Fields.insert(FD);
345342
} else if (auto *E = dyn_cast<MemberExpr>(&S)) {
346343
// FIXME: should we be using `E->getFoundDecl()`?
347344
const ValueDecl *VD = E->getMemberDecl();

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,27 +1449,30 @@ TEST(TransferTest, BaseClassInitializer) {
14491449
TEST(TransferTest, StructModeledFieldsWithAccessor) {
14501450
std::string Code = R"(
14511451
class S {
1452-
int *P;
1453-
int *Q;
1454-
int X;
1455-
int Y;
1456-
int Z;
1452+
int *Ptr;
1453+
int *PtrNonConst;
1454+
int Int;
1455+
int IntWithInc;
1456+
int IntNotAccessed;
1457+
int IntRef;
14571458
public:
1458-
int *getPtr() const { return P; }
1459-
int *getPtrNonConst() { return Q; }
1460-
int getInt(int i) const { return X; }
1461-
int getWithOtherWork(int i) { Y += i; return Y; }
1462-
int getIntNotCalled() const { return Z; }
1459+
int *getPtr() const { return Ptr; }
1460+
int *getPtrNonConst() { return PtrNonConst; }
1461+
int getInt(int i) const { return Int; }
1462+
int getWithInc(int i) { IntWithInc += i; return IntWithInc; }
1463+
int getIntNotAccessed() const { return IntNotAccessed; }
14631464
int getIntNoDefinition() const;
1465+
int &getIntRef() { return IntRef; }
14641466
};
14651467
14661468
void target() {
14671469
S s;
1468-
int *p = s.getPtr();
1469-
int *q = s.getPtrNonConst();
1470-
int x = s.getInt(1);
1471-
int y = s.getWithOtherWork(1);
1472-
int w = s.getIntNoDefinition();
1470+
int *p1 = s.getPtr();
1471+
int *p2 = s.getPtrNonConst();
1472+
int i1 = s.getInt(1);
1473+
int i2 = s.getWithInc(1);
1474+
int i3 = s.getIntNoDefinition();
1475+
int &iref = s.getIntRef();
14731476
// [[p]]
14741477
}
14751478
)";
@@ -1484,10 +1487,11 @@ TEST(TransferTest, StructModeledFieldsWithAccessor) {
14841487
for (auto [Field, _] : SLoc.children())
14851488
Fields.push_back(Field);
14861489
// Only the fields that have simple accessor methods (that have a
1487-
// single statement body that returns the member variable) should be modeled.
1490+
// single statement body that returns the member variable) should be
1491+
// modeled.
14881492
ASSERT_THAT(Fields, UnorderedElementsAre(
1489-
findValueDecl(ASTCtx, "P"), findValueDecl(ASTCtx, "Q"),
1490-
findValueDecl(ASTCtx, "X")));
1493+
findValueDecl(ASTCtx, "Ptr"), findValueDecl(ASTCtx, "PtrNonConst"),
1494+
findValueDecl(ASTCtx, "Int"), findValueDecl(ASTCtx, "IntRef")));
14911495
});
14921496
}
14931497

0 commit comments

Comments
 (0)