Skip to content

Commit 25de1a3

Browse files
committed
Merge pull request #107587 from Ivorforce/static-assert-includes
Add `STATIC_ASSERT_INCOMPLETE_TYPE` to enforce include minimality.
2 parents caefb0f + 712bc99 commit 25de1a3

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

SConstruct

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ if env["strict_checks"]:
634634

635635
# Run SCU file generation script if in a SCU build.
636636
if env["scu_build"]:
637+
env.Append(CPPDEFINES=["SCU_BUILD_ENABLED"])
637638
max_includes_per_scu = 8
638639
if env.dev_build:
639640
max_includes_per_scu = 1024

core/string/ustring.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
#include "ustring.h"
3232

33+
STATIC_ASSERT_INCOMPLETE_TYPE(class, Dictionary);
34+
3335
#include "core/crypto/crypto_core.h"
3436
#include "core/math/color.h"
3537
#include "core/math/math_funcs.h"

core/typedefs.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,23 @@ inline constexpr bool is_zero_constructible_v = is_zero_constructible<T>::value;
439439
#define GODOT_MSVC_WARNING_POP
440440
#define GODOT_MSVC_WARNING_PUSH_AND_IGNORE(m_warning)
441441
#endif
442+
443+
template <typename T, typename = void>
444+
struct is_fully_defined : std::false_type {};
445+
446+
template <typename T>
447+
struct is_fully_defined<T, std::void_t<decltype(sizeof(T))>> : std::true_type {};
448+
449+
template <typename T>
450+
constexpr bool is_fully_defined_v = is_fully_defined<T>::value;
451+
452+
#ifndef SCU_BUILD_ENABLED
453+
/// Enforces the requirement that a class is not fully defined.
454+
/// This can be used to reduce include coupling and keep compile times low.
455+
/// The check must be made at the top of the corresponding .cpp file of a header.
456+
#define STATIC_ASSERT_INCOMPLETE_TYPE(m_keyword, m_type) \
457+
m_keyword m_type; \
458+
static_assert(!is_fully_defined_v<m_type>, #m_type " was unexpectedly fully defined. Please check the include hierarchy of '" __FILE__ "' and remove includes that resolve the " #m_keyword ".");
459+
#else
460+
#define STATIC_ASSERT_INCOMPLETE_TYPE(m_keyword, m_type)
461+
#endif

core/variant/array.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
#include "array.h"
3232

33+
STATIC_ASSERT_INCOMPLETE_TYPE(class, Dictionary);
34+
STATIC_ASSERT_INCOMPLETE_TYPE(class, String);
35+
3336
#include "container_type_validate.h"
3437
#include "core/math/math_funcs.h"
3538
#include "core/object/script_language.h"

0 commit comments

Comments
 (0)