PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

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

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