Clang Project

clang_source_code/test/Sema/nonnull.c
1// RUN: %clang_cc1 -fsyntax-only -verify %s
2// rdar://9584012
3//
4// Verify All warnings are still issued with the option -fno-delete-null-pointer-checks
5// if nullptr is passed to function with nonnull attribute.
6// RUN: %clang_cc1 -fsyntax-only -fno-delete-null-pointer-checks -verify %s
7
8typedef struct {
9 char *str;
10} Class;
11
12typedef union {
13 Class *object;
14} Instance __attribute__((transparent_union));
15
16__attribute__((nonnull(1))) void Class_init(Instance this, char *str) {
17 this.object->str = str;
18}
19
20int main(void) {
21 Class *obj;
22 Class_init(0, "Hello World"); // expected-warning {{null passed to a callee that requires a non-null argument}}
23 Class_init(obj, "Hello World");
24}
25
26void foo(const char *str) __attribute__((nonnull("foo"))); // expected-error{{'nonnull' attribute requires parameter 1 to be an integer constant}}
27void bar(int i) __attribute__((nonnull(1))); // expected-warning {{'nonnull' attribute only applies to pointer arguments}} expected-warning {{'nonnull' attribute applied to function with no pointer arguments}}
28
29void baz(__attribute__((nonnull)) const char *str);
30void baz2(__attribute__((nonnull(1))) const char *str); // expected-warning {{'nonnull' attribute when used on parameters takes no arguments}}
31void baz3(__attribute__((nonnull)) int x); // expected-warning {{'nonnull' attribute only applies to pointer arguments}}
32
33void test_baz() {
34  baz(0); // expected-warning {{null passed to a callee that requires a non-null argument}}
35  baz2(0); // no-warning
36  baz3(0); // no-warning
37}
38
39void test_void_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
40int test_int_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
41void *test_ptr_returns_nonnull(void) __attribute__((returns_nonnull)); // no-warning
42
43int i __attribute__((nonnull)); // expected-warning {{'nonnull' attribute only applies to functions, methods, and parameters}}
44int j __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to Objective-C methods and functions}}
45void *test_no_fn_proto() __attribute__((returns_nonnull)); // no-warning
46void *test_with_fn_proto(void) __attribute__((returns_nonnull)); // no-warning
47
48__attribute__((returns_nonnull))
49void *test_bad_returns_null(void) {
50  return 0; // expected-warning {{null returned from function that requires a non-null return value}}
51}
52
53void PR18795(int (*g)(const char *h, ...) __attribute__((nonnull(1))) __attribute__((nonnull))) {
54  g(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
55}
56void PR18795_helper() {
57  PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
58}
59
60void vararg1(int n, ...) __attribute__((nonnull(2)));
61void vararg1_test() {
62  vararg1(0);
63  vararg1(1, (void*)0); // expected-warning{{null passed}}
64  vararg1(2, (void*)0, (void*)0); // expected-warning{{null passed}}
65  vararg1(2, (void*)&vararg1, (void*)0);
66}
67
68void vararg2(int n, ...) __attribute__((nonnull, nonnull, nonnull));
69void vararg2_test() {
70  vararg2(0);
71  vararg2(1, (void*)0); // expected-warning{{null passed}}
72  vararg2(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
73}
74
75void vararg3(int n, ...) __attribute__((nonnull, nonnull(2), nonnull(3)));
76void vararg3_test() {
77  vararg3(0);
78  vararg3(1, (void*)0); // expected-warning{{null passed}}
79  vararg3(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
80}
81
82void redecl(void *, void *);
83void redecl(void *, void *) __attribute__((nonnull(1)));
84void redecl(void *, void *) __attribute__((nonnull(2)));
85void redecl(void *, void *);
86void redecl_test(void *p) {
87  redecl(p, 0); // expected-warning{{null passed}}
88  redecl(0, p); // expected-warning{{null passed}}
89}
90
91// rdar://18712242
92#define NULL (void*)0
93__attribute__((__nonnull__))  // expected-note 2{{declared 'nonnull' here}}
94int evil_nonnull_func(int* pointer, void * pv)
95{
96   if (pointer == NULL) {  // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is 'false' on first encounter}}
97     return 0;
98   } else {
99     return *pointer;
100   } 
101
102   pointer = pv;
103   if (!pointer)
104     return 0;
105   else
106     return *pointer;
107
108   if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}}
109}
110
111void set_param_to_null(int**);
112int another_evil_nonnull_func(int* pointer, char ch, void * pv) __attribute__((nonnull(1, 3)));  // expected-note 2{{declared 'nonnull' here}}
113int another_evil_nonnull_func(int* pointer, char ch, void * pv) {
114   if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is 'false' on first encounter}}
115     return 0;
116   } else {
117     return *pointer;
118   } 
119
120   set_param_to_null(&pointer);
121   if (!pointer)
122     return 0;
123   else
124     return *pointer;
125
126   if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}}
127}
128
129extern void *returns_null(void**);
130extern void FOO();
131extern void FEE();
132
133extern void *pv;
134__attribute__((__nonnull__))  // expected-note {{declared 'nonnull' here}}
135void yet_another_evil_nonnull_func(int* pointer)
136{
137 while (pv) {
138   // This comparison will not be optimized away.
139   if (pointer) {  // expected-warning {{nonnull parameter 'pointer' will evaluate to 'true' on first encounter}}
140     FOO();
141   } else {
142     FEE();
143   } 
144   pointer = returns_null(&pv);
145 }
146}
147
148void pr21668_1(__attribute__((nonnull)) const char *p, const char *s) { // expected-note {{declared 'nonnull' here}}
149  if (p) // expected-warning {{nonnull parameter 'p' will evaluate to 'true' on first encounter}}
150    ;
151  if (s) // No warning
152    ;
153}
154
155void pr21668_2(__attribute__((nonnull)) const char *p) {
156  p = 0;
157  if (p) // No warning
158    ;
159}
160
161__attribute__((returns_nonnull)) void *returns_nonnull_whee();  // expected-note 6{{declared 'returns_nonnull' here}}
162
163void returns_nonnull_warning_tests() {
164  if (returns_nonnull_whee() == NULL) {} // expected-warning {{comparison of nonnull function call 'returns_nonnull_whee()' equal to a null pointer is 'false' on first encounter}}
165
166  if (returns_nonnull_whee() != NULL) {} // expected-warning {{comparison of nonnull function call 'returns_nonnull_whee()' not equal to a null pointer is 'true' on first encounter}}
167
168  if (returns_nonnull_whee()) {} // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}}
169  if (!returns_nonnull_whee()) {} // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}}
170
171  int and_again = !returns_nonnull_whee(); // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}}
172  and_again = !returns_nonnull_whee(); // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}}
173}
174
175void pr30828(char *p __attribute__((nonnull)));
176void pr30828(char *p) {}
177
178void call_pr30828() {
179  pr30828(0); // expected-warning {{null passed to a callee that requires a non-null argument}}
180}
181