From patchwork Sat Nov 30 10:41:42 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Glisse X-Patchwork-Id: 295560 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)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9585A2C0097 for ; Sat, 30 Nov 2013 21:42:05 +1100 (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:mime-version:content-type; q=dns; s=default; b=BTkn4PvTL6qvkRVKFBymZRInScA7K2O1yAUKgCJYSgl60/DhA0 eG0BjAllCbIa8rgeumEJb/nTQY9gxC4agJ1nlVAkOUXbsLC+jqzp8FmroVwfVMCE xoDfjRcOYU6ueFu98L7o9HADq6uWjGu8zyKohBxK8d/H/5MG9awDICNFI= 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:mime-version:content-type; s= default; bh=3QhUq5C06VTyekgxPJ4YbwVhrZg=; b=QvJmzmV/CZVaJ2H8MURn LmMa0iPS+1yQ4K4kL8mth1qa8LDO13dYp3WrnEk4LtiVrTDkD8K+QYCCROaxPoU1 1Vn5BG5Q2M/wsOJ2mH/8iDyC/3F9VmU0nP6O8h/mNxcb8bCZJLhwsPqjyU6AfZxi M4C885elwapUkiX3W5aKHq4= Received: (qmail 24620 invoked by alias); 30 Nov 2013 10:41:55 -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 24608 invoked by uid 89); 30 Nov 2013 10:41:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL, BAYES_50, RDNS_NONE, URIBL_BLOCKED autolearn=no version=3.3.2 X-HELO: mail3-relais-sop.national.inria.fr Received: from Unknown (HELO mail3-relais-sop.national.inria.fr) (192.134.164.104) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (CAMELLIA256-SHA encrypted) ESMTPS; Sat, 30 Nov 2013 10:41:52 +0000 Received: from stedding.saclay.inria.fr ([193.55.250.194]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 30 Nov 2013 11:41:43 +0100 Received: from glisse (helo=localhost) by stedding.saclay.inria.fr with local-esmtp (Exim 4.80) (envelope-from ) id 1Vmhzb-0002iq-2l; Sat, 30 Nov 2013 11:41:43 +0100 Date: Sat, 30 Nov 2013 11:41:42 +0100 (CET) From: Marc Glisse To: gcc-patches@gcc.gnu.org cc: jason@redhat.com Subject: [C,C++] integer constants in attribute arguments Message-ID: User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Hello, we currently reject: constexpr int s = 32; typedef double VEC __attribute__ ((__vector_size__ (s))); and similarly for other attributes, while we accept s+0 or (int)s, etc. The code is basically copied from the constructor attribute. The C front-end is much less forgiving than the C++ one, so we need to protect the call to default_conversion (as in PR c/59280), and for some reason one of the attributes can see a FUNCTION_DECL where others see an IDENTIFIER_NODE, I didn't try to understand why and just added that check to the code. Bootstrap and testsuite on x86_64-linux-gnu. 2013-11-30 Marc Glisse PR c++/53017 PR c++/59211 gcc/c-family/ * c-common.c (handle_aligned_attribute, handle_alloc_size_attribute, handle_vector_size_attribute, handle_nonnull_attribute): Call default_conversion on the attribute argument. gcc/cp/ * tree.c (handle_init_priority_attribute): Likewise. gcc/ * doc/extend.texi (Function Attributes): Typo. gcc/testsuite/ * c-c++-common/attributes-1.c: New testcase. * g++.dg/cpp0x/constexpr-attribute2.C: Likewise. Index: gcc/c-family/c-common.c =================================================================== --- gcc/c-family/c-common.c (revision 205548) +++ gcc/c-family/c-common.c (working copy) @@ -7504,24 +7504,32 @@ check_cxx_fundamental_alignment_constrai /* Handle a "aligned" attribute; arguments as in struct attribute_spec.handler. */ static tree handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args, int flags, bool *no_add_attrs) { tree decl = NULL_TREE; tree *type = NULL; int is_type = 0; - tree align_expr = (args ? TREE_VALUE (args) - : size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT)); + tree align_expr; int i; + if (args) + { + align_expr = TREE_VALUE (args); + if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE) + align_expr = default_conversion (align_expr); + } + else + align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT); + if (DECL_P (*node)) { decl = *node; type = &TREE_TYPE (decl); is_type = TREE_CODE (*node) == TYPE_DECL; } else if (TYPE_P (*node)) type = node, is_type = 1; if ((i = check_user_alignment (align_expr, false)) == -1 @@ -8007,20 +8015,23 @@ handle_malloc_attribute (tree *node, tre struct attribute_spec.handler. */ static tree handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args, int ARG_UNUSED (flags), bool *no_add_attrs) { 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); if (TREE_CODE (position) != INTEGER_CST || TREE_INT_CST_HIGH (position) || TREE_INT_CST_LOW (position) < 1 || TREE_INT_CST_LOW (position) > arg_count ) { warning (OPT_Wattributes, "alloc_size parameter outside range"); *no_add_attrs = true; return NULL_TREE; @@ -8451,20 +8462,22 @@ handle_vector_size_attribute (tree *node int ARG_UNUSED (flags), bool *no_add_attrs) { unsigned HOST_WIDE_INT vecsize, nunits; enum machine_mode orig_mode; tree type = *node, new_type, size; *no_add_attrs = true; size = TREE_VALUE (args); + if (size && size != error_mark_node && TREE_CODE (size) != IDENTIFIER_NODE) + size = default_conversion (size); if (!tree_fits_uhwi_p (size)) { warning (OPT_Wattributes, "%qE attribute ignored", name); return NULL_TREE; } /* Get the vector size (in bytes). */ vecsize = tree_to_uhwi (size); @@ -8548,21 +8561,25 @@ handle_nonnull_attribute (tree *node, tr } return NULL_TREE; } /* Argument list specified. Verify that each argument number references a pointer argument. */ for (attr_arg_num = 1; args; args = TREE_CHAIN (args)) { unsigned HOST_WIDE_INT arg_num = 0, ck_num; - if (!get_nonnull_operand (TREE_VALUE (args), &arg_num)) + tree arg = TREE_VALUE (args); + if (arg && TREE_CODE (arg) != IDENTIFIER_NODE) + arg = default_conversion (arg); + + if (!get_nonnull_operand (arg, &arg_num)) { error ("nonnull argument has invalid operand number (argument %lu)", (unsigned long) attr_arg_num); *no_add_attrs = true; return NULL_TREE; } if (prototype_p (type)) { function_args_iterator iter; Index: gcc/cp/tree.c =================================================================== --- gcc/cp/tree.c (revision 205548) +++ gcc/cp/tree.c (working copy) @@ -3229,20 +3229,21 @@ handle_init_priority_attribute (tree* no tree args, int /*flags*/, bool* no_add_attrs) { tree initp_expr = TREE_VALUE (args); tree decl = *node; tree type = TREE_TYPE (decl); int pri; STRIP_NOPS (initp_expr); + initp_expr = default_conversion (initp_expr); if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST) { error ("requested init_priority is not an integer constant"); *no_add_attrs = true; return NULL_TREE; } pri = TREE_INT_CST_LOW (initp_expr); Index: gcc/doc/extend.texi =================================================================== --- gcc/doc/extend.texi (revision 205548) +++ gcc/doc/extend.texi (working copy) @@ -2234,21 +2234,21 @@ information to improve the correctness o The function parameter(s) denoting the allocated size are specified by one or two integer arguments supplied to the attribute. The allocated size is either the value of the single function argument specified or the product of the two function arguments specified. Argument numbering starts at one. For instance, @smallexample void* my_calloc(size_t, size_t) __attribute__((alloc_size(1,2))) -void my_realloc(void*, size_t) __attribute__((alloc_size(2))) +void* my_realloc(void*, size_t) __attribute__((alloc_size(2))) @end smallexample @noindent declares that @code{my_calloc} returns memory of the size given by the product of parameter 1 and 2 and that @code{my_realloc} returns memory of the size given by parameter 2. @item always_inline @cindex @code{always_inline} function attribute Generally, functions are not inlined unless optimization is specified. Index: gcc/testsuite/c-c++-common/attributes-1.c =================================================================== --- gcc/testsuite/c-c++-common/attributes-1.c (revision 0) +++ gcc/testsuite/c-c++-common/attributes-1.c (working copy) @@ -0,0 +1,21 @@ +/* { dg-do compile } */ +/* { dg-prune-output "undeclared here \\(not in a function\\)|\[^\n\r\]* was not declared in this scope" } */ + +void* my_calloc(unsigned, unsigned) __attribute__((alloc_size(1,bar))); /* { dg-warning "outside range" } */ +void* my_realloc(void*, unsigned) __attribute__((alloc_size(bar))); /* { dg-warning "outside range" } */ + +typedef char vec __attribute__((vector_size(bar))); /* { dg-warning "ignored" } */ + +void f(char*) __attribute__((nonnull(bar))); /* { dg-error "invalid operand" } */ + +void g() __attribute__((aligned(bar))); /* { dg-error "invalid value|not an integer" } */ + +void foo(void); +void* my_calloc(unsigned, unsigned) __attribute__((alloc_size(1,foo))); /* { dg-warning "outside range" } */ +void* my_realloc(void*, unsigned) __attribute__((alloc_size(foo))); /* { dg-warning "outside range" } */ + +typedef char vec __attribute__((vector_size(foo))); /* { dg-warning "ignored" } */ + +void f(char*) __attribute__((nonnull(foo))); /* { dg-error "invalid operand" } */ + +void g() __attribute__((aligned(foo))); /* { dg-error "invalid value|not an integer" } */ Property changes on: gcc/testsuite/c-c++-common/attributes-1.c ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +Author Date Id Revision URL \ No newline at end of property Index: gcc/testsuite/g++.dg/cpp0x/constexpr-attribute2.C =================================================================== --- gcc/testsuite/g++.dg/cpp0x/constexpr-attribute2.C (revision 0) +++ gcc/testsuite/g++.dg/cpp0x/constexpr-attribute2.C (working copy) @@ -0,0 +1,32 @@ +// { dg-options -std=gnu++11 } + +struct t { t(); }; + +constexpr int prio = 123; +constexpr int size = 8; +constexpr int pos = 1; +enum A { zero = 0, one, two }; +__attribute__((init_priority(prio))) t a; + +enum class E1 : int { + first = 101, + second, + third, +}; +__attribute__((init_priority(E1::second))) t b; // Should not compile? + +enum E2 { + E2_first = 141, + E2_second, + E2_third, +}; +__attribute__((init_priority(E2_second))) t c; + +void* my_calloc(unsigned, unsigned) __attribute__((alloc_size(pos,two))); +void* my_realloc(void*, unsigned) __attribute__((alloc_size(two))); + +typedef char vec __attribute__((vector_size(size))); + +void f(char*) __attribute__((nonnull(pos))); + +void g() __attribute__((aligned(size)));