From: Martin Liska Date: Thu, 28 Jun 2018 07:08:01 +0000 (+0200) Subject: Makefile.in: Add opt-suggestions.o. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=98086b2ba2bd021e887788cea410410e61eaee42;p=gcc.git Makefile.in: Add opt-suggestions.o. . 2018-06-28 Martin Liska * Makefile.in: Add opt-suggestions.o. * gcc-main.c: Include opt-suggestions.h. * gcc.c (driver::driver): Likewise. (driver::~driver): Remove m_option_suggestions. (driver::build_option_suggestions): Moved to option_proposer. (driver::suggest_option): Likewise. (driver::handle_unrecognized_options): Use option_proposer. * gcc.h (class driver): Add new memver m_option_proposer. * opt-suggestions.c: New file. * opt-suggestions.h: New file. 2018-06-28 Martin Liska * cppspec.c: Include opt-suggestions.h. 2018-06-28 Martin Liska * gfortranspec.c: Include opt-suggestions.h. 2018-06-28 Martin Liska * jit-playback.c: Include opt-suggestions.h. From-SVN: r262209 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index a5a6ace9d04..aee4f0fbf7a 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,16 @@ +2018-06-28 Martin Liska + + * Makefile.in: Add opt-suggestions.o. + * gcc-main.c: Include opt-suggestions.h. + * gcc.c (driver::driver): Likewise. + (driver::~driver): Remove m_option_suggestions. + (driver::build_option_suggestions): Moved to option_proposer. + (driver::suggest_option): Likewise. + (driver::handle_unrecognized_options): Use option_proposer. + * gcc.h (class driver): Add new memver m_option_proposer. + * opt-suggestions.c: New file. + * opt-suggestions.h: New file. + 2018-06-28 Martin Liska * vec.h (class auto_string_vec): New (moved from auto_argvec). diff --git a/gcc/Makefile.in b/gcc/Makefile.in index 1f38cacde7a..66c8b6e4d03 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -1615,7 +1615,7 @@ OBJS-libcommon = diagnostic.o diagnostic-color.o diagnostic-show-locus.o \ # compiler and containing target-dependent code. OBJS-libcommon-target = $(common_out_object_file) prefix.o params.o \ opts.o opts-common.o options.o vec.o hooks.o common/common-targhooks.o \ - hash-table.o file-find.o spellcheck.o selftest.o + hash-table.o file-find.o spellcheck.o selftest.o opt-suggestions.o # This lists all host objects for the front ends. ALL_HOST_FRONTEND_OBJS = $(foreach v,$(CONFIG_LANGUAGES),$($(v)_OBJS)) diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index fc7f7de1ed4..9596eec56bc 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,7 @@ +2018-06-28 Martin Liska + + * cppspec.c: Include opt-suggestions.h. + 2018-06-20 Chung-Lin Tang Thomas Schwinge Cesar Philippidis diff --git a/gcc/c-family/cppspec.c b/gcc/c-family/cppspec.c index 1e0a8bcd294..66540239f53 100644 --- a/gcc/c-family/cppspec.c +++ b/gcc/c-family/cppspec.c @@ -21,6 +21,7 @@ along with GCC; see the file COPYING3. If not see #include "system.h" #include "coretypes.h" #include "tm.h" +#include "opt-suggestions.h" #include "gcc.h" #include "opts.h" diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index f052cfed2c8..6e1af2e3053 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,7 @@ +2018-06-28 Martin Liska + + * gfortranspec.c: Include opt-suggestions.h. + 2018-06-25 Fritz Reese PR fortran/82972 diff --git a/gcc/fortran/gfortranspec.c b/gcc/fortran/gfortranspec.c index fe1ec0447b3..4ba3a8dba60 100644 --- a/gcc/fortran/gfortranspec.c +++ b/gcc/fortran/gfortranspec.c @@ -47,6 +47,7 @@ along with GCC; see the file COPYING3. If not see #include "config.h" #include "system.h" #include "coretypes.h" +#include "opt-suggestions.h" #include "gcc.h" #include "opts.h" diff --git a/gcc/gcc-main.c b/gcc/gcc-main.c index 9e6de743adc..6a759bbcc1c 100644 --- a/gcc/gcc-main.c +++ b/gcc/gcc-main.c @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3. If not see #include "obstack.h" #include "intl.h" #include "prefix.h" +#include "opt-suggestions.h" #include "gcc.h" /* Implement the top-level "main" within the driver in terms of diff --git a/gcc/gcc.c b/gcc/gcc.c index 405d2e38d44..dda1fd35398 100644 --- a/gcc/gcc.c +++ b/gcc/gcc.c @@ -36,6 +36,7 @@ compilation is specified by a string called a "spec". */ #include "obstack.h" #include "intl.h" #include "prefix.h" +#include "opt-suggestions.h" #include "gcc.h" #include "diagnostic.h" #include "flags.h" @@ -7270,8 +7271,7 @@ compare_files (char *cmpfile[]) driver::driver (bool can_finalize, bool debug) : explicit_link_files (NULL), - decoded_options (NULL), - m_option_suggestions (NULL) + decoded_options (NULL) { env.init (can_finalize, debug); } @@ -7280,14 +7280,6 @@ driver::~driver () { XDELETEVEC (explicit_link_files); XDELETEVEC (decoded_options); - if (m_option_suggestions) - { - int i; - char *str; - FOR_EACH_VEC_ELT (*m_option_suggestions, i, str) - free (str); - delete m_option_suggestions; - } } /* driver::main is implemented as a series of driver:: method calls. */ @@ -7776,106 +7768,6 @@ driver::maybe_putenv_OFFLOAD_TARGETS () const offload_targets = NULL; } -/* Helper function for driver::suggest_option. Populate - m_option_suggestions with candidate strings for misspelled options. - The strings will be freed by the driver's dtor. */ - -void -driver::build_option_suggestions (void) -{ - gcc_assert (m_option_suggestions == NULL); - m_option_suggestions = new auto_vec (); - - /* We build a vec of m_option_suggestions, using add_misspelling_candidates - to add copies of strings, without a leading dash. */ - - for (unsigned int i = 0; i < cl_options_count; i++) - { - const struct cl_option *option = &cl_options[i]; - const char *opt_text = option->opt_text; - switch (i) - { - default: - if (option->var_type == CLVC_ENUM) - { - const struct cl_enum *e = &cl_enums[option->var_enum]; - for (unsigned j = 0; e->values[j].arg != NULL; j++) - { - char *with_arg = concat (opt_text, e->values[j].arg, NULL); - add_misspelling_candidates (m_option_suggestions, option, - with_arg); - free (with_arg); - } - } - else - add_misspelling_candidates (m_option_suggestions, option, - opt_text); - break; - - case OPT_fsanitize_: - case OPT_fsanitize_recover_: - /* -fsanitize= and -fsanitize-recover= can take - a comma-separated list of arguments. Given that combinations - are supported, we can't add all potential candidates to the - vec, but if we at least add them individually without commas, - we should do a better job e.g. correcting - "-sanitize=address" - to - "-fsanitize=address" - rather than to "-Wframe-address" (PR driver/69265). */ - { - for (int j = 0; sanitizer_opts[j].name != NULL; ++j) - { - struct cl_option optb; - /* -fsanitize=all is not valid, only -fno-sanitize=all. - So don't register the positive misspelling candidates - for it. */ - if (sanitizer_opts[j].flag == ~0U && i == OPT_fsanitize_) - { - optb = *option; - optb.opt_text = opt_text = "-fno-sanitize="; - optb.cl_reject_negative = true; - option = &optb; - } - /* Get one arg at a time e.g. "-fsanitize=address". */ - char *with_arg = concat (opt_text, - sanitizer_opts[j].name, - NULL); - /* Add with_arg and all of its variant spellings e.g. - "-fno-sanitize=address" to candidates (albeit without - leading dashes). */ - add_misspelling_candidates (m_option_suggestions, option, - with_arg); - free (with_arg); - } - } - break; - } - } -} - -/* Helper function for driver::handle_unrecognized_options. - - Given an unrecognized option BAD_OPT (without the leading dash), - locate the closest reasonable matching option (again, without the - leading dash), or NULL. - - The returned string is owned by the driver instance. */ - -const char * -driver::suggest_option (const char *bad_opt) -{ - /* Lazily populate m_option_suggestions. */ - if (!m_option_suggestions) - build_option_suggestions (); - gcc_assert (m_option_suggestions); - - /* "m_option_suggestions" is now populated. Use it. */ - return find_closest_string - (bad_opt, - (auto_vec *) m_option_suggestions); -} - /* Reject switches that no pass was interested in. */ void @@ -7884,7 +7776,7 @@ driver::handle_unrecognized_options () for (size_t i = 0; (int) i < n_switches; i++) if (! switches[i].validated) { - const char *hint = suggest_option (switches[i].part1); + const char *hint = m_option_proposer.suggest_option (switches[i].part1); if (hint) error ("unrecognized command line option %<-%s%>;" " did you mean %<-%s%>?", diff --git a/gcc/gcc.h b/gcc/gcc.h index ddbf42f78ea..a7606183393 100644 --- a/gcc/gcc.h +++ b/gcc/gcc.h @@ -45,8 +45,6 @@ class driver void putenv_COLLECT_GCC (const char *argv0) const; void maybe_putenv_COLLECT_LTO_WRAPPER () const; void maybe_putenv_OFFLOAD_TARGETS () const; - void build_option_suggestions (void); - const char *suggest_option (const char *bad_opt); void handle_unrecognized_options (); int maybe_print_and_exit () const; bool prepare_infiles (); @@ -59,7 +57,7 @@ class driver char *explicit_link_files; struct cl_decoded_option *decoded_options; unsigned int decoded_options_count; - auto_vec *m_option_suggestions; + option_proposer m_option_proposer; }; /* The mapping of a spec function name to the C function that diff --git a/gcc/jit/ChangeLog b/gcc/jit/ChangeLog index 059cbb886bf..22679671a01 100644 --- a/gcc/jit/ChangeLog +++ b/gcc/jit/ChangeLog @@ -1,3 +1,7 @@ +2018-06-28 Martin Liska + + * jit-playback.c: Include opt-suggestions.h. + 2018-06-28 Martin Liska * jit-playback.c (class auto_argvec): Moved to vec.h. diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c index 01c4567de05..f11642bf4c6 100644 --- a/gcc/jit/jit-playback.c +++ b/gcc/jit/jit-playback.c @@ -36,6 +36,7 @@ along with GCC; see the file COPYING3. If not see #include "attribs.h" #include "context.h" #include "fold-const.h" +#include "opt-suggestions.h" #include "gcc.h" #include "diagnostic.h" diff --git a/gcc/opt-suggestions.c b/gcc/opt-suggestions.c new file mode 100644 index 00000000000..90ab80e627d --- /dev/null +++ b/gcc/opt-suggestions.c @@ -0,0 +1,122 @@ +/* Provide option suggestion for --complete option and a misspelled + used by a user. + Copyright (C) 2016-2018 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "opts.h" +#include "params.h" +#include "spellcheck.h" +#include "opt-suggestions.h" +#include "selftest.h" + +option_proposer::~option_proposer () +{ + delete m_option_suggestions; +} + +const char * +option_proposer::suggest_option (const char *bad_opt) +{ + /* Lazily populate m_option_suggestions. */ + if (!m_option_suggestions) + build_option_suggestions (); + gcc_assert (m_option_suggestions); + + /* "m_option_suggestions" is now populated. Use it. */ + return find_closest_string + (bad_opt, + (auto_vec *) m_option_suggestions); +} + +void +option_proposer::build_option_suggestions (void) +{ + gcc_assert (m_option_suggestions == NULL); + m_option_suggestions = new auto_string_vec (); + + /* We build a vec of m_option_suggestions, using add_misspelling_candidates + to add copies of strings, without a leading dash. */ + + for (unsigned int i = 0; i < cl_options_count; i++) + { + const struct cl_option *option = &cl_options[i]; + const char *opt_text = option->opt_text; + switch (i) + { + default: + if (option->var_type == CLVC_ENUM) + { + const struct cl_enum *e = &cl_enums[option->var_enum]; + for (unsigned j = 0; e->values[j].arg != NULL; j++) + { + char *with_arg = concat (opt_text, e->values[j].arg, NULL); + add_misspelling_candidates (m_option_suggestions, option, + with_arg); + free (with_arg); + } + } + else + add_misspelling_candidates (m_option_suggestions, option, + opt_text); + break; + + case OPT_fsanitize_: + case OPT_fsanitize_recover_: + /* -fsanitize= and -fsanitize-recover= can take + a comma-separated list of arguments. Given that combinations + are supported, we can't add all potential candidates to the + vec, but if we at least add them individually without commas, + we should do a better job e.g. correcting + "-sanitize=address" + to + "-fsanitize=address" + rather than to "-Wframe-address" (PR driver/69265). */ + { + for (int j = 0; sanitizer_opts[j].name != NULL; ++j) + { + struct cl_option optb; + /* -fsanitize=all is not valid, only -fno-sanitize=all. + So don't register the positive misspelling candidates + for it. */ + if (sanitizer_opts[j].flag == ~0U && i == OPT_fsanitize_) + { + optb = *option; + optb.opt_text = opt_text = "-fno-sanitize="; + optb.cl_reject_negative = true; + option = &optb; + } + /* Get one arg at a time e.g. "-fsanitize=address". */ + char *with_arg = concat (opt_text, + sanitizer_opts[j].name, + NULL); + /* Add with_arg and all of its variant spellings e.g. + "-fno-sanitize=address" to candidates (albeit without + leading dashes). */ + add_misspelling_candidates (m_option_suggestions, option, + with_arg); + free (with_arg); + } + } + break; + } + } +} diff --git a/gcc/opt-suggestions.h b/gcc/opt-suggestions.h new file mode 100644 index 00000000000..ccd4e45a474 --- /dev/null +++ b/gcc/opt-suggestions.h @@ -0,0 +1,59 @@ +/* Provide suggestions to handle misspelled options, and implement the + --complete option for auto-completing options from a prefix. + Copyright (C) 2016-2018 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#ifndef GCC_OPT_PROPOSER_H +#define GCC_OPT_PROPOSER_H + +/* Option proposer is class used by driver in order to provide hints + for wrong options provided. And it's used by --complete option that's + intended to be invoked by BASH in order to provide better option + completion support. */ + +class option_proposer +{ +public: + /* Default constructor. */ + option_proposer (): m_option_suggestions (NULL) + {} + + /* Default destructor. */ + ~option_proposer (); + + /* Helper function for driver::handle_unrecognized_options. + + Given an unrecognized option BAD_OPT (without the leading dash), + locate the closest reasonable matching option (again, without the + leading dash), or NULL. + + The returned string is owned by the option_proposer instance. */ + const char *suggest_option (const char *bad_opt); + +private: + /* Helper function for option_proposer::suggest_option. Populate + m_option_suggestions with candidate strings for misspelled options. + The strings will be freed by the option_proposer's dtor. */ + void build_option_suggestions (); + +private: + /* Cache with all suggestions. */ + auto_string_vec *m_option_suggestions; +}; + +#endif /* GCC_OPT_PROPOSER_H */