[analyzer] Fix for faulty namespace test in SmartPtrModelling

This patch:
- Fixes how the std-namespace test is written in SmartPtrModelling
(now accounts for functions with no Decl available)
- Adds the smart pointer checker flag check where it was missing

Differential Revision: https://reviews.llvm.org/D106296
This commit is contained in:
Deep Majumder
2021-07-21 18:19:57 +05:30
parent 907efdf95d
commit 80068ca623
2 changed files with 23 additions and 9 deletions

View File

@@ -248,9 +248,12 @@ bool isStdBasicOstream(const Expr *E) {
return hasStdClassWithName(RD, BASIC_OSTREAM_NAMES);
}
static bool isStdFunctionCall(const CallEvent &Call) {
return Call.getDecl() && Call.getDecl()->getDeclContext()->isStdNamespace();
}
bool isStdOstreamOperatorCall(const CallEvent &Call) {
if (Call.getNumArgs() != 2 ||
!Call.getDecl()->getDeclContext()->isStdNamespace())
if (Call.getNumArgs() != 2 || !isStdFunctionCall(Call))
return false;
const auto *FC = dyn_cast<SimpleFunctionCall>(&Call);
if (!FC)
@@ -265,6 +268,13 @@ bool isStdOstreamOperatorCall(const CallEvent &Call) {
isStdBasicOstream(Call.getArgExpr(0));
}
static bool isPotentiallyComparisionOpCall(const CallEvent &Call) {
if (Call.getNumArgs() != 2 || !isStdFunctionCall(Call))
return false;
return smartptr::isStdSmartPtr(Call.getArgExpr(0)) ||
smartptr::isStdSmartPtr(Call.getArgExpr(1));
}
bool SmartPtrModeling::evalCall(const CallEvent &Call,
CheckerContext &C) const {
@@ -272,14 +282,11 @@ bool SmartPtrModeling::evalCall(const CallEvent &Call,
// If any one of the arg is a unique_ptr, then
// we can try this function
if (Call.getNumArgs() == 2 &&
Call.getDecl()->getDeclContext()->isStdNamespace())
if (smartptr::isStdSmartPtr(Call.getArgExpr(0)) ||
smartptr::isStdSmartPtr(Call.getArgExpr(1)))
if (handleComparisionOp(Call, C))
return true;
if (ModelSmartPtrDereference && isPotentiallyComparisionOpCall(Call))
if (handleComparisionOp(Call, C))
return true;
if (isStdOstreamOperatorCall(Call))
if (ModelSmartPtrDereference && isStdOstreamOperatorCall(Call))
return handleOstreamOperator(Call, C);
if (Call.isCalled(StdSwapCall)) {

View File

@@ -536,3 +536,10 @@ void testOstreamDoesntInvalidateGlobals(std::unique_ptr<int> P) {
}
#endif
// The following test isn't really a "smart-ptr" test
// It came up during a bug fix (D106296)
void testCheckForFunctionsWithNoDecl(void (*bar)(bool, bool)) {
// This should NOT crash.
bar(true, false);
}