bfgroup / Lyra

A simple to use, composable, command line parser for C++ 11 and beyond
https://bfgroup.github.io/Lyra/
Boost Software License 1.0
471 stars 56 forks source link

When building with -Wshadow on GCC there are many shadowing warnings. #66

Closed SadieCat closed 1 year ago

SadieCat commented 2 years ago

As Lyra is a header-only library it is affected by project build flags so there is no reasonable way for a project to work around this short of disabling the flag.

Here is a sanitised log from my project:

In file included from [PATH]/main.cpp:39:
[PATH]/lyra.hpp: In constructor ‘lyra::detail::print::print(const char*)’:
[PATH]/lyra.hpp:55:3: warning: declaration of ‘scope’ shadows a member of ‘lyra::detail::print’ [-Wshadow]
   55 |   : scope(scope)
      |   ^
[PATH]/lyra.hpp:84:15: note: shadowed declaration is here
   84 |  const char * scope;
      |               ^~~~~
[PATH]/lyra.hpp: In constructor ‘lyra::detail::print::print(const char*)’:
[PATH]/lyra.hpp:59:2: warning: declaration of ‘scope’ shadows a member of ‘lyra::detail::print’ [-Wshadow]
   59 |  }
      |  ^
[PATH]/lyra.hpp:84:15: note: shadowed declaration is here
   84 |  const char * scope;
      |               ^~~~~
[PATH]/lyra.hpp: In constructor ‘lyra::detail::print::print(const char*)’:
[PATH]/lyra.hpp:59:2: warning: declaration of ‘scope’ shadows a member of ‘lyra::detail::print’ [-Wshadow]
   59 |  }
      |  ^
[PATH]/lyra.hpp:84:15: note: shadowed declaration is here
   84 |  const char * scope;
      |               ^~~~~
In file included from [PATH]/main.cpp:39:
[PATH]/lyra.hpp: In constructor ‘lyra::args::args(std::initializer_list<std::__cxx11::basic_string<char> >)’:
[PATH]/lyra.hpp:136:3: warning: declaration of ‘args’ shadows a member of ‘lyra::args’ [-Wshadow]
  136 |   : m_exeName(*args.begin())
      |   ^
