From: Gabriel Dos Reis Date: Sat, 13 Mar 2004 02:31:36 +0000 (+0000) Subject: backport: re PR c++/14401 (Uninitialized reference error not reported.) X-Git-Tag: releases/gcc-3.3.4~166 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=685db8b93746c7e49f69bfb9473c80905c4c073c;p=thirdparty%2Fgcc.git backport: re PR c++/14401 (Uninitialized reference error not reported.) Backport: 2004-03-08 Mark Mitchell PR c++/14401 * class.c (check_field_decls): Complain about non-static data members of reference type in unions. Propagate CLASSTYPE_REF_FIELDS_NEED_INIT and CLASSTYPE_READONLY_FIELDS_NEED_INIT from the types of non-static data members. * init.c (perform_member_init): Complain about members with const type that are not explicitly initialized. From-SVN: r79426 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 762f1472b19d..cf29f936899d 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,16 @@ +2004-03-12 Gabriel Dos Reis + + Backport: + 2004-03-08 Mark Mitchell + PR c++/14401 + * class.c (check_field_decls): Complain about non-static data + members of reference type in unions. Propagate + CLASSTYPE_REF_FIELDS_NEED_INIT and + CLASSTYPE_READONLY_FIELDS_NEED_INIT from the types of non-static + data members. + * init.c (perform_member_init): Complain about members with const + type that are not explicitly initialized. + 2004-03-12 Gabriel Dos Reis Backport: diff --git a/gcc/cp/class.c b/gcc/cp/class.c index 3546743f215c..0c5ace708f2a 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -3231,9 +3231,30 @@ check_field_decls (tree t, tree *access_decls, /* If we've gotten this far, it's a data member, possibly static, or an enumerator. */ - DECL_CONTEXT (x) = t; + /* When this goes into scope, it will be a non-local reference. */ + DECL_NONLOCAL (x) = 1; + + if (TREE_CODE (t) == UNION_TYPE) + { + /* [class.union] + + If a union contains a static data member, or a member of + reference type, the program is ill-formed. */ + if (TREE_CODE (x) == VAR_DECL) + { + cp_error_at ("`%D' may not be static because it is a member of a union", x); + continue; + } + if (TREE_CODE (type) == REFERENCE_TYPE) + { + cp_error_at ("`%D' may not have reference type `%T' because it is a member of a union", + x, type); + continue; + } + } + /* ``A local class cannot have static data members.'' ARM 9.4 */ if (current_function_decl && TREE_STATIC (x)) cp_error_at ("field `%D' in local class cannot be static", x); @@ -3263,21 +3284,9 @@ check_field_decls (tree t, tree *access_decls, if (type == error_mark_node) continue; - /* When this goes into scope, it will be a non-local reference. */ - DECL_NONLOCAL (x) = 1; - - if (TREE_CODE (x) == CONST_DECL) + if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL) continue; - if (TREE_CODE (x) == VAR_DECL) - { - if (TREE_CODE (t) == UNION_TYPE) - /* Unions cannot have static members. */ - cp_error_at ("field `%D' declared static in union", x); - - continue; - } - /* Now it can only be a FIELD_DECL. */ if (TREE_PRIVATE (x) || TREE_PROTECTED (x)) @@ -3308,6 +3317,14 @@ check_field_decls (tree t, tree *access_decls, if (TREE_CODE (type) == POINTER_TYPE) has_pointers = 1; + if (CLASS_TYPE_P (type)) + { + if (CLASSTYPE_REF_FIELDS_NEED_INIT (type)) + SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1); + if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type)) + SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1); + } + if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type)) CLASSTYPE_HAS_MUTABLE (t) = 1; diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 70085280290b..f3fa9aa93f79 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -390,6 +390,9 @@ perform_member_init (tree member, tree init) /* member traversal: note it leaves init NULL */ else if (TREE_CODE (type) == REFERENCE_TYPE) pedwarn ("uninitialized reference member `%D'", member); + else if (CP_TYPE_CONST_P (type)) + pedwarn ("uninitialized member '%D' with 'const' type '%T'", + member, type); } else if (TREE_CODE (init) == TREE_LIST) {