From patchwork Wed Jan 20 13:22:48 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 60035 Delivered-To: patch@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp3170449lbb; Wed, 20 Jan 2016 05:23:08 -0800 (PST) X-Received: by 10.66.155.167 with SMTP id vx7mr16247450pab.109.1453296188642; Wed, 20 Jan 2016 05:23:08 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id q74si26595623pfq.207.2016.01.20.05.23.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 20 Jan 2016 05:23:08 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-419585-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-return-419585-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-419585-patch=linaro.org@gcc.gnu.org; dkim=pass header.i=@gcc.gnu.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; q=dns; s=default; b=BZhog/q/CN2LwOZaCu jUanm+dpG6wgHOFLE9mNgpiCpVJsww7INQNDqKx7vdwYfIkbbLRbgdrvztc+ukrP lnED+28cab9TCBsO6eyxqwd703QQU6F55UCnbumGe+7NiiSvsVGd4v5ye3ZR+Pef QHxpg9aVBC9JRAfpbfu6cP/oc= 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 :mime-version:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; s=default; bh=2s561zsZ7yC9C+AjsODedPM4 4lg=; b=Om9/4qsb1NFgdS8DDyX8pkTzB1jBgLCPw+OUcBaPt1VVLkTsCvcT31Qv FQ+tlT/kNT3AGlbpSv9h4JQrYFbUhtbyTijktUdq66Ec86WIRg1UTj6HPJjn+hrc pavqTVSUoJoGrfA3LkqZe5VnPjIdM8Fxwq+XHYp+B6+vPtME/bE= Received: (qmail 47747 invoked by alias); 20 Jan 2016 13:22:54 -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 47738 invoked by uid 89); 20 Jan 2016 13:22:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=915, c90, C90, Hx-languages-length:6395 X-HELO: mail-io0-f173.google.com Received: from mail-io0-f173.google.com (HELO mail-io0-f173.google.com) (209.85.223.173) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Wed, 20 Jan 2016 13:22:50 +0000 Received: by mail-io0-f173.google.com with SMTP id 1so19145894ion.1 for ; Wed, 20 Jan 2016 05:22:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=sAdB++f2lRTgaFeXUJTDn3GrdR6ZwHCq5we1BmS+t4o=; b=DI4ATZpUl3KCujknZhAIu/+XnFPVfsnEiU2sFOzDNBxs2bKZ/QRyNuqpfrLf89kyVP GemWxBZu8f7BF2+QEJyDZq/OvppUSmM2Yyey9LCtfjPtdZlZNkTG8LYera5xxYkWM5r7 wbXhvS98mzgt2+XaOEEzAMRMnadleqXZe5e02a/5rwlegjSeQnaGQ6ygYT603uBopqho 9glPkz5PL6Z3RpwlbLZY0TQGsYWBY+WQbOhkPmMc6Bu4GQAbGuC0riqu9Lt6+neFgmHV rE6EidjA125FyvXAWjPyXA4F3++ZS1SuihdmQI7S02XPnoWlFzFTfTDvMHHatYxdr/aL 99hA== X-Gm-Message-State: ALoCoQnbkX4uNKBRQjo+lqbrkOT4v7xbPhHamrrjBl+0pjNfql+DwDhMu/PgaJJaWIuMuo/FSwLSQ3znGy5VWS/I8W2wNAtMFXhKAedhRODt1VFD+VusI8E= MIME-Version: 1.0 X-Received: by 10.107.159.7 with SMTP id i7mr29720385ioe.29.1453296168611; Wed, 20 Jan 2016 05:22:48 -0800 (PST) Received: by 10.36.214.70 with HTTP; Wed, 20 Jan 2016 05:22:48 -0800 (PST) In-Reply-To: <20160119111932.GU25528@redhat.com> References: <20160119111932.GU25528@redhat.com> Date: Wed, 20 Jan 2016 18:52:48 +0530 Message-ID: Subject: Re: reject decl with incomplete struct/union type in check_global_declaration() From: Prathamesh Kulkarni To: Marek Polacek Cc: Joseph Myers , gcc Patches X-IsSubscribed: yes On 19 January 2016 at 16:49, Marek Polacek wrote: > Sorry for speaking up late, but I think we could do better with formatting > in this patch: > > On Sat, Jan 16, 2016 at 03:45:22PM +0530, Prathamesh Kulkarni wrote: >> diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c >> index 915376d..d36fc67 100644 >> --- a/gcc/c/c-decl.c >> +++ b/gcc/c/c-decl.c >> @@ -4791,6 +4791,13 @@ finish_decl (tree decl, location_t init_loc, tree init, >> TREE_TYPE (decl) = error_mark_node; >> } >> >> + if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)) >> + || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) >> + && DECL_SIZE (decl) == 0 && TREE_STATIC (decl)) > > DECL_SIZE yields a tree, so I'd rather see NULL_TREE instead of 0 here (yeah, > the enclosing code uses 0s :(). The "&& TREE_STATIC..." should be on its own > line. > >> + { >> + incomplete_record_decls.safe_push (decl); >> + } >> + > > Redundant braces. > >> diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c >> index a0e0052..3c8a496 100644 >> --- a/gcc/c/c-parser.c >> +++ b/gcc/c/c-parser.c >> @@ -59,6 +59,8 @@ along with GCC; see the file COPYING3. If not see >> #include "gimple-expr.h" >> #include "context.h" >> >> +vec incomplete_record_decls = vNULL; > > This could use a comment. > >> + >> + for (unsigned i = 0; i < incomplete_record_decls.length (); ++i) >> + { >> + tree decl = incomplete_record_decls[i]; >> + if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node) > > I'd s/0/NULL_TREE/. Thanks for the review, I have done the suggested changes in this version of the patch. Ok for trunk ? Thanks, Prathamesh > > Marek diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index 5830e22..1ec6042 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -4791,6 +4791,12 @@ finish_decl (tree decl, location_t init_loc, tree init, TREE_TYPE (decl) = error_mark_node; } + if ((RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)) + || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE) + && DECL_SIZE (decl) == NULL_TREE + && TREE_STATIC (decl)) + incomplete_record_decls.safe_push (decl); + if (is_global_var (decl) && DECL_SIZE (decl) != 0) { if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) diff --git a/gcc/c/c-parser.c b/gcc/c/c-parser.c index 919680a..1d3b9e1 100644 --- a/gcc/c/c-parser.c +++ b/gcc/c/c-parser.c @@ -59,6 +59,15 @@ along with GCC; see the file COPYING3. If not see #include "gimple-expr.h" #include "context.h" +/* We need to walk over decls with incomplete struct/union/enum types + after parsing the whole translation unit. + In finish_decl(), if the decl is static, has incomplete + struct/union/enum type, it is appened to incomplete_record_decls. + In c_parser_translation_unit(), we iterate over incomplete_record_decls + and report error if any of the decls are still incomplete. */ + +vec incomplete_record_decls = vNULL; + void set_c_expr_source_range (c_expr *expr, location_t start, location_t finish) @@ -1421,6 +1430,16 @@ c_parser_translation_unit (c_parser *parser) } while (c_parser_next_token_is_not (parser, CPP_EOF)); } + + for (unsigned i = 0; i < incomplete_record_decls.length (); ++i) + { + tree decl = incomplete_record_decls[i]; + if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node) + { + error ("storage size of %q+D isn%'t known", decl); + TREE_TYPE (decl) = error_mark_node; + } + } } /* Parse an external declaration (C90 6.7, C99 6.9). diff --git a/gcc/c/c-tree.h b/gcc/c/c-tree.h index 81a3d58..cf79ba7 100644 --- a/gcc/c/c-tree.h +++ b/gcc/c/c-tree.h @@ -731,4 +731,6 @@ set_c_expr_source_range (c_expr *expr, /* In c-fold.c */ extern tree decl_constant_value_for_optimization (tree); +extern vec incomplete_record_decls; + #endif /* ! GCC_C_TREE_H */ diff --git a/gcc/testsuite/gcc.dg/Wcxx-compat-8.c b/gcc/testsuite/gcc.dg/Wcxx-compat-8.c index f7e8c55..4e9ddc1 100644 --- a/gcc/testsuite/gcc.dg/Wcxx-compat-8.c +++ b/gcc/testsuite/gcc.dg/Wcxx-compat-8.c @@ -33,6 +33,7 @@ enum e3 __typeof__ (struct s5 { int i; }) v5; /* { dg-warning "invalid in C\[+\]\[+\]" } */ __typeof__ (struct t5) w5; /* { dg-bogus "invalid in C\[+\]\[+\]" } */ + /* { dg-error "storage size of 'w5' isn't known" "" { target *-*-* } 35 } */ int f1 (struct s1 *p) @@ -64,4 +65,4 @@ f5 () return &((struct t8) { }); /* { dg-warning "invalid in C\[+\]\[+\]" } */ } -/* { dg-error "invalid use of undefined type" "" { target *-*-* } 64 } */ +/* { dg-error "invalid use of undefined type" "" { target *-*-* } 65 } */ diff --git a/gcc/testsuite/gcc.dg/declspec-1.c b/gcc/testsuite/gcc.dg/declspec-1.c index c19f107..b024601 100644 --- a/gcc/testsuite/gcc.dg/declspec-1.c +++ b/gcc/testsuite/gcc.dg/declspec-1.c @@ -9,13 +9,15 @@ typedef int t; /* These should all be diagnosed, but only once, not for every identifier declared. */ struct s0 int x0, /* { dg-error "two or more data types" } */ -x1; +/* { dg-error "storage size of 'x0' isn't known" "" { target *-*-* } 11 } */ +x1; /* { dg-error "storage size of 'x1' isn't known" } */ char union u0 x2, /* { dg-error "two or more data types" } */ x3; enum e0 struct s1 x4, /* { dg-error "two or more data types" } */ -x5; + /* { dg-error "storage size of 'x4' isn't known" "" { target *-*-* } 18 } */ +x5; /* { dg-error "storage size of 'x5' isn't known" } */ short short x6, /* { dg-error "duplicate" } */ x7; diff --git a/gcc/testsuite/gcc.dg/pr24293.c b/gcc/testsuite/gcc.dg/pr24293.c new file mode 100644 index 0000000..5bf7ad17 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr24293.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-fsyntax-only" } */ + +static struct foo x; /* { dg-error "storage size of 'x' isn't known" } */ +static union bar y; /* { dg-error "storage size of 'y' isn't known" } */ + +typedef struct P p; +static p p_obj; /* { dg-error "storage size of 'p_obj' isn't known" } */ + +static enum e e_var; /* { dg-error "storage size of 'e_var' isn't known" } */ + +extern struct undefined_object object; diff --git a/gcc/testsuite/gcc.dg/pr63549.c b/gcc/testsuite/gcc.dg/pr63549.c index c9b1718..bd0b706 100644 --- a/gcc/testsuite/gcc.dg/pr63549.c +++ b/gcc/testsuite/gcc.dg/pr63549.c @@ -2,6 +2,6 @@ /* { dg-do compile } */ /* { dg-options "" } */ -enum E e; +enum E e; /* { dg-error "storage size of 'e' isn't known" } */ int a[10]; int i = a[e]; /* { dg-error "has an incomplete type" } */