Debian Codegraph @

gcc/testsuite/gcc.c-torture/execute/user-printf.c

raw
1 /* Verify that calls to a function declared wiith attribute format (printf)
2 don't get eliminated even if their result on success can be computed at
3 compile time (they can fail).
4 { dg-require-effective-target unwrapped }
5 { dg-require-effective-target fileio }
6 { dg-prune-output "warning: warning: \[^\n\r\]* possibly used unsafely" }
7 { dg-skip-if "requires io" { avr-*-* } }
8 { dg-skip-if "requires io" { freestanding } } */
9
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include "gcc_tmpnam.h"
15
16 void __attribute__ ((format (printf, 1, 2), noipa))
17 user_print (const char *fmt, ...)
18 {
19 va_list va;
20 va_start (va, fmt);
21 vfprintf (stdout, fmt, va);
22 va_end (va);
23 }
24
25 int main (void)
26 {
27 char *tmpfname = gcc_tmpnam (0);
28 FILE *f = freopen (tmpfname, "w", stdout);
29 if (!f)
30 {
31 perror ("fopen for writing");
32 return 1;
33 }
34
35 user_print ("1");
36 user_print ("%c", '2');
37 user_print ("%c%c", '3', '4');
38 user_print ("%s", "5");
39 user_print ("%s%s", "6", "7");
40 user_print ("%i", 8);
41 user_print ("%.1s\n", "9x");
42
43 fclose (f);
44
45 f = fopen (tmpfname, "r");
46 if (!f)
47 {
48 perror ("fopen for reading");
49 remove (tmpfname);
50 return 1;
51 }
52
53 char buf[12] = "";
54 if (1 != fscanf (f, "%s", buf))
55 {
56 perror ("fscanf");
57 fclose (f);
58 remove (tmpfname);
59 return 1;
60 }
61
62 fclose (f);
63 remove (tmpfname);
64
65 if (strcmp (buf, "123456789"))
66 abort ();
67
68 return 0;
69 }