From 1de7bfaf749dddf4e1e2840dbab6aee1559ace29 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Fri, 7 Apr 2017 20:18:16 -0400 Subject: [PATCH 08/10] FIXME: C FE: hints for missing stdlib includes --- gcc/c/c-decl.c | 79 ++++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/spellcheck-stdlib.c | 55 ++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/spellcheck-stdlib.c diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index d19f545..91ace02 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -3998,6 +3998,77 @@ lookup_name_in_scope (tree name, struct c_scope *scope) return NULL_TREE; } +/* Subroutine of suggest_missing_header::emit for handling unrecognized names + for some of the most common names within the C standard library. + Given non-NULL NAME, return the header name defining it within the C + standard library (without '<' and '>'), or NULL. */ + +static const char * +get_c_name_hint (const char *name) +{ + struct std_name_hint + { + const char *name; + const char *header; + }; + static const std_name_hint hints[] = { + /* . */ + {"errno", "errno.h"}, + + /* . */ + {"va_list", "stdarg.h"}, + + /* . */ + {"NULL", "stddef.h"}, + {"ptrdiff_t", "stddef.h"}, + {"wchar_t", "stddef.h"}, + {"size_t", "stddef.h"}, + + /* . */ + {"BUFSIZ", "stdio.h"}, + {"EOF", "stdio.h"}, + {"FILE", "stdio.h"}, + {"FILENAME_MAX", "stdio.h"}, + {"fpos_t", "stdio.h"}, + {"stderr", "stdio.h"}, + {"stdin", "stdio.h"}, + {"stdout", "stdio.h"} + }; + const size_t num_hints = sizeof (hints) / sizeof (hints[0]); + for (size_t i = 0; i < num_hints; i++) + { + if (0 == strcmp (name, hints[i].name)) + return hints[i].header; + } + /* FIXME: unify this with one in C++. */ + return NULL; +} + +/* FIXME. */ + +class suggest_missing_header : public deferred_diagnostic +{ + public: + suggest_missing_header (const char *name, const char *header_hint) + : m_name_str (name), m_header_hint (header_hint) + { + gcc_assert (name); + gcc_assert (header_hint); + } + + void emit () + { + inform (input_location, + "%<%s%> is defined in header %<<%s>%>;" + " did you forget to %<#include <%s>%>?", + m_name_str, m_header_hint, m_header_hint); + } + + private: + const char *m_name_str; + const char *m_header_hint; +}; + /* Look for the closest match for NAME within the currently valid scopes. @@ -4019,6 +4090,14 @@ lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind) { gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); + /* First, try some well-known names in the C standard library, in case + the user forgot a #include. */ + const char *header_hint = get_c_name_hint (IDENTIFIER_POINTER (name)); + if (header_hint) + return name_hint (NULL, + new suggest_missing_header (IDENTIFIER_POINTER (name), + header_hint)); + best_match bm (name); /* Look within currently valid scopes. */ diff --git a/gcc/testsuite/gcc.dg/spellcheck-stdlib.c b/gcc/testsuite/gcc.dg/spellcheck-stdlib.c new file mode 100644 index 0000000..85a21c3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/spellcheck-stdlib.c @@ -0,0 +1,55 @@ +/* Missing . */ + +void *ptr = NULL; /* { dg-error "'NULL' undeclared here" } */ +/* { dg-message "'NULL' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ + +ptrdiff_t pd; /* { dg-error "unknown type name 'ptrdiff_t'" } */ +/* { dg-message "'ptrdiff_t' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ + +wchar_t wc; /* { dg-error "unknown type name 'wchar_t'" } */ +/* { dg-message "'wchar_t' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ + +size_t sz; /* { dg-error "unknown type name 'size_t'" } */ +/* { dg-message "'size_t' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ + +/* Missing . */ + +void test_stdio_h (void) +{ + FILE *f; /* { dg-error "unknown type name 'FILE'" } */ + /* { dg-message "'FILE' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ + + char buf[BUFSIZ]; /* { dg-error "'BUFSIZ' undeclared" } */ + /* { dg-message "'BUFSIZ' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ + + char buf2[FILENAME_MAX]; /* { dg-error "'FILENAME_MAX' undeclared" } */ + /* { dg-message "'FILENAME_MAX' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ + + stderr; /* { dg-error "'stderr' undeclared" } */ + /* { dg-message "'stderr' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ + + stdin; /* { dg-error "'stdin' undeclared" } */ + /* { dg-message "'stdin' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ + + stdout; /* { dg-error "'stdout' undeclared" } */ + /* { dg-message "'stdout' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ + + EOF; /* { dg-error "'EOF' undeclared" } */ + /* { dg-message "'EOF' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ +} + +/* Missing . */ + +int test_errno_h (void) +{ + return errno; /* { dg-error "'errno' undeclared" } */ + /* { dg-message "'errno' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ +} + +/* Missing . */ + +void test_stdarg_h (void) +{ + va_list ap; /* { dg-error "unknown type name 'va_list'" } */ + /* { dg-message "'va_list' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ +} -- 1.8.5.3