xref: /llvm-project/clang/test/SemaObjC/nonnull.m (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1#include "nonnull.h"
2
3// RUN: %clang_cc1 -fblocks -fsyntax-only -verify -Wno-objc-root-class %s
4// REQUIRES: LP64
5@class NSObject;
6
7NONNULL_ATTR
8int f1(int x); //  no warning
9int f2(int *x) __attribute__ ((nonnull (1)));
10int f3(int *x) __attribute__ ((nonnull (0))); // expected-error {{'nonnull' attribute parameter 1 is out of bounds}}
11int f4(int *x, int *y) __attribute__ ((nonnull (1,2)));
12int f5(int *x, int *y) __attribute__ ((nonnull (2,1)));
13int f6(NSObject *x) __attribute__ ((nonnull (1))); // no-warning
14int f7(NSObject *x) __attribute__ ((nonnull)); // no-warning
15
16
17extern void func1 (void (^block1)(), void (^block2)(), int) __attribute__((nonnull));
18
19extern void func3 (void (^block1)(), int, void (^block2)(), int)
20__attribute__((nonnull(1,3)));
21
22extern void func4 (void (^block1)(), void (^block2)()) __attribute__((nonnull(1)))
23__attribute__((nonnull(2)));
24
25void func6(); // expected-warning {{a function declaration without a prototype is deprecated in all versions of C and is treated as a zero-parameter prototype in C23, conflicting with a subsequent definition}}
26void func7();
27
28void
29foo (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 that requires a non-null argument}}
34  func1(cp1, 0, i1);  // expected-warning {{null passed to a callee that 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 that requires a non-null argument}}
39  func3(cp3, i2, 0, i3);  // expected-warning {{null passed to a callee that requires a non-null argument}}
40
41  func4(0, cp1); // expected-warning {{null passed to a callee that requires a non-null argument}}
42  func4(cp1, 0); // expected-warning {{null passed to a callee that requires a non-null argument}}
43
44  func6((NSObject*) 0); // expected-warning {{passing arguments to 'func6' without a prototype is deprecated in all versions of C and is not supported in C23}}
45  func7((NSObject*) 0); // expected-warning {{passing arguments to 'func7' without a prototype is deprecated in all versions of C and is not supported in C23}}
46}
47
48void func5(int) NONNULL_ATTR; //  no warning
49
50struct dispatch_object_s {
51    int x;
52};
53
54typedef union {
55    long first;
56    struct dispatch_object_s *_do;
57} dispatch_object_t __attribute__((transparent_union));
58
59__attribute__((nonnull))
60void _dispatch_queue_push_list(dispatch_object_t _head); // no warning
61
62void func6(dispatch_object_t _head) { // expected-note {{conflicting prototype is here}}
63  _dispatch_queue_push_list(0); // expected-warning {{null passed to a callee that requires a non-null argument}}
64  _dispatch_queue_push_list(_head._do);  // no warning
65}
66
67#define NULL (void*)0
68
69@interface NSObject
70- (void)doSomethingWithNonNullPointer:(void *)ptr :(int)iarg : (void*)ptr1 __attribute__((nonnull(1, 3)));
71+ (void)doSomethingClassyWithNonNullPointer:(void *)ptr __attribute__((nonnull(1)));
72- (void*)returnsCNonNull __attribute__((returns_nonnull)); // no-warning
73- (id)returnsObjCNonNull __attribute__((returns_nonnull)); // no-warning
74- (int)returnsIntNonNull __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
75@end
76
77extern void DoSomethingNotNull(void *db) __attribute__((nonnull(1)));
78
79@interface IMP
80{
81  void * vp;
82}
83- (void*) testRetNull __attribute__((returns_nonnull));
84@end
85
86@implementation IMP
87- (void) Meth {
88  NSObject *object;
89  [object doSomethingWithNonNullPointer:NULL:1:NULL]; // expected-warning 2 {{null passed to a callee that requires a non-null argument}}
90  [object doSomethingWithNonNullPointer:vp:1:NULL]; // expected-warning {{null passed to a callee that requires a non-null argument}}
91  [NSObject doSomethingClassyWithNonNullPointer:NULL]; // expected-warning {{null passed to a callee that requires a non-null argument}}
92  DoSomethingNotNull(NULL); // expected-warning {{null passed to a callee that requires a non-null argument}}
93  [object doSomethingWithNonNullPointer:vp:1:vp];
94}
95- (void*) testRetNull {
96  return 0; // expected-warning {{null returned from method that requires a non-null return value}}
97}
98@end
99
100__attribute__((objc_root_class))
101@interface TestNonNullParameters
102- (void) doNotPassNullParameterNonPointerArg:(int)__attribute__((nonnull))x; // expected-warning {{'nonnull' attribute only applies to pointer arguments}}
103- (void) doNotPassNullParameter:(id)__attribute__((nonnull))x;
104- (void) doNotPassNullParameterArgIndex:(id)__attribute__((nonnull(1)))x; // expected-warning {{'nonnull' attribute when used on parameters takes no arguments}}
105- (void) doNotPassNullOnMethod:(id)x __attribute__((nonnull(1)));
106@end
107
108void test(TestNonNullParameters *f) {
109  [f doNotPassNullParameter:0]; // expected-warning {{null passed to a callee that requires a non-null argument}}
110  [f doNotPassNullParameterArgIndex:0]; // no-warning
111  [f doNotPassNullOnMethod:0]; // expected-warning {{null passed to a callee that requires a non-null argument}}
112}
113
114
115void PR18795(int (^g)(const char *h, ...) __attribute__((nonnull(1))) __attribute__((nonnull))) {
116  g(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
117}
118void PR18795_helper() {
119  PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
120}
121
122void (^PR23117)(int *) = ^(int *p1) __attribute__((nonnull(1))) {};
123
124typedef int *intptr;
125#pragma clang assume_nonnull begin
126intptr a, b;
127intptr c, (*d)();
128#pragma clang assume_nonnull end
129