|
41 | 41 | #endif // __FILE_NAME__ |
42 | 42 |
|
43 | 43 |
|
44 | | -#if defined(__has_include) && __has_include(<sanitizer/lsan_interface.h>) && (defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer) || __has_feature(leak_sanitizer)) |
45 | | -#include <sanitizer/lsan_interface.h> |
46 | | -/** |
47 | | - * Tell the leak sanitizer to ignore that the given object is leaked. |
48 | | - */ |
49 | | -#define DICE_IGNORE_LEAK(ptr) __lsan_ignore_object(ptr) |
| 44 | +#if defined(__has_attribute) && __has_attribute(weak) |
| 45 | +#define DICE_WEAK __attribute__((weak)) |
| 46 | +#define DICE_HAS_WEAK 1 |
50 | 47 | #else |
51 | | -#define DICE_IGNORE_LEAK(ptr) |
| 48 | +#define DICE_WEAK |
| 49 | +#define DICE_HAS_WEAK 0 |
| 50 | +#endif |
| 51 | + |
| 52 | +#if DICE_HAS_WEAK |
| 53 | +// forward declaration for __lsan_ignore_object from <sanitizer/lsan_interface.h> as a weak symbol. |
| 54 | +// If the sanitizer is linked this it set to the correct value by the linker, if not this is set to nullptr by the linker. |
| 55 | +extern "C" DICE_WEAK void __lsan_ignore_object(void const *ptr); // NOLINT(bugprone-reserved-identifier) |
| 56 | +#endif |
| 57 | + |
| 58 | +namespace dice::template_library { |
| 59 | + /** |
| 60 | + * Tell the leak sanitizer to ignore that the given object is leaked. |
| 61 | + */ |
| 62 | + inline void ignore_leak(void const *ptr) noexcept { |
| 63 | +#if DICE_HAS_WEAK |
| 64 | + if (__lsan_ignore_object) { |
| 65 | + __lsan_ignore_object(ptr); |
| 66 | + } |
52 | 67 | #endif |
53 | 68 |
|
| 69 | + (void) ptr; |
| 70 | + } |
| 71 | + |
| 72 | + namespace detail_ignore_leak { |
| 73 | + [[deprecated("DICE_IGNORE_LEAK is deprecated. Use dice::template_library::ignore_leak() instead.")]] |
| 74 | + inline void deprecated_macro_use() { |
| 75 | + } |
| 76 | + } // namespace detail_ignore_leak |
| 77 | +} // namespace dice::template_library |
| 78 | + |
| 79 | +/** |
| 80 | + * Tell the leak sanitizer to ignore that the given object is leaked. |
| 81 | + * This macro is deprecated, use dice::template_library::ignore_leak instead. |
| 82 | + */ |
| 83 | +#define DICE_IGNORE_LEAK(ptr) (::dice::template_library::detail_ignore_leak::deprecated_macro_use(), ::dice::template_library::ignore_leak(ptr)) |
54 | 84 |
|
55 | 85 | #endif // DICE_TEMPLATELIBRARY_MACROUTIL_HPP |
0 commit comments