Skip to content

Commit bd826f9

Browse files
committed
CodeGen: Store list of ContainerInfos in unique_ptrs for reference stability
Lots of places rely on reference stability of ContainerInfo objects (CodeGen's deduplication, Container nodes' containerInfo_ member). In the key capture work, we need to be able to append to this list, which would invalidate references before this change.
1 parent 5632738 commit bd826f9

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

oi/CodeGen.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,8 +1061,9 @@ bool CodeGen::codegenFromDrgn(struct drgn_type* drgnType, std::string& code) {
10611061
}
10621062

10631063
void CodeGen::registerContainer(const fs::path& path) {
1064-
const auto& info = containerInfos_.emplace_back(path);
1065-
VLOG(1) << "Registered container: " << info.typeName;
1064+
auto info = std::make_unique<ContainerInfo>(path);
1065+
VLOG(1) << "Registered container: " << info->typeName;
1066+
containerInfos_.emplace_back(std::move(info));
10661067
}
10671068

10681069
void CodeGen::addDrgnRoot(struct drgn_type* drgnType, TypeGraph& typeGraph) {

oi/CodeGen.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <filesystem>
1919
#include <functional>
20+
#include <memory>
2021
#include <string>
2122
#include <unordered_map>
2223
#include <unordered_set>
@@ -65,7 +66,7 @@ class CodeGen {
6566
private:
6667
const OICodeGen::Config& config_;
6768
SymbolService& symbols_;
68-
std::vector<ContainerInfo> containerInfos_;
69+
std::vector<std::unique_ptr<ContainerInfo>> containerInfos_;
6970
std::unordered_set<const ContainerInfo*> definedContainers_;
7071
std::unordered_map<const type_graph::Class*, const type_graph::Member*>
7172
thriftIssetMembers_;

oi/type_graph/DrgnParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ Container* DrgnParser::enumerateContainer(struct drgn_type* type,
138138
auto size = get_drgn_type_size(type);
139139

140140
for (const auto& containerInfo : containers_) {
141-
if (!std::regex_search(fqName, containerInfo.matcher)) {
141+
if (!std::regex_search(fqName, containerInfo->matcher)) {
142142
continue;
143143
}
144144

145-
VLOG(2) << "Matching container `" << containerInfo.typeName << "` from `"
145+
VLOG(2) << "Matching container `" << containerInfo->typeName << "` from `"
146146
<< fqName << "`" << std::endl;
147-
auto& c = makeType<Container>(type, containerInfo, size);
147+
auto& c = makeType<Container>(type, *containerInfo, size);
148148
enumerateClassTemplateParams(type, c.templateParams);
149149
return &c;
150150
}

oi/type_graph/DrgnParser.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#pragma once
1717

18+
#include <memory>
1819
#include <unordered_map>
1920
#include <vector>
2021

@@ -53,7 +54,7 @@ struct DrgnParserOptions {
5354
class DrgnParser {
5455
public:
5556
DrgnParser(TypeGraph& typeGraph,
56-
const std::vector<ContainerInfo>& containers,
57+
const std::vector<std::unique_ptr<ContainerInfo>>& containers,
5758
DrgnParserOptions options)
5859
: typeGraph_(typeGraph), containers_(containers), options_(options) {
5960
}
@@ -97,7 +98,7 @@ class DrgnParser {
9798
drgn_types_;
9899

99100
TypeGraph& typeGraph_;
100-
const std::vector<ContainerInfo>& containers_;
101+
const std::vector<std::unique_ptr<ContainerInfo>>& containers_;
101102
int depth_;
102103
DrgnParserOptions options_;
103104
};

test/test_drgn_parser.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ using ::testing::HasSubstr;
2121
SymbolService* DrgnParserTest::symbols_ = nullptr;
2222

2323
namespace {
24-
const std::vector<ContainerInfo>& getContainerInfos() {
24+
const std::vector<std::unique_ptr<ContainerInfo>>& getContainerInfos() {
2525
static auto res = []() {
2626
// TODO more container types, with various template parameter options
27-
ContainerInfo std_vector{"std::vector", SEQ_TYPE, "vector"};
28-
std_vector.stubTemplateParams = {1};
27+
auto std_vector =
28+
std::make_unique<ContainerInfo>("std::vector", SEQ_TYPE, "vector");
29+
std_vector->stubTemplateParams = {1};
2930

30-
std::vector<ContainerInfo> containers;
31+
std::vector<std::unique_ptr<ContainerInfo>> containers;
3132
containers.emplace_back(std::move(std_vector));
3233
return containers;
3334
}();

0 commit comments

Comments
 (0)