From 70cc15064fd9021451a6c4824713cc05de56938b Mon Sep 17 00:00:00 2001 From: kornilova-l Date: Fri, 6 Jul 2018 12:49:07 +0300 Subject: [PATCH] Do not search usages of typedef in other typedefs Fixes Function::usesType method --- bindgen/ir/Function.cpp | 7 ++++--- bindgen/ir/Function.h | 2 +- bindgen/ir/IR.cpp | 14 +++++++++----- bindgen/ir/IR.h | 2 +- bindgen/ir/LiteralDefine.cpp | 6 ++++++ bindgen/ir/LiteralDefine.h | 2 ++ bindgen/ir/Struct.cpp | 6 ++++-- bindgen/ir/Struct.h | 3 ++- bindgen/ir/TypeAndName.cpp | 6 ++++++ bindgen/ir/TypeAndName.h | 2 ++ bindgen/ir/TypeDef.cpp | 9 +++++++-- bindgen/ir/TypeDef.h | 3 ++- bindgen/ir/types/ArrayType.cpp | 6 ++++-- bindgen/ir/types/ArrayType.h | 3 ++- bindgen/ir/types/FunctionPointerType.cpp | 9 ++++++--- bindgen/ir/types/FunctionPointerType.h | 3 ++- bindgen/ir/types/PointerType.cpp | 6 ++++-- bindgen/ir/types/PointerType.h | 3 ++- bindgen/ir/types/PrimitiveType.cpp | 3 ++- bindgen/ir/types/PrimitiveType.h | 3 ++- bindgen/ir/types/Type.cpp | 5 ++++- bindgen/ir/types/Type.h | 10 +++++++++- 22 files changed, 83 insertions(+), 30 deletions(-) diff --git a/bindgen/ir/Function.cpp b/bindgen/ir/Function.cpp index 1e94437..fb173da 100644 --- a/bindgen/ir/Function.cpp +++ b/bindgen/ir/Function.cpp @@ -29,12 +29,13 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func) { return s; } -bool Function::usesType(std::shared_ptr type) const { - if (retType == type) { +bool Function::usesType(std::shared_ptr type, bool stopOnTypeDefs) const { + if (*retType == *type || retType.get()->usesType(type, stopOnTypeDefs)) { return true; } for (const auto ¶meter : parameters) { - if (parameter->getType() == type) { + if (*parameter->getType() == *type || + parameter->getType().get()->usesType(type, stopOnTypeDefs)) { return true; } } diff --git a/bindgen/ir/Function.h b/bindgen/ir/Function.h index 659e93d..0514309 100644 --- a/bindgen/ir/Function.h +++ b/bindgen/ir/Function.h @@ -21,7 +21,7 @@ class Function { friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Function &func); - bool usesType(std::shared_ptr type) const; + bool usesType(std::shared_ptr type, bool stopOnTypeDefs) const; std::string getName() const; diff --git a/bindgen/ir/IR.cpp b/bindgen/ir/IR.cpp index d16dd71..b7e7510 100644 --- a/bindgen/ir/IR.cpp +++ b/bindgen/ir/IR.cpp @@ -230,9 +230,9 @@ void IR::replaceTypeInTypeDefs(std::shared_ptr oldType, template bool IR::isTypeUsed(const std::vector &declarations, - std::shared_ptr type) { - for (const auto decl : declarations) { - if (decl->usesType(type)) { + std::shared_ptr type, bool stopOnTypeDefs) { + for (const auto &decl : declarations) { + if (decl->usesType(type, stopOnTypeDefs)) { return true; } } @@ -240,8 +240,12 @@ bool IR::isTypeUsed(const std::vector &declarations, } bool IR::typeIsUsedOnlyInTypeDefs(std::shared_ptr 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() { diff --git a/bindgen/ir/IR.h b/bindgen/ir/IR.h index ee0a375..1cdd953 100644 --- a/bindgen/ir/IR.h +++ b/bindgen/ir/IR.h @@ -116,7 +116,7 @@ class IR { */ template bool isTypeUsed(const std::vector &declarations, - std::shared_ptr type); + std::shared_ptr type, bool stopOnTypeDefs); void setScalaNames(); diff --git a/bindgen/ir/LiteralDefine.cpp b/bindgen/ir/LiteralDefine.cpp index 3142676..001518c 100644 --- a/bindgen/ir/LiteralDefine.cpp +++ b/bindgen/ir/LiteralDefine.cpp @@ -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, + bool stopOnTypeDefs) const { + return *this->type == *type || + this->type.get()->usesType(type, stopOnTypeDefs); +} diff --git a/bindgen/ir/LiteralDefine.h b/bindgen/ir/LiteralDefine.h index 759ffcc..01a08b1 100644 --- a/bindgen/ir/LiteralDefine.h +++ b/bindgen/ir/LiteralDefine.h @@ -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, bool stopOnTypeDefs) const; + private: std::string literal; std::shared_ptr type; diff --git a/bindgen/ir/Struct.cpp b/bindgen/ir/Struct.cpp index c6e473b..5837b24 100644 --- a/bindgen/ir/Struct.cpp +++ b/bindgen/ir/Struct.cpp @@ -134,9 +134,11 @@ std::string Struct::str() const { return ss.str(); } -bool Struct::usesType(const std::shared_ptr &type) const { +bool Struct::usesType(const std::shared_ptr &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; } } diff --git a/bindgen/ir/Struct.h b/bindgen/ir/Struct.h index f4e7974..990c6f3 100644 --- a/bindgen/ir/Struct.h +++ b/bindgen/ir/Struct.h @@ -54,7 +54,8 @@ class Struct : public StructOrUnion, */ bool hasHelperMethods() const; - bool usesType(const std::shared_ptr &type) const override; + bool usesType(const std::shared_ptr &type, + bool stopOnTypeDefs) const override; std::string str() const override; diff --git a/bindgen/ir/TypeAndName.cpp b/bindgen/ir/TypeAndName.cpp index abe70ba..e3e7eb4 100644 --- a/bindgen/ir/TypeAndName.cpp +++ b/bindgen/ir/TypeAndName.cpp @@ -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, + bool stopOnTypeDefs) const { + return *this->type == *type || + this->type.get()->usesType(type, stopOnTypeDefs); +} diff --git a/bindgen/ir/TypeAndName.h b/bindgen/ir/TypeAndName.h index 667a6d0..1b2b588 100644 --- a/bindgen/ir/TypeAndName.h +++ b/bindgen/ir/TypeAndName.h @@ -23,6 +23,8 @@ class TypeAndName { bool operator!=(const TypeAndName &other) const; + bool usesType(const std::shared_ptr &type, bool stopOnTypeDefs) const; + protected: std::string name; std::shared_ptr type; diff --git a/bindgen/ir/TypeDef.cpp b/bindgen/ir/TypeDef.cpp index 6b11eac..3d37203 100644 --- a/bindgen/ir/TypeDef.cpp +++ b/bindgen/ir/TypeDef.cpp @@ -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) const { - return *this->type == *type; +bool TypeDef::usesType(const std::shared_ptr &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); } diff --git a/bindgen/ir/TypeDef.h b/bindgen/ir/TypeDef.h index 5898683..d3767a2 100644 --- a/bindgen/ir/TypeDef.h +++ b/bindgen/ir/TypeDef.h @@ -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) const override; + bool usesType(const std::shared_ptr &type, + bool stopOnTypeDefs) const override; std::string str() const override; diff --git a/bindgen/ir/types/ArrayType.cpp b/bindgen/ir/types/ArrayType.cpp index e5d14bd..fc729e7 100644 --- a/bindgen/ir/types/ArrayType.cpp +++ b/bindgen/ir/types/ArrayType.cpp @@ -9,8 +9,10 @@ std::string ArrayType::str() const { uint64ToScalaNat(size) + "]"; } -bool ArrayType::usesType(const std::shared_ptr &type) const { - return *elementsType == *type; +bool ArrayType::usesType(const std::shared_ptr &type, + bool stopOnTypeDefs) const { + return *elementsType == *type || + elementsType.get()->usesType(type, stopOnTypeDefs); } bool ArrayType::operator==(const Type &other) const { diff --git a/bindgen/ir/types/ArrayType.h b/bindgen/ir/types/ArrayType.h index 460f0e6..0a1c582 100644 --- a/bindgen/ir/types/ArrayType.h +++ b/bindgen/ir/types/ArrayType.h @@ -9,7 +9,8 @@ class ArrayType : public Type { ~ArrayType() override = default; - bool usesType(const std::shared_ptr &type) const override; + bool usesType(const std::shared_ptr &type, + bool stopOnTypeDefs) const override; std::string str() const override; diff --git a/bindgen/ir/types/FunctionPointerType.cpp b/bindgen/ir/types/FunctionPointerType.cpp index a6ece35..3a528d5 100644 --- a/bindgen/ir/types/FunctionPointerType.cpp +++ b/bindgen/ir/types/FunctionPointerType.cpp @@ -23,13 +23,16 @@ std::string FunctionPointerType::str() const { return ss.str(); } -bool FunctionPointerType::usesType(const std::shared_ptr &type) const { - if (*returnType == *type) { +bool FunctionPointerType::usesType(const std::shared_ptr &type, + bool stopOnTypeDefs) const { + if (*returnType == *type || + returnType.get()->usesType(type, stopOnTypeDefs)) { return true; } for (const auto ¶meterType : parametersTypes) { - if (*parameterType == *type) { + if (*parameterType == *type || + parameterType.get()->usesType(type, stopOnTypeDefs)) { return true; } } diff --git a/bindgen/ir/types/FunctionPointerType.h b/bindgen/ir/types/FunctionPointerType.h index 299d97f..bcfc994 100644 --- a/bindgen/ir/types/FunctionPointerType.h +++ b/bindgen/ir/types/FunctionPointerType.h @@ -13,7 +13,8 @@ class FunctionPointerType : public Type { ~FunctionPointerType() override = default; - bool usesType(const std::shared_ptr &type) const override; + bool usesType(const std::shared_ptr &type, + bool stopOnTypeDefs) const override; std::string str() const override; diff --git a/bindgen/ir/types/PointerType.cpp b/bindgen/ir/types/PointerType.cpp index b307f32..ba03462 100644 --- a/bindgen/ir/types/PointerType.cpp +++ b/bindgen/ir/types/PointerType.cpp @@ -7,8 +7,10 @@ std::string PointerType::str() const { return "native.Ptr[" + type->str() + "]"; } -bool PointerType::usesType(const std::shared_ptr &type) const { - return *this->type == *type; +bool PointerType::usesType(const std::shared_ptr &type, + bool stopOnTypeDefs) const { + return *this->type == *type || + this->type.get()->usesType(type, stopOnTypeDefs); } bool PointerType::operator==(const Type &other) const { diff --git a/bindgen/ir/types/PointerType.h b/bindgen/ir/types/PointerType.h index cd9f7b9..08cfcb8 100644 --- a/bindgen/ir/types/PointerType.h +++ b/bindgen/ir/types/PointerType.h @@ -9,7 +9,8 @@ class PointerType : public Type { ~PointerType() override = default; - bool usesType(const std::shared_ptr &type) const override; + bool usesType(const std::shared_ptr &type, + bool stopOnTypeDefs) const override; std::string str() const override; diff --git a/bindgen/ir/types/PrimitiveType.cpp b/bindgen/ir/types/PrimitiveType.cpp index 151594e..c9f8821 100644 --- a/bindgen/ir/types/PrimitiveType.cpp +++ b/bindgen/ir/types/PrimitiveType.cpp @@ -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) const { +bool PrimitiveType::usesType(const std::shared_ptr &type, + bool stopOnTypeDefs) const { return false; } diff --git a/bindgen/ir/types/PrimitiveType.h b/bindgen/ir/types/PrimitiveType.h index 0e9ddb7..4e5e829 100644 --- a/bindgen/ir/types/PrimitiveType.h +++ b/bindgen/ir/types/PrimitiveType.h @@ -13,7 +13,8 @@ class PrimitiveType : public Type { std::string getType() const; - bool usesType(const std::shared_ptr &type) const override; + bool usesType(const std::shared_ptr &type, + bool stopOnTypeDefs) const override; std::string str() const override; diff --git a/bindgen/ir/types/Type.cpp b/bindgen/ir/types/Type.cpp index b82b676..c2bcaeb 100644 --- a/bindgen/ir/types/Type.cpp +++ b/bindgen/ir/types/Type.cpp @@ -2,7 +2,10 @@ std::string Type::str() const { return ""; } -bool Type::usesType(const std::shared_ptr &type) const { return false; } +bool Type::usesType(const std::shared_ptr &type, + bool stopOnTypeDefs) const { + return false; +} bool Type::operator==(const Type &other) const { return false; } diff --git a/bindgen/ir/types/Type.h b/bindgen/ir/types/Type.h index 1b9fdd3..3c2b964 100644 --- a/bindgen/ir/types/Type.h +++ b/bindgen/ir/types/Type.h @@ -13,7 +13,15 @@ class Type { virtual std::string str() const; - virtual bool usesType(const std::shared_ptr &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, + bool stopOnTypeDefs) const; virtual bool operator==(const Type &other) const;