Skip to content

Commit 70cc150

Browse files
committed
Do not search usages of typedef in other typedefs
Fixes Function::usesType method
1 parent 9f25e72 commit 70cc150

22 files changed

+83
-30
lines changed

bindgen/ir/Function.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func) {
2929
return s;
3030
}
3131

32-
bool Function::usesType(std::shared_ptr<Type> type) const {
33-
if (retType == type) {
32+
bool Function::usesType(std::shared_ptr<Type> type, bool stopOnTypeDefs) const {
33+
if (*retType == *type || retType.get()->usesType(type, stopOnTypeDefs)) {
3434
return true;
3535
}
3636
for (const auto &parameter : parameters) {
37-
if (parameter->getType() == type) {
37+
if (*parameter->getType() == *type ||
38+
parameter->getType().get()->usesType(type, stopOnTypeDefs)) {
3839
return true;
3940
}
4041
}

bindgen/ir/Function.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Function {
2121
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
2222
const Function &func);
2323

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

2626
std::string getName() const;
2727

bindgen/ir/IR.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,22 @@ void IR::replaceTypeInTypeDefs(std::shared_ptr<Type> oldType,
230230

231231
template <typename T>
232232
bool IR::isTypeUsed(const std::vector<T> &declarations,
233-
std::shared_ptr<Type> type) {
234-
for (const auto decl : declarations) {
235-
if (decl->usesType(type)) {
233+
std::shared_ptr<Type> type, bool stopOnTypeDefs) {
234+
for (const auto &decl : declarations) {
235+
if (decl->usesType(type, stopOnTypeDefs)) {
236236
return true;
237237
}
238238
}
239239
return false;
240240
}
241241

242242
bool IR::typeIsUsedOnlyInTypeDefs(std::shared_ptr<Type> type) {
243-
return !(isTypeUsed(functions, type) || isTypeUsed(structs, type) ||
244-
isTypeUsed(unions, type));
243+
/* varDefines are not checked here because they are simply
244+
* aliases for variables.*/
245+
return !(
246+
isTypeUsed(functions, type, true) || isTypeUsed(structs, type, true) ||
247+
isTypeUsed(unions, type, true) || isTypeUsed(variables, type, true) ||
248+
isTypeUsed(literalDefines, type, true));
245249
}
246250

247251
void IR::setScalaNames() {

bindgen/ir/IR.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class IR {
116116
*/
117117
template <typename T>
118118
bool isTypeUsed(const std::vector<T> &declarations,
119-
std::shared_ptr<Type> type);
119+
std::shared_ptr<Type> type, bool stopOnTypeDefs);
120120

121121
void setScalaNames();
122122

bindgen/ir/LiteralDefine.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
1010
<< " = " << literalDefine.literal << "\n";
1111
return s;
1212
}
13+
14+
bool LiteralDefine::usesType(const std::shared_ptr<Type> &type,
15+
bool stopOnTypeDefs) const {
16+
return *this->type == *type ||
17+
this->type.get()->usesType(type, stopOnTypeDefs);
18+
}

bindgen/ir/LiteralDefine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class LiteralDefine : public Define {
1313
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
1414
const LiteralDefine &literalDefine);
1515

16+
bool usesType(const std::shared_ptr<Type> &type, bool stopOnTypeDefs) const;
17+
1618
private:
1719
std::string literal;
1820
std::shared_ptr<Type> type;

bindgen/ir/Struct.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ std::string Struct::str() const {
134134
return ss.str();
135135
}
136136

137-
bool Struct::usesType(const std::shared_ptr<Type> &type) const {
137+
bool Struct::usesType(const std::shared_ptr<Type> &type,
138+
bool stopOnTypeDefs) const {
138139
for (const auto &field : fields) {
139-
if (*field->getType() == *type) {
140+
if (*field->getType() == *type ||
141+
field->getType().get()->usesType(type, stopOnTypeDefs)) {
140142
return true;
141143
}
142144
}

bindgen/ir/Struct.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class Struct : public StructOrUnion,
5454
*/
5555
bool hasHelperMethods() const;
5656

57-
bool usesType(const std::shared_ptr<Type> &type) const override;
57+
bool usesType(const std::shared_ptr<Type> &type,
58+
bool stopOnTypeDefs) const override;
5859

5960
std::string str() const override;
6061

bindgen/ir/TypeAndName.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ bool TypeAndName::operator==(const TypeAndName &other) const {
1717
bool TypeAndName::operator!=(const TypeAndName &other) const {
1818
return !(*this == other);
1919
}
20+
21+
bool TypeAndName::usesType(const std::shared_ptr<Type> &type,
22+
bool stopOnTypeDefs) const {
23+
return *this->type == *type ||
24+
this->type.get()->usesType(type, stopOnTypeDefs);
25+
}

bindgen/ir/TypeAndName.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class TypeAndName {
2323

2424
bool operator!=(const TypeAndName &other) const;
2525

26+
bool usesType(const std::shared_ptr<Type> &type, bool stopOnTypeDefs) const;
27+
2628
protected:
2729
std::string name;
2830
std::shared_ptr<Type> type;

0 commit comments

Comments
 (0)