[analyzer] NonnullGlobalConstants: Add support for kCFNull.

It's a singleton in CoreFoundation that always contains a non-null CFNullRef.
This commit is contained in:
Artem Dergachev 2019-12-18 12:04:18 -08:00
parent 2caeaf2f45
commit badba5118f
2 changed files with 12 additions and 2 deletions

View File

@ -36,6 +36,7 @@ class NonnullGlobalConstantsChecker : public Checker<check::Location> {
mutable IdentifierInfo *NSStringII = nullptr;
mutable IdentifierInfo *CFStringRefII = nullptr;
mutable IdentifierInfo *CFBooleanRefII = nullptr;
mutable IdentifierInfo *CFNullRefII = nullptr;
public:
NonnullGlobalConstantsChecker() {}
@ -61,6 +62,7 @@ void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
NSStringII = &Ctx.Idents.get("NSString");
CFStringRefII = &Ctx.Idents.get("CFStringRef");
CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
CFNullRefII = &Ctx.Idents.get("CFNullRef");
}
/// Add an assumption that const string-like globals are non-null.
@ -136,7 +138,7 @@ bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
T->getInterfaceDecl()->getIdentifier() == NSStringII;
} else if (auto *T = dyn_cast<TypedefType>(Ty)) {
IdentifierInfo* II = T->getDecl()->getIdentifier();
return II == CFStringRefII || II == CFBooleanRefII;
return II == CFStringRefII || II == CFBooleanRefII || II == CFNullRefII;
}
return false;
}

View File

@ -7,7 +7,11 @@ void clang_analyzer_eval(bool);
@class NSString;
typedef const struct __CFString *CFStringRef;
typedef const struct __CFBoolean * CFBooleanRef;
typedef const struct __CFBoolean *CFBooleanRef;
#define CF_BRIDGED_TYPE(T) __attribute__((objc_bridge(T)))
typedef const struct CF_BRIDGED_TYPE(NSNull) __CFNull *CFNullRef;
extern const CFNullRef kCFNull;
// Global NSString* is non-null.
extern NSString *const StringConstGlobal;
@ -113,3 +117,7 @@ extern const CFStringRef _Nonnull str4;
void testNonnullNonnullCFString() {
clang_analyzer_eval(str4); // expected-warning{{TRUE}}
}
void test_kCFNull() {
clang_analyzer_eval(kCFNull); // expected-warning{{TRUE}}
}