Skip to content

Commit

Permalink
Check number of headers before running bindgen
Browse files Browse the repository at this point in the history
Cast type to Union/Struct in operator== of Union/Struct class
Fix error message when definition of opaque type was not found
  • Loading branch information
kornilova203 committed Jul 10, 2018
1 parent 5447079 commit 7e45389
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 30 deletions.
7 changes: 6 additions & 1 deletion bindgen/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ int main(int argc, const char *argv[]) {
clang::tooling::ClangTool Tool(op.getCompilations(),
op.getSourcePathList());

if (op.getSourcePathList().size() != 1) {
llvm::errs() << "Error: Only one file may be processed at a time.\n";
llvm::errs().flush();
return -1;
}

auto libName = LibName.getValue();
if (libName.empty()) {
llvm::errs()
Expand All @@ -53,7 +59,6 @@ int main(int argc, const char *argv[]) {
objectName = "nativeLib";
}

assert(op.getSourcePathList().size() == 1);
char *resolved = realpath(op.getSourcePathList()[0].c_str(), nullptr);
LocationManager locationManager(resolved);

Expand Down
3 changes: 0 additions & 3 deletions bindgen/ir/Enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class Enum : public PrimitiveType, public std::enable_shared_from_this<Enum> {
private:
std::string name; // might be empty
std::vector<Enumerator> enumerators;
/**
* nullptr if type is generated.
*/
std::shared_ptr<Location> location;
};

Expand Down
21 changes: 10 additions & 11 deletions bindgen/ir/IR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
<< "object " << objectName << " {\n";

for (const auto &typeDef : ir.typeDefs) {
if (ir.isOutputted(typeDef)) {
if (ir.shouldOutput(typeDef)) {
s << *typeDef;
}
}
Expand Down Expand Up @@ -145,7 +145,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {

std::string sep = "";
for (const auto &e : ir.enums) {
if (ir.isOutputted(e)) {
if (ir.shouldOutput(e)) {
s << sep << *e;
sep = "\n";
}
Expand All @@ -158,13 +158,13 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
s << "object " << ir.libName << "Helpers {\n";

for (const auto &st : ir.structs) {
if (ir.isOutputted(st) && st->hasHelperMethods()) {
if (ir.shouldOutput(st) && st->hasHelperMethods()) {
s << "\n" << st->generateHelperClass();
}
}

for (const auto &u : ir.unions) {
if (ir.isOutputted(u)) {
if (ir.shouldOutput(u)) {
s << "\n" << u->generateHelperClass();
}
}
Expand All @@ -179,7 +179,6 @@ void IR::generate(const std::string &excludePrefix) {
if (!generated) {
setScalaNames();
filterDeclarations(excludePrefix);
// removeUnusedExternalTypedefs();
generated = true;
}
}
Expand All @@ -191,7 +190,7 @@ bool IR::hasHelperMethods() const {
}

for (const auto &s : structs) {
if (isOutputted(s) && s->hasHelperMethods()) {
if (shouldOutput(s) && s->hasHelperMethods()) {
return true;
}
}
Expand Down Expand Up @@ -269,7 +268,7 @@ bool IR::isTypeUsed(const std::shared_ptr<Type> &type,
* references this type */
for (const auto &typeDef : typeDefs) {
if (typeDef->usesType(type, false)) {
if (isOutputted(typeDef)) {
if (shouldOutput(typeDef)) {
return true;
}
}
Expand All @@ -278,7 +277,7 @@ bool IR::isTypeUsed(const std::shared_ptr<Type> &type,
/* stopOnTypeDefs parameter is true because because typedefs were
* checked */
if (s->usesType(type, true)) {
if (isOutputted(s)) {
if (shouldOutput(s)) {
return true;
}
}
Expand All @@ -287,7 +286,7 @@ bool IR::isTypeUsed(const std::shared_ptr<Type> &type,
/* stopOnTypeDefs parameter is true because because typedefs were
* checked */
if (u->usesType(type, true)) {
if (isOutputted(u)) {
if (shouldOutput(u)) {
return true;
}
}
Expand Down Expand Up @@ -429,14 +428,14 @@ template <typename T>
bool IR::hasOutputtedDeclaration(
const std::vector<std::shared_ptr<T>> &declarations) const {
for (const auto &declaration : declarations) {
if (isOutputted(declaration)) {
if (shouldOutput(declaration)) {
return true;
}
}
return false;
}

template <typename T>
bool IR::isOutputted(const std::shared_ptr<T> &type) const {
bool IR::shouldOutput(const std::shared_ptr<T> &type) const {
return inMainFile(*type) || isTypeUsed(type, true);
}
2 changes: 1 addition & 1 deletion bindgen/ir/IR.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class IR {
* main file.
*/
template <typename T>
bool isOutputted(const std::shared_ptr<T> &type) const;
bool shouldOutput(const std::shared_ptr<T> &type) const;

/**
* @tparam T Struct or Union
Expand Down
12 changes: 6 additions & 6 deletions bindgen/ir/Struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ bool Struct::usesType(const std::shared_ptr<Type> &type,
}

bool Struct::operator==(const Type &other) const {
auto *structOrUnion = dynamic_cast<const StructOrUnion *>(&other);
if (structOrUnion) {
return this->equals(*structOrUnion);
auto *s = dynamic_cast<const Struct *>(&other);
if (s) {
return this->equals(*s);
}
return false;
}
Expand Down Expand Up @@ -198,9 +198,9 @@ std::string Union::generateHelperClass() const {
std::string Union::getTypeAlias() const { return "union_" + name; }

bool Union::operator==(const Type &other) const {
auto *structOrUnion = dynamic_cast<const StructOrUnion *>(&other);
if (structOrUnion) {
return this->equals(*structOrUnion);
auto *u = dynamic_cast<const Union *>(&other);
if (u) {
return this->equals(*u);
}
return false;
}
3 changes: 0 additions & 3 deletions bindgen/ir/Struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ class StructOrUnion {
protected:
std::string name;
std::vector<Field *> fields;
/**
* nullptr if type is generated.
*/
std::shared_ptr<Location> location;
};

Expand Down
8 changes: 5 additions & 3 deletions bindgen/ir/TypeDef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ TypeDef::TypeDef(std::string name, std::shared_ptr<Type> type,

llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const TypeDef &typeDef) {
if (!typeDef.getType()) {
llvm::errs() << "Error: type declaration for " << typeDef.getName()
llvm::errs() << "Error: type definition for " << typeDef.getName()
<< " was not found.\n";
llvm::errs().flush();
return s;
Expand All @@ -25,8 +25,10 @@ bool TypeDef::usesType(const std::shared_ptr<Type> &type,
if (stopOnTypeDefs) {
return false;
}
return *this->type == *type ||
this->type.get()->usesType(type, stopOnTypeDefs);
if (!this->type) {
return false;
}
return *this->type == *type || this->type->usesType(type, stopOnTypeDefs);
}

std::string TypeDef::str() const { return handleReservedWords(name); }
Expand Down
4 changes: 2 additions & 2 deletions tests/samples/include/included.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum unusedEnum { UNUSED }; // removed
typedef enum unusedEnum unusedEnum; // removed

struct unusedStruct {
enum unusedEnum e; // removed
};
enum unusedEnum e;
}; // removed

enum semester { AUTUMN, SPRING };

0 comments on commit 7e45389

Please sign in to comment.