diff mbox

[C++] Diagnose conversions from string constants to char* as forbidden, not deprecated, in c++11 and above

Message ID 5475F8E2.6080805@redhat.com
State New
Headers show

Commit Message

Jason Merrill Nov. 26, 2014, 3:59 p.m. UTC
On 11/26/2014 10:48 AM, Manuel López-Ibáñez wrote:
> If I understand correctly, the cxx11 standard requires to diagnose
> this. Thus, it should be always a pedwarn, independently of Wpedantic.

I agree.  This is what I checked in:
diff mbox

Patch

commit 2aeccf739475ee181f4ca6422776b46bc9526352
Author: jason <jason@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Wed Nov 26 15:16:07 2014 +0000

    	Diagnose string constant conversion to char* in c++11 and above
    	as forbidden, not deprecated.
    	* typeck.c (string_conv_p): Do a pedwarn in c++11 and above,
    	change the diagnostic for the Wwrite-strings case for c++11 and above.
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@218087 138bc75d-0d04-0410-961f-82ee72b054a4

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index e100d70..8b66acc 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -2139,12 +2139,18 @@  string_conv_p (const_tree totype, const_tree exp, int warn)
 	  || TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
 	return 0;
     }
-
-  /* This warning is not very useful, as it complains about printf.  */
   if (warn)
-    warning (OPT_Wwrite_strings,
-	     "deprecated conversion from string constant to %qT",
-	     totype);
+    {
+      if (cxx_dialect >= cxx11)
+	pedwarn (input_location,
+		 pedantic ? OPT_Wpedantic : OPT_Wwrite_strings,
+		 "ISO C++ forbids converting a string constant to %qT",
+		 totype);
+      else
+	warning (OPT_Wwrite_strings,
+		 "deprecated conversion from string constant to %qT",
+		 totype);
+    }
 
   return 1;
 }
diff --git a/gcc/testsuite/g++.dg/warn/write-strings-default.C b/gcc/testsuite/g++.dg/warn/write-strings-default.C
index ee6b217..063b303 100644
--- a/gcc/testsuite/g++.dg/warn/write-strings-default.C
+++ b/gcc/testsuite/g++.dg/warn/write-strings-default.C
@@ -3,5 +3,5 @@ 
 
 int main()
 {
-   char* p = "Asgaard";         // { dg-warning "deprecated" }
+   char* p = "Asgaard";         // { dg-warning "deprecated|forbids converting a string constant" }
 }
diff --git a/gcc/testsuite/g++.dg/warn/write-strings.C b/gcc/testsuite/g++.dg/warn/write-strings.C
index 73c8149..1293e85 100644
--- a/gcc/testsuite/g++.dg/warn/write-strings.C
+++ b/gcc/testsuite/g++.dg/warn/write-strings.C
@@ -3,5 +3,5 @@ 
 
 int main()
 {
-   char* p = "Asgaard";         // { dg-warning "deprecated" }
+   char* p = "Asgaard";         // { dg-warning "deprecated|forbids converting a string constant" }
 }
diff --git a/gcc/testsuite/g++.old-deja/g++.bob/inherit1.C b/gcc/testsuite/g++.old-deja/g++.bob/inherit1.C
index e75190b..c037a1c 100644
--- a/gcc/testsuite/g++.old-deja/g++.bob/inherit1.C
+++ b/gcc/testsuite/g++.old-deja/g++.bob/inherit1.C
@@ -12,7 +12,7 @@  public:
 class B : public A {
 public:
     char* m1 () { C::m1(); return ""; } // { dg-error "cannot call" } 
-    // { dg-warning "deprecated" "depr" { target *-*-* } 14 }
+    // { dg-warning "deprecated|forbids converting a string constant" "depr" { target *-*-* } 14 }
 };
 
 int main () {
diff --git a/gcc/testsuite/g++.old-deja/g++.brendan/template17.C b/gcc/testsuite/g++.old-deja/g++.brendan/template17.C
index 94eaf3d..b8e2a0b 100644
--- a/gcc/testsuite/g++.old-deja/g++.brendan/template17.C
+++ b/gcc/testsuite/g++.old-deja/g++.brendan/template17.C
@@ -10,6 +10,6 @@  public:
 
 const Regex NDAMName<'L'>::pattern("^[Ll](.*)$",   1);// { dg-error "type/value mismatch" "mismatch" }
 // { dg-message "expected a type" "expected" { target *-*-* } 11 }
-// { dg-warning "deprecated" "depr" { target *-*-* } 11 }
+// { dg-warning "deprecated|forbids converting a string constant" "depr" { target *-*-* } 11 }
 unsigned NDAMName<'L'>::sequence_number = 0;// { dg-error "type/value mismatch" "mismatch" }
 // { dg-message "expected a type" "exp" { target *-*-* } 14 }
diff --git a/gcc/testsuite/g++.old-deja/g++.law/temps1.C b/gcc/testsuite/g++.old-deja/g++.law/temps1.C
index bd344b4..5734210 100644
--- a/gcc/testsuite/g++.old-deja/g++.law/temps1.C
+++ b/gcc/testsuite/g++.old-deja/g++.law/temps1.C
@@ -16,5 +16,5 @@  struct cookie
   cookie ( foo * x) { v=x; }
 };
 
-cookie cat(&foo("apabepa"));// { dg-warning "deprecated conversion" "dep" }
+cookie cat(&foo("apabepa"));// { dg-warning "deprecated conversion|forbids converting a string constant" "dep" }
 // { dg-warning "taking address of temporary" "add" { target *-*-* } 19 }
diff --git a/gcc/testsuite/g++.old-deja/g++.martin/typedef2.C b/gcc/testsuite/g++.old-deja/g++.martin/typedef2.C
index fa31867..99603cf 100644
--- a/gcc/testsuite/g++.old-deja/g++.martin/typedef2.C
+++ b/gcc/testsuite/g++.old-deja/g++.martin/typedef2.C
@@ -3,5 +3,5 @@ 
 // Check implicit conversion from string constants into typedefs
 
 typedef char CHAR;
-void f2(CHAR *s="");		// { dg-warning "deprecated" }
+void f2(CHAR *s="");		// { dg-warning "deprecated|forbids converting a string constant" }