1 /* Test for the "nonnull" function attribute.  */
2 /* Origin: Jason Thorpe <thorpej@wasabisystems.com> */
3 /* { dg-do compile } */
4 /* { dg-options "-Wnonnull" } */
5 
6 #include <stddef.h>
7 
8 extern void func1 (char *, char *, int) __attribute__((nonnull));
9 
10 extern void func2 (char *, char *) __attribute__((nonnull(1)));
11 
12 extern void func3 (char *, int, char *, int)
13   __attribute__((nonnull(1,3)));
14 
15 extern void func4 (char *, char *) __attribute__((nonnull(1)))
16   __attribute__((nonnull(2)));
17 
18 void
foo(int i1,int i2,int i3,char * cp1,char * cp2,char * cp3)19 foo (int i1, int i2, int i3, char *cp1, char *cp2, char *cp3)
20 {
21   func1(cp1, cp2, i1);
22 
23   func1(NULL, cp2, i1); /* { dg-warning "null" "null with argless nonnull 1" } */
24   func1(cp1, NULL, i1); /* { dg-warning "null" "null with argless nonnull 2" } */
25   func1(cp1, cp2, 0);
26 
27   func2(cp1, NULL);
28   func2(NULL, cp1); /* { dg-warning "null" "null with single explicit nonnull" } */
29 
30   func3(NULL, i2, cp3, i3); /* { dg-warning "null" "null with explicit nonnull 1" } */
31   func3(cp3, i2, NULL, i3); /* { dg-warning "null" "null with explicit nonnull 3" } */
32 
33   func1(i1 ? cp1 : NULL, cp2, i3); /* { dg-warning "null" "null with cond expr rhs" } */
34   func1(i1 ? NULL : cp1, cp2, i3); /* { dg-warning "null" "null with cond expr lhs" } */
35   func1(i1 ? (i2 ? cp1 : NULL) : cp2, cp3, i3); /* { dg-warning "null" "null with nested cond expr" } */
36 
37   func4(NULL, cp1); /* { dg-warning "null" "null with multiple attributes 1" } */
38   func4(cp1, NULL); /* { dg-warning "null" "null with multiple attributes 2" } */
39 }
40