diff options
| author | Glenn Smith <glenn@vector35.com> | 2025-05-20 18:36:42 -0400 |
|---|---|---|
| committer | Glenn Smith <glenn@vector35.com> | 2025-05-22 15:09:32 -0400 |
| commit | 6458016449500151053964411398145244374352 (patch) | |
| tree | 0bccdae090d891591f5fa7844c7878f73c78e75e | |
| parent | 5094770405372098081110764843950364933904 (diff) | |
Add fmt printer for Ref<Type>
| -rw-r--r-- | binaryninjaapi.h | 18 | ||||
| -rw-r--r-- | type.cpp | 35 |
2 files changed, 50 insertions, 3 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 520c038d..42364c2c 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -9403,7 +9403,7 @@ namespace BinaryNinja { bool EnumerateTypesForAccess(BinaryView* data, uint64_t offset, size_t size, uint8_t baseConfidence, const std::function<void(const Confidence<Ref<Type>>& type, FieldResolutionInfo* path)>& terminal); std::vector<TypeDefinitionLine> GetLines(const TypeContainer& types, const std::string& name, - int paddingCols = 64, bool collapsed = false, BNTokenEscapingType escaping = NoTokenEscapingType); + int paddingCols = 64, bool collapsed = false, BNTokenEscapingType escaping = NoTokenEscapingType) const; static std::string GetSizeSuffix(size_t size); }; @@ -21001,7 +21001,6 @@ template<> struct fmt::formatter<BinaryNinja::NameList> constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator { return ctx.begin(); } }; - template<> struct fmt::formatter<BinaryNinja::StringRef> : fmt::formatter<std::string_view> { 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>> return it; } }; + +template<> struct fmt::formatter<BinaryNinja::Type> +{ + // s -> short, ? -> full + char presentation = 's'; + format_context::iterator format(const BinaryNinja::Type& obj, format_context& ctx) const; + constexpr auto parse(format_parse_context& ctx) -> format_parse_context::iterator + { + auto it = ctx.begin(), end = ctx.end(); + if (it != end && *it == '?') presentation = *it++; + if (it != end && *it != '}') report_error("invalid format"); + return it; + } +}; + @@ -1332,7 +1332,7 @@ bool Type::EnumerateTypesForAccess(BinaryView* data, uint64_t offset, size_t siz std::vector<TypeDefinitionLine> Type::GetLines(const TypeContainer& types, const std::string& name, - int paddingCols, bool collapsed, BNTokenEscapingType escaping) + int paddingCols, bool collapsed, BNTokenEscapingType escaping) const { size_t count; BNTypeDefinitionLine* list = @@ -3110,3 +3110,36 @@ bool BinaryNinja::PreprocessSource( delete[] includeDirList; return result; } + + +fmt::format_context::iterator fmt::formatter<BinaryNinja::Type>::format( + const Type& obj, + fmt::format_context& ctx +) const +{ + if (presentation == '?') + { + auto tc = TypeContainer::GetEmptyTypeContainer(); + auto lines = obj.GetLines(tc, ""); + + string result = ""; + for (auto& line: lines) + { + string lineStr = ""; + for (auto& token: line.tokens) + { + lineStr += token.text; + } + result += lineStr + "\n"; + } + // Remove trailing newline + if (!result.empty()) + result.erase(result.size() - 1); + return fmt::format_to(ctx.out(), "{}", result); + } + else + { + return fmt::format_to(ctx.out(), "{}{}", obj.GetStringBeforeName(), obj.GetStringAfterName()); + } +} + |
