C++: -Wwrite-strings: use location of string constant
authorDavid Malcolm <dmalcolm@redhat.com>
Fri, 17 Aug 2018 21:56:21 +0000 (21:56 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Fri, 17 Aug 2018 21:56:21 +0000 (21:56 +0000)
commit505ce5abd48385f1b415b6ed0aed80e5b378249c
treebeb75700fa738bc1c04197dd61012336a42fd914
parent8bdea5282fffac0643c4111482e85cfa7c6f7e9f
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.

From-SVN: r263635
gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/conversion/Wwrite-strings.C [new file with mode: 0644]