Index: gcc/doc/invoke.texi =================================================================== --- gcc/doc/invoke.texi (revision 120173) +++ gcc/doc/invoke.texi (working copy) @@ -192,7 +192,7 @@ in the following sections. -Weffc++ -Wno-deprecated -Wstrict-null-sentinel @gol -Wno-non-template-friend -Wold-style-cast @gol -Woverloaded-virtual -Wno-pmf-conversions @gol --Wsign-promo} +-Wsign-promo -Wuninitializable-member} @item Objective-C and Objective-C++ Language Options @xref{Objective-C and Objective-C++ Dialect Options,,Options Controlling @@ -2935,7 +2935,7 @@ An enumerator and a non-enumerator both @item @r{(C++ only)} A non-static reference or non-static @samp{const} member appears in a -class without constructors. +class without constructors. This warning can be independently controlled by @option{-Wuninitializable-member}. @item @r{(C++ only)} Ambiguous virtual bases. @@ -3187,6 +3187,12 @@ an incorrect result when the signed valu This warning is also enabled by @option{-Wextra}; to get the other warnings of @option{-Wextra} without this warning, use @samp{-Wextra -Wno-sign-compare}. +@item -Wuninitializable-member @r{(C++ only)} +@opindex Wuninitializable-member +A non-static reference or non-static @samp{const} member appears in a +class without constructors. This warning is also enabled by +@option{-Wextra}. + @item -Waggregate-return @opindex Waggregate-return Warn if any functions that return structures or unions are defined or Index: gcc/testsuite/g++.dg/warn/Wuninitializable-member.C =================================================================== --- gcc/testsuite/g++.dg/warn/Wuninitializable-member.C (revision 0) +++ gcc/testsuite/g++.dg/warn/Wuninitializable-member.C (revision 0) @@ -0,0 +1,15 @@ + +// { dg-do compile } +// { dg-options "-Wuninitializable-member" } + +class X { + int & flag;// { dg-warning "non-static reference 'int& X::flag' in class without a constructor" } +public: + void f(){ flag++ ; } +}; + +class Y { + const int var;// { dg-warning "non-static const member 'const int Y::var' in class without a constructor" } +public: + int g(){ return 2*var; } +}; Index: gcc/testsuite/g++.dg/warn/Wuninitializable-member-Wextra.C =================================================================== --- gcc/testsuite/g++.dg/warn/Wuninitializable-member-Wextra.C (revision 0) +++ gcc/testsuite/g++.dg/warn/Wuninitializable-member-Wextra.C (revision 0) @@ -0,0 +1,15 @@ +// Test -Wuninitializable-member is enabled by -Wextra +// { dg-do compile } +// { dg-options "-Wextra" } + +class X { + int & flag;// { dg-warning "non-static reference 'int& X::flag' in class without a constructor" } +public: + void f(){ flag++ ; } +}; + +class Y { + const int var;// { dg-warning "non-static const member 'const int Y::var' in class without a constructor" } +public: + int g(){ return 2*var; } +}; Index: gcc/testsuite/g++.dg/warn/Wuninitializable-member-no.C =================================================================== --- gcc/testsuite/g++.dg/warn/Wuninitializable-member-no.C (revision 0) +++ gcc/testsuite/g++.dg/warn/Wuninitializable-member-no.C (revision 0) @@ -0,0 +1,15 @@ +// Test disabling -Wuninitializable-member +// { dg-do compile } +// { dg-options "-Wextra -Wno-uninitializable-member" } + +class X { + int & flag;// { dg-bogus "non-static reference 'int& X::flag' in class without a constructor" } +public: + void f(){ flag++ ; } +}; + +class Y { + const int var;// { dg-bogus "non-static const member 'const int Y::var' in class without a constructor" } +public: + int g(){ return 2*var; } +}; Index: gcc/cp/class.c =================================================================== --- gcc/cp/class.c (revision 120173) +++ gcc/cp/class.c (working copy) @@ -2907,9 +2907,8 @@ check_field_decls (tree t, tree *access_ members. */ TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1; - if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t) - && extra_warnings) - warning (OPT_Wextra, "non-static reference %q+#D in class without a constructor", x); + if (!TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)) + warning (OPT_Wuninitializable_member, "non-static reference %q+#D in class without a constructor", x); } type = strip_array_types (type); @@ -2985,9 +2984,8 @@ check_field_decls (tree t, tree *access_ members. */ TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1; - if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t) - && extra_warnings) - warning (OPT_Wextra, "non-static const member %q+#D in class without a constructor", x); + if (!TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)) + warning (OPT_Wuninitializable_member, "non-static const member %q+#D in class without a constructor", x); } /* A field that is pseudo-const makes the structure likewise. */ else if (CLASS_TYPE_P (type)) Index: gcc/c.opt =================================================================== --- gcc/c.opt (revision 120173) +++ gcc/c.opt (working copy) @@ -427,6 +427,10 @@ Wundef C ObjC C++ ObjC++ Warn if an undefined macro is used in an #if directive +Wuninitializable-member +C++ ObjC++ Var(warn_uninitializable_member) Init(-1) +Warn for non-static reference or const member in a class without constructors + Wunknown-pragmas C ObjC C++ ObjC++ Warn about unrecognized pragmas Index: gcc/c-opts.c =================================================================== --- gcc/c-opts.c (revision 120173) +++ gcc/c-opts.c (working copy) @@ -1026,7 +1026,8 @@ c_common_post_options (const char **pfil flag_exceptions = 1; /* -Wextra implies -Wclobbered, -Wempty-body, -Wsign-compare, - -Wmissing-field-initializers, -Wmissing-parameter-type and -Woverride-init, + -Wmissing-field-initializers, -Wmissing-parameter-type, + -Woverride-init and -Wuninitializable-member, but not if explicitly overridden. */ if (warn_clobbered == -1) warn_clobbered = extra_warnings; @@ -1040,6 +1041,8 @@ c_common_post_options (const char **pfil warn_missing_parameter_type = extra_warnings; if (warn_override_init == -1) warn_override_init = extra_warnings; + if (warn_uninitializable_member == -1) + warn_uninitializable_member = extra_warnings; /* -Wpointer_sign is disabled by default, but it is enabled if any of -Wall or -pedantic are given. */