@@ -1322,6 +1322,15 @@ get_substring_ranges_for_loc (cpp_reader *pfile,
if (strloc == UNKNOWN_LOCATION)
return "unknown location";
+ /* Reparsing the strings requires accurate location information.
+ If -ftrack-macro-expansion has been overridden from its default
+ of 2, then we might have a location of a macro expansion point,
+ rather than the location of the literal itself.
+ Avoid this by requiring that we have full macro expansion tracking
+ for substring locations to be available. */
+ if (cpp_get_options (pfile)->track_macro_expansion != 2)
+ return "track_macro_expansion != 2";
+
/* If string concatenation has occurred at STRLOC, get the locations
of all of the literal tokens making up the compound string.
Otherwise, just use STRLOC. */
@@ -243,6 +243,22 @@ test_macro (void)
{ 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 "" } */
+/* { 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. */
new file mode 100644
@@ -0,0 +1,43 @@
+/* Similar to diagnostic-test-string-literals-1.c, but with
+ -ftrack-macro-expansion=0. */
+
+/* { dg-do compile } */
+/* { dg-options "-O -ftrack-macro-expansion=0" } */
+
+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-error "unable to read substring location: track_macro_expansion != 2" } */
+ 6, 6, 7);
+}
+
+void
+test_concatenated_string_literal (void)
+{
+ __emit_string_literal_range ("01234" "56789", /* { dg-error "unable to read substring location: track_macro_expansion != 2" } */
+ 4, 3, 6);
+}
+
+/* To reproduce PR preprocessor/78324, the macro name should start
+ with the letter 'R'. */
+
+void
+test_macro (void)
+{
+#define RANGE "01234"
+ __emit_string_literal_range (RANGE /* { dg-error "unable to read substring location: track_macro_expansion != 2" } */
+ "56789",
+ 4, 3, 6);
+#undef RANGE
+}
+
+void
+test_multitoken_macro (void)
+{
+#define RANGE ("0123456789")
+ __emit_string_literal_range (RANGE, 4, 3, 6); /* { dg-error "unable to read substring location: track_macro_expansion != 2" } */
+#undef RANGE
+}
new file mode 100644
@@ -0,0 +1,43 @@
+/* Similar to diagnostic-test-string-literals-1.c, but with
+ -ftrack-macro-expansion=1. */
+
+/* { dg-do compile } */
+/* { dg-options "-O -ftrack-macro-expansion=1" } */
+
+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-error "unable to read substring location: track_macro_expansion != 2" } */
+ 6, 6, 7);
+}
+
+void
+test_concatenated_string_literal (void)
+{
+ __emit_string_literal_range ("01234" "56789", /* { dg-error "unable to read substring location: track_macro_expansion != 2" } */
+ 4, 3, 6);
+}
+
+/* To reproduce PR preprocessor/78324, the macro name should start
+ with the letter 'R'. */
+
+void
+test_macro (void)
+{
+#define RANGE "01234" /* { dg-error "unable to read substring location: track_macro_expansion != 2" } */
+ __emit_string_literal_range (RANGE
+ "56789",
+ 4, 3, 6);
+#undef RANGE
+}
+
+void
+test_multitoken_macro (void)
+{
+#define RANGE ("0123456789") /* { dg-error "unable to read substring location: track_macro_expansion != 2" } */
+ __emit_string_literal_range (RANGE, 4, 3, 6);
+#undef RANGE
+}
@@ -73,7 +73,9 @@ set plugin_test_list [list \
diagnostic-test-show-trees-1.c } \
{ diagnostic_plugin_test_string_literals.c \
diagnostic-test-string-literals-1.c \
- diagnostic-test-string-literals-2.c } \
+ diagnostic-test-string-literals-2.c \
+ diagnostic-test-string-literals-3.c \
+ diagnostic-test-string-literals-4.c } \
{ location_overflow_plugin.c \
location-overflow-test-1.c \
location-overflow-test-2.c } \
@@ -181,13 +181,13 @@ void test_sprintf_note (void)
/* Diagnostic column numbers are 1-based. */
P (buffer (0), /* { dg-message "format output 4 bytes into a destination of size 0" } */
- "%c%s%i", '1', "2", 3); /* { dg-warning "7:.%c. directive writing 1 byte into a region of size 0" } */
+ "%c%s%i", '1', "2", 3); /* { dg-warning ".%c. directive writing 1 byte into a region of size 0" } */
P (buffer (1), /* { dg-message "format output 6 bytes into a destination of size 1" } */
- "%c%s%i", '1', "23", 45); /* { dg-warning "9:.%s. directive writing 2 bytes into a region of size 0" } */
+ "%c%s%i", '1', "23", 45); /* { dg-warning ".%s. directive writing 2 bytes into a region of size 0" } */
P (buffer (2), /* { dg-message "format output 6 bytes into a destination of size 2" } */
- "%c%s%i", '1', "2", 345); /* { dg-warning "11:.%i. directive writing 3 bytes into a region of size 0" } */
+ "%c%s%i", '1', "2", 345); /* { dg-warning ".%i. directive writing 3 bytes into a region of size 0" } */
/* It would be nice if the caret in the location range for the format
string below could be made to point at the closing quote of the format
@@ -1,5 +1,5 @@
/* { dg-do compile } */
-/* { dg-options "-Wformat -Wformat-length=1 -fdiagnostics-show-caret -ftrack-macro-expansion=0" } */
+/* { dg-options "-Wformat -Wformat-length=1 -fdiagnostics-show-caret" } */
extern int sprintf (char*, const char*, ...);