|
| 1 | +#ifndef CCAN_CHECK_TYPE_H |
| 2 | +#define CCAN_CHECK_TYPE_H |
| 3 | + |
| 4 | +/** |
| 5 | + * check_type - issue a warning or build failure if type is not correct. |
| 6 | + * @expr: the expression whose type we should check (not evaluated). |
| 7 | + * @type: the exact type we expect the expression to be. |
| 8 | + * |
| 9 | + * This macro is usually used within other macros to try to ensure that a macro |
| 10 | + * argument is of the expected type. No type promotion of the expression is |
| 11 | + * done: an unsigned int is not the same as an int! |
| 12 | + * |
| 13 | + * check_type() always evaluates to 1. |
| 14 | + * |
| 15 | + * If your compiler does not support typeof, then the best we can do is fail |
| 16 | + * to compile if the sizes of the types are unequal (a less complete check). |
| 17 | + * |
| 18 | + * Example: |
| 19 | + * // They should always pass a 64-bit value to _set_some_value! |
| 20 | + * #define set_some_value(expr) \ |
| 21 | + * _set_some_value((check_type((expr), uint64_t), (expr))) |
| 22 | + */ |
| 23 | + |
| 24 | +/** |
| 25 | + * check_types_match - issue a warning or build failure if types are not same. |
| 26 | + * @expr1: the first expression (not evaluated). |
| 27 | + * @expr2: the second expression (not evaluated). |
| 28 | + * |
| 29 | + * This macro is usually used within other macros to try to ensure that |
| 30 | + * arguments are of identical types. No type promotion of the expressions is |
| 31 | + * done: an unsigned int is not the same as an int! |
| 32 | + * |
| 33 | + * check_types_match() always evaluates to 0. |
| 34 | + * |
| 35 | + * If your compiler does not support typeof, then the best we can do is fail |
| 36 | + * to compile if the sizes of the types are unequal (a less complete check). |
| 37 | + * |
| 38 | + * Example: |
| 39 | + * // Do subtraction to get to enclosing type, but make sure that |
| 40 | + * // pointer is of correct type for that member. |
| 41 | + * #define container_of(mbr_ptr, encl_type, mbr) \ |
| 42 | + * (check_types_match((mbr_ptr), &((encl_type *)0)->mbr), \ |
| 43 | + * ((encl_type *) \ |
| 44 | + * ((char *)(mbr_ptr) - offsetof(enclosing_type, mbr)))) |
| 45 | + */ |
| 46 | +#define check_type(expr, type) \ |
| 47 | + ((typeof(expr) *)0 != (type *)0) |
| 48 | + |
| 49 | +#define check_types_match(expr1, expr2) \ |
| 50 | + ((typeof(expr1) *)0 != (typeof(expr2) *)0) |
| 51 | + |
| 52 | +#endif /* CCAN_CHECK_TYPE_H */ |
0 commit comments