[analyzer] Fix NSErrorChecker false positives on constructors.

Constructors and delete operators cannot return a boolean value.
Therefore they cannot possibly follow the NS/CFError-related coding
conventions.

Patch by Valeriy Savchenko!

Differential Revision: https://reviews.llvm.org/D77551
This commit is contained in:
Artem Dergachev 2020-04-06 20:27:40 +03:00
parent 2ddd3325c4
commit 9b1e4a8218
2 changed files with 39 additions and 0 deletions

View file

@ -95,6 +95,15 @@ public:
};
}
static bool hasReservedReturnType(const FunctionDecl *D) {
if (isa<CXXConstructorDecl>(D))
return true;
// operators delete and delete[] are required to have 'void' return type
auto OperatorKind = D->getOverloadedOperator();
return OperatorKind == OO_Delete || OperatorKind == OO_Array_Delete;
}
void CFErrorFunctionChecker::checkASTDecl(const FunctionDecl *D,
AnalysisManager &mgr,
BugReporter &BR) const {
@ -102,6 +111,8 @@ void CFErrorFunctionChecker::checkASTDecl(const FunctionDecl *D,
return;
if (!D->getReturnType()->isVoidType())
return;
if (hasReservedReturnType(D))
return;
if (!II)
II = &D->getASTContext().Idents.get("CFErrorRef");

View file

@ -0,0 +1,28 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.coreFoundation.CFError \
// RUN: -verify %s
typedef unsigned long size_t;
struct __CFError {};
typedef struct __CFError *CFErrorRef;
void *malloc(size_t);
class Foo {
public:
Foo(CFErrorRef *error) {} // no-warning
void operator delete(void *pointer, CFErrorRef *error) { // no-warning
return;
}
void operator delete[](void *pointer, CFErrorRef *error) { // no-warning
return;
}
// Check that we report warnings for operators when it can be useful
void operator()(CFErrorRef *error) {} // expected-warning {{Function accepting CFErrorRef* should have a non-void return value to indicate whether or not an error occurred}}
};
// Check that global delete operator is not bothered as well
void operator delete(void *pointer, CFErrorRef *error) { // no-warning
return;
}