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
|
/* { dg-do compile } */
/* { dg-options "-O -fdiagnostics-show-caret" } */
/* This is a collection of unittests for ranges within string literals,
using diagnostic_plugin_test_string_literals, which handles
"__emit_string_literal_range" by generating a warning at the given
subset of a string literal.
The indices are 0-based. It's easiest to verify things using string
literals that are runs of 0-based digits (to avoid having to count
characters).
LITERAL is a const void * to allow testing the various kinds of wide
string literal, rather than just const char *. */
extern void __emit_string_literal_range (const void *literal, int caret_idx,
int start_idx, int end_idx);
void
test_simple_string_literal (void)
{
__emit_string_literal_range ("0123456789", /* { dg-warning "range" } */
6, 6, 7);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range ("0123456789",
^~
{ dg-end-multiline-output "" } */
}
void
test_concatenated_string_literal (void)
{
__emit_string_literal_range ("01234" "56789", /* { dg-warning "range" } */
4, 3, 6);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range ("01234" "56789",
~^~~~~~
{ dg-end-multiline-output "" } */
}
void
test_multiline_string_literal (void)
{
__emit_string_literal_range ("01234" /* { dg-warning "range" } */
"56789",
4, 3, 6);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range ("01234"
~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"56789",
~~~
{ dg-end-multiline-output "" } */
/* FIXME: why does the above need three trailing spaces? */
}
/* Tests of various unicode encodings.
Digits 0 through 9 are unicode code points:
U+0030 DIGIT ZERO
...
U+0039 DIGIT NINE
However, these are not always valid as UCN (see the comment in
libcpp/charset.c:_cpp_valid_ucn).
Hence we need to test UCN using an alternative unicode
representation of numbers; let's use Roman numerals,
(though these start at one, not zero):
U+2170 SMALL ROMAN NUMERAL ONE
...
U+2174 SMALL ROMAN NUMERAL FIVE ("v")
U+2175 SMALL ROMAN NUMERAL SIX ("vi")
...
U+2178 SMALL ROMAN NUMERAL NINE. */
void
test_hex (void)
{
/* Digits 0-9, expressing digit 5 in ASCII as "\x35"
and with a space in place of digit 6, to terminate the escaped
hex code. */
__emit_string_literal_range ("01234\x35 789", /* { dg-warning "range" } */
4, 3, 7);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range ("01234\x35 789"
~^~~~~~~
{ dg-end-multiline-output "" } */
}
void
test_oct (void)
{
/* Digits 0-9, expressing digit 5 in ASCII as "\065"
and with a space in place of digit 6, to terminate the escaped
octal code. */
__emit_string_literal_range ("01234\065 789", /* { dg-warning "range" } */
4, 3, 7);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range ("01234\065 789"
~^~~~~~~
{ dg-end-multiline-output "" } */
}
void
test_multiple (void)
{
/* Digits 0-9, expressing digit 5 in ASCII as hex "\x35"
digit 6 in ASCII as octal "\066", concatenating multiple strings. */
__emit_string_literal_range ("01234" "\x35" "\066" "789", /* { dg-warning "range" } */
5, 3, 8);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range ("01234" "\x35" "\066" "789",
~~~~~~^~~~~~~~~~~~~~~~~~
{ dg-end-multiline-output "" } */
}
void
test_ucn4 (void)
{
/* Digits 0-9, expressing digits 5 and 6 as Roman numerals expressed
as UCN 4.
The resulting string is encoded as UTF-8. Most of the digits are 1 byte
each, but digits 5 and 6 are encoded with 3 bytes each.
Hence to underline digits 4-7 we need to underling using bytes 4-11 in
the UTF-8 encoding. */
__emit_string_literal_range ("01234\u2174\u2175789", /* { dg-warning "range" } */
5, 4, 11);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range ("01234\u2174\u2175789",
~^~~~~~~~~~~~~
{ dg-end-multiline-output "" } */
}
void
test_ucn8 (void)
{
/* Digits 0-9, expressing digits 5 and 6 as Roman numerals as UCN 8.
The resulting string is the same as as in test_ucn4 above, and hence
has the same UTF-8 encoding, and so we again need to underline bytes
4-11 in the UTF-8 encoding in order to underline digits 4-7. */
__emit_string_literal_range ("01234\U00002174\U00002175789", /* { dg-warning "range" } */
5, 4, 11);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range ("01234\U00002174\U00002175789",
~^~~~~~~~~~~~~~~~~~~~~
{ dg-end-multiline-output "" } */
}
void
test_u8 (void)
{
/* Digits 0-9. */
__emit_string_literal_range (u8"0123456789", /* { dg-warning "range" } */
6, 4, 7);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range (u8"0123456789",
~~^~
{ dg-end-multiline-output "" } */
}
void
test_u (void)
{
/* Digits 0-9. */
__emit_string_literal_range (u"0123456789", /* { dg-error "unable to read substring location: execution character set != source character set" } */
6, 4, 7);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range (u"0123456789",
^~~~~~~~~~~~~
{ dg-end-multiline-output "" } */
}
void
test_U (void)
{
/* Digits 0-9. */
__emit_string_literal_range (U"0123456789", /* { dg-error "unable to read substring location: execution character set != source character set" } */
6, 4, 7);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range (U"0123456789",
^~~~~~~~~~~~~
{ dg-end-multiline-output "" } */
}
void
test_L (void)
{
/* Digits 0-9. */
__emit_string_literal_range (L"0123456789", /* { dg-error "unable to read substring location: execution character set != source character set" } */
6, 4, 7);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range (L"0123456789",
^~~~~~~~~~~~~
{ dg-end-multiline-output "" } */
}
void
test_raw_string_one_liner (void)
{
/* Digits 0-9. */
__emit_string_literal_range (R"foo(0123456789)foo", /* { dg-warning "range" } */
6, 4, 7);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range (R"foo(0123456789)foo",
~~^~
{ dg-end-multiline-output "" } */
}
void
test_raw_string_multiline (void)
{
__emit_string_literal_range (R"foo(
hello
world
)foo",
6, 4, 7);
/* { dg-error "unable to read substring location: range endpoints are on different lines" "" { target *-*-* } .-5 } */
/* { dg-begin-multiline-output "" }
__emit_string_literal_range (R"foo(
^~~~~~
hello
~~~~~
world
~~~~~
)foo",
~~~~~
{ dg-end-multiline-output "" } */
}
void
test_macro (void)
{
#define START "01234" /* { dg-warning "range" } */
__emit_string_literal_range (START
"56789",
4, 3, 6);
/* { dg-begin-multiline-output "" }
#define START "01234"
~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
__emit_string_literal_range (START
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"56789",
~~~
{ dg-end-multiline-output "" } */
}
void
test_multitoken_macro (void)
{
#define RANGE ("0123456789") /* { dg-error "unable to read substring location: macro expansion" } */
__emit_string_literal_range (RANGE, 4, 3, 6);
/* { dg-begin-multiline-output "" }
#define RANGE ("0123456789")
^~~~~~~~~~~~~~
{ dg-end-multiline-output "" { target c } } */
/* { dg-begin-multiline-output "" }
#define RANGE ("0123456789")
~^~~~~~~~~~~~~
{ dg-end-multiline-output "" { target c++ } } */
/* { dg-begin-multiline-output "" }
__emit_string_literal_range (RANGE, 4, 3, 6);
^~~~~
{ dg-end-multiline-output "" } */
#undef RANGE
}
/* Verify that the location of the closing quote is used
for the location of the null terminating character. */
void
test_terminator_location (void)
{
__emit_string_literal_range ("0123456789", /* { dg-warning "range" } */
10, 10, 10);
/* { dg-begin-multiline-output "" }
__emit_string_literal_range ("0123456789",
^
{ dg-end-multiline-output "" } */
}
/* Verify that we fail gracefully when a string literal token is split
across multiple physical lines. */
void
test_backslash_continued_logical_lines (void)
{
__emit_string_literal_range ("\
01234\
56789", 6, 6, 7);
/* { dg-error "unable to read substring location: range endpoints are on different lines" "" { target *-*-* } .-3 } */
/* { dg-begin-multiline-output "" }
__emit_string_literal_range ("\
^~
01234\
~~~~~~
56789", 6, 6, 7);
~~~~~~
{ dg-end-multiline-output "" } */
}
/* Reproducer for PR 87652; this is whitespace-sensitive. */
#include "pr87562-a.h"
#include "pr87562-b.h"
void
pr87652 (const char *stem, int counter)
{
char label[100];
ASM_GENERATE_INTERNAL_LABEL (label, stem, counter);
/* This warning is actually in "pr87562-a.h". */
/* { dg-warning "39: range" "" { target *-*-* } 5 } */
/* { dg-begin-multiline-output "" }
__emit_string_literal_range ("*.%s%u", 2, 2, 3); \
^~
{ dg-end-multiline-output "" } */
}
/* Reproducer for PR 87721. */
# define OFFSET __builtin_strlen (__FILE__) + __builtin_strlen(":%5d: ")
# define DBG_ERROR(format, caret_idx, start_idx, end_idx) \
do { \
__emit_string_literal_range(__FILE__":%5d: " format, \
OFFSET + caret_idx, \
OFFSET + start_idx, \
OFFSET + end_idx); \
} while (0)
/* { dg-error "unable to read substring location: failed to get ordinary maps" "" { target c } 329 } */
/* { dg-error "unable to read substring location: macro expansion" "" { target c++ } 329 } */
/* { dg-begin-multiline-output "" }
__emit_string_literal_range(__FILE__":%5d: " format, \
^~~~~~~~
{ dg-end-multiline-output "" { target c } } */
/* { dg-begin-multiline-output "" }
__emit_string_literal_range(__FILE__":%5d: " format, \
^
{ dg-end-multiline-output "" { target c++ } } */
void pr87721 (void) {
DBG_ERROR("Bad password, expected [%s], got [%s].", 24, 24, 25); /* { dg-message "in expansion of macro 'DBG_ERROR'" } */
/* { dg-begin-multiline-output "" }
DBG_ERROR("Bad password, expected [%s], got [%s].", 24, 24, 25);
^~~~~~~~~
{ dg-end-multiline-output "" } */
}
|