===================================================================
@@ -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
{
===================================================================
@@ -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" }
+ }
+}