Skip to content

Commit 6458016

Browse files
committed
Add fmt printer for Ref<Type>
1 parent 5094770 commit 6458016

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

binaryninjaapi.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9403,7 +9403,7 @@ namespace BinaryNinja {
94039403
bool EnumerateTypesForAccess(BinaryView* data, uint64_t offset, size_t size, uint8_t baseConfidence,
94049404
const std::function<void(const Confidence<Ref<Type>>& type, FieldResolutionInfo* path)>& terminal);
94059405
std::vector<TypeDefinitionLine> GetLines(const TypeContainer& types, const std::string& name,
9406-
int paddingCols = 64, bool collapsed = false, BNTokenEscapingType escaping = NoTokenEscapingType);
9406+
int paddingCols = 64, bool collapsed = false, BNTokenEscapingType escaping = NoTokenEscapingType) const;
94079407

94089408
static std::string GetSizeSuffix(size_t size);
94099409
};
@@ -21001,7 +21001,6 @@ template<> struct fmt::formatter<BinaryNinja::NameList>
2100121001
constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator { return ctx.begin(); }
2100221002
};
2100321003

21004-
2100521004
template<> struct fmt::formatter<BinaryNinja::StringRef> : fmt::formatter<std::string_view>
2100621005
{
2100721006
format_context::iterator format(const BinaryNinja::StringRef& obj, format_context& ctx) const
@@ -21051,3 +21050,18 @@ struct fmt::formatter<T, char, std::enable_if_t<std::is_enum_v<T>, void>>
2105121050
return it;
2105221051
}
2105321052
};
21053+
21054+
template<> struct fmt::formatter<BinaryNinja::Type>
21055+
{
21056+
// s -> short, ? -> full
21057+
char presentation = 's';
21058+
format_context::iterator format(const BinaryNinja::Type& obj, format_context& ctx) const;
21059+
constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator
21060+
{
21061+
auto it = ctx.begin(), end = ctx.end();
21062+
if (it != end && *it == '?') presentation = *it++;
21063+
if (it != end && *it != '}') report_error("invalid format");
21064+
return it;
21065+
}
21066+
};
21067+

type.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ bool Type::EnumerateTypesForAccess(BinaryView* data, uint64_t offset, size_t siz
13321332

13331333

13341334
std::vector<TypeDefinitionLine> Type::GetLines(const TypeContainer& types, const std::string& name,
1335-
int paddingCols, bool collapsed, BNTokenEscapingType escaping)
1335+
int paddingCols, bool collapsed, BNTokenEscapingType escaping) const
13361336
{
13371337
size_t count;
13381338
BNTypeDefinitionLine* list =
@@ -3110,3 +3110,36 @@ bool BinaryNinja::PreprocessSource(
31103110
delete[] includeDirList;
31113111
return result;
31123112
}
3113+
3114+
3115+
fmt::format_context::iterator fmt::formatter<BinaryNinja::Type>::format(
3116+
const Type& obj,
3117+
fmt::format_context& ctx
3118+
) const
3119+
{
3120+
if (presentation == '?')
3121+
{
3122+
auto tc = TypeContainer::GetEmptyTypeContainer();
3123+
auto lines = obj.GetLines(tc, "");
3124+
3125+
string result = "";
3126+
for (auto& line: lines)
3127+
{
3128+
string lineStr = "";
3129+
for (auto& token: line.tokens)
3130+
{
3131+
lineStr += token.text;
3132+
}
3133+
result += lineStr + "\n";
3134+
}
3135+
// Remove trailing newline
3136+
if (!result.empty())
3137+
result.erase(result.size() - 1);
3138+
return fmt::format_to(ctx.out(), "{}", result);
3139+
}
3140+
else
3141+
{
3142+
return fmt::format_to(ctx.out(), "{}{}", obj.GetStringBeforeName(), obj.GetStringAfterName());
3143+
}
3144+
}
3145+

0 commit comments

Comments
 (0)