From patchwork Sat Jan 16 10:15:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 59907 Delivered-To: patch@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp1022099lbb; Sat, 16 Jan 2016 02:15:41 -0800 (PST) X-Received: by 10.66.255.39 with SMTP id an7mr19728277pad.101.1452939341332; Sat, 16 Jan 2016 02:15:41 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id so5si23123522pab.198.2016.01.16.02.15.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 16 Jan 2016 02:15:41 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-419249-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-419249-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-419249-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=FXBf7TmCJP2ZSnIAXG TDraPygcmRR0L4nv5NVH71SPmkaduC3RS22tp8TztgL3KfPSMq9eEtHvEhSTnq74 caXoNEMiSoymXRwVQnomFIcX5VYPBPXEIU0AB5y9Dxj3UADBbXUzCLnsSZJA3tfK PxIIe3OXBINRM51whmtmN65SU= 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=ptuLh/m9CirfzzJhKnqCVWJy 5uc=; b=LQga3J1jWGf6o6nOcqfuwxYnH0kszDB1YkFkvu+oKHPeUWhZguze6Oaw +UJXtkL5kX3Vqd7pUU2DhspqDwJHSZb+RjPi4Zp5QZNIGdYOGXL3zfBxRGs9wZ8C 7XglFo2jyDL+cysdrABn8ewDUWw09PKMXbOOLHDLTLQb1ZxPryA= Received: (qmail 106629 invoked by alias); 16 Jan 2016 10:15:29 -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 106609 invoked by uid 89); 16 Jan 2016 10:15:28 -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 X-HELO: mail-ig0-f175.google.com Received: from mail-ig0-f175.google.com (HELO mail-ig0-f175.google.com) (209.85.213.175) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Sat, 16 Jan 2016 10:15:26 +0000 Received: by mail-ig0-f175.google.com with SMTP id ik10so28604606igb.1 for ; Sat, 16 Jan 2016 02:15:26 -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=3mkPB6UYklnFOPPxxSl5zADT7CJK3w2VsHtT6106NWE=; b=nFy1wR69lhi8rDS2palu5fY5j/8MTiTf5Es5CBEGWxLfLLNVzVaxJdw6wOmdK+pf2K vfPAy7DRNJtOoQrrBkMPfnt3RbVMRLc4k1SD48uB8QSM7Wa5xkEx1bl0bLy7jsOmIMB5 hFmfFycGOyQGDUUSZ9RFEYNOxKuV9oqnCIsxZ1AAxmzy74GD7gEBnlzOJjQ1QoRH7vlC wtjo98I9IayVBPmH8mGa3DK3vywtjqfYeT6+s5V4V7QWHk4lvJIAB586b6TagsxrQBxU UfQXgzeYOXZ2DNn5G8onZUXUwD9FRtQlZCT8tyYwdvLEL9oyenHintJDVtLK8UJMmiRo QwDw== X-Gm-Message-State: AG10YOSJRdmmN8CBy/Nu3zDFbBBOjW4oebmHGlwHaNwy3KJg6pMyQwAha/loAUiKQw96nyNkMZ3buWnXTi3mfxDO MIME-Version: 1.0 X-Received: by 10.50.50.9 with SMTP id y9mr2906742ign.46.1452939324163; Sat, 16 Jan 2016 02:15:24 -0800 (PST) Received: by 10.36.214.70 with HTTP; Sat, 16 Jan 2016 02:15:22 -0800 (PST) In-Reply-To: References: Date: Sat, 16 Jan 2016 15:45:22 +0530 Message-ID: Subject: Re: reject decl with incomplete struct/union type in check_global_declaration() From: Prathamesh Kulkarni To: Joseph Myers Cc: gcc Patches X-IsSubscribed: yes On 16 January 2016 at 02:56, Joseph Myers wrote: > On Fri, 15 Jan 2016, Prathamesh Kulkarni wrote: > >> On 15 January 2016 at 03:27, Joseph Myers wrote: >> > On Thu, 14 Jan 2016, Prathamesh Kulkarni wrote: >> > >> >> Hi, >> >> For test-case containing only the following declaration: >> >> static struct undefined_struct object; >> >> gcc rejects it at -O0 in assemble_variable() with error "storage size >> >> of is unknown", >> >> however no error is reported when compiled with -O2. >> > >> > Cf bug 24293 (for the -fsyntax-only case) - does this patch fix that? >> Ah this doesn't fix PR24293, it seems analyze_function() doesn't get >> called for -fsyntax-only. >> I don't have a good solution for this. I assume varpool won't be >> populated for -fsyntax-only ? >> And we need to walk over decls with incomplete struct/union types >> after parsing the whole translation unit. >> In the attached patch, I kept a global vec incomplete_record_decls; >> In finish_decl(), if the decl is static, has type struct/union and >> size 0 then it is appened to incomplete_record_decls. >> In c_parser_translation_unit(), iterate over incomplete_record_decls >> and if report error if any decl has size zero. >> The patch passes testsuite. > > There's a GNU C extension allowing forward declarations of enums, and it > seems that > > static enum e x; > > doesn't get diagnosed either with -fsyntax-only. Thus I think you should > cover that case as well. Done in the attached patch. It regresses pr63549.c because the error showed up there, adjusted the test-case to accept the error. OK to commit ? Thanks, Prathamesh > > -- > Joseph S. Myers > joseph@codesourcery.com 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)) + { + 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 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; + void set_c_expr_source_range (c_expr *expr, location_t start, location_t finish) @@ -1421,6 +1423,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) == 0 && 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" } */