From 3a243059164015263954df473b823dd200f96a43 Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Tue, 6 Jul 2021 16:50:49 +0100 Subject: [PATCH 1/4] rtos: Remove UNITTEST macro from rtos Production code should not contain any test-specific checks. Rather than checking `UNITTEST`, unit tests can directly set `MBED_CONF_RTOS_PRESENT=1` to make RTOS available for testing. Note: The cellular ATHandler test also has `MBED_CONF_RTOS_PRESENT=1` added because `ATHandler.cpp` contains a check of this variable. --- .../UNITTESTS/framework/device/athandler/CMakeLists.txt | 1 + rtos/include/rtos/Thread.h | 2 +- rtos/include/rtos/internal/mbed_rtos1_types.h | 2 +- rtos/include/rtos/internal/mbed_rtos_storage.h | 2 +- rtos/include/rtos/mbed_rtos_types.h | 2 +- rtos/tests/UNITTESTS/doubles/CMakeLists.txt | 5 ++++- 6 files changed, 9 insertions(+), 5 deletions(-) diff --git a/connectivity/cellular/tests/UNITTESTS/framework/device/athandler/CMakeLists.txt b/connectivity/cellular/tests/UNITTESTS/framework/device/athandler/CMakeLists.txt index 428cac1a01..76f3c8af11 100644 --- a/connectivity/cellular/tests/UNITTESTS/framework/device/athandler/CMakeLists.txt +++ b/connectivity/cellular/tests/UNITTESTS/framework/device/athandler/CMakeLists.txt @@ -13,6 +13,7 @@ target_compile_definitions(${TEST_NAME} DEVICE_INTERRUPTIN=1 MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE=115200 MBED_CONF_CELLULAR_AT_HANDLER_BUFFER_SIZE=32 + MBED_CONF_RTOS_PRESENT=1 ) target_sources(${TEST_NAME} diff --git a/rtos/include/rtos/Thread.h b/rtos/include/rtos/Thread.h index ec698c358d..1aa72de714 100644 --- a/rtos/include/rtos/Thread.h +++ b/rtos/include/rtos/Thread.h @@ -33,7 +33,7 @@ #include "rtos/Semaphore.h" #include "rtos/Mutex.h" -#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) || defined(UNITTEST) +#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) namespace rtos { /** \addtogroup rtos-public-api */ diff --git a/rtos/include/rtos/internal/mbed_rtos1_types.h b/rtos/include/rtos/internal/mbed_rtos1_types.h index e178b273c1..a2a18008f4 100644 --- a/rtos/include/rtos/internal/mbed_rtos1_types.h +++ b/rtos/include/rtos/internal/mbed_rtos1_types.h @@ -17,7 +17,7 @@ #ifndef MBED_RTOS_RTX1_TYPES_H #define MBED_RTOS_RTX1_TYPES_H -#if MBED_CONF_RTOS_PRESENT || defined(UNITTEST) +#if MBED_CONF_RTOS_PRESENT #include "cmsis_os.h" diff --git a/rtos/include/rtos/internal/mbed_rtos_storage.h b/rtos/include/rtos/internal/mbed_rtos_storage.h index 750725dc4e..c9b06fa2c8 100644 --- a/rtos/include/rtos/internal/mbed_rtos_storage.h +++ b/rtos/include/rtos/internal/mbed_rtos_storage.h @@ -17,7 +17,7 @@ #ifndef MBED_RTOS_STORAGE_H #define MBED_RTOS_STORAGE_H -#if MBED_CONF_RTOS_PRESENT || defined(UNITTEST) +#if MBED_CONF_RTOS_PRESENT #include "mbed_rtx_storage.h" diff --git a/rtos/include/rtos/mbed_rtos_types.h b/rtos/include/rtos/mbed_rtos_types.h index eaf5da0e3d..40bd9d9165 100644 --- a/rtos/include/rtos/mbed_rtos_types.h +++ b/rtos/include/rtos/mbed_rtos_types.h @@ -17,7 +17,7 @@ #ifndef RTOS_TYPES_H_ #define RTOS_TYPES_H_ -#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) || defined(UNITTEST) +#if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) #include "cmsis_os2.h" #else diff --git a/rtos/tests/UNITTESTS/doubles/CMakeLists.txt b/rtos/tests/UNITTESTS/doubles/CMakeLists.txt index 4216a6ab6c..e827f16d45 100644 --- a/rtos/tests/UNITTESTS/doubles/CMakeLists.txt +++ b/rtos/tests/UNITTESTS/doubles/CMakeLists.txt @@ -18,7 +18,10 @@ target_include_directories(mbed-stubs-rtos-headers add_library(mbed-stubs-rtos) -add_definitions(-DUNITTEST) +target_compile_definitions(mbed-stubs-rtos + PRIVATE + MBED_CONF_RTOS_PRESENT=1 +) target_sources(mbed-stubs-rtos PRIVATE From eb6d1aa03e3e8058d4fc489abbe9307a96bc30a2 Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Tue, 6 Jul 2021 16:56:28 +0100 Subject: [PATCH 2/4] Unit tests: Remove `#ifndef UNITTEST` from the mbed_assert.h stub The stub version of mbed_assert.h is never used outside unit tests, so the check on `UNITTEST` is redundant. --- UNITTESTS/target_h/platform/mbed_assert.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/UNITTESTS/target_h/platform/mbed_assert.h b/UNITTESTS/target_h/platform/mbed_assert.h index c52c452abe..1603c2e4dd 100644 --- a/UNITTESTS/target_h/platform/mbed_assert.h +++ b/UNITTESTS/target_h/platform/mbed_assert.h @@ -41,9 +41,7 @@ extern "C" { */ // mbed_assert_internal UT stub only prints an assert trace and returns, so therefore // MBED_NORETURN must not be defined for UTs. -#ifndef UNITTEST MBED_NORETURN -#endif void mbed_assert_internal(const char *expr, const char *file, int line); #ifdef __cplusplus From dd81b536da2c39cc059b4a0cbf1bcff49f2b950c Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Tue, 6 Jul 2021 17:14:11 +0100 Subject: [PATCH 3/4] Unit tests: Update stub The stub version of is mostly identical to the production file of the same name, except the former lags behind and doesn't contain the recent "is_constant_evaluated" feature. It's safe to update the stub file, because "is_constant_evaluated" only checks generic GCC and Clang versions that also apply to compilers on PCs. This enables MbedCRC.h to include without distinguishing between Mbed applications and unit tests. --- .../platform/cxxsupport/mstd_type_traits | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/UNITTESTS/target_h/platform/cxxsupport/mstd_type_traits b/UNITTESTS/target_h/platform/cxxsupport/mstd_type_traits index b1c069b8f6..0513ba91f3 100644 --- a/UNITTESTS/target_h/platform/cxxsupport/mstd_type_traits +++ b/UNITTESTS/target_h/platform/cxxsupport/mstd_type_traits @@ -20,26 +20,6 @@ /* * * - includes toolchain's - * - For ARM C 5, standard C++11/14 features: - * - std::integral_constant, std::true_type, std::false_type - * - primary type categories (std::is_void, std::is_integral etc) - * - composite type categories (std::is_reference etc) - * - type properties (std::is_const, std::is_constructible etc), except std::is_final - * - type property queries (std::alignment_of, std::rank, std::extent) - * - type relations (std::is_same, std::is_base_of, std::is_convertible) - * - const-volatile modifications (std::remove_cv, std::add_const etc) - * - reference modifications (std::remove_reference, std::add_lvalue_reference etc) - * - sign modifications (std::make_signed, std::make_unsigned) - * - array modifications (std::remove_extent, std::remove_all_extents) - * - pointer modifications (std::remove_pointer, std::add_pointer) - * - other modifications: - * - std::aligned_storage - * - std::decay - * - std::enable_if - * - std::conditional - * - std::common_type - * - std::underlying_type - * - std::result_of * - For all toolchains, C++17/20 backports: * - mstd::type_identity * - mstd::bool_constant @@ -47,6 +27,7 @@ * - mstd::is_invocable, mbed::is_invocable_r, etc * - mstd::invoke_result * - logical operator traits (mstd::conjunction, mstd::disjunction, mstd::negation) + * - mstd::is_constant_evaluated */ #include @@ -55,7 +36,6 @@ // The template stuff in here is too confusing for astyle // *INDENT-OFF* - namespace mstd { /* C++20 type identity */ @@ -92,6 +72,10 @@ template struct invoke_result; #endif +} // namespace mstd + +namespace mstd { + using std::is_same; using std::conditional; using std::conditional_t; @@ -462,8 +446,30 @@ struct is_nothrow_invocable_r : impl::is_invocable_r= 9 +#define MSTD_HAS_IS_CONSTANT_EVALUATED 1 + return __builtin_is_constant_evaluated(); +#else + return false; +#endif +} + +#if MSTD_HAS_IS_CONSTANT_EVALUATED +#define MSTD_CONSTEXPR_IF_HAS_IS_CONSTANT_EVALUATED constexpr +#else +#define MSTD_CONSTEXPR_IF_HAS_IS_CONSTANT_EVALUATED +#endif } // namespace mstd - #endif /* MSTD_TYPE_TRAITS_ */ From 9b8acca1360a65062e14b2dc6b7f59d6d0235044 Mon Sep 17 00:00:00 2001 From: Lingkai Dong Date: Tue, 6 Jul 2021 17:26:55 +0100 Subject: [PATCH 4/4] Remove `#ifdef UNITTEST` from MbedCRC.h Production code should not contain any test-specific checks. Rather than checking `UNITTEST`, MbedCRC.h can simply include in all cases whose unit test stub exists. Also remove the `UNITTEST` macro from CMake definitions of kvstore unit tests which depend on MbedCRC.h. --- drivers/include/drivers/MbedCRC.h | 5 ----- .../tests/UNITTESTS/FileSystemStore/CMakeLists.txt | 1 - .../kvstore/tdbstore/tests/UNITTESTS/TDBStore/CMakeLists.txt | 5 ----- 3 files changed, 11 deletions(-) diff --git a/drivers/include/drivers/MbedCRC.h b/drivers/include/drivers/MbedCRC.h index d601fe0aa2..be8539b378 100644 --- a/drivers/include/drivers/MbedCRC.h +++ b/drivers/include/drivers/MbedCRC.h @@ -29,12 +29,7 @@ #include "platform/SingletonPtr.h" #include "platform/PlatformMutex.h" -#ifdef UNITTEST -#include -#define MSTD_CONSTEXPR_IF_HAS_IS_CONSTANT_EVALUATED -#else #include -#endif namespace mbed { /** \addtogroup drivers-public-api */ diff --git a/storage/kvstore/filesystemstore/tests/UNITTESTS/FileSystemStore/CMakeLists.txt b/storage/kvstore/filesystemstore/tests/UNITTESTS/FileSystemStore/CMakeLists.txt index a11b900194..432bd26155 100644 --- a/storage/kvstore/filesystemstore/tests/UNITTESTS/FileSystemStore/CMakeLists.txt +++ b/storage/kvstore/filesystemstore/tests/UNITTESTS/FileSystemStore/CMakeLists.txt @@ -7,7 +7,6 @@ add_executable(${TEST_NAME}) target_compile_definitions(${TEST_NAME} PRIVATE - UNITTEST MBED_LFS_READ_SIZE=64 MBED_LFS_PROG_SIZE=64 MBED_LFS_BLOCK_SIZE=512 diff --git a/storage/kvstore/tdbstore/tests/UNITTESTS/TDBStore/CMakeLists.txt b/storage/kvstore/tdbstore/tests/UNITTESTS/TDBStore/CMakeLists.txt index 46dd748b1f..c68c57c24b 100644 --- a/storage/kvstore/tdbstore/tests/UNITTESTS/TDBStore/CMakeLists.txt +++ b/storage/kvstore/tdbstore/tests/UNITTESTS/TDBStore/CMakeLists.txt @@ -5,11 +5,6 @@ set(TEST_NAME tdbstore-unittest) add_executable(${TEST_NAME}) -target_compile_definitions(${TEST_NAME} - PRIVATE - UNITTEST -) - target_sources(${TEST_NAME} PRIVATE ${mbed-os_SOURCE_DIR}/storage/blockdevice/source/HeapBlockDevice.cpp