Repository: godotengine/godot
File: core/typedefs.h (C/C++)

Code before:
/**
 * Basic definitions and simple functions to be used everywhere.
 */

// IWYU pragma: always_keep

// Ensure that C++ standard is at least C++17.
// If on MSVC, also ensures that the `Zc:__cplusplus` flag is present.
static_assert(__cplusplus >= 201703L, "Minimum of C++17 required.");

// IWYU pragma: begin_exports

// Include first in case the platform needs to pre-define/include some things.
#include "platform_config.h"

// Should be available everywhere.
#include "core/error/error_list.h"

#include <cstddef>
#include <cstdint>
#include <cstring>
#include <utility>

#ifndef __has_feature
#define __has_feature(m_feature) 0
#endif

#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
#define ASAN_ENABLED
#include <sanitizer/asan_interface.h>
#endif

#if __has_feature(thread_sanitizer) || defined(__SANITIZE_THREAD__)
#define TSAN_ENABLED
#include <sanitizer/tsan_interface.h>
#endif

#if __has_feature(memory_sanitizer) || defined(__SANITIZE_MEMORY__)
#define MSAN_ENABLED
#include <sanitizer/msan_interface.h>
#endif

#if __has_feature(leak_sanitizer) || defined(__SANITIZE_LEAKS__)
#define LSAN_ENABLED
#include <sanitizer/lsan_interface.h>
#endif

#if __has_feature(undefined_behavior_sanitizer)
#define UBSAN_ENABLED
#include <sanitizer/ubsan_interface.h>
#endif

Review comment (suggestion):
Having this in `typedefs.h` can break code elsewhere that would do:
```
#ifdef __has_feature
#if __has_feature(foo)
// Do stuff.
#endif
#else
#if defined(__FOO__)
// Do stuff.
#endif
#endif
```
as it wouldn't run the `#else`.

That's a bit convoluted but I wouldn't be surprised to find code like this in thirdparty libs (not affected by our typedefs for the most part) but still it's a bad practice IMO as `#ifdef __has_feature` is a very common check before using `__has_feature`.



Code after:
/**
 * Basic definitions and simple functions to be used everywhere.
 */

// IWYU pragma: always_keep

// Ensure that C++ standard is at least C++17.
// If on MSVC, also ensures that the `Zc:__cplusplus` flag is present.
static_assert(__cplusplus >= 201703L, "Minimum of C++17 required.");

// IWYU pragma: begin_exports

// Include first in case the platform needs to pre-define/include some things.
#include "platform_config.h"

// Should be available everywhere.
#include "core/error/error_list.h"

#include <cstddef>
#include <cstdint>
#include <cstring>
#include <type_traits>
#include <utility>

// IWYU pragma: end_exports

#if defined(__has_feature)
#define GD_HAS_FEATURE(m_feature) __has_feature(m_feature)
#else
#define GD_HAS_FEATURE(m_feature) 0
#endif

#if (GD_HAS_FEATURE(address_sanitizer) || defined(__SANITIZE_ADDRESS__)) && !defined(ASAN_ENABLED)
#error Address sanitizer was enabled without defining `ASAN_ENABLED`
#endif

#if (GD_HAS_FEATURE(leak_sanitizer) || defined(__SANITIZE_LEAKS__)) && !defined(LSAN_ENABLED)
#error Leak sanitizer was enabled without defining `LSAN_ENABLED`
#endif

#if (GD_HAS_FEATURE(memory_sanitizer) || defined(__SANITIZE_MEMORY__)) && !defined(MSAN_ENABLED)
#error Memory sanitizer was enabled without defining `MSAN_ENABLED`
#endif

#if (GD_HAS_FEATURE(thread_sanitizer) || defined(__SANITIZE_THREAD__)) && !defined(TSAN_ENABLED)
#error Thread sanitizer was enabled without defining `TSAN_ENABLED`
#endif

#if (GD_HAS_FEATURE(undefined_behavior_sanitizer) || defined(__UNDEFINED_SANITIZER__)) && !defined(UBSAN_ENABLED)
#error Undefined behavior sanitizer was enabled without defining `UBSAN_ENABLED`
#endif