format-strings-fixit.c revision 68db6f0541bb9d54ab2ddffec4f7e2c0294e1f85
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
62// Validate the fixes...
63// CHECK: printf("%d", (int) 123);
64// CHECK: printf("abc%s", "testing testing 123");
65// CHECK: printf("%ld", (long) -12);
66// CHECK: printf("%d", 123);
67// CHECK: printf("%s\n", "x");
68// CHECK: printf("%f\n", 1.23);
69// CHECK: printf("%.2llu", (unsigned long long) 123456);
70// CHECK: printf("%1Lf", (long double) 1.23);
71// CHECK: printf("%0u", (unsigned) 31337);
72// CHECK: printf("%p", (void *) 0);
73// CHECK: printf("%+f", 1.23);
74// CHECK: printf("%-f", 1.23);
75// CHECK: printf("%1$d:%2$.*3$d:%4$.*3$d\n", 1, 2, 3, 4);
76// CHECK: printf("%10.5ld", 1l);
77// CHECK: printf("%c", 'a');
78// CHECK: printf("%-f", 1.23);
79// CHECK: printf("%s", "foo");
80// CHECK: printf("%1$p", (void *)0);
81// CHECK: printf("%lX", val);
82// CHECK: printf("%zu", (size_t) 42);
83// CHECK: printf("%jd", (intmax_t) 42);
84// CHECK: printf("%ju", (uintmax_t) 42);
85// CHECK: printf("%td", (ptrdiff_t) 42);
86