blob: 902105b924dd8b2469705144bf5a774eb088368c [file] [log] [blame]
Fariborz Jahanian60acea42010-09-27 19:05:51 +00001#include "nonnull.h"
2
Patrick Beardb2f68202012-04-06 18:12:22 +00003// RUN: %clang_cc1 -fblocks -fsyntax-only -verify -Wno-objc-root-class %s
NAKAMURA Takumib774d732012-09-12 10:45:40 +00004// REQUIRES: LP64
Ted Kremenek465172f2008-07-21 22:09:15 +00005
Ted Kremenekdbfe99e2009-07-15 23:23:54 +00006@class NSObject;
7
Fariborz Jahanian60acea42010-09-27 19:05:51 +00008NONNULL_ATTR
9int f1(int x); // no warning
Ted Kremenek465172f2008-07-21 22:09:15 +000010int f2(int *x) __attribute__ ((nonnull (1)));
11int f3(int *x) __attribute__ ((nonnull (0))); // expected-error {{'nonnull' attribute parameter 1 is out of bounds}}
12int f4(int *x, int *y) __attribute__ ((nonnull (1,2)));
13int f5(int *x, int *y) __attribute__ ((nonnull (2,1)));
Ted Kremenekdbfe99e2009-07-15 23:23:54 +000014int f6(NSObject *x) __attribute__ ((nonnull (1))); // no-warning
15int f7(NSObject *x) __attribute__ ((nonnull)); // no-warning
16
Ted Kremenek465172f2008-07-21 22:09:15 +000017
Chris Lattner9b82ce92009-05-25 18:20:11 +000018extern void func1 (void (^block1)(), void (^block2)(), int) __attribute__((nonnull));
19
20extern void func3 (void (^block1)(), int, void (^block2)(), int)
21__attribute__((nonnull(1,3)));
22
23extern void func4 (void (^block1)(), void (^block2)()) __attribute__((nonnull(1)))
24__attribute__((nonnull(2)));
25
Chris Lattnere0303582010-01-09 20:43:19 +000026void func6();
27void func7();
28
Chris Lattner9b82ce92009-05-25 18:20:11 +000029void
30foo (int i1, int i2, int i3, void (^cp1)(), void (^cp2)(), void (^cp3)())
31{
32 func1(cp1, cp2, i1);
33
34 func1(0, cp2, i1); // expected-warning {{null passed to a callee which requires a non-null argument}}
35 func1(cp1, 0, i1); // expected-warning {{null passed to a callee which requires a non-null argument}}
36 func1(cp1, cp2, 0);
37
38
39 func3(0, i2, cp3, i3); // expected-warning {{null passed to a callee which requires a non-null argument}}
40 func3(cp3, i2, 0, i3); // expected-warning {{null passed to a callee which requires a non-null argument}}
41
42 func4(0, cp1); // expected-warning {{null passed to a callee which requires a non-null argument}}
43 func4(cp1, 0); // expected-warning {{null passed to a callee which requires a non-null argument}}
Ted Kremenekdbfe99e2009-07-15 23:23:54 +000044
45 // Shouldn't these emit warnings? Clang doesn't, and neither does GCC. It
46 // seems that the checking should handle Objective-C pointers.
47 func6((NSObject*) 0); // no-warning
48 func7((NSObject*) 0); // no-warning
Chris Lattner9b82ce92009-05-25 18:20:11 +000049}
Douglas Gregorc9ef4052010-08-12 18:48:43 +000050
Fariborz Jahanian60acea42010-09-27 19:05:51 +000051void func5(int) NONNULL_ATTR; // no warning
Fariborz Jahanianff3a0782010-09-27 22:42:37 +000052
53// rdar://6857843
54struct dispatch_object_s {
55 int x;
56};
57
58typedef union {
59 long first;
60 struct dispatch_object_s *_do;
61} dispatch_object_t __attribute__((transparent_union));
62
63__attribute__((nonnull))
64void _dispatch_queue_push_list(dispatch_object_t _head); // no warning
65
66void func6(dispatch_object_t _head) {
67 _dispatch_queue_push_list(0); // expected-warning {{null passed to a callee which requires a non-null argument}}
68 _dispatch_queue_push_list(_head._do); // no warning
69}
70
Fariborz Jahanian5272adf2011-04-15 22:06:22 +000071// rdar://9287695
72#define NULL (void*)0
73
74@interface NSObject
75- (void)doSomethingWithNonNullPointer:(void *)ptr :(int)iarg : (void*)ptr1 __attribute__((nonnull(1, 3)));
76+ (void)doSomethingClassyWithNonNullPointer:(void *)ptr __attribute__((nonnull(1)));
77@end
78
79extern void DoSomethingNotNull(void *db) __attribute__((nonnull(1)));
80
81@interface IMP
82{
83 void * vp;
84}
85@end
86
87@implementation IMP
88- (void) Meth {
89 NSObject *object;
90 [object doSomethingWithNonNullPointer:NULL:1:NULL]; // expected-warning 2 {{null passed to a callee which requires a non-null argument}}
91 [object doSomethingWithNonNullPointer:vp:1:NULL]; // expected-warning {{null passed to a callee which requires a non-null argument}}
92 [NSObject doSomethingClassyWithNonNullPointer:NULL]; // expected-warning {{null passed to a callee which requires a non-null argument}}
93 DoSomethingNotNull(NULL); // expected-warning {{null passed to a callee which requires a non-null argument}}
94 [object doSomethingWithNonNullPointer:vp:1:vp];
95}
96@end
97