Skip to content

silence warning C4100 on MSVC 2019 when exceptions are disabled - #2397

Merged
vitaut merged 1 commit into
fmtlib:masterfrom
mattiasljungstrom:master
Jul 2, 2021
Merged

vitaut merged 1 commit into
fmtlib:masterfrom
mattiasljungstrom:master

Conversation

@mattiasljungstrom

Copy link
Copy Markdown
Contributor

Removes the following warnings under MSVC 2019 (16.10.2), with W4 enabled and when exceptions are disabled. (/W4 -D_HAS_EXCEPTIONS=0)

fmt\include\fmt\format.h(810,44): warning C4100: 'message': unreferenced formal parameter
fmt\include\fmt/format-inl.h(2553,59): warning C4100: 'message': unreferenced formal parameter

@mattiasljungstrom mattiasljungstrom changed the title silence warning C4100 on MSVC when exceptions are disabled silence warning C4100 on MSVC 2019 when exceptions are disabled Jun 28, 2021
@vitaut

vitaut commented Jun 29, 2021

Copy link
Copy Markdown
Contributor

This is strange because FMT_THROW always uses the argument:

fmt/include/fmt/format.h

Lines 91 to 100 in a3f762c

# define FMT_THROW(x) detail::do_throw(x)
# else
# define FMT_THROW(x) throw x
# endif
# else
# define FMT_THROW(x) \
do { \
FMT_ASSERT(false, (x).what()); \
} while (false)
# endif

Do you have a godbolt repro?

@mattiasljungstrom

Copy link
Copy Markdown
Contributor Author

I believe that in release mode the FMT_ASSERT is declared ((void)0), which means that the compiler removes everything in the FMT_THROW(x) macro. (Given that exceptions are disabled.)

@vitaut

vitaut commented Jun 29, 2021

Copy link
Copy Markdown
Contributor

Right. In this case it should be fixed in the FMT_ASSERT macro, not in individual call sites.

@phprus

phprus commented Jun 29, 2021

Copy link
Copy Markdown
Contributor

@vitaut, maybe replace

# define FMT_ASSERT(condition, message) ((void)0)

to:

FMT_INLINE void assert_ignore(bool, const char*) {}
#    define FMT_ASSERT(condition, message) ::fmt::detail::assert_ignore((condition), (message))

?

@mattiasljungstrom, please check this solution.

@mattiasljungstrom

mattiasljungstrom commented Jun 29, 2021

Copy link
Copy Markdown
Contributor Author

@vitaut makes sense!

This works for my case:

#    define FMT_ASSERT(condition, message) ((void)message) 

Or this works for MSVC (note constexpr needed), haven't tried other compilers:

FMT_INLINE constexpr void assert_ignore(bool, const char*) {}
#    define FMT_ASSERT(condition, message) ::fmt::detail::assert_ignore((condition), (message))

@mattiasljungstrom

Copy link
Copy Markdown
Contributor Author

I've updated the PR to use the assert_ignore() solution for FMT_ASSERT.

@vitaut

vitaut commented Jul 2, 2021

Copy link
Copy Markdown
Contributor

Can we use the newly added ignore_unused

template <class T> void ignore_unused(const T&) {}

instead of introducing a new function?

@mattiasljungstrom

Copy link
Copy Markdown
Contributor Author

Sure! I've changed the code to use the ::fmt::ignore_unused(), but I had to add FMT_CONSTEXPR since the assert is sometimes used in constexpr functions.

@phprus

phprus commented Jul 2, 2021

Copy link
Copy Markdown
Contributor

Maybe rewrite ignore_unused function as:

template <class ...Ts> FMT_CONSTEXPR void ignore_unused(const Ts& ...) {}

and remove do...while block?

@vitaut

vitaut commented Jul 2, 2021

Copy link
Copy Markdown
Contributor

Maybe rewrite ignore_unused function as

Was about to suggest the same =)

@mattiasljungstrom

Copy link
Copy Markdown
Contributor Author

updated code with @phprus suggestions.

@vitaut
vitaut merged commit 54014e4 into fmtlib:master Jul 2, 2021
@vitaut

vitaut commented Jul 2, 2021

Copy link
Copy Markdown
Contributor

Thank you!

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 this pull request may close these issues.

3 participants