1 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-strict-prototypes %s
3 // Verify All warnings are still issued with the option -fno-delete-null-pointer-checks
4 // if nullptr is passed to function with nonnull attribute.
5 // RUN: %clang_cc1 -fsyntax-only -fno-delete-null-pointer-checks -verify %s
13 } Instance
__attribute__((transparent_union
));
15 __attribute__((nonnull(1))) void Class_init(Instance
this, char *str
) {
16 this.object
->str
= str
;
21 Class_init(0, "Hello World"); // expected-warning {{null passed to a callee that requires a non-null argument}}
22 Class_init(obj
, "Hello World");
25 void foo(const char *str
) __attribute__((nonnull("foo"))); // expected-error{{'nonnull' attribute requires parameter 1 to be an integer constant}}
26 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 void baz(__attribute__((nonnull
)) const char *str
);
29 void baz2(__attribute__((nonnull(1))) const char *str
); // expected-warning {{'nonnull' attribute when used on parameters takes no arguments}}
30 void baz3(__attribute__((nonnull
)) int x
); // expected-warning {{'nonnull' attribute only applies to pointer arguments}}
33 baz(0); // expected-warning {{null passed to a callee that requires a non-null argument}}
34 baz2(0); // no-warning
35 baz3(0); // no-warning
38 void test_void_returns_nonnull(void) __attribute__((returns_nonnull
)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
39 int test_int_returns_nonnull(void) __attribute__((returns_nonnull
)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
40 void *test_ptr_returns_nonnull(void) __attribute__((returns_nonnull
)); // no-warning
42 int i
__attribute__((nonnull
)); // expected-warning {{'nonnull' attribute only applies to functions, methods, and parameters}}
43 int j
__attribute__((returns_nonnull
)); // expected-warning {{'returns_nonnull' attribute only applies to Objective-C methods and functions}}
44 void *test_no_fn_proto() __attribute__((returns_nonnull
)); // no-warning
45 void *test_with_fn_proto(void) __attribute__((returns_nonnull
)); // no-warning
47 __attribute__((returns_nonnull
))
48 void *test_bad_returns_null(void) {
49 return 0; // expected-warning {{null returned from function that requires a non-null return value}}
52 void PR18795(int (*g
)(const char *h
, ...) __attribute__((nonnull(1))) __attribute__((nonnull
))) {
53 g(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
55 void PR18795_helper(void) {
56 PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
59 void vararg1(int n
, ...) __attribute__((nonnull(2)));
60 void vararg1_test(void) {
62 vararg1(1, (void*)0); // expected-warning{{null passed}}
63 vararg1(2, (void*)0, (void*)0); // expected-warning{{null passed}}
64 vararg1(2, (void*)&vararg1
, (void*)0);
67 void vararg2(int n
, ...) __attribute__((nonnull
, nonnull
, nonnull
));
68 void vararg2_test(void) {
70 vararg2(1, (void*)0); // expected-warning{{null passed}}
71 vararg2(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
74 void vararg3(int n
, ...) __attribute__((nonnull
, nonnull(2), nonnull(3)));
75 void vararg3_test(void) {
77 vararg3(1, (void*)0); // expected-warning{{null passed}}
78 vararg3(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
81 void redecl(void *, void *);
82 void redecl(void *, void *) __attribute__((nonnull(1)));
83 void redecl(void *, void *) __attribute__((nonnull(2)));
84 void redecl(void *, void *);
85 void redecl_test(void *p
) {
86 redecl(p
, 0); // expected-warning{{null passed}}
87 redecl(0, p
); // expected-warning{{null passed}}
91 __attribute__((__nonnull__
)) // expected-note 2{{declared 'nonnull' here}}
92 int evil_nonnull_func(int* pointer
, void * pv
)
94 if (pointer
== NULL
) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is 'false' on first encounter}}
106 if (pv
== NULL
) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}}
109 void set_param_to_null(int**);
110 int another_evil_nonnull_func(int* pointer
, char ch
, void * pv
) __attribute__((nonnull(1, 3))); // expected-note 2{{declared 'nonnull' here}}
111 int another_evil_nonnull_func(int* pointer
, char ch
, void * pv
) {
112 if (pointer
== NULL
) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is 'false' on first encounter}}
118 set_param_to_null(&pointer
);
124 if (pv
== NULL
) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}}
127 extern void *returns_null(void**);
128 extern void FOO(void);
129 extern void FEE(void);
132 __attribute__((__nonnull__
)) // expected-note {{declared 'nonnull' here}}
133 void yet_another_evil_nonnull_func(int* pointer
)
136 // This comparison will not be optimized away.
137 if (pointer
) { // expected-warning {{nonnull parameter 'pointer' will evaluate to 'true' on first encounter}}
142 pointer
= returns_null(&pv
);
146 void pr21668_1(__attribute__((nonnull
)) const char *p
, const char *s
) { // expected-note {{declared 'nonnull' here}}
147 if (p
) // expected-warning {{nonnull parameter 'p' will evaluate to 'true' on first encounter}}
153 void pr21668_2(__attribute__((nonnull
)) const char *p
) {
159 __attribute__((returns_nonnull
)) void *returns_nonnull_whee(void); // expected-note 6{{declared 'returns_nonnull' here}}
161 void returns_nonnull_warning_tests() {
162 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}}
164 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}}
166 if (returns_nonnull_whee()) {} // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}}
167 if (!returns_nonnull_whee()) {} // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}}
169 int and_again
= !returns_nonnull_whee(); // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}}
170 and_again
= !returns_nonnull_whee(); // expected-warning {{nonnull function call 'returns_nonnull_whee()' will evaluate to 'true' on first encounter}}
173 void pr30828(char *p
__attribute__((nonnull
)));
174 void pr30828(char *p
) {}
176 void call_pr30828(void) {
177 pr30828(0); // expected-warning {{null passed to a callee that requires a non-null argument}}