David Malcolm <dmalc...@redhat.com> writes: > On Mon, 2017-07-03 at 19:57 +0100, Richard Sandiford wrote: >> [Thanks for all your diagnostic work btw.] >> >> David Malcolm <dmalc...@redhat.com> writes: >> > clang can also print notes about matching opening symbols >> > e.g. the note here: >> > >> > missing-symbol-2.c:25:22: error: expected ']' >> > const char test [42; >> > ^ >> > missing-symbol-2.c:25:19: note: to match this '[' >> > const char test [42; >> > ^ >> > which, although somewhat redundant for this example, seems much >> > more >> > useful if there's non-trivial nesting of constructs, or more than a >> > few >> > lines separating the open/close symbols (e.g. showing a stray >> > "namespace {" >> > that the user forgot to close). >> > >> > I'd like to implement both of these ideas as followups, but in >> > the meantime, is the fix-it hint patch OK for trunk? >> > (successfully bootstrapped & regrtested on x86_64-pc-linux-gnu) >> >> Just wondering: how easy would it be to restrict the note to the >> kinds >> of cases you mention? TBH I think clang goes in for extra notes too >> much, and it's not always that case that an "expected 'foo'" message >> really is caused by a missing 'foo'. It'd be great if there was some >> way of making the notes a bit more discerning. :-) >> >> Or maybe do something like restrict the extra note to cases in which >> the >> opening character is on a different line and use an underlined range >> when the opening character is on the same line? >> >> Thanks, >> Richard > > Thanks. > > This patch implements a new method: > > bool gcc_rich_location::add_location_if_nearby (location_t); > > to make it easy for a diagnostic to compactly print secondary locations > for these kinds of cases, falling back to printing them via a note > otherwise. > > Usage example (adapted from the one in the header): > > gcc_rich_location richloc (primary_loc); > bool added secondary = richloc.add_location_if_nearby (secondary_loc); > error_at_rich_loc (&richloc, "missing %qs", "}"); > if (!added secondary) > inform (secondary_loc, "here's the associated %qs", "{"); > > When primary_loc and secondary_loc are on the same line this will print: > > test.c:1:39: error: missing '}' > struct same_line { double x; double y; ; > ~ ^ > > When they are on different lines, this will print: > > test.c:6:1: error: missing '}' > ; > ^ > test.c:3:1: note: here's the associated '{' > { > ^
Thanks, this looks great! Richard