title: How can I know if Leak Sanitizer is enabled at compile time?
body: <p>The GCC and Clang compilers both have support for <a href="https://code.google.com/p/address-sanitizer/wiki/LeakSanitizer" rel="noreferrer">LeakSanitizer</a> 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).</p>
  
  <p>Such memory can be annotated using the <a href="https://github.com/llvm-mirror/compiler-rt/blob/master/include/sanitizer/lsan_interface.h" rel="noreferrer">Leak Sanitizer interface</a>:</p>
  
  <pre><code>#include &lt;sanitizer/lsan_interface.h&gt;
  
  void *p = create_new_object();
  __lsan_ignore_object(p);
  </code></pre>
  
  <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:</p>
  
  <pre><code>/* __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
  </code></pre>
  
  <p>There is no <code>__has_feature(leak_sanitizer)</code> to detect just the existence of LSan in Clang and neither does <code>__SANITIZE_LEAKS__</code> exist for GCC. How can I detect ASAN availability anyway? Note that LSan can be enabled independently of AddressSanitizer and ThreadSanitizer.</p>