Skip to content

Commit bd2be71

Browse files
authored
Merge pull request #283 from FeignClaims/fix/sanitizer_for_dynamic_project_options
2 parents 9be7709 + ac6c7bc commit bd2be71

11 files changed

+55
-49
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ if(FEATURE_TESTS)
101101
set(ENABLE_COVERAGE "ENABLE_COVERAGE")
102102
103103
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
104-
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
104+
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
105105
endif()
106106
107107
# Enable doxgen for the docs
@@ -123,7 +123,7 @@ project_options(
123123
${ENABLE_DOXYGEN}
124124
${ENABLE_COVERAGE}
125125
${ENABLE_SANITIZER_ADDRESS}
126-
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
126+
${ENABLE_SANITIZER_UNDEFINED}
127127
# ${ENABLE_SANITIZER_THREAD}
128128
# ${ENABLE_SANITIZER_MEMORY}
129129
# ENABLE_SANITIZER_POINTER_COMPARE

docs/src/project_options_example.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ if(FEATURE_TESTS)
5454
set(ENABLE_COVERAGE "ENABLE_COVERAGE")
5555
5656
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
57-
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
57+
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
5858
endif()
5959
6060
# Enable doxgen for the docs
@@ -76,7 +76,7 @@ project_options(
7676
${ENABLE_DOXYGEN}
7777
${ENABLE_COVERAGE}
7878
${ENABLE_SANITIZER_ADDRESS}
79-
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
79+
${ENABLE_SANITIZER_UNDEFINED}
8080
# ${ENABLE_SANITIZER_THREAD}
8181
# ${ENABLE_SANITIZER_MEMORY}
8282
# ENABLE_SANITIZER_POINTER_COMPARE

src/DynamicProjectOptions.cmake

+14-23
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,18 @@ macro(dynamic_project_options)
103103
)
104104
endif()
105105

106-
check_sanitizers_support(
107-
ENABLE_SANITIZER_ADDRESS
108-
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
109-
ENABLE_SANITIZER_LEAK
110-
ENABLE_SANITIZER_THREAD
111-
ENABLE_SANITIZER_MEMORY
112-
ENABLE_SANITIZER_POINTER_COMPARE
113-
ENABLE_SANITIZER_POINTER_SUBTRACT
114-
)
115-
116-
if(ENABLE_SANITIZER_ADDRESS)
117-
set(SUPPORTS_ASAN ON)
118-
else()
119-
set(SUPPORTS_ASAN OFF)
120-
endif()
106+
# Fallback for ENABLE_SANITIZER_UNDEFINED_BEHAVIOR option
107+
foreach(default_type IN ITEMS DEFAULT DEVELOPER_DEFAULT USER_DEFAULT)
108+
if(DEFINED ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_${default_type})
109+
if(DEFINED ENABLE_SANITIZER_UNDEFINED_${default_type})
110+
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.")
111+
else()
112+
message(DEPRECATION "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_${default_type} is deprecated. Use ENABLE_SANITIZER_UNDEFINED_${default_type} instead.")
113+
endif()
121114

122-
if(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR)
123-
set(SUPPORTS_UBSAN ON)
124-
else()
125-
set(SUPPORTS_UBSAN OFF)
126-
endif()
115+
set(ENABLE_SANITIZER_UNDEFINED_${default_type} ${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_${default_type}})
116+
endif()
117+
endforeach()
127118

128119
# ccache, clang-tidy, cppcheck are only supported with Ninja and Makefile based generators
129120
# 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)
163154
"0\;DISABLE_RTTI\;OFF\;OFF\;Disable RTTI (no-rtti flag)"
164155
"0\;ENABLE_BUILD_WITH_TIME_TRACE\;OFF\;OFF\;Generates report of where compile-time is spent"
165156
"0\;ENABLE_UNITY\;OFF\;OFF\;Merge C++ files into larger C++ files, can speed up compilation sometimes"
166-
"0\;ENABLE_SANITIZER_ADDRESS\;OFF\;${SUPPORTS_ASAN}\;Make memory errors into hard runtime errors (windows/linux/macos)"
157+
"0\;ENABLE_SANITIZER_ADDRESS\;OFF\;ON\;Make memory errors into hard runtime errors (windows/linux/macos)"
167158
"0\;ENABLE_SANITIZER_LEAK\;OFF\;OFF\;Make memory leaks into hard runtime errors"
168-
"0\;ENABLE_SANITIZER_UNDEFINED_BEHAVIOR\;OFF\;${SUPPORTS_UBSAN}\;Make certain types (numeric mostly) of undefined behavior into runtime errors"
159+
"0\;ENABLE_SANITIZER_UNDEFINED\;OFF\;ON\;Make certain types (numeric mostly) of undefined behavior into runtime errors"
169160
"0\;ENABLE_SANITIZER_THREAD\;OFF\;OFF\;Make thread race conditions into hard runtime errors"
170161
"0\;ENABLE_SANITIZER_MEMORY\;OFF\;OFF\;Make other memory errors into runtime errors"
171162
"0\;ENABLE_CONTROL_FLOW_PROTECTION\;OFF\;OFF\;Enable control flow protection instrumentation"
@@ -277,7 +268,7 @@ macro(dynamic_project_options)
277268
${ENABLE_UNITY_VALUE}
278269
${ENABLE_SANITIZER_ADDRESS_VALUE}
279270
${ENABLE_SANITIZER_LEAK_VALUE}
280-
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR_VALUE}
271+
${ENABLE_SANITIZER_UNDEFINED_VALUE}
281272
${ENABLE_SANITIZER_THREAD_VALUE}
282273
${ENABLE_SANITIZER_MEMORY_VALUE}
283274
${ENABLE_CONTROL_FLOW_PROTECTION_VALUE}

