From: Jonathan Wakely Date: Mon, 23 May 2011 08:15:16 +0000 (+0000) Subject: re PR c++/18016 (Warn about member variables initialized with itself) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=c11e39b0bdfc580582012fe56f0cce1b84f064f4;p=gcc.git re PR c++/18016 (Warn about member variables initialized with itself) 2011-05-23 Jonathan Wakely PR c++/18016 * init.c (perform_member_init): Check for self-initialization. From-SVN: r174058 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index e6399d364e9..20c98083e9f 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2011-05-23 Jonathan Wakely + + PR c++/18016 + * init.c (perform_member_init): Check for self-initialization. + 2011-05-22 Jason Merrill PR c++/48647 diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 7d7adbe0405..5f30275ae5b 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -501,6 +501,17 @@ perform_member_init (tree member, tree init) 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. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b1446bb7951..b87cb2aa51e 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2011-05-23 Jonathan Wakely + + PR c++/18016 + * g++.dg/warn/pr18016.C: New. + 2011-05-23 Tom de Vries PR target/45098 diff --git a/gcc/testsuite/g++.dg/warn/pr18016.C b/gcc/testsuite/g++.dg/warn/pr18016.C new file mode 100644 index 00000000000..9cf1ea3b70a --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/pr18016.C @@ -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" }