summaryrefslogtreecommitdiff
path: root/test/static_if.c
diff options
context:
space:
mode:
authorJack Rosenthal <jrosenth@chromium.org>2019-08-01 12:49:16 -0600
committerCommit Bot <commit-bot@chromium.org>2019-08-20 20:00:39 +0000
commit4cd06cf22b89b7d400c76897d011327b70cee842 (patch)
treeb9a4af087482e0d6ad7fa709e29e4895660f90a2 /test/static_if.c
parentd3784365bf1633dddd6671ee0c5322e87ada9db7 (diff)
downloadchrome-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/static_if.c')
-rw-r--r--test/static_if.c50
1 files changed, 50 insertions, 0 deletions
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();
+}