diff options
Diffstat (limited to 'test/Sema/nonnull.c')
-rw-r--r-- | test/Sema/nonnull.c | 107 |
1 files changed, 103 insertions, 4 deletions
diff --git a/test/Sema/nonnull.c b/test/Sema/nonnull.c index b1f69208241b1..4b3df8518cfc1 100644 --- a/test/Sema/nonnull.c +++ b/test/Sema/nonnull.c @@ -15,7 +15,7 @@ __attribute__((nonnull(1))) void Class_init(Instance this, char *str) { int main(void) { Class *obj; - Class_init(0, "Hello World"); // expected-warning {{null passed to a callee which requires a non-null argument}} + Class_init(0, "Hello World"); // expected-warning {{null passed to a callee that requires a non-null argument}} Class_init(obj, "Hello World"); } @@ -27,7 +27,7 @@ void baz2(__attribute__((nonnull(1))) const char *str); // expected-warning {{'n void baz3(__attribute__((nonnull)) int x); // expected-warning {{'nonnull' attribute only applies to pointer arguments}} void test_baz() { - baz(0); // expected-warning {{null passed to a callee which requires a non-null argument}} + baz(0); // expected-warning {{null passed to a callee that requires a non-null argument}} baz2(0); // no-warning baz3(0); // no-warning } @@ -47,10 +47,109 @@ void *test_bad_returns_null(void) { } void PR18795(int (*g)(const char *h, ...) __attribute__((nonnull(1))) __attribute__((nonnull))) { - g(0); // expected-warning{{null passed to a callee which requires a non-null argument}} + g(0); // expected-warning{{null passed to a callee that requires a non-null argument}} } void PR18795_helper() { - PR18795(0); // expected-warning{{null passed to a callee which requires a non-null argument}} + PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}} } +void vararg1(int n, ...) __attribute__((nonnull(2))); +void vararg1_test() { + vararg1(0); + vararg1(1, (void*)0); // expected-warning{{null passed}} + vararg1(2, (void*)0, (void*)0); // expected-warning{{null passed}} + vararg1(2, (void*)&vararg1, (void*)0); +} + +void vararg2(int n, ...) __attribute__((nonnull, nonnull, nonnull)); +void vararg2_test() { + vararg2(0); + vararg2(1, (void*)0); // expected-warning{{null passed}} + vararg2(2, (void*)0, (void*)0); // expected-warning 2{{null passed}} +} + +void vararg3(int n, ...) __attribute__((nonnull, nonnull(2), nonnull(3))); +void vararg3_test() { + vararg3(0); + vararg3(1, (void*)0); // expected-warning{{null passed}} + vararg3(2, (void*)0, (void*)0); // expected-warning 2{{null passed}} +} + +void redecl(void *, void *); +void redecl(void *, void *) __attribute__((nonnull(1))); +void redecl(void *, void *) __attribute__((nonnull(2))); +void redecl(void *, void *); +void redecl_test(void *p) { + redecl(p, 0); // expected-warning{{null passed}} + redecl(0, p); // expected-warning{{null passed}} +} + +// rdar://18712242 +#define NULL (void*)0 +__attribute__((__nonnull__)) +int evil_nonnull_func(int* pointer, void * pv) +{ + if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is false on first encounter}} + return 0; + } else { + return *pointer; + } + + pointer = pv; + if (!pointer) + return 0; + else + return *pointer; + + if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is false on first encounter}} +} +void set_param_to_null(int**); +int another_evil_nonnull_func(int* pointer, char ch, void * pv) __attribute__((nonnull(1, 3))); +int another_evil_nonnull_func(int* pointer, char ch, void * pv) { + if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is false on first encounter}} + return 0; + } else { + return *pointer; + } + + set_param_to_null(&pointer); + if (!pointer) + return 0; + else + return *pointer; + + if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is false on first encounter}} +} + +extern void *returns_null(void**); +extern void FOO(); +extern void FEE(); + +extern void *pv; +__attribute__((__nonnull__)) +void yet_another_evil_nonnull_func(int* pointer) +{ + while (pv) { + // This comparison will not be optimized away. + if (pointer) { // expected-warning {{nonnull parameter 'pointer' will evaluate to 'true' on first encounter}} + FOO(); + } else { + FEE(); + } + pointer = returns_null(&pv); + } +} + +void pr21668_1(__attribute__((nonnull)) const char *p, const char *s) { + if (p) // expected-warning {{nonnull parameter 'p' will evaluate to 'true' on first encounter}} + ; + if (s) // No warning + ; +} + +void pr21668_2(__attribute__((nonnull)) const char *p) { + p = 0; + if (p) // No warning + ; +} |