From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24606 invoked by alias); 21 Dec 2018 03:45: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 24189 invoked by uid 89); 21 Dec 2018 03:45:14 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.1 required=5.0 tests=BAYES_00,FREEMAIL_FROM,GIT_PATCH_2,GIT_PATCH_3,KAM_ASCII_DIVIDERS,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy=decorated, specifications, triggering X-HELO: mail-qk1-f169.google.com Received: from mail-qk1-f169.google.com (HELO mail-qk1-f169.google.com) (209.85.222.169) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 21 Dec 2018 03:45:08 +0000 Received: by mail-qk1-f169.google.com with SMTP id o89so2351585qko.0 for ; Thu, 20 Dec 2018 19:45:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:subject:to:message-id:date:user-agent:mime-version :content-language; bh=XK0tcTFqawVhhbw8wGX91mfTSeVDh1HgFHGrW/nGwgg=; b=H4AWQ4ihJf1Pz+wbqc7/TlIy/Kqsdoci9qlK7m3Uy4IdrOUInLmiRyVLK/WfBmKlEL bYgtQHyJZOHfk+uh3y64KqESceamyW3sCBbDUzYLcuKI+TqmV2tjTetzE/h8SES/TlpO IX+Fwx6w3VJzP8moZ0csX3XBBvQ6d0NFn3c+POm+Wm4TTBEhpG4aTpGoxmQrxwL32pUO PPLPyQmnW6pWSCVhV8f29bBgRUne83u4jvzsU/U13yIdCw9TAp8Ejc4QCfaDa8J/9qZT /B+JxWbh8j5ErLGVfHixaO7b6jSVdYKx+cahGUbjAfSjwStBuuUIAqC87ubxmfG3lHyg DKsg== Return-Path: Received: from [192.168.0.106] (97-118-99-160.hlrn.qwest.net. [97.118.99.160]) by smtp.gmail.com with ESMTPSA id u4sm3625024qkk.51.2018.12.20.19.45.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 20 Dec 2018 19:45:05 -0800 (PST) From: Martin Sebor Subject: [PATCH] attribute copy, leaf, weakref and -Wmisisng-attributes (PR 88546) To: gcc-patches@gcc.gnu.org Message-ID: <6ff6565b-51d8-8620-f345-a0082747297b@gmail.com> Date: Fri, 21 Dec 2018 06:01:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.1 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------6A6A9441B325B4C449C42643" X-IsSubscribed: yes X-SW-Source: 2018-12/txt/msg01530.txt.bz2 This is a multi-part message in MIME format. --------------6A6A9441B325B4C449C42643 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-length: 1233 The enhancement to detect mismatched attributes between function aliases and their targets triggers (expected) warnings in GCC builds due to aliases being declared with fewer attributes than their targets. Using attribute copy as recommended to copy the attributes from the target to the alias triggers another warning, this time due to applying attribute leaf to static functions (the attribute only applies to extern functions). This is due to an oversight in both the handler for attribute copy and in the -Wmissing-attributes warning. In addition, the copy attribute handler doesn't account for C11 _Noreturn and C++ throw() specifications, both of which set the corresponding tree bits but don't attach the synonymous attribute to it. This also leads to warnings in GCC builds (in libgfortran). The attached patch corrects all of these problems: the attribute copy handler to avoid copying attribute leaf to declarations of static functions, and to set the noreturn and nonthrow bits, and the missing attribute warning to avoid triggering for static weakref aliases whose targets are decorated wiwth attribute leaf. With this patch, GCC should build with no -Wmissing-attributes warnings. Tested on x86_64-linux. Martin --------------6A6A9441B325B4C449C42643 Content-Type: text/x-patch; name="gcc-88546.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="gcc-88546.diff" Content-length: 5443 PR c/88546 - Copy attribute unusable for weakrefs gcc/c-family/ChangeLog: PR c/88546 * c-attribs.c (handle_copy_attribute): Avoid copying attribute leaf. gcc/ChangeLog: PR c/88546 * attribs.c (decls_mismatched_attributes): Avoid warning for attribute leaf. libgcc/ChangeLog: PR c/88546 * gthr-posix.h (__gthrw2): Use attribute copy. libgfortran/ChangeLog: PR c/88546 * libgfortran.h (iexport2): Use attribute copy. gcc/testsuite/ChangeLog: PR c/88546 * gcc.dg/attr-copy-6.c: New test. Index: gcc/attribs.c =================================================================== --- gcc/attribs.c (revision 267282) +++ gcc/attribs.c (working copy) @@ -1912,6 +1912,12 @@ decls_mismatched_attributes (tree tmpl, tree decl, for (unsigned i = 0; blacklist[i]; ++i) { + /* Attribute leaf only applies to extern functions. Avoid mentioning + it when it's missing from a static declaration. */ + if (!TREE_PUBLIC (decl) + && !strcmp ("leaf", blacklist[i])) + continue; + for (unsigned j = 0; j != 2; ++j) { if (!has_attribute (tmpls[j], tmpl_attrs[j], blacklist[i])) Index: gcc/c-family/c-attribs.c =================================================================== --- gcc/c-family/c-attribs.c (revision 267282) +++ gcc/c-family/c-attribs.c (working copy) @@ -2455,6 +2455,12 @@ handle_copy_attribute (tree *node, tree name, tree || is_attribute_p ("weakref", atname)) continue; + /* Aattribute leaf only applies to extern functions. + Avoid copying it to static ones. */ + if (!TREE_PUBLIC (decl) + && is_attribute_p ("leaf", atname)) + continue; + tree atargs = TREE_VALUE (at); /* Create a copy of just the one attribute ar AT, including its argumentsm and add it to DECL. */ Index: gcc/testsuite/gcc.dg/attr-copy-6.c =================================================================== --- gcc/testsuite/gcc.dg/attr-copy-6.c (nonexistent) +++ gcc/testsuite/gcc.dg/attr-copy-6.c (working copy) @@ -0,0 +1,68 @@ +/* PR middle-end/88546 - Copy attribute unusable for weakrefs + { dg-do compile } + { dg-options "-O2 -Wall" } + { dg-require-weak "" } */ + +#define ATTR(...) __attribute__ ((__VA_ARGS__)) +#define ASRT(expr) _Static_assert (expr, #expr) + +/* Variable that is local to this translation unit but that can + be modified from other units by calling reset_unit_local(). */ +static int unit_local; + +void reset_unit_local (void) +{ + unit_local = 0; +} + +/* Attribute leaf implies that fleaf() doesn't modify unit_local(). */ +ATTR (leaf, returns_nonnull) +void* fleaf_retnz (void); + +/* Verify both attributes have been applied. */ +ASRT (__builtin_has_attribute (fleaf_retnz, leaf)); +ASRT (__builtin_has_attribute (fleaf_retnz, returns_nonnull)); + +/* Verify that attribute leaf has the expected effect. */ +void call_fleaf_retnz (void) +{ + int i = unit_local; + void *p = fleaf_retnz (); + + /* Expect both tests to be folded to false and the calls eliminated. */ + extern void call_fleaf_retnz_test_leaf_eliminated (void); + if (i != unit_local) + call_fleaf_retnz_test_leaf_eliminated (); + + extern void call_fleaf_retnz_test_nonnull_eliminated (void); + if (p == 0) + call_fleaf_retnz_test_nonnull_eliminated (); +} + + +/* Verify that attribute copy copies the returns_nonnull attribute + but doesn't try to copy attribute leaf which only applies to extern + function. */ +static ATTR (copy (fleaf_retnz), weakref ("fleaf_retnz")) +void* fweakref_fleaf_retnz_copy (void); + +ASRT (!__builtin_has_attribute (fweakref_fleaf_retnz_copy, leaf)); +ASRT (__builtin_has_attribute (fweakref_fleaf_retnz_copy, returns_nonnull)); + +void call_fweakref_fleaf_retnz_copy (void) +{ + int i = unit_local; + void *p = fweakref_fleaf_retnz_copy (); + + /* Since leaf is not copied, expect the following test not to be + folded and the call to be emitted. */ + extern void call_fweakref_test_leaf_emitted (void); + if (i != unit_local) + call_fweakref_test_leaf_emitted (); + + /* Expect the following test to be folded to false and the call + eliminated. */ + extern void call_fweakref_fleaf_nonnull_eliminated (void); + if (p == 0) + call_fweakref_fleaf_nonnull_eliminated (); +} Index: libgcc/gthr-posix.h =================================================================== --- libgcc/gthr-posix.h (revision 267282) +++ libgcc/gthr-posix.h (working copy) @@ -87,7 +87,8 @@ typedef struct timespec __gthread_time_t; # define __gthrw_pragma(pragma) # endif # define __gthrw2(name,name2,type) \ - static __typeof(type) name __attribute__ ((__weakref__(#name2))); \ + static __typeof(type) name \ + __attribute__ ((__weakref__(#name2), copy (type))); \ __gthrw_pragma(weak type) # define __gthrw_(name) __gthrw_ ## name #else Index: libgfortran/libgfortran.h =================================================================== --- libgfortran/libgfortran.h (revision 267282) +++ libgfortran/libgfortran.h (working copy) @@ -202,7 +202,7 @@ extern int __mingw_snprintf (char *, size_t, const # define iexport(x) iexport1(x, IPREFIX(x)) # define iexport1(x,y) iexport2(x,y) # define iexport2(x,y) \ - extern __typeof(x) PREFIX(x) __attribute__((__alias__(#y))) + extern __typeof(x) PREFIX(x) __attribute__((__alias__(#y), __copy__ (x))) #else # define export_proto(x) sym_rename(x, PREFIX(x)) # define export_proto_np(x) extern char swallow_semicolon --------------6A6A9441B325B4C449C42643--