From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 50272 invoked by alias); 28 Aug 2017 15:22:45 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 48316 invoked by uid 89); 28 Aug 2017 15:22:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=stray, hopes, emulate X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 28 Aug 2017 15:22:33 +0000 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0A36D4A71F for ; Mon, 28 Aug 2017 15:22:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 0A36D4A71F Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=law@redhat.com Received: from localhost.localdomain (ovpn-117-47.phx2.redhat.com [10.3.117.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id D70EC7FB5C; Mon, 28 Aug 2017 15:22:30 +0000 (UTC) Subject: Re: [PATCH] C/C++: add fix-it hints for various missing symbols To: David Malcolm , gcc-patches@gcc.gnu.org References: <1499107059-28855-1-git-send-email-dmalcolm@redhat.com> From: Jeff Law Message-ID: <8c7ccded-25bc-1ea4-6901-8edbc45a14ca@redhat.com> Date: Mon, 28 Aug 2017 16:11:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <1499107059-28855-1-git-send-email-dmalcolm@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-IsSubscribed: yes X-SW-Source: 2017-08/txt/msg01585.txt.bz2 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