From patchwork Thu Jan 14 16:34:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 59747 Delivered-To: patch@linaro.org Received: by 10.112.130.2 with SMTP id oa2csp4122281lbb; Thu, 14 Jan 2016 08:35:48 -0800 (PST) X-Received: by 10.98.79.140 with SMTP id f12mr7443061pfj.102.1452789347270; Thu, 14 Jan 2016 08:35:47 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id x16si10275173pfa.181.2016.01.14.08.35.46 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Jan 2016 08:35:47 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-419011-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-419011-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-419011-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:date:message-id:subject:from:to:content-type; q= dns; s=default; b=rmEO7/bHup2s06lPJJJ4PmK7q4LiHf7/3Ky9D3W4CXDY0Z kYvn+jrvZcbXP/IVTTtnUpOCjm0K+9nltcQ1Yr/q86Xi6A4M4B/RO2gTZy8MZrAE Z1Rp+3d9+PREOM/TWik5y1okvg/bwnJshjQ0ONBhdP0PWo87P8qz70KwenhUE= 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:date:message-id:subject:from:to:content-type; s= default; bh=VMvJHcopD1k3iiVZDReV8Sno4uw=; b=Zxm4YkP825y7sDahqBz7 ppk1fGSzNTjzLsi0ocBBKGOZWS5N2gnWTfaHt36rvP7rls0Y6j/m7g8WCv7qA0De 5jyz/InLmRXRhiBECuTUBfkijR/NgM2yco1wMaMXqlvo06xQZkI+Z9JhX8Bk6DM/ GfVG7pIJsFe1bF58pwcwfhU= Received: (qmail 53515 invoked by alias); 14 Jan 2016 16:34:50 -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 53440 invoked by uid 89); 14 Jan 2016 16:34:49 -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=97, sk:RECORD_, 9, 7, symtab_node X-HELO: mail-ig0-f172.google.com Received: from mail-ig0-f172.google.com (HELO mail-ig0-f172.google.com) (209.85.213.172) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Thu, 14 Jan 2016 16:34:46 +0000 Received: by mail-ig0-f172.google.com with SMTP id z14so196599808igp.0 for ; Thu, 14 Jan 2016 08:34:46 -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:date:message-id:subject:from:to :content-type; bh=MwhNpaZ1znPGUet5wDszfDsfiR5Vgr8Noab+VxeCXuU=; b=lnIenCz91uXCCPqcTnvCFw5/wzLVAk+egAwCoZmekpuYORucIMFhg9MjFzjXZFsKHh LwwWJbEsjgKD4HRus95/ZKsDX31uefmyKx86jBDzteHhvbiTKAwnyJWtsXsQBX8LD/l3 b5u+zrevCqVhOVZnL7eaScLLdfI+9/uPvdfU3zFrDs1RcwKkJdnzga80ohiSCA28ntaO 1PJs4qU8276h6Ivk26R69ZExPxJ2/eunCm276RBMiUiSR5iB1qXADu24g+xM3PCXkevt y3oAXyP33mMxAFrRh4xufiZEbiOXIk9yRPK534pygEVoBEusDSGOSdnmd05tkA2JlTii qMcA== X-Gm-Message-State: ALoCoQmEMqt2iDx76KFp/x0FC6uGIigxy1ibvdeGgTe8jJbOXcPcP4oUqkqKSUYamg/sj+iiL/J4Il6MH60nxqvHfJyEMvm0zHVwdUDhGJJboTbqDZi1MUo= MIME-Version: 1.0 X-Received: by 10.50.160.1 with SMTP id xg1mr5821216igb.62.1452789284641; Thu, 14 Jan 2016 08:34:44 -0800 (PST) Received: by 10.36.214.70 with HTTP; Thu, 14 Jan 2016 08:34:44 -0800 (PST) Date: Thu, 14 Jan 2016 22:04:44 +0530 Message-ID: Subject: reject decl with incomplete struct/union type in check_global_declaration() From: Prathamesh Kulkarni To: gcc Patches X-IsSubscribed: yes 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. AFAIU that happens because at -O2, analyze_function() removes the symbol "object" from symbol table and assemble_variable() has no chance to process it. g++ rejects it during parsing. I tried similarly in C FE by adding a check for decl with incomplete struct/union type in finish_decl(), however that fails to compile the following case: typedef struct foo foo_t; foo_t x; struct foo { int i; }; g++ rejects the above case as well but gcc accepts it. Do C and C++ standards differ in this regard ? So instead of finish_decl(), I added check for incomplete struct/union in check_global_declaration(), which is called by analyze_function() before removing unused nodes. The patch regresses Wcxx-compat-8.c and declspecs-1.c because the error "storage size of is unknown" shows up in these test-cases. I modified the test-cases to accept that error. Does the patch look OK ? Thank you, Prathamesh diff --git a/gcc/cgraphunit.c b/gcc/cgraphunit.c index 950f6c5..1560a78 100644 --- a/gcc/cgraphunit.c +++ b/gcc/cgraphunit.c @@ -974,6 +974,12 @@ check_global_declaration (symtab_node *snode) ? OPT_Wunused_const_variable : OPT_Wunused_variable), "%qD defined but not used", decl); + + if (VAR_P (decl) && !DECL_EXTERNAL (decl) + && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)) && DECL_SIZE (decl) == 0) + { + error ("storage size of %q+D isn%'t known", decl); + } } /* Discover all functions and variables that are trivially needed, analyze 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..9113076 100644 --- a/gcc/testsuite/gcc.dg/declspec-1.c +++ b/gcc/testsuite/gcc.dg/declspec-1.c @@ -9,7 +9,8 @@ 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; diff --git a/gcc/testsuite/gcc.dg/struct-incompl-2.c b/gcc/testsuite/gcc.dg/struct-incompl-2.c new file mode 100644 index 0000000..f3957eb --- /dev/null +++ b/gcc/testsuite/gcc.dg/struct-incompl-2.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +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" } */ + +extern struct undefined_object object;