Message ID | BANLkTi=ttoRS=h1j4oYbGGNWWsFft+v7kA@mail.gmail.com |
---|---|
State | New |
Headers | show |
On 05/23/2011 04:20 AM, Jonathan Wakely wrote: > I still think this warning shouldn't be tied to -Winit-self. That > isn't included in -Wall or -Wextra, and it implies we're saying that a > self-referencing mem-initializer is a GNU extension to silence a > warning, when I think it's always a mistake and never done > intentionally. I agree, but I think the answer to that is to enable -Winit-self with -Wextra in C++ mode. > Thanks, I've attached what I committed. I only checked > warn_init_self, not both warn_uninitialized&& warn_init_self, I think > that's right. I think so. > I've also just noticed that my change doesn't catch references which > are initialized with themselves. I won't try to fix that now, but > would that be done by checking for INDIRECT_REF? Yes, or you can use REFERENCE_REF_P. > I also had some thoughts about making init-self work for class types e.g. > std::string s = s; > Would checking if the copy constructor's parameter is *this be a > simple way to catch that? Well, in this snippet there's no *this involved, the parameter would be the decl being initialized. Jason
Index: cp/init.c =================================================================== --- cp/init.c (revision 174044) +++ cp/init.c (working copy) @@ -501,6 +501,17 @@ if (decl == error_mark_node) return; + if (warn_init_self && init && TREE_CODE (init) == TREE_LIST + && TREE_CHAIN (init) == NULL_TREE) + { + tree val = TREE_VALUE (init); + if (TREE_CODE (val) == COMPONENT_REF && TREE_OPERAND (val, 1) == member + && TREE_OPERAND (val, 0) == current_class_ref) + warning_at (DECL_SOURCE_LOCATION (current_function_decl), + OPT_Wuninitialized, "%qD is initialized with itself", + member); + } + if (init == void_type_node) { /* mem() means value-initialization. */ Index: testsuite/g++.dg/warn/pr18016.C =================================================================== --- testsuite/g++.dg/warn/pr18016.C (revision 0) +++ testsuite/g++.dg/warn/pr18016.C (revision 0) @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-Wuninitialized -Winit-self" } */ + +class X { + int i; + X() : i(i) { } // { dg-warning "initialized with itself" } + X(int i) : i(i) { } + X(const X& x) : i(x.i) { } +}; + +// { dg-prune-output "In constructor" }