From patchwork Mon Apr 14 17:32:32 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 339023 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 6CC95140087 for ; Tue, 15 Apr 2014 03:32:49 +1000 (EST) 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:cc:subject:message-id:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=CRhd8cXM6I252G0Il 5QsnE5l0caRjBmTcML1TMNAvrrGUaNTvkyQdqK5F45KPjuBQZzwaUtvMohqDbhCY viiloL1T6ihaV2MXqWnKJL5kqsBGPbZfnMrs8hrXHlsJgI0R3QirayjWIKDuLaHG cqDzudMmxt43Te2tnZvfoJlDXg= 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:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=default; bh=lyS70484/DS+7ZcrYbWEbqg kXmE=; b=cP/khkAZwbzsBs4A1lABP0zHkUhRQ0jCflSgbmOjYtlpKL1dxL40h9/ KInNKR8YY0SO4nJK0NWI69WppZa7JI4snmS38JQQMSV7/JTi/Ch8/vMFF8D72IX1 YCIw5MLPtHjzj7Kt8mWxfWZDJDSuxC1siDmI/D44eaMw2EmHU2gI= Received: (qmail 32670 invoked by alias); 14 Apr 2014 17:32:42 -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 32651 invoked by uid 89); 14 Apr 2014 17:32:41 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.3 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 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, 14 Apr 2014 17:32:39 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s3EHWa6b014307 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 14 Apr 2014 13:32:37 -0400 Received: from redhat.com (ovpn-116-79.ams2.redhat.com [10.36.116.79]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s3EHWX9x001733 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=NO); Mon, 14 Apr 2014 13:32:35 -0400 Date: Mon, 14 Apr 2014 19:32:32 +0200 From: Marek Polacek To: Marc Glisse Cc: GCC Patches , Jason Merrill , "Joseph S. Myers" Subject: Re: [C PATCH] Make attributes accept enum values (PR c/50459) Message-ID: <20140414173232.GB20332@redhat.com> References: <20140414151045.GA20332@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) On Mon, Apr 14, 2014 at 05:26:03PM +0200, Marc Glisse wrote: > If val is NULL, it seems you still end up looking at its TREE_CODE? > (don't know if that can happen) I didn't hit that while testing, but it looks weird, so fixed. > In C++, default_conversion probably handles IDENTIFIER_NODE just > fine, but having different code for the 2 languages would not be > nice, right? Right. Incidentally, C's default_conversion can handle CONST_DECLs too, so I tweaked get_attrib_value routine a bit. Is it any better? Tested x86_64. 2014-04-14 Marek Polacek PR c/50459 c-family/ * c-common.c (get_attrib_value): New function. (get_priority): Call get_attrib_value. (check_user_alignment): Likewise. (handle_alloc_size_attribute): Likewise. (handle_alloc_align_attribute): Likewise. (handle_assume_aligned_attribute): Likewise. (handle_vector_size_attribute): Likewise. (handle_sentinel_attribute): Likewise. testsuite/ * c-c++-common/pr50459.c: New test. * g++.dg/ext/pr50459.C: New test. Marek diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c index 1d56bc0..d627462 100644 --- gcc/c-family/c-common.c +++ gcc/c-family/c-common.c @@ -6448,6 +6448,28 @@ attribute_takes_identifier_p (const_tree attr_id) return targetm.attribute_takes_identifier_p (attr_id); } +/* Extract the integer constant from an enum value and/or perform default + promotions. This function is used for getting a value from an attribute + argument. */ + +static tree +get_attrib_value (tree val) +{ + if (val) + { + if (TREE_CODE (val) != IDENTIFIER_NODE + && TREE_CODE (val) != FUNCTION_DECL) + val = default_conversion (val); + else if (TREE_CODE (val) == IDENTIFIER_NODE) + { + tree t = lookup_name (val); + if (t && TREE_CODE (t) == CONST_DECL) + val = default_conversion (t); + } + } + return val; +} + /* Attribute handlers common to C front ends. */ /* Handle a "packed" attribute; arguments as in @@ -7030,11 +7052,10 @@ get_priority (tree args, bool is_destructor) } arg = TREE_VALUE (args); - if (TREE_CODE (arg) == IDENTIFIER_NODE) - goto invalid; if (arg == error_mark_node) return DEFAULT_INIT_PRIORITY; - arg = default_conversion (arg); + arg = get_attrib_value (arg); + if (!tree_fits_shwi_p (arg) || !INTEGRAL_TYPE_P (TREE_TYPE (arg))) goto invalid; @@ -7418,6 +7439,8 @@ check_user_alignment (const_tree align, bool allow_zero) { int i; + align = get_attrib_value (CONST_CAST_TREE (align)); + if (TREE_CODE (align) != INTEGER_CST || !INTEGRAL_TYPE_P (TREE_TYPE (align))) { @@ -8037,11 +8060,7 @@ handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args, unsigned arg_count = type_num_arguments (*node); for (; args; args = TREE_CHAIN (args)) { - tree position = TREE_VALUE (args); - if (position && TREE_CODE (position) != IDENTIFIER_NODE - && TREE_CODE (position) != FUNCTION_DECL) - position = default_conversion (position); - + tree position = get_attrib_value (TREE_VALUE (args)); if (!tree_fits_uhwi_p (position) || !arg_count || !IN_RANGE (tree_to_uhwi (position), 1, arg_count)) @@ -8063,10 +8082,7 @@ handle_alloc_align_attribute (tree *node, tree, tree args, int, bool *no_add_attrs) { unsigned arg_count = type_num_arguments (*node); - tree position = TREE_VALUE (args); - if (position && TREE_CODE (position) != IDENTIFIER_NODE) - position = default_conversion (position); - + tree position = get_attrib_value (TREE_VALUE (args)); if (!tree_fits_uhwi_p (position) || !arg_count || !IN_RANGE (tree_to_uhwi (position), 1, arg_count)) @@ -8088,11 +8104,7 @@ handle_assume_aligned_attribute (tree *, tree, tree args, int, { for (; args; args = TREE_CHAIN (args)) { - tree position = TREE_VALUE (args); - if (position && TREE_CODE (position) != IDENTIFIER_NODE - && TREE_CODE (position) != FUNCTION_DECL) - position = default_conversion (position); - + tree position = get_attrib_value (TREE_VALUE (args)); if (TREE_CODE (position) != INTEGER_CST) { warning (OPT_Wattributes, @@ -8532,10 +8544,7 @@ handle_vector_size_attribute (tree *node, tree name, tree args, *no_add_attrs = true; - size = TREE_VALUE (args); - if (size && TREE_CODE (size) != IDENTIFIER_NODE) - size = default_conversion (size); - + size = get_attrib_value (TREE_VALUE (args)); if (!tree_fits_uhwi_p (size)) { warning (OPT_Wattributes, "%qE attribute ignored", name); @@ -8943,8 +8952,7 @@ handle_sentinel_attribute (tree *node, tree name, tree args, if (args) { - tree position = TREE_VALUE (args); - + tree position = get_attrib_value (TREE_VALUE (args)); if (TREE_CODE (position) != INTEGER_CST) { warning (OPT_Wattributes, diff --git gcc/testsuite/c-c++-common/pr50459.c gcc/testsuite/c-c++-common/pr50459.c index e69de29..f837b63 100644 --- gcc/testsuite/c-c++-common/pr50459.c +++ gcc/testsuite/c-c++-common/pr50459.c @@ -0,0 +1,14 @@ +/* PR c/50459 */ +/* { dg-do compile } */ +/* { dg-options "-Wall -Wextra" } */ + +enum { A = 128, B = 1 }; +void *fn1 (void) __attribute__((assume_aligned (A))); +void *fn2 (void) __attribute__((assume_aligned (A, 4))); +void fn3 (void) __attribute__((constructor (A))); +void fn4 (void) __attribute__((destructor (A))); +void *fn5 (int) __attribute__((alloc_size (B))); +void *fn6 (int) __attribute__((alloc_align (B))); +void fn7 (const char *, ...) __attribute__ ((sentinel (B))); +int __attribute__((vector_size (A))) a; +int __attribute__((aligned (A))) foo; diff --git gcc/testsuite/g++.dg/ext/pr50459.C gcc/testsuite/g++.dg/ext/pr50459.C index e69de29..8acc299 100644 --- gcc/testsuite/g++.dg/ext/pr50459.C +++ gcc/testsuite/g++.dg/ext/pr50459.C @@ -0,0 +1,14 @@ +// PR c/50459 +// { dg-do compile } +// { dg-options "-Wall -Wextra" } + +const int A = 128, B = 1; +void *fn1 (void) __attribute__((assume_aligned (A))); +void *fn2 (void) __attribute__((assume_aligned (A, 4))); +void fn3 (void) __attribute__((constructor (A))); +void fn4 (void) __attribute__((destructor (A))); +void *fn5 (int) __attribute__((alloc_size (B))); +void *fn6 (int) __attribute__((alloc_align (B))); +void fn7 (const char *, ...) __attribute__ ((sentinel (B))); +int __attribute__((vector_size (A))) a; +int __attribute__((aligned (A))) foo;