From 31d1aceca6fbc8353456ab080b01ef87aedb42dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabien=20Ch=C3=AAne?= Date: Wed, 28 Apr 2010 00:03:21 +0000 Subject: [PATCH] re PR c++/29043 (Constructor for POD type with const member without member initializer accepted) PR c++/29043 * init.c (perform_member_init): check for uninitialized const or reference members, including array types. From-SVN: r158817 --- gcc/cp/ChangeLog | 6 ++++ gcc/cp/init.c | 6 ++++ gcc/testsuite/ChangeLog | 5 +++ gcc/testsuite/g++.dg/init/pr29043.C | 52 +++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 gcc/testsuite/g++.dg/init/pr29043.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 08746bae335..3935cc21241 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2010-04-27 Fabien ChĂȘne + + PR c++/29043 + * init.c (perform_member_init): check for uninitialized const or + reference members, including array types. + 2010-04-24 Jason Merrill * tree.c (get_fns): Split out from get_first_fn. diff --git a/gcc/cp/init.c b/gcc/cp/init.c index e1dee1d10dc..57b874d5e4a 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -506,6 +506,7 @@ perform_member_init (tree member, tree init) { if (init == NULL_TREE) { + tree core_type; /* member traversal: note it leaves init NULL */ if (TREE_CODE (type) == REFERENCE_TYPE) permerror (DECL_SOURCE_LOCATION (current_function_decl), @@ -515,6 +516,11 @@ perform_member_init (tree member, tree init) permerror (DECL_SOURCE_LOCATION (current_function_decl), "uninitialized member %qD with % type %qT", member, type); + + core_type = strip_array_types (type); + if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type) + || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)) + diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false); } else if (TREE_CODE (init) == TREE_LIST) /* There was an explicit member initialization. Do some work diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 1bd00ff9bef..ac3d6e63021 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2010-04-27 Fabien ChĂȘne + + PR c++/29043 + * g++.dg/init/pr29043.C: New. + 2010-04-27 Jason Merrill * g++.dg/lookup/scoped5.C: Adjust. diff --git a/gcc/testsuite/g++.dg/init/pr29043.C b/gcc/testsuite/g++.dg/init/pr29043.C new file mode 100644 index 00000000000..6ed31b5d6b5 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/pr29043.C @@ -0,0 +1,52 @@ +// PR c++/29043 +// { dg-do compile } + +struct S +{ + int const i; // { dg-message "should be initialized" } +}; + +class C +{ +public: + C() {} // { dg-error "uninitialized const member" } + S s; +}; + +struct S2 +{ + int& ref; // { dg-message "should be initialized" } +}; + +class C2 +{ +public: + C2() {} // { dg-error "uninitialized reference member" } + S2 s; +}; + +class C3 +{ + C3() { } + struct s { + const int i; + }; +}; + +struct S4 +{ + int const i; // { dg-message "should be initialized" } +}; + +struct C4 +{ + C4() {} // { dg-error "uninitialized const member" } + S4 s4[ 1 ]; +}; + +struct C5 +{ + C5() {} // { dg-message "uninitialized" } + int const iit[ 1 ]; +}; +