PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/gcc/testsuite/gcc.dg/format/ms_c90-printf-1.c

https://bitbucket.org/tari/prizm-gcc
C | 184 lines | 121 code | 2 blank | 61 comment | 0 complexity | 30d56789f97dc890e5bf96723feb021f MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, GPL-2.0, LGPL-2.1, AGPL-1.0, GPL-3.0
  1. /* Test for printf 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 { target { *-*-mingw* } } } */
  7. /* { dg-options "-std=iso9899:1990 -pedantic -Wformat" } */
  8. #define USE_SYSTEM_FORMATS
  9. #include "format.h"
  10. void
  11. foo (int i, int i1, int i2, unsigned int u, double d, char *s, void *p,
  12. int *n, short int *hn, long int l, unsigned long int ul,
  13. long int *ln, long double ld, wint_t lc, wchar_t *ls, llong ll,
  14. ullong ull, unsigned int *un, const int *cn, signed char *ss,
  15. unsigned char *us, const signed char *css, unsigned int u1,
  16. unsigned int u2)
  17. {
  18. /* See ISO/IEC 9899:1990 (E) subclause 7.9.6.1 (pages 131-134). */
  19. /* Basic sanity checks for the different components of a format. */
  20. printf ("%d\n", i);
  21. printf ("%+d\n", i);
  22. printf ("%3d\n", i);
  23. printf ("%-3d\n", i);
  24. printf ("%*d\n", i1, i);
  25. printf ("%d %lu\n", i, ul);
  26. /* Bogus use of width. */
  27. printf ("%5n\n", n); /* { dg-warning "width" "width with %n" } */
  28. /* Valid and invalid %% constructions. Some of the warning messages
  29. are non-optimal, but they do detect the errorneous nature of the
  30. format string.
  31. */
  32. printf ("%%");
  33. printf ("%-%"); /* { dg-warning "format" "bogus %%" } */
  34. printf ("%-%\n"); /* { dg-warning "format" "bogus %%" } */
  35. printf ("%5%\n"); /* { dg-warning "format" "bogus %%" } */
  36. printf ("%h%\n"); /* { dg-warning "format" "bogus %%" } */
  37. /* Valid and invalid %h, %l, %L constructions. */
  38. printf ("%hd", i);
  39. printf ("%hi", i);
  40. /* Strictly, these parameters should be int or unsigned int according to
  41. what unsigned short promotes to. However, GCC ignores sign
  42. differences in format checking here, and this is relied on to get the
  43. correct checking without print_char_table needing to know whether
  44. int and short are the same size.
  45. */
  46. printf ("%ho%hu%hx%hX", u, u, u, u);
  47. printf ("%hn", hn);
  48. printf ("%hf", d); /* { dg-warning "length" "bad use of %h" } */
  49. printf ("%he", d); /* { dg-warning "length" "bad use of %h" } */
  50. printf ("%hE", d); /* { dg-warning "length" "bad use of %h" } */
  51. printf ("%hg", d); /* { dg-warning "length" "bad use of %h" } */
  52. printf ("%hG", d); /* { dg-warning "length" "bad use of %h" } */
  53. printf ("%hc", i);
  54. printf ("%hs", hn);
  55. printf ("%hp", p); /* { dg-warning "length" "bad use of %h" } */
  56. printf ("%h"); /* { dg-warning "conversion lacks type" "bare %h" } */
  57. printf ("%ld%li%lo%lu%lx%lX", l, l, ul, ul, ul, ul);
  58. printf ("%ln", ln);
  59. printf ("%lf", d); /* { dg-warning "length|C" "bad use of %l" } */
  60. printf ("%le", d); /* { dg-warning "length|C" "bad use of %l" } */
  61. printf ("%lE", d); /* { dg-warning "length|C" "bad use of %l" } */
  62. printf ("%lg", d); /* { dg-warning "length|C" "bad use of %l" } */
  63. printf ("%lG", d); /* { dg-warning "length|C" "bad use of %l" } */
  64. printf ("%lp", p); /* { dg-warning "length|C" "bad use of %l" } */
  65. /* These next two were added in C94, but should be objected to in C90.
  66. For the first one, GCC has wanted wchar_t instead of the correct C94
  67. and C99 wint_t.
  68. */
  69. printf ("%lc", lc); /* { dg-warning "length|C" "C90 bad use of %l" } */
  70. printf ("%ls", ls); /* { dg-warning "length|C" "C90 bad use of %l" } */
  71. /* Valid uses of each bare conversion. */
  72. printf ("%d%i%o%u%x%X%f%e%E%g%G%c%s%p%n%%", i, i, u, u, u, u, d, d, d, d, d,
  73. i, s, p, n);
  74. /* Uses of the - flag (valid on all non-%, non-n conversions). */
  75. printf ("%-d%-i%-o%-u%-x%-X%-f%-e%-E%-g%-G%-c%-s%-p", i, i, u, u, u, u,
  76. d, d, d, d, d, i, s, p);
  77. printf ("%-n", n); /* { dg-warning "flag" "bad use of %-n" } */
  78. /* Uses of the + flag (valid on signed conversions only). */
  79. printf ("%+d%+i%+f%+e%+E%+g%+G\n", i, i, d, d, d, d, d);
  80. printf ("%+o", u); /* { dg-warning "flag" "bad use of + flag" } */
  81. printf ("%+u", u); /* { dg-warning "flag" "bad use of + flag" } */
  82. printf ("%+x", u); /* { dg-warning "flag" "bad use of + flag" } */
  83. printf ("%+X", u); /* { dg-warning "flag" "bad use of + flag" } */
  84. printf ("%+c", i); /* { dg-warning "flag" "bad use of + flag" } */
  85. printf ("%+s", s); /* { dg-warning "flag" "bad use of + flag" } */
  86. printf ("%+p", p); /* { dg-warning "flag" "bad use of + flag" } */
  87. printf ("%+n", n); /* { dg-warning "flag" "bad use of + flag" } */
  88. /* Uses of the space flag (valid on signed conversions only, and ignored
  89. with +).
  90. */
  91. printf ("% +d", i); /* { dg-warning "use of both|ignored" "use of space and + flags" } */
  92. printf ("%+ d", i); /* { dg-warning "use of both|ignored" "use of space and + flags" } */
  93. printf ("% d% i% f% e% E% g% G\n", i, i, d, d, d, d, d);
  94. printf ("% o", u); /* { dg-warning "flag" "bad use of space flag" } */
  95. printf ("% u", u); /* { dg-warning "flag" "bad use of space flag" } */
  96. printf ("% x", u); /* { dg-warning "flag" "bad use of space flag" } */
  97. printf ("% X", u); /* { dg-warning "flag" "bad use of space flag" } */
  98. printf ("% c", i); /* { dg-warning "flag" "bad use of space flag" } */
  99. printf ("% s", s); /* { dg-warning "flag" "bad use of space flag" } */
  100. printf ("% p", p); /* { dg-warning "flag" "bad use of space flag" } */
  101. printf ("% n", n); /* { dg-warning "flag" "bad use of space flag" } */
  102. /* Uses of the # flag. */
  103. printf ("%#o%#x%#X%#e%#E%#f%#g%#G", u, u, u, d, d, d, d, d);
  104. printf ("%#d", i); /* { dg-warning "flag" "bad use of # flag" } */
  105. printf ("%#i", i); /* { dg-warning "flag" "bad use of # flag" } */
  106. printf ("%#u", u); /* { dg-warning "flag" "bad use of # flag" } */
  107. printf ("%#c", i); /* { dg-warning "flag" "bad use of # flag" } */
  108. printf ("%#s", s); /* { dg-warning "flag" "bad use of # flag" } */
  109. printf ("%#p", p); /* { dg-warning "flag" "bad use of # flag" } */
  110. printf ("%#n", n); /* { dg-warning "flag" "bad use of # flag" } */
  111. /* Uses of the 0 flag. */
  112. printf ("%08d%08i%08o%08u%08x%08X%08e%08E%08f%08g%08G", i, i, u, u, u, u,
  113. d, d, d, d, d);
  114. printf ("%0c", i); /* { dg-warning "flag" "bad use of 0 flag" } */
  115. printf ("%0s", s); /* { dg-warning "flag" "bad use of 0 flag" } */
  116. printf ("%0p", p); /* { dg-warning "flag" "bad use of 0 flag" } */
  117. printf ("%0n", n); /* { dg-warning "flag" "bad use of 0 flag" } */
  118. /* 0 flag ignored with - flag. */
  119. printf ("%-08d", i); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
  120. printf ("%-08i", i); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
  121. printf ("%-08o", u); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
  122. printf ("%-08u", u); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
  123. printf ("%-08x", u); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
  124. printf ("%-08X", u); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
  125. printf ("%-08e", d); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
  126. printf ("%-08E", d); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
  127. printf ("%-08f", d); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
  128. printf ("%-08g", d); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
  129. printf ("%-08G", d); /* { dg-warning "flags|ignored" "0 flag ignored with - flag" } */
  130. /* Various tests of bad argument types. */
  131. printf ("%d", l); /* { dg-warning "format" "bad argument types" } */
  132. printf ("%ld", i); /* { dg-warning "format" "bad argument types" } */
  133. printf ("%s", n); /* { dg-warning "format" "bad argument types" } */
  134. printf ("%p", i); /* { dg-warning "format" "bad argument types" } */
  135. printf ("%n", p); /* { dg-warning "format" "bad argument types" } */
  136. /* With -pedantic, we want some further checks for pointer targets:
  137. %p should allow only pointers to void (possibly qualified) and
  138. to character types (possibly qualified), but not function pointers
  139. or pointers to other types. (Whether, in fact, character types are
  140. allowed here is unclear; see thread on comp.std.c, July 2000 for
  141. discussion of the requirements of rules on identical representation,
  142. and of the application of the as if rule with the new va_arg
  143. allowances in C99 to printf.) Likewise, we should warn if
  144. pointer targets differ in signedness, except in some circumstances
  145. for character pointers. (In C99 we should consider warning for
  146. char * or unsigned char * being passed to %hhn, even if strictly
  147. legitimate by the standard.)
  148. */
  149. printf ("%p", foo); /* { dg-warning "format" "bad argument types" } */
  150. printf ("%n", un); /* { dg-warning "format" "bad argument types" } */
  151. printf ("%p", n); /* { dg-warning "format" "bad argument types" } */
  152. /* Allow character pointers with %p. */
  153. printf ("%p%p%p%p", s, ss, us, css);
  154. /* %s allows any character type. */
  155. printf ("%s%s%s%s", s, ss, us, css);
  156. /* Warning for void * arguments for %s is GCC's historical behavior,
  157. and seems useful to keep, even if some standard versions might be
  158. read to permit it.
  159. */
  160. printf ("%s", p); /* { dg-warning "format" "bad argument types" } */
  161. /* The historical behavior is to allow signed / unsigned types
  162. interchangably as arguments. For values representable in both types,
  163. such usage may be correct. For now preserve the behavior of GCC
  164. in such cases.
  165. */
  166. printf ("%d", u);
  167. /* Wrong number of arguments. */
  168. printf ("%d%d", i); /* { dg-warning "matching" "wrong number of args" } */
  169. printf ("%d", i, i); /* { dg-warning "arguments" "wrong number of args" } */
  170. /* Miscellaneous bogus constructions. */
  171. printf (""); /* { dg-warning "zero-length" "warning for empty format" } */
  172. printf ("\0"); /* { dg-warning "embedded" "warning for embedded NUL" } */
  173. printf ("%d\0", i); /* { dg-warning "embedded" "warning for embedded NUL" } */
  174. printf ("%d\0%d", i, i); /* { dg-warning "embedded|too many" "warning for embedded NUL" } */
  175. printf (NULL); /* { dg-warning "null" "null format string warning" } */
  176. printf ("%"); /* { dg-warning "trailing" "trailing % warning" } */
  177. printf ("%++d", i); /* { dg-warning "repeated" "repeated flag warning" } */
  178. printf ("%n", cn); /* { dg-warning "constant" "%n with const" } */
  179. printf ((const char *)L"foo"); /* { dg-warning "wide" "wide string" } */
  180. printf ("%n", (int *)0); /* { dg-warning "null" "%n with NULL" } */
  181. printf ("%s", (char *)0); /* { dg-warning "null" "%s with NULL" } */
  182. }