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