Skip to content

Sanitizers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
include(CheckCXXCompilerFlag)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
    message("Looking for -fsanitize=${flag}")
    set(CMAKE_REQUIRED_FLAGS "-Werror -fsanitize=${flag}")
    check_cxx_compiler_flag(-fsanitize=${flag} HAVE_FLAG_SANITIZER)
    if (HAVE_FLAG_SANITIZER)
        message("Adding -fsanitize=${flag}")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=${flag} -fno-omit-frame-pointer")
        set(DCMAKE_C_FLAGS "${DCMAKE_C_FLAGS} -fsanitize=${flag} -fno-omit-frame-pointer")
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=${flag}")
        set(DCMAKE_MODULE_LINKER_FLAGS "${DCMAKE_MODULE_LINKER_FLAGS} -fsanitize=${flag}")
    else ()
        message("-fsanitize=${flag} unavailable")
    endif ()
endif ()

1
add_sanitizer("address")

1
add_sanitizer("thread")

1
add_sanitizer("undefined")

1
add_sanitizer("memory")

1
add_sanitizer("leak")

1
2
3
4
5
6
7
8
# Choose a subset of sanitizers not in conflict
add_address_sanitizer()
add_leak_sanitizer()
add_undefined_sanitizer()
# not allowed with address sanitizer
# add_thread_sanitizer()
# not supported
# add_memory_sanitizer()

Share Snippets