aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2018-08-17 21:56:21 +0000
committerDavid Malcolm <dmalcolm@redhat.com>2018-08-17 21:56:21 +0000
commit6d9274ce62fb7f0bdeda170656eb1383e18e7446 (patch)
treebeb75700fa738bc1c04197dd61012336a42fd914
parentc56484a19fc94604c9381e5318b7cb1db3110db9 (diff)
C++: -Wwrite-strings: use location of string constant
Consider: extern int callee (const char *one, char *two, const char *three); int test () { return callee ("first", "second", "third"); } for which -Wwrite-strings was emitting: Wwrite-strings.C: In function 'int test()': Wwrite-strings.C:10:44: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] 10 | return callee ("first", "second", "third"); | ^ This patch fixes the warning so that it underlines the pertinent argument at the callsite: Wwrite-strings.C: In function 'int test()': Wwrite-strings.C:10:27: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] 10 | return callee ("first", "second", "third"); | ^~~~~~~~ Ideally we ought to also issue a "note" highlighting the pertinent parameter within the decl, but that's not readily available, so I'm saving it for another patch. gcc/cp/ChangeLog: * typeck.c (string_conv_p): Extract location from EXP and use it in preference to input_location when issuing warnings. gcc/testsuite/ChangeLog: * g++.dg/conversion/Wwrite-strings.C: New test. git-svn-id: https://gcc.gnu.org/svn/gcc/trunk@263635 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r--gcc/cp/ChangeLog5
-rw-r--r--gcc/cp/typeck.c10
-rw-r--r--gcc/testsuite/ChangeLog4
-rw-r--r--gcc/testsuite/g++.dg/conversion/Wwrite-strings.C24
4 files changed, 39 insertions, 4 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 0b695907cf1..d8be93089b4 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,8 @@
+2018-08-17 David Malcolm <dmalcolm@redhat.com>
+
+ * typeck.c (string_conv_p): Extract location from EXP and use it
+ in preference to input_location when issuing warnings.
+
2018-08-15 David Malcolm <dmalcolm@redhat.com>
* call.c: Include "gcc-rich-location.h".
diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 64b3d583356..8c13ae9b19b 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -2208,6 +2208,8 @@ string_conv_p (const_tree totype, const_tree exp, int warn)
&& !same_type_p (t, wchar_type_node))
return 0;
+ location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
+
STRIP_ANY_LOCATION_WRAPPER (exp);
if (TREE_CODE (exp) == STRING_CST)
@@ -2230,13 +2232,13 @@ string_conv_p (const_tree totype, const_tree exp, int warn)
if (warn)
{
if (cxx_dialect >= cxx11)
- pedwarn (input_location, OPT_Wwrite_strings,
+ pedwarn (loc, 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);
+ warning_at (loc, OPT_Wwrite_strings,
+ "deprecated conversion from string constant to %qT",
+ totype);
}
return 1;
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index ea1f1fa6061..b8677d729bc 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2018-08-17 David Malcolm <dmalcolm@redhat.com>
+
+ * g++.dg/conversion/Wwrite-strings.C: New test.
+
2018-08-17 Sandra Loosemore <sandra@codesourcery.com>
Chung-Lin Tang <cltang@codesourcery.com>
Xianmiao Qu <xianmiao_qu@c-sky.com>
diff --git a/gcc/testsuite/g++.dg/conversion/Wwrite-strings.C b/gcc/testsuite/g++.dg/conversion/Wwrite-strings.C
new file mode 100644
index 00000000000..f6dbb150699
--- /dev/null
+++ b/gcc/testsuite/g++.dg/conversion/Wwrite-strings.C
@@ -0,0 +1,24 @@
+// { dg-options "-fdiagnostics-show-caret" }
+
+/* Verify that -Wwrite-strings underlines the string literal in question. */
+
+extern int callee (const char *one, char *two, const char *three);
+
+int test_1 ()
+{
+ return callee ("first", "second", "third"); // { dg-warning "string constant to 'char\\*'" }
+ /* { dg-begin-multiline-output "" }
+ return callee ("first", "second", "third");
+ ^~~~~~~~
+ { dg-end-multiline-output "" } */
+ // TODO: underline the pertinent param in the decl of callee
+}
+
+char *test_2 (void)
+{
+ return "foo"; // { dg-warning "string constant to 'char\\*'" }
+ /* { dg-begin-multiline-output "" }
+ return "foo";
+ ^~~~~
+ { dg-end-multiline-output "" } */
+}