]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/Sema/nonnull.c
Vendor import of clang trunk r338150:
[FreeBSD/FreeBSD.git] / 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
8 typedef struct {
9         char *str;
10 } Class;
11
12 typedef 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
20 int 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
26 void foo(const char *str) __attribute__((nonnull("foo"))); // expected-error{{'nonnull' attribute requires parameter 1 to be an integer constant}}
27 void 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
29 void baz(__attribute__((nonnull)) const char *str);
30 void baz2(__attribute__((nonnull(1))) const char *str); // expected-warning {{'nonnull' attribute when used on parameters takes no arguments}}
31 void baz3(__attribute__((nonnull)) int x); // expected-warning {{'nonnull' attribute only applies to pointer arguments}}
32
33 void 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
39 void test_void_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
40 int test_int_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
41 void *test_ptr_returns_nonnull(void) __attribute__((returns_nonnull)); // no-warning
42
43 int i __attribute__((nonnull)); // expected-warning {{'nonnull' attribute only applies to functions, methods, and parameters}}
44 int j __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to Objective-C methods and functions}}
45 void *test_no_fn_proto() __attribute__((returns_nonnull)); // no-warning
46 void *test_with_fn_proto(void) __attribute__((returns_nonnull)); // no-warning
47
48 __attribute__((returns_nonnull))
49 void *test_bad_returns_null(void) {
50   return 0; // expected-warning {{null returned from function that requires a non-null return value}}
51 }
52
53 void 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 }
56 void PR18795_helper() {
57   PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
58 }
59
60 void vararg1(int n, ...) __attribute__((nonnull(2)));
61 void 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
68 void vararg2(int n, ...) __attribute__((nonnull, nonnull, nonnull));
69 void 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
75 void vararg3(int n, ...) __attribute__((nonnull, nonnull(2), nonnull(3)));
76 void 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
82 void redecl(void *, void *);
83 void redecl(void *, void *) __attribute__((nonnull(1)));
84 void redecl(void *, void *) __attribute__((nonnull(2)));
85 void redecl(void *, void *);
86 void 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}}
94 int 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
111 void set_param_to_null(int**);
112 int another_evil_nonnull_func(int* pointer, char ch, void * pv) __attribute__((nonnull(1, 3)));  // expected-note 2{{declared 'nonnull' here}}
113 int 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
129 extern void *returns_null(void**);
130 extern void FOO();
131 extern void FEE();
132
133 extern void *pv;
134 __attribute__((__nonnull__))  // expected-note {{declared 'nonnull' here}}
135 void 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
148 void 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
155 void 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
163 void 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
175 void pr30828(char *p __attribute__((nonnull)));
176 void pr30828(char *p) {}
177
178 void call_pr30828() {
179   pr30828(0); // expected-warning {{null passed to a callee that requires a non-null argument}}
180 }