-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filter functions, extern vars, defines and unused types from included headers #113
Changes from 3 commits
2f9f9bf
919c4be
a0a616b
331dabe
bce760d
902fa75
5447079
7e45389
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,10 @@ DefineFinder::DefineFinder(IR &ir, const clang::CompilerInstance &compiler, | |
void DefineFinder::MacroDefined(const clang::Token ¯oNameTok, | ||
const clang::MacroDirective *md) { | ||
clang::SourceManager &sm = compiler.getSourceManager(); | ||
if (!sm.isInMainFile(md->getLocation())) { | ||
/* include defines only from the original header */ | ||
return; | ||
} | ||
if (sm.isWrittenInMainFile(macroNameTok.getLocation()) && md->isDefined() && | ||
!md->getMacroInfo()->isFunctionLike()) { | ||
/* save defines only from the given header. | ||
|
@@ -100,6 +104,9 @@ void DefineFinder::MacroUndefined(const clang::Token ¯oNameTok, | |
const clang::MacroDefinition &md, | ||
const clang::MacroDirective *undef) { | ||
clang::SourceManager &sm = compiler.getSourceManager(); | ||
if (!sm.isInMainFile(undef->getLocation())) { | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a test for this would be very valuable. |
||
if (sm.isWrittenInMainFile(macroNameTok.getLocation()) && | ||
md.getMacroInfo() && !md.getMacroInfo()->isFunctionLike()) { | ||
std::string macroName = macroNameTok.getIdentifierInfo()->getName(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,11 @@ Enum::Enum(std::string name, std::string type, | |
|
||
bool Enum::isAnonymous() const { return name.empty(); } | ||
|
||
std::shared_ptr<TypeDef> Enum::generateTypeDef() { | ||
std::shared_ptr<TypeDef> | ||
Enum::generateTypeDef(std::shared_ptr<Location> location) { | ||
assert(!isAnonymous()); | ||
return std::make_shared<TypeDef>("enum_" + name, shared_from_this()); | ||
return std::make_shared<TypeDef>("enum_" + name, shared_from_this(), | ||
std::move(location)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand why the Also, from
this is generated, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, docstring is wrong, |
||
} | ||
|
||
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#include "IR.h" | ||
#include "../Utils.h" | ||
#include "location/SourceLocation.h" | ||
|
||
IR::IR(std::string libName, std::string linkName, std::string objectName, | ||
std::string packageName) | ||
|
@@ -13,48 +14,53 @@ void IR::addFunction(std::string name, std::vector<Parameter *> parameters, | |
} | ||
|
||
std::shared_ptr<TypeDef> IR::addTypeDef(std::string name, | ||
std::shared_ptr<Type> type) { | ||
typeDefs.push_back(std::make_shared<TypeDef>(std::move(name), type)); | ||
std::shared_ptr<Type> type, | ||
std::shared_ptr<Location> location) { | ||
typeDefs.push_back( | ||
std::make_shared<TypeDef>(std::move(name), type, std::move(location))); | ||
return typeDefs.back(); | ||
} | ||
|
||
std::shared_ptr<Type> IR::addEnum(std::string name, const std::string &type, | ||
std::vector<Enumerator> enumerators) { | ||
std::vector<Enumerator> enumerators, | ||
std::shared_ptr<Location> location) { | ||
std::shared_ptr<Enum> e = | ||
std::make_shared<Enum>(std::move(name), type, std::move(enumerators)); | ||
enums.push_back(e); | ||
if (!e->isAnonymous()) { | ||
typeDefs.push_back(e->generateTypeDef()); | ||
typeDefs.push_back(e->generateTypeDef(std::move(location))); | ||
return typeDefs.back(); | ||
} | ||
return nullptr; | ||
} | ||
|
||
void IR::addStruct(std::string name, std::vector<Field *> fields, | ||
uint64_t typeSize) { | ||
uint64_t typeSize, std::shared_ptr<Location> location) { | ||
std::shared_ptr<Struct> s = | ||
std::make_shared<Struct>(name, std::move(fields), typeSize); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, I'm curious why the location is associated with the generated typedef instead of the "concrete" type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It is possible to add So when generator checks for unused external types it can go through all typedefs and check their I am not sure if it was right choice, I can try to add |
||
structs.push_back(s); | ||
std::shared_ptr<TypeDef> typeDef = getTypeDefWithName("struct_" + name); | ||
if (typeDef) { | ||
/* the struct type used to be opaque type, typeDef contains nullptr */ | ||
typeDef.get()->setType(s); | ||
typeDef.get()->setLocation(location); | ||
} else { | ||
typeDefs.push_back(s->generateTypeDef()); | ||
typeDefs.push_back(s->generateTypeDef(std::move(location))); | ||
} | ||
} | ||
|
||
void IR::addUnion(std::string name, std::vector<Field *> fields, | ||
uint64_t maxSize) { | ||
uint64_t maxSize, std::shared_ptr<Location> location) { | ||
std::shared_ptr<Union> u = | ||
std::make_shared<Union>(name, std::move(fields), maxSize); | ||
unions.push_back(u); | ||
std::shared_ptr<TypeDef> typeDef = getTypeDefWithName("union_" + name); | ||
if (typeDef) { | ||
/* the union type used to be opaque type, typeDef contains nullptr */ | ||
typeDef.get()->setType(u); | ||
typeDef.get()->setLocation(location); | ||
} else { | ||
typeDefs.push_back(u->generateTypeDef()); | ||
typeDefs.push_back(u->generateTypeDef(std::move(location))); | ||
} | ||
} | ||
|
||
|
@@ -171,6 +177,7 @@ void IR::generate(const std::string &excludePrefix) { | |
if (!generated) { | ||
setScalaNames(); | ||
filterDeclarations(excludePrefix); | ||
removeUnusedExternalTypes(); | ||
generated = true; | ||
} | ||
} | ||
|
@@ -230,7 +237,7 @@ void IR::replaceTypeInTypeDefs(std::shared_ptr<Type> oldType, | |
|
||
template <typename T> | ||
bool IR::isTypeUsed(const std::vector<T> &declarations, | ||
std::shared_ptr<Type> type, bool stopOnTypeDefs) { | ||
std::shared_ptr<Type> type, bool stopOnTypeDefs) const { | ||
for (const auto &decl : declarations) { | ||
if (decl->usesType(type, stopOnTypeDefs)) { | ||
return true; | ||
|
@@ -239,7 +246,7 @@ bool IR::isTypeUsed(const std::vector<T> &declarations, | |
return false; | ||
} | ||
|
||
bool IR::typeIsUsedOnlyInTypeDefs(std::shared_ptr<Type> type) { | ||
bool IR::typeIsUsedOnlyInTypeDefs(const std::shared_ptr<Type> &type) const { | ||
/* varDefines are not checked here because they are simply | ||
* aliases for variables.*/ | ||
return !( | ||
|
@@ -248,6 +255,11 @@ bool IR::typeIsUsedOnlyInTypeDefs(std::shared_ptr<Type> type) { | |
isTypeUsed(literalDefines, type, true)); | ||
} | ||
|
||
bool IR::isTypeUsed(const std::shared_ptr<Type> &type) const { | ||
return !(typeIsUsedOnlyInTypeDefs(type) && | ||
!isTypeUsed(typeDefs, type, false)); | ||
} | ||
|
||
void IR::setScalaNames() { | ||
/* Renaming according to Scala naming conventions | ||
* should happen here */ | ||
|
@@ -353,3 +365,47 @@ IR::~IR() { | |
variables.clear(); | ||
varDefines.clear(); | ||
} | ||
|
||
void IR::removeUnusedExternalTypes() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it makes sense to remove types, since we loose a lot of information. We need the external types to correctly reference types from other modules, like |
||
for (auto it = typeDefs.begin(); it != typeDefs.end();) { | ||
std::shared_ptr<TypeDef> typeDef = *it; | ||
std::shared_ptr<Location> location = typeDef->getLocation(); | ||
auto *sourceLocation = dynamic_cast<SourceLocation *>(location.get()); | ||
if (sourceLocation && !sourceLocation->isMainFile()) { | ||
if (!isTypeUsed(typeDef)) { | ||
removeStructOrUnionOrEnum(typeDef->getType()); | ||
it = typeDefs.erase(it); | ||
} else { | ||
++it; | ||
} | ||
} else { | ||
++it; | ||
} | ||
} | ||
} | ||
|
||
template <typename T> | ||
void IR::removeDeclaration(std::vector<std::shared_ptr<T>> &declarations, | ||
T *declaration) { | ||
for (auto it = declarations.begin(); it != declarations.end();) { | ||
T *decl = (*it).get(); | ||
if (decl == declaration) { | ||
it = declarations.erase(it); | ||
} else { | ||
++it; | ||
} | ||
} | ||
} | ||
|
||
void IR::removeStructOrUnionOrEnum(std::shared_ptr<Type> type) { | ||
if (isInstanceOf<Struct>(type.get())) { | ||
auto *s = dynamic_cast<Struct *>(type.get()); | ||
removeDeclaration(structs, s); | ||
} else if (isInstanceOf<Union>(type.get())) { | ||
auto *u = dynamic_cast<Union *>(type.get()); | ||
removeDeclaration(unions, u); | ||
} else if (isInstanceOf<Enum>(type.get())) { | ||
auto *e = dynamic_cast<Enum *>(type.get()); | ||
removeDeclaration(enums, e); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually passing a location here would allow us to provide a better diagnostic if an opaque type is used in a problematic way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a good idea but we should not use
location
field for this.Maybe create an optional field
typeDeclarationLocation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I started doing it but it required passing
Location
instance to methods ofTypeTranslator
and it does not look good to me, so I decided to leave it for now.I hope that after fixing #106 we will not see the error message often (except the case when one wants to generated bindings for broken header).