xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/nonnull.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc // rdar://9584012
3f4a2713aSLionel Sambuc 
4f4a2713aSLionel Sambuc typedef struct {
5f4a2713aSLionel Sambuc 	char *str;
6f4a2713aSLionel Sambuc } Class;
7f4a2713aSLionel Sambuc 
8f4a2713aSLionel Sambuc typedef union {
9f4a2713aSLionel Sambuc 	Class *object;
10f4a2713aSLionel Sambuc } Instance __attribute__((transparent_union));
11f4a2713aSLionel Sambuc 
Class_init(Instance this,char * str)12f4a2713aSLionel Sambuc __attribute__((nonnull(1))) void Class_init(Instance this, char *str) {
13f4a2713aSLionel Sambuc 	this.object->str = str;
14f4a2713aSLionel Sambuc }
15f4a2713aSLionel Sambuc 
main(void)16f4a2713aSLionel Sambuc int main(void) {
17f4a2713aSLionel Sambuc 	Class *obj;
18*0a6a1f1dSLionel Sambuc 	Class_init(0, "Hello World"); // expected-warning {{null passed to a callee that requires a non-null argument}}
19f4a2713aSLionel Sambuc 	Class_init(obj, "Hello World");
20f4a2713aSLionel Sambuc }
21f4a2713aSLionel Sambuc 
22f4a2713aSLionel Sambuc void foo(const char *str) __attribute__((nonnull("foo"))); // expected-error{{'nonnull' attribute requires parameter 1 to be an integer constant}}
23*0a6a1f1dSLionel Sambuc 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}}
24*0a6a1f1dSLionel Sambuc 
25*0a6a1f1dSLionel Sambuc void baz(__attribute__((nonnull)) const char *str);
26*0a6a1f1dSLionel Sambuc void baz2(__attribute__((nonnull(1))) const char *str); // expected-warning {{'nonnull' attribute when used on parameters takes no arguments}}
27*0a6a1f1dSLionel Sambuc void baz3(__attribute__((nonnull)) int x); // expected-warning {{'nonnull' attribute only applies to pointer arguments}}
28*0a6a1f1dSLionel Sambuc 
test_baz()29*0a6a1f1dSLionel Sambuc void test_baz() {
30*0a6a1f1dSLionel Sambuc   baz(0); // expected-warning {{null passed to a callee that requires a non-null argument}}
31*0a6a1f1dSLionel Sambuc   baz2(0); // no-warning
32*0a6a1f1dSLionel Sambuc   baz3(0); // no-warning
33*0a6a1f1dSLionel Sambuc }
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc void test_void_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
36*0a6a1f1dSLionel Sambuc int test_int_returns_nonnull(void) __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to return values that are pointers}}
37*0a6a1f1dSLionel Sambuc void *test_ptr_returns_nonnull(void) __attribute__((returns_nonnull)); // no-warning
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc int i __attribute__((nonnull)); // expected-warning {{'nonnull' attribute only applies to functions, methods, and parameters}}
40*0a6a1f1dSLionel Sambuc int j __attribute__((returns_nonnull)); // expected-warning {{'returns_nonnull' attribute only applies to functions and methods}}
41*0a6a1f1dSLionel Sambuc void *test_no_fn_proto() __attribute__((returns_nonnull)); // no-warning
42*0a6a1f1dSLionel Sambuc void *test_with_fn_proto(void) __attribute__((returns_nonnull)); // no-warning
43*0a6a1f1dSLionel Sambuc 
44*0a6a1f1dSLionel Sambuc __attribute__((returns_nonnull))
test_bad_returns_null(void)45*0a6a1f1dSLionel Sambuc void *test_bad_returns_null(void) {
46*0a6a1f1dSLionel Sambuc   return 0; // expected-warning {{null returned from function that requires a non-null return value}}
47*0a6a1f1dSLionel Sambuc }
48*0a6a1f1dSLionel Sambuc 
PR18795(int (* g)(const char * h,...))49*0a6a1f1dSLionel Sambuc void PR18795(int (*g)(const char *h, ...) __attribute__((nonnull(1))) __attribute__((nonnull))) {
50*0a6a1f1dSLionel Sambuc   g(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
51*0a6a1f1dSLionel Sambuc }
PR18795_helper()52*0a6a1f1dSLionel Sambuc void PR18795_helper() {
53*0a6a1f1dSLionel Sambuc   PR18795(0); // expected-warning{{null passed to a callee that requires a non-null argument}}
54*0a6a1f1dSLionel Sambuc }
55*0a6a1f1dSLionel Sambuc 
56*0a6a1f1dSLionel Sambuc void vararg1(int n, ...) __attribute__((nonnull(2)));
vararg1_test()57*0a6a1f1dSLionel Sambuc void vararg1_test() {
58*0a6a1f1dSLionel Sambuc   vararg1(0);
59*0a6a1f1dSLionel Sambuc   vararg1(1, (void*)0); // expected-warning{{null passed}}
60*0a6a1f1dSLionel Sambuc   vararg1(2, (void*)0, (void*)0); // expected-warning{{null passed}}
61*0a6a1f1dSLionel Sambuc   vararg1(2, (void*)&vararg1, (void*)0);
62*0a6a1f1dSLionel Sambuc }
63*0a6a1f1dSLionel Sambuc 
64*0a6a1f1dSLionel Sambuc void vararg2(int n, ...) __attribute__((nonnull, nonnull, nonnull));
vararg2_test()65*0a6a1f1dSLionel Sambuc void vararg2_test() {
66*0a6a1f1dSLionel Sambuc   vararg2(0);
67*0a6a1f1dSLionel Sambuc   vararg2(1, (void*)0); // expected-warning{{null passed}}
68*0a6a1f1dSLionel Sambuc   vararg2(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
69*0a6a1f1dSLionel Sambuc }
70*0a6a1f1dSLionel Sambuc 
71*0a6a1f1dSLionel Sambuc void vararg3(int n, ...) __attribute__((nonnull, nonnull(2), nonnull(3)));
vararg3_test()72*0a6a1f1dSLionel Sambuc void vararg3_test() {
73*0a6a1f1dSLionel Sambuc   vararg3(0);
74*0a6a1f1dSLionel Sambuc   vararg3(1, (void*)0); // expected-warning{{null passed}}
75*0a6a1f1dSLionel Sambuc   vararg3(2, (void*)0, (void*)0); // expected-warning 2{{null passed}}
76*0a6a1f1dSLionel Sambuc }
77*0a6a1f1dSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc void redecl(void *, void *);
79*0a6a1f1dSLionel Sambuc void redecl(void *, void *) __attribute__((nonnull(1)));
80*0a6a1f1dSLionel Sambuc void redecl(void *, void *) __attribute__((nonnull(2)));
81*0a6a1f1dSLionel Sambuc void redecl(void *, void *);
redecl_test(void * p)82*0a6a1f1dSLionel Sambuc void redecl_test(void *p) {
83*0a6a1f1dSLionel Sambuc   redecl(p, 0); // expected-warning{{null passed}}
84*0a6a1f1dSLionel Sambuc   redecl(0, p); // expected-warning{{null passed}}
85*0a6a1f1dSLionel Sambuc }
86*0a6a1f1dSLionel Sambuc 
87*0a6a1f1dSLionel Sambuc // rdar://18712242
88*0a6a1f1dSLionel Sambuc #define NULL (void*)0
89*0a6a1f1dSLionel Sambuc __attribute__((__nonnull__))
evil_nonnull_func(int * pointer,void * pv)90*0a6a1f1dSLionel Sambuc int evil_nonnull_func(int* pointer, void * pv)
91*0a6a1f1dSLionel Sambuc {
92*0a6a1f1dSLionel Sambuc    if (pointer == NULL) {  // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is false on first encounter}}
93*0a6a1f1dSLionel Sambuc      return 0;
94*0a6a1f1dSLionel Sambuc    } else {
95*0a6a1f1dSLionel Sambuc      return *pointer;
96*0a6a1f1dSLionel Sambuc    }
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc    pointer = pv;
99*0a6a1f1dSLionel Sambuc    if (!pointer)
100*0a6a1f1dSLionel Sambuc      return 0;
101*0a6a1f1dSLionel Sambuc    else
102*0a6a1f1dSLionel Sambuc      return *pointer;
103*0a6a1f1dSLionel Sambuc 
104*0a6a1f1dSLionel Sambuc    if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is false on first encounter}}
105*0a6a1f1dSLionel Sambuc }
106*0a6a1f1dSLionel Sambuc 
107*0a6a1f1dSLionel Sambuc void set_param_to_null(int**);
108*0a6a1f1dSLionel Sambuc int another_evil_nonnull_func(int* pointer, char ch, void * pv) __attribute__((nonnull(1, 3)));
another_evil_nonnull_func(int * pointer,char ch,void * pv)109*0a6a1f1dSLionel Sambuc int another_evil_nonnull_func(int* pointer, char ch, void * pv) {
110*0a6a1f1dSLionel Sambuc    if (pointer == NULL) { // expected-warning {{comparison of nonnull parameter 'pointer' equal to a null pointer is false on first encounter}}
111*0a6a1f1dSLionel Sambuc      return 0;
112*0a6a1f1dSLionel Sambuc    } else {
113*0a6a1f1dSLionel Sambuc      return *pointer;
114*0a6a1f1dSLionel Sambuc    }
115*0a6a1f1dSLionel Sambuc 
116*0a6a1f1dSLionel Sambuc    set_param_to_null(&pointer);
117*0a6a1f1dSLionel Sambuc    if (!pointer)
118*0a6a1f1dSLionel Sambuc      return 0;
119*0a6a1f1dSLionel Sambuc    else
120*0a6a1f1dSLionel Sambuc      return *pointer;
121*0a6a1f1dSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc    if (pv == NULL) {} // expected-warning {{comparison of nonnull parameter 'pv' equal to a null pointer is false on first encounter}}
123*0a6a1f1dSLionel Sambuc }
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc extern void *returns_null(void**);
126*0a6a1f1dSLionel Sambuc extern void FOO();
127*0a6a1f1dSLionel Sambuc extern void FEE();
128*0a6a1f1dSLionel Sambuc 
129*0a6a1f1dSLionel Sambuc extern void *pv;
130*0a6a1f1dSLionel Sambuc __attribute__((__nonnull__))
yet_another_evil_nonnull_func(int * pointer)131*0a6a1f1dSLionel Sambuc void yet_another_evil_nonnull_func(int* pointer)
132*0a6a1f1dSLionel Sambuc {
133*0a6a1f1dSLionel Sambuc  while (pv) {
134*0a6a1f1dSLionel Sambuc    // This comparison will not be optimized away.
135*0a6a1f1dSLionel Sambuc    if (pointer) {  // expected-warning {{nonnull parameter 'pointer' will evaluate to 'true' on first encounter}}
136*0a6a1f1dSLionel Sambuc      FOO();
137*0a6a1f1dSLionel Sambuc    } else {
138*0a6a1f1dSLionel Sambuc      FEE();
139*0a6a1f1dSLionel Sambuc    }
140*0a6a1f1dSLionel Sambuc    pointer = returns_null(&pv);
141*0a6a1f1dSLionel Sambuc  }
142*0a6a1f1dSLionel Sambuc }
143*0a6a1f1dSLionel Sambuc 
pr21668_1(const char * p,const char * s)144*0a6a1f1dSLionel Sambuc void pr21668_1(__attribute__((nonnull)) const char *p, const char *s) {
145*0a6a1f1dSLionel Sambuc   if (p) // expected-warning {{nonnull parameter 'p' will evaluate to 'true' on first encounter}}
146*0a6a1f1dSLionel Sambuc     ;
147*0a6a1f1dSLionel Sambuc   if (s) // No warning
148*0a6a1f1dSLionel Sambuc     ;
149*0a6a1f1dSLionel Sambuc }
150*0a6a1f1dSLionel Sambuc 
pr21668_2(const char * p)151*0a6a1f1dSLionel Sambuc void pr21668_2(__attribute__((nonnull)) const char *p) {
152*0a6a1f1dSLionel Sambuc   p = 0;
153*0a6a1f1dSLionel Sambuc   if (p) // No warning
154*0a6a1f1dSLionel Sambuc     ;
155*0a6a1f1dSLionel Sambuc }
156