Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1 | #include "nonnull.h" |
| 2 | |
Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 3 | // RUN: %clang_cc1 -fblocks -fsyntax-only -verify %s |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 4 | |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 5 | @class NSObject; |
| 6 | |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 7 | NONNULL_ATTR |
| 8 | int f1(int x); // no warning |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 9 | int f2(int *x) __attribute__ ((nonnull (1))); |
| 10 | int f3(int *x) __attribute__ ((nonnull (0))); // expected-error {{'nonnull' attribute parameter 1 is out of bounds}} |
| 11 | int f4(int *x, int *y) __attribute__ ((nonnull (1,2))); |
| 12 | int f5(int *x, int *y) __attribute__ ((nonnull (2,1))); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 13 | int f6(NSObject *x) __attribute__ ((nonnull (1))); // no-warning |
| 14 | int f7(NSObject *x) __attribute__ ((nonnull)); // no-warning |
| 15 | |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 16 | |
Chris Lattner | 9b82ce9 | 2009-05-25 18:20:11 +0000 | [diff] [blame] | 17 | extern void func1 (void (^block1)(), void (^block2)(), int) __attribute__((nonnull)); |
| 18 | |
| 19 | extern void func3 (void (^block1)(), int, void (^block2)(), int) |
| 20 | __attribute__((nonnull(1,3))); |
| 21 | |
| 22 | extern void func4 (void (^block1)(), void (^block2)()) __attribute__((nonnull(1))) |
| 23 | __attribute__((nonnull(2))); |
| 24 | |
Chris Lattner | e030358 | 2010-01-09 20:43:19 +0000 | [diff] [blame] | 25 | void func6(); |
| 26 | void func7(); |
| 27 | |
Chris Lattner | 9b82ce9 | 2009-05-25 18:20:11 +0000 | [diff] [blame] | 28 | void |
| 29 | foo (int i1, int i2, int i3, void (^cp1)(), void (^cp2)(), void (^cp3)()) |
| 30 | { |
| 31 | func1(cp1, cp2, i1); |
| 32 | |
| 33 | func1(0, cp2, i1); // expected-warning {{null passed to a callee which requires a non-null argument}} |
| 34 | func1(cp1, 0, i1); // expected-warning {{null passed to a callee which requires a non-null argument}} |
| 35 | func1(cp1, cp2, 0); |
| 36 | |
| 37 | |
| 38 | func3(0, i2, cp3, i3); // expected-warning {{null passed to a callee which requires a non-null argument}} |
| 39 | func3(cp3, i2, 0, i3); // expected-warning {{null passed to a callee which requires a non-null argument}} |
| 40 | |
| 41 | func4(0, cp1); // expected-warning {{null passed to a callee which requires a non-null argument}} |
| 42 | func4(cp1, 0); // expected-warning {{null passed to a callee which requires a non-null argument}} |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 43 | |
| 44 | // Shouldn't these emit warnings? Clang doesn't, and neither does GCC. It |
| 45 | // seems that the checking should handle Objective-C pointers. |
| 46 | func6((NSObject*) 0); // no-warning |
| 47 | func7((NSObject*) 0); // no-warning |
Chris Lattner | 9b82ce9 | 2009-05-25 18:20:11 +0000 | [diff] [blame] | 48 | } |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 49 | |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 50 | void func5(int) NONNULL_ATTR; // no warning |
Fariborz Jahanian | ff3a078 | 2010-09-27 22:42:37 +0000 | [diff] [blame] | 51 | |
| 52 | // rdar://6857843 |
| 53 | struct dispatch_object_s { |
| 54 | int x; |
| 55 | }; |
| 56 | |
| 57 | typedef union { |
| 58 | long first; |
| 59 | struct dispatch_object_s *_do; |
| 60 | } dispatch_object_t __attribute__((transparent_union)); |
| 61 | |
| 62 | __attribute__((nonnull)) |
| 63 | void _dispatch_queue_push_list(dispatch_object_t _head); // no warning |
| 64 | |
| 65 | void func6(dispatch_object_t _head) { |
| 66 | _dispatch_queue_push_list(0); // expected-warning {{null passed to a callee which requires a non-null argument}} |
| 67 | _dispatch_queue_push_list(_head._do); // no warning |
| 68 | } |
| 69 | |