I have a function dangerous(GEN x)
which is called frequently in my code, where GEN
is a typedef
. For debugging purposes, I would like to add checkSafe
to all instances of this function, something like
#ifdef DEBUG
#define dangerous(x) GEN __x = (x); if(checkSafe(__x)) dangerous(__x)
#endif
but I'm concerned that this might not work as intended. What's the right way to do something like this? The function is used too often to instrument each use individually and it is not desirable to check outside debug mode (for various reasons).
Things to be aware of / careful about:
Solution: rename the original function into something like _dangerous
.
if
with a single statement: if (foo) dangerous(x);
else
from the parent if
: if (foo) dangerous(x); else bar();
GEN __x = 5; dangerous(__x);
.Solution: enclose the macro in a construct like do { ... } while(0)
.
GEN
is a typedef, this is likely not a concern).Lastly, you may also want to complain when checkSafe fails, e.g. by logging an error message, or even aborting the program.
Putting the above together, you would instrument the function like this:
#ifdef DEBUG
#define dangerous(x) do { \
GEN __x = (x); \
if (checkSafe(__x)) \
_dangerous(__x); \
else \
complainAbout(__x); \
} while(0)
#else
#define dangerous _dangerous
#endif
dangerous()
returns a value (e.g. int
) that you want to use.Solution: Define a function to instrument your original function and pass the return value up:
#ifdef DEBUG
static inline int dangerous(GEN x) {
if (checkSafe(x))
return _dangerous(x);
complainAbout(x);
return ERROR_CODE;
}
#else
#define dangerous _dangerous
#endif