From patchwork Fri Apr 22 13:26:47 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Schmidt X-Patchwork-Id: 613605 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qrxHd6nq8z9s9N for ; Fri, 22 Apr 2016 23:27:25 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=RPVRIjtF; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to:cc :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=W9sBC7Bx8Hs1P79UfCzMZCaBsYAzeKyRtjxe/jemmL7weuzpAB /oLlkU+f3MwbAiDB/0Wmx0YL66GEu5q1WizXvyPqP1P2wuI5KStOtzOD5/x8Ni3+ ggDs8a6AdI3TRLIaXtBK2xrEy8Gw9ZKMsNHvJGXipwPprHYg12KQR4qz0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to:cc :from:subject:message-id:date:mime-version:content-type; s= default; bh=IONwcG56IvnIXFj0OvsvMSZKBDM=; b=RPVRIjtFiVjAz+RlTt1f l1tn+i/0ryIJVhlbIhpMoQsBHx3dzrF8FQEXMaQg1ratBLWlOEiCuX2VcQzUWsvs tpgjxAyJ5HCeBrgzxu8EVKkX1SA9BNlGpftqZ9+GvHIZAyybRhXFOhtOwYH0DR2+ f1seT5q/fEDT5r0BvhKCFKo= Received: (qmail 58384 invoked by alias); 22 Apr 2016 13:27:17 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 57675 invoked by uid 89); 22 Apr 2016 13:27:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.1 required=5.0 tests=BAYES_00, KAM_ASCII_DIVIDERS, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_NONE, RP_MATCHES_RCVD autolearn=no version=3.3.2 spammy=H*MI:online, bernd, H*M:online, H*RU:194.25.134.21 X-HELO: mailout10.t-online.de Received: from mailout10.t-online.de (HELO mailout10.t-online.de) (194.25.134.21) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Fri, 22 Apr 2016 13:27:14 +0000 Received: from fwd19.aul.t-online.de (fwd19.aul.t-online.de [172.20.27.65]) by mailout10.t-online.de (Postfix) with SMTP id 51B5240FAFB; Fri, 22 Apr 2016 15:27:10 +0200 (CEST) Received: from sweetums.local (Ze5jhUZb8hhFX+8E4rNf7+SojRGAAopv3qC4KSN6Ucw3eLS0ccI+hiYHT7q1hFgQqM@[87.168.121.82]) by fwd19.t-online.de with (TLSv1.2:ECDHE-RSA-AES256-SHA encrypted) esmtp id 1atb6u-1vSeJs0; Fri, 22 Apr 2016 15:27:04 +0200 To: GCC Patches Cc: Bernd Schmidt , "Joseph S. Myers" , Jason Merrill From: Bernd Schmidt Subject: C, C++: Fix PR 69733 (bad location for ignored qualifiers warning) Message-ID: <571A2697.4050208@t-online.de> Date: Fri, 22 Apr 2016 15:26:47 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 MIME-Version: 1.0 X-IsSubscribed: yes The PR is for a C++ form of the form const double val() const { ... } where the warning location is at the second const (by accident, in reality it's just past the function's declarator), while the first const is the one that we are warning about. This patch adds some logic to the C and C++ frontends to look for the qualifier, or a typedef name, and point the warning there. C needs a little more work because the ignored qualifier could also be an address space. Bootstrapped and tested on x86_64-linux (a while ago, will retest). Ok for trunk? Bernd c/ PR c++/69733 * c-decl.c (smallest_type_quals_location): New static function. (grokdeclarator): Try to find the correct location for an ignored qualifier. cp/ PR c++/69733 * decl.c (grokdeclarator): Try to find the correct location for an ignored qualifier. testsuite/ PR c++/69733 * c-c++-common/pr69733.c: New test. * gcc.target/i386/pr69733.c: New test. Index: gcc/c/c-decl.c =================================================================== --- gcc/c/c-decl.c (revision 234183) +++ gcc/c/c-decl.c (working copy) @@ -5291,6 +5291,27 @@ warn_defaults_to (location_t location, i va_end (ap); } +/* Returns the smallest location != UNKNOWN_LOCATION in LOCATIONS, + considering only those c_declspec_words found in LIST, which + must be terminated by cdw_number_of_elements. */ + +static location_t +smallest_type_quals_location (const location_t* locations, + c_declspec_word *list) +{ + location_t loc = UNKNOWN_LOCATION; + while (*list != cdw_number_of_elements) + { + location_t newloc = locations[*list]; + if (loc == UNKNOWN_LOCATION + || (newloc != UNKNOWN_LOCATION && newloc < loc)) + loc = newloc; + list++; + } + + return loc; +} + /* Given declspecs and a declarator, determine the name and type of the object declared and construct a ..._DECL node for it. @@ -6101,6 +6122,18 @@ grokdeclarator (const struct c_declarato qualify the return type, not the function type. */ if (type_quals) { + enum c_declspec_word ignored_quals_list[] = + { + cdw_const, cdw_volatile, cdw_restrict, cdw_address_space, + cdw_number_of_elements + }; + location_t specs_loc + = smallest_type_quals_location (declspecs->locations, + ignored_quals_list); + if (specs_loc == UNKNOWN_LOCATION) + specs_loc = declspecs->locations[cdw_typedef]; + if (specs_loc == UNKNOWN_LOCATION) + specs_loc = loc; /* Type qualifiers on a function return type are normally permitted by the standard but have no effect, so give a warning at -Wreturn-type. @@ -6108,10 +6141,10 @@ grokdeclarator (const struct c_declarato function definitions in ISO C; GCC used to used them for noreturn functions. */ if (VOID_TYPE_P (type) && really_funcdef) - pedwarn (loc, 0, + pedwarn (specs_loc, 0, "function definition has qualified void return type"); else - warning_at (loc, OPT_Wignored_qualifiers, + warning_at (specs_loc, OPT_Wignored_qualifiers, "type qualifiers ignored on function return type"); type = c_build_qualified_type (type, type_quals); Index: gcc/cp/decl.c =================================================================== --- gcc/cp/decl.c (revision 234183) +++ gcc/cp/decl.c (working copy) @@ -10010,8 +10010,15 @@ grokdeclarator (const cp_declarator *dec if (type_quals != TYPE_UNQUALIFIED) { if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)) - warning (OPT_Wignored_qualifiers, - "type qualifiers ignored on function return type"); + { + location_t loc; + loc = smallest_type_quals_location (type_quals, + declspecs->locations); + if (loc == UNKNOWN_LOCATION) + loc = declspecs->locations[ds_type_spec]; + warning_at (loc, OPT_Wignored_qualifiers, "type " + "qualifiers ignored on function return type"); + } /* We now know that the TYPE_QUALS don't apply to the decl, but to its return type. */ type_quals = TYPE_UNQUALIFIED; Index: gcc/testsuite/c-c++-common/pr69733.c =================================================================== --- gcc/testsuite/c-c++-common/pr69733.c (revision 0) +++ gcc/testsuite/c-c++-common/pr69733.c (working copy) @@ -0,0 +1,24 @@ +/* { dg-do compile } */ +/* { dg-options "-W -fdiagnostics-show-caret" } */ + +typedef const double cd; +double val; + +const double val0() {return val;} /* { dg-warning "qualifiers ignored" } */ +/* { dg-begin-multiline-output "" } + const double val0() {return val;} + ^~~~~ +{ dg-end-multiline-output "" } */ + +volatile double val1() {return val;} /* { dg-warning "qualifiers ignored" } */ +/* { dg-begin-multiline-output "" } + volatile double val1() {return val;} + ^~~~~~~~ +{ dg-end-multiline-output "" } */ + +cd val2() {return val;} /* { dg-warning "qualifiers ignored" } */ +/* { dg-begin-multiline-output "" } + cd val2() {return val;} + ^~ +{ dg-end-multiline-output "" } */ + Index: gcc/testsuite/gcc.target/i386/pr69733.c =================================================================== --- gcc/testsuite/gcc.target/i386/pr69733.c (revision 0) +++ gcc/testsuite/gcc.target/i386/pr69733.c (working copy) @@ -0,0 +1,30 @@ +/* { dg-do compile } */ +/* { dg-options "-W -fdiagnostics-show-caret" } */ + +typedef const double cd; +double val; + +const double val0() {return val;} /* { dg-warning "qualifiers ignored" } */ +/* { dg-begin-multiline-output "" } + const double val0() {return val;} + ^~~~~ +{ dg-end-multiline-output "" } */ + +volatile double val1() {return val;} /* { dg-warning "qualifiers ignored" } */ +/* { dg-begin-multiline-output "" } + volatile double val1() {return val;} + ^~~~~~~~ +{ dg-end-multiline-output "" } */ + +cd val2() {return val;} /* { dg-warning "qualifiers ignored" } */ +/* { dg-begin-multiline-output "" } + cd val2() {return val;} + ^~ +{ dg-end-multiline-output "" } */ + +__seg_fs int val3() {return val;} /* { dg-warning "qualifiers ignored" } */ +/* { dg-begin-multiline-output "" } + __seg_fs int val3() {return val;} + ^~~~~~~~ +{ dg-end-multiline-output "" } */ +