Skip to content

Commit

Permalink
[analyzer] NonnullGlobalConstants: Add support for kCFNull.
Browse files Browse the repository at this point in the history
It's a singleton in CoreFoundation that always contains a non-null CFNullRef.
  • Loading branch information
haoNoQ committed Dec 18, 2019
1 parent 2caeaf2 commit badba51
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 9 additions & 1 deletion clang/test/Analysis/nonnull-global-constants.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

@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;
Expand Down Expand Up @@ -113,3 +117,7 @@ void testNonnullNonconstCFString() {
void testNonnullNonnullCFString() {
clang_analyzer_eval(str4); // expected-warning{{TRUE}}
}

void test_kCFNull() {
clang_analyzer_eval(kCFNull); // expected-warning{{TRUE}}
}

0 comments on commit badba51

Please sign in to comment.