]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gcc.dg/Wnonnull.c
[Darwin, testsuite] Fix Wnonnull on Darwin.
[thirdparty/gcc.git] / gcc / testsuite / gcc.dg / Wnonnull.c
1 /* PR tree-optimization/83369 - Missing diagnostics during inlining
2 { dg-do compile }
3 { dg-options "-O2 -Wall" } */
4
5 #ifndef __APPLE__
6 #include <string.h>
7 #else
8 /* OSX headers do not mark up the nonnull elements yet. */
9 # include <stddef.h>
10 extern size_t strlen (const char *__s)
11 __attribute ((pure)) __attribute ((nonnull (1)));
12 extern void *memcpy (void *__restrict __dest, const void *__restrict __src,
13 size_t __n) __attribute ((nonnull (1, 2)));
14 #endif
15
16 char buf[100];
17
18 struct Test
19 {
20 const char* s1;
21 const char* s2;
22 };
23
24 __attribute ((nonnull (1, 2)))
25 inline char*
26 my_strcpy (char *restrict dst, const char *restrict src, size_t size)
27 {
28 size_t len = strlen (src); /* { dg-warning "argument 1 null where non-null expected" } */
29 if (len < size)
30 memcpy (dst, src, len + 1); /* { dg-warning "argument 2 null where non-null expected" } */
31 else
32 {
33 memcpy (dst, src, size - 1); /* { dg-warning "argument 2 null where non-null expected" } */
34 dst[size - 1] = '\0';
35 }
36 return dst;
37 }
38
39 void test (struct Test* test)
40 {
41 if (test->s1)
42 my_strcpy (buf, test->s1, sizeof buf);
43 else if (test->s2)
44 my_strcpy (buf, test->s2, sizeof buf);
45 else
46 my_strcpy (buf, test->s2, sizeof buf);
47 }
48
49 /* Verify that the inlining context is printed for -Wnonnull:
50 { dg-message "function .my_strcpy..*inlined from .test." "" { target *-*-* } 0 } */