Skip to content

Commit badba51

Browse files
committed
[analyzer] NonnullGlobalConstants: Add support for kCFNull.
It's a singleton in CoreFoundation that always contains a non-null CFNullRef.
1 parent 2caeaf2 commit badba51

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

clang/lib/StaticAnalyzer/Checkers/NonnullGlobalConstantsChecker.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class NonnullGlobalConstantsChecker : public Checker<check::Location> {
3636
mutable IdentifierInfo *NSStringII = nullptr;
3737
mutable IdentifierInfo *CFStringRefII = nullptr;
3838
mutable IdentifierInfo *CFBooleanRefII = nullptr;
39+
mutable IdentifierInfo *CFNullRefII = nullptr;
3940

4041
public:
4142
NonnullGlobalConstantsChecker() {}
@@ -61,6 +62,7 @@ void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
6162
NSStringII = &Ctx.Idents.get("NSString");
6263
CFStringRefII = &Ctx.Idents.get("CFStringRef");
6364
CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
65+
CFNullRefII = &Ctx.Idents.get("CFNullRef");
6466
}
6567

6668
/// Add an assumption that const string-like globals are non-null.
@@ -136,7 +138,7 @@ bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
136138
T->getInterfaceDecl()->getIdentifier() == NSStringII;
137139
} else if (auto *T = dyn_cast<TypedefType>(Ty)) {
138140
IdentifierInfo* II = T->getDecl()->getIdentifier();
139-
return II == CFStringRefII || II == CFBooleanRefII;
141+
return II == CFStringRefII || II == CFBooleanRefII || II == CFNullRefII;
140142
}
141143
return false;
142144
}

clang/test/Analysis/nonnull-global-constants.mm

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
@class NSString;
99
typedef const struct __CFString *CFStringRef;
10-
typedef const struct __CFBoolean * CFBooleanRef;
10+
typedef const struct __CFBoolean *CFBooleanRef;
11+
12+
#define CF_BRIDGED_TYPE(T) __attribute__((objc_bridge(T)))
13+
typedef const struct CF_BRIDGED_TYPE(NSNull) __CFNull *CFNullRef;
14+
extern const CFNullRef kCFNull;
1115

1216
// Global NSString* is non-null.
1317
extern NSString *const StringConstGlobal;
@@ -113,3 +117,7 @@ void testNonnullNonconstCFString() {
113117
void testNonnullNonnullCFString() {
114118
clang_analyzer_eval(str4); // expected-warning{{TRUE}}
115119
}
120+
121+
void test_kCFNull() {
122+
clang_analyzer_eval(kCFNull); // expected-warning{{TRUE}}
123+
}

0 commit comments

Comments
 (0)