format-strings-fixit.c revision 36e76089b30180ba86598be95bbb0989657fe2fc
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 signed long int ssize_t; // FIXME: Figure out the right typedef. 52 typedef __INTMAX_TYPE__ intmax_t; 53 typedef __UINTMAX_TYPE__ uintmax_t; 54 typedef __PTRDIFF_TYPE__ ptrdiff_t; 55 56 // size_t, etc. 57 printf("%f", (size_t) 42); 58 //printf("%f", (ssize_t) 42); 59 printf("%f", (intmax_t) 42); 60 printf("%f", (uintmax_t) 42); 61 printf("%f", (ptrdiff_t) 42); 62} 63 64// Validate the fixes... 65// CHECK: printf("%d", (int) 123); 66// CHECK: printf("abc%s", "testing testing 123"); 67// CHECK: printf("%ld", (long) -12); 68// CHECK: printf("%d", 123); 69// CHECK: printf("%s\n", "x"); 70// CHECK: printf("%f\n", 1.23); 71// CHECK: printf("%.2llu", (unsigned long long) 123456); 72// CHECK: printf("%1Lf", (long double) 1.23); 73// CHECK: printf("%0u", (unsigned) 31337); 74// CHECK: printf("%p", (void *) 0); 75// CHECK: printf("%+f", 1.23); 76// CHECK: printf("%-f", 1.23); 77// CHECK: printf("%1$d:%2$.*3$d:%4$.*3$d\n", 1, 2, 3, 4); 78// CHECK: printf("%10.5ld", 1l); 79// CHECK: printf("%c", 'a'); 80// CHECK: printf("%-f", 1.23); 81// CHECK: printf("%s", "foo"); 82// CHECK: printf("%1$p", (void *)0); 83// CHECK: printf("%lX", val); 84// CHECK: printf("%zu", (size_t) 42); 85// CHECK: printf("%jd", (intmax_t) 42); 86// CHECK: printf("%ju", (uintmax_t) 42); 87// CHECK: printf("%td", (ptrdiff_t) 42); 88