Skip to content

Commit 5ab97a7

Browse files
committed
[clang][analyzer][NFCi] Pass if bind is to a Decl or not to checkBind
Binding a value to location can happen when a new value is created or when and existing value is updated. This modification exposes whether the value binding happens at a declaration. This helps simplify the hacky logic of the BindToImmutable checker.
1 parent 34c2ea3 commit 5ab97a7

18 files changed

+57
-85
lines changed

clang/include/clang/StaticAnalyzer/Core/Checker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ class Location {
209209
class Bind {
210210
template <typename CHECKER>
211211
static void _checkBind(void *checker, SVal location, SVal val, const Stmt *S,
212-
CheckerContext &C) {
213-
((const CHECKER *)checker)->checkBind(location, val, S, C);
212+
bool atDeclInit, CheckerContext &C) {
213+
((const CHECKER *)checker)->checkBind(location, val, S, atDeclInit, C);
214214
}
215215

216216
public:

clang/include/clang/StaticAnalyzer/Core/CheckerManager.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,9 @@ class CheckerManager {
338338
ExprEngine &Eng);
339339

340340
/// Run checkers for binding of a value to a location.
341-
void runCheckersForBind(ExplodedNodeSet &Dst,
342-
const ExplodedNodeSet &Src,
343-
SVal location, SVal val,
344-
const Stmt *S, ExprEngine &Eng,
341+
void runCheckersForBind(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src,
342+
SVal location, SVal val, const Stmt *S,
343+
bool atDeclInit, ExprEngine &Eng,
345344
const ProgramPoint &PP);
346345

347346
/// Run checkers after taking a control flow edge.
@@ -499,8 +498,8 @@ class CheckerManager {
499498
using CheckLocationFunc = CheckerFn<void(SVal location, bool isLoad,
500499
const Stmt *S, CheckerContext &)>;
501500

502-
using CheckBindFunc =
503-
CheckerFn<void(SVal location, SVal val, const Stmt *S, CheckerContext &)>;
501+
using CheckBindFunc = CheckerFn<void(SVal location, SVal val, const Stmt *S,
502+
bool atDeclInit, CheckerContext &)>;
504503

505504
using CheckBlockEntranceFunc =
506505
CheckerFn<void(const BlockEntrance &, CheckerContext &)>;

clang/lib/StaticAnalyzer/Checkers/AnalysisOrderChecker.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ class AnalysisOrderChecker
183183
llvm::errs() << "NewAllocator\n";
184184
}
185185

186-
void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const {
186+
void checkBind(SVal Loc, SVal Val, const Stmt *S, bool atDeclInit,
187+
CheckerContext &C) const {
187188
if (isCallbackEnabled(C, "Bind"))
188189
llvm::errs() << "Bind\n";
189190
}

clang/lib/StaticAnalyzer/Checkers/BoolAssignmentChecker.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class BoolAssignmentChecker : public Checker<check::Bind> {
2929
bool IsTainted = false) const;
3030

3131
public:
32-
void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const;
32+
void checkBind(SVal Loc, SVal Val, const Stmt *S, bool atDeclInit,
33+
CheckerContext &C) const;
3334
};
3435
} // end anonymous namespace
3536

@@ -55,6 +56,7 @@ static bool isBooleanType(QualType Ty) {
5556
}
5657

5758
void BoolAssignmentChecker::checkBind(SVal Loc, SVal Val, const Stmt *S,
59+
bool atDeclInit,
5860
CheckerContext &C) const {
5961

6062
// We are only interested in stores into Booleans.

clang/lib/StaticAnalyzer/Checkers/CheckerDocumentation.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,12 @@ class CheckerDocumentation
175175
/// \param Loc The value of the location (pointer).
176176
/// \param Val The value which will be stored at the location Loc.
177177
/// \param S The bind is performed while processing the statement S.
178+
/// \param atDeclInit Whether the bind is performed during declaration
179+
/// initialization.
178180
///
179181
/// check::Bind
180-
void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &) const {}
182+
void checkBind(SVal Loc, SVal Val, const Stmt *S, bool atDeclInit,
183+
CheckerContext &) const {}
181184

182185
/// Called after a CFG edge is taken within a function.
183186
///

clang/lib/StaticAnalyzer/Checkers/DereferenceChecker.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class DereferenceChecker
4848
public:
4949
void checkLocation(SVal location, bool isLoad, const Stmt* S,
5050
CheckerContext &C) const;
51-
void checkBind(SVal L, SVal V, const Stmt *S, CheckerContext &C) const;
51+
void checkBind(SVal L, SVal V, const Stmt *S, bool atDeclInit,
52+
CheckerContext &C) const;
5253

5354
static void AddDerefSource(raw_ostream &os,
5455
SmallVectorImpl<SourceRange> &Ranges,
@@ -309,7 +310,7 @@ void DereferenceChecker::checkLocation(SVal l, bool isLoad, const Stmt* S,
309310
}
310311

311312
void DereferenceChecker::checkBind(SVal L, SVal V, const Stmt *S,
312-
CheckerContext &C) const {
313+
bool atDeclInit, CheckerContext &C) const {
313314
// If we're binding to a reference, check if the value is known to be null.
314315
if (V.isUndef())
315316
return;

clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ class IteratorModeling
150150
IteratorModeling() = default;
151151

152152
void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
153-
void checkBind(SVal Loc, SVal Val, const Stmt *S, CheckerContext &C) const;
153+
void checkBind(SVal Loc, SVal Val, const Stmt *S, bool atDeclInit,
154+
CheckerContext &C) const;
154155
void checkPostStmt(const UnaryOperator *UO, CheckerContext &C) const;
155156
void checkPostStmt(const BinaryOperator *BO, CheckerContext &C) const;
156157
void checkPostStmt(const MaterializeTemporaryExpr *MTE,
@@ -234,7 +235,7 @@ void IteratorModeling::checkPostCall(const CallEvent &Call,
234235
}
235236

236237
void IteratorModeling::checkBind(SVal Loc, SVal Val, const Stmt *S,
237-
CheckerContext &C) const {
238+
bool atDeclInit, CheckerContext &C) const {
238239
auto State = C.getState();
239240
const auto *Pos = getIteratorPosition(State, Val);
240241
if (Pos) {

clang/lib/StaticAnalyzer/Checkers/NullabilityChecker.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ class NullabilityChecker
9797
// libraries.
9898
bool NoDiagnoseCallsToSystemHeaders = false;
9999

100-
void checkBind(SVal L, SVal V, const Stmt *S, CheckerContext &C) const;
100+
void checkBind(SVal L, SVal V, const Stmt *S, bool atDeclInit,
101+
CheckerContext &C) const;
101102
void checkPostStmt(const ExplicitCastExpr *CE, CheckerContext &C) const;
102103
void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
103104
void checkPostObjCMessage(const ObjCMethodCall &M, CheckerContext &C) const;
@@ -1250,7 +1251,7 @@ static bool isARCNilInitializedLocal(CheckerContext &C, const Stmt *S) {
12501251
/// Propagate the nullability information through binds and warn when nullable
12511252
/// pointer or null symbol is assigned to a pointer with a nonnull type.
12521253
void NullabilityChecker::checkBind(SVal L, SVal V, const Stmt *S,
1253-
CheckerContext &C) const {
1254+
bool atDeclInit, CheckerContext &C) const {
12541255
const TypedValueRegion *TVR =
12551256
dyn_cast_or_null<TypedValueRegion>(L.getAsRegion());
12561257
if (!TVR)

clang/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ class ObjCSelfInitChecker : public Checker< check::PostObjCMessage,
7373
void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
7474
void checkLocation(SVal location, bool isLoad, const Stmt *S,
7575
CheckerContext &C) const;
76-
void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
76+
void checkBind(SVal loc, SVal val, const Stmt *S, bool atDeclInit,
77+
CheckerContext &C) const;
7778

7879
void checkPreCall(const CallEvent &CE, CheckerContext &C) const;
7980
void checkPostCall(const CallEvent &CE, CheckerContext &C) const;
@@ -311,9 +312,8 @@ void ObjCSelfInitChecker::checkLocation(SVal location, bool isLoad,
311312
C);
312313
}
313314

314-
315315
void ObjCSelfInitChecker::checkBind(SVal loc, SVal val, const Stmt *S,
316-
CheckerContext &C) const {
316+
bool atDeclInit, CheckerContext &C) const {
317317
// Allow assignment of anything to self. Self is a local variable in the
318318
// initializer, so it is legal to assign anything to it, like results of
319319
// static functions/method calls. After self is assigned something we cannot

clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ ExplodedNode * RetainCountChecker::checkReturnWithRetEffect(const ReturnStmt *S,
11281128
//===----------------------------------------------------------------------===//
11291129

11301130
void RetainCountChecker::checkBind(SVal loc, SVal val, const Stmt *S,
1131-
CheckerContext &C) const {
1131+
bool atDeclInit, CheckerContext &C) const {
11321132
ProgramStateRef state = C.getState();
11331133
const MemRegion *MR = loc.getAsRegion();
11341134

0 commit comments

Comments
 (0)