From 65c5bbe1c92f9c08e99d3a37c136f2ef9804a37f Mon Sep 17 00:00:00 2001 From: Lewis Hyatt Date: Fri, 22 Mar 2024 12:55:27 -0400 Subject: diagnostics: libcpp: Improve locations for _Pragma lexing diagnostics [PR114423] libcpp is not currently set up to be able to generate valid locations for tokens lexed from a _Pragma string. Instead, after obtaining the tokens, it sets their locations all to the location of the _Pragma operator itself. This makes things like _Pragma("GCC diagnostic") work well enough, but if any diagnostics are issued during lexing, prior to resetting the token locations, those diagnostics get issued at the invalid locations. Fix that up by adding a new field pfile->diagnostic_override_loc that instructs libcpp to issue diagnostics at the alternate location. libcpp/ChangeLog: PR preprocessor/114423 * internal.h (struct cpp_reader): Add DIAGNOSTIC_OVERRIDE_LOC field. * directives.cc (destringize_and_run): Set the new field to the location of the _Pragma operator. * errors.cc (cpp_diagnostic_at): Support DIAGNOSTIC_OVERRIDE_LOC to temporarily issue diagnostics at a different location. (cpp_diagnostic_with_line): Likewise. gcc/testsuite/ChangeLog: PR preprocessor/114423 * c-c++-common/cpp/pragma-diagnostic-loc.c: New test. * c-c++-common/cpp/diagnostic-pragma-1.c: Adjust expected output. * g++.dg/pch/operator-1.C: Likewise. --- libcpp/errors.cc | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'libcpp/errors.cc') diff --git a/libcpp/errors.cc b/libcpp/errors.cc index ad45f61..b644c36 100644 --- a/libcpp/errors.cc +++ b/libcpp/errors.cc @@ -60,13 +60,15 @@ cpp_diagnostic_at (cpp_reader * pfile, enum cpp_diagnostic_level level, enum cpp_warning_reason reason, rich_location *richloc, const char *msgid, va_list *ap) { - bool ret; - if (!pfile->cb.diagnostic) abort (); - ret = pfile->cb.diagnostic (pfile, level, reason, richloc, _(msgid), ap); - - return ret; + if (pfile->diagnostic_override_loc && level != CPP_DL_NOTE) + { + rich_location rc2{pfile->line_table, pfile->diagnostic_override_loc}; + rc2.set_escape_on_output (richloc->escape_on_output_p ()); + return pfile->cb.diagnostic (pfile, level, reason, &rc2, _(msgid), ap); + } + return pfile->cb.diagnostic (pfile, level, reason, richloc, _(msgid), ap); } /* Print a diagnostic at the location of the previously lexed token. */ @@ -201,8 +203,14 @@ cpp_diagnostic_with_line (cpp_reader * pfile, enum cpp_diagnostic_level level, if (!pfile->cb.diagnostic) abort (); + /* Don't override note locations, which will likely make the note + more confusing. */ + const bool do_loc_override + = pfile->diagnostic_override_loc && level != CPP_DL_NOTE; + if (do_loc_override) + src_loc = pfile->diagnostic_override_loc; rich_location richloc (pfile->line_table, src_loc); - if (column) + if (column && !do_loc_override) richloc.override_column (column); ret = pfile->cb.diagnostic (pfile, level, reason, &richloc, _(msgid), ap); -- cgit v1.1