GCC Obfuscated Code
For years I've struggled to understand the GNU compiler collection internals, I am ashamed to say, without much success. I always thought that the subject was intrinsically too complicated for me, but after struggling to understand a two line gcc code snippet of a fairly simple operation for more than two minutes, I realized that the code style may have something to do with my problems.
Here is the relevant code:
#define DEFAULT_SWITCH_TAKES_ARG(CHAR) \ ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \ || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \ || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \ || (CHAR) == 'L' || (CHAR) == 'A' || (CHAR) == 'V' \ || (CHAR) == 'B' || (CHAR) == 'b') /* [...] */ const char *p = argv[i] + 1; int c = *p; int nskip = 1; if (SWITCH_TAKES_ARG (c) > (p != 0)) nskip += SWITCH_TAKES_ARG (c) - (p != 0);
Here is my understanding.
The two elements of the
if statements are Boolean expressions,
and therefore take values 0 or 1.
Therefore, the inequality will be true only when the first term is 1
and the second 0.
This is the only case that will allow the execution of the next
statement, so the result of the subtraction will always be 1.
if statement could (and, I believe, should)
be written as:
if (SWITCH_TAKES_ARG (c) && (p == 0)) nskip++;