aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.c-torture/execute/user-printf.c
blob: bfee0760dd74c86edaf6526c358b26e507594f39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Verify that calls to a function declared wiith attribute format (printf)
   don't get eliminated even if their result on success can be computed at
   compile time (they can fail).
   { dg-require-effective-target unwrapped }
   { dg-require-effective-target fileio }
   { dg-prune-output "warning: warning: \[^\n\r\]* possibly used unsafely" }
   { dg-skip-if "requires io" { avr-*-* } }
   { dg-skip-if "requires io" { freestanding } } */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gcc_tmpnam.h"

void __attribute__ ((format (printf, 1, 2), noipa))
user_print (const char *fmt, ...)
{
  va_list va;
  va_start (va, fmt);
  vfprintf (stdout, fmt, va);
  va_end (va);
}

int main (void)
{
  char *tmpfname = gcc_tmpnam (0);
  FILE *f = freopen (tmpfname, "w", stdout);
  if (!f)
    {
      perror ("fopen for writing");
      return 1;
    }

  user_print ("1");
  user_print ("%c", '2');
  user_print ("%c%c", '3', '4');
  user_print ("%s", "5");
  user_print ("%s%s", "6", "7");
  user_print ("%i", 8);
  user_print ("%.1s\n", "9x");

  fclose (f);

  f = fopen (tmpfname, "r");
  if (!f)
    {
      perror ("fopen for reading");
      remove (tmpfname);
      return 1;
    }

  char buf[12] = "";
  if (1 != fscanf (f, "%s", buf))
    {
      perror ("fscanf");
      fclose (f);
      remove (tmpfname);
      return 1;
    }

  fclose (f);
  remove (tmpfname);

  if (strcmp (buf, "123456789"))
    abort ();

  return 0;
}