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