Skip to content

Commit abc0c7c

Browse files
committed
Swift: add trace logging of all trap emission
1 parent a386c58 commit abc0c7c

File tree

7 files changed

+71
-0
lines changed

7 files changed

+71
-0
lines changed

misc/codegen/templates/cpp_classes_h.mustache

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <iostream>
66
#include <optional>
77
#include <vector>
8+
#include <binlog/binlog.hpp>
9+
#include <binlog/adapt_stdoptional.hpp>
810

911
#include "{{trap_library}}/TrapLabel.h"
1012
#include "{{trap_library}}/TrapTagTraits.h"
@@ -80,3 +82,9 @@ struct detail::ToTrapClassFunctor<{{name}}Tag> {
8082
};
8183
{{/classes}}
8284
}
85+
86+
{{#classes}}
87+
{{#final}}
88+
BINLOG_ADAPT_STRUCT(codeql::{{name}}, id{{> cpp_list_fields}});
89+
{{/final}}
90+
{{/classes}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{#bases}}{{#ref}}{{> cpp_list_fields}}{{/ref}}{{/bases}}{{#fields}}, {{field_name}}{{/fields}}

misc/codegen/templates/trap_traps_h.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <iostream>
66
#include <string>
7+
#include <binlog/binlog.hpp>
78

89
#include "{{trap_library_dir}}/TrapLabel.h"
910
#include "{{trap_library_dir}}/TrapTagTraits.h"
@@ -43,3 +44,7 @@ struct ToBindingTrapFunctor<{{type}}> {
4344
{{/id}}
4445
{{/traps}}
4546
}
47+
48+
{{#traps}}
49+
BINLOG_ADAPT_STRUCT(codeql::{{name}}Trap{{#fields}}, {{field_name}}{{/fields}});
50+
{{/traps}}

swift/extractor/infra/file/TargetFile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class TargetFile {
3636
return *this;
3737
}
3838

39+
const std::filesystem::path& target() const { return targetPath; }
40+
3941
private:
4042
TargetFile(const std::filesystem::path& target,
4143
const std::filesystem::path& targetDir,

swift/extractor/trap/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ swift_cc_library(
4949
visibility = ["//visibility:public"],
5050
deps = [
5151
"//swift/extractor/infra/file",
52+
"//swift/extractor/infra/log",
5253
],
5354
)

swift/extractor/trap/TrapDomain.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55

66
#include "swift/extractor/trap/TrapLabel.h"
77
#include "swift/extractor/infra/file/TargetFile.h"
8+
#include "swift/extractor/infra/log/SwiftLogging.h"
89

910
namespace codeql {
1011

1112
// Abstracts a given trap output file, with its own universe of trap labels
1213
class TrapDomain {
1314
TargetFile out;
15+
Logger logger{out.target().filename()};
1416

1517
public:
1618
explicit TrapDomain(TargetFile&& out) : out{std::move(out)} {}
1719

1820
template <typename Entry>
1921
void emit(const Entry& e) {
22+
LOG_TRACE("{}", e);
2023
out << e << '\n';
2124
}
2225

@@ -48,6 +51,7 @@ class TrapDomain {
4851
Args&&... args) {
4952
auto ret = allocateLabel<Tag>();
5053
assignKey(ret, std::forward<Args>(args)...);
54+
LOG_TRACE("^^^ .implementation {}", implementationId);
5155
out << " .implementation " << trapQuoted(implementationId) << '\n';
5256
return ret;
5357
}
@@ -62,13 +66,15 @@ class TrapDomain {
6266

6367
template <typename Tag>
6468
void assignStar(TrapLabel<Tag> label) {
69+
LOG_TRACE("{}=*", label);
6570
out << label << "=*";
6671
}
6772

6873
template <typename Tag>
6974
void assignKey(TrapLabel<Tag> label, const std::string& key) {
7075
// prefix the key with the id to guarantee the same key is not used wrongly with different tags
7176
auto prefixed = std::string(Tag::prefix) + '_' + key;
77+
LOG_TRACE("{}=@{}", label, prefixed);
7278
out << label << "=@" << trapQuoted(prefixed);
7379
}
7480

swift/extractor/trap/TrapLabel.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <iostream>
66
#include <string>
77
#include <vector>
8+
#include <binlog/binlog.hpp>
9+
#include <cmath>
10+
#include <charconv>
811

912
namespace 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

4462
template <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

Comments
 (0)