1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s
3 // Nullability of const string-like globals, testing
4 // NonnullGlobalConstantsChecker.
6 void clang_analyzer_eval(bool);
9 typedef const struct __CFString *CFStringRef;
10 typedef const struct __CFBoolean *CFBooleanRef;
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;
16 // Global NSString* is non-null.
17 extern NSString *const StringConstGlobal;
18 void stringConstGlobal() {
19 clang_analyzer_eval(StringConstGlobal); // expected-warning{{TRUE}}
22 // The logic does not apply to local variables though.
23 extern NSString *stringGetter();
24 void stringConstLocal() {
25 NSString *const local = stringGetter();
26 clang_analyzer_eval(local); // expected-warning{{UNKNOWN}}
29 // Global const CFStringRef's are also assumed to be non-null.
30 extern const CFStringRef CFStringConstGlobal;
31 void cfStringCheckGlobal() {
32 clang_analyzer_eval(CFStringConstGlobal); // expected-warning{{TRUE}}
35 // But only "const" ones.
36 extern CFStringRef CFStringNonConstGlobal;
37 void cfStringCheckMutableGlobal() {
38 clang_analyzer_eval(CFStringNonConstGlobal); // expected-warning{{UNKNOWN}}
41 // char* const is also assumed to be non-null.
42 extern const char *const ConstCharStarConst;
43 void constCharStarCheckGlobal() {
44 clang_analyzer_eval(ConstCharStarConst); // expected-warning{{TRUE}}
47 // Pointer value can be mutable.
48 extern char *const CharStarConst;
49 void charStarCheckGlobal() {
50 clang_analyzer_eval(CharStarConst); // expected-warning{{TRUE}}
53 // But the pointer itself should be immutable.
54 extern char *CharStar;
55 void charStartCheckMutableGlobal() {
56 clang_analyzer_eval(CharStar); // expected-warning{{UNKNOWN}}
59 // Type definitions should also work across typedefs, for pointers:
60 typedef char *const str;
62 void charStarCheckTypedef() {
63 clang_analyzer_eval(globalStr); // expected-warning{{TRUE}}
67 typedef NSString *const NStr;
68 extern NStr globalNSString;
69 void NSStringCheckTypedef() {
70 clang_analyzer_eval(globalNSString); // expected-warning{{TRUE}}
73 // Note that constness could be either inside
74 // the var declaration, or in a typedef.
75 typedef NSString *NStr2;
76 extern const NStr2 globalNSString2;
77 void NSStringCheckConstTypedef() {
78 clang_analyzer_eval(globalNSString2); // expected-warning{{TRUE}}
81 // Nested typedefs should work as well.
82 typedef const CFStringRef str1;
84 extern str2 globalStr2;
85 void testNestedTypedefs() {
86 clang_analyzer_eval(globalStr2); // expected-warning{{TRUE}}
89 // And for NSString *.
90 typedef NSString *const nstr1;
92 extern nstr2 nglobalStr2;
93 void testNestedTypedefsForNSString() {
94 clang_analyzer_eval(nglobalStr2); // expected-warning{{TRUE}}
97 // And for CFBooleanRefs.
98 extern const CFBooleanRef kBool;
99 void testNonnullBool() {
100 clang_analyzer_eval(kBool); // expected-warning{{TRUE}}
103 // And again, only for const one.
104 extern CFBooleanRef kBoolMutable;
105 void testNonnullNonconstBool() {
106 clang_analyzer_eval(kBoolMutable); // expected-warning{{UNKNOWN}}
109 // If it's annotated as nonnull, it doesn't even need to be const.
110 extern CFStringRef _Nonnull str3;
111 void testNonnullNonconstCFString() {
112 clang_analyzer_eval(str3); // expected-warning{{TRUE}}
115 // This one's nonnull for two reasons.
116 extern const CFStringRef _Nonnull str4;
117 void testNonnullNonnullCFString() {
118 clang_analyzer_eval(str4); // expected-warning{{TRUE}}
121 void test_kCFNull() {
122 clang_analyzer_eval(kCFNull); // expected-warning{{TRUE}}