format-strings-ms.c 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // RUN: %clang_cc1 -fsyntax-only -verify -fms-compatibility -triple=i386-pc-win32 %s
  2. // RUN: %clang_cc1 -fsyntax-only -verify -fms-compatibility -triple=i386-pc-win32 -Wformat-non-iso -DNON_ISO_WARNING %s
  3. int printf(const char *format, ...) __attribute__((format(printf, 1, 2)));
  4. int scanf(const char * restrict, ...) ;
  5. typedef unsigned short wchar_t;
  6. #ifdef NON_ISO_WARNING
  7. // Split off this test to reduce the warning noise in the rest of the file.
  8. void non_iso_warning_test(__int32 i32, __int64 i64, wchar_t c, void *p) {
  9. printf("%Id", i32); // expected-warning{{'I' length modifier is not supported by ISO C}}
  10. printf("%I32d", i32); // expected-warning{{'I32' length modifier is not supported by ISO C}}
  11. printf("%I64d", i64); // expected-warning{{'I64' length modifier is not supported by ISO C}}
  12. printf("%wc", c); // expected-warning{{'w' length modifier is not supported by ISO C}}
  13. printf("%Z", p); // expected-warning{{'Z' conversion specifier is not supported by ISO C}}
  14. }
  15. #else
  16. void signed_test() {
  17. short val = 30;
  18. printf("val = %I64d\n", val); // expected-warning{{format specifies type '__int64' (aka 'long long') but the argument has type 'short'}}
  19. long long bigval = 30;
  20. printf("val = %I32d\n", bigval); // expected-warning{{format specifies type '__int32' (aka 'int') but the argument has type 'long long'}}
  21. printf("val = %Id\n", bigval); // expected-warning{{format specifies type '__int32' (aka 'int') but the argument has type 'long long'}}
  22. }
  23. void unsigned_test() {
  24. unsigned short val = 30;
  25. printf("val = %I64u\n", val); // expected-warning{{format specifies type 'unsigned __int64' (aka 'unsigned long long') but the argument has type 'unsigned short'}}
  26. unsigned long long bigval = 30;
  27. printf("val = %I32u\n", bigval); // expected-warning{{format specifies type 'unsigned __int32' (aka 'unsigned int') but the argument has type 'unsigned long long'}}
  28. printf("val = %Iu\n", bigval); // expected-warning{{format specifies type 'unsigned __int32' (aka 'unsigned int') but the argument has type 'unsigned long long'}}
  29. }
  30. void w_test(wchar_t c, wchar_t *s) {
  31. printf("%wc", c);
  32. printf("%wC", c);
  33. printf("%C", c);
  34. printf("%ws", s);
  35. printf("%wS", s);
  36. printf("%S", s);
  37. scanf("%wc", &c);
  38. scanf("%wC", &c);
  39. scanf("%C", &c);
  40. scanf("%ws", s);
  41. scanf("%wS", s);
  42. scanf("%S", s);
  43. double bad;
  44. printf("%wc", bad); // expected-warning{{format specifies type 'wint_t' (aka 'int') but the argument has type 'double'}}
  45. printf("%wC", bad); // expected-warning{{format specifies type 'wchar_t' (aka 'unsigned short') but the argument has type 'double'}}
  46. printf("%C", bad); // expected-warning{{format specifies type 'wchar_t' (aka 'unsigned short') but the argument has type 'double'}}
  47. printf("%ws", bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double'}}
  48. printf("%wS", bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double'}}
  49. printf("%S", bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double'}}
  50. scanf("%wc", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
  51. scanf("%wC", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
  52. scanf("%C", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
  53. scanf("%ws", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
  54. scanf("%wS", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
  55. scanf("%S", &bad); // expected-warning{{format specifies type 'wchar_t *' (aka 'unsigned short *') but the argument has type 'double *'}}
  56. }
  57. void h_test(char c, char* s) {
  58. double bad;
  59. printf("%hc", bad); // expected-warning{{format specifies type 'int' but the argument has type 'double'}}
  60. printf("%hC", bad); // expected-warning{{format specifies type 'int' but the argument has type 'double'}}
  61. printf("%hs", bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double'}}
  62. printf("%hS", bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double'}}
  63. scanf("%hc", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
  64. scanf("%hC", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
  65. scanf("%hs", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
  66. scanf("%hS", &bad); // expected-warning{{format specifies type 'char *' but the argument has type 'double *'}}
  67. }
  68. void z_test(void *p) {
  69. printf("%Z", p);
  70. printf("%hZ", p);
  71. printf("%lZ", p);
  72. printf("%wZ", p);
  73. printf("%hhZ", p); // expected-warning{{length modifier 'hh' results in undefined behavior or no effect with 'Z' conversion specifier}}
  74. scanf("%Z", p); // expected-warning{{invalid conversion specifier 'Z'}}
  75. }
  76. #endif