aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/format/diagnostic-ranges.c
blob: d3e334d581c9503afd61eb5f545e78ad21311919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* { dg-options "-Wformat -fdiagnostics-show-caret -fdiagnostics-add-output=experimental-html:javascript=no" } */

/* See PR 52952. */

#include "format.h"

void test_mismatching_types (const char *msg)
{
  printf("hello %i", msg);  /* { dg-warning "format '%i' expects argument of type 'int', but argument 2 has type 'const char \\*' " } */

/* { dg-begin-multiline-output "" }
   printf("hello %i", msg);
                 ~^   ~~~
                  |   |
                  int const char *
                 %s
   { dg-end-multiline-output "" } */


  printf("hello %s", 42);  /* { dg-warning "format '%s' expects argument of type 'char \\*', but argument 2 has type 'int'" } */
/* { dg-begin-multiline-output "" }
   printf("hello %s", 42);
                 ~^   ~~
                  |   |
                  |   int
                  char *
                 %d
   { dg-end-multiline-output "" } */

  printf("hello %i", (long)0);  /* { dg-warning "format '%i' expects argument of type 'int', but argument 2 has type 'long int' " } */
/* { dg-begin-multiline-output "" }
   printf("hello %i", (long)0);
                 ~^   ~~~~~~~
                  |   |
                  int long int
                 %li
   { dg-end-multiline-output "" } */
}

void test_multiple_arguments (void)
{
  printf ("arg0: %i  arg1: %s arg 2: %i", /* { dg-warning "29: format '%s'" } */
          100, 101, 102);
/* { dg-begin-multiline-output "" }
   printf ("arg0: %i  arg1: %s arg 2: %i",
                            ~^
                             |
                             char *
                            %d
           100, 101, 102);
                ~~~           
                |
                int
   { dg-end-multiline-output "" } */
}

void test_multiple_arguments_2 (int i, int j)
{
  printf ("arg0: %i  arg1: %s arg 2: %i", /* { dg-warning "29: format '%s'" } */
          100, i + j, 102);
/* { dg-begin-multiline-output "" }
   printf ("arg0: %i  arg1: %s arg 2: %i",
                            ~^
                             |
                             char *
                            %d
           100, i + j, 102);
                ~~~~~         
                  |
                  int
   { dg-end-multiline-output "" } */
}

void multiline_format_string (void) {
  printf ("before the fmt specifier" /* { dg-warning "11: format '%d' expects a matching 'int' argument" } */
/* { dg-begin-multiline-output "" }
   printf ("before the fmt specifier"
           ^~~~~~~~~~~~~~~~~~~~~~~~~~
   { dg-end-multiline-output "" } */

          "%"
          "d" /* { dg-message "12: format string is defined here" } */
          "after the fmt specifier");

/* { dg-begin-multiline-output "" }
           "%"
            ~~
           "d"
           ~^
            |
            int
   { dg-end-multiline-output "" } */
}

void test_hex (const char *msg)
{
  /* "%" is \x25
     "i" is \x69 */
  printf("hello \x25\x69", msg);  /* { dg-warning "format '%i' expects argument of type 'int', but argument 2 has type 'const char \\*' " } */

/* { dg-begin-multiline-output "" }
   printf("hello \x25\x69", msg);
                 ~~~~^~~~   ~~~
                     |      |
                     int    const char *
                 \x25s
   { dg-end-multiline-output "" } */
}

void test_oct (const char *msg)
{
  /* "%" is octal 045
     "i" is octal 151.  */
  printf("hello \045\151", msg);  /* { dg-warning "format '%i' expects argument of type 'int', but argument 2 has type 'const char \\*' " } */

/* { dg-begin-multiline-output "" }
   printf("hello \045\151", msg);
                 ~~~~^~~~   ~~~
                     |      |
                     int    const char *
                 \045s
   { dg-end-multiline-output "" } */
}

void test_multiple (const char *msg)
{
  /* "%" is \x25 in hex
     "i" is \151 in octal.  */
  printf("prefix"  "\x25"  "\151"  "suffix",  /* { dg-warning "format '%i'" } */
         msg);
/* { dg-begin-multiline-output "" }
   printf("prefix"  "\x25"  "\151"  "suffix",
          ^~~~~~~~
          msg);
          ~~~
          |
          const char *
  { dg-end-multiline-output "" } */

/* { dg-begin-multiline-output "" }
   printf("prefix"  "\x25"  "\151"  "suffix",
                     ~~~~~~~~^~~~
                             |
                             int
                     \x25"  "s
  { dg-end-multiline-output "" } */
}

