From patchwork Mon Aug 3 13:51:26 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 503158 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 AA86B140E0F for ; Mon, 3 Aug 2015 23:51:39 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=OntZN/jj; dkim-atps=neutral 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=BhobSPkFaM8G/kquhtwEQ/OmOoONuDJDHA5qUIUcqW0ioMrTZdlKB 3ImFurQN47KYSYGlRvBYect6dGG2SzVmBnHLZKcz+nG1dAs0dRjYsyMzaOEKM53m LbFTcqENqoxoxjpmW3uoY5zz0+24aJ+vwmQnxvsGDihDza+U0lF+cA= 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=Q4nooFTj6Ba8aYrghe4IQz2F6SM=; b=OntZN/jjfYauHPTCZmvQ nFzbtQnraekPlG9AWYiEjBMdUiqJJY9Fv9ECyAtxBEbDz5L6C2o2FuqEO4DjDldo 6fB3HzjgWAlyUT6JtGei3kxUT5eOs9gQ+DgwpTJ1ageZ8if0dS7aO5vD8x7AwxQm +VIocWQ3d+1X1BkzGHBUgsc= Received: (qmail 83508 invoked by alias); 3 Aug 2015 13:51:33 -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 83499 invoked by uid 89); 3 Aug 2015 13:51:32 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=no 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 (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 03 Aug 2015 13:51:31 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id 58E53C9D21 for ; Mon, 3 Aug 2015 13:51:30 +0000 (UTC) Received: from redhat.com (ovpn-204-63.brq.redhat.com [10.40.204.63]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t73DpQEp025693 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Mon, 3 Aug 2015 09:51:29 -0400 Date: Mon, 3 Aug 2015 15:51:26 +0200 From: Marek Polacek To: GCC Patches Subject: [C PATCH] Better location for bit-fields (PR c/67088) Message-ID: <20150803135126.GV3335@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) This patch improves diagnostics for invalid bit-fields. Bootstrapped/regtested on x86_64-linux, applying to trunk. 2015-08-03 Marek Polacek PR c/67088 * c-decl.c (check_bitfield_type_and_width): Add location parameter. Use it. (grokdeclarator): Pass LOC down to check_bitfield_type_and_width. * gcc.dg/pr67088.c: New test. Marek diff --git gcc/c/c-decl.c gcc/c/c-decl.c index 2669764..0d7aa3f 100644 --- gcc/c/c-decl.c +++ gcc/c/c-decl.c @@ -5153,8 +5153,10 @@ flexible_array_type_p (tree type) /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME, replacing with appropriate values if they are invalid. */ + static void -check_bitfield_type_and_width (tree *type, tree *width, tree orig_name) +check_bitfield_type_and_width (location_t loc, tree *type, tree *width, + tree orig_name) { tree type_mv; unsigned int max_width; @@ -5167,7 +5169,7 @@ check_bitfield_type_and_width (tree *type, tree *width, tree orig_name) field widths. */ if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))) { - error ("bit-field %qs width not an integer constant", name); + error_at (loc, "bit-field %qs width not an integer constant", name); *width = integer_one_node; } else @@ -5176,24 +5178,24 @@ check_bitfield_type_and_width (tree *type, tree *width, tree orig_name) { *width = c_fully_fold (*width, false, NULL); if (TREE_CODE (*width) == INTEGER_CST) - pedwarn (input_location, OPT_Wpedantic, + pedwarn (loc, OPT_Wpedantic, "bit-field %qs width not an integer constant expression", name); } if (TREE_CODE (*width) != INTEGER_CST) { - error ("bit-field %qs width not an integer constant", name); + error_at (loc, "bit-field %qs width not an integer constant", name); *width = integer_one_node; } constant_expression_warning (*width); if (tree_int_cst_sgn (*width) < 0) { - error ("negative width in bit-field %qs", name); + error_at (loc, "negative width in bit-field %qs", name); *width = integer_one_node; } else if (integer_zerop (*width) && orig_name) { - error ("zero width for bit-field %qs", name); + error_at (loc, "zero width for bit-field %qs", name); *width = integer_one_node; } } @@ -5203,7 +5205,7 @@ check_bitfield_type_and_width (tree *type, tree *width, tree orig_name) && TREE_CODE (*type) != BOOLEAN_TYPE && TREE_CODE (*type) != ENUMERAL_TYPE) { - error ("bit-field %qs has invalid type", name); + error_at (loc, "bit-field %qs has invalid type", name); *type = unsigned_type_node; } @@ -5212,14 +5214,14 @@ check_bitfield_type_and_width (tree *type, tree *width, tree orig_name) && type_mv != integer_type_node && type_mv != unsigned_type_node && type_mv != boolean_type_node) - pedwarn_c90 (input_location, OPT_Wpedantic, + pedwarn_c90 (loc, OPT_Wpedantic, "type of bit-field %qs is a GCC extension", name); max_width = TYPE_PRECISION (*type); if (0 < compare_tree_int (*width, max_width)) { - error ("width of %qs exceeds its type", name); + error_at (loc, "width of %qs exceeds its type", name); w = max_width; *width = build_int_cst (integer_type_node, w); } @@ -5232,7 +5234,7 @@ check_bitfield_type_and_width (tree *type, tree *width, tree orig_name) if (!lt || w < tree_int_cst_min_precision (lt->enum_min, TYPE_SIGN (*type)) || w < tree_int_cst_min_precision (lt->enum_max, TYPE_SIGN (*type))) - warning (0, "%qs is narrower than values of its type", name); + warning_at (loc, 0, "%qs is narrower than values of its type", name); } } @@ -6224,7 +6226,7 @@ grokdeclarator (const struct c_declarator *declarator, /* Check the type and width of a bit-field. */ if (bitfield) { - check_bitfield_type_and_width (&type, width, name); + check_bitfield_type_and_width (loc, &type, width, name); /* C11 makes it implementation-defined (6.7.2.1#5) whether atomic types are permitted for bit-fields; we have no code to make bit-field accesses atomic, so disallow them. */ diff --git gcc/testsuite/gcc.dg/pr67088.c gcc/testsuite/gcc.dg/pr67088.c index e69de29..b2f77f5 100644 --- gcc/testsuite/gcc.dg/pr67088.c +++ gcc/testsuite/gcc.dg/pr67088.c @@ -0,0 +1,18 @@ +/* PR c/67088 */ +/* { dg-do compile } */ +/* { dg-options "-Wpedantic -std=c90" } */ + +enum E { A = 2 }; +int j; +float f; +struct S1 { + double b1:1; /* { dg-error "10:has invalid type" } */ + int b2:j; /* { dg-error "7:width not an integer constant" } */ + int b3:f; /* { dg-error "7:width not an integer constant" } */ + int b4:(int) __builtin_log (100); /* { dg-warning "7:width not an integer constant" } */ + int b5:-2; /* { dg-error "7:negative width" } */ + int b6:0; /* { dg-error "7:zero width" } */ + long int b7:32; /* { dg-warning "12:type of bit-field" } */ + int b8:sizeof (int) * __CHAR_BIT__ * 2; /* { dg-error "7:width of" } */ + __extension__ enum E b9:1; /* { dg-warning "24:is narrower" } */ +};