1

I am working on removing warnings from legacy code when I encountered the below macro

#define DISBALE_DEBUG
#ifdef DISBALE_DEBUG
    #define Dbg(fmt,...) (0) 
#else
    #define Dbg print
#endif

and used in the below code:

#ifdef __arm__
    Dbg("\n %s %s %d  %d \n", __FILE__, __FUNCTION__, __LINE__, mode);
#endif

for which i get the warning: ** expression has no effect**

jxgn
  • 741
  • 2
  • 15
  • 36

1 Answers1

1

If you enter the DISBALE_DEBUG #ifdef branch, the Dbg macro will be defined as a variadic macro which just consumes its variadic arguments and does nothing with them. I.e., a call such as

Dbg("\n %s %s %d  %d \n", __FILE__, __FUNCTION__, __LINE__, mode);

will resolve to

(0);

which, as the compiler accurately warns you about, has no effect.

If you enter the #else branch, on the other hand, the Dbg macro will just be a replacement, by the pre-processor, with print, meaning

 Dbg("\n %s %s %d  %d \n", __FILE__, __FUNCTION__, __LINE__, mode);

resolves to

print("\n %s %s %d  %d \n", __FILE__, __FUNCTION__, __LINE__, mode);

Now, given the information in your question, it is unclear what print(...) will resolve to, as it is not a standard function in C++ nor C. It's likely another variadic macro or a variadic function.

dfrib
  • 70,367
  • 12
  • 127
  • 192
  • Great thanks :) so now this warning has to be suppressed done? cause it is intended like that. – jxgn Jun 26 '20 at 09:15
  • yes you are correct print(...) is another variadic function. – jxgn Jun 26 '20 at 09:17
  • @XavierGeoffrey If you may modify this macro, I guess you could try changing it into `#define Dbg(fmt,...) (void)(0)` to possibly avoid the "expression has no effect" warning. Note also, based on the snippet you showed us, we will always enter the `#ifdef` branch and the macro will always expand to nothing. Finally, it may be good to noticed that there's a typo in the pre-processor constant, `DISBALE_DEBUG` should arguably be `DISABLE_DEBUG`, which can lead to confusion in case someone intents to make use of this from the build system. – dfrib Jun 26 '20 at 09:20
  • 2
    @XavierGeoffrey Happy to help! Good luck with your legacy code base, and consider having a look into [GCC's doc page on macros](https://gcc.gnu.org/onlinedocs/cpp/Macros.html#Macros) if you run into more macro confusion. – dfrib Jun 26 '20 at 09:25