src/Index.cmake

+15-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ include("${ProjectOptions_SRC_DIR}/Vcpkg.cmake")
8282
those.
8383
- ``ENABLE_SANITIZER_ADDRESS``: Enable address sanitizer
8484
- ``ENABLE_SANITIZER_LEAK``: Enable leak sanitizer
85-
- ``ENABLE_SANITIZER_UNDEFINED_BEHAVIOR``: Enable undefined behavior
85+
- ``ENABLE_SANITIZER_UNDEFINED``: Enable undefined behavior
8686
sanitizer
8787
- ``ENABLE_SANITIZER_THREAD``: Enable thread sanitizer
8888
- ``ENABLE_SANITIZER_MEMORY``: Enable memory sanitizer
@@ -148,7 +148,8 @@ macro(project_options)
148148
ENABLE_UNITY
149149
ENABLE_SANITIZER_ADDRESS
150150
ENABLE_SANITIZER_LEAK
151-
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
151+
ENABLE_SANITIZER_UNDEFINED
152+
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR # deprecated, ENABLE_SANITIZER_UNDEFINED
152153
ENABLE_SANITIZER_THREAD
153154
ENABLE_SANITIZER_MEMORY
154155
ENABLE_SANITIZER_POINTER_COMPARE
@@ -260,12 +261,23 @@ macro(project_options)
260261
enable_coverage(${_options_target})
261262
endif()
262263

