Skip to content

Commit 137ebc6

Browse files
committed
Use smart pointers to manage memory
1 parent bd9d0df commit 137ebc6

33 files changed

+237
-294
lines changed

bindgen/CycleDetection.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,12 @@ class CycleDetection {
3434
return;
3535
}
3636

37-
Type *type = tpeTransl.translate(qtpe);
37+
std::shared_ptr<Type> type = tpeTransl.translate(qtpe);
3838
if (type == nullptr) {
3939
return;
4040
}
4141

4242
std::string qtpeString = type->str();
43-
if (type->canBeDeallocated()) {
44-
delete type;
45-
}
4643

4744
// Add the dependence of qtpe
4845
if (contains(qtpeString)) {

bindgen/TypeTranslator.cpp

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,24 @@ TypeTranslator::TypeTranslator(clang::ASTContext *ctx_, IR &ir)
3232
typeMap["double"] = "native.CDouble";
3333
}
3434

35-
Type *TypeTranslator::translateFunctionPointer(const clang::QualType &qtpe,
36-
const std::string *avoid) {
35+
std::shared_ptr<Type>
36+
TypeTranslator::translateFunctionPointer(const clang::QualType &qtpe,
37+
const std::string *avoid) {
3738
const auto *ptr = qtpe.getTypePtr()->getAs<clang::PointerType>();
3839
const clang::QualType &inner = ptr->getPointeeType();
3940

4041
if (inner->isFunctionProtoType()) {
4142
const auto *fc = inner->getAs<clang::FunctionProtoType>();
42-
Type *returnType = translate(fc->getReturnType(), avoid);
43-
std::vector<Type *> parametersTypes;
43+
std::shared_ptr<Type> returnType =
44+
translate(fc->getReturnType(), avoid);
45+
std::vector<std::shared_ptr<Type>> parametersTypes;
4446

4547
for (const clang::QualType &param : fc->param_types()) {
4648
parametersTypes.push_back(translate(param, avoid));
4749
}
4850

49-
return new FunctionPointerType(returnType, parametersTypes,
50-
fc->isVariadic());
51+
return std::make_shared<FunctionPointerType>(
52+
returnType, parametersTypes, fc->isVariadic());
5153

5254
} else {
5355
llvm::errs() << "Unsupported function pointer type: "
@@ -57,29 +59,31 @@ Type *TypeTranslator::translateFunctionPointer(const clang::QualType &qtpe,
5759
}
5860
}
5961

60-
Type *TypeTranslator::translatePointer(const clang::QualType &pte,
61-
const std::string *avoid) {
62+
std::shared_ptr<Type>
63+
TypeTranslator::translatePointer(const clang::QualType &pte,
64+
const std::string *avoid) {
6265

6366
if (pte->isBuiltinType()) {
6467
const clang::BuiltinType *as = pte->getAs<clang::BuiltinType>();
6568

6669
// Take care of void*
6770
if (as->getKind() == clang::BuiltinType::Void) {
68-
return new PointerType(new PrimitiveType("Byte"));
71+
return std::make_shared<PointerType>(
72+
std::make_shared<PrimitiveType>("Byte"));
6973
}
7074

7175
// Take care of char*
7276
if (as->getKind() == clang::BuiltinType::Char_S ||
7377
as->getKind() == clang::BuiltinType::SChar) {
7478
// TODO: new PointerType(new PrimitiveType("native.CChar"))
75-
return new PrimitiveType("native.CString");
79+
return std::make_shared<PrimitiveType>("native.CString");
7680
}
7781
}
7882

79-
return new PointerType(translate(pte, avoid));
83+
return std::make_shared<PointerType>(translate(pte, avoid));
8084
}
8185

82-
Type *
86+
std::shared_ptr<Type>
8387
TypeTranslator::translateStructOrUnionOrEnum(const clang::QualType &qtpe) {
8488
std::string name = qtpe.getUnqualifiedType().getAsString();
8589

@@ -93,40 +97,43 @@ TypeTranslator::translateStructOrUnionOrEnum(const clang::QualType &qtpe) {
9397
return ir.getTypeDefWithName(name);
9498
}
9599

96-
Type *TypeTranslator::translateStructOrUnion(const clang::QualType &qtpe) {
100+
std::shared_ptr<Type>
101+
TypeTranslator::translateStructOrUnion(const clang::QualType &qtpe) {
97102
if (qtpe->hasUnnamedOrLocalType()) {
98103
// TODO: Verify that the local part is not a problem
99104
uint64_t size = ctx->getTypeSize(qtpe);
100-
return new ArrayType(new PrimitiveType("Byte"), size);
105+
return std::make_shared<ArrayType>(
106+
std::make_shared<PrimitiveType>("Byte"), size);
101107
}
102108

103109
return translateStructOrUnionOrEnum(qtpe);
104110
}
105111

106-
Type *TypeTranslator::translateConstantArray(const clang::ConstantArrayType *ar,
107-
const std::string *avoid) {
112+
std::shared_ptr<Type>
113+
TypeTranslator::translateConstantArray(const clang::ConstantArrayType *ar,
114+
const std::string *avoid) {
108115
const uint64_t size = ar->getSize().getZExtValue();
109-
Type *elementType = translate(ar->getElementType(), avoid);
116+
std::shared_ptr<Type> elementType = translate(ar->getElementType(), avoid);
110117
if (elementType == nullptr) {
111118
llvm::errs() << "Failed to translate array type "
112-
<< ar->getElementType().getAsString()
113-
<< "\n";
114-
elementType = new PrimitiveType("Byte");
119+
<< ar->getElementType().getAsString() << "\n";
120+
elementType = std::make_shared<PrimitiveType>("Byte");
115121
}
116122

117-
return new ArrayType(elementType, size);
123+
return std::make_shared<ArrayType>(elementType, size);
118124
}
119125

120-
Type *TypeTranslator::translate(const clang::QualType &qtpe,
121-
const std::string *avoid) {
126+
std::shared_ptr<Type> TypeTranslator::translate(const clang::QualType &qtpe,
127+
const std::string *avoid) {
122128

123129
const clang::Type *tpe = qtpe.getTypePtr();
124130

125131
if (typeEquals(tpe, avoid)) {
126132
// This is a type that we want to avoid the usage.
127133
// Êxample: A struct that has a pointer to itself
128134
uint64_t size = ctx->getTypeSize(tpe);
129-
return new ArrayType(new PrimitiveType("Byte"), size);
135+
return std::make_shared<ArrayType>(
136+
std::make_shared<PrimitiveType>("Byte"), size);
130137
}
131138

132139
if (tpe->isFunctionPointerType()) {
@@ -154,15 +161,15 @@ Type *TypeTranslator::translate(const clang::QualType &qtpe,
154161

155162
auto found = typeMap.find(qtpe.getUnqualifiedType().getAsString());
156163
if (found != typeMap.end()) {
157-
return new PrimitiveType(found->second);
164+
return std::make_shared<PrimitiveType>(found->second);
158165
} else {
159166
return ir.getTypeDefWithName(
160167
qtpe.getUnqualifiedType().getAsString());
161168
}
162169
}
163170
}
164171

165-
void TypeTranslator::addAlias(std::string cName, Type *type) {
172+
void TypeTranslator::addAlias(std::string cName, std::shared_ptr<Type> type) {
166173
aliasesMap[cName] = type;
167174
}
168175

bindgen/TypeTranslator.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ class TypeTranslator {
1414
* structs, unions, ...
1515
* @return the type translated
1616
*/
17-
Type *translate(const clang::QualType &tpe, const std::string * = nullptr);
17+
std::shared_ptr<Type> translate(const clang::QualType &tpe,
18+
const std::string * = nullptr);
1819

19-
void addAlias(std::string cName, Type *type);
20+
void addAlias(std::string cName, std::shared_ptr<Type> type);
2021

2122
std::string getTypeFromTypeMap(std::string cType);
2223

@@ -32,18 +33,20 @@ class TypeTranslator {
3233
/**
3334
* Maps C struct, union or enum name to Type alias
3435
*/
35-
std::map<std::string, Type *> aliasesMap;
36+
std::map<std::string, std::shared_ptr<Type>> aliasesMap;
3637

37-
Type *translateStructOrUnionOrEnum(const clang::QualType &qtpe);
38+
std::shared_ptr<Type>
39+
translateStructOrUnionOrEnum(const clang::QualType &qtpe);
3840

39-
Type *translateStructOrUnion(const clang::QualType &qtpe);
41+
std::shared_ptr<Type> translateStructOrUnion(const clang::QualType &qtpe);
4042

41-
Type *translateFunctionPointer(const clang::QualType &qtpe,
42-
const std::string *avoid);
43+
std::shared_ptr<Type> translateFunctionPointer(const clang::QualType &qtpe,
44+
const std::string *avoid);
4345

44-
Type *translatePointer(const clang::QualType &pointee,
45-
const std::string *avoid);
46+
std::shared_ptr<Type> translatePointer(const clang::QualType &pointee,
47+
const std::string *avoid);
4648

47-
Type *translateConstantArray(const clang::ConstantArrayType *ar,
48-
const std::string *avoid);
49+
std::shared_ptr<Type>
50+
translateConstantArray(const clang::ConstantArrayType *ar,
51+
const std::string *avoid);
4952
};

bindgen/defines/DefineFinder.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ void DefineFinder::MacroDefined(const clang::Token &macroNameTok,
4242
clang::Token stringToken = (*tokens)[0];
4343
std::string literal(stringToken.getLiteralData(),
4444
stringToken.getLength());
45-
ir.addLiteralDefine(macroName, "c" + literal,
46-
new PrimitiveType("native.CString"));
45+
ir.addLiteralDefine(
46+
macroName, "c" + literal,
47+
std::make_shared<PrimitiveType>("native.CString"));
4748
} else if (tokens->size() == 1 &&
4849
(*tokens)[0].getKind() == clang::tok::identifier) {
4950
// token might be a variable
@@ -152,7 +153,8 @@ void DefineFinder::addNumericConstantDefine(const std::string &macroName,
152153
if (!positive) {
153154
scalaLiteral = "-" + scalaLiteral;
154155
}
155-
ir.addLiteralDefine(macroName, scalaLiteral, new PrimitiveType(type));
156+
ir.addLiteralDefine(macroName, scalaLiteral,
157+
std::make_shared<PrimitiveType>(type));
156158
}
157159
}
158160

bindgen/ir/Enum.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Enum::Enum(std::string name, std::string type,
1414

1515
bool Enum::isAnonymous() const { return name.empty(); }
1616

17-
TypeDef *Enum::generateTypeDef() {
17+
std::shared_ptr<TypeDef> Enum::generateTypeDef() {
1818
assert(!isAnonymous());
19-
return new TypeDef("enum_" + name, this);
19+
return std::make_shared<TypeDef>("enum_" + name, shared_from_this());
2020
}
2121

2222
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) {
@@ -49,5 +49,3 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) {
4949
}
5050

5151
std::string Enum::getName() const { return name; }
52-
53-
bool Enum::canBeDeallocated() const { return false; }

bindgen/ir/Enum.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,19 @@ class Enumerator {
1818
int64_t value;
1919
};
2020

21-
class Enum : public PrimitiveType {
21+
class Enum : public PrimitiveType, public std::enable_shared_from_this<Enum> {
2222
public:
2323
Enum(std::string name, std::string type,
2424
std::vector<Enumerator> enumerators);
2525

2626
bool isAnonymous() const;
2727

28-
TypeDef *generateTypeDef();
28+
std::shared_ptr<TypeDef> generateTypeDef();
2929

3030
std::string getName() const;
3131

3232
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e);
3333

34-
bool canBeDeallocated() const override;
35-
3634
private:
3735
std::string name; // might be empty
3836
std::vector<Enumerator> enumerators;

bindgen/ir/Function.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include "Function.h"
22
#include "../Utils.h"
33

4-
Parameter::Parameter(std::string name, Type *type)
4+
Parameter::Parameter(std::string name, std::shared_ptr<Type> type)
55
: TypeAndName(std::move(name), type) {}
66

77
Function::Function(const std::string &name, std::vector<Parameter *> parameters,
8-
Type *retType, bool isVariadic)
8+
std::shared_ptr<Type> retType, bool isVariadic)
99
: name(name), scalaName(name), parameters(std::move(parameters)),
1010
retType(retType), isVariadic(isVariadic) {}
1111

@@ -29,7 +29,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func) {
2929
return s;
3030
}
3131

32-
bool Function::usesType(Type *type) const {
32+
bool Function::usesType(std::shared_ptr<Type> type) const {
3333
if (retType == type) {
3434
return true;
3535
}
@@ -65,16 +65,6 @@ void Function::setScalaName(std::string scalaName) {
6565
this->scalaName = std::move(scalaName);
6666
}
6767

68-
void Function::deallocateTypesThatAreNotInIR() {
69-
if (retType->canBeDeallocated()) {
70-
delete retType;
71-
}
72-
73-
for (const auto &parameter : parameters) {
74-
parameter->deallocateTypesThatAreNotInIR();
75-
}
76-
}
77-
7868
Function::~Function() {
7969
for (const auto &parameter : parameters) {
8070
delete parameter;

bindgen/ir/Function.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,25 @@
88

99
class Parameter : public TypeAndName {
1010
public:
11-
Parameter(std::string name, Type *type);
11+
Parameter(std::string name, std::shared_ptr<Type> type);
1212
};
1313

1414
class Function {
1515
public:
1616
Function(const std::string &name, std::vector<Parameter *> parameters,
17-
Type *retType, bool isVariadic);
17+
std::shared_ptr<Type> retType, bool isVariadic);
1818

1919
~Function();
2020

2121
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
2222
const Function &func);
2323

24-
bool usesType(Type *type) const;
24+
bool usesType(std::shared_ptr<Type> type) const;
2525

2626
std::string getName() const;
2727

2828
void setScalaName(std::string scalaName);
2929

30-
void deallocateTypesThatAreNotInIR();
31-
3230
private:
3331
std::string getVarargsParameterName() const;
3432

@@ -37,7 +35,7 @@ class Function {
3735
std::string name; // real name of the function
3836
std::string scalaName; // not empty
3937
std::vector<Parameter *> parameters;
40-
Type *retType;
38+
std::shared_ptr<Type> retType;
4139
bool isVariadic;
4240
};
4341

0 commit comments

Comments
 (0)