Skip to content

Commit 9bb4311

Browse files
committed
Add STATIC_ASSERT_INCOMPLETE_CLASS to enforce include minimality.
Add enforcements against `Dictionary` for `ustring.h` and two for `Dictionary` and `String` from `array.h`.
1 parent 6f2ab52 commit 9bb4311

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
@@ -632,6 +632,7 @@ if env["strict_checks"]:
632632

633633
# Run SCU file generation script if in a SCU build.
634634
if env["scu_build"]:
635+
env.Append(CPPDEFINES=["SCU_BUILD_ENABLED"])
635636
max_includes_per_scu = 8
636637
if env.dev_build:
637638
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_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
@@ -440,3 +440,23 @@ inline constexpr bool is_zero_constructible_v = is_zero_constructible<T>::value;
440440
#define GODOT_MSVC_WARNING_POP
441441
#define GODOT_MSVC_WARNING_PUSH_AND_IGNORE(m_warning)
442442
#endif
443+
444+
template <typename T, typename = void>
445+
struct is_fully_defined : std::false_type {};
446+
447+
template <typename T>
448+
struct is_fully_defined<T, std::void_t<decltype(sizeof(T))>> : std::true_type {};
449+
450+
template <typename T>
451+
constexpr bool is_fully_defined_v = is_fully_defined<T>::value;
452+
453+
#if !defined(SCU_BUILD_ENABLED) && !defined(DEV_ENABLED) && !defined(DEBUG_ENABLED)
454+
/// Enforces the requirement that a class is not fully defined.
455+
/// This can be used to reduce include coupling and keep compile times low.
456+
/// The check must be made at the top of the corresponding .cpp file of a header.
457+
#define STATIC_ASSERT_INCOMPLETE_CLASS(m_type) \
458+
class m_type; \
459+
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 class.");
460+
#else
461+
#define STATIC_ASSERT_INCOMPLETE_CLASS(m_type)
462+
#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_CLASS(Dictionary);
34+
STATIC_ASSERT_INCOMPLETE_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)