How does the typecheck macro from the Linux kernel work?

前端 未结 2 1495
眼角桃花
眼角桃花 2020-12-31 04:38

The file include/linux/typecheck.h of the Linux kernel 4.16 contains this code.

#define typecheck(type,x) \\
({      type __dummy; \\
        ty         


        
2条回答
  •  醉酒成梦
    2020-12-31 05:15

    This uses two GCC extensions — expression statements ({ ... }) and typeof().

    1. The first line of the expansion declares a variable of the named type type.
    2. The second line of the expansion declares a variable of the same type as the variable or expression x.
    3. The third line compares the two pointers, which will only match if the types of the two dummy variables match, generating a pointer mismatch warning (or error if compiling with -Werror).
    4. The last line (containing the 1) is the value of the expression — equivalent to true.

    So, you get a compilation warning/error if the type of x is not the same as the named type.

    Example code:

    #include 
    
    #define typecheck(type,x) \
    ({      type __dummy; \
            typeof(x) __dummy2; \
            (void)(&__dummy == &__dummy2); \
            1; \
    })
    
    int main(void)
    {
        int x;
        if (typecheck(int, x))
            printf("int x OK\n");
        if (typecheck(double, x))
            printf("double x OK\n");
        return(0);
    }
    

    Compilation message:

    $ /usr/bin/gcc -O3 -g -std=gnu99 -Wall -Wextra xx.c -o xx  
    xx.c: In function ‘main’:
    xx.c:15: warning: comparison of distinct pointer types lacks a cast
    $
    

    Note that because I did not use -Werror, the code compiled 'OK'. The output was:

    int x OK
    double x OK
    

提交回复
热议问题