From b6b98c07eb8ce704da4edf5a0b16959215a2c450 Mon Sep 17 00:00:00 2001 From: Mark Rowe Date: Fri, 30 May 2025 09:16:48 -0700 Subject: [ObjC] Prefer displaying id rather than objc_object* --- lang/c/objctypes.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lang/c/objctypes.cpp (limited to 'lang/c/objctypes.cpp') diff --git a/lang/c/objctypes.cpp b/lang/c/objctypes.cpp new file mode 100644 index 00000000..83a097be --- /dev/null +++ b/lang/c/objctypes.cpp @@ -0,0 +1,45 @@ +#include "objctypes.h" +#include "binaryninjaapi.h" + +using namespace BinaryNinja; + +namespace { + +bool IsPointerToObjCObject(const Ref& type) +{ + if (!type || type->GetClass() != PointerTypeClass) + return false; + + auto childType = type->GetChildType(); + if (!childType || childType->GetClass() != NamedTypeReferenceClass) + return false; + + auto namedType = childType->GetNamedTypeReference(); + return namedType && namedType->GetName().GetString() == "objc_object"; +} + +} // unnamed namespace + +PseudoObjCTypePrinter::PseudoObjCTypePrinter() : TypePrinter("Objective-C") {} + +std::vector PseudoObjCTypePrinter::GetTypeTokensBeforeName( + Ref type, Ref platform, uint8_t baseConfidence, Ref parentType, BNTokenEscapingType escaping) +{ + // It is idiomatic in Objective-C to use `id` rather than `objc_object*`. + if (IsPointerToObjCObject(type)) + return {InstructionTextToken {baseConfidence, TypeNameToken, "id"}}; + + return TypePrinter::GetDefault()->GetTypeTokensBeforeName(type, platform, baseConfidence, parentType, escaping); +} + +std::vector PseudoObjCTypePrinter::GetTypeTokensAfterName( + Ref type, Ref platform, uint8_t baseConfidence, Ref parentType, BNTokenEscapingType escaping) +{ + return TypePrinter::GetDefault()->GetTypeTokensAfterName(type, platform, baseConfidence, parentType, escaping); +} + +std::vector PseudoObjCTypePrinter::GetTypeLines(Ref type, const TypeContainer& types, + const QualifiedName& name, int paddingCols, bool collapsed, BNTokenEscapingType escaping) +{ + return TypePrinter::GetDefault()->GetTypeLines(type, types, name, paddingCols, collapsed, escaping); +} -- cgit v1.3.1