googletest icon indicating copy to clipboard operation
googletest copied to clipboard

Protect against -Wundef

Open adambadura opened this issue 4 years ago • 5 comments

-Wundef detects cases where an undefined symbol is evaluated in the context of #if and defaults to 0. While this default behavior could sometimes be a nice shortcut it can also hide issues like lack of proper configuration or a misspelled macro.

Furthermore, clients adding the library by CMake add_subdirectory might be hit by the problem of -Wundef triggered in the library itself. Protecting against it also simplifies client scenarios by not requiring the client to disable the flag in the library builds.

For "detection macros" this is done by either adding proper #else cases (in simplest scenarios) or by appending a block:

#ifndef <VAR>
# define <VAR> 0
#endif

which avoids complexity while ensuring the macro is always defined.

For "configuration macros" this is done by adding a similar block prior to the first use to ensure it is always defined.

adambadura avatar Jan 11 '21 08:01 adambadura

Sorry for the delayed response on this one. Unfortunately we can't take this as is because it is a breaking change. For example, any code doing

#ifdef GTEST_HAS_DEATH_TEST

will be broken by this.

The style used in GoogleTest is inconsistent at times. Some macros are always defined to 0 or 1, others are either defined to 1 or undefined. Resolving this is going to be more complicated.

derekmauro avatar Apr 16 '21 20:04 derekmauro

@derekmauro, so... should I abandon this pull request? Or do you see any way to work around it? Can we perhaps determine which symbols might be used this way? Or go the other way and stop relying on value where it wasn't clearly specified?

adambadura avatar Apr 17 '21 19:04 adambadura

I haven't completely thought it through, but my first suggestion would be to look into changing #if to #ifdef or #if defined() for the macros that are either defined to 1 or undefined.

Let us know if you are interested in doing this.

derekmauro avatar Apr 19 '21 14:04 derekmauro

@derekmauro, I will try to rewrite it. It may take some time thou, so if you are in a rush with this fix, do it yourself then.

adambadura avatar Apr 20 '21 05:04 adambadura

This should be fixed as of commit d92a270d2d9d6bb7ed3bf7dc3f698028550a65ef.

thughes avatar Mar 06 '23 18:03 thughes