From 519e01e83bc8581639ed1d50b24a04683662e6c5 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 23 Jan 2025 11:26:37 -0500 Subject: [PATCH 95/98] FIXME: add suggest_macro_expansion_problem --- gcc/c-family/known-headers.cc | 39 +++++++++++++++++++++++++++++++++-- gcc/c-family/known-headers.h | 2 +- libcpp/include/cpplib.h | 2 +- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/gcc/c-family/known-headers.cc b/gcc/c-family/known-headers.cc index 283edbbd8601..bac9bf2f84d1 100644 --- a/gcc/c-family/known-headers.cc +++ b/gcc/c-family/known-headers.cc @@ -298,11 +298,41 @@ get_cp_stdlib_header_for_string_macro_name (const char *name) return get_string_macro_hint (name, STDLIB_CPLUSPLUS); } +/* Subclass of deferred_diagnostic for suggesting to the user + that something has gone wrong with expansion of a preprocessor macro. */ + +class suggest_macro_expansion_problem : public deferred_diagnostic +{ + public: + suggest_macro_expansion_problem (const cpp_hashnode *hashnode) + : deferred_diagnostic (cpp_macro_definition_location (hashnode)), + m_hashnode (hashnode) + { + gcc_assert (cpp_macro_p (hashnode)); + } + ~suggest_macro_expansion_problem () + { + if (is_suppressed_p ()) + return; + + location_t loc = get_location (); + if (loc <= BUILTINS_LOCATION) + return; + + inform (loc, + "%qs was defined as a preprocessor macro here", + NODE_NAME (m_hashnode)); + } + + private: + const cpp_hashnode *m_hashnode; +}; + /* Implementation of class suggest_missing_header. */ // FIXME -std::unique_ptr +std::unique_ptr suggest_missing_header::maybe_make (location_t loc, const char *name, const char *header_hint) { @@ -315,7 +345,12 @@ suggest_missing_header::maybe_make (location_t loc, const char *name, (const unsigned char *) (name), strlen (name))) if (cpp_macro_p (hashnode)) - return nullptr; // TODO: or a different suggestion + { + if (cpp_user_macro_p (hashnode)) + return std::make_unique (hashnode); + else + return nullptr; + } // FIXME: use should_suggest_as_macro_p ? diff --git a/gcc/c-family/known-headers.h b/gcc/c-family/known-headers.h index fc78e2670553..8c595d1a01c6 100644 --- a/gcc/c-family/known-headers.h +++ b/gcc/c-family/known-headers.h @@ -32,7 +32,7 @@ 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 + static std::unique_ptr maybe_make (location_t loc, const char *name, const char *header_hint); diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index bc0d7714e96f..f0152ef0c2f4 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -1276,7 +1276,7 @@ inline bool cpp_keyword_p (cpp_hashnode *node) extern const unsigned char *cpp_macro_definition (cpp_reader *, cpp_hashnode *); extern const unsigned char *cpp_macro_definition (cpp_reader *, cpp_hashnode *, const cpp_macro *); -inline location_t cpp_macro_definition_location (cpp_hashnode *node) +inline location_t cpp_macro_definition_location (const cpp_hashnode *node) { const cpp_macro *macro = node->value.macro; return macro ? macro->line : 0; -- 2.49.0