C++: Handle C and C++ versions of enum type definitions correctly (#62)

This commit is contained in:
Katy Coe
2020-09-09 19:06:09 +02:00
parent a548fbb4f6
commit 4a2cc55cb6
2 changed files with 17 additions and 2 deletions

View File

@@ -407,11 +407,15 @@ namespace Il2CppInspector.Cpp
// Return the type as a field // Return the type as a field
public override string ToFieldString(string fieldName) => "enum " + Name + " " + fieldName; public override string ToFieldString(string fieldName) => "enum " + Name + " " + fieldName;
// Format specifier: 'c' = don't output C++-style enum with base type, use C-compatible code only
public override string ToString(string format = "") { public override string ToString(string format = "") {
var sb = new StringBuilder(); var sb = new StringBuilder();
// Don't output " : {underlyingType.Name}" because it breaks C // Don't output " : {underlyingType.Name}" because it breaks C
if (format.Contains('c'))
sb.Append($"enum {Name} {{"); sb.Append($"enum {Name} {{");
else
sb.Append($"enum {Name} : {UnderlyingType.Name} {{");
foreach (var field in Fields.Values.SelectMany(f => f)) foreach (var field in Fields.Values.SelectMany(f => f))
sb.Append("\n " + string.Join("\n ", field.ToString(format).Split('\n')) + ","); sb.Append("\n " + string.Join("\n ", field.ToString(format).Split('\n')) + ",");

View File

@@ -49,6 +49,8 @@ typedef __int64 int64_t;
typedef __int{model.Package.BinaryImage.Bits} size_t; typedef __int{model.Package.BinaryImage.Bits} size_t;
typedef size_t intptr_t; typedef size_t intptr_t;
typedef size_t uintptr_t; typedef size_t uintptr_t;
#else
#define _CPLUSPLUS_
#endif #endif
"); ");
@@ -247,7 +249,16 @@ typedef size_t uintptr_t;
private void writeTypesForGroup(string header, string group) { 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) {
// Ghidra can't process C++ enum base types
writeCode("#if defined(_CPLUSPLUS_)");
writeCode(cppType.ToString()); writeCode(cppType.ToString());
writeCode("#else");
writeCode(cppType.ToString("c"));
writeCode("#endif");
} else {
writeCode(cppType.ToString());
}
} }
private void writeCode(string text) { private void writeCode(string text) {