xref: /minix3/external/bsd/llvm/dist/clang/test/SemaObjC/nonnull.m (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc#include "nonnull.h"
2f4a2713aSLionel Sambuc
3f4a2713aSLionel Sambuc// RUN: %clang_cc1 -fblocks -fsyntax-only -verify -Wno-objc-root-class %s
4f4a2713aSLionel Sambuc// REQUIRES: LP64
5f4a2713aSLionel Sambuc
6f4a2713aSLionel Sambuc@class NSObject;
7f4a2713aSLionel Sambuc
8f4a2713aSLionel SambucNONNULL_ATTR
9f4a2713aSLionel Sambucint f1(int x); //  no warning
10f4a2713aSLionel Sambucint f2(int *x) __attribute__ ((nonnull (1)));
11f4a2713aSLionel Sambucint f3(int *x) __attribute__ ((nonnull (0))); // expected-error {{'nonnull' attribute parameter 1 is out of bounds}}
12f4a2713aSLionel Sambucint f4(int *x, int *y) __attribute__ ((nonnull (1,2)));
13f4a2713aSLionel Sambucint f5(int *x, int *y) __attribute__ ((nonnull (2,1)));
14f4a2713aSLionel Sambucint f6(NSObject *x) __attribute__ ((nonnull (1))); // no-warning
15f4a2713aSLionel Sambucint f7(NSObject *x) __attribute__ ((nonnull)); // no-warning
16f4a2713aSLionel Sambuc
17f4a2713aSLionel Sambuc
18f4a2713aSLionel Sambucextern void func1 (void (^block1)(), void (^block2)(), int) __attribute__((nonnull));
19f4a2713aSLionel Sambuc
20f4a2713aSLionel Sambucextern void func3 (void (^block1)(), int, void (^block2)(), int)
21f4a2713aSLionel Sambuc__attribute__((nonnull(1,3)));
22f4a2713aSLionel Sambuc
23f4a2713aSLionel Sambucextern void func4 (void (^block1)(), void (^block2)()) __attribute__((nonnull(1)))
24f4a2713aSLionel Sambuc__attribute__((nonnull(2)));
25f4a2713aSLionel Sambuc
26f4a2713aSLionel Sambucvoid func6();
27f4a2713aSLionel Sambucvoid func7();
28f4a2713aSLionel Sambuc
29f4a2713aSLionel Sambucvoid
30f4a2713aSLionel Sambucfoo (int i1, int i2, int i3, void (^cp1)(), void (^cp2)(), void (^cp3)())
31f4a2713aSLionel Sambuc{
32f4a2713aSLionel Sambuc  func1(cp1, cp2, i1);
33f4a2713aSLionel Sambuc
34*0a6a1f1dSLionel Sambuc  func1(0, cp2, i1);  // expected-warning {{null passed to a callee that requires a non-null argument}}
35*0a6a1f1dSLionel Sambuc  func1(cp1, 0, i1);  // expected-warning {{null passed to a callee that requires a non-null argument}}
36f4a2713aSLionel Sambuc  func1(cp1, cp2, 0);
37f4a2713aSLionel Sambuc
38f4a2713aSLionel Sambuc
39*0a6a1f1dSLionel Sambuc  func3(0, i2, cp3, i3); // expected-warning {{null passed to a callee that requires a non-null argument}}
40*0a6a1f1dSLionel Sambuc  func3(cp3, i2, 0, i3);  // expected-warning {{null passed to a callee that requires a non-null argument}}
41f4a2713aSLionel Sambuc
42*0a6a1f1dSLionel Sambuc  func4(0, cp1); // expected-warning {{null passed to a callee that requires a non-null argument}}
43*0a6a1f1dSLionel Sambuc  func4(cp1, 0); // expected-warning {{null passed to a callee that requires a non-null argument}}
44f4a2713aSLionel Sambuc
45f4a2713aSLionel Sambuc  // Shouldn't these emit warnings?  Clang doesn't, and neither does GCC.  It
46f4a2713aSLionel Sambuc  // seems that the checking should handle Objective-C pointers.
47f4a2713aSLionel Sambuc  func6((NSObject*) 0); // no-warning
48f4a2713aSLionel Sambuc  func7((NSObject*) 0); // no-warning
49f4a2713aSLionel Sambuc}
50f4a2713aSLionel Sambuc
51f4a2713aSLionel Sambucvoid func5(int) NONNULL_ATTR; //  no warning
52f4a2713aSLionel Sambuc
53f4a2713aSLionel Sambuc// rdar://6857843
54f4a2713aSLionel Sambucstruct dispatch_object_s {
55f4a2713aSLionel Sambuc    int x;
56f4a2713aSLionel Sambuc};
57f4a2713aSLionel Sambuc
58f4a2713aSLionel Sambuctypedef union {
59f4a2713aSLionel Sambuc    long first;
60f4a2713aSLionel Sambuc    struct dispatch_object_s *_do;
61f4a2713aSLionel Sambuc} dispatch_object_t __attribute__((transparent_union));
62f4a2713aSLionel Sambuc
63f4a2713aSLionel Sambuc__attribute__((nonnull))
64f4a2713aSLionel Sambucvoid _dispatch_queue_push_list(dispatch_object_t _head); // no warning
65f4a2713aSLionel Sambuc
66f4a2713aSLionel Sambucvoid func6(dispatch_object_t _head) {
67*0a6a1f1dSLionel Sambuc  _dispatch_queue_push_list(0); // expected-warning {{null passed to a callee that requires a non-null argument}}
68f4a2713aSLionel Sambuc  _dispatch_queue_push_list(_head._do);  // no warning
69f4a2713aSLionel Sambuc}
70f4a2713aSLionel Sambuc
71f4a2713aSLionel Sambuc// rdar://9287695
72f4a2713aSLionel Sambuc#define NULL (void*)0
73f4a2713aSLionel Sambuc
74f4a2713aSLionel Sambuc@interface NSObject
75f4a2713aSLionel Sambuc- (void)doSomethingWithNonNullPointer:(void *)ptr :(int)iarg : (void*)ptr1 __attribute__((nonnull(1, 3)));
76f4a2713aSLionel Sambuc+ (void)doSomethingClassyWithNonNullPointer:(void *)ptr __attribute__((nonnull(1)));
77*0a6a1f1dSLionel Sambuc- (void*)returnsCNonNull __attribute__((returns_nonnull)); // no-warning
78*0a6a1f1dSLionel Sambuc- (id)returnsObjCNonNull __attribute__((returns_nonnull)); // no-warning
79*0a6a1f1dSLionel Sambuc- (int)returnsIntNonNull __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
80f4a2713aSLionel Sambuc@end
81f4a2713aSLionel Sambuc
82f4a2713aSLionel Sambucextern void DoSomethingNotNull(void *db) __attribute__((nonnull(1)));
83f4a2713aSLionel Sambuc
84f4a2713aSLionel Sambuc@interface IMP
85f4a2713aSLionel Sambuc{
86f4a2713aSLionel Sambuc  void * vp;
87f4a2713aSLionel Sambuc}
88*0a6a1f1dSLionel Sambuc- (void*) testRetNull __attribute__((returns_nonnull));
89f4a2713aSLionel Sambuc@end
90f4a2713aSLionel Sambuc
91f4a2713aSLionel Sambuc@implementation IMP
92f4a2713aSLionel Sambuc- (void) Meth {
93f4a2713aSLionel Sambuc  NSObject *object;
94*0a6a1f1dSLionel Sambuc  [object doSomethingWithNonNullPointer:NULL:1:NULL]; // expected-warning 2 {{null passed to a callee that requires a non-null argument}}
95*0a6a1f1dSLionel Sambuc  [object doSomethingWithNonNullPointer:vp:1:NULL]; // expected-warning {{null passed to a callee that requires a non-null argument}}
96*0a6a1f1dSLionel Sambuc  [NSObject doSomethingClassyWithNonNullPointer:NULL]; // expected-warning {{null passed to a callee that requires a non-null argument}}
97*0a6a1f1dSLionel Sambuc  DoSomethingNotNull(NULL); // expected-warning {{null passed to a callee that requires a non-null argument}}
98f4a2713aSLionel Sambuc  [object doSomethingWithNonNullPointer:vp:1:vp];
99f4a2713aSLionel Sambuc}
100*0a6a1f1dSLionel Sambuc- (void*) testRetNull {
101*0a6a1f1dSLionel Sambuc  return 0; // expected-warning {{null returned from method that requires a non-null return value}}
102*0a6a1f1dSLionel Sambuc}
103f4a2713aSLionel Sambuc@end
104f4a2713aSLionel Sambuc
105*0a6a1f1dSLionel Sambuc__attribute__((objc_root_class))
106*0a6a1f1dSLionel Sambuc@interface TestNonNullParameters
107*0a6a1f1dSLionel Sambuc- (void) doNotPassNullParameterNonPointerArg:(int)__attribute__((nonnull))x; // expected-warning {{'nonnull' attribute only applies to pointer arguments}}
108*0a6a1f1dSLionel Sambuc- (void) doNotPassNullParameter:(id)__attribute__((nonnull))x;
109*0a6a1f1dSLionel Sambuc- (void) doNotPassNullParameterArgIndex:(id)__attribute__((nonnull(1)))x; // expected-warning {{'nonnull' attribute when used on parameters takes no arguments}}
110*0a6a1f1dSLionel Sambuc- (void) doNotPassNullOnMethod:(id)x __attribute__((nonnull(1)));
111*0a6a1f1dSLionel Sambuc@end
112*0a6a1f1dSLionel Sambuc
113*0a6a1f1dSLionel Sambucvoid test(TestNonNullParameters *f) {
114*0a6a1f1dSLionel Sambuc  [f doNotPassNullParameter:0]; // expected-warning {{null passed to a callee that requires a non-null argument}}
115*0a6a1f1dSLionel Sambuc  [f doNotPassNullParameterArgIndex:0]; // no-warning
116*0a6a1f1dSLionel Sambuc  [f doNotPassNullOnMethod:0]; // expected-warning {{null passed to a callee that requires a non-null argument}}
117*0a6a1f1dSLionel Sambuc}
118*0a6a1f1dSLionel Sambuc
119*0a6a1f1dSLionel Sambuc
120*0a6a1f1dSLionel Sambucvoid PR18795(int (^g)(const char *h, ...) __attribute__((nonnull(1))) __attribute__((nonnull))) {
121*0a6a1f1dSLionel Sambuc  g(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
122*0a6a1f1dSLionel Sambuc}
123*0a6a1f1dSLionel Sambucvoid PR18795_helper() {
124*0a6a1f1dSLionel Sambuc  PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
125*0a6a1f1dSLionel Sambuc}
126