typecheck(): Difference between revisions

From vegard.wiki
Jump to navigation Jump to search
Content added Content deleted
(new page)
 
(No difference)

Latest revision as of 09:00, 10 March 2020

It is possible (although not straightforward) to typecheck an expression (and get warnings, etc.) without actually compiling the expression.

Definition:

#define typecheck(...) \
    do { \
        (void) __builtin_constant_p(__VA_ARGS__); \
    } while(0)

Usage:

void foo()
{
    int i = -1;
    unsigned int j = 1;

    // warning: comparison of integer expressions of different signedness: 'int' and 'unsigned int'
    typecheck(i != j);
}

Note: this only works in gcc/clang.

See also