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