Operating System: Linux / Windows 10 (only supports AddressSanitizer) or WSL / macOS (supports AddressSanitizer, ThreadSanitizer, and UndefinedBehaviorSanitizer)
Project Format: CMake (fully supported) / Makefile and compilation database (no output visualization)
Toolchain: Local / Remote / WSL / Docker
Sanitizers are open-source tools designed by Google for dynamic code analysis. CLion integrates the following Sanitizers:
-
AddressSanitizer (ASan)
-
LeakSanitizer (LSan)
-
ThreadSanitizer (TSan)
-
UndefinedBehaviorSanitizer (UBSsan)
-
MemorySanitizer (MSan)
👇 Click to receive 👇
👉 C Language Knowledge Resource Collection
Sanitizers have been implemented since Clang 3.1 and GCC 4.8. All Sanitizers are applicable to Linux x86_64 machines. You can use AddressSanitizer on Windows 10 by simply using clang-cl under the MSVC toolchain. For macOS, the supported Sanitizers include AddressSanitizer, ThreadSanitizer, and UndefinedBehaviorSanitizer.
Since Sanitizers are based on compiler instrumentation, you need to rebuild the project to start using these tools.
Specify Compiler Flags
Adjust the following template line and add it to your CMakeLists.txt:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=[sanitizer_name] [additional_options] [-g] [-OX]")
For C projects, use CMAKE_C_FLAGS.
For [sanitizer_name], you can use one of the following:
[additional_flags] are other compilation flags such as -fno-omit-frame-pointer, -fsanitize-recover/-fno-sanitize-recover, -fsanitize-blacklist, etc.
Use [-g] to include file names and line numbers in warning messages.
Add optimization level [-OX] for reasonable performance (see recommendations in the specific Sanitizer documentation).
-
address for AddressSanitizer
-
leak for LeakSanitizer
-
thread for ThreadSanitizer
-
undefined for UndefinedBehaviorSanitizer
-
memory for MemorySanitizer
Sanitizers for Non-CMake Projects
If you are using Makefile projects or compilation database projects with custom build targets, ensure to specify linker flags along with compiler flags. For example, in the case of Makefile and AddressSanitizer:

For Makefile and compilation database, the output visualization of Sanitizers is only available for CMake projects. For Makefiles and compilation databases, the output is logged in plain text in the console.
Adjust Sanitizers Settings
-
Go to Settings | Build, Execution, Deployment | Dynamic Analysis Tools | Sanitizers and set the following:

-
Runtime Flags
In this section, specify runtime options for each Sanitizer. You can set them manually or click the “Import flags from existing environment variables” button (this button is available if ASAN/MSAN/LSAN/TSAN_OPTIONS variables exist).
-
Use Visual Representation of Sanitizer Output
Check this box to view tree view output in the preview editor and frame information:

To make the visual output available, switch to at least Clang 3.8.0 or GCC 5.0.0.
The Sanitizers view allows jumping back to the source code and copying warning data to the clipboard:

When the visual representation checkbox is cleared, or the compiler does not meet the requirements, the Sanitizers output is presented in plain text:

Provide Path to llvm-symbolizer
To allow Sanitizers to convert addresses to source code locations and make stack traces easier to understand, ensure that the PATH or *SAN_SYMBOLIZER_PATH environment variable includes the location of llvm-symbolizer.
You can use one of the following options:
-
Set the absolute path to the llvm-symbolizer executable.
-
Add the llvm-symbolizer executable to one of the system paths (e.g., /usr/bin).
-
Set the PATH environment variable in CLion’s run/debug configuration.
Popular Recommendations
-
Leadership requires overtime, but my girlfriend says it’s lonely at home…
-
Why does it feel like the opposition to 996 is getting quieter?
-
Interviewer: “How many offers do you have?” How should I respond?