Skip to content

Commit 6f6742f

Browse files
authored
Merge pull request #78622 from rintaro/sanitizer-rdar142516855
[CMake] Globally set sanitizer options for Swift targets
2 parents 649072b + 84a0907 commit 6f6742f

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,9 @@ endif()
10821082
#
10831083
swift_common_cxx_warnings()
10841084

1085+
# Set sanitizer options for Swift compiler.
1086+
swift_common_sanitizer_config()
1087+
10851088
# Check if we're build with MSVC or Clang-cl, as these compilers have similar command line arguments.
10861089
if("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
10871090
set(SWIFT_COMPILER_IS_MSVC_LIKE TRUE)

cmake/modules/AddPureSwift.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ function(_add_host_swift_compile_options name)
6363
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-tools-directory;${tools_path};>)
6464
endif()
6565
endif()
66-
_add_host_variant_swift_sanitizer_flags(${name})
6766

6867
target_compile_options(${name} PRIVATE
6968
$<$<COMPILE_LANGUAGE:Swift>:-color-diagnostics>

cmake/modules/AddSwift.cmake

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,36 +82,6 @@ function(_set_target_prefix_and_suffix target kind sdk)
8282
endif()
8383
endfunction()
8484

85-
function(_add_host_variant_swift_sanitizer_flags target)
86-
if(LLVM_USE_SANITIZER)
87-
if(LLVM_USE_SANITIZER STREQUAL "Address")
88-
set(_Swift_SANITIZER_FLAGS "-sanitize=address" "-Xclang-linker" "-fsanitize=address")
89-
elseif(LLVM_USE_SANITIZER STREQUAL "HWAddress")
90-
# Not supported?
91-
elseif(LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
92-
# Not supported
93-
if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
94-
# Not supported
95-
endif()
96-
elseif(LLVM_USE_SANITIZER STREQUAL "Undefined")
97-
set(_Swift_SANITIZER_FLAGS "-sanitize=undefined" "-Xclang-linker" "-fsanitize=undefined")
98-
elseif(LLVM_USE_SANITIZER STREQUAL "Thread")
99-
set(_Swift_SANITIZER_FLAGS "-sanitize=thread" "-Xclang-linker" "-fsanitize=thread")
100-
elseif(LLVM_USE_SANITIZER STREQUAL "DataFlow")
101-
# Not supported
102-
elseif(LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR
103-
LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
104-
set(_Swift_SANITIZER_FLAGS "-sanitize=address" "-sanitize=undefined" "-Xclang-linker" "-fsanitize=address" "-Xclang-linker" "-fsanitize=undefined")
105-
elseif(LLVM_USE_SANITIZER STREQUAL "Leaks")
106-
# Not supported
107-
else()
108-
message(SEND_ERROR "unsupported value for LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
109-
endif()
110-
111-
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:${_Swift_SANITIZER_FLAGS}>)
112-
endif()
113-
endfunction()
114-
11585
function(swift_get_host_triple out_var)
11686
if(SWIFT_HOST_VARIANT_SDK IN_LIST SWIFT_DARWIN_PLATFORMS)
11787
set(DEPLOYMENT_VERSION "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
@@ -145,8 +115,6 @@ function(_add_host_variant_c_compile_link_flags name)
145115

146116
if (CMAKE_Swift_COMPILER)
147117
target_compile_options(${name} PRIVATE $<$<COMPILE_LANGUAGE:Swift>:-target;${SWIFT_HOST_TRIPLE}>)
148-
149-
_add_host_variant_swift_sanitizer_flags(${name})
150118
endif()
151119

152120
set(_sysroot

cmake/modules/SwiftSharedCMakeConfig.cmake

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,35 @@ function(swift_common_llvm_config target)
377377
llvm_config("${target}" ${ARGN})
378378
endif()
379379
endfunction()
380+
381+
# Set sanitizer options to all Swift compiler flags. Similar options are added to C/CXX compiler in 'HandleLLVMOptions'
382+
macro(swift_common_sanitizer_config)
383+
if(LLVM_USE_SANITIZER)
384+
if(LLVM_USE_SANITIZER STREQUAL "Address")
385+
set(_Swift_SANITIZER_FLAGS "-sanitize=address -Xclang-linker -fsanitize=address")
386+
elseif(LLVM_USE_SANITIZER STREQUAL "HWAddress")
387+
# Not supported?
388+
elseif(LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
389+
# Not supported
390+
if(LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
391+
# Not supported
392+
endif()
393+
elseif(LLVM_USE_SANITIZER STREQUAL "Undefined")
394+
set(_Swift_SANITIZER_FLAGS "-sanitize=undefined -Xclang-linker -fsanitize=undefined")
395+
elseif(LLVM_USE_SANITIZER STREQUAL "Thread")
396+
set(_Swift_SANITIZER_FLAGS "-sanitize=thread -Xclang-linker -fsanitize=thread")
397+
elseif(LLVM_USE_SANITIZER STREQUAL "DataFlow")
398+
# Not supported
399+
elseif(LLVM_USE_SANITIZER STREQUAL "Address;Undefined" OR
400+
LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
401+
set(_Swift_SANITIZER_FLAGS "-sanitize=address -sanitize=undefined -Xclang-linker -fsanitize=address -Xclang-linker -fsanitize=undefined")
402+
elseif(LLVM_USE_SANITIZER STREQUAL "Leaks")
403+
# Not supported
404+
else()
405+
message(SEND_ERROR "unsupported value for LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
406+
endif()
407+
408+
set(CMAKE_Swift_FLAGS "${CMAKE_Swift_FLAGS} ${_Swift_SANITIZER_FLAGS}")
409+
410+
endif()
411+
endmacro()

0 commit comments

Comments
 (0)