1 /* { dg-do compile } */
2 /* { dg-options "-O -fdiagnostics-show-caret" } */
4 /* This is a collection of unittests for ranges within string literals,
5 using diagnostic_plugin_test_string_literals, which handles
6 "__emit_string_literal_range" by generating a warning at the given
7 subset of a string literal.
9 The indices are 0-based. It's easiest to verify things using string
10 literals that are runs of 0-based digits (to avoid having to count
13 LITERAL is a const void * to allow testing the various kinds of wide
14 string literal, rather than just const char *. */
16 extern void __emit_string_literal_range (const void *literal
, int caret_idx
,
17 int start_idx
, int end_idx
);
20 test_simple_string_literal (void)
22 __emit_string_literal_range ("0123456789", /* { dg-warning "range" } */
24 /* { dg-begin-multiline-output "" }
25 __emit_string_literal_range ("0123456789",
27 { dg-end-multiline-output "" } */
31 test_concatenated_string_literal (void)
33 __emit_string_literal_range ("01234" "56789", /* { dg-warning "range" } */
35 /* { dg-begin-multiline-output "" }
36 __emit_string_literal_range ("01234" "56789",
38 { dg-end-multiline-output "" } */
42 test_multiline_string_literal (void)
44 __emit_string_literal_range ("01234" /* { dg-warning "range" } */
47 /* { dg-begin-multiline-output "" }
48 __emit_string_literal_range ("01234"
49 ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52 { dg-end-multiline-output "" } */
53 /* FIXME: why does the above need three trailing spaces? */
56 /* Tests of various unicode encodings.
58 Digits 0 through 9 are unicode code points:
62 However, these are not always valid as UCN (see the comment in
63 libcpp/charset.c:_cpp_valid_ucn).
65 Hence we need to test UCN using an alternative unicode
66 representation of numbers; let's use Roman numerals,
67 (though these start at one, not zero):
68 U+2170 SMALL ROMAN NUMERAL ONE
70 U+2174 SMALL ROMAN NUMERAL FIVE ("v")
71 U+2175 SMALL ROMAN NUMERAL SIX ("vi")
73 U+2178 SMALL ROMAN NUMERAL NINE. */
78 /* Digits 0-9, expressing digit 5 in ASCII as "\x35"
79 and with a space in place of digit 6, to terminate the escaped
81 __emit_string_literal_range ("01234\x35 789", /* { dg-warning "range" } */
83 /* { dg-begin-multiline-output "" }
84 __emit_string_literal_range ("01234\x35 789"
86 { dg-end-multiline-output "" } */
92 /* Digits 0-9, expressing digit 5 in ASCII as "\065"
93 and with a space in place of digit 6, to terminate the escaped
95 __emit_string_literal_range ("01234\065 789", /* { dg-warning "range" } */
97 /* { dg-begin-multiline-output "" }
98 __emit_string_literal_range ("01234\065 789"
100 { dg-end-multiline-output "" } */
106 /* Digits 0-9, expressing digit 5 in ASCII as hex "\x35"
107 digit 6 in ASCII as octal "\066", concatenating multiple strings. */
108 __emit_string_literal_range ("01234" "\x35" "\066" "789", /* { dg-warning "range" } */
110 /* { dg-begin-multiline-output "" }
111 __emit_string_literal_range ("01234" "\x35" "\066" "789",
112 ~~~~~~^~~~~~~~~~~~~~~~~~
113 { dg-end-multiline-output "" } */
119 /* Digits 0-9, expressing digits 5 and 6 as Roman numerals expressed
121 The resulting string is encoded as UTF-8. Most of the digits are 1 byte
122 each, but digits 5 and 6 are encoded with 3 bytes each.
123 Hence to underline digits 4-7 we need to underling using bytes 4-11 in
124 the UTF-8 encoding. */
125 __emit_string_literal_range ("01234\u2174\u2175789", /* { dg-warning "range" } */
127 /* { dg-begin-multiline-output "" }
128 __emit_string_literal_range ("01234\u2174\u2175789",
130 { dg-end-multiline-output "" } */
136 /* Digits 0-9, expressing digits 5 and 6 as Roman numerals as UCN 8.
137 The resulting string is the same as as in test_ucn4 above, and hence
138 has the same UTF-8 encoding, and so we again need to underline bytes
139 4-11 in the UTF-8 encoding in order to underline digits 4-7. */
140 __emit_string_literal_range ("01234\U00002174\U00002175789", /* { dg-warning "range" } */
142 /* { dg-begin-multiline-output "" }
143 __emit_string_literal_range ("01234\U00002174\U00002175789",
144 ~^~~~~~~~~~~~~~~~~~~~~
145 { dg-end-multiline-output "" } */
152 __emit_string_literal_range (u8
"0123456789", /* { dg-warning "range" } */
154 /* { dg-begin-multiline-output "" }
155 __emit_string_literal_range (u8"0123456789",
157 { dg-end-multiline-output "" } */
164 __emit_string_literal_range (u
"0123456789", /* { dg-error "unable to read substring location: execution character set != source character set" } */
166 /* { dg-begin-multiline-output "" }
167 __emit_string_literal_range (u"0123456789",
169 { dg-end-multiline-output "" } */
176 __emit_string_literal_range (U
"0123456789", /* { dg-error "unable to read substring location: execution character set != source character set" } */
178 /* { dg-begin-multiline-output "" }
179 __emit_string_literal_range (U"0123456789",
181 { dg-end-multiline-output "" } */
188 __emit_string_literal_range (L
"0123456789", /* { dg-error "unable to read substring location: execution character set != source character set" } */
190 /* { dg-begin-multiline-output "" }
191 __emit_string_literal_range (L"0123456789",
193 { dg-end-multiline-output "" } */
197 test_raw_string_one_liner (void)
200 __emit_string_literal_range (R
"foo(0123456789)foo", /* { dg-warning "range" } */
202 /* { dg-begin-multiline-output "" }
203 __emit_string_literal_range (R"foo(0123456789)foo",
205 { dg-end-multiline-output "" } */
209 test_raw_string_multiline (void)
211 __emit_string_literal_range (R
"foo(
216 /* { dg-error "unable to read substring location: range endpoints are on different lines" "" { target *-*-* } .-5 } */
217 /* { dg-begin-multiline-output "" }
218 __emit_string_literal_range (R"foo(
226 { dg-end-multiline-output "" } */
232 #define START "01234" /* { dg-warning "range" } */
233 __emit_string_literal_range (START
236 /* { dg-begin-multiline-output "" }
237 #define START "01234"
238 ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239 __emit_string_literal_range (START
240 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243 { dg-end-multiline-output "" } */
247 test_multitoken_macro (void)
249 #define RANGE ("0123456789") /* { dg-error "unable to read substring location: macro expansion" } */
250 __emit_string_literal_range (RANGE
, 4, 3, 6);
251 /* { dg-begin-multiline-output "" }
252 #define RANGE ("0123456789")
254 { dg-end-multiline-output "" { target c } } */
255 /* { dg-begin-multiline-output "" }
256 #define RANGE ("0123456789")
258 { dg-end-multiline-output "" { target c++ } } */
259 /* { dg-begin-multiline-output "" }
260 __emit_string_literal_range (RANGE, 4, 3, 6);
262 { dg-end-multiline-output "" } */
266 /* Verify that the location of the closing quote is used
267 for the location of the null terminating character. */
270 test_terminator_location (void)
272 __emit_string_literal_range ("0123456789", /* { dg-warning "range" } */
274 /* { dg-begin-multiline-output "" }
275 __emit_string_literal_range ("0123456789",
277 { dg-end-multiline-output "" } */
280 /* Verify that we fail gracefully when a string literal token is split
281 across multiple physical lines. */
284 test_backslash_continued_logical_lines (void)
286 __emit_string_literal_range ("\
289 /* { dg-error "unable to read substring location: range endpoints are on different lines" "" { target *-*-* } .-3 } */
290 /* { dg-begin-multiline-output "" }
291 __emit_string_literal_range ("\
297 { dg-end-multiline-output "" } */
300 /* Reproducer for PR 87652; this is whitespace-sensitive. */
302 #include "pr87562-a.h"
307 #include "pr87562-b.h"
310 pr87652 (const char *stem
, int counter
)
313 ASM_GENERATE_INTERNAL_LABEL (label
, stem
, counter
);
315 /* This warning is actually in "pr87562-a.h". */
316 /* { dg-warning "39: range" "" { target *-*-* } 5 } */
317 /* { dg-begin-multiline-output "" }
318 __emit_string_literal_range ("*.%s%u", 2, 2, 3); \
320 { dg-end-multiline-output "" } */
323 /* Reproducer for PR 87721. */
325 # define OFFSET __builtin_strlen (__FILE__) + __builtin_strlen(":%5d: ")
327 # define DBG_ERROR(format, caret_idx, start_idx, end_idx) \
329 __emit_string_literal_range(__FILE__":%5d: " format, \
330 OFFSET + caret_idx, \
331 OFFSET + start_idx, \
335 /* { dg-error "unable to read substring location: failed to get ordinary maps" "" { target c } 329 } */
336 /* { dg-error "unable to read substring location: macro expansion" "" { target c++ } 329 } */
337 /* { dg-begin-multiline-output "" }
338 __emit_string_literal_range(__FILE__":%5d: " format, \
340 { dg-end-multiline-output "" { target c } } */
341 /* { dg-begin-multiline-output "" }
342 __emit_string_literal_range(__FILE__":%5d: " format, \
344 { dg-end-multiline-output "" { target c++ } } */
346 void pr87721 (void) {
347 DBG_ERROR("Bad password, expected [%s], got [%s].", 24, 24, 25); /* { dg-message "in expansion of macro 'DBG_ERROR'" } */
348 /* { dg-begin-multiline-output "" }
349 DBG_ERROR("Bad password, expected [%s], got [%s].", 24, 24, 25);
351 { dg-end-multiline-output "" } */