1 // RUN: %clang_cc1 -fsyntax-only -verify %s
10 } Instance __attribute__((transparent_union));
12 __attribute__((nonnull(1))) void Class_init(Instance this, char *str) {
13 this.object->str = str;
18 Class_init(0, "Hello World"); // expected-warning {{null passed to a callee that requires a non-null argument}}
19 Class_init(obj, "Hello World");
22 void foo(const char *str) __attribute__((nonnull("foo"))); // expected-error{{'nonnull' attribute requires parameter 1 to be an integer constant}}
23 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}}
25 void baz(__attribute__((nonnull)) const char *str);
26 void baz2(__attribute__((nonnull(1))) const char *str); // expected-warning {{'nonnull' attribute when used on parameters takes no arguments}}
27 void baz3(__attribute__((nonnull)) int x); // expected-warning {{'nonnull' attribute only applies to pointer arguments}}
30 baz(0); // expected-warning {{null passed to a callee that requires a non-null argument}}
31 baz2(0); // no-warning
32 baz3(0); // no-warning
35 void test_void_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
36 int test_int_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
37 void *test_ptr_returns_nonnull(void) __attribute__((returns_nonnull)); // no-warning
39 int i __attribute__((nonnull)); // expected-warning {{'nonnull' attribute only applies to functions, methods, and parameters}}
40 int j __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to functions and methods}}
41 void *test_no_fn_proto() __attribute__((returns_nonnull)); // no-warning
42 void *test_with_fn_proto(void) __attribute__((returns_nonnull)); // no-warning
44 __attribute__((returns_nonnull))
45 void *test_bad_returns_null(void) {
46 return 0; // expected-warning {{null returned from function that requires a non-null return value}}
49 void PR18795(int (*g)(const char *h, ...) __attribute__((nonnull(1))) __attribute__((nonnull))) {
50 g(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
52 void PR18795_helper() {
53 PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
56 void vararg1(int n, ...) __attribute__((nonnull(2)));
59 vararg1(1, (void*)0); // expected-warning{{null passed}}
60 vararg1(2, (void*)0, (void*)0); // expected-warning{{null passed}}
61 vararg1(2, (void*)&vararg1, (void*)0);
64 void vararg2(int n, ...) __attribute__((nonnull, nonnull, nonnull));
67 vararg2(1, (void*)0); // expected-warning{{null passed}}
68 vararg2(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
71 void vararg3(int n, ...) __attribute__((nonnull, nonnull(2), nonnull(3)));
74 vararg3(1, (void*)0); // expected-warning{{null passed}}
75 vararg3(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
78 void redecl(void *, void *);
79 void redecl(void *, void *) __attribute__((nonnull(1)));
80 void redecl(void *, void *) __attribute__((nonnull(2)));
81 void redecl(void *, void *);
82 void redecl_test(void *p) {
83 redecl(p, 0); // expected-warning{{null passed}}
84 redecl(0, p); // expected-warning{{null passed}}
89 __attribute__((__nonnull__)) // expected-note 2{{declared 'nonnull' here}}
90 int evil_nonnull_func(int* pointer, void * pv)
92 if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is 'false' on first encounter}}
104 if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}}
107 void set_param_to_null(int**);
108 int another_evil_nonnull_func(int* pointer, char ch, void * pv) __attribute__((nonnull(1, 3))); // expected-note 2{{declared 'nonnull' here}}
109 int another_evil_nonnull_func(int* pointer, char ch, void * pv) {
110 if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is 'false' on first encounter}}
116 set_param_to_null(&pointer);
122 if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}}
125 extern void *returns_null(void**);
130 __attribute__((__nonnull__)) // expected-note {{declared 'nonnull' here}}
131 void yet_another_evil_nonnull_func(int* pointer)
134 // This comparison will not be optimized away.
135 if (pointer) { // expected-warning {{nonnull parameter 'pointer' will evaluate to 'true' on first encounter}}
140 pointer = returns_null(&pv);
144 void pr21668_1(__attribute__((nonnull)) const char *p, const char *s) { // expected-note {{declared 'nonnull' here}}
145 if (p) // expected-warning {{nonnull parameter 'p' will evaluate to 'true' on first encounter}}
151 void pr21668_2(__attribute__((nonnull)) const char *p) {
157 __attribute__((returns_nonnull)) void *returns_nonnull_whee(); // expected-note 6{{declared 'returns_nonnull' here}}
159 void returns_nonnull_warning_tests() {
160 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}}
162 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}}
164 if (returns_nonnull_whee()) {} // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}}
165 if (!returns_nonnull_whee()) {} // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}}
167 int and_again = !returns_nonnull_whee(); // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}}
168 and_again = !returns_nonnull_whee(); // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}}
171 void pr30828(char *p __attribute__((nonnull)));
172 void pr30828(char *p) {}
174 void call_pr30828() {
175 pr30828(0); // expected-warning {{null passed to a callee that requires a non-null argument}}