55#include < iostream>
66#include < string>
77#include < vector>
8+ #include < binlog/binlog.hpp>
9+ #include < cmath>
10+ #include < charconv>
811
912namespace codeql {
1013
@@ -18,6 +21,7 @@ class UntypedTrapLabel {
1821 friend class std ::hash<UntypedTrapLabel>;
1922 template <typename Tag>
2023 friend class TrapLabel ;
24+ BINLOG_ADAPT_STRUCT_FRIEND;
2125
2226 static constexpr uint64_t undefined = 0xffffffffffffffff ;
2327
@@ -38,7 +42,21 @@ class UntypedTrapLabel {
3842 return out;
3943 }
4044
45+ std::string str () const {
46+ std::string ret (strSize (), ' \0 ' );
47+ ret[0 ] = ' #' ;
48+ std::to_chars (ret.data () + 1 , ret.data () + ret.size (), id_, 16 );
49+ return ret;
50+ }
51+
4152 friend bool operator ==(UntypedTrapLabel lhs, UntypedTrapLabel rhs) { return lhs.id_ == rhs.id_ ; }
53+
54+ private:
55+ size_t strSize () const {
56+ if (id_ == undefined) return 17 ; // #ffffffffffffffff
57+ if (id_ == 0 ) return 2 ; // #0
58+ return /* # */ 1 + /* hex digits */ static_cast <size_t >(ceil (log2 (id_ + 1 ) / 4 ));
59+ }
4260};
4361
4462template <typename TagParam>
@@ -100,3 +118,33 @@ struct hash<codeql::UntypedTrapLabel> {
100118 }
101119};
102120} // namespace std
121+
122+ namespace mserialize {
123+ // log labels using their string representation, using binlog/mserialize internal plumbing
124+ template <>
125+ struct CustomTag <codeql::UntypedTrapLabel, void > : detail::BuiltinTag<std::string> {
126+ using T = codeql::UntypedTrapLabel;
127+ };
128+
129+ template <typename Tag>
130+ struct CustomTag <codeql::TrapLabel<Tag>, void > : detail::BuiltinTag<std::string> {
131+ using T = codeql::TrapLabel<Tag>;
132+ };
133+
134+ template <>
135+ struct CustomSerializer <codeql::UntypedTrapLabel, void > {
136+ template <typename OutputStream>
137+ static void serialize (codeql::UntypedTrapLabel label, OutputStream& out) {
138+ mserialize::serialize (label.str (), out);
139+ }
140+
141+ static size_t serialized_size (codeql::UntypedTrapLabel label) {
142+ return sizeof (std::uint32_t ) + label.strSize ();
143+ }
144+ };
145+
146+ template <typename Tag>
147+ struct CustomSerializer <codeql::TrapLabel<Tag>, void > : CustomSerializer<codeql::UntypedTrapLabel> {
148+ };
149+
150+ } // namespace mserialize
0 commit comments