[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Sema / nonnull.c
blob0b30243f21d5846ad848ffac39fb73b7205b978d
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-strict-prototypes %s
2 //
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
7 typedef struct {
8 char *str;
9 } Class;
11 typedef union {
12 Class *object;
13 } Instance __attribute__((transparent_union));
15 __attribute__((nonnull(1))) void Class_init(Instance this, char *str) {
16 this.object->str = str;
19 int main(void) {
20 Class *obj;
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}}
32 void test_baz(void) {
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) {
61 vararg1(0);
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) {
69 vararg2(0);
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) {
76 vararg3(0);
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}}
90 #define NULL (void*)0
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}}
95 return 0;
96 } else {
97 return *pointer;
100 pointer = pv;
101 if (!pointer)
102 return 0;
103 else
104 return *pointer;
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}}
113 return 0;
114 } else {
115 return *pointer;
118 set_param_to_null(&pointer);
119 if (!pointer)
120 return 0;
121 else
122 return *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);
131 extern void *pv;
132 __attribute__((__nonnull__)) // expected-note {{declared 'nonnull' here}}
133 void yet_another_evil_nonnull_func(int* pointer)
135 while (pv) {
136 // This comparison will not be optimized away.
137 if (pointer) { // expected-warning {{nonnull parameter 'pointer' will evaluate to 'true' on first encounter}}
138 FOO();
139 } else {
140 FEE();
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}}
149 if (s) // No warning
153 void pr21668_2(__attribute__((nonnull)) const char *p) {
154 p = 0;
155 if (p) // No warning
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}}