C: hints for missing stdlib includes for macros and types
commit0911a8d3846dbc8b4a5f2848498befd7236c9d66
authordmalcolm <dmalcolm@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 21 Nov 2017 00:46:24 +0000 (21 00:46 +0000)
committerdmalcolm <dmalcolm@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 21 Nov 2017 00:46:24 +0000 (21 00:46 +0000)
tree21b4a30929c43eb96e39856f306ac8ee9bdbcf69
parentdbfb2c436f818142ab4d441f22cd1d9a8747af9e
C: hints for missing stdlib includes for macros and types

The C frontend already "knows" about many common functions in
the C standard library:

  test.c: In function 'test':
  test.c:3:3: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
     printf ("hello world\n");
     ^~~~~~
  test.c:3:3: warning: incompatible implicit declaration of built-in function 'printf'
  test.c:3:3: note: include '<stdio.h>' or provide a declaration of 'printf'

and which header file they are in.

However it doesn't know about various types and macros:

test.c:1:13: error: 'NULL' undeclared here (not in a function)
 void *ptr = NULL;
             ^~~~

This patch uses the name_hint/deferred_diagnostic machinery to
add hints for missing C standard library headers for some of the
most common type and macro names.

For example, the above becomes:
test.c:1:13: error: 'NULL' undeclared here (not in a function)
 void *ptr = NULL;
             ^~~~
test.c:1:13: note: 'NULL' is defined in header '<stddef.h>'; did you forget to '#include <stddef.h>'?

gcc/c/ChangeLog:
* c-decl.c (get_c_name_hint): New function.
(class suggest_missing_header): New class.
(lookup_name_fuzzy): Call get_c_name_hint and use it to
suggest missing headers to the user.

gcc/testsuite/ChangeLog:
* gcc.dg/spellcheck-stdlib.c: New test case.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@254979 138bc75d-0d04-0410-961f-82ee72b054a4
gcc/c/ChangeLog
gcc/c/c-decl.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/spellcheck-stdlib.c [new file with mode: 0644]