diff mbox

[C++] PR 63203

Message ID 5472F14F.9010301@oracle.com
State New
Headers show

Commit Message

Paolo Carlini Nov. 24, 2014, 8:50 a.m. UTC
Hi,

in the audit trail Jon argued that we should have code specifically 
checking for references initialized with themselves (because one can't 
rebind references). I added to his draft a STRIP_NOPS, which manages to 
get back to the decl on the right hand side which we are looking for + 
minor tweaks. To make sure that the patch is safe from the false 
positives point of view, I also ran the testsuite with -Winit-self 
enabled by default and only g++.dg/init/ref6.C, correctly, showed up. 
Tested x86_64-linux.

Thanks,
Paolo.

////////////////////
/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.

Comments

Jason Merrill Nov. 24, 2014, 12:52 p.m. UTC | #1
OK.

Jason
diff mbox

Patch

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 217980)
+++ cp/decl.c	(working copy)
@@ -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
 	{
Index: testsuite/g++.dg/warn/Winit-self-2.C
===================================================================
--- testsuite/g++.dg/warn/Winit-self-2.C	(revision 0)
+++ testsuite/g++.dg/warn/Winit-self-2.C	(working copy)
@@ -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" }
+  }
+}