diff mbox

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

Message ID CAFk2RUY6V1C-O1mUj93Wkda9_zs_GXn-i6oWXiEO91x-9QxqJw@mail.gmail.com
State New
Headers show

Commit Message

Ville Voutilainen Nov. 25, 2014, 11:27 p.m. UTC
Tested on Linux-x64. Remotely related to pr50645, which we should
probably close as invalid.

/cp
2014-11-26  Ville Voutilainen  <ville.voutilainen@gmail.com>

    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.

/testsuite
2014-11-26  Ville Voutilainen  <ville.voutilainen@gmail.com>

    Diagnose string constant conversion to char* in c++11 and above
    as forbidden, not deprecated.
    * g++.dg/warn/write-strings-default.C: Adjust.
    * g++.dg/warn/write-strings.C: Adjust.

Comments

Jason Merrill Nov. 26, 2014, 2:41 a.m. UTC | #1
Tested how, exactly?  It doesn't seem to have run old-deja.exp.  I tend 
to use the toplevel make-c++ target.

> +FAIL: g++.old-deja/g++.bob/inherit1.C  -std=c++11 depr (test for warnings, line 14)
> +FAIL: g++.old-deja/g++.bob/inherit1.C  -std=c++11 (test for excess errors)
> +FAIL: g++.old-deja/g++.bob/inherit1.C  -std=c++14 depr (test for warnings, line 14)
> +FAIL: g++.old-deja/g++.bob/inherit1.C  -std=c++14 (test for excess errors)
> +FAIL: g++.old-deja/g++.brendan/template17.C  -std=c++11 depr (test for warnings, line 11)
> +FAIL: g++.old-deja/g++.brendan/template17.C  -std=c++11 (test for excess errors)
> +FAIL: g++.old-deja/g++.brendan/template17.C  -std=c++14 depr (test for warnings, line 11)
> +FAIL: g++.old-deja/g++.brendan/template17.C  -std=c++14 (test for excess errors)
> +FAIL: g++.old-deja/g++.law/temps1.C  -std=gnu++11 dep (test for warnings, line 19)
> +FAIL: g++.old-deja/g++.law/temps1.C  -std=gnu++11 (test for excess errors)
> +FAIL: g++.old-deja/g++.law/temps1.C  -std=gnu++14 dep (test for warnings, line 19)
> +FAIL: g++.old-deja/g++.law/temps1.C  -std=gnu++14 (test for excess errors)
> +FAIL: g++.old-deja/g++.martin/typedef2.C  -std=c++11 (test for excess errors)
> +FAIL: g++.old-deja/g++.martin/typedef2.C  -std=c++11  (test for warnings, line 6)
> +FAIL: g++.old-deja/g++.martin/typedef2.C  -std=c++14 (test for excess errors)
> +FAIL: g++.old-deja/g++.martin/typedef2.C  -std=c++14  (test for warnings, line 6)

Jason
Jason Merrill Nov. 26, 2014, 2:42 a.m. UTC | #2
On 11/25/2014 09:41 PM, Jason Merrill wrote:
> Tested how, exactly?  It doesn't seem to have run old-deja.exp.  I tend
> to use the toplevel make-c++ target.

Er, check-c++.

Jason
diff mbox

Patch

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 7156851..c7d422a 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -2139,12 +2139,24 @@  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 (warn) {
+    if (cxx_dialect >= cxx11)
+      {
+	if (pedantic)
+	  pedwarn (input_location, OPT_Wpedantic,
+		   "ISO C++ forbids converting a string constant to %qT",
+		   totype);
+	else
+	  warning (OPT_Wwrite_strings,
+		   "ISO C++ forbids converting a string constant to %qT",
+		   totype);
+      }
+    /* This warning is not very useful, as it complains about printf.  */
+    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" }
 }