Skip to content

Commit 087f908

Browse files
committed
[ObjC] Prefer displaying id rather than objc_object*
1 parent 13ce138 commit 087f908

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

lang/c/objctypes.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "objctypes.h"
2+
#include "binaryninjaapi.h"
3+
4+
using namespace BinaryNinja;
5+
6+
namespace {
7+
8+
bool IsPointerToObjCObject(const Ref<Type>& type)
9+
{
10+
if (!type || type->GetClass() != PointerTypeClass)
11+
return false;
12+
13+
auto childType = type->GetChildType();
14+
if (!childType || childType->GetClass() != NamedTypeReferenceClass)
15+
return false;
16+
17+
auto namedType = childType->GetNamedTypeReference();
18+
return namedType && namedType->GetName().GetString() == "objc_object";
19+
}
20+
21+
} // unnamed namespace
22+
23+
PseudoObjCTypePrinter::PseudoObjCTypePrinter() : TypePrinter("Objective-C") {}
24+
25+
std::vector<InstructionTextToken> PseudoObjCTypePrinter::GetTypeTokensBeforeName(
26+
Ref<Type> type, Ref<Platform> platform, uint8_t baseConfidence, Ref<Type> parentType, BNTokenEscapingType escaping)
27+
{
28+
// It is idiomatic in Objective-C to use `id` rather than `objc_object*`.
29+
if (IsPointerToObjCObject(type))
30+
return {InstructionTextToken {baseConfidence, TypeNameToken, "id"}};
31+
32+
return TypePrinter::GetDefault()->GetTypeTokensBeforeName(type, platform, baseConfidence, parentType, escaping);
33+
}
34+
35+
std::vector<InstructionTextToken> PseudoObjCTypePrinter::GetTypeTokensAfterName(
36+
Ref<Type> type, Ref<Platform> platform, uint8_t baseConfidence, Ref<Type> parentType, BNTokenEscapingType escaping)
37+
{
38+
return TypePrinter::GetDefault()->GetTypeTokensAfterName(type, platform, baseConfidence, parentType, escaping);
39+
}
40+
41+
std::vector<TypeDefinitionLine> PseudoObjCTypePrinter::GetTypeLines(Ref<Type> type, const TypeContainer& types,
42+
const QualifiedName& name, int paddingCols, bool collapsed, BNTokenEscapingType escaping)
43+
{
44+
return TypePrinter::GetDefault()->GetTypeLines(type, types, name, paddingCols, collapsed, escaping);
45+
}

lang/c/objctypes.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma once
2+
3+
#include "binaryninjaapi.h"
4+
5+
class PseudoObjCTypePrinter : public BinaryNinja::TypePrinter
6+
{
7+
public:
8+
PseudoObjCTypePrinter();
9+
10+
std::vector<BinaryNinja::InstructionTextToken> GetTypeTokensBeforeName(BinaryNinja::Ref<BinaryNinja::Type> type,
11+
BinaryNinja::Ref<BinaryNinja::Platform> platform,
12+
uint8_t baseConfidence = BN_FULL_CONFIDENCE, BinaryNinja::Ref<BinaryNinja::Type> parentType = nullptr,
13+
BNTokenEscapingType escaping = NoTokenEscapingType) override;
14+
15+
std::vector<BinaryNinja::InstructionTextToken> GetTypeTokensAfterName(BinaryNinja::Ref<BinaryNinja::Type> type,
16+
BinaryNinja::Ref<BinaryNinja::Platform> platform,
17+
uint8_t baseConfidence = BN_FULL_CONFIDENCE, BinaryNinja::Ref<BinaryNinja::Type> parentType = nullptr,
18+
BNTokenEscapingType escaping = NoTokenEscapingType) override;
19+
20+
std::vector<BinaryNinja::TypeDefinitionLine> GetTypeLines(BinaryNinja::Ref<BinaryNinja::Type> type,
21+
const BinaryNinja::TypeContainer& types,
22+
const BinaryNinja::QualifiedName& name, int paddingCols = 64, bool collapsed = false,
23+
BNTokenEscapingType escaping = NoTokenEscapingType) override;
24+
};

lang/c/pseudoobjc.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "binaryninjaapi.h"
44
#include "highlevelilinstruction.h"
5+
#include "objctypes.h"
56
#include <optional>
67
#include <string>
78
#include <vector>
@@ -457,3 +458,8 @@ Ref<LanguageRepresentationFunction> PseudoObjCFunctionType::Create(
457458
{
458459
return new PseudoObjCFunction(this, arch, owner, highLevelILFunction);
459460
}
461+
462+
Ref<TypePrinter> PseudoObjCFunctionType::GetTypePrinter()
463+
{
464+
return new PseudoObjCTypePrinter();
465+
}

lang/c/pseudoobjc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ class PseudoObjCFunctionType : public PseudoCFunctionType {
4141
PseudoObjCFunctionType();
4242
BinaryNinja::Ref<BinaryNinja::LanguageRepresentationFunction> Create(BinaryNinja::Architecture* arch,
4343
BinaryNinja::Function* owner, BinaryNinja::HighLevelILFunction* highLevelILFunction) override;
44+
BinaryNinja::Ref<BinaryNinja::TypePrinter> GetTypePrinter() override;
4445
};

0 commit comments

Comments
 (0)