From patchwork Wed Jul 17 06:02:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 1133121 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-505173-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="cOjxHk/a"; dkim-atps=neutral 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 45pRWD0ZjCz9sLt for ; Wed, 17 Jul 2019 16:02:30 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=Dqx+FYHXXJG0WPXi 03QfBG0izhZfv5SnWU8RoH4AJW2kAp92J79ezmsfK/jcC/nxDb/atgr0rLBGB/uR ePU+89bxL75frxWl6IB8+esE5+Xpx86cw1SbgLGn+dUb2r8Nlpr31WVsOBHyinKa SYRnv5lFsOi/4q2eFR9gnNCOjps= 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:from :to:cc:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=QWn+nzJTnn91+YIv9N7vYI 9zgM8=; b=cOjxHk/aV43641AN1pucO/VEkf4x8i2xL6mWVdXo/ecFo1dwMFWqTu k1z6i5aiw3rYUoJSAl/mxFWOB69gOpjZ155FiJL5IVt8fRvj0Jr5hgvvWR58iWxw rNYB9xcEDiGbneXTgIv5A5W7gWekEKUJ/rhfTcetnuVbDN9Vf0ZLE= Received: (qmail 8512 invoked by alias); 17 Jul 2019 06:02:22 -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 8500 invoked by uid 89); 17 Jul 2019 06:02:22 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=free!, pero, tmpl, que X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 17 Jul 2019 06:02:20 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 9707456148; Wed, 17 Jul 2019 02:02:18 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id e4CqG7kXcuJU; Wed, 17 Jul 2019 02:02:18 -0400 (EDT) Received: from free.home (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by rock.gnat.com (Postfix) with ESMTPS id 2861556147; Wed, 17 Jul 2019 02:02:17 -0400 (EDT) Received: from livre (livre.home [172.31.160.2]) by free.home (8.15.2/8.15.2) with ESMTPS id x6H627u6309439 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 17 Jul 2019 03:02:07 -0300 From: Alexandre Oliva To: Martin Sebor Cc: gcc-patches@gcc.gnu.org Subject: -Wmissing-attributes: avoid duplicates and false positives Date: Wed, 17 Jul 2019 03:02:07 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 Hello, Martin, The initial patch for PR 81824 fixed one of the possibilities of -Wmissing-attributes reporting duplicates, namely, if TMPL had an attribute in ATTRLIST that was missing from DECL's decl and type attribute lists, both being non-empty. Another possibility of duplicate reporting remained: when an attribute in ATTRLIST is present in both decl and type attribute lists of TMPL, and absent from DECL's attribute lists, it is reported once for each of TMPL's lists. The implementation still allowed for false positives: an attribute in ATTRLIST that is present in TMPL will be regarded as missing as soon as it is not found in DECL's decl attribute list, even if it is later found in DECL's type attribute list. This patch fixes both problems, so that an attribute from ATTRLIST that is present in any of TMPL's lists will be reported at most once, and only if it is missing from both DECL's lists. Now, I realize there are some attributes that are only acceptable for types, and some that are only acceptable for function declarations. ISTM that some even have different meanings depending on whether they're associated with types or declarations. There's room for an argument for checking only corresponding lists, for at least some of the attributes. AFAICT it doesn't apply to -Wmissing-attributes, that are either acceptable in either list, or only in the FUNCTION_DECL list, so I'm leaving it at that in the hope that it doesn't apply to any other users of decls_mismatched_attributes either. Regstrapping on x86_64-linux-gnu. Ok to install if it passes? for gcc/ChangeLog PR middle-end/81824 * attribs.c (decls_mismatched_attributes): Avoid duplicates and false positives. for gcc/testsuite/ChangeLog PR middle-end/81824 * g++.dg/Wmissing-attributes-1.C: New. --- gcc/attribs.c | 14 +++++-- gcc/testsuite/g++.dg/Wmissing-attributes-1.C | 55 ++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/Wmissing-attributes-1.C diff --git a/gcc/attribs.c b/gcc/attribs.c index 8e5401655972..f4777c6a8233 100644 --- a/gcc/attribs.c +++ b/gcc/attribs.c @@ -1931,15 +1931,19 @@ decls_mismatched_attributes (tree tmpl, tree decl, tree attrlist, if (!has_attribute (tmpls[j], tmpl_attrs[j], blacklist[i])) continue; + bool found = false; unsigned kmax = 1 + !!decl_attrs[1]; for (unsigned k = 0; k != kmax; ++k) { if (has_attribute (decls[k], decl_attrs[k], blacklist[i])) - break; - - if (!k && kmax > 1) - continue; + { + found = true; + break; + } + } + if (!found) + { if (nattrs) pp_string (attrstr, ", "); pp_begin_quote (attrstr, pp_show_color (global_dc->printer)); @@ -1947,6 +1951,8 @@ decls_mismatched_attributes (tree tmpl, tree decl, tree attrlist, pp_end_quote (attrstr, pp_show_color (global_dc->printer)); ++nattrs; } + + break; } } diff --git a/gcc/testsuite/g++.dg/Wmissing-attributes-1.C b/gcc/testsuite/g++.dg/Wmissing-attributes-1.C new file mode 100644 index 000000000000..56d28b339e17 --- /dev/null +++ b/gcc/testsuite/g++.dg/Wmissing-attributes-1.C @@ -0,0 +1,55 @@ +// { dg-do compile } +// { dg-options "-Wmissing-attributes" } + +#define ATTR(list) __attribute__ (list) + +/* Type attributes are normally absent in template functions, and the + mere presence of any such attribute used to cause the + -Wmissing-attributes checks, that checked for attributes typically + associated with functions rather than types, to report any missing + attributes twice: once for the specialization attribute list, once + for its type attribute list. + + If we force any of the checked-for attributes to be associated with + types rather than functions, even later implementations that fixed + the duplicate reporting problem above would still report them as + missing (in the function attribute list) even when present (in the + type attribute list). */ +typedef void* ATTR ((alloc_size (1))) f_type (int); + +template +f_type +ATTR ((malloc)) +missing_malloc; // { dg-message "missing primary template attribute .malloc." } + +template <> +f_type +missing_malloc; // { dg-warning "explicit specialization .\[^\n\r\]+. may be missing attributes" } + + +/* Check that even an attribute that appears in both lists (decl and + type) in a template declaration is reported as missing only + once. */ + +template +f_type +ATTR ((alloc_size (1))) // In both attr lists, decl's and type's. +missing_alloc_size; // { dg-message "missing primary template attribute .alloc_size." } + +template <> +void * +missing_alloc_size(int); // { dg-warning "explicit specialization .\[^\n\r\]+. may be missing attributes" } + + +/* Check that even an attribute that appears in both lists (decl and + type) is not report as missing if it's present only in the type + list. */ + +template +f_type +ATTR ((alloc_size (1))) // In both attr lists, decl's and type's. +missing_nothing; + +template <> +f_type +missing_nothing;