From patchwork Fri Apr 18 20:25:33 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 340422 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 B08C61400E0 for ; Sat, 19 Apr 2014 06:25:51 +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:mime-version:content-type; q=dns; s=default; b=pB+DDsE4nOgC4XsCmAlKJ/dQaBT97OQsKD+V/ul4pa501EyAOE 3KFiCjTh8mG1uEOGle9YIKutR4QnlNn8Yj6YsJUquNcN2NqnKhpwZSd7SY5aiAiu Ra7EYWuQnKSv8CpWKEJIVKhi3Pyj8KMDa4C5X+aRWmc4feq/pKTCxzyVI= 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=k7SsNc+Pev6Wqh3lOWBDSVqkw+g=; b=xMNKi4ctpGRYXvlnL7SH iEFEKf+Dk0OATcKns/9V40GSF3ve5mT1QTsFRXpX/a5WDUO4ee2HyB05nYUrDCrL nyv6s+6HPHhXNXyLDzd8BOYW6dAsJXPqJ4Z0bGVw1zUKY7nHQ62sW+PjggaCa+C5 HZpRk/oG0OOnwSSWLjsWj+w= Received: (qmail 1069 invoked by alias); 18 Apr 2014 20:25:45 -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 1060 invoked by uid 89); 18 Apr 2014 20:25:45 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.1 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; Fri, 18 Apr 2014 20:25:44 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s3IKPcs8024857 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 18 Apr 2014 16:25:38 -0400 Received: from redhat.com (ovpn-116-31.ams2.redhat.com [10.36.116.31]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id s3IKPYtf014944 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Fri, 18 Apr 2014 16:25:37 -0400 Date: Fri, 18 Apr 2014 22:25:33 +0200 From: Marek Polacek To: GCC Patches Cc: "Joseph S. Myers" Subject: [C PATCH] Fix up diagnostics (PR c/25801) Message-ID: <20140418202533.GN20332@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Here's a patch that tidies up diagnostics given when there's an arithmetic on pointer to an incomplete type. Firstly, we ought not to say "unknown structure" when the type is not a structure; secondly, we shouldn't report identical error twice. (It might make sense to instead "increment/decrement of" just say "arithmetic on", but I'm not changing that now.) Regtested on x86_64-unknown-linux-gnu, bootstrapped on powerpc64-unknown-linux-gnu, ok for trunk? 2014-04-18 Marek Polacek PR c/25801 * c-typeck.c (c_size_in_bytes): Don't call error. (pointer_diff): Remove comment. (build_unary_op): Improve error messages. * gcc.dg/pr25801.c: New test. Marek diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c index 65aad45..bc977a7 100644 --- gcc/c/c-typeck.c +++ gcc/c/c-typeck.c @@ -1760,15 +1760,10 @@ c_size_in_bytes (const_tree type) { enum tree_code code = TREE_CODE (type); - if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK) + if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK + || !COMPLETE_OR_VOID_TYPE_P (type)) return size_one_node; - if (!COMPLETE_OR_VOID_TYPE_P (type)) - { - error ("arithmetic on pointer to an incomplete type"); - return size_one_node; - } - /* Convert in case a char is more than one unit. */ return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type), size_int (TYPE_PRECISION (char_type_node) @@ -3528,7 +3523,6 @@ pointer_diff (location_t loc, tree op0, tree op1) if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1)))) error_at (loc, "arithmetic on pointer to an incomplete type"); - /* This generates an error if op0 is pointer to incomplete type. */ op1 = c_size_in_bytes (target_type); if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1))) @@ -4002,16 +3996,18 @@ build_unary_op (location_t location, if (typecode == POINTER_TYPE) { - /* If pointer target is an undefined struct, + /* If pointer target is an incomplete type, we just cannot know how to do the arithmetic. */ if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype))) { if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR) error_at (location, - "increment of pointer to unknown structure"); + "increment of pointer to an incomplete type %qT", + TREE_TYPE (argtype)); else error_at (location, - "decrement of pointer to unknown structure"); + "decrement of pointer to an incomplete type %qT", + TREE_TYPE (argtype)); } else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE) diff --git gcc/testsuite/gcc.dg/pr25801.c gcc/testsuite/gcc.dg/pr25801.c index e69de29..70b4ef8 100644 --- gcc/testsuite/gcc.dg/pr25801.c +++ gcc/testsuite/gcc.dg/pr25801.c @@ -0,0 +1,19 @@ +/* PR c/25801 */ +/* { dg-do compile } */ + +int (*a)[]; +struct S *s; + +void +f (void) +{ + a++; /* { dg-error "increment of pointer to an incomplete type" } */ + ++a; /* { dg-error "increment of pointer to an incomplete type" } */ + a--; /* { dg-error "decrement of pointer to an incomplete type" } */ + --a; /* { dg-error "decrement of pointer to an incomplete type" } */ + + s++; /* { dg-error "increment of pointer to an incomplete type" } */ + ++s; /* { dg-error "increment of pointer to an incomplete type" } */ + s--; /* { dg-error "decrement of pointer to an incomplete type" } */ + --s; /* { dg-error "decrement of pointer to an incomplete type" } */ +}