The VALUE_IF_KUNIT macro will add expression statement only if the
CONFIG_KUNIT is enabled. Otherwise if CONFIG_KUNIT is not enabled
it will evaluate always to 0.

Signed-off-by: Michal Wajdeczko <[email protected]>
---
Cc: Rae Moar <[email protected]>
Cc: David Gow <[email protected]>
Cc: Lucas De Marchi <[email protected]>
---
v2: add simple usage example (Rae)
    reformat kernel-doc for better output (Michal)
    s/ONLY_IF_KUNIT/VALUE_IF_KUNIT
---
 include/kunit/visibility.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/include/kunit/visibility.h b/include/kunit/visibility.h
index c1825381adfc..b156cafa4705 100644
--- a/include/kunit/visibility.h
+++ b/include/kunit/visibility.h
@@ -30,6 +30,25 @@
      */
     #define DECLARE_IF_KUNIT(body...)  body
 
+    /**
+     * VALUE_IF_KUNIT - Conditionally evaluate an expression
+     * @expr: the expression to be evaluated conditionally
+     *
+     * This macro evaluates expression statement only if CONFIG_KUNIT is 
enabled.
+     * Otherwise if CONFIG_KUNIT is not enabled it will evaluate always to 0.
+     *
+     * .. code-block:: C
+     *
+     *     int real_func(int i)
+     *     {
+     *         if (VALUE_IF_KUNIT(i == 0xC0FFE))
+     *             return 0;
+     *
+     *         return i + 1;
+     *     }
+     */
+    #define VALUE_IF_KUNIT(expr...)    expr
+
     /**
      * VISIBLE_IF_KUNIT - A macro that sets symbols to be static if
      * CONFIG_KUNIT is not enabled. Otherwise if CONFIG_KUNIT is enabled
@@ -46,6 +65,7 @@
            EXPORTED_FOR_KUNIT_TESTING)
 #else
     #define DECLARE_IF_KUNIT(body...)
+    #define VALUE_IF_KUNIT(expr...) 0
     #define VISIBLE_IF_KUNIT static
     #define EXPORT_SYMBOL_IF_KUNIT(symbol)
 #endif
-- 
2.43.0


Reply via email to