diff options
author | David Malcolm <dmalcolm@redhat.com> | 2024-11-27 19:21:15 -0500 |
---|---|---|
committer | David Malcolm <dmalcolm@redhat.com> | 2024-11-27 19:21:15 -0500 |
commit | 5341eb669658c7c73d55021b10a4765bf4ce3078 (patch) | |
tree | 4fcf4200563d9bba8e033d3be34bd1fbbb1aadb2 /gcc/c-family | |
parent | 5336b63fe81029cfd790208fbc69a08c70b82b01 (diff) | |
download | gcc-5341eb669658c7c73d55021b10a4765bf4ce3078.zip gcc-5341eb669658c7c73d55021b10a4765bf4ce3078.tar.gz gcc-5341eb669658c7c73d55021b10a4765bf4ce3078.tar.bz2 |
c-family: offer suggestions for missing command-line options [PR82892]
Some builtin macros are only defined when certain command-line options
are provided. Update the error messages for them so that we suggest
the pertinent option ('-fopenacc' for '_OPENACC', and '-fopenmp' for
'_OPENMP')
gcc/c-family/ChangeLog:
PR c/82892
* c-common.h (get_option_for_builtin_define): New decl.
* c-cppbuiltin.cc (get_option_for_builtin_define): New.
* known-headers.cc: Include "opts.h".
(suggest_missing_option::suggest_missing_option): New.
(suggest_missing_option::~suggest_missing_option): New.
* known-headers.h (class suggest_missing_option): New.
gcc/c/ChangeLog:
PR c/82892
* c-decl.cc (lookup_name_fuzzy): Provide hints for missing
command-line options.
gcc/cp/ChangeLog:
PR c/82892
* name-lookup.cc (suggest_alternatives_for_1): Provide hints for
missing command-line options.
gcc/testsuite/ChangeLog:
PR c/82892
* c-c++-common/spellcheck-missing-option.c: New test.
Signed-off-by: David Malcolm <dmalcolm@redhat.com>
Diffstat (limited to 'gcc/c-family')
-rw-r--r-- | gcc/c-family/c-common.h | 1 | ||||
-rw-r--r-- | gcc/c-family/c-cppbuiltin.cc | 13 | ||||
-rw-r--r-- | gcc/c-family/known-headers.cc | 28 | ||||
-rw-r--r-- | gcc/c-family/known-headers.h | 15 |
4 files changed, 57 insertions, 0 deletions
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index a323982..7834e0d 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -1260,6 +1260,7 @@ extern void c_stddef_cpp_builtins (void); extern void fe_file_change (const line_map_ordinary *); extern void c_parse_error (const char *, enum cpp_ttype, tree, unsigned char, rich_location *richloc); +extern diagnostic_option_id get_option_for_builtin_define (const char *macro_name); /* In c-ppoutput.cc */ extern void init_pp_output (FILE *); diff --git a/gcc/c-family/c-cppbuiltin.cc b/gcc/c-family/c-cppbuiltin.cc index 8fbfef5..c354c79 100644 --- a/gcc/c-family/c-cppbuiltin.cc +++ b/gcc/c-family/c-cppbuiltin.cc @@ -1673,6 +1673,19 @@ c_cpp_builtins (cpp_reader *pfile) cpp_define (pfile, "__DECIMAL_BID_FORMAT__"); } +/* Given NAME, return the command-line option that would make it be + a builtin define, or 0 if unrecognized. */ + +diagnostic_option_id +get_option_for_builtin_define (const char *name) +{ + if (!strcmp (name, "_OPENACC")) + return OPT_fopenacc; + if (!strcmp (name, "_OPENMP")) + return OPT_fopenmp; + return 0; +} + /* Pass an object-like macro. If it doesn't lie in the user's namespace, defines it unconditionally. Otherwise define a version with two leading underscores, and another version with two leading diff --git a/gcc/c-family/known-headers.cc b/gcc/c-family/known-headers.cc index 58a7259..2077cab 100644 --- a/gcc/c-family/known-headers.cc +++ b/gcc/c-family/known-headers.cc @@ -24,6 +24,7 @@ along with GCC; see the file COPYING3. If not see #include "c-family/name-hint.h" #include "c-family/known-headers.h" #include "gcc-rich-location.h" +#include "opts.h" /* An enum for distinguishing between the C and C++ stdlibs. */ @@ -323,3 +324,30 @@ suggest_missing_header::~suggest_missing_header () " this is probably fixable by adding %<#include %s%>", m_name_str, m_header_hint, m_header_hint); } + +/* Implementation of class suggest_missing_option. */ + +/* suggest_missing_option's ctor. */ + +suggest_missing_option::suggest_missing_option (location_t loc, + const char *macro_name, + diagnostic_option_id option_id) +: deferred_diagnostic (loc), m_name_str (macro_name), m_option_id (option_id) +{ + gcc_assert (macro_name); + gcc_assert (option_id.m_idx > 0); +} + +/* suggest_missing_option's dtor. */ + +suggest_missing_option::~suggest_missing_option () +{ + if (is_suppressed_p ()) + return; + + const char *option_name = cl_options[m_option_id.m_idx].opt_text; + inform (get_location (), + "%qs is defined when using option %qs;" + " this is probably fixable by adding %qs to the command-line options", + m_name_str, option_name, option_name); +} diff --git a/gcc/c-family/known-headers.h b/gcc/c-family/known-headers.h index 7c7ee78..a6cbbd2 100644 --- a/gcc/c-family/known-headers.h +++ b/gcc/c-family/known-headers.h @@ -41,4 +41,19 @@ class suggest_missing_header : public deferred_diagnostic const char *m_header_hint; }; +/* Subclass of deferred_diagnostic for suggesting to the user + that they have missed a command-line option. */ + +class suggest_missing_option : public deferred_diagnostic +{ + public: + suggest_missing_option (location_t loc, const char *name, + diagnostic_option_id option_id); + ~suggest_missing_option (); + + private: + const char *m_name_str; + diagnostic_option_id m_option_id; +}; + #endif /* GCC_KNOWN_HEADERS_H */ |