format-strings-gnu.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // RUN: %clang_cc1 -fsyntax-only -verify -triple i386-apple-darwin9 %s
  2. // RUN: %clang_cc1 -fsyntax-only -verify -triple thumbv6-apple-ios4.0 %s
  3. // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-mingw32 -DMS %s
  4. // RUN: %clang_cc1 -fsyntax-only -verify -triple i686-pc-win32 -DMS %s
  5. // RUN: %clang_cc1 -fsyntax-only -verify -triple i686-linux-gnu -DALLOWED %s
  6. // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-unknown-freebsd -DALLOWED %s
  7. int printf(const char *restrict, ...);
  8. int scanf(const char * restrict, ...) ;
  9. void test() {
  10. long notLongEnough = 1;
  11. long long quiteLong = 2;
  12. printf("%Ld", notLongEnough); // expected-warning {{format specifies type 'long long' but the argument has type 'long'}}
  13. printf("%Ld", quiteLong);
  14. #ifndef ALLOWED
  15. // expected-warning@-4 {{length modifier 'L' results in undefined behavior or no effect with 'd' conversion specifier}}
  16. // expected-note@-5 {{did you mean to use 'll'?}}
  17. // expected-warning@-6 {{length modifier 'L' results in undefined behavior or no effect with 'd' conversion specifier}}
  18. // expected-note@-7 {{did you mean to use 'll'?}}
  19. #endif
  20. #ifndef MS
  21. printf("%Z\n", quiteLong); // expected-warning{{invalid conversion specifier 'Z'}}
  22. #endif
  23. }
  24. void testAlwaysInvalid() {
  25. // We should not suggest 'll' here!
  26. printf("%Lc", 'a'); // expected-warning {{length modifier 'L' results in undefined behavior or no effect with 'c' conversion specifier}}
  27. printf("%Ls", "a"); // expected-warning {{length modifier 'L' results in undefined behavior or no effect with 's' conversion specifier}}
  28. }
  29. #ifdef ALLOWED
  30. // PR 9466: clang: doesn't know about %Lu, %Ld, and %Lx
  31. void printf_longlong(long long x, unsigned long long y) {
  32. printf("%Ld", y); // no-warning
  33. printf("%Lu", y); // no-warning
  34. printf("%Lx", y); // no-warning
  35. printf("%Ld", x); // no-warning
  36. printf("%Lu", x); // no-warning
  37. printf("%Lx", x); // no-warning
  38. printf("%Ls", "hello"); // expected-warning {{length modifier 'L' results in undefined behavior or no effect with 's' conversion specifier}}
  39. }
  40. void scanf_longlong(long long *x, unsigned long long *y) {
  41. scanf("%Ld", y); // no-warning
  42. scanf("%Lu", y); // no-warning
  43. scanf("%Lx", y); // no-warning
  44. scanf("%Ld", x); // no-warning
  45. scanf("%Lu", x); // no-warning
  46. scanf("%Lx", x); // no-warning
  47. scanf("%Ls", "hello"); // expected-warning {{length modifier 'L' results in undefined behavior or no effect with 's' conversion specifier}}
  48. }
  49. #endif