From patchwork Tue Jul 25 15:38:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 793498 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-458941-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="jJUDWJkn"; 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 3xH2TX2yX6z9rxm for ; Wed, 26 Jul 2017 01:38:55 +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:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=KwRDUZrPcRDCpSk+uvcs+TSY+kngideOgrzudvOqDss5zSHK0Oz69 r5YrOUpVjyKxSKjb48OD4FMmbmpsZy/q3GyNxuXdL04hgELaMdmtpQqAaj8T3mqW 1pgKlEhrSau2wduxGLwc5erVThCOAriiJoT8KzvW+mzg8a7O9eJoUA= 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:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=CYkKVEqScATeGTfGRV4XKKjsGRg=; b=jJUDWJknP4arQWdkfwkX cJqOlHdy4uDzTEulzP+mGyMun/ddDKvjH7QIudZsqJIA8+QCusYTceBf4fkWU4Yy mq9B5uKP6LiyWB98C02IiGD9CrKcbIllQRfXx+aYskZHcmN5Z0ds97/WbnOILZ4A HLx5Lrjsg+VoZuwmlkBxhj0= Received: (qmail 125883 invoked by alias); 25 Jul 2017 15:38:46 -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 125857 invoked by uid 89); 25 Jul 2017 15:38:44 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No 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, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=complaints, systematic 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; Tue, 25 Jul 2017 15:38:42 +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 5AA57C0B7886; Tue, 25 Jul 2017 15:38:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5AA57C0B7886 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=polacek@redhat.com Received: from redhat.com (ovpn-204-38.brq.redhat.com [10.40.204.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 80BED8F375; Tue, 25 Jul 2017 15:38:40 +0000 (UTC) Date: Tue, 25 Jul 2017 17:38:31 +0200 From: Marek Polacek To: GCC Patches , Joseph Myers Subject: C PATCH to detect clashing attributes (PR c/81544) Message-ID: <20170725153831.GG3397@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.8.3 (2017-05-23) PR c/81544 complaints that we aren't detecting clashing noreturn / warn_unused_result attributes so this patch adds that checking. Martin plans to do more systematic checking in this area but meanwhile we might want to go with this. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2017-07-25 Marek Polacek PR c/81544 * c-common.h (diagnose_mismatched_attributes): Update decl. * c-warn.c (diagnose_mismatched_attributes): Add OLDTYPE and NEWTYPE parameters. Diagnose clashing noreturn / warn_unused_result attributes. * c-decl.c (diagnose_mismatched_decls): Pass OLDTYPE and NEWTYPE down to diagnose_mismatched_attributes. * decl.c (duplicate_decls): Pass the types of OLDDECL and NEWDECL down to diagnose_mismatched_attributes. * c-c++-common/attributes-4.c: New test. Marek diff --git gcc/c-family/c-common.h gcc/c-family/c-common.h index a29f1ade25d..7c4b2a0e108 100644 --- gcc/c-family/c-common.h +++ gcc/c-family/c-common.h @@ -1537,7 +1537,7 @@ extern void maybe_warn_unused_local_typedefs (void); extern void maybe_warn_bool_compare (location_t, enum tree_code, tree, tree); extern bool maybe_warn_shift_overflow (location_t, tree, tree); extern void warn_duplicated_cond_add_or_warn (location_t, tree, vec **); -extern bool diagnose_mismatched_attributes (tree, tree); +extern bool diagnose_mismatched_attributes (tree, tree, tree, tree); extern tree do_warn_duplicated_branches_r (tree *, int *, void *); extern void warn_for_multistatement_macros (location_t, location_t, location_t, enum rid); diff --git gcc/c-family/c-warn.c gcc/c-family/c-warn.c index a8b38c1b98d..d3c62a5ad9e 100644 --- gcc/c-family/c-warn.c +++ gcc/c-family/c-warn.c @@ -2125,7 +2125,8 @@ warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec **chain) attributes, such as always_inline vs. noinline. */ bool -diagnose_mismatched_attributes (tree olddecl, tree newdecl) +diagnose_mismatched_attributes (tree olddecl, tree newdecl, tree oldtype, + tree newtype) { bool warned = false; @@ -2172,6 +2173,17 @@ diagnose_mismatched_attributes (tree olddecl, tree newdecl) warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " "%qs follows declaration with attribute %qs", newdecl, "hot", "cold"); + else if (lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl)) + && lookup_attribute ("warn_unused_result", + TYPE_ATTRIBUTES (oldtype))) + warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " + "%qs follows declaration with attribute %qs", + newdecl, "noreturn", "warn_unused_result"); + else if (lookup_attribute ("warn_unused_result", TYPE_ATTRIBUTES (newtype)) + && lookup_attribute ("noreturn", DECL_ATTRIBUTES (olddecl))) + warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute " + "%qs follows declaration with attribute %qs", + newdecl, "warn_unused_result", "noreturn"); return warned; } diff --git gcc/c/c-decl.c gcc/c/c-decl.c index 12fbc18bb94..e995606499f 100644 --- gcc/c/c-decl.c +++ gcc/c/c-decl.c @@ -2234,7 +2234,8 @@ diagnose_mismatched_decls (tree newdecl, tree olddecl, } if (TREE_CODE (newdecl) == FUNCTION_DECL) - warned |= diagnose_mismatched_attributes (olddecl, newdecl); + warned |= diagnose_mismatched_attributes (olddecl, newdecl, oldtype, + newtype); else /* PARM_DECL, VAR_DECL */ { /* Redeclaration of a parameter is a constraint violation (this is diff --git gcc/cp/decl.c gcc/cp/decl.c index d98fab370d7..4a67e57c675 100644 --- gcc/cp/decl.c +++ gcc/cp/decl.c @@ -1397,7 +1397,9 @@ duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend) if (DECL_P (olddecl) && TREE_CODE (newdecl) == FUNCTION_DECL && TREE_CODE (olddecl) == FUNCTION_DECL - && diagnose_mismatched_attributes (olddecl, newdecl)) + && diagnose_mismatched_attributes (olddecl, newdecl, + TREE_TYPE (olddecl), + TREE_TYPE (newdecl))) { if (DECL_INITIAL (olddecl)) inform (DECL_SOURCE_LOCATION (olddecl), diff --git gcc/testsuite/c-c++-common/attributes-4.c gcc/testsuite/c-c++-common/attributes-4.c index e69de29bb2d..2f8b9676ecd 100644 --- gcc/testsuite/c-c++-common/attributes-4.c +++ gcc/testsuite/c-c++-common/attributes-4.c @@ -0,0 +1,8 @@ +/* PR c/81544 */ +/* { dg-do compile } */ + +int __attribute__ ((noreturn)) foo (void); /* { dg-message "previous declaration" } */ +int __attribute__ ((warn_unused_result)) foo (void); /* { dg-warning "attribute .warn_unused_result. follows declaration with attribute .noreturn." } */ + +int __attribute__ ((warn_unused_result)) foo2 (void); /* { dg-message "previous declaration" } */ +int __attribute__ ((noreturn)) foo2 (void); /* { dg-warning "attribute .noreturn. follows declaration with attribute .warn_unused_result." } */