[PATH]/lyra.hpp:128:1: note: shadowed declaration is here
  128 | {
      | ^
[PATH]/lyra.hpp: In constructor ‘lyra::args::args(std::initializer_list<std::__cxx11::basic_string<char> >)’:
[PATH]/lyra.hpp:138:3: warning: declaration of ‘args’ shadows a member of ‘lyra::args’ [-Wshadow]
  138 |  {}
      |   ^
[PATH]/lyra.hpp:128:1: note: shadowed declaration is here
  128 | {
      | ^
[PATH]/lyra.hpp: In constructor ‘lyra::args::args(std::initializer_list<std::__cxx11::basic_string<char> >)’:
[PATH]/lyra.hpp:138:3: warning: declaration of ‘args’ shadows a member of ‘lyra::args’ [-Wshadow]
  138 |  {}
      |   ^
[PATH]/lyra.hpp:128:1: note: shadowed declaration is here
  128 | {
      | ^
    BUILD:      logger.cpp
In file included from [PATH]/main.cpp:39:
[PATH]/lyra.hpp: In constructor ‘lyra::detail::choices_check<Lambda>::choices_check(const Lambda&)’:
[PATH]/lyra.hpp:984:3: warning: declaration of ‘checker’ shadows a member of ‘lyra::detail::choices_check<Lambda>’ [-Wshadow]
  984 |   : checker(checker)
      |   ^
[PATH]/lyra.hpp:980:9: note: shadowed declaration is here
  980 |  Lambda checker;
      |         ^~~~~~~
[PATH]/lyra.hpp: In constructor ‘lyra::option_style::option_style(std::string&&, std::string&&, std::size_t, std::string&&, std::size_t)’:
[PATH]/lyra.hpp:1063:3: warning: declaration of ‘short_option_size’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1063 |   : value_delimiters(std::move(value_delimiters))
      |   ^
[PATH]/lyra.hpp:1055:14: note: shadowed declaration is here
 1055 |  std::size_t short_option_size = 0;
      |              ^~~~~~~~~~~~~~~~~
[PATH]/lyra.hpp:1063:3: warning: declaration of ‘short_option_prefix’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1063 |   : value_delimiters(std::move(value_delimiters))
      |   ^
[PATH]/lyra.hpp:1054:14: note: shadowed declaration is here
 1054 |  std::string short_option_prefix;
      |              ^~~~~~~~~~~~~~~~~~~
[PATH]/lyra.hpp:1063:3: warning: declaration of ‘long_option_size’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1063 |   : value_delimiters(std::move(value_delimiters))
      |   ^
[PATH]/lyra.hpp:1053:14: note: shadowed declaration is here
 1053 |  std::size_t long_option_size = 0;
      |              ^~~~~~~~~~~~~~~~
[PATH]/lyra.hpp:1063:3: warning: declaration of ‘long_option_prefix’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1063 |   : value_delimiters(std::move(value_delimiters))
      |   ^
[PATH]/lyra.hpp:1052:14: note: shadowed declaration is here
 1052 |  std::string long_option_prefix;
      |              ^~~~~~~~~~~~~~~~~~
[PATH]/lyra.hpp:1063:3: warning: declaration of ‘value_delimiters’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1063 |   : value_delimiters(std::move(value_delimiters))
      |   ^
[PATH]/lyra.hpp:1051:14: note: shadowed declaration is here
 1051 |  std::string value_delimiters;
      |              ^~~~~~~~~~~~~~~~
[PATH]/lyra.hpp: In constructor ‘lyra::option_style::option_style(std::string&&, std::string&&, std::size_t, std::string&&, std::size_t)’:
[PATH]/lyra.hpp:1068:3: warning: declaration of ‘short_option_size’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1068 |  {}
      |   ^
[PATH]/lyra.hpp:1055:14: note: shadowed declaration is here
 1055 |  std::size_t short_option_size = 0;
      |              ^~~~~~~~~~~~~~~~~
[PATH]/lyra.hpp:1068:3: warning: declaration of ‘short_option_prefix’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1068 |  {}
      |   ^
[PATH]/lyra.hpp:1054:14: note: shadowed declaration is here
 1054 |  std::string short_option_prefix;
      |              ^~~~~~~~~~~~~~~~~~~
[PATH]/lyra.hpp:1068:3: warning: declaration of ‘long_option_size’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1068 |  {}
      |   ^
[PATH]/lyra.hpp:1053:14: note: shadowed declaration is here
 1053 |  std::size_t long_option_size = 0;
      |              ^~~~~~~~~~~~~~~~
[PATH]/lyra.hpp:1068:3: warning: declaration of ‘long_option_prefix’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1068 |  {}
      |   ^
[PATH]/lyra.hpp:1052:14: note: shadowed declaration is here
 1052 |  std::string long_option_prefix;
      |              ^~~~~~~~~~~~~~~~~~
[PATH]/lyra.hpp:1068:3: warning: declaration of ‘value_delimiters’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1068 |  {}
      |   ^
[PATH]/lyra.hpp:1051:14: note: shadowed declaration is here
 1051 |  std::string value_delimiters;
      |              ^~~~~~~~~~~~~~~~
[PATH]/lyra.hpp: In constructor ‘lyra::option_style::option_style(std::string&&, std::string&&, std::size_t, std::string&&, std::size_t)’:
[PATH]/lyra.hpp:1068:3: warning: declaration of ‘short_option_size’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1068 |  {}
      |   ^
[PATH]/lyra.hpp:1055:14: note: shadowed declaration is here
 1055 |  std::size_t short_option_size = 0;
      |              ^~~~~~~~~~~~~~~~~
[PATH]/lyra.hpp:1068:3: warning: declaration of ‘short_option_prefix’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1068 |  {}
      |   ^
[PATH]/lyra.hpp:1054:14: note: shadowed declaration is here
 1054 |  std::string short_option_prefix;
      |              ^~~~~~~~~~~~~~~~~~~
[PATH]/lyra.hpp:1068:3: warning: declaration of ‘long_option_size’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1068 |  {}
      |   ^
[PATH]/lyra.hpp:1053:14: note: shadowed declaration is here
 1053 |  std::size_t long_option_size = 0;
      |              ^~~~~~~~~~~~~~~~
[PATH]/lyra.hpp:1068:3: warning: declaration of ‘long_option_prefix’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1068 |  {}
      |   ^
[PATH]/lyra.hpp:1052:14: note: shadowed declaration is here
 1052 |  std::string long_option_prefix;
      |              ^~~~~~~~~~~~~~~~~~
[PATH]/lyra.hpp:1068:3: warning: declaration of ‘value_delimiters’ shadows a member of ‘lyra::option_style’ [-Wshadow]
 1068 |  {}
      |   ^
[PATH]/lyra.hpp:1051:14: note: shadowed declaration is here
 1051 |  std::string value_delimiters;
      |              ^~~~~~~~~~~~~~~~
[PATH]/lyra.hpp: In member function ‘virtual lyra::parse_result lyra::arg::parse(const lyra::detail::token_iterator&, const lyra::option_style&) const’:
[PATH]/lyra.hpp:2182:8: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 2182 |   auto result = valueRef->setValue(token.name);
      |        ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~
[PATH]/lyra.hpp: In member function ‘virtual lyra::result lyra::arguments::validate() const’:
[PATH]/lyra.hpp:2471:9: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 2471 |    auto result = p->validate();
      |         ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~
[PATH]/lyra.hpp: In member function ‘lyra::parse_result lyra::arguments::parse_any(const lyra::detail::token_iterator&, const lyra::option_style&) const’:
[PATH]/lyra.hpp:2508:8: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 2508 |   auto result = parse_result::ok(
      |        ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~
[PATH]/lyra.hpp: In member function ‘lyra::parse_result lyra::arguments::parse_sequence(const lyra::detail::token_iterator&, const lyra::option_style&) const’:
[PATH]/lyra.hpp:2589:8: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 2589 |   auto result = parse_result::ok(
      |        ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~
[PATH]/lyra.hpp: In member function ‘virtual const lyra::parser* lyra::arguments::get_named(const string&) const’:
[PATH]/lyra.hpp:2654:19: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 2654 |    const parser * result = p->get_named(n);
      |                   ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~
[PATH]/lyra.hpp: In member function ‘virtual lyra::parse_result lyra::group::parse(const lyra::detail::token_iterator&, const lyra::option_style&) const’:
[PATH]/lyra.hpp:2926:16: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 2926 |   parse_result result = arguments::parse(tokens, style);
      |                ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~
[PATH]/lyra.hpp: In member function ‘lyra::cli::value_result::operator T() const’:
[PATH]/lyra.hpp:3157:43: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 3157 |    typename detail::remove_cvref<T>::type result {};
      |                                           ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~
[PATH]/lyra.hpp: In member function ‘lyra::cli::value_result::operator std::vector<T>() const’:
[PATH]/lyra.hpp:3168:19: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 3168 |    std::vector<T> result;
      |                   ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~
[PATH]/lyra.hpp: In member function ‘lyra::parse_result lyra::cli::parse(const lyra::args&, const lyra::option_style&) const’:
[PATH]/lyra.hpp:3399:15: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 3399 |  parse_result result = parse(args_tokens, style);
      |               ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~
[PATH]/lyra.hpp: In member function ‘lyra::literal& lyra::literal::operator()(const string&)’:
[PATH]/lyra.hpp:3589:69: warning: declaration of ‘description’ shadows a member of ‘lyra::literal’ [-Wshadow]
 3589 | inline literal & literal::operator()(std::string const & description)
      |                                                                     ^
[PATH]/lyra.hpp:3535:14: note: shadowed declaration is here
 3535 |  std::string description;
      |              ^~~~~~~~~~~
[PATH]/lyra.hpp: In member function ‘virtual std::string lyra::opt::get_usage_text(const lyra::option_style&) const’:
[PATH]/lyra.hpp:3825:15: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 3825 |   std::string result;
      |               ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~
[PATH]/lyra.hpp: In member function ‘virtual lyra::parser::help_text lyra::opt::get_help_text(const lyra::option_style&) const’:
[PATH]/lyra.hpp:3839:21: warning: declaration of ‘opt’ shadows a member of ‘lyra::opt’ [-Wshadow]
 3839 |   for (auto const & opt : opt_names)
      |                     ^~~
[PATH]/lyra.hpp:3773:1: note: shadowed declaration is here
 3773 | {
      | ^
[PATH]/lyra.hpp: In member function ‘virtual lyra::parse_result lyra::opt::parse(const lyra::detail::token_iterator&, const lyra::option_style&) const’:
[PATH]/lyra.hpp:3886:11: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 3886 |      auto result = flagRef->setFlag(true);
      |           ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~
[PATH]/lyra.hpp:3910:11: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 3910 |      auto result = valueRef->setValue(argToken.name);
      |           ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~
[PATH]/lyra.hpp: In member function ‘lyra::main& lyra::main::operator()(const T&)’:
[PATH]/lyra.hpp:4273:41: warning: declaration of ‘parser’ shadows a member of ‘lyra::main’ [-Wshadow]
 4273 | main & main::operator()(const T & parser)
      |                                         ^
[PATH]/lyra.hpp:1582:1: note: shadowed declaration is here
 1582 | {
      | ^
[PATH]/lyra.hpp: In member function ‘lyra::main& lyra::main::add_argument(const T&)’:
[PATH]/lyra.hpp:4279:43: warning: declaration of ‘parser’ shadows a member of ‘lyra::main’ [-Wshadow]
 4279 | main & main::add_argument(const T & parser)
      |                                           ^
[PATH]/lyra.hpp:1582:1: note: shadowed declaration is here
 1582 | {
      | ^
[PATH]/lyra.hpp: In member function ‘lyra::main& lyra::main::operator|=(const T&)’:
[PATH]/lyra.hpp:4285:41: warning: declaration of ‘parser’ shadows a member of ‘lyra::main’ [-Wshadow]
 4285 | main & main::operator|=(const T & parser)
      |                                         ^
[PATH]/lyra.hpp:1582:1: note: shadowed declaration is here
 1582 | {
      | ^
[PATH]/lyra.hpp: In member function ‘int lyra::main::operator()(const lyra::args&, L)’:
[PATH]/lyra.hpp:4391:7: warning: declaration of ‘result’ shadows a global declaration [-Wshadow]
 4391 |  auto result = cli::parse(argv);
      |       ^~~~~~
[PATH]/lyra.hpp:603:7: note: shadowed declaration is here
  603 | using result = detail::basic_result<void>;
      |       ^~~~~~

I'm using the single header version of 1.6.0 but this should still occur with the multi-header version.

johnmcfarlane commented 2 years ago

How are you specifying Lyra headers as search paths in your project? Are you using -I, -isystem, or something else?

SadieCat commented 2 years ago

I'm using data/single_include/lyra/lyra.hpp and it is in a directory that is added to the search path with -I.

johnmcfarlane commented 2 years ago

Have you tried -isystem instead?

On Wed 27 Jul 2022, 23:19 Sadie Powell, @.***> wrote:

I'm using data/single_include/lyra/lyra.hpp and it is in a directory that is added to the search path with -I.

— Reply to this email directly, view it on GitHub https://github.com/bfgroup/Lyra/issues/66#issuecomment-1197431644, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFTGN72CAL7WK5TWUAFJMLVWGYXVANCNFSM5T2HBKAQ . You are receiving this because you commented.Message ID: @.***>

SadieCat commented 2 years ago

That hides the warnings for me but it would also hide warnings for other headers in the same directory and would not do anything for other users who are including the header using a path relative to a source file.

I proposed a better fix for the problem in PR #67.

johnmcfarlane commented 2 years ago

It ought to be possible always to include 3rd-party headers via -isystem. It's a flag which is used specifically for this purpose.

Fixing warnings generally is good practice and the intent of #67 is worthwhile. However, as a solution to the problem of using -Werror in client code, it isn't a permanent or scalable solution.

In the future, new compiler versions (or even new compilers) might introduce new warnings. Ideally this should not break dependencies in code which might be working perfectly as is. Hence, -isystem which expresses the fact that fixing 3rd-party header warnings is beyond the scope of the dependent project.