Skip to content

Commit

Permalink
Merge pull request #110 from kornilova-l/usesType-fixes
Browse files Browse the repository at this point in the history
Do not search usages of typedef in other typedefs
  • Loading branch information
kornilova203 authored Jul 7, 2018
2 parents 9f25e72 + 70cc150 commit 87b7d3c
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 30 deletions.
7 changes: 4 additions & 3 deletions bindgen/ir/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func) {
return s;
}

bool Function::usesType(std::shared_ptr<Type> type) const {
if (retType == type) {
bool Function::usesType(std::shared_ptr<Type> type, bool stopOnTypeDefs) const {
if (*retType == *type || retType.get()->usesType(type, stopOnTypeDefs)) {
return true;
}
for (const auto &parameter : parameters) {
if (parameter->getType() == type) {
if (*parameter->getType() == *type ||
parameter->getType().get()->usesType(type, stopOnTypeDefs)) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion bindgen/ir/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Function {
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
const Function &func);

bool usesType(std::shared_ptr<Type> type) const;
bool usesType(std::shared_ptr<Type> type, bool stopOnTypeDefs) const;

std::string getName() const;

Expand Down
14 changes: 9 additions & 5 deletions bindgen/ir/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,22 @@ void IR::replaceTypeInTypeDefs(std::shared_ptr<Type> oldType,

template <typename T>
bool IR::isTypeUsed(const std::vector<T> &declarations,
std::shared_ptr<Type> type) {
for (const auto decl : declarations) {
if (decl->usesType(type)) {
std::shared_ptr<Type> type, bool stopOnTypeDefs) {
for (const auto &decl : declarations) {
if (decl->usesType(type, stopOnTypeDefs)) {
return true;
}
}
return false;
}

bool IR::typeIsUsedOnlyInTypeDefs(std::shared_ptr<Type> type) {
return !(isTypeUsed(functions, type) || isTypeUsed(structs, type) ||
isTypeUsed(unions, type));
/* varDefines are not checked here because they are simply
* aliases for variables.*/
return !(
isTypeUsed(functions, type, true) || isTypeUsed(structs, type, true) ||
isTypeUsed(unions, type, true) || isTypeUsed(variables, type, true) ||
isTypeUsed(literalDefines, type, true));
}

void IR::setScalaNames() {
Expand Down
2 changes: 1 addition & 1 deletion bindgen/ir/IR.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class IR {
*/
template <typename T>
bool isTypeUsed(const std::vector<T> &declarations,
std::shared_ptr<Type> type);
std::shared_ptr<Type> type, bool stopOnTypeDefs);

void setScalaNames();

Expand Down
6 changes: 6 additions & 0 deletions bindgen/ir/LiteralDefine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
<< " = " << literalDefine.literal << "\n";
return s;
}

bool LiteralDefine::usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const {
return *this->type == *type ||
this->type.get()->usesType(type, stopOnTypeDefs);
}
2 changes: 2 additions & 0 deletions bindgen/ir/LiteralDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class LiteralDefine : public Define {
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
const LiteralDefine &literalDefine);

bool usesType(const std::shared_ptr<Type> &type, bool stopOnTypeDefs) const;

private:
std::string literal;
std::shared_ptr<Type> type;
Expand Down
6 changes: 4 additions & 2 deletions bindgen/ir/Struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ std::string Struct::str() const {
return ss.str();
}

bool Struct::usesType(const std::shared_ptr<Type> &type) const {
bool Struct::usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const {
for (const auto &field : fields) {
if (*field->getType() == *type) {
if (*field->getType() == *type ||
field->getType().get()->usesType(type, stopOnTypeDefs)) {
return true;
}
}
Expand Down
3 changes: 2 additions & 1 deletion bindgen/ir/Struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class Struct : public StructOrUnion,
*/
bool hasHelperMethods() const;

bool usesType(const std::shared_ptr<Type> &type) const override;
bool usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const override;

std::string str() const override;

Expand Down
6 changes: 6 additions & 0 deletions bindgen/ir/TypeAndName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ bool TypeAndName::operator==(const TypeAndName &other) const {
bool TypeAndName::operator!=(const TypeAndName &other) const {
return !(*this == other);
}

bool TypeAndName::usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const {
return *this->type == *type ||
this->type.get()->usesType(type, stopOnTypeDefs);
}
2 changes: 2 additions & 0 deletions bindgen/ir/TypeAndName.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class TypeAndName {

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

bool usesType(const std::shared_ptr<Type> &type, bool stopOnTypeDefs) const;

protected:
std::string name;
std::shared_ptr<Type> type;
Expand Down
9 changes: 7 additions & 2 deletions bindgen/ir/TypeDef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const TypeDef &typeDef) {
return s;
}

bool TypeDef::usesType(const std::shared_ptr<Type> &type) const {
return *this->type == *type;
bool TypeDef::usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const {
if (stopOnTypeDefs) {
return false;
}
return *this->type == *type ||
this->type.get()->usesType(type, stopOnTypeDefs);
}

std::string TypeDef::str() const { return handleReservedWords(name); }
Expand Down
3 changes: 2 additions & 1 deletion bindgen/ir/TypeDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class TypeDef : public TypeAndName, public Type {
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s,
const TypeDef &type);

bool usesType(const std::shared_ptr<Type> &type) const override;
bool usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const override;

std::string str() const override;

Expand Down
6 changes: 4 additions & 2 deletions bindgen/ir/types/ArrayType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ std::string ArrayType::str() const {
uint64ToScalaNat(size) + "]";
}

bool ArrayType::usesType(const std::shared_ptr<Type> &type) const {
return *elementsType == *type;
bool ArrayType::usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const {
return *elementsType == *type ||
elementsType.get()->usesType(type, stopOnTypeDefs);
}

bool ArrayType::operator==(const Type &other) const {
Expand Down
3 changes: 2 additions & 1 deletion bindgen/ir/types/ArrayType.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class ArrayType : public Type {

~ArrayType() override = default;

bool usesType(const std::shared_ptr<Type> &type) const override;
bool usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const override;

std::string str() const override;

Expand Down
9 changes: 6 additions & 3 deletions bindgen/ir/types/FunctionPointerType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ std::string FunctionPointerType::str() const {
return ss.str();
}

bool FunctionPointerType::usesType(const std::shared_ptr<Type> &type) const {
if (*returnType == *type) {
bool FunctionPointerType::usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const {
if (*returnType == *type ||
returnType.get()->usesType(type, stopOnTypeDefs)) {
return true;
}

for (const auto &parameterType : parametersTypes) {
if (*parameterType == *type) {
if (*parameterType == *type ||
parameterType.get()->usesType(type, stopOnTypeDefs)) {
return true;
}
}
Expand Down
3 changes: 2 additions & 1 deletion bindgen/ir/types/FunctionPointerType.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class FunctionPointerType : public Type {

~FunctionPointerType() override = default;

bool usesType(const std::shared_ptr<Type> &type) const override;
bool usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const override;

std::string str() const override;

Expand Down
6 changes: 4 additions & 2 deletions bindgen/ir/types/PointerType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ std::string PointerType::str() const {
return "native.Ptr[" + type->str() + "]";
}

bool PointerType::usesType(const std::shared_ptr<Type> &type) const {
return *this->type == *type;
bool PointerType::usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const {
return *this->type == *type ||
this->type.get()->usesType(type, stopOnTypeDefs);
}

bool PointerType::operator==(const Type &other) const {
Expand Down
3 changes: 2 additions & 1 deletion bindgen/ir/types/PointerType.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class PointerType : public Type {

~PointerType() override = default;

bool usesType(const std::shared_ptr<Type> &type) const override;
bool usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const override;

std::string str() const override;

Expand Down
3 changes: 2 additions & 1 deletion bindgen/ir/types/PrimitiveType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ std::string PrimitiveType::str() const { return handleReservedWords(type); }

std::string PrimitiveType::getType() const { return type; }

bool PrimitiveType::usesType(const std::shared_ptr<Type> &type) const {
bool PrimitiveType::usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const {
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion bindgen/ir/types/PrimitiveType.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class PrimitiveType : public Type {

std::string getType() const;

bool usesType(const std::shared_ptr<Type> &type) const override;
bool usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const override;

std::string str() const override;

Expand Down
5 changes: 4 additions & 1 deletion bindgen/ir/types/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

std::string Type::str() const { return ""; }

bool Type::usesType(const std::shared_ptr<Type> &type) const { return false; }
bool Type::usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const {
return false;
}

bool Type::operator==(const Type &other) const { return false; }

Expand Down
10 changes: 9 additions & 1 deletion bindgen/ir/types/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ class Type {

virtual std::string str() const;

virtual bool usesType(const std::shared_ptr<Type> &type) const;
/**
* @param stopOnTypeDefs if this parameter is true then TypeDefs instances
* will not be checked. This parameter is needed when
* usages of TypeDefs are checked, it helps to avoid
* false positives when usages if aliases for the
* typedef are found.
*/
virtual bool usesType(const std::shared_ptr<Type> &type,
bool stopOnTypeDefs) const;

virtual bool operator==(const Type &other) const;

Expand Down

0 comments on commit 87b7d3c

Please sign in to comment.