2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / testsuite / gcc.dg / format / c90-scanf-1.c
blobea42f1e131e5c69e39b072c6d52bd675e8a170ce
1 /* Test for scanf formats. Formats using C90 features, including cases
2 where C90 specifies some aspect of the format to be ignored or where
3 the behavior is undefined.
4 */
5 /* Origin: Joseph Myers <jsm28@cam.ac.uk> */
6 /* { dg-do compile } */
7 /* { dg-options "-std=iso9899:1990 -pedantic -Wformat" } */
9 #include "format.h"
11 void
12 foo (int *ip, unsigned int *uip, short int *hp, unsigned short int *uhp,
13 long int *lp, unsigned long int *ulp, float *fp, double *dp,
14 long double *ldp, char *s, signed char *ss, unsigned char *us,
15 void **pp, int *n, llong *llp, ullong *ullp, wchar_t *ls,
16 const int *cip, const int *cn, const char *cs, const void **ppc,
17 void *const *pcp, short int *hn, long int *ln, void *p, char **sp,
18 volatile void *ppv)
20 /* See ISO/IEC 9899:1990 (E) subclause 7.9.6.2 (pages 134-138). */
21 /* Basic sanity checks for the different components of a format. */
22 scanf ("%d", ip);
23 scanf ("%*d");
24 scanf ("%3d", ip);
25 scanf ("%hd", hp);
26 scanf ("%3ld", lp);
27 scanf ("%*3d");
28 scanf ("%d %ld", ip, lp);
29 /* Valid and invalid %% constructions. */
30 scanf ("%%");
31 scanf ("%*%"); /* { dg-warning "format" "bogus %%" } */
32 scanf ("%*%\n"); /* { dg-warning "format" "bogus %%" } */
33 scanf ("%4%"); /* { dg-warning "format" "bogus %%" } */
34 scanf ("%4%\n"); /* { dg-warning "format" "bogus %%" } */
35 scanf ("%h%"); /* { dg-warning "format" "bogus %%" } */
36 scanf ("%h%\n"); /* { dg-warning "format" "bogus %%" } */
37 /* Valid, invalid and silly assignment-suppression constructions. */
38 scanf ("%*d%*i%*o%*u%*x%*X%*e%*E%*f%*g%*G%*s%*[abc]%*c%*p");
39 scanf ("%*2d%*8s%*3c");
40 scanf ("%*n", n); /* { dg-warning "suppress" "suppression of %n" } */
41 scanf ("%*hd"); /* { dg-warning "together" "suppression with length" } */
42 /* Valid, invalid and silly width constructions. */
43 scanf ("%2d%3i%4o%5u%6x%7X%8e%9E%10f%11g%12G%13s%14[abc]%15c%16p",
44 ip, ip, uip, uip, uip, uip, fp, fp, fp, fp, fp, s, s, s, pp);
45 scanf ("%0d", ip); /* { dg-warning "width" "warning for zero width" } */
46 scanf ("%3n", n); /* { dg-warning "width" "width with %n" } */
47 /* Valid and invalid %h, %l, %L constructions. */
48 scanf ("%hd%hi%ho%hu%hx%hX%hn", hp, hp, uhp, uhp, uhp, uhp, hn);
49 scanf ("%he", fp); /* { dg-warning "length" "bad use of %h" } */
50 scanf ("%hE", fp); /* { dg-warning "length" "bad use of %h" } */
51 scanf ("%hf", fp); /* { dg-warning "length" "bad use of %h" } */
52 scanf ("%hg", fp); /* { dg-warning "length" "bad use of %h" } */
53 scanf ("%hG", fp); /* { dg-warning "length" "bad use of %h" } */
54 scanf ("%hs", s); /* { dg-warning "length" "bad use of %h" } */
55 scanf ("%h[ac]", s); /* { dg-warning "length" "bad use of %h" } */
56 scanf ("%hc", s); /* { dg-warning "length" "bad use of %h" } */
57 scanf ("%hp", pp); /* { dg-warning "length" "bad use of %h" } */
58 scanf ("%h"); /* { dg-warning "conversion lacks type" "bare %h" } */
59 scanf ("%h."); /* { dg-warning "conversion" "bogus %h" } */
60 scanf ("%ld%li%lo%lu%lx%lX%ln", lp, lp, ulp, ulp, ulp, ulp, ln);
61 scanf ("%le%lE%lf%lg%lG", dp, dp, dp, dp, dp);
62 scanf ("%lp", pp); /* { dg-warning "length" "bad use of %l" } */
63 /* These next three formats were added in C94. */
64 scanf ("%ls", ls); /* { dg-warning "length|C" "bad use of %l" } */
65 scanf ("%l[ac]", ls); /* { dg-warning "length|C" "bad use of %l" } */
66 scanf ("%lc", ls); /* { dg-warning "length|C" "bad use of %l" } */
67 scanf ("%Le%LE%Lf%Lg%LG", ldp, ldp, ldp, ldp, ldp);
68 scanf ("%Ld", llp); /* { dg-warning "does not support" "bad use of %L" } */
69 scanf ("%Li", llp); /* { dg-warning "does not support" "bad use of %L" } */
70 scanf ("%Lo", ullp); /* { dg-warning "does not support" "bad use of %L" } */
71 scanf ("%Lu", ullp); /* { dg-warning "does not support" "bad use of %L" } */
72 scanf ("%Lx", ullp); /* { dg-warning "does not support" "bad use of %L" } */
73 scanf ("%LX", ullp); /* { dg-warning "does not support" "bad use of %L" } */
74 scanf ("%Ls", s); /* { dg-warning "length" "bad use of %L" } */
75 scanf ("%L[ac]", s); /* { dg-warning "length" "bad use of %L" } */
76 scanf ("%Lc", s); /* { dg-warning "length" "bad use of %L" } */
77 scanf ("%Lp", pp); /* { dg-warning "length" "bad use of %L" } */
78 scanf ("%Ln", n); /* { dg-warning "length" "bad use of %L" } */
79 /* Valid uses of each bare conversion. */
80 scanf ("%d%i%o%u%x%X%e%E%f%g%G%s%[abc]%c%p%n%%", ip, ip, uip, uip, uip,
81 uip, fp, fp, fp, fp, fp, s, s, s, pp, n);
82 /* Allow other character pointers with %s, %c, %[]. */
83 scanf ("%2s%3s%4c%5c%6[abc]%7[abc]", ss, us, ss, us, ss, us);
84 /* Further tests for %[]. */
85 scanf ("%[%d]%d", s, ip);
86 scanf ("%[^%d]%d", s, ip);
87 scanf ("%[]%d]%d", s, ip);
88 scanf ("%[^]%d]%d", s, ip);
89 scanf ("%[%d]%d", s, ip);
90 scanf ("%[]abcd", s); /* { dg-warning "no closing" "incomplete scanset" } */
91 /* Various tests of bad argument types. Some of these are only pedantic
92 warnings.
94 scanf ("%d", lp); /* { dg-warning "format" "bad argument types" } */
95 scanf ("%d", uip); /* { dg-warning "format" "bad argument types" } */
96 scanf ("%d", pp); /* { dg-warning "format" "bad argument types" } */
97 scanf ("%p", ppc); /* { dg-warning "format" "bad argument types" } */
98 scanf ("%p", ppv); /* { dg-warning "format" "bad argument types" } */
99 scanf ("%s", n); /* { dg-warning "format" "bad argument types" } */
100 scanf ("%s", p); /* { dg-warning "format" "bad argument types" } */
101 scanf ("%p", p); /* { dg-warning "format" "bad argument types" } */
102 scanf ("%p", sp); /* { dg-warning "format" "bad argument types" } */
103 /* Tests for writing into constant values. */
104 scanf ("%d", cip); /* { dg-warning "constant" "%d writing into const" } */
105 scanf ("%n", cn); /* { dg-warning "constant" "%n writing into const" } */
106 scanf ("%s", cs); /* { dg-warning "constant" "%s writing into const" } */
107 scanf ("%p", pcp); /* { dg-warning "constant" "%p writing into const" } */
108 /* Wrong number of arguments. */
109 scanf ("%d%d", ip); /* { dg-warning "arguments" "wrong number of args" } */
110 scanf ("%d", ip, ip); /* { dg-warning "arguments" "wrong number of args" } */
111 /* Miscellaneous bogus constructions. */
112 scanf (""); /* { dg-warning "zero-length" "warning for empty format" } */
113 scanf ("\0"); /* { dg-warning "embedded" "warning for embedded NUL" } */
114 scanf ("%d\0", ip); /* { dg-warning "embedded" "warning for embedded NUL" } */
115 scanf ("%d\0%d", ip, ip); /* { dg-warning "embedded|too many" "warning for embedded NUL" } */
116 scanf (NULL); /* { dg-warning "null" "null format string warning" } */
117 scanf ("%"); /* { dg-warning "trailing" "trailing % warning" } */
118 scanf ("%d", (int *)0); /* { dg-warning "null" "writing into NULL" } */