This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH 3/3] C: hints for missing stdlib includes for macros and types
- From: Jeff Law <law at redhat dot com>
- To: David Malcolm <dmalcolm at redhat dot com>, gcc-patches at gcc dot gnu dot org
- Date: Mon, 3 Jul 2017 10:30:47 -0600
- Subject: Re: [PATCH 3/3] C: hints for missing stdlib includes for macros and types
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=law at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 3F76AC04B936
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 3F76AC04B936
- References: <1494006675-28033-1-git-send-email-dmalcolm@redhat.com> <1494006675-28033-3-git-send-email-dmalcolm@redhat.com>
On 05/05/2017 11:51 AM, David Malcolm wrote:
> 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>'?
>
> If the patch to add fix-it hints for missing #includes is approved:
> https://gcc.gnu.org/ml/gcc-patches/2017-05/msg00321.html
> then it's trivial to add a fix-it hint to the note.
>
> 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.
OK once prereqs are approved.
FWIW, I'm getting a little concerned that we're adding a lot of overhead
to the error paths -- that often doesn't matter. But sometimes it does
(testcase reduction, errors with machine generated code, etc).
Something to be aware of.
jeff