diff mbox

[C++,/,RFC] PR 51474

Message ID 5328A65E.9010401@oracle.com
State New
Headers show

Commit Message

Paolo Carlini March 18, 2014, 8:02 p.m. UTC
Hi,

the attached is slightly larger than my last ones but, assuming the 
analysis is correct, should be pretty safe, it avoids a segfault: in 
fact, for a pure virtual called from a NSDMI, current_function_decl is 
null and the existing warning about *structors breaks. I'm reading 
12.7/4 and I think we can solve the issue by just adding an 
appropriately worded warning. Tested x86_64-linux.

Thanks,
Paolo.

///////////////////

Comments

Jason Merrill March 19, 2014, 5:03 p.m. UTC | #1
OK.

Jason
diff mbox

Patch

Index: cp/call.c
===================================================================
--- cp/call.c	(revision 208658)
+++ cp/call.c	(working copy)
@@ -7828,15 +7828,20 @@  build_new_method_call_1 (tree instance, tree fns,
 	  if (!(flags & LOOKUP_NONVIRTUAL)
 	      && DECL_PURE_VIRTUAL_P (fn)
 	      && instance == current_class_ref
-	      && (DECL_CONSTRUCTOR_P (current_function_decl)
-		  || DECL_DESTRUCTOR_P (current_function_decl))
 	      && (complain & tf_warning))
-	    /* This is not an error, it is runtime undefined
-	       behavior.  */
-	    warning (0, (DECL_CONSTRUCTOR_P (current_function_decl) ?
-		      "pure virtual %q#D called from constructor"
-		      : "pure virtual %q#D called from destructor"),
-		     fn);
+	    {
+	      /* These is not an error, it is runtime undefined
+		 behavior.  */
+	      if (!current_function_decl)
+		warning (0, "pure virtual %q#D called from "
+			 "non-static data member initializer", fn);
+	      else if (DECL_CONSTRUCTOR_P (current_function_decl)
+		       || DECL_DESTRUCTOR_P (current_function_decl))
+		warning (0, (DECL_CONSTRUCTOR_P (current_function_decl)
+			     ? "pure virtual %q#D called from constructor"
+			     : "pure virtual %q#D called from destructor"),
+			 fn);
+	    }
 
 	  if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
 	      && is_dummy_object (instance))
Index: testsuite/g++.dg/cpp0x/nsdmi-virtual2.C
===================================================================
--- testsuite/g++.dg/cpp0x/nsdmi-virtual2.C	(revision 0)
+++ testsuite/g++.dg/cpp0x/nsdmi-virtual2.C	(working copy)
@@ -0,0 +1,8 @@ 
+// PR c++/51474
+// { dg-do compile { target c++11 } }
+
+struct A
+{
+  virtual int foo() = 0;
+  int i = foo();  // { dg-warning "pure virtual" }
+};