How can I prevent GCC from eliminating the code inside if(0) block?
When I use Visual Studio, one of my debugging techniques is to put code like this in my program:
if (0)
do_some_debug_printing_and_checking();
Then, when a breakpoint is hit, I click at the do_some_debug_printing_and_checking() line, select "set next statement" and force it to execute.
When I use gcc/gdb as a back-end, the "set next statement" does not work anymore, as GCC simply removes the code from inside the if(0) statement.
I am of course using the -O0 flag to disable optimization. I have also tried the -fno-dce -fno-tree-dce flags to disable dead code elimination explicitly, but it has no effect: the contents of if(0) is just not present in the binary file and I cannot use set next statement to jump into it.
Is there any good way to tell gcc to disable elimination of if(0) contents?
Edit:
Thanks for the "additional variable" workaround, however there are 2 things I don't like about it:
- It's still an extra line of code
- It won't be automatically optimized out when I build the release version and do want those debug things to disappear. Surely I can use #ifdef-s, but that's even more extra lines.
Really, there's absolutely no option to make GCC keep that dead code?