On Fri, 2024-10-18 at 09:25 -0400, Lewis Hyatt wrote: > Hello- > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114423 > > The diagnostics we issue while lexing tokens from a _Pragma string > have > always come out at invalid locations. I had tried a couple years ago > to > fix this in a general way, but I think that ended up being too > invasive a > change to fix a problem that's pretty minor in practice, and it never > got > over the finish line. Here is a simple patch that improves the > situation > and addresses the recently filed PR on that topic, hopefully this > incremental improvement is a better way to make some progress on > this? > > It just adds a way for libcpp to override the location for all > diagnostics > temporarily, so that the diagnostics issued while lexing from a > _Pragma > string are issued at a real location (the location of the _Pragma > token) > and not a bogus one. That's a lot simpler than trying to arrange to > produce valid locations when lexing tokens from an internal > buffer. Bootstrap + regtest all languages on x86-64 Linux, tweaked a > few > existing tests to adjust to the new locations. OK for trunk? Thanks!
[...snip...] > diff --git a/libcpp/errors.cc b/libcpp/errors.cc > index ad45f61913c..96fc165c12a 100644 > --- a/libcpp/errors.cc > +++ b/libcpp/errors.cc > @@ -60,13 +60,14 @@ 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}; > + return pfile->cb.diagnostic (pfile, level, reason, &rc2, > _(msgid), ap); > + } This will effectively override the primary location in the rich_location, but by using a second rich_location instance it will also ignore any secondary locations and fix-it hints. This might will be what we want here, but did you consider richloc.set_range (0, pfile->diagnostic_override_loc, SHOW_RANGE_WITH_CARET); to reset the primary location? Otherwise, looks good to me. [...snip...] Thanks Dave