| 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 |
| 6 |
|
| 7 |
typedef struct { |
| 8 |
char *str; |
| 9 |
} Class; |
| 10 |
|
| 11 |
typedef union { |
| 12 |
Class *object; |
| 13 |
} Instance __attribute__((transparent_union)); |
| 14 |
|
| 15 |
__attribute__((nonnull(1))) void Class_init(Instance this, char *str) { |
| 16 |
this.object->str = str; |
| 17 |
} |
| 18 |
|
| 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"); |
| 23 |
} |
| 24 |
|
| 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}} |
| 27 |
|
| 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}} |
| 31 |
|
| 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 |
| 36 |
} |
| 37 |
|
| 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 |
| 41 |
|
| 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 |
| 46 |
|
| 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}} |
| 50 |
} |
| 51 |
|
| 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}} |
| 54 |
} |
| 55 |
void PR18795_helper(void) { |
| 56 |
PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}} |
| 57 |
} |
| 58 |
|
| 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); |
| 65 |
} |
| 66 |
|
| 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}} |
| 72 |
} |
| 73 |
|
| 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}} |
| 79 |
} |
| 80 |
|
| 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}} |
| 88 |
} |
| 89 |
|
| 90 |
#define NULL (void*)0 |
| 91 |
__attribute__((__nonnull__)) // expected-note 2{{declared 'nonnull' here}} |
| 92 |
int evil_nonnull_func(int* pointer, void * pv) |
| 93 |
{ |
| 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; |
| 98 |
} |
| 99 |
|
| 100 |
pointer = pv; |
| 101 |
if (!pointer) |
| 102 |
return 0; |
| 103 |
else |
| 104 |
return *pointer; |
| 105 |
|
| 106 |
if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}} |
| 107 |
} |
| 108 |
|
| 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; |
| 116 |
} |
| 117 |
|
| 118 |
set_param_to_null(&pointer); |
| 119 |
if (!pointer) |
| 120 |
return 0; |
| 121 |
else |
| 122 |
return *pointer; |
| 123 |
|
| 124 |
if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is 'false' on first encounter}} |
| 125 |
} |
| 126 |
|
| 127 |
extern void *returns_null(void**); |
| 128 |
extern void FOO(void); |
| 129 |
extern void FEE(void); |
| 130 |
|
| 131 |
extern void *pv; |
| 132 |
__attribute__((__nonnull__)) // expected-note {{declared 'nonnull' here}} |
| 133 |
void yet_another_evil_nonnull_func(int* pointer) |
| 134 |
{ |
| 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(); |
| 141 |
} |
| 142 |
pointer = returns_null(&pv); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 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}} |
| 148 |
; |
| 149 |
if (s) // No warning |
| 150 |
; |
| 151 |
} |
| 152 |
|
| 153 |
void pr21668_2(__attribute__((nonnull)) const char *p) { |
| 154 |
p = 0; |
| 155 |
if (p) // No warning |
| 156 |
; |
| 157 |
} |
| 158 |
|
| 159 |
__attribute__((returns_nonnull)) void *returns_nonnull_whee(void); // expected-note 6{{declared 'returns_nonnull' here}} |
| 160 |
|
| 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}} |
| 163 |
|
| 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}} |
| 165 |
|
| 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}} |
| 168 |
|
| 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}} |
| 171 |
} |
| 172 |
|
| 173 |
void pr30828(char *p __attribute__((nonnull))); |
| 174 |
void pr30828(char *p) {} |
| 175 |
|
| 176 |
void call_pr30828(void) { |
| 177 |
pr30828(0); // expected-warning {{null passed to a callee that requires a non-null argument}} |
| 178 |
} |