Enforcement
include/enforce.h View source View on GitHubMacros
Section titled “Macros”_enforce_msg
Section titled “_enforce_msg”#define _enforce_msg(s) source Expansion
#define _enforce_msg(s) "Enforcement \"" s "\" failed" _enforce_eval
Section titled “_enforce_eval”#define _enforce_eval(x, ...) source Expansion
#define _enforce_eval(x, ...) \
_enforce_eval_as(x, PP_CONCAT(_enforce_res_, __COUNTER__), __VA_ARGS__) _enforce_eval_as
Section titled “_enforce_eval_as”#define _enforce_eval_as(x, res_, ...) source Expansion
#define _enforce_eval_as(x, res_, ...) \
({ \
ct_decay(x) res_ = (x); \
if (cc_unlikely(!(res_))) { \
__VA_ARGS__; \
ci_unreachable(); \
} \
res_; \
}) _enforce_pick
Section titled “_enforce_pick”#define _enforce_pick(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, \ _13, _14, _15, _16, NAME, ...) source Expansion
#define _enforce_pick(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, \
_13, _14, _15, _16, NAME, ...) \
NAME _enforce_arity
Section titled “_enforce_arity”#define _enforce_arity(...) source Expansion
#define _enforce_arity(...) \
_enforce_pick(_, ##__VA_ARGS__, _enforce_n, _enforce_n, _enforce_n, \
_enforce_n, _enforce_n, _enforce_n, _enforce_n, _enforce_n, \
_enforce_n, _enforce_n, _enforce_n, _enforce_n, _enforce_n, \
_enforce_n, _enforce_n, _enforce_1, _enforce_0) _enforce_fn_as
Section titled “_enforce_fn_as”#define _enforce_fn_as(a, msg_fn, code_fn) source Expansion
#define _enforce_fn_as(a, msg_fn, code_fn) \
cc_generic((a), char * : msg_fn, const char * : msg_fn, default : code_fn) _enforce_fn
Section titled “_enforce_fn”#define _enforce_fn(a, code_fn) source Expansion
#define _enforce_fn(a, code_fn) _enforce_fn_as(a, enforce_impl_msg, code_fn) _enforce_ctx
Section titled “_enforce_ctx”#define _enforce_ctx(prefix, s) source Expansion
#define _enforce_ctx(prefix, s) \
__FILE__, __LINE__, __func__, prefix, _enforce_msg(s) _enforce_0
Section titled “_enforce_0”#define _enforce_0(prefix, s, x) source enforce(x)Expansion
#define _enforce_0(prefix, s, x) \
_enforce_eval(x, enforce_impl_code_only(_enforce_ctx(prefix, s), NULL)) _enforce_1
Section titled “_enforce_1”#define _enforce_1(prefix, s, x, a) source enforce(x, "msg")enforce(x, INVALID_ARGUMENT)enforce(x, (asan, POISONED))Code forms are documented at CRASH_CODE_ARG in console/crash.h
Expansion
#define _enforce_1(prefix, s, x, a) \
_enforce_eval( \
x, CRASH_CODE_SCOPED( \
a, _enforce_fn(CRASH_CODE_ARG(a), enforce_impl_code_only)( \
_enforce_ctx(prefix, s), CRASH_CODE_ARG(a)))) _enforce_n
Section titled “_enforce_n”#define _enforce_n(prefix, s, x, a, ...) source enforce(x, "msg %d", 12)enforce(x, INVALID_ARGUMENT, "msg %d", 12)enforce(x, (asan, POISONED), "msg %d", 12)Expansion
#define _enforce_n(prefix, s, x, a, ...) \
_enforce_eval(x, CRASH_CODE_SCOPED( \
a, _enforce_fn(CRASH_CODE_ARG(a), enforce_impl_code)( \
_enforce_ctx(prefix, s), CRASH_CODE_ARG(a), \
__VA_ARGS__))) _enforce_fail
Section titled “_enforce_fail”#define _enforce_fail(prefix, ...) source Expansion
#define _enforce_fail(prefix, ...) \
enforce_impl_msg(__FILE__, __LINE__, __func__, prefix, NULL, "" __VA_ARGS__) enforce
Section titled “enforce”#define enforce(x, ...) source Expansion
#define enforce(x, ...) _enforce_arity(__VA_ARGS__)("", #x, x, ##__VA_ARGS__) _precond_msg
Section titled “_precond_msg”#define _precond_msg(s) source Expansion
#define _precond_msg(s) "Precondition \"" s "\" failed" _precond_msg
Section titled “_precond_msg”#define _precond_msg(s) source Expansion
#define _precond_msg(s) "Precondition failed" _precond_ctx
Section titled “_precond_ctx”#define _precond_ctx(s) source Expansion
#define _precond_ctx(s) __FILE__, __LINE__, __func__, "", _precond_msg(s) _precond_arity
Section titled “_precond_arity”#define _precond_arity(...) source Expansion
#define _precond_arity(...) \
_enforce_pick(_, ##__VA_ARGS__, _precond_n, _precond_n, _precond_n, \
_precond_n, _precond_n, _precond_n, _precond_n, _precond_n, \
_precond_n, _precond_n, _precond_n, _precond_n, _precond_n, \
_precond_n, _precond_n, _precond_1, _precond_0) _precond_0
Section titled “_precond_0”#define _precond_0(s, x) source Expansion
#define _precond_0(s, x) \
_enforce_eval( \
x, enforce_impl_code_only(_precond_ctx(s), \
CRASH_CODE(global, INVALID_ARGUMENT))) _precond_1
Section titled “_precond_1”#define _precond_1(s, x, a) source Expansion
#define _precond_1(s, x, a) \
_enforce_eval( \
x, CRASH_CODE_SCOPED( \
a, _enforce_fn_as(CRASH_CODE_ARG(a), enforce_impl_precond_msg, \
enforce_impl_code_only)(_precond_ctx(s), \
CRASH_CODE_ARG(a)))) _precond_n
Section titled “_precond_n”#define _precond_n(s, x, a, ...) source Expansion
#define _precond_n(s, x, a, ...) \
_enforce_eval( \
x, CRASH_CODE_SCOPED( \
a, _enforce_fn_as(CRASH_CODE_ARG(a), enforce_impl_precond_msg, \
enforce_impl_code)( \
_precond_ctx(s), CRASH_CODE_ARG(a), __VA_ARGS__))) enforce_precond
Section titled “enforce_precond”#define enforce_precond(x, ...) source Expansion
#define enforce_precond(x, ...) \
_precond_arity(__VA_ARGS__)(#x, x, ##__VA_ARGS__) unimplemented
Section titled “unimplemented”#define unimplemented(...) source Expansion
#define unimplemented(...) _enforce_fail("unimplemented! ", ##__VA_ARGS__) #define todo(...) source Expansion
#define todo(...) _enforce_fail("TODO: ", ##__VA_ARGS__) unreachable
Section titled “unreachable”#define unreachable(...) source Expansion
#define unreachable(...) \
enforce_impl_code(__FILE__, __LINE__, __func__, "unreachable! ", NULL, \
CRASH_CODE(global, UNREACHABLE), "" __VA_ARGS__) _assume_static_checks
Section titled “_assume_static_checks”#define _assume_static_checks(x) source Expansion
#define _assume_static_checks(x) \
((void) sizeof(({ \
cc_assume(x); \
_assume_not_constant_false(x); \
0; \
}))) _assume_msg_only
Section titled “_assume_msg_only”#define _assume_msg_only(...) source Expansion
#define _assume_msg_only(...) \
static_assert(!PP_IS_PAREN(PP_GET_1(__VA_ARGS__, ~)), \
"assume() takes no crash code"); \
(void) sizeof(_assume_fmt("" __VA_ARGS__)) assume
Section titled “assume”#define assume(x, ...) source Expansion
#define assume(x, ...) \
({ \
_assume_static_checks(x); \
_assume_msg_only(__VA_ARGS__); \
if (cc_unlikely(!(x))) \
enforce_impl_msg(_enforce_ctx("assume! ", #x), "" __VA_ARGS__); \
}) assume
Section titled “assume”#define assume(x, ...) source Expansion
#define assume(x, ...) \
({ \
_assume_static_checks(x); \
_assume_msg_only(__VA_ARGS__); \
cc_assume(x); \
})