From 6d4ef19c46b8dd1d629b70c7603985e3a001a187 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 23 Jan 2025 10:43:09 -0500 Subject: [PATCH 94/98] FIXME: add suggest_missing_header::maybe_make --- gcc/c-family/known-headers.cc | 23 +++++++++++++++++++++++ gcc/c-family/known-headers.h | 4 ++++ gcc/c/c-decl.cc | 6 +++--- gcc/c/c-parser.cc | 7 +++---- gcc/cp/name-lookup.cc | 6 +++--- gcc/cp/parser.cc | 6 +++--- 6 files changed, 39 insertions(+), 13 deletions(-) diff --git a/gcc/c-family/known-headers.cc b/gcc/c-family/known-headers.cc index 36f3f88601fb..283edbbd8601 100644 --- a/gcc/c-family/known-headers.cc +++ b/gcc/c-family/known-headers.cc @@ -23,6 +23,7 @@ along with GCC; see the file COPYING3. If not see #include "c-family/c-common.h" #include "c-family/name-hint.h" #include "c-family/known-headers.h" +#include "c-family/c-pragma.h" #include "gcc-rich-location.h" #include "opts.h" @@ -299,6 +300,28 @@ get_cp_stdlib_header_for_string_macro_name (const char *name) /* Implementation of class suggest_missing_header. */ +// FIXME + +std::unique_ptr +suggest_missing_header::maybe_make (location_t loc, const char *name, + const char *header_hint) +{ + // TODO: check to see if this header was already included, + // in which case the hint is likely to be meaningless + + /* If NAME is a macro, then we likely have a macro expansion problem + rather than a missing include. */ + if (const cpp_hashnode *hashnode = cpp_lookup (parse_in, + (const unsigned char *) (name), + strlen (name))) + if (cpp_macro_p (hashnode)) + return nullptr; // TODO: or a different suggestion + + // FIXME: use should_suggest_as_macro_p ? + + return std::make_unique (loc, name, header_hint); +} + /* suggest_missing_header's ctor. */ suggest_missing_header::suggest_missing_header (location_t loc, diff --git a/gcc/c-family/known-headers.h b/gcc/c-family/known-headers.h index 72fe3d745226..fc78e2670553 100644 --- a/gcc/c-family/known-headers.h +++ b/gcc/c-family/known-headers.h @@ -32,6 +32,10 @@ extern const char *get_cp_stdlib_header_for_string_macro_name (const char *n); class suggest_missing_header : public deferred_diagnostic { public: + static std::unique_ptr + maybe_make (location_t loc, const char *name, + const char *header_hint); + suggest_missing_header (location_t loc, const char *name, const char *header_hint); ~suggest_missing_header (); diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 98249b922695..4c352eec1dac 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -4689,9 +4689,9 @@ lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc) if (header_hint) return name_hint (nullptr, - std::make_unique (loc, - IDENTIFIER_POINTER (name), - header_hint)); + suggest_missing_header::maybe_make (loc, + IDENTIFIER_POINTER (name), + header_hint)); /* Next, look for exact matches for builtin defines that would have been defined if the user had passed a command-line option (e.g. -fopenmp diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index 0abdeedb3bc5..071dd655bdfb 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -1162,10 +1162,9 @@ c_parser_error_richloc (c_parser *parser, const char *gmsgid, = get_c_stdlib_header_for_string_macro_name (token_name); if (header_hint != NULL) h = name_hint (nullptr, - std::make_unique - (token->location, - token_name, - header_hint)); + suggest_missing_header::maybe_make (token->location, + token_name, + header_hint)); } c_parse_error (gmsgid, diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 524e1dfd470f..3271238839d2 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -8060,9 +8060,9 @@ lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc) if (header_hint) return name_hint (nullptr, - std::make_unique (loc, - IDENTIFIER_POINTER (name), - header_hint)); + suggest_missing_header::maybe_make (loc, + IDENTIFIER_POINTER (name), + header_hint)); best_match bm (name); diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 5c9ba69d12f3..d1a87d54e41d 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -3515,9 +3515,9 @@ cp_parser_error_1 (cp_parser* parser, const char* gmsgid, if (header_hint != NULL) h = name_hint (nullptr, - std::make_unique (token->location, - token_name, - header_hint)); + suggest_missing_header::maybe_make (token->location, + token_name, + header_hint)); } /* Actually emit the error. */ -- 2.49.0