summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaul E Rangel <rrangel@chromium.org>2019-04-04 14:58:16 -0600
committerCommit Bot <commit-bot@chromium.org>2019-07-30 16:54:22 +0000
commit18e2dc171100e4ebe145184a816119431ce1a7a0 (patch)
treee5daedd96475cadd3563501ff39e281cac16805e
parent85abf535d7ab96b23ebefb8a76ec8665d5b875c7 (diff)
downloadchrome-ec-18e2dc171100e4ebe145184a816119431ce1a7a0.tar.gz
ec/common: Introduce IS_ENABLED to check config options
This is copied from coreboot with added support for empty defines. We should favor using this macro instead of using #ifdef. The macro will evaluate to 0 if the option is not defined. This allows all the code to be compiled and then the optimizer will remove the sections of code that won't ever run. This way we don't end up with #ifdef sections with invalid syntax because no one ever tests that specific permutation. e.g., if (IS_ENABLED(CONFIG_USBC_SS_MUX)) { ... } There are currently spots where #ifdefs are nested 3 levels deep. This makes it very hard to follow the code. BUG=none TEST=Added some code that uses the macro and verified it executes when the config value is defined, and doesn't when it's not. Change-Id: I796b899f7cbbd3067ea3a4d52527d980c68935c9 Signed-off-by: Raul E Rangel <rrangel@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/1553573 Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com> Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org> (cherry picked from commit 46e28e28c2973db09dd9a7a2c14db3cd2609905a) Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1726332 Commit-Queue: Daisuke Nojiri <dnojiri@chromium.org> Tested-by: Daisuke Nojiri <dnojiri@chromium.org> Auto-Submit: Daisuke Nojiri <dnojiri@chromium.org>
-rw-r--r--include/common.h27
1 files changed, 27 insertions, 0 deletions
diff --git a/include/common.h b/include/common.h
index a98fce3ca2..6f49f0115d 100644
--- a/include/common.h
+++ b/include/common.h
@@ -159,4 +159,31 @@ enum ec_error_list {
/* find the most significant bit. Not defined in n == 0. */
#define __fls(n) (31 - __builtin_clz(n))
+/*
+ * Getting something that works in C and CPP for an arg that may or may
+ * not be defined is tricky. Here, if we have "#define CONFIG_BOOGER"
+ * we match on the placeholder define, insert the "0," for arg1 and generate
+ * the triplet (0, 1, 0). Then the last step cherry picks the 2nd arg (a one).
+ * When CONFIG_BOOGER is not defined, we generate a (... 1, 0) pair, and when
+ * the last step cherry picks the 2nd arg, we get a zero.
+ */
+#define __ARG_PLACEHOLDER_ 0,
+#define config_enabled(cfg) _config_enabled(cfg)
+#define _config_enabled(value) __config_enabled(__ARG_PLACEHOLDER_##value)
+#define __config_enabled(arg1_or_junk) ___config_enabled(arg1_or_junk 1, 0, 0)
+#define ___config_enabled(__ignored, val, ...) val
+
+/**
+ * Checks if a config option is defined to an empty value.
+ *
+ * IS_ENABLED(CONFIG_MY_OPTION) will return 1 in the following case:
+ * #define CONFIG_MY_OPTION
+ *
+ * Otherwise if the option has not been defined or defined with a value, it will
+ * return 0.
+ *
+ * @param CONFIG_OPTION
+ */
+#define IS_ENABLED(option) config_enabled(option)
+
#endif /* __CROS_EC_COMMON_H */