From 8be2ce257e49c440df49313116f59b805cc97259 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Mon, 24 Nov 2014 13:35:08 +0000 Subject: [PATCH] re PR c++/63203 (Self-initialization of reference not diagnosed if it occurs within a loop) /cp 2014-11-24 Jonathan Wakely Paolo Carlini PR c++/63203 * decl.c (initialize_local_var): Add -Winit-self warning for references initialized with themselves. /testsuite 2014-11-24 Jonathan Wakely Paolo Carlini PR c++/63203 * g++.dg/warn/Winit-self-2.C: New. Co-Authored-By: Paolo Carlini From-SVN: r218017 --- gcc/cp/ChangeLog | 7 +++++++ gcc/cp/decl.c | 16 +++++++++++++--- gcc/testsuite/ChangeLog | 6 ++++++ gcc/testsuite/g++.dg/warn/Winit-self-2.C | 12 ++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Winit-self-2.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index d23bad5b914..7f17929c20a 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2014-11-24 Jonathan Wakely + Paolo Carlini + + PR c++/63203 + * decl.c (initialize_local_var): Add -Winit-self warning for + references initialized with themselves. + 2014-11-24 Jason Merrill PR c++/63942 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 225d4089882..4ce4645679e 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 07b00cf2bb4..a9ac86bd33c 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2014-11-24 Jonathan Wakely + Paolo Carlini + + PR c++/63203 + * g++.dg/warn/Winit-self-2.C: New. + 2014-11-24 Petr Murzin * 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 00000000000..a619d5a0b42 --- /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" } + } +} -- 2.30.2