From c15c9fffa70d180c74c28a3682b9ec2f17841153 Mon Sep 17 00:00:00 2001 From: Jett Rink Date: Wed, 4 Dec 2019 11:27:46 -0700 Subject: build_assert: use _Static_assert We have compiler support for _static_assert, so we should use it. Also allow for an option message in BUILD_ASSERT that will display as a part of the failure. Here is an example of a compile failure with the optional message ... CC RW/chip/npcx/gpio.o CC RW/chip/npcx/header.o In file included from include/common.h:13, from include/adc.h:11, from board/phaser/board.c:8: include/compile_time_macros.h:13:2: error: static assertion failed: "board/phaser/board.c:74: Size mismatch" _Static_assert(cond, file ":" #line ": " msg) ... BRANCH=none BUG=none TEST=verified by manually update BUILD_ASSERTS and viewing output. Change-Id: I6421084868ea81be8b3fa6ad0613730f4e4e149b Signed-off-by: Jett Rink Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1950820 Reviewed-by: Jack Rosenthal Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2607027 Tested-by: Chen-Tsung Hsieh Reviewed-by: Chen-Tsung Hsieh Commit-Queue: Chen-Tsung Hsieh --- include/compile_time_macros.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/compile_time_macros.h b/include/compile_time_macros.h index 8565a3605d..cdf8b1998b 100644 --- a/include/compile_time_macros.h +++ b/include/compile_time_macros.h @@ -9,11 +9,11 @@ #define __CROS_EC_COMPILE_TIME_MACROS_H /* Test an important condition at compile time, not run time */ -#define _BA1_(cond, line) \ - extern int __build_assertion_ ## line[1 - 2*!(cond)] \ - __attribute__ ((unused)) -#define _BA0_(c, x) _BA1_(c, x) -#define BUILD_ASSERT(cond) _BA0_(cond, __LINE__) +#define _BA1_(cond, file, line, msg) \ + _Static_assert(cond, file ":" #line ": " msg) +#define _BA0_(c, f, l, msg) _BA1_(c, f, l, msg) +/* Pass in an option message to display after condition */ +#define BUILD_ASSERT(cond, ...) _BA0_(cond, __FILE__, __LINE__, __VA_ARGS__) /* * Test an important condition inside code path at run time, taking advantage of -- cgit v1.2.1