typecheck()

From vegard.wiki
Revision as of 09:00, 10 March 2020 by Vegard (talk | contribs) (new page)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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