Aaron Ballman | bf5cf8c | 2013-07-22 17:55:04 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 2 | // rdar://9584012 |
| 3 | |
| 4 | typedef struct { |
| 5 | char *str; |
| 6 | } Class; |
| 7 | |
| 8 | typedef union { |
| 9 | Class *object; |
| 10 | } Instance __attribute__((transparent_union)); |
| 11 | |
| 12 | __attribute__((nonnull(1))) void Class_init(Instance this, char *str) { |
| 13 | this.object->str = str; |
| 14 | } |
| 15 | |
| 16 | int main(void) { |
| 17 | Class *obj; |
| 18 | Class_init(0, "Hello World"); // expected-warning {{null passed to a callee which requires a non-null argument}} |
| 19 | Class_init(obj, "Hello World"); |
| 20 | } |
| 21 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 22 | void foo(const char *str) __attribute__((nonnull("foo"))); // expected-error{{'nonnull' attribute requires parameter 1 to be an integer constant}} |
Aaron Ballman | cedaaea | 2013-12-26 17:07:49 +0000 | [diff] [blame] | 23 | 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}} |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 24 | |
| 25 | void baz(__attribute__((nonnull)) const char *str); |
| 26 | void baz2(__attribute__((nonnull(1))) const char *str); // expected-warning {{'nonnull' attribute when used on parameters takes no arguments}} |
| 27 | void baz3(__attribute__((nonnull)) int x); // expected-warning {{'nonnull' attribute only applies to pointer arguments}} |
| 28 | |
| 29 | void test_baz() { |
| 30 | baz(0); // expected-warning {{null passed to a callee which requires a non-null argument}} |
| 31 | baz2(0); // no-warning |
| 32 | baz3(0); // no-warning |
| 33 | } |
| 34 | |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 35 | void test_void_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}} |
| 36 | int test_int_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}} |
| 37 | void *test_ptr_returns_nonnull(void) __attribute__((returns_nonnull)); // no-warning |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 38 | |
Aaron Ballman | d6432f8 | 2014-01-17 14:38:58 +0000 | [diff] [blame] | 39 | int i __attribute__((nonnull)); // expected-warning {{'nonnull' attribute only applies to functions, methods, and parameters}} |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 40 | int j __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to functions and methods}} |
Aaron Ballman | 6288d06 | 2014-07-11 16:31:29 +0000 | [diff] [blame] | 41 | void *test_no_fn_proto() __attribute__((returns_nonnull)); // no-warning |
| 42 | void *test_with_fn_proto(void) __attribute__((returns_nonnull)); // no-warning |
Ted Kremenek | ef9e7f8 | 2014-01-22 06:10:28 +0000 | [diff] [blame] | 43 | |
| 44 | __attribute__((returns_nonnull)) |
| 45 | void *test_bad_returns_null(void) { |
| 46 | return 0; // expected-warning {{null returned from function that requires a non-null return value}} |
| 47 | } |
| 48 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 49 | void PR18795(int (*g)(const char *h, ...) __attribute__((nonnull(1))) __attribute__((nonnull))) { |
| 50 | g(0); // expected-warning{{null passed to a callee which requires a non-null argument}} |
| 51 | } |
| 52 | void PR18795_helper() { |
| 53 | PR18795(0); // expected-warning{{null passed to a callee which requires a non-null argument}} |
| 54 | } |
| 55 | |
| 56 | |