diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2014-11-24 13:35:08 +0000 |
---|---|---|
committer | Paolo Carlini <paolo@gcc.gnu.org> | 2014-11-24 13:35:08 +0000 |
commit | 8be2ce257e49c440df49313116f59b805cc97259 (patch) | |
tree | bd9653f692b6298911e2f6dd5ac0385951218b7b /gcc | |
parent | f9b1eec21582f7801a8ca57463080331db95f2fb (diff) | |
download | gcc-8be2ce257e49c440df49313116f59b805cc97259.zip gcc-8be2ce257e49c440df49313116f59b805cc97259.tar.gz gcc-8be2ce257e49c440df49313116f59b805cc97259.tar.bz2 |
re PR c++/63203 (Self-initialization of reference not diagnosed if it occurs within a loop)
/cp
2014-11-24 Jonathan Wakely <jwakely@redhat.com>
Paolo Carlini <paolo.carlini@oracle.com>
PR c++/63203
* decl.c (initialize_local_var): Add -Winit-self warning for
references initialized with themselves.
/testsuite
2014-11-24 Jonathan Wakely <jwakely@redhat.com>
Paolo Carlini <paolo.carlini@oracle.com>
PR c++/63203
* g++.dg/warn/Winit-self-2.C: New.
Co-Authored-By: Paolo Carlini <paolo.carlini@oracle.com>
From-SVN: r218017
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/ChangeLog | 7 | ||||
-rw-r--r-- | gcc/cp/decl.c | 16 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/warn/Winit-self-2.C | 12 |
4 files changed, 38 insertions, 3 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index d23bad5..7f17929 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2014-11-24 Jonathan Wakely <jwakely@redhat.com> + Paolo Carlini <paolo.carlini@oracle.com> + + PR c++/63203 + * decl.c (initialize_local_var): Add -Winit-self warning for + references initialized with themselves. + 2014-11-24 Jason Merrill <jason@redhat.com> PR c++/63942 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 225d408..4ce4645 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -6132,13 +6132,23 @@ initialize_local_var (tree decl, tree init) /* Perform the initialization. */ if (init) { - if (TREE_CODE (init) == INIT_EXPR - && !TREE_SIDE_EFFECTS (TREE_OPERAND (init, 1))) + tree rinit = (TREE_CODE (init) == INIT_EXPR + ? TREE_OPERAND (init, 1) : NULL_TREE); + if (rinit && !TREE_SIDE_EFFECTS (rinit)) { /* Stick simple initializers in DECL_INITIAL so that -Wno-init-self works (c++/34772). */ gcc_assert (TREE_OPERAND (init, 0) == decl); - DECL_INITIAL (decl) = TREE_OPERAND (init, 1); + DECL_INITIAL (decl) = rinit; + + if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE) + { + STRIP_NOPS (rinit); + if (rinit == decl) + warning_at (DECL_SOURCE_LOCATION (decl), + OPT_Winit_self, + "reference %qD is initialized with itself", decl); + } } else { diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 07b00cf..a9ac86b 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2014-11-24 Jonathan Wakely <jwakely@redhat.com> + Paolo Carlini <paolo.carlini@oracle.com> + + PR c++/63203 + * g++.dg/warn/Winit-self-2.C: New. + 2014-11-24 Petr Murzin <petr.murzin@intel.com> * gcc.target/i386/extract-insert-combining.c: New test. diff --git a/gcc/testsuite/g++.dg/warn/Winit-self-2.C b/gcc/testsuite/g++.dg/warn/Winit-self-2.C new file mode 100644 index 0000000..a619d5a --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Winit-self-2.C @@ -0,0 +1,12 @@ +// PR c++/63203 +// { dg-options "-Winit-self" } + +struct string { }; + +int main() +{ + for (int ii = 0; ii < 1; ++ii) + { + const string& str = str; // { dg-warning "is initialized with itself" } + } +} |