diff options
author | Jack Rosenthal <jrosenth@chromium.org> | 2019-08-01 12:49:16 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-08-20 20:00:39 +0000 |
commit | 4cd06cf22b89b7d400c76897d011327b70cee842 (patch) | |
tree | b9a4af087482e0d6ad7fa709e29e4895660f90a2 /test | |
parent | d3784365bf1633dddd6671ee0c5322e87ada9db7 (diff) | |
download | chrome-ec-4cd06cf22b89b7d400c76897d011327b70cee842.tar.gz |
common: add STATIC_IF and STATIC_IF_NOT macros
A common pattern to use with IS_ENABLED is like this:
/*
* This var should only be used if CONFIG_FOO. The linker errors if
* CONFIG_FOO is not defined is intentional.
*/
#ifdef CONFIG_FOO
static
#else
extern
#endif
int some_var;
The issue with this is that it leads to an over-verbose and
potentially hard to read pattern, and does not have the check that
CONFIG_FOO was only defined to blank.
Suppose a macro like this existed:
STATIC_IF(CONFIG_FOO) int some_var;
... which expands to "static" when CONFIG_FOO is defined to empty,
"extern" when CONFIG_FOO is not defined, and errors when CONFIG_FOO is
defined to non-empty.
This CL implements that, as well as the inverse (STATIC_IF_NOT).
BUG=chromium:989786
BRANCH=none
TEST=provided unit tests, buildall
Change-Id: Ib57aaba62bc184fda9aa782a780d5f13ba44ae88
Signed-off-by: Jack Rosenthal <jrosenth@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1731859
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
Diffstat (limited to 'test')
-rw-r--r-- | test/build.mk | 6 | ||||
-rw-r--r-- | test/static_if.c | 50 | ||||
-rw-r--r-- | test/static_if.tasklist | 9 | ||||
-rw-r--r-- | test/static_if_error.c | 34 | ||||
-rw-r--r-- | test/static_if_error.sh | 42 | ||||
-rw-r--r-- | test/static_if_error.tasklist | 9 |
6 files changed, 150 insertions, 0 deletions
diff --git a/test/build.mk b/test/build.mk index 8d68da07f5..5868513962 100644 --- a/test/build.mk +++ b/test/build.mk @@ -61,6 +61,8 @@ test-list-host += sbs_charging_v2 test-list-host += sha256 test-list-host += sha256_unrolled test-list-host += shmalloc +test-list-host += static_if +test-list-host += static_if_error test-list-host += system test-list-host += thermal test-list-host += timer_dos @@ -132,6 +134,7 @@ sbs_charging_v2-y=sbs_charging_v2.o sha256-y=sha256.o sha256_unrolled-y=sha256.o shmalloc-y=shmalloc.o +static_if-y=static_if.o stress-y=stress.o system-y=system.o thermal-y=thermal.o @@ -163,3 +166,6 @@ $(out)/RO/test/nvmem_tpm2_mock.o: CFLAGS += -I$(TPM2_ROOT) host-is_enabled_error: TEST_SCRIPT=is_enabled_error.sh is_enabled_error-y=is_enabled_error.o.cmd + +host-static_if_error: TEST_SCRIPT=static_if_error.sh +static_if_error-y=static_if_error.o.cmd diff --git a/test/static_if.c b/test/static_if.c new file mode 100644 index 0000000000..46e8057d7f --- /dev/null +++ b/test/static_if.c @@ -0,0 +1,50 @@ +/* Copyright 2019 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* Test the STATIC_IF and STATIC_IF_NOT macros. */ + +#include "common.h" +#include "test_util.h" + +#undef CONFIG_UNDEFINED +#define CONFIG_BLANK + +STATIC_IF(CONFIG_UNDEFINED) int this_var_is_extern; +STATIC_IF_NOT(CONFIG_BLANK) int this_var_is_extern_too; +STATIC_IF(CONFIG_BLANK) int this_var_is_static; +STATIC_IF_NOT(CONFIG_UNDEFINED) int this_var_is_static_too; + +static int test_static_if_blank(void) +{ + TEST_ASSERT(this_var_is_static == 0); + TEST_ASSERT(this_var_is_static_too == 0); + + return EC_SUCCESS; +} + +static int test_static_if_unused_no_fail(void) +{ + /* + * This should not cause linker errors because the variables + * go unused (usage is optimized away). + */ + if (IS_ENABLED(CONFIG_UNDEFINED)) + this_var_is_extern = 1; + + if (!IS_ENABLED(CONFIG_BLANK)) + this_var_is_extern_too = 1; + + return EC_SUCCESS; +} + +void run_test(void) +{ + test_reset(); + + RUN_TEST(test_static_if_blank); + RUN_TEST(test_static_if_unused_no_fail); + + test_print_result(); +} diff --git a/test/static_if.tasklist b/test/static_if.tasklist new file mode 100644 index 0000000000..5ffe662d01 --- /dev/null +++ b/test/static_if.tasklist @@ -0,0 +1,9 @@ +/* Copyright 2019 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * See CONFIG_TASK_LIST in config.h for details. + */ +#define CONFIG_TEST_TASK_LIST /* No test task */ diff --git a/test/static_if_error.c b/test/static_if_error.c new file mode 100644 index 0000000000..bbe4839981 --- /dev/null +++ b/test/static_if_error.c @@ -0,0 +1,34 @@ +/* Copyright 2019 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* + * Test the STATIC_IF and STATIC_IF_NOT macros fail on unexpected + * input. + */ + +#include "common.h" +#include "test_util.h" + +#define CONFIG_FOO TEST_VALUE + +/* + * At compiler invocation, define TEST_MACRO to STATIC_IF or + * STATIC_IF_NOT. + */ +#ifndef TEST_MACRO +#error "This error should not be seen in the compiler output!" +#endif + +/* This is intended to cause a compilation error. */ +TEST_MACRO(CONFIG_FOO) __maybe_unused int foo; + +void run_test(void) +{ + test_reset(); + + /* Nothing to do, observe compilation error */ + + test_print_result(); +} diff --git a/test/static_if_error.sh b/test/static_if_error.sh new file mode 100644 index 0000000000..efc7cd3e1e --- /dev/null +++ b/test/static_if_error.sh @@ -0,0 +1,42 @@ +#!/bin/bash -e +# Copyright 2019 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# This file is implemented similar to is_enabled_error.sh + +TEST_DIR="$(dirname "${BASH_SOURCE[0]}")" +TEST_CMD="$(cat "${TEST_DIR}/RO/test/static_if_error.o.cmd")" +TEST_ERROR_COUNT=0 +BAD_ERROR_MSG="This error should not be seen in the compiler output!" + +fail() { + echo "Fail" + echo "$1" + echo "$BUILD_OUTPUT" + TEST_ERROR_COUNT=$((TEST_ERROR_COUNT+1)) +} + +for test_macro in STATIC_IF STATIC_IF_NOT; do + for test_value in 0 1 2 A "5 + 5"; do + echo -n "Running TEST_MACRO=${test_macro} TEST_VALUE=${test_value}..." + TEST_CMD_COMPLETE=" + ${TEST_CMD} \"-DTEST_MACRO=${test_macro}\" \"-DTEST_VALUE=${test_value}\"" + echo "$TEST_CMD_COMPLETE" + if BUILD_OUTPUT="$(sh -c "$TEST_CMD_COMPLETE" 2>&1)"; then + fail "Compilation should not have succeeded." + continue + fi + + if grep -q "$BAD_ERROR_MSG" <<<"$BUILD_OUTPUT"; then + fail "TEST_MACRO was not defined." + continue + fi + done +done + +if [[ $TEST_ERROR_COUNT -eq 0 ]]; then + echo "Pass!" +else + echo "Fail! (${TEST_ERROR_COUNT} tests)" +fi diff --git a/test/static_if_error.tasklist b/test/static_if_error.tasklist new file mode 100644 index 0000000000..5ffe662d01 --- /dev/null +++ b/test/static_if_error.tasklist @@ -0,0 +1,9 @@ +/* Copyright 2019 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * See CONFIG_TASK_LIST in config.h for details. + */ +#define CONFIG_TEST_TASK_LIST /* No test task */ |