Skip to content

Commit f18da19

Browse files
author
Balazs Benics
committed
[analyzer][NFC] Switch to using CallDescription::matches() instead of isCalled()
This patch replaces each use of the previous API with the new one. In variadic cases, it will use the ADL `matchesAny(Call, CDs...)` variadic function. Also simplifies some code involving such operations. Reviewed By: martong, xazax.hun Differential Revision: https://reviews.llvm.org/D113591
1 parent 6c51270 commit f18da19

16 files changed

+34
-77
lines changed

clang/include/clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -257,20 +257,6 @@ class CallEvent {
257257
return false;
258258
}
259259

260-
/// Returns true if the CallEvent is a call to a function that matches
261-
/// the CallDescription.
262-
///
263-
/// Note that this function is not intended to be used to match Obj-C method
264-
/// calls.
265-
bool isCalled(const CallDescription &CD) const;
266-
267-
/// Returns true whether the CallEvent is any of the CallDescriptions supplied
268-
/// as a parameter.
269-
template <typename FirstCallDesc, typename... CallDescs>
270-
bool isCalled(const FirstCallDesc &First, const CallDescs &... Rest) const {
271-
return isCalled(First) || isCalled(Rest...);
272-
}
273-
274260
/// Returns a source range for the entire call, suitable for
275261
/// outputting in diagnostics.
276262
virtual SourceRange getSourceRange() const {

clang/lib/StaticAnalyzer/Checkers/BasicObjCFoundationChecks.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,8 @@ void CFRetainReleaseChecker::checkPreCall(const CallEvent &Call,
551551
return;
552552

553553
// Check if we called CFRetain/CFRelease/CFMakeCollectable/CFAutorelease.
554-
if (!(Call.isCalled(CFRetain) || Call.isCalled(CFRelease) ||
555-
Call.isCalled(CFMakeCollectable) || Call.isCalled(CFAutorelease)))
554+
555+
if (!matchesAny(Call, CFRetain, CFRelease, CFMakeCollectable, CFAutorelease))
556556
return;
557557

558558
// Get the argument's value.

clang/lib/StaticAnalyzer/Checkers/BlockInCriticalSectionChecker.cpp

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,7 @@ void BlockInCriticalSectionChecker::initIdentifierInfo(ASTContext &Ctx) const {
9797
}
9898

9999
bool BlockInCriticalSectionChecker::isBlockingFunction(const CallEvent &Call) const {
100-
if (Call.isCalled(SleepFn)
101-
|| Call.isCalled(GetcFn)
102-
|| Call.isCalled(FgetsFn)
103-
|| Call.isCalled(ReadFn)
104-
|| Call.isCalled(RecvFn)) {
105-
return true;
106-
}
107-
return false;
100+
return matchesAny(Call, SleepFn, GetcFn, FgetsFn, ReadFn, RecvFn);
108101
}
109102

110103
bool BlockInCriticalSectionChecker::isLockFunction(const CallEvent &Call) const {
@@ -114,15 +107,8 @@ bool BlockInCriticalSectionChecker::isLockFunction(const CallEvent &Call) const
114107
return true;
115108
}
116109

117-
if (Call.isCalled(LockFn)
118-
|| Call.isCalled(PthreadLockFn)
119-
|| Call.isCalled(PthreadTryLockFn)
120-
|| Call.isCalled(MtxLock)
121-
|| Call.isCalled(MtxTimedLock)
122-
|| Call.isCalled(MtxTryLock)) {
123-
return true;
124-
}
125-
return false;
110+
return matchesAny(Call, LockFn, PthreadLockFn, PthreadTryLockFn, MtxLock,
111+
MtxTimedLock, MtxTryLock);
126112
}
127113

128114
bool BlockInCriticalSectionChecker::isUnlockFunction(const CallEvent &Call) const {
@@ -133,12 +119,7 @@ bool BlockInCriticalSectionChecker::isUnlockFunction(const CallEvent &Call) cons
133119
return true;
134120
}
135121

136-
if (Call.isCalled(UnlockFn)
137-
|| Call.isCalled(PthreadUnlockFn)
138-
|| Call.isCalled(MtxUnlock)) {
139-
return true;
140-
}
141-
return false;
122+
return matchesAny(Call, UnlockFn, PthreadUnlockFn, MtxUnlock);
142123
}
143124

