Thursday, April 28, 2022

[FIXED] What does #define Dbg(fmt,...) (0) mean? warning: expression has no effect

Issue

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**


Solution

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.



Answered By - dfrib
Answer Checked By - Gilberto Lyons (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.