264+
# Fallback for deprecated ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
265+
if(ProjectOptions_ENABLE_SANITIZER_UNDEFINED_BEHAVIOR)
266+
if(ProjectOptions_ENABLE_SANITIZER_UNDEFINED)
267+
message(WARNING "Don't switch on both ENABLE_SANITIZER_UNDEFINED_BEHAVIOR and ENABLE_SANITIZER_UNDEFINED. Use ENABLE_SANITIZER_UNDEFINED only.")
268+
else()
269+
message(DEPRECATION "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR is deprecated. Use ENABLE_SANITIZER_UNDEFINED instead.")
270+
endif()
271+
272+
set(ProjectOptions_ENABLE_SANITIZER_UNDEFINED ${ProjectOptions_ENABLE_SANITIZER_UNDEFINED_BEHAVIOR})
273+
endif()
274+
263275
# sanitizer options if supported by compiler
264276
enable_sanitizers(
265277
${_options_target}
266278
${ProjectOptions_ENABLE_SANITIZER_ADDRESS}
267279
${ProjectOptions_ENABLE_SANITIZER_LEAK}
268-
${ProjectOptions_ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
280+
${ProjectOptions_ENABLE_SANITIZER_UNDEFINED}
269281
${ProjectOptions_ENABLE_SANITIZER_THREAD}
270282
${ProjectOptions_ENABLE_SANITIZER_MEMORY}
271283
${ProjectOptions_ENABLE_SANITIZER_POINTER_COMPARE}

src/Sanitizers.cmake

+10-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function(
88
_project_name
99
ENABLE_SANITIZER_ADDRESS
1010
ENABLE_SANITIZER_LEAK
11-
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
11+
ENABLE_SANITIZER_UNDEFINED
1212
ENABLE_SANITIZER_THREAD
1313
ENABLE_SANITIZER_MEMORY
1414
ENABLE_SANITIZER_POINTER_COMPARE
@@ -18,7 +18,7 @@ function(
1818
# check if the sanitizers are supported
1919
check_sanitizers_support(
2020
SUPPORTS_SANITIZER_ADDRESS
21-
SUPPORTS_SANITIZER_UNDEFINED_BEHAVIOR
21+
SUPPORTS_SANITIZER_UNDEFINED
2222
SUPPORTS_SANITIZER_LEAK
2323
SUPPORTS_SANITIZER_THREAD
2424
SUPPORTS_SANITIZER_MEMORY
@@ -38,8 +38,11 @@ function(
3838
"pointer-compare"
3939
"pointer-subtract"
4040
)
41-
if(${ENABLE_SANITIZER_${SANITIZER}})
42-
if(${SUPPORTS_SANITIZER_${SANITIZER}})
41+
set(SANITIZER_UPPERCASE "${SANITIZER}")
42+
string(TOUPPER ${SANITIZER} SANITIZER_UPPERCASE)
43+
44+
if(${ENABLE_SANITIZER_${SANITIZER_UPPERCASE}})
45+
if(${SUPPORTS_SANITIZER_${SANITIZER_UPPERCASE}})
4346
list(APPEND SANITIZERS ${SANITIZER})
4447
else()
4548
# 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
134137
Output variables:
135138
136139
- ``ENABLE_SANITIZER_ADDRESS``: Address sanitizer is supported
137-
- ``ENABLE_SANITIZER_UNDEFINED_BEHAVIOR``: Undefined behavior sanitizer is supported
140+
- ``ENABLE_SANITIZER_UNDEFINED``: Undefined behavior sanitizer is supported
138141
- ``ENABLE_SANITIZER_LEAK``: Leak sanitizer is supported
139142
- ``ENABLE_SANITIZER_THREAD``: Thread sanitizer is supported
140143
- ``ENABLE_SANITIZER_MEMORY``: Memory sanitizer is supported
@@ -145,7 +148,7 @@ Output variables:
145148
.. code:: cmake
146149
147150
check_sanitizers_support(ENABLE_SANITIZER_ADDRESS
148-
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
151+
ENABLE_SANITIZER_UNDEFINED
149152
ENABLE_SANITIZER_LEAK
150153
ENABLE_SANITIZER_THREAD
151154
ENABLE_SANITIZER_MEMORY
@@ -158,7 +161,7 @@ Output variables:
158161
function(
159162
check_sanitizers_support
160163
ENABLE_SANITIZER_ADDRESS
161-
ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
164+
ENABLE_SANITIZER_UNDEFINED
162165
ENABLE_SANITIZER_LEAK
163166
ENABLE_SANITIZER_THREAD
164167
ENABLE_SANITIZER_MEMORY

tests/install/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ project(anotherproj VERSION 0.1.0 LANGUAGES CXX C)
1111
option(FEATURE_TESTS "Enable the tests" ON)
1212
if(FEATURE_TESTS)
1313
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
14-
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
14+
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
1515
endif()
1616

1717
# Initialize project_options
@@ -33,7 +33,7 @@ project_options(
3333
# ENABLE_BUILD_WITH_TIME_TRACE
3434
# ENABLE_UNITY
3535
${ENABLE_SANITIZER_ADDRESS}
36-
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
36+
${ENABLE_SANITIZER_UNDEFINED}
3737
# ${ENABLE_SANITIZER_LEAK}
3838
# ${ENABLE_SANITIZER_THREAD}
3939
# ${ENABLE_SANITIZER_MEMORY}

tests/minimal/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ if(ENABLE_TESTING)
4444
if(NOT DISABLE_SANITIZER)
4545
if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
4646
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
47-
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
47+
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
4848
else()
4949
# or it is MSVC and has run vcvarsall
5050
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
@@ -64,7 +64,7 @@ project_options(
6464
ENABLE_VS_ANALYSIS
6565
${ENABLE_COVERAGE}
6666
${ENABLE_SANITIZER_ADDRESS}
67-
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
67+
${ENABLE_SANITIZER_UNDEFINED}
6868
# DISABLE_EXCEPTIONS
6969
# DISABLE_RTTI
7070
# Note: PCH is disabled by default in developer mode because these headers become globally included and they can mask other errors

tests/myproj/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ option(FEATURE_TESTS "Enable the tests" ON)
3838
if(FEATURE_TESTS)
3939
# Enable sanitizers and static analyzers when running the tests
4040
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
41-
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
41+
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
4242
set(ENABLE_SANITIZER_POINTER_COMPARE "ENABLE_SANITIZER_POINTER_COMPARE")
4343
set(ENABLE_SANITIZER_POINTER_SUBTRACT "ENABLE_SANITIZER_POINTER_SUBTRACT")
4444
endif()
@@ -71,7 +71,7 @@ project_options(
7171
# ENABLE_UNITY
7272
${ENABLE_SANITIZER_ADDRESS}
7373
# ${ENABLE_SANITIZER_LEAK}
74-
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
74+
${ENABLE_SANITIZER_UNDEFINED}
7575
${ENABLE_SANITIZER_POINTER_COMPARE}
7676
${ENABLE_SANITIZER_POINTER_SUBTRACT}
7777
# ${ENABLE_SANITIZER_THREAD}

tests/rpi3/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ if(ENABLE_TESTING)
6767
if(NOT DISABLE_SANITIZER)
6868
if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
6969
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
70-
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
70+
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
7171
else()
7272
# or it is MSVC and has run vcvarsall
7373
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
@@ -90,7 +90,7 @@ project_options(
9090
ENABLE_VS_ANALYSIS
9191
${ENABLE_COVERAGE}
9292
${ENABLE_SANITIZER_ADDRESS}
93-
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
93+
${ENABLE_SANITIZER_UNDEFINED}
9494
DISABLE_EXCEPTIONS
9595
DISABLE_RTTI
9696
# Note: PCH is disabled by default in developer mode because these headers become globally included and they can mask other errors

tests/rpi4-vcpkg/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ if(ENABLE_TESTING)
4444
if(NOT DISABLE_SANITIZER)
4545
if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
4646
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
47-
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
47+
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
4848
else()
4949
# or it is MSVC and has run vcvarsall
5050
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
@@ -64,7 +64,7 @@ project_options(
6464
ENABLE_VS_ANALYSIS
6565
${ENABLE_COVERAGE}
6666
${ENABLE_SANITIZER_ADDRESS}
67-
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
67+
${ENABLE_SANITIZER_UNDEFINED}
6868
# DISABLE_EXCEPTIONS
6969
# DISABLE_RTTI
7070
# Note: PCH is disabled by default in developer mode because these headers become globally included and they can mask other errors

tests/rpi4/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ if(ENABLE_TESTING)
6262
if(NOT DISABLE_SANITIZER)
6363
if(NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
6464
set(ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS")
65-
set(ENABLE_SANITIZER_UNDEFINED_BEHAVIOR "ENABLE_SANITIZER_UNDEFINED_BEHAVIOR")
65+
set(ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED")
6666
else()
6767
# or it is MSVC and has run vcvarsall
6868
string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
@@ -82,7 +82,7 @@ project_options(
8282
ENABLE_VS_ANALYSIS
8383
${ENABLE_COVERAGE}
8484
${ENABLE_SANITIZER_ADDRESS}
85-
${ENABLE_SANITIZER_UNDEFINED_BEHAVIOR}
85+
${ENABLE_SANITIZER_UNDEFINED}
8686
DISABLE_EXCEPTIONS
8787
DISABLE_RTTI
8888
# Note: PCH is disabled by default in developer mode because these headers become globally included and they can mask other errors

0 commit comments

Comments
 (0)