Debian Codegraph @

gcc/testsuite/gcc.dg/nonnull-8.c

raw
1 /* Test for the "nonnull_if_nonzero" function attribute. */
2 /* { dg-do compile } */
3 /* { dg-options "-Wnonnull" } */
4
5 #include <stddef.h>
6
7 extern void func1 (char *, char *, int)
8 __attribute__((nonnull_if_nonzero (1, 3), nonnull_if_nonzero (2, 3)));
9
10 extern void func2 (char *, char *, unsigned long)
11 __attribute__((nonnull_if_nonzero (1, 3)));
12
13 enum E { E0 = 0, E1 = __INT_MAX__ };
14 extern void func3 (char *, int, char *, enum E)
15 __attribute__((nonnull_if_nonzero (1, 4), nonnull_if_nonzero (3, 2)));
16
17 extern void func4 (long, char *, char *, long)
18 __attribute__((nonnull_if_nonzero (2, 1)))
19 __attribute__((nonnull_if_nonzero (3, 4)));
20
21 void
22 foo (int i1, int i2, int i3, char *cp1, char *cp2, char *cp3)
23 {
24 func1 (cp1, cp2, i1);
25 func1 (cp1, cp2, 0);
26 func1 (cp1, cp2, 42);
27 func1 (NULL, NULL, 0);
28 func1 (NULL, NULL, i1);
29
30 func1 (NULL, cp2, 42); /* { dg-warning "argument 1 null where non-null expected because argument 3 is nonzero" } */
31 func1 (cp1, NULL, 1); /* { dg-warning "argument 2 null where non-null expected because argument 3 is nonzero" } */
32
33 func2 (cp1, NULL, 17);
34 func2 (NULL, cp2, 0);
35 func2 (NULL, cp1, 2); /* { dg-warning "argument 1 null where non-null expected because argument 3 is nonzero" } */
36
37 func3 (NULL, i2, cp3, i3);
38 func3 (cp1, i2, NULL, i3);
39 func3 (NULL, i2, cp3, E0);
40 func3 (cp1, 0, NULL, E1);
41 func3 (NULL, i2, cp3, E1); /* { dg-warning "argument 1 null where non-null expected because argument 4 is nonzero" } */
42 func3 (cp3, 5, NULL, i3); /* { dg-warning "argument 3 null where non-null expected because argument 2 is nonzero" } */
43
44 func1 (i2 ? cp1 : NULL, cp2, i3);
45 func1 (i2 ? NULL : cp1, cp2, i3);
46 func1 (i2 ? (i3 ? cp1 : NULL) : cp2, cp3, i1);
47 func1 (i1 ? cp1 : NULL, cp2, 0);
48 func1 (i1 ? NULL : cp1, cp2, 0);
49 func1 (i1 ? (i2 ? cp1 : NULL) : cp2, cp3, 0);
50 func1 (i1 ? cp1 : NULL, cp2, 1); /* { dg-warning "argument 1 null where non-null expected because argument 3 is nonzero" } */
51 func1 (i1 ? NULL : cp1, cp2, 2); /* { dg-warning "argument 1 null where non-null expected because argument 3 is nonzero" } */
52 func1 (i1 ? (i2 ? cp1 : NULL) : cp2, cp3, 3); /* { dg-warning "argument 1 null where non-null expected because argument 3 is nonzero" } */
53
54 func4 (0, NULL, NULL, 0);
55 func4 (-1, NULL, cp1, 0); /* { dg-warning "argument 2 null where non-null expected because argument 1 is nonzero" } */
56 func4 (0, cp1, NULL, 77); /* { dg-warning "argument 3 null where non-null expected because argument 4 is nonzero" } */
57 }