Fix compiler warning about unused parameters
Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
42
src/utils.h
42
src/utils.h
@ -36,7 +36,7 @@ safe_isnan(double a) {
|
||||
#define CASESTRRET(s) \
|
||||
case s: return #s
|
||||
|
||||
/// Same as assert false, but make sure we abort _even in release builds_.
|
||||
/// Same as assert(false), but make sure we abort _even in release builds_.
|
||||
/// Silence compiler warning caused by release builds making some code paths reachable.
|
||||
#define BUG() \
|
||||
do { \
|
||||
@ -47,11 +47,35 @@ safe_isnan(double a) {
|
||||
/// Same as assert, but evaluates the expression even in release builds
|
||||
#define CHECK(expr) \
|
||||
do { \
|
||||
__auto_type _ = (expr); \
|
||||
auto _ = (expr); \
|
||||
/* make sure the original expression appears in the assertion message */ \
|
||||
assert((CHECK_EXPR(expr), _)); \
|
||||
(void)_; \
|
||||
} while (0)
|
||||
|
||||
/// Asserts that var is within [lower, upper]. Silence compiler warning about expressions
|
||||
/// being always true or false.
|
||||
#define ASSERT_IN_RANGE(var, lower, upper) \
|
||||
do { \
|
||||
auto __tmp = (var); \
|
||||
_Pragma("GCC diagnostic push"); \
|
||||
_Pragma("GCC diagnostic ignored \"-Wtype-limits\""); \
|
||||
assert(__tmp >= lower); \
|
||||
assert(__tmp <= upper); \
|
||||
_Pragma("GCC diagnostic pop"); \
|
||||
} while (0)
|
||||
|
||||
/// Asserts that var >= lower. Silence compiler warning about expressions
|
||||
/// being always true or false.
|
||||
#define ASSERT_GEQ(var, lower) \
|
||||
do { \
|
||||
auto __tmp = (var); \
|
||||
_Pragma("GCC diagnostic push"); \
|
||||
_Pragma("GCC diagnostic ignored \"-Wtype-limits\""); \
|
||||
assert(__tmp >= lower); \
|
||||
_Pragma("GCC diagnostic pop"); \
|
||||
} while (0)
|
||||
|
||||
// Some macros for checked cast
|
||||
// Note these macros are not complete, as in, they won't work for every integer types. But
|
||||
// they are good enough for compton.
|
||||
@ -59,28 +83,28 @@ safe_isnan(double a) {
|
||||
#define to_int_checked(val) \
|
||||
({ \
|
||||
int64_t tmp = (val); \
|
||||
assert(tmp >= INT_MIN && tmp <= INT_MAX); \
|
||||
ASSERT_IN_RANGE(tmp, INT_MIN, INT_MAX); \
|
||||
(int)tmp; \
|
||||
})
|
||||
|
||||
#define to_char_checked(val) \
|
||||
({ \
|
||||
int64_t tmp = (val); \
|
||||
assert(tmp >= CHAR_MIN && tmp <= CHAR_MAX); \
|
||||
ASSERT_IN_RANGE(tmp, CHAR_MIN, CHAR_MAX); \
|
||||
(char)tmp; \
|
||||
})
|
||||
|
||||
#define to_u16_checked(val) \
|
||||
({ \
|
||||
auto tmp = (val); \
|
||||
assert(tmp >= 0 && tmp <= UINT16_MAX); \
|
||||
ASSERT_IN_RANGE(tmp, 0, UINT16_MAX); \
|
||||
(uint16_t) tmp; \
|
||||
})
|
||||
|
||||
#define to_i16_checked(val) \
|
||||
({ \
|
||||
int64_t tmp = (val); \
|
||||
assert(tmp >= INT16_MIN && tmp <= INT16_MAX); \
|
||||
ASSERT_IN_RANGE(tmp, INT16_MIN, INT16_MAX); \
|
||||
(int16_t) tmp; \
|
||||
})
|
||||
|
||||
@ -89,7 +113,7 @@ safe_isnan(double a) {
|
||||
auto tmp = (val); \
|
||||
int64_t max = UINT32_MAX; /* silence clang tautological \
|
||||
comparison warning*/ \
|
||||
CHECK(tmp >= 0 && tmp <= max); \
|
||||
ASSERT_IN_RANGE(tmp, 0, max); \
|
||||
(uint32_t) tmp; \
|
||||
})
|
||||
/**
|
||||
@ -171,7 +195,7 @@ allocchk_(const char *func_name, const char *file, unsigned int line, void *ptr)
|
||||
#define ccalloc(nmemb, type) \
|
||||
({ \
|
||||
auto tmp = (nmemb); \
|
||||
assert(tmp >= 0); \
|
||||
ASSERT_GEQ(tmp, 0); \
|
||||
((type *)allocchk(calloc((size_t)tmp, sizeof(type)))); \
|
||||
})
|
||||
|
||||
@ -179,7 +203,7 @@ allocchk_(const char *func_name, const char *file, unsigned int line, void *ptr)
|
||||
#define crealloc(ptr, nmemb) \
|
||||
({ \
|
||||
auto tmp = (nmemb); \
|
||||
assert(tmp >= 0); \
|
||||
ASSERT_GEQ(tmp, 0); \
|
||||
((__typeof__(ptr))allocchk(realloc((ptr), (size_t)tmp * sizeof(*(ptr))))); \
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user