[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / Sema / nonnull.c
blob40eeb11e0bf8243d8c61229899a394cb1f5cd486
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-strict-prototypes %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
8 typedef struct {
9 char *str;
10 } Class;
12 typedef union {
13 Class *object;
14 } Instance __attribute__((transparent_union));
16 __attribute__((nonnull(1))) void Class_init(Instance this, char *str) {
17 this.object->str = str;
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");
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}}
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}}
33 void test_baz(void) {
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
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
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
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}}
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}}
56 void PR18795_helper(void) {
57 PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
60 void vararg1(int n, ...) __attribute__((nonnull(2)));
61 void vararg1_test(void) {
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);
68 void vararg2(int n, ...) __attribute__((nonnull, nonnull, nonnull));
69 void vararg2_test(void) {
70 vararg2(0);
71 vararg2(1, (void*)0); // expected-warning{{null passed}}
72 vararg2(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
75 void vararg3(int n, ...) __attribute__((nonnull, nonnull(2), nonnull(3)));
76 void vararg3_test(void) {
77 vararg3(0);
78 vararg3(1, (void*)0); // expected-warning{{null passed}}
79 vararg3(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
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}}
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)
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;
102 pointer = pv;
103 if (!pointer)
104 return 0;
105 else
106 return *pointer;
108 if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}}
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;
120 set_param_to_null(&pointer);
121 if (!pointer)
122 return 0;
123 else
124 return *pointer;
126 if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}}
129 extern void *returns_null(void**);
130 extern void FOO(void);
131 extern void FEE(void);
133 extern void *pv;
134 __attribute__((__nonnull__)) // expected-note {{declared 'nonnull' here}}
135 void yet_another_evil_nonnull_func(int* pointer)
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();
144 pointer = returns_null(&pv);
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}}
151 if (s) // No warning
155 void pr21668_2(__attribute__((nonnull)) const char *p) {
156 p = 0;
157 if (p) // No warning
161 __attribute__((returns_nonnull)) void *returns_nonnull_whee(void); // expected-note 6{{declared 'returns_nonnull' here}}
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}}
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}}
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}}
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}}
175 void pr30828(char *p __attribute__((nonnull)));
176 void pr30828(char *p) {}
178 void call_pr30828(void) {
179 pr30828(0); // expected-warning {{null passed to a callee that requires a non-null argument}}