format-strings-fixit.c revision 2315318436b3e94d54c220c3b8986e8002394a43
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
51// Validate the fixes...
52// CHECK: printf("%d", (int) 123);
53// CHECK: printf("abc%s", "testing testing 123");
54// CHECK: printf("%ld", (long) -12);
55// CHECK: printf("%d", 123);
56// CHECK: printf("%s\n", "x");
57// CHECK: printf("%f\n", 1.23);
58// CHECK: printf("%.2llu", (unsigned long long) 123456);
59// CHECK: printf("%1Lf", (long double) 1.23);
60// CHECK: printf("%0u", (unsigned) 31337);
61// CHECK: printf("%p", (void *) 0);
62// CHECK: printf("%+f", 1.23);
63// CHECK: printf("%-f", 1.23);
64// CHECK: printf("%1$d:%2$.*3$d:%4$.*3$d\n", 1, 2, 3, 4);
65// CHECK: printf("%10.5ld", 1l);
66// CHECK: printf("%c", 'a');
67// CHECK: printf("%-f", 1.23);
68// CHECK: printf("%s", "foo");
69// CHECK: printf("%1$p", (void *)0);
70// CHECK: printf("%lX", val);
71