diff options
author | Gwendal Grignou <gwendal@chromium.org> | 2018-10-16 10:32:25 -0700 |
---|---|---|
committer | ChromeOS Commit Bot <chromeos-commit-bot@chromium.org> | 2019-05-07 12:55:24 +0000 |
commit | 470b1a065d966a3e1fa73f40ee8138b7eed7e177 (patch) | |
tree | d60fd6749566906c507fe9714fe2944e84b8975f | |
parent | 86560650851f2b9d6839992fc3910e0646b6348d (diff) | |
download | chrome-ec-470b1a065d966a3e1fa73f40ee8138b7eed7e177.tar.gz |
ec: Add macro to check if constant index is too large
BUILD_ASSERT can only be used for declarative code, add a macro to check
if a condition resolved at build time is true or not.
Take advantage of compiler detection of division by zero error message.
[-Werror=div-by-zero]
BUG=none
BRANCH=eve,nocturne
TEST=compile, check error is detected when condition is not true.
Change-Id: I4ab1ad4ae516c00c9e30e778dd81f18893ef1673
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1283969
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Reviewed-by: Nicolas Boichat <drinkcat@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1557839
Commit-Queue: Yong Hong <yhong@google.com>
Tested-by: Yong Hong <yhong@google.com>
-rw-r--r-- | include/compile_time_macros.h | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/include/compile_time_macros.h b/include/compile_time_macros.h index 1cc41f4d0e..bf133d60f4 100644 --- a/include/compile_time_macros.h +++ b/include/compile_time_macros.h @@ -15,6 +15,12 @@ #define _BA0_(c, x) _BA1_(c, x) #define BUILD_ASSERT(cond) _BA0_(cond, __LINE__) +/* + * Test an important condition inside code path at run time, taking advantage of + * -Werror=div-by-zero. + */ +#define BUILD_CHECK_INLINE(value, cond_true) ((value) / (!!(cond_true))) + /* Number of elements in an array */ #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |