File: nonnull-global-constants.mm

package info (click to toggle)
llvm-toolchain-7 1%3A7.0.1-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 733,456 kB
  • sloc: cpp: 3,776,651; ansic: 633,271; asm: 350,301; python: 142,716; objc: 107,612; sh: 22,626; lisp: 11,056; perl: 7,999; pascal: 6,742; ml: 5,537; awk: 3,536; makefile: 2,557; cs: 2,027; xml: 841; ruby: 156
file content (103 lines) | stat: -rw-r--r-- 3,022 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify %s

// Nullability of const string-like globals, testing
// NonnullGlobalConstantsChecker.

void clang_analyzer_eval(bool);

@class NSString;
typedef const struct __CFString *CFStringRef;
typedef const struct __CFBoolean * CFBooleanRef;

// Global NSString* is non-null.
extern NSString *const StringConstGlobal;
void stringConstGlobal() {
  clang_analyzer_eval(StringConstGlobal); // expected-warning{{TRUE}}
}

// The logic does not apply to local variables though.
extern NSString *stringGetter();
void stringConstLocal() {
  NSString *const local = stringGetter();
  clang_analyzer_eval(local); // expected-warning{{UNKNOWN}}
}

// Global const CFStringRef's are also assumed to be non-null.
extern const CFStringRef CFStringConstGlobal;
void cfStringCheckGlobal() {
  clang_analyzer_eval(CFStringConstGlobal); // expected-warning{{TRUE}}
}

// But only "const" ones.
extern CFStringRef CFStringNonConstGlobal;
void cfStringCheckMutableGlobal() {
  clang_analyzer_eval(CFStringNonConstGlobal); // expected-warning{{UNKNOWN}}
}

// char* const is also assumed to be non-null.
extern const char *const ConstCharStarConst;
void constCharStarCheckGlobal() {
  clang_analyzer_eval(ConstCharStarConst); // expected-warning{{TRUE}}
}

// Pointer value can be mutable.
extern char *const CharStarConst;
void charStarCheckGlobal() {
  clang_analyzer_eval(CharStarConst); // expected-warning{{TRUE}}
}

// But the pointer itself should be immutable.
extern char *CharStar;
void charStartCheckMutableGlobal() {
  clang_analyzer_eval(CharStar); // expected-warning{{UNKNOWN}}
}

// Type definitions should also work across typedefs, for pointers:
typedef char *const str;
extern str globalStr;
void charStarCheckTypedef() {
  clang_analyzer_eval(globalStr); // expected-warning{{TRUE}}
}

// And for types.
typedef NSString *const NStr;
extern NStr globalNSString;
void NSStringCheckTypedef() {
  clang_analyzer_eval(globalNSString); // expected-warning{{TRUE}}
}

// Note that constness could be either inside
// the var declaration, or in a typedef.
typedef NSString *NStr2;
extern const NStr2 globalNSString2;
void NSStringCheckConstTypedef() {
  clang_analyzer_eval(globalNSString2); // expected-warning{{TRUE}}
}

// Nested typedefs should work as well.
typedef const CFStringRef str1;
typedef str1 str2;
extern str2 globalStr2;
void testNestedTypedefs() {
  clang_analyzer_eval(globalStr2); // expected-warning{{TRUE}}
}

// And for NSString *.
typedef NSString *const nstr1;
typedef nstr1 nstr2;
extern nstr2 nglobalStr2;
void testNestedTypedefsForNSString() {
  clang_analyzer_eval(nglobalStr2); // expected-warning{{TRUE}}
}

// And for CFBooleanRefs.
extern const CFBooleanRef kBool;
void testNonnullBool() {
  clang_analyzer_eval(kBool); // expected-warning{{TRUE}}
}

// And again, only for const one.
extern CFBooleanRef kBoolMutable;
void testNonnullNonconstBool() {
  clang_analyzer_eval(kBoolMutable); // expected-warning{{UNKNOWN}}
}