diff --git a/README.md b/README.md index b70683b9..e59bc804 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ if(FEATURE_TESTS) set(ENABLE_COVERAGE "ENABLE_COVERAGE") set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS") - set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR") + set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED") endif() # Enable doxgen for the docs @@ -123,7 +123,7 @@ project_options( ${ENABLE_DOXYGEN} ${ENABLE_COVERAGE} ${ENABLE_SANITIZER_ADDRESS} - ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR} + ${ENABLE_SANITIZER_UNDEFINED} # ${ENABLE_SANITIZER_THREAD} # ${ENABLE_SANITIZER_MEMORY} # ENABLE_SANITIZER_POINTER_COMPARE diff --git a/docs/src/project_options_example.md b/docs/src/project_options_example.md index 124654eb..384d287d 100644 --- a/docs/src/project_options_example.md +++ b/docs/src/project_options_example.md @@ -54,7 +54,7 @@ if(FEATURE_TESTS) set(ENABLE_COVERAGE "ENABLE_COVERAGE") set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS") - set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR") + set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED") endif() # Enable doxgen for the docs @@ -76,7 +76,7 @@ project_options( ${ENABLE_DOXYGEN} ${ENABLE_COVERAGE} ${ENABLE_SANITIZER_ADDRESS} - ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR} + ${ENABLE_SANITIZER_UNDEFINED} # ${ENABLE_SANITIZER_THREAD} # ${ENABLE_SANITIZER_MEMORY} # ENABLE_SANITIZER_POINTER_COMPARE diff --git a/src/DynamicProjectOptions.cmake b/src/DynamicProjectOptions.cmake index a008a541..b43d061f 100644 --- a/src/DynamicProjectOptions.cmake +++ b/src/DynamicProjectOptions.cmake @@ -103,27 +103,18 @@ macro(dynamic_project_options) ) endif() - check_sanitizers_support( - ENABLE_SANITIZER_ADDRESS - ENABLE_SANITIZER_UNDEFINED_BEHAVIOR - ENABLE_SANITIZER_LEAK - ENABLE_SANITIZER_THREAD - ENABLE_SANITIZER_MEMORY - ENABLE_SANITIZER_POINTER_COMPARE - ENABLE_SANITIZER_POINTER_SUBTRACT - ) - - if(ENABLE_SANITIZER_ADDRESS) - set(SUPPORTS_ASAN ON) - else() - set(SUPPORTS_ASAN OFF) - endif() + # Fallback for ENABLE_SANITIZER_UNDEFINED_BEHAVIOR option + foreach(default_type IN ITEMS DEFAULT DEVELOPER_DEFAULT USER_DEFAULT) + if(DEFINED ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_${default_type}) + if(DEFINED ENABLE_SANITIZER_UNDEFINED_${default_type}) + message(WARNING "Don't set both ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_${default_type} and ENABLE_SANITIZER_UNDEFINED_${default_type}. Use ENABLE_SANITIZER_UNDEFINED_${default_type} only.") + else() + message(DEPRECATION "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_${default_type} is deprecated. Use ENABLE_SANITIZER_UNDEFINED_${default_type} instead.") + endif() - if(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR) - set(SUPPORTS_UBSAN ON) - else() - set(SUPPORTS_UBSAN OFF) - endif() + set(ENABLE_SANITIZER_UNDEFINED_${default_type} ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_${default_type}}) + endif() + endforeach() # ccache, clang-tidy, cppcheck are only supported with Ninja and Makefile based generators # note that it is possible to use Ninja with cl, so this still allows clang-tidy on Windows @@ -163,9 +154,9 @@ macro(dynamic_project_options) "0\;DISABLE_RTTI\;OFF\;OFF\;Disable RTTI (no-rtti flag)" "0\;ENABLE_BUILD_WITH_TIME_TRACE\;OFF\;OFF\;Generates report of where compile-time is spent" "0\;ENABLE_UNITY\;OFF\;OFF\;Merge C++ files into larger C++ files, can speed up compilation sometimes" - "0\;ENABLE_SANITIZER_ADDRESS\;OFF\;${SUPPORTS_ASAN}\;Make memory errors into hard runtime errors (windows/linux/macos)" + "0\;ENABLE_SANITIZER_ADDRESS\;OFF\;ON\;Make memory errors into hard runtime errors (windows/linux/macos)" "0\;ENABLE_SANITIZER_LEAK\;OFF\;OFF\;Make memory leaks into hard runtime errors" - "0\;ENABLE_SANITIZER_UNDEFINED_BEHAVIOR\;OFF\;${SUPPORTS_UBSAN}\;Make certain types (numeric mostly) of undefined behavior into runtime errors" + "0\;ENABLE_SANITIZER_UNDEFINED\;OFF\;ON\;Make certain types (numeric mostly) of undefined behavior into runtime errors" "0\;ENABLE_SANITIZER_THREAD\;OFF\;OFF\;Make thread race conditions into hard runtime errors" "0\;ENABLE_SANITIZER_MEMORY\;OFF\;OFF\;Make other memory errors into runtime errors" "0\;ENABLE_CONTROL_FLOW_PROTECTION\;OFF\;OFF\;Enable control flow protection instrumentation" @@ -277,7 +268,7 @@ macro(dynamic_project_options) ${ENABLE_UNITY_VALUE} ${ENABLE_SANITIZER_ADDRESS_VALUE} ${ENABLE_SANITIZER_LEAK_VALUE} - ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_VALUE} + ${ENABLE_SANITIZER_UNDEFINED_VALUE} ${ENABLE_SANITIZER_THREAD_VALUE} ${ENABLE_SANITIZER_MEMORY_VALUE} ${ENABLE_CONTROL_FLOW_PROTECTION_VALUE} diff --git a/src/Index.cmake b/src/Index.cmake index f4544398..a46f387a 100644 --- a/src/Index.cmake +++ b/src/Index.cmake @@ -82,7 +82,7 @@ include("${ProjectOptions_SRC_DIR}/Vcpkg.cmake") those. - ``ENABLE_SANITIZER_ADDRESS``: Enable address sanitizer - ``ENABLE_SANITIZER_LEAK``: Enable leak sanitizer -- ``ENABLE_SANITIZER_UNDEFINED_BEHAVIOR``: Enable undefined behavior +- ``ENABLE_SANITIZER_UNDEFINED``: Enable undefined behavior sanitizer - ``ENABLE_SANITIZER_THREAD``: Enable thread sanitizer - ``ENABLE_SANITIZER_MEMORY``: Enable memory sanitizer @@ -148,7 +148,8 @@ macro(project_options) ENABLE_UNITY ENABLE_SANITIZER_ADDRESS ENABLE_SANITIZER_LEAK - ENABLE_SANITIZER_UNDEFINED_BEHAVIOR + ENABLE_SANITIZER_UNDEFINED + ENABLE_SANITIZER_UNDEFINED_BEHAVIOR # deprecated, ENABLE_SANITIZER_UNDEFINED ENABLE_SANITIZER_THREAD ENABLE_SANITIZER_MEMORY ENABLE_SANITIZER_POINTER_COMPARE @@ -260,12 +261,23 @@ macro(project_options) enable_coverage(${_options_target}) endif() + # Fallback for deprecated ENABLE_SANITIZER_UNDEFINED_BEHAVIOR + if(ProjectOptions_ENABLE_SANITIZER_UNDEFINED_BEHAVIOR) + if(ProjectOptions_ENABLE_SANITIZER_UNDEFINED) + message(WARNING "Don't switch on both ENABLE_SANITIZER_UNDEFINED_BEHAVIOR and ENABLE_SANITIZER_UNDEFINED. Use ENABLE_SANITIZER_UNDEFINED only.") + else() + message(DEPRECATION "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR is deprecated. Use ENABLE_SANITIZER_UNDEFINED instead.") + endif() + + set(ProjectOptions_ENABLE_SANITIZER_UNDEFINED ${ProjectOptions_ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}) + endif() + # sanitizer options if supported by compiler enable_sanitizers( ${_options_target} ${ProjectOptions_ENABLE_SANITIZER_ADDRESS} ${ProjectOptions_ENABLE_SANITIZER_LEAK} - ${ProjectOptions_ENABLE_SANITIZER_UNDEFINED_BEHAVIOR} + ${ProjectOptions_ENABLE_SANITIZER_UNDEFINED} ${ProjectOptions_ENABLE_SANITIZER_THREAD} ${ProjectOptions_ENABLE_SANITIZER_MEMORY} ${ProjectOptions_ENABLE_SANITIZER_POINTER_COMPARE} diff --git a/src/Sanitizers.cmake b/src/Sanitizers.cmake index 7d516ba9..8100b6f9 100644 --- a/src/Sanitizers.cmake +++ b/src/Sanitizers.cmake @@ -8,7 +8,7 @@ function( _project_name ENABLE_SANITIZER_ADDRESS ENABLE_SANITIZER_LEAK - ENABLE_SANITIZER_UNDEFINED_BEHAVIOR + ENABLE_SANITIZER_UNDEFINED ENABLE_SANITIZER_THREAD ENABLE_SANITIZER_MEMORY ENABLE_SANITIZER_POINTER_COMPARE @@ -18,7 +18,7 @@ function( # check if the sanitizers are supported check_sanitizers_support( SUPPORTS_SANITIZER_ADDRESS - SUPPORTS_SANITIZER_UNDEFINED_BEHAVIOR + SUPPORTS_SANITIZER_UNDEFINED SUPPORTS_SANITIZER_LEAK SUPPORTS_SANITIZER_THREAD SUPPORTS_SANITIZER_MEMORY @@ -38,8 +38,11 @@ function( "pointer-compare" "pointer-subtract" ) - if(${ENABLE_SANITIZER_${SANITIZER}}) - if(${SUPPORTS_SANITIZER_${SANITIZER}}) + set(SANITIZER_UPPERCASE "${SANITIZER}") + string(TOUPPER ${SANITIZER} SANITIZER_UPPERCASE) + + if(${ENABLE_SANITIZER_${SANITIZER_UPPERCASE}}) + if(${SUPPORTS_SANITIZER_${SANITIZER_UPPERCASE}}) list(APPEND SANITIZERS ${SANITIZER}) else() # do not enable the sanitizer if it is not supported @@ -134,7 +137,7 @@ Note that some sanitizers cannot be enabled together, and this function doesn't Output variables: - ``ENABLE_SANITIZER_ADDRESS``: Address sanitizer is supported -- ``ENABLE_SANITIZER_UNDEFINED_BEHAVIOR``: Undefined behavior sanitizer is supported +- ``ENABLE_SANITIZER_UNDEFINED``: Undefined behavior sanitizer is supported - ``ENABLE_SANITIZER_LEAK``: Leak sanitizer is supported - ``ENABLE_SANITIZER_THREAD``: Thread sanitizer is supported - ``ENABLE_SANITIZER_MEMORY``: Memory sanitizer is supported @@ -145,7 +148,7 @@ Output variables: .. code:: cmake check_sanitizers_support(ENABLE_SANITIZER_ADDRESS - ENABLE_SANITIZER_UNDEFINED_BEHAVIOR + ENABLE_SANITIZER_UNDEFINED ENABLE_SANITIZER_LEAK ENABLE_SANITIZER_THREAD ENABLE_SANITIZER_MEMORY @@ -158,7 +161,7 @@ Output variables: function( check_sanitizers_support ENABLE_SANITIZER_ADDRESS - ENABLE_SANITIZER_UNDEFINED_BEHAVIOR + ENABLE_SANITIZER_UNDEFINED ENABLE_SANITIZER_LEAK ENABLE_SANITIZER_THREAD ENABLE_SANITIZER_MEMORY diff --git a/tests/install/CMakeLists.txt b/tests/install/CMakeLists.txt index 091ab6de..27ff2633 100644 --- a/tests/install/CMakeLists.txt +++ b/tests/install/CMakeLists.txt @@ -11,7 +11,7 @@ project(anotherproj VERSION 0.1.0 LANGUAGES CXX C) option(FEATURE_TESTS "Enable the tests" ON) if(FEATURE_TESTS) set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS") - set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR") + set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED") endif() # Initialize project_options @@ -33,7 +33,7 @@ project_options( # ENABLE_BUILD_WITH_TIME_TRACE # ENABLE_UNITY ${ENABLE_SANITIZER_ADDRESS} - ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR} + ${ENABLE_SANITIZER_UNDEFINED} # ${ENABLE_SANITIZER_LEAK} # ${ENABLE_SANITIZER_THREAD} # ${ENABLE_SANITIZER_MEMORY} diff --git a/tests/minimal/CMakeLists.txt b/tests/minimal/CMakeLists.txt index 9e8f02c2..7bb7ac26 100644 --- a/tests/minimal/CMakeLists.txt +++ b/tests/minimal/CMakeLists.txt @@ -44,7 +44,7 @@ if(ENABLE_TESTING) if(NOT DISABLE_SANITIZER) if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS") - set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR") + set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED") else() # or it is MSVC and has run vcvarsall string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir) @@ -64,7 +64,7 @@ project_options( ENABLE_VS_ANALYSIS ${ENABLE_COVERAGE} ${ENABLE_SANITIZER_ADDRESS} - ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR} + ${ENABLE_SANITIZER_UNDEFINED} # DISABLE_EXCEPTIONS # DISABLE_RTTI # Note: PCH is disabled by default in developer mode because these headers become globally included and they can mask other errors diff --git a/tests/myproj/CMakeLists.txt b/tests/myproj/CMakeLists.txt index 041c5456..ec8b0040 100644 --- a/tests/myproj/CMakeLists.txt +++ b/tests/myproj/CMakeLists.txt @@ -38,7 +38,7 @@ option(FEATURE_TESTS "Enable the tests" ON) if(FEATURE_TESTS) # Enable sanitizers and static analyzers when running the tests set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS") - set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR") + set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED") set(ENABLE_SANITIZER_POINTER_COMPARE "ENABLE_SANITIZER_POINTER_COMPARE") set(ENABLE_SANITIZER_POINTER_SUBTRACT "ENABLE_SANITIZER_POINTER_SUBTRACT") endif() @@ -71,7 +71,7 @@ project_options( # ENABLE_UNITY ${ENABLE_SANITIZER_ADDRESS} # ${ENABLE_SANITIZER_LEAK} - ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR} + ${ENABLE_SANITIZER_UNDEFINED} ${ENABLE_SANITIZER_POINTER_COMPARE} ${ENABLE_SANITIZER_POINTER_SUBTRACT} # ${ENABLE_SANITIZER_THREAD} diff --git a/tests/rpi3/CMakeLists.txt b/tests/rpi3/CMakeLists.txt index de9007a2..535df44f 100644 --- a/tests/rpi3/CMakeLists.txt +++ b/tests/rpi3/CMakeLists.txt @@ -67,7 +67,7 @@ if(ENABLE_TESTING) if(NOT DISABLE_SANITIZER) if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS") - set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR") + set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED") else() # or it is MSVC and has run vcvarsall string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir) @@ -90,7 +90,7 @@ project_options( ENABLE_VS_ANALYSIS ${ENABLE_COVERAGE} ${ENABLE_SANITIZER_ADDRESS} - ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR} + ${ENABLE_SANITIZER_UNDEFINED} DISABLE_EXCEPTIONS DISABLE_RTTI # Note: PCH is disabled by default in developer mode because these headers become globally included and they can mask other errors diff --git a/tests/rpi4-vcpkg/CMakeLists.txt b/tests/rpi4-vcpkg/CMakeLists.txt index 07dc289b..8f86ab77 100644 --- a/tests/rpi4-vcpkg/CMakeLists.txt +++ b/tests/rpi4-vcpkg/CMakeLists.txt @@ -44,7 +44,7 @@ if(ENABLE_TESTING) if(NOT DISABLE_SANITIZER) if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS") - set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR") + set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED") else() # or it is MSVC and has run vcvarsall string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir) @@ -64,7 +64,7 @@ project_options( ENABLE_VS_ANALYSIS ${ENABLE_COVERAGE} ${ENABLE_SANITIZER_ADDRESS} - ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR} + ${ENABLE_SANITIZER_UNDEFINED} # DISABLE_EXCEPTIONS # DISABLE_RTTI # Note: PCH is disabled by default in developer mode because these headers become globally included and they can mask other errors diff --git a/tests/rpi4/CMakeLists.txt b/tests/rpi4/CMakeLists.txt index 68b245ae..27fb4f66 100644 --- a/tests/rpi4/CMakeLists.txt +++ b/tests/rpi4/CMakeLists.txt @@ -62,7 +62,7 @@ if(ENABLE_TESTING) if(NOT DISABLE_SANITIZER) if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows") set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS") - set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR") + set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED") else() # or it is MSVC and has run vcvarsall string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir) @@ -82,7 +82,7 @@ project_options( ENABLE_VS_ANALYSIS ${ENABLE_COVERAGE} ${ENABLE_SANITIZER_ADDRESS} - ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR} + ${ENABLE_SANITIZER_UNDEFINED} DISABLE_EXCEPTIONS DISABLE_RTTI # Note: PCH is disabled by default in developer mode because these headers become globally included and they can mask other errors