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