reduce clang errors for header file, fix better array size struct, emit required forward definitions in header

This commit is contained in:
LukeFZ
2025-07-25 21:21:10 +02:00
parent 6ddbf7ecae
commit 48473e9247

View File

@@ -44,8 +44,13 @@ namespace Il2CppInspector.Outputs
// Write primitive type definitions for when we're not including other headers // Write primitive type definitions for when we're not including other headers
writeCode($""" writeCode($"""
#define IS_LIBCLANG_DECOMPILER (defined(_IDACLANG_) || defined(_BINARYNINJA_)) #if defined(_IDACLANG_) || defined(_BINARYNINJA_)
#define IS_DECOMPILER (defined(_GHIDRA_) || defined(_IDA_) || IS_LIBCLANG_DECOMPILER) #define IS_LIBCLANG_DECOMPILER
#endif
#if defined(_GHIDRA_) || defined(_IDA_) || defined(IS_LIBCLANG_DECOMPILER)
#define IS_DECOMPILER
#endif
#if defined(_GHIDRA_) || defined(_IDA_) #if defined(_GHIDRA_) || defined(_IDA_)
typedef unsigned __int8 uint8_t; typedef unsigned __int8 uint8_t;
@@ -58,7 +63,7 @@ namespace Il2CppInspector.Outputs
typedef __int64 int64_t; typedef __int64 int64_t;
#endif #endif
#if IS_LIBCLANG_DECOMPILER #if defined(IS_LIBCLANG_DECOMPILER)
typedef unsigned char uint8_t; typedef unsigned char uint8_t;
typedef unsigned short uint16_t; typedef unsigned short uint16_t;
typedef unsigned int uint32_t; typedef unsigned int uint32_t;
@@ -67,21 +72,26 @@ namespace Il2CppInspector.Outputs
typedef short int16_t; typedef short int16_t;
typedef int int32_t; typedef int int32_t;
typedef long int64_t; typedef long int64_t;
#ifdef linux
#undef linux
#endif #endif
#if defined(_GHIDRA_) || IS_LIBCLANG_DECOMPILER #endif
#if defined(_GHIDRA_) || defined(IS_LIBCLANG_DECOMPILER)
typedef int{_model.Package.BinaryImage.Bits}_t intptr_t; typedef int{_model.Package.BinaryImage.Bits}_t intptr_t;
typedef uint{_model.Package.BinaryImage.Bits}_t uintptr_t; typedef uint{_model.Package.BinaryImage.Bits}_t uintptr_t;
typedef uint{_model.Package.BinaryImage.Bits}_t size_t; typedef uint{_model.Package.BinaryImage.Bits}_t size_t;
#endif #endif
#if !IS_DECOMPILER #ifndef IS_DECOMPILER
#define _CPLUSPLUS_ #define _CPLUSPLUS_
#endif #endif
"""); """);
if (_useBetterArraySize) if (_useBetterArraySize)
writeCode("#define actual_il2cpp_array_size_t il2cpp_array_size_t"); writeCode("#define il2cpp_array_size_t actual_il2cpp_array_size_t");
writeSectionHeader("IL2CPP internal types"); writeSectionHeader("IL2CPP internal types");
writeCode(_model.UnityHeaders.GetTypeHeaderText(_model.WordSizeBits)); writeCode(_model.UnityHeaders.GetTypeHeaderText(_model.WordSizeBits));
@@ -96,7 +106,7 @@ namespace Il2CppInspector.Outputs
actual_il2cpp_array_size_t value; actual_il2cpp_array_size_t value;
} better_il2cpp_array_size_t; } better_il2cpp_array_size_t;
#define better_il2cpp_array_size_t il2cpp_array_size_t #define il2cpp_array_size_t better_il2cpp_array_size_t
"""); """);
if (_model.TargetCompiler == CppCompilerType.MSVC) if (_model.TargetCompiler == CppCompilerType.MSVC)
@@ -115,17 +125,20 @@ namespace Il2CppInspector.Outputs
} }
// C does not support namespaces // C does not support namespaces
writeCode("#if !IS_DECOMPILER"); writeCode("#ifndef IS_DECOMPILER");
writeCode("namespace app {"); writeCode("namespace app {");
writeCode("#endif"); writeCode("#endif");
writeLine(""); writeLine("");
writeForwardDefinitions();
writeTypesForGroup("Required forward definitions", "required_forward_definitions");
writeTypesForGroup("Application types from method calls", "types_from_methods"); writeTypesForGroup("Application types from method calls", "types_from_methods");
writeTypesForGroup("Application types from generic methods", "types_from_generic_methods"); writeTypesForGroup("Application types from generic methods", "types_from_generic_methods");
writeTypesForGroup("Application types from usages", "types_from_usages"); writeTypesForGroup("Application types from usages", "types_from_usages");
writeTypesForGroup("Application unused value types", "unused_concrete_types"); writeTypesForGroup("Application unused value types", "unused_concrete_types");
writeCode("#if !IS_DECOMPILER"); writeCode("#ifndef IS_DECOMPILER");
writeCode("}"); writeCode("}");
writeCode("#endif"); writeCode("#endif");
} }
@@ -306,19 +319,32 @@ namespace Il2CppInspector.Outputs
writeLine(""); writeLine("");
} }
private void writeTypesForGroup(string header, string group) { private void writeForwardDefinitions()
{
writeSectionHeader("Required forward definitions");
foreach (var cppType in _model.RequiredForwardDefinitions)
writeCode(cppType.ToString());
}
private void writeTypesForGroup(string header, string group)
{
writeSectionHeader(header); writeSectionHeader(header);
foreach (var cppType in _model.GetDependencyOrderedCppTypeGroup(group)) foreach (var cppType in _model.GetDependencyOrderedCppTypeGroup(group))
if (cppType is CppEnumType) { {
if (cppType is CppEnumType)
{
// Ghidra can't process C++ enum base types // Ghidra can't process C++ enum base types
writeCode("#if defined(_CPLUSPLUS_)"); writeCode("#if defined(_CPLUSPLUS_)");
writeCode(cppType.ToString()); writeCode(cppType.ToString());
writeCode("#else"); writeCode("#else");
writeCode(cppType.ToString("c")); writeCode(cppType.ToString("c"));
writeCode("#endif"); writeCode("#endif");
} else { }
else
{
writeCode(cppType.ToString()); writeCode(cppType.ToString());
} }
}
} }
private void writeCode(string text) { private void writeCode(string text) {