format-strings-fixit.c revision 117348caf0a8f91782e9e32e32a8689576f394d7
1// RUN: cp %s %t 2// RUN: %clang_cc1 -pedantic -Wall -fixit %t 3// RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Werror %t 4// RUN: %clang_cc1 -E -o - %t | FileCheck %s 5 6/* This is a test of the various code modification hints that are 7 provided as part of warning or extension diagnostics. All of the 8 warnings will be fixed by -fixit, and the resulting file should 9 compile cleanly with -Werror -pedantic. */ 10 11int printf(char const *, ...); 12 13void test() { 14 // Basic types 15 printf("%s", (int) 123); 16 printf("abc%0f", "testing testing 123"); 17 printf("%u", (long) -12); 18 printf("%p", 123); 19 printf("%c\n", "x"); 20 printf("%c\n", 1.23); 21 22 // Larger types 23 printf("%+.2d", (unsigned long long) 123456); 24 printf("%1d", (long double) 1.23); 25 26 // Flag handling 27 printf("%0+s", (unsigned) 31337); // 0 flag should stay 28 printf("%#p", (void *) 0); 29 printf("% +f", 1.23); // + flag should stay 30 printf("%0-f", 1.23); // - flag should stay 31 32 // Positional arguments 33 printf("%1$f:%2$.*3$f:%4$.*3$f\n", 1, 2, 3, 4); 34 35 // Precision 36 printf("%10.5d", 1l); // (bug 7394) 37 printf("%.2c", 'a'); 38 39 // Ignored flags 40 printf("%0-f", 1.23); 41 42 // Bad length modifiers 43 printf("%hhs", "foo"); 44 printf("%1$zp", (void *)0); 45 46 // Perserve the original formatting for unsigned integers. 47 unsigned long val = 42; 48 printf("%X", val); 49 50 typedef __SIZE_TYPE__ size_t; 51 typedef __INTMAX_TYPE__ intmax_t; 52 typedef __UINTMAX_TYPE__ uintmax_t; 53 typedef __PTRDIFF_TYPE__ ptrdiff_t; 54 55 // size_t, etc. 56 printf("%f", (size_t) 42); 57 printf("%f", (intmax_t) 42); 58 printf("%f", (uintmax_t) 42); 59 printf("%f", (ptrdiff_t) 42); 60 61 // string 62 printf("%ld", "foo"); 63} 64 65// Validate the fixes... 66// CHECK: printf("%d", (int) 123); 67// CHECK: printf("abc%s", "testing testing 123"); 68// CHECK: printf("%ld", (long) -12); 69// CHECK: printf("%d", 123); 70// CHECK: printf("%s\n", "x"); 71// CHECK: printf("%f\n", 1.23); 72// CHECK: printf("%.2llu", (unsigned long long) 123456); 73// CHECK: printf("%1Lf", (long double) 1.23); 74// CHECK: printf("%0u", (unsigned) 31337); 75// CHECK: printf("%p", (void *) 0); 76// CHECK: printf("%+f", 1.23); 77// CHECK: printf("%-f", 1.23); 78// CHECK: printf("%1$d:%2$.*3$d:%4$.*3$d\n", 1, 2, 3, 4); 79// CHECK: printf("%10.5ld", 1l); 80// CHECK: printf("%c", 'a'); 81// CHECK: printf("%-f", 1.23); 82// CHECK: printf("%s", "foo"); 83// CHECK: printf("%1$p", (void *)0); 84// CHECK: printf("%lX", val); 85// CHECK: printf("%zu", (size_t) 42); 86// CHECK: printf("%jd", (intmax_t) 42); 87// CHECK: printf("%ju", (uintmax_t) 42); 88// CHECK: printf("%td", (ptrdiff_t) 42); 89// CHECK: printf("%s", "foo"); 90