On 07/03/2017 12:37 PM, David Malcolm wrote: > This patch improves our C/C++ frontends' handling of missing > symbols, by making c_parser_require and cp_parser_require use > "better" locations for the diagnostic, and insert fix-it hints, > under certain circumstances (see the comments in the patch for > full details). > > For example, for this code with a missing semicolon: > > $ cat test.c > int missing_semicolon (void) > { > return 42 > } > > trunk currently emits: > > test.c:4:1: error: expected ‘;’ before ‘}’ token > } > ^ > > This patch adds a fix-it hint for the missing semicolon, and puts > the error at the location of the missing semicolon, printing the > followup token as a secondary location: > > test.c:3:12: error: expected ‘;’ before ‘}’ token > return 42 > ^ > ; > } > ~ > > More examples can be seen in the test cases. > > For reference, clang prints the following: > > test.c:3:12: error: expected ';' after return statement > return 42 > ^ > ; > > i.e. describing what syntactic thing came before, which > I think is likely to be more meaningful to the user. > > 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) > > gcc/c-family/ChangeLog: > * c-common.c (c_parse_error): Add RICHLOC param, and use it rather > than implicitly using input_location. > (enum missing_token_insertion_kind): New enum. > (get_missing_token_insertion_kind): New function. > (maybe_suggest_missing_token_insertion): New function. > * c-common.h (c_parse_error): Add RICHLOC param. > (maybe_suggest_missing_token_insertion): New decl. > > gcc/c/ChangeLog: > * c-parser.c (struct c_parser): Add "previous_token_loc" field. > (c_parser_consume_token): Set parser->previous_token_loc. > (c_parser_error): Rename to... > (c_parser_error_richloc): ...this, making static, and adding > "richloc" parameter, passing it to the c_parse_error call, > rather than calling c_parser_set_source_position_from_token. > (c_parser_error): Reintroduce, reimplementing in terms of the > above. > (c_parser_require): Add "type_is_unique" param. Use > c_parser_error_richloc rather than c_parser_error, calling > maybe_suggest_missing_token_insertion. > (c_parser_parms_list_declarator): Override default value of new > "type_is_unique" param to c_parser_require. > (c_parser_asm_statement): Likewise. > * c-parser.h (c_parser_require): Add "type_is_unique" param, > defaulting to true. > > gcc/cp/ChangeLog: > * parser.c (cp_parser_error): Add rich_location to call to > c_parse_error. > (get_required_cpp_ttype): New function. > (cp_parser_required_error): Remove calls to cp_parser_error, > instead setting a non-NULL gmsgid, and handling it if set by > calling c_parse_error, potentially with a fix-it hint. > > gcc/testsuite/ChangeLog: > * c-c++-common/cilk-plus/AN/parser_errors.c: Update expected > output to reflect changes to reported locations of missing > symbols. > * c-c++-common/cilk-plus/AN/parser_errors2.c: Likewise. > * c-c++-common/cilk-plus/AN/parser_errors3.c: Likewise. > * c-c++-common/cilk-plus/AN/pr61191.c: Likewise. > * c-c++-common/gomp/pr63326.c: Likewise. > * c-c++-common/missing-symbol.c: New test case. > * g++.dg/cpp1y/digit-sep-neg.C: Update expected output to reflect > changes to reported locations of missing symbols. > * g++.dg/cpp1y/pr65202.C: Likewise. > * g++.dg/other/do1.C: Likewise. > * g++.dg/missing-symbol-2.C: New test case. > * g++.dg/parse/error11.C: Update expected output to reflect > changes to reported locations of missing symbols. > * g++.dg/parse/pragma2.C: Likewise. > * g++.dg/template/error11.C: Likewise. > * gcc.dg/missing-symbol-2.c: New test case. > * gcc.dg/missing-symbol-3.c: New test case. > * gcc.dg/noncompile/940112-1.c: Update expected output to reflect > changes to reported locations of missing symbols. > * gcc.dg/noncompile/971104-1.c: Likewise. > * obj-c++.dg/exceptions-6.mm: Likewise. > * obj-c++.dg/pr48187.mm: Likewise. > * objc.dg/exceptions-6.m: Likewise. AFAICT, this never got moved forward after the comments from Richard and Joseph.
> +} > + > +/* Given RICHLOC, a location for a diagnostic describing a missing token > + of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the > + insertion of the token. > + > + The location of the attemped fix-it hint depends on TOKEN_TYPE: s/attemped/attempted/ > + > + if (gmsgid) > + { > + /* Emulate rest of cp_parser_error. */ > + cp_token *token = cp_lexer_peek_token (parser->lexer); > + cp_lexer_set_source_position_from_token (token); > + > + rich_location richloc (line_table, input_location); So is it worth trying to factor the bits you want to emulate from cp_parser_error so that they're shared? Or just a comment in cp_parser_error in the hopes that if someone changes it in a meaningful way they'll know to come back here and potentially update this routine? In general though it looks really good. I think we just want to make a decision whether or not there's some way to avoid a long term maintenance headache noted immediately above. jeff Jeff