Skip to content

Commit

Permalink
Suppress a false positive in gcc
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed May 6, 2023
1 parent 192df93 commit f61f15c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ FMT_MODULE_EXPORT template <typename Context> class basic_format_args;
FMT_MODULE_EXPORT template <typename Context> class dynamic_format_arg_store;

// A formatter for objects of type T.
FMT_MODULE_EXPORT
FMT_MODULE_EXPORT
template <typename T, typename Char = char, typename Enable = void>
struct formatter {
// A deleted default constructor indicates a disabled formatter.
Expand Down Expand Up @@ -1671,22 +1671,22 @@ constexpr auto encode_types() -> unsigned long long {

template <typename Context, typename T>
FMT_CONSTEXPR FMT_INLINE auto make_value(T&& val) -> value<Context> {
const auto& arg = arg_mapper<Context>().map(FMT_FORWARD(val));
auto&& arg = arg_mapper<Context>().map(FMT_FORWARD(val));
using arg_type = remove_cvref_t<decltype(arg)>;

constexpr bool formattable_char =
!std::is_same<decltype(arg), const unformattable_char&>::value;
!std::is_same<arg_type, unformattable_char>::value;
static_assert(formattable_char, "Mixing character types is disallowed.");

// Formatting of arbitrary pointers is disallowed. If you want to format a
// pointer cast it to `void*` or `const void*`. In particular, this forbids
// formatting of `[const] volatile char*` printed as bool by iostreams.
constexpr bool formattable_pointer =
!std::is_same<decltype(arg), const unformattable_pointer&>::value;
!std::is_same<arg_type, unformattable_pointer>::value;
static_assert(formattable_pointer,
"Formatting of non-void pointers is disallowed.");

constexpr bool formattable =
!std::is_same<decltype(arg), const unformattable&>::value;
constexpr bool formattable = !std::is_same<arg_type, unformattable>::value;
static_assert(
formattable,
"Cannot format an argument. To make type T formattable provide a "
Expand Down

5 comments on commit f61f15c

@gendalph
Copy link

@gendalph gendalph commented on f61f15c May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fix doesn't change anything, the warning remains

@gendalph
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[1/1] /usr/bin/c++ -DGTEST_HAS_STD_WSTRING=1 -D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING=1 -I~/src/fmt/include -isystem ~/src/fmt/test/gtest/. -isystem ~/src/fmt/test/gtest/googlemock/include -isystem ~/src/fmt/test/gtest/googletest/include -isystem ~/src/fmt/test/gtest/googletest -isystem ~/src/fmt/test/gtest/googlemock -O2 -DNDEBUG -std=gnu++20 -fvisibility=hidden -fvisibility-inlines-hidden -Wno-stringop-overflow -pedantic-errors -Wall -Wextra -pedantic -Wold-style-cast -Wundef -Wredundant-decls -Wwrite-strings -Wpointer-arith -Wcast-qual -Wformat=2 -Wmissing-include-dirs -Wcast-align -Wctor-dtor-privacy -Wdisabled-optimization -Winvalid-pch -Woverloaded-virtual -Wconversion -Wno-ctor-dtor-privacy -Wno-format-nonliteral -Wno-dangling-else -Wno-unused-local-typedefs -Wdouble-promotion -Wtrampolines -Wzero-as-null-pointer-constant -Wuseless-cast -Wvector-operation-performance -Wsized-deallocation -Wshadow -Wshift-overflow=2 -Wnull-dereference -Wduplicated-cond -MD -MT test/CMakeFiles/args-test.dir/args-test.cc.o -MF test/CMakeFiles/args-test.dir/args-test.cc.o.d -o test/CMakeFiles/args-test.dir/args-test.cc.o -c ~/src/fmt/test/args-test.cc
In file included from ~/src/fmt/include/fmt/args.h:15,
                 from ~/src/fmt/test/args-test.cc:8:
~/src/fmt/include/fmt/core.h: In instantiation of 'constexpr fmt::v9::detail::value<Context> fmt::v9::detail::make_value(T&&) [with Context = fmt::v9::basic_format_context<fmt::v9::appender, char>; T = const custom_type&]':
~/src/fmt/include/fmt/core.h:1701:35:   required from 'constexpr fmt::v9::basic_format_arg<Context> fmt::v9::detail::make_arg(T&&) [with Context = fmt::v9::basic_format_context<fmt::v9::appender, char>; T = const custom_type&]'
~/src/fmt/include/fmt/args.h:125:49:   required from 'void fmt::v9::dynamic_format_arg_store<Context>::emplace_arg(const T&) [with T = custom_type; Context = fmt::v9::basic_format_context<fmt::v9::appender, char>]'
~/src/fmt/include/fmt/args.h:167:18:   required from 'void fmt::v9::dynamic_format_arg_store<Context>::push_back(const T&) [with T = custom_type; Context = fmt::v9::basic_format_context<fmt::v9::appender, char>]'
~/src/fmt/test/args-test.cc:55:18:   required from here
~/src/fmt/include/fmt/core.h:1674:10: warning: possibly dangling reference to a temporary [-Wdangling-reference]
 1674 |   auto&& arg = arg_mapper<Context>().map(FMT_FORWARD(val));
      |          ^~~
~/src/fmt/include/fmt/core.h:1674:41: note: the temporary was destroyed at the end of the full expression 'fmt::v9::detail::arg_mapper<fmt::v9::basic_format_context<fmt::v9::appender, char> >().fmt::v9::detail::arg_mapper<fmt::v9::basic_format_context<fmt::v9::appender, char> >::map<const custom_type&>((* & val))'
 1674 |   auto&& arg = arg_mapper<Context>().map(FMT_FORWARD(val));
      |                ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
~/src/fmt/include/fmt/core.h: In instantiation of 'constexpr fmt::v9::detail::value<Context> fmt::v9::detail::make_value(T&&) [with Context = fmt::v9::basic_format_context<fmt::v9::appender, char>; T = const to_stringable&]':
~/src/fmt/include/fmt/core.h:1701:35:   required from 'constexpr fmt::v9::basic_format_arg<Context> fmt::v9::detail::make_arg(T&&) [with Context = fmt::v9::basic_format_context<fmt::v9::appender, char>; T = const to_stringable&]'
~/src/fmt/include/fmt/args.h:125:49:   required from 'void fmt::v9::dynamic_format_arg_store<Context>::emplace_arg(const T&) [with T = to_stringable; Context = fmt::v9::basic_format_context<fmt::v9::appender, char>]'
~/src/fmt/include/fmt/args.h:167:18:   required from 'void fmt::v9::dynamic_format_arg_store<Context>::push_back(const T&) [with T = to_stringable; Context = fmt::v9::basic_format_context<fmt::v9::appender, char>]'
~/src/fmt/test/args-test.cc:84:18:   required from here
~/src/fmt/include/fmt/core.h:1674:10: warning: possibly dangling reference to a temporary [-Wdangling-reference]
 1674 |   auto&& arg = arg_mapper<Context>().map(FMT_FORWARD(val));
      |          ^~~
~/src/fmt/include/fmt/core.h:1674:41: note: the temporary was destroyed at the end of the full expression 'fmt::v9::detail::arg_mapper<fmt::v9::basic_format_context<fmt::v9::appender, char> >().fmt::v9::detail::arg_mapper<fmt::v9::basic_format_context<fmt::v9::appender, char> >::map<const to_stringable&>((* & val))'
 1674 |   auto&& arg = arg_mapper<Context>().map(FMT_FORWARD(val));
      |                ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
~/src/fmt/include/fmt/core.h: In instantiation of 'constexpr fmt::v9::detail::value<Context> fmt::v9::detail::make_value(T&&) [with Context = fmt::v9::basic_format_context<fmt::v9::appender, char>; T = const copy_throwable&]':
~/src/fmt/include/fmt/core.h:1701:35:   required from 'constexpr fmt::v9::basic_format_arg<Context> fmt::v9::detail::make_arg(T&&) [with Context = fmt::v9::basic_format_context<fmt::v9::appender, char>; T = const copy_throwable&]'
~/src/fmt/include/fmt/args.h:125:49:   required from 'void fmt::v9::dynamic_format_arg_store<Context>::emplace_arg(const T&) [with T = copy_throwable; Context = fmt::v9::basic_format_context<fmt::v9::appender, char>]'
~/src/fmt/include/fmt/args.h:167:18:   required from 'void fmt::v9::dynamic_format_arg_store<Context>::push_back(const T&) [with T = copy_throwable; Context = fmt::v9::basic_format_context<fmt::v9::appender, char>]'
~/src/fmt/test/args-test.cc:171:20:   required from here
~/src/fmt/include/fmt/core.h:1674:10: warning: possibly dangling reference to a temporary [-Wdangling-reference]
 1674 |   auto&& arg = arg_mapper<Context>().map(FMT_FORWARD(val));
      |          ^~~
~/src/fmt/include/fmt/core.h:1674:41: note: the temporary was destroyed at the end of the full expression 'fmt::v9::detail::arg_mapper<fmt::v9::basic_format_context<fmt::v9::appender, char> >().fmt::v9::detail::arg_mapper<fmt::v9::basic_format_context<fmt::v9::appender, char> >::map<const copy_throwable&>((* & val))'
 1674 |   auto&& arg = arg_mapper<Context>().map(FMT_FORWARD(val));
      |                ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~

@vitaut
Copy link
Contributor Author

@vitaut vitaut commented on f61f15c May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fix doesn't change anything, the warning remains

It fixes the warning reported in #3415 which is trivial to check: https://godbolt.org/z/ajsGKe4nE. If you have another example please provide a godbolt repro.

@rubidium42
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know whether this has the same root cause as gendalph's issue, but it's at least very similar, The important bit in the code is the const for the path; without it, it does not create a warning and with it, it does.

#include <fmt/format.h>
#include <fmt/std.h>

int main() {
    const std::filesystem::path from("");
    fmt::print("{}", from);
    return 0;
}

It can also be found at: https://godbolt.org/z/KhcYo4q8z
I'm not sure whether it reproduces on trunk, but I run into the "possibly dangling reference to a temporary" warning at line 1674 of this diff with version 10.0.0.

@vitaut
Copy link
Contributor Author

@vitaut vitaut commented on f61f15c May 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rubidium42, this false positive is also suppressed now (ef55d4f).

Please sign in to comment.