xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/format-strings-fixit.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc // RUN: cp %s %t
2f4a2713aSLionel Sambuc // RUN: %clang_cc1 -pedantic -Wall -fixit %t
3f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -pedantic -Wall -Werror %t
4f4a2713aSLionel Sambuc // RUN: %clang_cc1 -E -o - %t | FileCheck %s
5f4a2713aSLionel Sambuc 
6f4a2713aSLionel Sambuc /* This is a test of the various code modification hints that are
7f4a2713aSLionel Sambuc    provided as part of warning or extension diagnostics. All of the
8f4a2713aSLionel Sambuc    warnings will be fixed by -fixit, and the resulting file should
9f4a2713aSLionel Sambuc    compile cleanly with -Werror -pedantic. */
10f4a2713aSLionel Sambuc 
11f4a2713aSLionel Sambuc int printf(char const *, ...);
12f4a2713aSLionel Sambuc 
13f4a2713aSLionel Sambuc typedef __SIZE_TYPE__ size_t;
14f4a2713aSLionel Sambuc typedef __INTMAX_TYPE__ intmax_t;
15f4a2713aSLionel Sambuc typedef __UINTMAX_TYPE__ uintmax_t;
16f4a2713aSLionel Sambuc typedef __PTRDIFF_TYPE__ ptrdiff_t;
17*0a6a1f1dSLionel Sambuc typedef __WCHAR_TYPE__ wchar_t;
18f4a2713aSLionel Sambuc 
test()19f4a2713aSLionel Sambuc void test() {
20f4a2713aSLionel Sambuc   // Basic types
21f4a2713aSLionel Sambuc   printf("%s", (int) 123);
22f4a2713aSLionel Sambuc   printf("abc%0f", "testing testing 123");
23f4a2713aSLionel Sambuc   printf("%u", (long) -12);
24f4a2713aSLionel Sambuc   printf("%p", 123);
25f4a2713aSLionel Sambuc   printf("%c\n", "x");
26f4a2713aSLionel Sambuc   printf("%c\n", 1.23);
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc   // Larger types
29f4a2713aSLionel Sambuc   printf("%+.2d", (unsigned long long) 123456);
30f4a2713aSLionel Sambuc   printf("%1d", (long double) 1.23);
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc   // Flag handling
33f4a2713aSLionel Sambuc   printf("%0+s", (unsigned) 31337); // 0 flag should stay
34f4a2713aSLionel Sambuc   printf("%#p", (void *) 0);
35f4a2713aSLionel Sambuc   printf("% +f", 1.23); // + flag should stay
36f4a2713aSLionel Sambuc   printf("%0-f", 1.23); // - flag should stay
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc   // Positional arguments
39f4a2713aSLionel Sambuc #pragma clang diagnostic push // Don't warn about using positional arguments.
40f4a2713aSLionel Sambuc #pragma clang diagnostic ignored "-Wformat-non-iso"
41f4a2713aSLionel Sambuc   printf("%1$f:%2$.*3$f:%4$.*3$f\n", 1, 2, 3, 4);
42f4a2713aSLionel Sambuc #pragma clang diagnostic pop
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc   // Precision
45f4a2713aSLionel Sambuc   printf("%10.5d", 1l); // (bug 7394)
46f4a2713aSLionel Sambuc   printf("%.2c", 'a');
47f4a2713aSLionel Sambuc 
48f4a2713aSLionel Sambuc   // Ignored flags
49f4a2713aSLionel Sambuc   printf("%0-f", 1.23);
50f4a2713aSLionel Sambuc 
51f4a2713aSLionel Sambuc   // Bad length modifiers
52f4a2713aSLionel Sambuc   printf("%hhs", "foo");
53f4a2713aSLionel Sambuc #pragma clang diagnostic push // Don't warn about using positional arguments.
54f4a2713aSLionel Sambuc #pragma clang diagnostic ignored "-Wformat-non-iso"
55f4a2713aSLionel Sambuc   printf("%1$zp", (void *)0);
56f4a2713aSLionel Sambuc #pragma clang diagnostic pop
57f4a2713aSLionel Sambuc 
58f4a2713aSLionel Sambuc   // Preserve the original formatting for unsigned integers.
59f4a2713aSLionel Sambuc   unsigned long val = 42;
60f4a2713aSLionel Sambuc   printf("%X", val);
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc   // size_t, etc.
63f4a2713aSLionel Sambuc   printf("%f", (size_t) 42);
64f4a2713aSLionel Sambuc   printf("%f", (intmax_t) 42);
65f4a2713aSLionel Sambuc   printf("%f", (uintmax_t) 42);
66f4a2713aSLionel Sambuc   printf("%f", (ptrdiff_t) 42);
67f4a2713aSLionel Sambuc 
68f4a2713aSLionel Sambuc   // Look beyond the first typedef.
69f4a2713aSLionel Sambuc   typedef size_t my_size_type;
70f4a2713aSLionel Sambuc   typedef intmax_t my_intmax_type;
71f4a2713aSLionel Sambuc   typedef uintmax_t my_uintmax_type;
72f4a2713aSLionel Sambuc   typedef ptrdiff_t my_ptrdiff_type;
73f4a2713aSLionel Sambuc   typedef int my_int_type;
74f4a2713aSLionel Sambuc   printf("%f", (my_size_type) 42);
75f4a2713aSLionel Sambuc   printf("%f", (my_intmax_type) 42);
76f4a2713aSLionel Sambuc   printf("%f", (my_uintmax_type) 42);
77f4a2713aSLionel Sambuc   printf("%f", (my_ptrdiff_type) 42);
78f4a2713aSLionel Sambuc   printf("%f", (my_int_type) 42);
79f4a2713aSLionel Sambuc 
80f4a2713aSLionel Sambuc   // string
81f4a2713aSLionel Sambuc   printf("%ld", "foo");
82f4a2713aSLionel Sambuc 
83f4a2713aSLionel Sambuc   // Preserve the original choice of conversion specifier.
84f4a2713aSLionel Sambuc   printf("%o", (long) 42);
85f4a2713aSLionel Sambuc   printf("%u", (long) 42);
86f4a2713aSLionel Sambuc   printf("%x", (long) 42);
87f4a2713aSLionel Sambuc   printf("%X", (long) 42);
88f4a2713aSLionel Sambuc   printf("%i", (unsigned long) 42);
89f4a2713aSLionel Sambuc   printf("%d", (unsigned long) 42);
90f4a2713aSLionel Sambuc   printf("%F", (long double) 42);
91f4a2713aSLionel Sambuc   printf("%e", (long double) 42);
92f4a2713aSLionel Sambuc   printf("%E", (long double) 42);
93f4a2713aSLionel Sambuc   printf("%g", (long double) 42);
94f4a2713aSLionel Sambuc   printf("%G", (long double) 42);
95f4a2713aSLionel Sambuc   printf("%a", (long double) 42);
96f4a2713aSLionel Sambuc   printf("%A", (long double) 42);
97f4a2713aSLionel Sambuc }
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc int scanf(char const *, ...);
100f4a2713aSLionel Sambuc 
test2(int intSAParm[static2])101*0a6a1f1dSLionel Sambuc void test2(int intSAParm[static 2]) {
102f4a2713aSLionel Sambuc   char str[100];
103*0a6a1f1dSLionel Sambuc   char *vstr = "abc";
104f4a2713aSLionel Sambuc   short shortVar;
105f4a2713aSLionel Sambuc   unsigned short uShortVar;
106f4a2713aSLionel Sambuc   int intVar;
107*0a6a1f1dSLionel Sambuc   int intAVar[2];
108f4a2713aSLionel Sambuc   unsigned uIntVar;
109f4a2713aSLionel Sambuc   float floatVar;
110f4a2713aSLionel Sambuc   double doubleVar;
111f4a2713aSLionel Sambuc   long double longDoubleVar;
112f4a2713aSLionel Sambuc   long longVar;
113f4a2713aSLionel Sambuc   unsigned long uLongVar;
114f4a2713aSLionel Sambuc   long long longLongVar;
115f4a2713aSLionel Sambuc   unsigned long long uLongLongVar;
116f4a2713aSLionel Sambuc   size_t sizeVar;
117f4a2713aSLionel Sambuc   intmax_t intmaxVar;
118f4a2713aSLionel Sambuc   uintmax_t uIntmaxVar;
119f4a2713aSLionel Sambuc   ptrdiff_t ptrdiffVar;
120*0a6a1f1dSLionel Sambuc   enum {A, B, C} enumVar;
121f4a2713aSLionel Sambuc 
122*0a6a1f1dSLionel Sambuc   // Some string types.
123f4a2713aSLionel Sambuc   scanf("%lf", str);
124*0a6a1f1dSLionel Sambuc   scanf("%lf", vstr);
125*0a6a1f1dSLionel Sambuc   scanf("%ls", str);
126*0a6a1f1dSLionel Sambuc   scanf("%ls", str);
127*0a6a1f1dSLionel Sambuc 
128*0a6a1f1dSLionel Sambuc   // Some integer types.
129f4a2713aSLionel Sambuc   scanf("%f", &shortVar);
130f4a2713aSLionel Sambuc   scanf("%f", &uShortVar);
131f4a2713aSLionel Sambuc   scanf("%p", &intVar);
132*0a6a1f1dSLionel Sambuc   scanf("%f", intAVar);
133*0a6a1f1dSLionel Sambuc   scanf("%f", intSAParm);
134f4a2713aSLionel Sambuc   scanf("%Lf", &uIntVar);
135f4a2713aSLionel Sambuc   scanf("%ld", &floatVar);
136f4a2713aSLionel Sambuc   scanf("%f", &doubleVar);
137f4a2713aSLionel Sambuc   scanf("%d", &longDoubleVar);
138f4a2713aSLionel Sambuc   scanf("%f", &longVar);
139f4a2713aSLionel Sambuc   scanf("%f", &uLongVar);
140f4a2713aSLionel Sambuc   scanf("%f", &longLongVar);
141f4a2713aSLionel Sambuc   scanf("%f", &uLongLongVar);
142*0a6a1f1dSLionel Sambuc   scanf("%d", &enumVar); // FIXME: We ought to fix specifiers for enums.
143f4a2713aSLionel Sambuc 
144f4a2713aSLionel Sambuc   // Some named ints.
145f4a2713aSLionel Sambuc   scanf("%f", &sizeVar);
146f4a2713aSLionel Sambuc   scanf("%f", &intmaxVar);
147f4a2713aSLionel Sambuc   scanf("%f", &uIntmaxVar);
148f4a2713aSLionel Sambuc   scanf("%f", &ptrdiffVar);
149f4a2713aSLionel Sambuc 
150f4a2713aSLionel Sambuc   // Look beyond the first typedef for named integer types.
151f4a2713aSLionel Sambuc   typedef size_t my_size_type;
152f4a2713aSLionel Sambuc   typedef intmax_t my_intmax_type;
153f4a2713aSLionel Sambuc   typedef uintmax_t my_uintmax_type;
154f4a2713aSLionel Sambuc   typedef ptrdiff_t my_ptrdiff_type;
155f4a2713aSLionel Sambuc   typedef int my_int_type;
156f4a2713aSLionel Sambuc   scanf("%f", (my_size_type*)&sizeVar);
157f4a2713aSLionel Sambuc   scanf("%f", (my_intmax_type*)&intmaxVar);
158f4a2713aSLionel Sambuc   scanf("%f", (my_uintmax_type*)&uIntmaxVar);
159f4a2713aSLionel Sambuc   scanf("%f", (my_ptrdiff_type*)&ptrdiffVar);
160f4a2713aSLionel Sambuc   scanf("%f", (my_int_type*)&intVar);
161f4a2713aSLionel Sambuc 
162f4a2713aSLionel Sambuc   // Preserve the original formatting.
163f4a2713aSLionel Sambuc   scanf("%o", &longVar);
164f4a2713aSLionel Sambuc   scanf("%u", &longVar);
165f4a2713aSLionel Sambuc   scanf("%x", &longVar);
166f4a2713aSLionel Sambuc   scanf("%X", &longVar);
167f4a2713aSLionel Sambuc   scanf("%i", &uLongVar);
168f4a2713aSLionel Sambuc   scanf("%d", &uLongVar);
169f4a2713aSLionel Sambuc   scanf("%F", &longDoubleVar);
170f4a2713aSLionel Sambuc   scanf("%e", &longDoubleVar);
171f4a2713aSLionel Sambuc   scanf("%E", &longDoubleVar);
172f4a2713aSLionel Sambuc   scanf("%g", &longDoubleVar);
173f4a2713aSLionel Sambuc   scanf("%G", &longDoubleVar);
174f4a2713aSLionel Sambuc   scanf("%a", &longDoubleVar);
175f4a2713aSLionel Sambuc   scanf("%A", &longDoubleVar);
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc 
178f4a2713aSLionel Sambuc // Validate the fixes.
179f4a2713aSLionel Sambuc // CHECK: printf("%d", (int) 123);
180f4a2713aSLionel Sambuc // CHECK: printf("abc%s", "testing testing 123");
181f4a2713aSLionel Sambuc // CHECK: printf("%ld", (long) -12);
182f4a2713aSLionel Sambuc // CHECK: printf("%d", 123);
183f4a2713aSLionel Sambuc // CHECK: printf("%s\n", "x");
184f4a2713aSLionel Sambuc // CHECK: printf("%f\n", 1.23);
185f4a2713aSLionel Sambuc // CHECK: printf("%+.2lld", (unsigned long long) 123456);
186f4a2713aSLionel Sambuc // CHECK: printf("%1Lf", (long double) 1.23);
187f4a2713aSLionel Sambuc // CHECK: printf("%0u", (unsigned) 31337);
188f4a2713aSLionel Sambuc // CHECK: printf("%p", (void *) 0);
189f4a2713aSLionel Sambuc // CHECK: printf("%+f", 1.23);
190f4a2713aSLionel Sambuc // CHECK: printf("%-f", 1.23);
191f4a2713aSLionel Sambuc // CHECK: printf("%1$d:%2$.*3$d:%4$.*3$d\n", 1, 2, 3, 4);
192f4a2713aSLionel Sambuc // CHECK: printf("%10.5ld", 1l);
193f4a2713aSLionel Sambuc // CHECK: printf("%c", 'a');
194f4a2713aSLionel Sambuc // CHECK: printf("%-f", 1.23);
195f4a2713aSLionel Sambuc // CHECK: printf("%s", "foo");
196f4a2713aSLionel Sambuc // CHECK: printf("%1$p", (void *)0);
197f4a2713aSLionel Sambuc // CHECK: printf("%lX", val);
198f4a2713aSLionel Sambuc // CHECK: printf("%zu", (size_t) 42);
199f4a2713aSLionel Sambuc // CHECK: printf("%jd", (intmax_t) 42);
200f4a2713aSLionel Sambuc // CHECK: printf("%ju", (uintmax_t) 42);
201f4a2713aSLionel Sambuc // CHECK: printf("%td", (ptrdiff_t) 42);
202f4a2713aSLionel Sambuc // CHECK: printf("%zu", (my_size_type) 42);
203f4a2713aSLionel Sambuc // CHECK: printf("%jd", (my_intmax_type) 42);
204f4a2713aSLionel Sambuc // CHECK: printf("%ju", (my_uintmax_type) 42);
205f4a2713aSLionel Sambuc // CHECK: printf("%td", (my_ptrdiff_type) 42);
206f4a2713aSLionel Sambuc // CHECK: printf("%d", (my_int_type) 42);
207f4a2713aSLionel Sambuc // CHECK: printf("%s", "foo");
208f4a2713aSLionel Sambuc // CHECK: printf("%lo", (long) 42);
209f4a2713aSLionel Sambuc // CHECK: printf("%ld", (long) 42);
210f4a2713aSLionel Sambuc // CHECK: printf("%lx", (long) 42);
211f4a2713aSLionel Sambuc // CHECK: printf("%lX", (long) 42);
212f4a2713aSLionel Sambuc // CHECK: printf("%lu", (unsigned long) 42);
213f4a2713aSLionel Sambuc // CHECK: printf("%lu", (unsigned long) 42);
214f4a2713aSLionel Sambuc // CHECK: printf("%LF", (long double) 42);
215f4a2713aSLionel Sambuc // CHECK: printf("%Le", (long double) 42);
216f4a2713aSLionel Sambuc // CHECK: printf("%LE", (long double) 42);
217f4a2713aSLionel Sambuc // CHECK: printf("%Lg", (long double) 42);
218f4a2713aSLionel Sambuc // CHECK: printf("%LG", (long double) 42);
219f4a2713aSLionel Sambuc // CHECK: printf("%La", (long double) 42);
220f4a2713aSLionel Sambuc // CHECK: printf("%LA", (long double) 42);
221f4a2713aSLionel Sambuc 
222*0a6a1f1dSLionel Sambuc // CHECK: scanf("%99s", str);
223*0a6a1f1dSLionel Sambuc // CHECK: scanf("%s", vstr);
224*0a6a1f1dSLionel Sambuc // CHECK: scanf("%99s", str);
225*0a6a1f1dSLionel Sambuc // CHECK: scanf("%99s", str);
226f4a2713aSLionel Sambuc // CHECK: scanf("%hd", &shortVar);
227f4a2713aSLionel Sambuc // CHECK: scanf("%hu", &uShortVar);
228f4a2713aSLionel Sambuc // CHECK: scanf("%d", &intVar);
229*0a6a1f1dSLionel Sambuc // CHECK: scanf("%d", intAVar);
230*0a6a1f1dSLionel Sambuc // CHECK: scanf("%d", intSAParm);
231f4a2713aSLionel Sambuc // CHECK: scanf("%u", &uIntVar);
232f4a2713aSLionel Sambuc // CHECK: scanf("%f", &floatVar);
233f4a2713aSLionel Sambuc // CHECK: scanf("%lf", &doubleVar);
234f4a2713aSLionel Sambuc // CHECK: scanf("%Lf", &longDoubleVar);
235f4a2713aSLionel Sambuc // CHECK: scanf("%ld", &longVar);
236f4a2713aSLionel Sambuc // CHECK: scanf("%lu", &uLongVar);
237f4a2713aSLionel Sambuc // CHECK: scanf("%lld", &longLongVar);
238f4a2713aSLionel Sambuc // CHECK: scanf("%llu", &uLongLongVar);
239*0a6a1f1dSLionel Sambuc // CHECK: scanf("%d", &enumVar);
240f4a2713aSLionel Sambuc // CHECK: scanf("%zu", &sizeVar);
241f4a2713aSLionel Sambuc // CHECK: scanf("%jd", &intmaxVar);
242f4a2713aSLionel Sambuc // CHECK: scanf("%ju", &uIntmaxVar);
243f4a2713aSLionel Sambuc // CHECK: scanf("%td", &ptrdiffVar);
244f4a2713aSLionel Sambuc // CHECK: scanf("%zu", (my_size_type*)&sizeVar);
245f4a2713aSLionel Sambuc // CHECK: scanf("%jd", (my_intmax_type*)&intmaxVar);
246f4a2713aSLionel Sambuc // CHECK: scanf("%ju", (my_uintmax_type*)&uIntmaxVar);
247f4a2713aSLionel Sambuc // CHECK: scanf("%td", (my_ptrdiff_type*)&ptrdiffVar);
248f4a2713aSLionel Sambuc // CHECK: scanf("%d", (my_int_type*)&intVar);
249f4a2713aSLionel Sambuc // CHECK: scanf("%lo", &longVar);
250f4a2713aSLionel Sambuc // CHECK: scanf("%lu", &longVar);
251f4a2713aSLionel Sambuc // CHECK: scanf("%lx", &longVar);
252f4a2713aSLionel Sambuc // CHECK: scanf("%lX", &longVar);
253f4a2713aSLionel Sambuc // CHECK: scanf("%li", &uLongVar);
254f4a2713aSLionel Sambuc // CHECK: scanf("%ld", &uLongVar);
255f4a2713aSLionel Sambuc // CHECK: scanf("%LF", &longDoubleVar);
256f4a2713aSLionel Sambuc // CHECK: scanf("%Le", &longDoubleVar);
257f4a2713aSLionel Sambuc // CHECK: scanf("%LE", &longDoubleVar);
258f4a2713aSLionel Sambuc // CHECK: scanf("%Lg", &longDoubleVar);
259f4a2713aSLionel Sambuc // CHECK: scanf("%LG", &longDoubleVar);
260f4a2713aSLionel Sambuc // CHECK: scanf("%La", &longDoubleVar);
261f4a2713aSLionel Sambuc // CHECK: scanf("%LA", &longDoubleVar);
262