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 
15 void __attribute__ ((format (printf, 1, 2), noipa))
user_print(const char * fmt,...)16 user_print (const char *fmt, ...)
17 {
18   va_list va;
19   va_start (va, fmt);
20   vfprintf (stdout, fmt, va);
21   va_end (va);
22 }
23 
main(void)24 int main (void)
25 {
26   char *tmpfname = tmpnam (0);
27   FILE *f = freopen (tmpfname, "w", stdout);
28   if (!f)
29     {
30       perror ("fopen for writing");
31       return 1;
32     }
33 
34   user_print ("1");
35   user_print ("%c", '2');
36   user_print ("%c%c", '3', '4');
37   user_print ("%s", "5");
38   user_print ("%s%s", "6", "7");
39   user_print ("%i", 8);
40   user_print ("%.1s\n", "9x");
41 
42   fclose (f);
43 
44   f = fopen (tmpfname, "r");
45   if (!f)
46     {
47       perror ("fopen for reading");
48       remove (tmpfname);
49       return 1;
50     }
51 
52   char buf[12] = "";
53   if (1 != fscanf (f, "%s", buf))
54     {
55       perror ("fscanf");
56       fclose (f);
57       remove (tmpfname);
58       return 1;
59     }
60 
61   fclose (f);
62   remove (tmpfname);
63 
64   if (strcmp (buf, "123456789"))
65     abort ();
66 
67   return 0;
68 }
69