The GCC and Clang compilers both have support for LeakSanitizer which helps finding memory leaks in C programs. Sometimes a memory leak is unavoidable (because it is being tested in a test suite for example).
Such memory can be annotated using the Leak Sanitizer interface:
#include <sanitizer/lsan_interface.h>
void *p = create_new_object();
__lsan_ignore_object(p);
This will however break on compilers that do not support LSan. In Address Sanitizer, this construct can be used to detect the availablity of ASAN:
/* __has_feature(address_sanitizer) is used later for Clang, this is for
* compatibility with other compilers (such as GCC and MSVC) */
#ifndef __has_feature
# define __has_feature(x) 0
#endif
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
/* ASAN-aware code here. */
#endif
There is no __has_feature(leak_sanitizer)
to detect just the existence of LSan in Clang and neither does __SANITIZE_LEAKS__
exist for GCC. How can I detect ASAN availability anyway? Note that LSan can be enabled independently of AddressSanitizer and ThreadSanitizer.