Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gcc 7 and 8.2 warning: parameter ‘arg_name’ set but not used #2170

Closed
phprus opened this issue Mar 8, 2021 · 4 comments · Fixed by #2177
Closed

gcc 7 and 8.2 warning: parameter ‘arg_name’ set but not used #2170

phprus opened this issue Mar 8, 2021 · 4 comments · Fixed by #2177

Comments

@phprus
Copy link
Contributor

phprus commented Mar 8, 2021

Warning:

/home/.../3rdparty/fmt/src/include/fmt/compile.h: In instantiation of ‘static constexpr bool fmt::v7::detail::runtime_named_field<Char>::try_format_argument(OutputIt&, fmt::v7::basic_string_view<Char>, const T&) [with OutputIt = fmt::v7::detail::buffer_appender<char>; T = char [4]; Char = char]’:
/home/.../3rdparty/fmt/src/include/fmt/compile.h:570:38:   required from ‘constexpr OutputIt fmt::v7::detail::runtime_named_field<Char>::format(OutputIt, const Args& ...) const [with OutputIt = fmt::v7::detail::buffer_appender<char>; Args = {char [4], fmt::v7::detail::named_arg<char, char [4]>}; Char = char]’
/home/.../3rdparty/fmt/src/include/fmt/compile.h:608:35:   required from ‘constexpr OutputIt fmt::v7::detail::concat<L, R>::format(OutputIt, const Args& ...) const [with OutputIt = fmt::v7::detail::buffer_appender<char>; Args = {char [4], fmt::v7::detail::named_arg<char, char [4]>}; L = fmt::v7::detail::field<char, char [4], 0>; R = fmt::v7::detail::runtime_named_field<char>]’
/home/.../3rdparty/fmt/src/include/fmt/compile.h:822:3:   required from ‘std::__cxx11::basic_string<Char> fmt::v7::format(const CompiledFormat&, const Args& ...) [with CompiledFormat = fmt::v7::detail::concat<fmt::v7::detail::field<char, char [4], 0>, fmt::v7::detail::runtime_named_field<char> >; Args = {char [4], fmt::v7::detail::named_arg<char, char [4]>}; Char = char; typename std::enable_if<fmt::v7::detail::is_compiled_format<T>::value, int>::type <anonymous> = 0]’
/home/.../3rdparty/fmt/src/include/fmt/compile.h:872:18:   required from ‘std::__cxx11::basic_string<typename S::char_type> fmt::v7::format(const S&, Args&& ...) [with S = CompileTest_Named_Test::TestBody()::<lambda()>::FMT_COMPILE_STRING; Args = {const char (&)[4], fmt::v7::detail::named_arg<char, char [4]>}; typename std::enable_if<fmt::v7::detail::is_compiled_string<S>::value, int>::type <anonymous> = 0; typename FormatContext::char_type = char; typename Context::char_type = char; typename S::char_type = char]’
/home/.../3rdparty/fmt/src/test/compile-test.cc:230:3:   required from here
/home/.../3rdparty/fmt/src/include/fmt/compile.h:557:69: warning: parameter ‘arg_name’ set but not used [-Wunused-but-set-parameter]
                                             basic_string_view<Char> arg_name,
                                                                     ^~~~~~~~
@alexezeder
Copy link
Contributor

I was unable to reproduce it on Compiler Explorer with the corresponding warning option: https://godbolt.org/z/Gr65Tb.
But I think it can be solved by using [[maybe_unused]] attribute for all arguments of try_format_argument() function:

fmt/include/fmt/compile.h

Lines 556 to 566 in 6a9016e

constexpr static bool try_format_argument(OutputIt& out,
basic_string_view<Char> arg_name,
const T& arg) {
if constexpr (is_named_arg<typename std::remove_cv<T>::type>::value) {
if (arg_name == arg.name) {
out = write<Char>(out, arg.value);
return true;
}
}
return false;
}

exactly as it's done here:

fmt/include/fmt/compile.h

Lines 478 to 479 in 6a9016e

constexpr const auto& get([[maybe_unused]] const T& first,
[[maybe_unused]] const Args&... rest) {

@phprus, could you please check if this solution works for you?

@phprus
Copy link
Contributor Author

phprus commented Mar 10, 2021

Хм...
I am using gcc from opensuse:

Using built-in specs.
COLLECT_GCC=/usr/bin/g++-7
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/7/lto-wrapper
OFFLOAD_TARGET_NAMES=hsa:nvptx-none
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,ada,go --enable-offload-targets=hsa,nvptx-none=/usr/nvptx-none, --without-cuda-driver --enable-checking=release --disable-werror --with-gxx-include-dir=/usr/include/c++/7 --enable-ssp --disable-libssp --disable-libvtv --disable-libcc1 --disable-plugin --with-bugurl=https://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --with-slibdir=/lib64 --with-system-zlib --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-version-specific-runtime-libs --with-gcc-major-version-only --enable-linker-build-id --enable-linux-futex --enable-gnu-indirect-function --program-suffix=-7 --without-system-libunwind --enable-multilib --with-arch-32=x86-64 --with-tune=generic --build=x86_64-suse-linux --host=x86_64-suse-linux
Thread model: posix
gcc version 7.5.0 (SUSE Linux)

and

Using built-in specs.
COLLECT_GCC=/usr/bin/g++-8
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/8/lto-wrapper
OFFLOAD_TARGET_NAMES=hsa:nvptx-none
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,fortran,ada,go --enable-offload-targets=hsa,nvptx-none=/usr/nvptx-none, --without-cuda-driver --enable-checking=release --disable-werror --with-gxx-include-dir=/usr/include/c++/8 --enable-ssp --disable-libssp --disable-libvtv --disable-cet --disable-libcc1 --disable-plugin --with-bugurl=http://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-version-specific-runtime-libs --with-gcc-major-version-only --enable-linker-build-id --enable-linux-futex --enable-gnu-indirect-function --program-suffix=-8 --without-system-libunwind --enable-multilib --with-arch-32=x86-64 --with-tune=generic --build=x86_64-suse-linux --host=x86_64-suse-linux
Thread model: posix
gcc version 8.2.1 20180831 [gcc-8-branch revision 264010] (SUSE Linux)

This flag-set already generate a warning (fmt version: 6a9016e):

/usr/bin/g++-7   -isystem /.../3rdparty/fmt/src/test/gtest -isystem /.../3rdparty/fmt/src/test/gmock -isystem /.../3rdparty/fmt/src/test/. -I/.../3rdparty/fmt/src/include -Wunused-but-set-parameter  -O3 -std=gnu++1z -o CMakeFiles/compile-test.dir/compile-test.cc.o -c /.../3rdparty/fmt/src/test/compile-test.cc

@alexezeder Yes, adding [[maybe_unused]] to the try_format_argument arguments suppresses this warning.

@alexezeder
Copy link
Contributor

I was able to reproduce it with GCC 7.3. For this version it's even enough to add [[maybe_unused]] attribute only for arg_name to get rid of this warning, like so:

  constexpr static bool try_format_argument(
      OutputIt& out, [[maybe_unused]] basic_string_view<Char> arg_name,
      const T& arg) {

For me, it's a compiler bug because arg_name is still used in this function despite the fact that the usage is disabled by if constexpr in some cases.

Also, I'm not sure which solution is clearer, the one with all arguments marked as maybe unused or the above one.

@vitaut
Copy link
Contributor

vitaut commented Mar 12, 2021

Also, I'm not sure which solution is clearer, the one with all arguments marked as maybe unused or the above one.

I think it's better to only annotate the argument gcc complains about with a brief comment saying that it's a workaround for gcc 7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants