C++: -Wwrite-strings: use location of string constant
commit3cad0e37167b7e45857750afdda10054fb3a22c8
authordmalcolm <dmalcolm@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 17 Aug 2018 21:56:21 +0000 (17 21:56 +0000)
committerdmalcolm <dmalcolm@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 17 Aug 2018 21:56:21 +0000 (17 21:56 +0000)
treebeb75700fa738bc1c04197dd61012336a42fd914
parent4b947be3ed66982e68c3ecb8a0284bd49a224eac
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: svn+ssh://gcc.gnu.org/svn/gcc/trunk@263635 138bc75d-0d04-0410-961f-82ee72b054a4
gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/conversion/Wwrite-strings.C [new file with mode: 0644]