3

In linux 5.4.21 source code include/linux/irqflags.h,

#define raw_local_save_flags(flags)         \
    do {                        \
        typecheck(unsigned long, flags);    \
        flags = arch_local_save_flags();    \
    } while (0)

and in include/linux/typecheck.h

#define typecheck(type,x) \
({  type __dummy; \
    typeof(x) __dummy2; \
    (void)(&__dummy == &__dummy2); \
    1; \
})

I can't understand how the typecheck macro works. So the typecheck macro makes a variable __dummy having type type, and another variable __dummy2 with the same type with x. It then compares the address of those two variables. I guess putting (void) makes the comparison statement not make error.. but what does this comparison do? How can two variables have same address? and what is the last state 1; ? And what effect does this macro have in the raw_local_save_flags macro? (related to how it is called..) Can somebody explain it to me please? Thanks!

Chan Kim
  • 5,177
  • 12
  • 57
  • 112
  • This comparison does nothing if the types match, and makes the compiler complain if they don't. – n. m. could be an AI Feb 03 '22 at 09:51
  • I think it will only generate a compiler warning. TBH, `raw_local_save_flags` and similar macros that save and restore irq flags ought to be changed to generate a compiler error. – Ian Abbott Feb 03 '22 at 10:59
  • 1
    Note that the cast to `void` is merely to avoid a possible compiler warning about a statement with no effect; most compilers will suppress such a warning if the expression is cast to `void`. – Nate Eldredge Feb 03 '22 at 15:21

1 Answers1

0

The comparison &__dummy == &__dummy2 is only permitted if the objects are of the same type, so a compiler will complain if they are not.

See this post about Compound Statements. You can supply a brace-surrounded block (a compound statement) as a valid expression whose value is the last expression in the block.

So in ({ int x = c; x; }) the value of the expression is evaluated as x;

mmixLinus
  • 1,646
  • 2
  • 11
  • 16