I am reading "No Bugs!" by David Thielen and chapter 3 discusses a way to determine if a given code path has been hit. It suggests having a macro that check an argument, and, if it is true, executes an assembly instruction that generates a specific interrupt (0x3) to create a debugger breakpoint. If the argument to the macro is false, it simply does nothing.
The macro, then, look like this:
#ifdef DEBUG
#define Trap(t) ( (t) ? __asm__("int $0x3") : )
#endif
This code, however, causes a compile error for gcc:
int_test.c:16:35: error: expected expression before ‘__asm__’
I learned from here that because gcc's asm is a block statement and not an expression, I must use statement expressions in order to use the asm in this way. So, now it becomes:
#define Trap(t) ( (t) ? ({ __asm__("int $0x3"); }) : )
The compiler still complains:
int_test.c:16:64: error: expected expression before ‘)’ token
Okay, so now I have to do this?
#define Trap(t) ( (t) ? ({ __asm__("int $0x3"); }) : ({ ; }) )
That seems really dumb. Can't I just have the preprocessor insert nothing if t is false without using this annoying syntax?
Note: I have left out a few other similar macros for simplicity and I have adapted the syntax from the book to work for gcc (such as replacing the book's _asm with asm as well as using AT&T syntax and surrounding the assmembly in "")