ThrowTheSwitch / Unity

Simple Unit Testing for C
ThrowTheSwitch.org
MIT License
4.02k stars 969 forks source link

`_VSTD::isnan` – "error: expected unqualified-id" on Clang #660

Closed MacDada closed 10 months ago

MacDada commented 1 year ago

Hey, it's me with C++ again ;)

I've just changed the stock Apple's Clang into the "generic" Clang from Homebrew. Everything works but one thing – some of my unity tests stopped working.

➜  DnWiFiDoorLock git:(master) ✗ clang -v
Homebrew clang version 15.0.7
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
➜  DnWiFiDoorLock git:(master) ✗ pio test
Verbosity level can be increased via `-v, -vv, or -vvv` option
Collected 23 tests

Processing native/DnAppLogger/test_Logger in native environment
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Building...
Library Manager: Installing throwtheswitch/Unity @ ^2.5.2
Unpacking  [####################################]  100%
Library Manager: Unity@2.5.2 has been installed!
In file included from test/native/DnAppLogger/test_Logger/LoggerTest.cpp:3:
In file included from lib/DnAppLogger/src/DnApp/Logger/Logger.h:3:
In file included from /usr/local/opt/llvm/bin/../include/c++/v1/memory:853:
In file included from /usr/local/opt/llvm/bin/../include/c++/v1/__memory/compressed_pair.h:16:
In file included from /usr/local/opt/llvm/bin/../include/c++/v1/tuple:227:
In file included from /usr/local/opt/llvm/bin/../include/c++/v1/utility:251:
In file included from /usr/local/opt/llvm/bin/../include/c++/v1/compare:145:
In file included from /usr/local/opt/llvm/bin/../include/c++/v1/__compare/compare_partial_order_fallback.h:13:
In file included from /usr/local/opt/llvm/bin/../include/c++/v1/__compare/partial_order.h:14:
In file included from /usr/local/opt/llvm/bin/../include/c++/v1/__compare/weak_order.h:14:
/usr/local/opt/llvm/bin/../include/c++/v1/__compare/strong_order.h:73:44: error: expected unqualified-id
                    if (__t == 0 || _VSTD::isinf(__t)) {
                                           ^
.pio/libdeps/native/Unity/src/unity_internals.h:211:18: note: expanded from macro 'isinf'
#define isinf(n) (isnan((n) - (n)) && !isnan(n))
                 ^

////// and many more like that

If I go and remove the isnan macro from unity_internals.h. everything is starting to work again.

The upgrade in my project.

jac4e commented 1 year ago

I encountered the same issue. A workaround that I used was to use macros to use unity's definitions for isnan and isinf when unit testing, then use the std::isinf and std::isnan otherwise.

#ifdef UNIT_TESTING
#define _ISNAN(x) isnan(x)
#define _ISINF(x) isinf(x)
#else
#define _ISNAN(x) std::isnan(x)
#define _ISINF(x) std::isinf(x)
#endif // UNIT_TESTING

I am unsure what the implications of this workaround are, but at least it allows for the use of unity without modifying it.

DavidSchinazi commented 11 months ago

I just hit the same issue with Apple's default toolchain. The workaround above didn't work for me, but I was able to find another one. I added a custom unity config.h with #define UNITY_EXCLUDE_FLOAT in it. This only works for me because I don't use floating point math in unity itself, but it'll have to do for now.

jonathangjertsen commented 11 months ago

I encountered a similar issue when evaluating Au, which defines an isnan template function. I think this macro should be renamed to UNITY_ISNAN or similar, isnan is too generic.

Note: my workaround here was to add #undef isnan to the top of the generated au.h file. In cmake:

add_custom_target(
    au-rebuild
    COMMAND echo \#undef isnan > ${CMAKE_CURRENT_SOURCE_DIR}/prebuilt/au-prebuilt.h
    COMMAND ${PYTHON_EXE} tools/bin/make-single-file --noio --units ${UNITS} >> ${CMAKE_CURRENT_SOURCE_DIR}/prebuilt/au-prebuilt.h
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/au
)
mvandervoord commented 11 months ago

Good point. I agree.

mvandervoord commented 10 months ago

Thanks everyone. I believe the cutting edge has this fixed now.

DavidSchinazi commented 10 months ago

Thank you! For anyone wondering, this was fixed in a1b1600e4361cdf98bdb57e7d32a1cafa6eb90bf and is included in v2.6.0 RC1.

mvandervoord commented 10 months ago

Thanks @DavidSchinazi ! I should have included those details. Thanks for picking up the slack!