void test_u8 (const char *msg)
{
  printf(u8"hello %i", msg);/* { dg-warning "format '%i' expects argument of type 'int', but argument 2 has type 'const char \\*' " } */
/* { dg-begin-multiline-output "" }
   printf(u8"hello %i", msg);
                   ~^   ~~~
                    |   |
                    int const char *
                   %s
   { dg-end-multiline-output "" } */
}

void test_param (long long_i, long long_j)
{
  printf ("foo %s bar", long_i + long_j); /* { dg-warning "17: format '%s' expects argument of type 'char \\*', but argument 2 has type 'long int'" } */
/* { dg-begin-multiline-output "" }
   printf ("foo %s bar", long_i + long_j);
                ~^       ~~~~~~~~~~~~~~~
                 |              |
                 char *         long int
                %ld
   { dg-end-multiline-output "" } */
}

void test_field_width_specifier (long l, int i1, int i2)
{
  printf (" %*.*d ", l, i1, i2); /* { dg-warning "14: field width specifier '\\*' expects argument of type 'int', but argument 2 has type 'long int'" } */
/* { dg-begin-multiline-output "" }
   printf (" %*.*d ", l, i1, i2);
             ~^~~~    ~
              |       |
              int     long int
   { dg-end-multiline-output "" } */
}

/* PR c/72857.  */

void test_field_width_specifier_2 (char *d, long foo, long bar)
{
  __builtin_sprintf (d, " %*ld ", foo, foo); /* { dg-warning "28: field width specifier '\\*' expects argument of type 'int', but argument 3 has type 'long int'" } */
  /* { dg-begin-multiline-output "" }
   __builtin_sprintf (d, " %*ld ", foo, foo);
                           ~^~~    ~~~
                            |      |
                            int    long int
   { dg-end-multiline-output "" } */

  __builtin_sprintf (d, " %*ld ", foo + bar, foo); /* { dg-warning "28: field width specifier '\\*' expects argument of type 'int', but argument 3 has type 'long int'" } */
  /* { dg-begin-multiline-output "" }
   __builtin_sprintf (d, " %*ld ", foo + bar, foo);
                           ~^~~    ~~~~~~~~~
                            |          |
                            int        long int
   { dg-end-multiline-output "" } */
}

void test_field_precision_specifier (char *d, long foo, long bar)
{
  __builtin_sprintf (d, " %.*ld ", foo, foo); /* { dg-warning "29: field precision specifier '\\.\\*' expects argument of type 'int', but argument 3 has type 'long int'" } */
  /* { dg-begin-multiline-output "" }
   __builtin_sprintf (d, " %.*ld ", foo, foo);
                           ~~^~~    ~~~
                             |      |
                             int    long int
   { dg-end-multiline-output "" } */

  __builtin_sprintf (d, " %.*ld ", foo + bar, foo); /* { dg-warning "29: field precision specifier '\\.\\*' expects argument of type 'int', but argument 3 has type 'long int'" } */
  /* { dg-begin-multiline-output "" }
   __builtin_sprintf (d, " %.*ld ", foo + bar, foo);
                           ~~^~~    ~~~~~~~~~
                             |          |
                             int        long int
   { dg-end-multiline-output "" } */
}

void test_spurious_percent (void)
{
  printf("hello world %"); /* { dg-warning "23: spurious trailing" } */

/* { dg-begin-multiline-output "" }
   printf("hello world %");
                       ^
   { dg-end-multiline-output "" } */
}

void test_empty_precision (char *s, size_t m, double d)
{
  strfmon (s, m, "%#.5n", d); /* { dg-warning "20: empty left precision in gnu_strfmon format" } */
/* { dg-begin-multiline-output "" }
   strfmon (s, m, "%#.5n", d);
                    ^
   { dg-end-multiline-output "" } */

  strfmon (s, m, "%#5.n", d); /* { dg-warning "22: empty precision in gnu_strfmon format" } */
/* { dg-begin-multiline-output "" }
   strfmon (s, m, "%#5.n", d);
                      ^
   { dg-end-multiline-output "" } */
}

void test_repeated (int i)
{
  printf ("%++d", i); /* { dg-warning "14: repeated '\\+' flag in format" } */
/* { dg-begin-multiline-output "" }
   printf ("%++d", i);
              ^
   { dg-end-multiline-output "" } */
}

void test_conversion_lacks_type (void)
{
  printf (" %h"); /* { dg-warning "14:conversion lacks type at end of format" } */
/* { dg-begin-multiline-output "" }
   printf (" %h");
              ^
   { dg-end-multiline-output "" } */
}

void test_embedded_nul (void)
{
  printf (" \0 "); /* { dg-warning "13:embedded" "warning for embedded NUL" } */
/* { dg-begin-multiline-output "" }
   printf (" \0 ");
             ^~
   { dg-end-multiline-output "" } */
}

