]> git.ipfire.org Git - thirdparty/gcc.git/commit
C/C++: add fix-it hints for missing '&' and '*' (v5) [PR87850]
authorDavid Malcolm <dmalcolm@redhat.com>
Thu, 28 Nov 2024 00:21:15 +0000 (19:21 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Thu, 28 Nov 2024 00:21:15 +0000 (19:21 -0500)
commit5336b63fe81029cfd790208fbc69a08c70b82b01
treeef4e3d88378a00915f2a6e2a508781450aacbfe2
parent9f06b910a840d8ed06e27942bc23f260a0e0ccf3
C/C++: add fix-it hints for missing '&' and '*' (v5) [PR87850]

This patch adds a note with a fix-it hint to various
pointer-vs-non-pointer diagnostics, suggesting the addition of
a leading '&' or '*'.

For example, note the ampersand fix-it hint in the following:

demo.c: In function 'int main()':
demo.c:5:22: error: invalid conversion from 'pthread_key_t' {aka 'unsigned int'}
   to 'pthread_key_t*' {aka 'unsigned int*'} [-fpermissive]
    5 |   pthread_key_create(key, NULL);
      |                      ^~~
      |                      |
      |                      pthread_key_t {aka unsigned int}
demo.c:5:22: note: possible fix: take the address with '&'
    5 |   pthread_key_create(key, NULL);
      |                      ^~~
      |                      &
In file included from demo.c:1:
/usr/include/pthread.h:1122:47: note:   initializing argument 1 of
   'int pthread_key_create(pthread_key_t*, void (*)(void*))'
 1122 | extern int pthread_key_create (pthread_key_t *__key,
      |                                ~~~~~~~~~~~~~~~^~~~~

gcc/c-family/ChangeLog:
PR c++/87850
* c-common.cc: Include "gcc-rich-location.h".
(maybe_emit_indirection_note): New function.
* c-common.h (maybe_emit_indirection_note): New decl.
(compatible_types_for_indirection_note_p): New decl.

gcc/c/ChangeLog:
PR c++/87850
* c-typeck.cc (compatible_types_for_indirection_note_p): New
function.
(convert_for_assignment): Call maybe_emit_indirection_note for
pointer vs non-pointer diagnostics.

gcc/cp/ChangeLog:
PR c++/87850
* call.cc (convert_like_real): Call maybe_emit_indirection_note
for "invalid conversion" diagnostic.
* typeck.cc (compatible_types_for_indirection_note_p): New
function.

gcc/testsuite/ChangeLog:
PR c++/87850
* c-c++-common/indirection-fixits.c: New test.
* g++.dg/template/error60.C: Add fix-it hint to expected output.
* g++.dg/template/error60a.C: Likewise.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/c-family/c-common.cc
gcc/c-family/c-common.h
gcc/c/c-typeck.cc
gcc/cp/call.cc
gcc/cp/typeck.cc
gcc/testsuite/c-c++-common/indirection-fixits.c [new file with mode: 0644]
gcc/testsuite/g++.dg/template/error60.C
gcc/testsuite/g++.dg/template/error60a.C