Skip to content

Commit af3fb3d

Browse files
authored
Fix: DICE_IGNORE_LEAK (#192)
1 parent 62a72b4 commit af3fb3d

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

include/dice/template-library/macro_util.hpp

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,45 @@
4141
#endif // __FILE_NAME__
4242

4343

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
5047
#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+
}
5267
#endif
5368

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))
5484

5585
#endif // DICE_TEMPLATELIBRARY_MACROUTIL_HPP

tests/tests_macro_util.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ TEST_SUITE("macro_util") {
2121

2222
TEST_CASE("ignore leak") {
2323
auto *my_obj = new int{42};
24+
dice::template_library::ignore_leak(my_obj);
25+
}
26+
27+
TEST_CASE("ignore leak, deprecated macro") {
28+
auto *my_obj = new int{42};
29+
30+
#pragma GCC diagnostic push
31+
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
2432
DICE_IGNORE_LEAK(my_obj);
33+
#pragma GCC diagnostic pop
2534
}
2635
}

0 commit comments

Comments
 (0)