144125
void BlockInCriticalSectionChecker::checkPostCall(const CallEvent &Call,

clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,11 +2272,10 @@ CStringChecker::FnCheck CStringChecker::identifyCall(const CallEvent &Call,
22722272
if (!FD)
22732273
return nullptr;
22742274

2275-
if (Call.isCalled(StdCopy)) {
2275+
if (StdCopy.matches(Call))
22762276
return &CStringChecker::evalStdCopy;
2277-
} else if (Call.isCalled(StdCopyBackward)) {
2277+
if (StdCopyBackward.matches(Call))
22782278
return &CStringChecker::evalStdCopyBackward;
2279-
}
22802279

22812280
// Pro-actively check that argument types are safe to do arithmetic upon.
22822281
// We do not want to crash if someone accidentally passes a structure

clang/lib/StaticAnalyzer/Checkers/ChrootChecker.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ class ChrootChecker : public Checker<eval::Call, check::PreCall> {
6464
} // end anonymous namespace
6565

6666
bool ChrootChecker::evalCall(const CallEvent &Call, CheckerContext &C) const {
67-
if (Call.isCalled(Chroot)) {
67+
if (Chroot.matches(Call)) {
6868
evalChroot(Call, C);
6969
return true;
7070
}
71-
if (Call.isCalled(Chdir)) {
71+
if (Chdir.matches(Call)) {
7272
evalChdir(Call, C);
7373
return true;
7474
}
@@ -116,7 +116,7 @@ void ChrootChecker::evalChdir(const CallEvent &Call, CheckerContext &C) const {
116116
void ChrootChecker::checkPreCall(const CallEvent &Call,
117117
CheckerContext &C) const {
118118
// Ignore chroot and chdir.
119-
if (Call.isCalled(Chroot) || Call.isCalled(Chdir))
119+
if (matchesAny(Call, Chroot, Chdir))
120120
return;
121121

122122
// If jail state is ROOT_CHANGED, generate BugReport.

clang/lib/StaticAnalyzer/Checkers/InnerPointerChecker.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,15 @@ bool InnerPointerChecker::isInvalidatingMemberFunction(
126126
return true;
127127
return false;
128128
}
129-
return (isa<CXXDestructorCall>(Call) || Call.isCalled(AppendFn) ||
130-
Call.isCalled(AssignFn) || Call.isCalled(ClearFn) ||
131-
Call.isCalled(EraseFn) || Call.isCalled(InsertFn) ||
132-
Call.isCalled(PopBackFn) || Call.isCalled(PushBackFn) ||
133-
Call.isCalled(ReplaceFn) || Call.isCalled(ReserveFn) ||
134-
Call.isCalled(ResizeFn) || Call.isCalled(ShrinkToFitFn) ||
135-
Call.isCalled(SwapFn));
129+
return isa<CXXDestructorCall>(Call) ||
130+
matchesAny(Call, AppendFn, AssignFn, ClearFn, EraseFn, InsertFn,
131+
PopBackFn, PushBackFn, ReplaceFn, ReserveFn, ResizeFn,
132+
ShrinkToFitFn, SwapFn);
136133
}
137134

138135
bool InnerPointerChecker::isInnerPointerAccessFunction(
139136
const CallEvent &Call) const {
140-
return (Call.isCalled(CStrFn) || Call.isCalled(DataFn) ||
141-
Call.isCalled(DataMemberFn));
137+
return matchesAny(Call, CStrFn, DataFn, DataMemberFn);
142138
}
143139

144140
void InnerPointerChecker::markPtrSymbolsReleased(const CallEvent &Call,
@@ -185,7 +181,7 @@ void InnerPointerChecker::checkFunctionArguments(const CallEvent &Call,
185181

186182
// std::addressof function accepts a non-const reference as an argument,
187183
// but doesn't modify it.
188-
if (Call.isCalled(AddressofFn))
184+
if (AddressofFn.matches(Call))
189185
continue;
190186

191187
markPtrSymbolsReleased(Call, State, ArgRegion, C);

clang/lib/StaticAnalyzer/Checkers/MIGChecker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static bool isInMIGCall(CheckerContext &C) {
181181
}
182182

183183
void MIGChecker::checkPostCall(const CallEvent &Call, CheckerContext &C) const {
184-
if (Call.isCalled(OsRefRetain)) {
184+
if (OsRefRetain.matches(Call)) {
185185
// If the code is doing reference counting over the parameter,
186186
// it opens up an opportunity for safely calling a destructor function.
187187
// TODO: We should still check for over-releases.
@@ -199,7 +199,7 @@ void MIGChecker::checkPostCall(const CallEvent &Call, CheckerContext &C) const {
199199

200200
auto I = llvm::find_if(Deallocators,
201201
[&](const std::pair<CallDescription, unsigned> &Item) {
202-
return Call.isCalled(Item.first);
202+
return Item.first.matches(Call);
203203
});
204204
if (I == Deallocators.end())
205205
return;

clang/lib/StaticAnalyzer/Checkers/MmapWriteExecChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int MmapWriteExecChecker::ProtRead = 0x01;
4747

4848
void MmapWriteExecChecker::checkPreCall(const CallEvent &Call,
4949
CheckerContext &C) const {
50-
if (Call.isCalled(MmapFn) || Call.isCalled(MprotectFn)) {
50+
if (matchesAny(Call, MmapFn, MprotectFn)) {
5151
SVal ProtVal = Call.getArgSVal(2);
5252
Optional<nonloc::ConcreteInt> ProtLoc = ProtVal.getAs<nonloc::ConcreteInt>();
5353
int64_t Prot = ProtLoc->getValue().getSExtValue();

clang/lib/StaticAnalyzer/Checkers/SimpleStreamChecker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void SimpleStreamChecker::checkPostCall(const CallEvent &Call,
120120
if (!Call.isGlobalCFunction())
121121
return;
122122

123-
if (!Call.isCalled(OpenFn))
123+
if (!OpenFn.matches(Call))
124124
return;
125125

126126
// Get the symbolic value corresponding to the file handle.
@@ -139,7 +139,7 @@ void SimpleStreamChecker::checkPreCall(const CallEvent &Call,
139139
if (!Call.isGlobalCFunction())
140140
return;
141141

142-
if (!Call.isCalled(CloseFn))
142+
if (!CloseFn.matches(Call))
143143
return;
144144

145145
// Get the symbolic value corresponding to the file handle.

clang/lib/StaticAnalyzer/Checkers/SmartPtrModeling.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ bool SmartPtrModeling::evalCall(const CallEvent &Call,
286286
if (ModelSmartPtrDereference && isStdOstreamOperatorCall(Call))
287287
return handleOstreamOperator(Call, C);
288288

289-
if (Call.isCalled(StdSwapCall)) {
289+
if (StdSwapCall.matches(Call)) {
290290
// Check the first arg, if it is of std::unique_ptr type.
291291
assert(Call.getNumArgs() == 2 && "std::swap should have two arguments");
292292
const Expr *FirstArg = Call.getArgExpr(0);
@@ -295,8 +295,7 @@ bool SmartPtrModeling::evalCall(const CallEvent &Call,
295295
return handleSwap(State, Call.getArgSVal(0), Call.getArgSVal(1), C);
296296
}
297297

298-
if (Call.isCalled(StdMakeUniqueCall) ||
299-
Call.isCalled(StdMakeUniqueForOverwriteCall)) {
298+
if (matchesAny(Call, StdMakeUniqueCall, StdMakeUniqueForOverwriteCall)) {
300299
if (!ModelSmartPtrDereference)
301300
return false;
302301

clang/lib/StaticAnalyzer/Checkers/StringChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class StringChecker : public Checker<check::PreCall> {
3737

3838
bool StringChecker::isCharToStringCtor(const CallEvent &Call,
3939
const ASTContext &ACtx) const {
40-
if (!Call.isCalled(TwoParamStdStringCtor))
40+
if (!TwoParamStdStringCtor.matches(Call))
4141
return false;
4242
const auto *FD = dyn_cast<FunctionDecl>(Call.getDecl());
4343
assert(FD);

clang/lib/StaticAnalyzer/Checkers/ValistChecker.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ void ValistChecker::checkPreCall(const CallEvent &Call,
127127
CheckerContext &C) const {
128128
if (!Call.isGlobalCFunction())
129129
return;
130-
if (Call.isCalled(VaStart))
130+
if (VaStart.matches(Call))
131131
checkVAListStartCall(Call, C, false);
132-
else if (Call.isCalled(VaCopy))
132+
else if (VaCopy.matches(Call))
133133
checkVAListStartCall(Call, C, true);
134-
else if (Call.isCalled(VaEnd))
134+
else if (VaEnd.matches(Call))
135135
checkVAListEndCall(Call, C);
136136
else {
137137
for (auto FuncInfo : VAListAccepters) {
138-
if (!Call.isCalled(FuncInfo.Func))
138+
if (!FuncInfo.Func.matches(Call))
139139
continue;
140140
bool Symbolic;
141141
const MemRegion *VAList =

clang/lib/StaticAnalyzer/Checkers/cert/PutenvWithAutoChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class PutenvWithAutoChecker : public Checker<check::PostCall> {
3939

4040
void PutenvWithAutoChecker::checkPostCall(const CallEvent &Call,
4141
CheckerContext &C) const {
42-
if (!Call.isCalled(Putenv))
42+
if (!Putenv.matches(Call))
4343
return;
4444

4545
SVal ArgV = Call.getArgSVal(0);

clang/lib/StaticAnalyzer/Core/CallEvent.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,6 @@ ProgramPoint CallEvent::getProgramPoint(bool IsPreVisit,
303303
return PostImplicitCall(D, Loc, getLocationContext(), Tag);
304304
}
305305

306-
bool CallEvent::isCalled(const CallDescription &CD) const {
307-
return CD.matches(*this);
308-
}
309-
310306
SVal CallEvent::getArgSVal(unsigned Index) const {
311307
const Expr *ArgE = getArgExpr(Index);
312308
if (!ArgE)

clang/unittests/StaticAnalyzer/ConflictingEvalCallsTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class EvalCallBase : public Checker<eval::Call> {
2222

2323
public:
2424
bool evalCall(const CallEvent &Call, CheckerContext &C) const {
25-
return Call.isCalled(Foo);
25+
return Foo.matches(Call);
2626
}
2727
};
2828

clang/unittests/StaticAnalyzer/NoStateChangeFuncVisitorTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ class StatefulChecker : public Checker<check::PreCall> {
8686

8787
public:
8888
void checkPreCall(const CallEvent &Call, CheckerContext &C) const {
89-
if (Call.isCalled(CallDescription{"preventError", 0})) {
89+
if (CallDescription{"preventError", 0}.matches(Call)) {
9090
C.addTransition(C.getState()->set<ErrorPrevented>(true));
9191
return;
9292
}
9393

94-
if (Call.isCalled(CallDescription{"allowError", 0})) {
94+
if (CallDescription{"allowError", 0}.matches(Call)) {
9595
C.addTransition(C.getState()->set<ErrorPrevented>(false));
9696
return;
9797
}
9898

99-
if (Call.isCalled(CallDescription{"error", 0})) {
99+
if (CallDescription{"error", 0}.matches(Call)) {
100100
if (C.getState()->get<ErrorPrevented>())
101101
return;
102102
const ExplodedNode *N = C.generateErrorNode();

0 commit comments

Comments
 (0)