Skip to content

Commit e63d079

Browse files
committed
Swift: transfer TrapArena
1 parent 25336df commit e63d079

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

swift/extractor/SwiftExtractor.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <llvm/Support/Path.h>
1414

1515
#include "swift/extractor/trap/TrapClasses.h"
16+
#include "swift/extractor/trap/TrapArena.h"
1617

1718
using namespace codeql;
1819

@@ -75,10 +76,12 @@ static void extractFile(const SwiftExtractorConfiguration& config, swift::Source
7576
}
7677
trap << "\n\n";
7778

79+
TrapArena arena{trap};
80+
// arena will be passed to another class in a later PR, the next block of code is only an example
7881
File f;
79-
f.id = TrapLabel<FileTag>{};
82+
f.id = arena.getLabel<FileTag>();
8083
f.name = srcFilePath.str().str();
81-
trap << f.id << "=*\n" << f;
84+
arena.emit(f);
8285

8386
// TODO: Pick a better name to avoid collisions
8487
std::string trapName = file.getFilename().str() + ".trap";

swift/extractor/trap/TrapArena.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// Created by redsun82 on 18.01.22.
3+
//
4+
5+
#pragma once
6+
7+
#include <iostream>
8+
#include <sstream>
9+
#include <vector>
10+
11+
#include "swift/extractor/trap/TrapLabel.h"
12+
13+
namespace codeql {
14+
15+
// TrapArena has the responsibilities to
16+
// * allocate distinct trap #-labels outputting their assignment to '*' or a @-keys to a trap output
17+
// * forwarding trap entries to said output
18+
// TODO: split or move these responsibilities into separate classes
19+
class TrapArena {
20+
public:
21+
explicit TrapArena(std::ostream& out) : out_{out} {}
22+
23+
// get a new label for *
24+
template <typename Tag>
25+
TrapLabel<Tag> getLabel() {
26+
auto ret = allocateLabel<Tag>();
27+
print(ret, "=*");
28+
return ret;
29+
}
30+
31+
// get a new label for an @-key
32+
template <typename Tag>
33+
TrapLabel<Tag> getLabel(const std::string& key) {
34+
auto ret = allocateLabel<Tag>();
35+
// prefix the key with the id to guarantee the same key is not used wrongly with different tags
36+
auto prefixed = std::string(Tag::prefix) + '_' + key;
37+
print(ret, "=@", quoted(prefixed));
38+
return ret;
39+
}
40+
41+
// same as getLabel(const std::string&) above, concatenating keyParts
42+
template <typename Tag, typename... Args>
43+
TrapLabel<Tag> getLabel(const Args&... keyParts) {
44+
std::ostringstream oss;
45+
(oss << ... << keyParts);
46+
return getLabel<Tag>(oss.str());
47+
}
48+
49+
// emit a trap entry
50+
template <typename Entry>
51+
void emit(const Entry& e) {
52+
print(e);
53+
}
54+
55+
private:
56+
template <typename... Args>
57+
void print(const Args&... args) {
58+
(out_ << ... << args) << '\n';
59+
}
60+
61+
template <typename Tag>
62+
TrapLabel<Tag> allocateLabel() {
63+
return {id_++};
64+
}
65+
66+
uint64_t id_{0};
67+
std::ostream& out_;
68+
};
69+
70+
} // namespace codeql

swift/extractor/trap/TrapLabel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class TrapLabel : public UntypedTrapLabel {
3434

3535
using UntypedTrapLabel::UntypedTrapLabel;
3636

37+
// we want only TrapArena to create non-default labels
38+
friend class TrapArena;
39+
3740
public:
3841
using Tag = TagParam;
3942

0 commit comments

Comments
 (0)