Skip to content

Commit 699f4d5

Browse files
committed
Change type of StructOrUnion::fields to vector of shared_ptr
1 parent 67ef777 commit 699f4d5

File tree

5 files changed

+21
-27
lines changed

5 files changed

+21
-27
lines changed

bindgen/ir/IR.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void IR::addEnum(std::string name, const std::string &type,
3232
}
3333
}
3434

35-
void IR::addStruct(std::string name, std::vector<Field *> fields,
35+
void IR::addStruct(std::string name, std::vector<std::shared_ptr<Field>> fields,
3636
uint64_t typeSize, std::shared_ptr<Location> location) {
3737
std::shared_ptr<Struct> s = std::make_shared<Struct>(
3838
name, std::move(fields), typeSize, std::move(location));
@@ -46,7 +46,7 @@ void IR::addStruct(std::string name, std::vector<Field *> fields,
4646
}
4747
}
4848

49-
void IR::addUnion(std::string name, std::vector<Field *> fields,
49+
void IR::addUnion(std::string name, std::vector<std::shared_ptr<Field>> fields,
5050
uint64_t maxSize, std::shared_ptr<Location> location) {
5151
std::shared_ptr<Union> u = std::make_shared<Union>(
5252
name, std::move(fields), maxSize, std::move(location));

bindgen/ir/IR.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class IR {
3434
std::vector<Enumerator> enumerators,
3535
std::shared_ptr<Location> location);
3636

37-
void addStruct(std::string name, std::vector<Field *> fields,
37+
void addStruct(std::string name, std::vector<std::shared_ptr<Field>> fields,
3838
uint64_t typeSize, std::shared_ptr<Location> location);
3939

40-
void addUnion(std::string name, std::vector<Field *> fields,
40+
void addUnion(std::string name, std::vector<std::shared_ptr<Field>> fields,
4141
uint64_t maxSize, std::shared_ptr<Location> location);
4242

4343
void addLiteralDefine(std::string name, std::string literal,

bindgen/ir/Struct.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,14 @@ std::string Field::generateGetter(int fieldIndex) {
3838
return s.str();
3939
}
4040

41-
StructOrUnion::StructOrUnion(std::string name, std::vector<Field *> fields,
41+
StructOrUnion::StructOrUnion(std::string name,
42+
std::vector<std::shared_ptr<Field>> fields,
4243
std::shared_ptr<Location> location)
4344
: name(std::move(name)), fields(std::move(fields)),
4445
location(std::move(location)) {}
4546

4647
std::string StructOrUnion::getName() const { return name; }
4748

48-
StructOrUnion::~StructOrUnion() {
49-
for (const auto &field : fields) {
50-
delete field;
51-
}
52-
}
53-
5449
bool StructOrUnion::equals(const StructOrUnion &other) const {
5550
if (this == &other) {
5651
return true;
@@ -77,8 +72,8 @@ std::shared_ptr<Location> StructOrUnion::getLocation() const {
7772
return location;
7873
}
7974

80-
Struct::Struct(std::string name, std::vector<Field *> fields, uint64_t typeSize,
81-
std::shared_ptr<Location> location)
75+
Struct::Struct(std::string name, std::vector<std::shared_ptr<Field>> fields,
76+
uint64_t typeSize, std::shared_ptr<Location> location)
8277
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
8378
typeSize(typeSize) {}
8479

@@ -163,8 +158,8 @@ bool Struct::operator==(const Type &other) const {
163158
return false;
164159
}
165160

166-
Union::Union(std::string name, std::vector<Field *> fields, uint64_t maxSize,
167-
std::shared_ptr<Location> location)
161+
Union::Union(std::string name, std::vector<std::shared_ptr<Field>> fields,
162+
uint64_t maxSize, std::shared_ptr<Location> location)
168163
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
169164
ArrayType(std::make_shared<PrimitiveType>("Byte"), maxSize) {}
170165

bindgen/ir/Struct.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ class Field : public TypeAndName {
2020

2121
class StructOrUnion {
2222
public:
23-
StructOrUnion(std::string name, std::vector<Field *> fields,
23+
StructOrUnion(std::string name, std::vector<std::shared_ptr<Field>> fields,
2424
std::shared_ptr<Location> location);
2525

26-
~StructOrUnion();
27-
2826
virtual std::shared_ptr<TypeDef> generateTypeDef() = 0;
2927

3028
virtual std::string generateHelperClass() const = 0;
@@ -39,16 +37,16 @@ class StructOrUnion {
3937

4038
protected:
4139
std::string name;
42-
std::vector<Field *> fields;
40+
std::vector<std::shared_ptr<Field>> fields;
4341
std::shared_ptr<Location> location;
4442
};
4543

4644
class Struct : public StructOrUnion,
4745
public Type,
4846
public std::enable_shared_from_this<Struct> {
4947
public:
50-
Struct(std::string name, std::vector<Field *> fields, uint64_t typeSize,
51-
std::shared_ptr<Location> location);
48+
Struct(std::string name, std::vector<std::shared_ptr<Field>> fields,
49+
uint64_t typeSize, std::shared_ptr<Location> location);
5250

5351
std::shared_ptr<TypeDef> generateTypeDef() override;
5452

@@ -77,8 +75,8 @@ class Union : public StructOrUnion,
7775
public ArrayType,
7876
public std::enable_shared_from_this<Union> {
7977
public:
80-
Union(std::string name, std::vector<Field *> fields, uint64_t maxSize,
81-
std::shared_ptr<Location> location);
78+
Union(std::string name, std::vector<std::shared_ptr<Field>> fields,
79+
uint64_t maxSize, std::shared_ptr<Location> location);
8280

8381
std::shared_ptr<TypeDef> generateTypeDef() override;
8482

bindgen/visitor/TreeVisitor.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record) {
101101
void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
102102
uint64_t maxSize = 0;
103103

104-
std::vector<Field *> fields;
104+
std::vector<std::shared_ptr<Field>> fields;
105105

106106
for (const clang::FieldDecl *field : record->fields()) {
107107
uint64_t sizeInBits = astContext->getTypeSize(field->getType());
@@ -111,7 +111,7 @@ void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
111111
std::shared_ptr<Type> ftype =
112112
typeTranslator.translate(field->getType(), &name);
113113

114-
fields.push_back(new Field(fname, ftype));
114+
fields.push_back(std::make_shared<Field>(fname, ftype));
115115
}
116116

117117
ir.addUnion(name, std::move(fields), maxSize, getLocation(record));
@@ -128,12 +128,13 @@ void TreeVisitor::handleStruct(clang::RecordDecl *record, std::string name) {
128128
}
129129

130130
int fieldCnt = 0;
131-
std::vector<Field *> fields;
131+
std::vector<std::shared_ptr<Field>> fields;
132132

133133
for (const clang::FieldDecl *field : record->fields()) {
134134
std::shared_ptr<Type> ftype =
135135
typeTranslator.translate(field->getType(), &name);
136-
fields.push_back(new Field(field->getNameAsString(), ftype));
136+
fields.push_back(
137+
std::make_shared<Field>(field->getNameAsString(), ftype));
137138

138139
cycleDetection.AddDependency(newName, field->getType());
139140

0 commit comments

Comments
 (0)