void test_macro (const char *msg)
{
#define INT_FMT "%i" /* { dg-message "19: format string is defined here" } */
  printf("hello " INT_FMT " world", msg);  /* { dg-warning "10: format '%i' expects argument of type 'int', but argument 2 has type 'const char \\*' " } */
/* { dg-begin-multiline-output "" }
   printf("hello " INT_FMT " world", msg);
          ^~~~~~~~                   ~~~
                                     |
                                     const char *
   { dg-end-multiline-output "" } */
/* { dg-begin-multiline-output "" }
 #define INT_FMT "%i"
                  ~^
                   |
                   int
                  %s
   { dg-end-multiline-output "" } */
#undef INT_FMT
}

void test_macro_2 (const char *msg)
{
#define PRIu32 "u" /* { dg-message "17: format string is defined here" } */
  printf("hello %" PRIu32 " world", msg);  /* { dg-warning "10: format '%u' expects argument of type 'unsigned int', but argument 2 has type 'const char \\*' " } */
/* { dg-begin-multiline-output "" }
   printf("hello %" PRIu32 " world", msg);
          ^~~~~~~~~                  ~~~
                                     |
                                     const char *
   { dg-end-multiline-output "" } */
/* { dg-begin-multiline-output "" }
 #define PRIu32 "u"
                 ^
                 |
                 unsigned int
   { dg-end-multiline-output "" } */
#undef PRIu32
}

void test_macro_3 (const char *msg)
{
#define FMT_STRING "hello %i world" /* { dg-line test_macro_3_macro_line } */
  /* { dg-warning "20: format '%i' expects argument of type 'int', but argument 2 has type 'const char \\*'" "" { target *-*-*} .-1 } */
  printf(FMT_STRING, msg);  /* { dg-message "10: in expansion of macro 'FMT_STRING" } */
/* { dg-begin-multiline-output "" }
 #define FMT_STRING "hello %i world"
                    ^~~~~~~~~~~~~~~~
   { dg-end-multiline-output "" } */
/* { dg-begin-multiline-output "" }
   printf(FMT_STRING, msg);
          ^~~~~~~~~~
   { dg-end-multiline-output "" } */
/* { dg-message "28: format string is defined here" "" { target *-*-* } test_macro_3_macro_line } */
/* { dg-begin-multiline-output "" }
 #define FMT_STRING "hello %i world"
                           ~^
                            |
                            int
                           %s
   { dg-end-multiline-output "" } */
#undef FMT_STRING
}

void test_macro_4 (const char *msg)
{
#define FMT_STRING "hello %i world" /* { dg-warning "20: format '%i' expects argument of type 'int', but argument 2 has type 'const char \\*' " } */
  printf(FMT_STRING "\n", msg);  /* { dg-message "10: in expansion of macro 'FMT_STRING" } */
/* { dg-begin-multiline-output "" }
 #define FMT_STRING "hello %i world"
                    ^~~~~~~~~~~~~~~~
   { dg-end-multiline-output "" } */
/* { dg-begin-multiline-output "" }
   printf(FMT_STRING "\n", msg);
          ^~~~~~~~~~
   { dg-end-multiline-output "" } */
/* { dg-begin-multiline-output "" }
 #define FMT_STRING "hello %i world"
                           ~^
                            |
                            int
                           %s
   { dg-end-multiline-output "" } */
#undef FMT_STRING
}

void test_non_contiguous_strings (void)
{
  __builtin_printf(" %" "d ", 0.5); /* { dg-warning "20: format .%d. expects argument of type .int., but argument 2 has type .double." } */
                                    /* { dg-message "26: format string is defined here" "" { target *-*-* } .-1 } */
  /* { dg-begin-multiline-output "" }
   __builtin_printf(" %" "d ", 0.5);
                    ^~~~       ~~~
                               |
                               double
   { dg-end-multiline-output "" } */
  /* { dg-begin-multiline-output "" }
   __builtin_printf(" %" "d ", 0.5);
                      ~~~~^
                          |
                          int
                      %" "f
   { dg-end-multiline-output "" } */
}

void test_const_arrays (void)
{
  /* TODO: ideally we'd highlight both the format string *and* the use of
     it here.  For now, just verify that we gracefully handle this case.  */
  const char a[] = " %d ";
  __builtin_printf(a, 0.5); /* { dg-warning "20: format .%d. expects argument of type .int., but argument 2 has type .double." } */
  /* { dg-begin-multiline-output "" }
   __builtin_printf(a, 0.5);
                    ^  ~~~
                       |
                       double
   { dg-end-multiline-output "" } */
}

/* Use a Python script to verify various properties about the generated
   HTML file:
   { dg-final { run-html-pytest diagnostic-ranges.c "diagnostic-ranges-html.py" } } */