Skip to content

Commit 7e45389

Browse files
committed
Check number of headers before running bindgen
Cast type to Union/Struct in operator== of Union/Struct class Fix error message when definition of opaque type was not found
1 parent 5447079 commit 7e45389

File tree

8 files changed

+30
-30
lines changed

8 files changed

+30
-30
lines changed

bindgen/Main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ int main(int argc, const char *argv[]) {
3030
clang::tooling::ClangTool Tool(op.getCompilations(),
3131
op.getSourcePathList());
3232

33+
if (op.getSourcePathList().size() != 1) {
34+
llvm::errs() << "Error: Only one file may be processed at a time.\n";
35+
llvm::errs().flush();
36+
return -1;
37+
}
38+
3339
auto libName = LibName.getValue();
3440
if (libName.empty()) {
3541
llvm::errs()
@@ -53,7 +59,6 @@ int main(int argc, const char *argv[]) {
5359
objectName = "nativeLib";
5460
}
5561

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

bindgen/ir/Enum.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ class Enum : public PrimitiveType, public std::enable_shared_from_this<Enum> {
3939
private:
4040
std::string name; // might be empty
4141
std::vector<Enumerator> enumerators;
42-
/**
43-
* nullptr if type is generated.
44-
*/
4542
std::shared_ptr<Location> location;
4643
};
4744

bindgen/ir/IR.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
108108
<< "object " << objectName << " {\n";
109109

110110
for (const auto &typeDef : ir.typeDefs) {
111-
if (ir.isOutputted(typeDef)) {
111+
if (ir.shouldOutput(typeDef)) {
112112
s << *typeDef;
113113
}
114114
}
@@ -145,7 +145,7 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
145145

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

160160
for (const auto &st : ir.structs) {
161-
if (ir.isOutputted(st) && st->hasHelperMethods()) {
161+
if (ir.shouldOutput(st) && st->hasHelperMethods()) {
162162
s << "\n" << st->generateHelperClass();
163163
}
164164
}
165165

166166
for (const auto &u : ir.unions) {
167-
if (ir.isOutputted(u)) {
167+
if (ir.shouldOutput(u)) {
168168
s << "\n" << u->generateHelperClass();
169169
}
170170
}
@@ -179,7 +179,6 @@ void IR::generate(const std::string &excludePrefix) {
179179
if (!generated) {
180180
setScalaNames();
181181
filterDeclarations(excludePrefix);
182-
// removeUnusedExternalTypedefs();
183182
generated = true;
184183
}
185184
}
@@ -191,7 +190,7 @@ bool IR::hasHelperMethods() const {
191190
}
192191

193192
for (const auto &s : structs) {
194-
if (isOutputted(s) && s->hasHelperMethods()) {
193+
if (shouldOutput(s) && s->hasHelperMethods()) {
195194
return true;
196195
}
197196
}
@@ -269,7 +268,7 @@ bool IR::isTypeUsed(const std::shared_ptr<Type> &type,
269268
* references this type */
270269
for (const auto &typeDef : typeDefs) {
271270
if (typeDef->usesType(type, false)) {
272-
if (isOutputted(typeDef)) {
271+
if (shouldOutput(typeDef)) {
273272
return true;
274273
}
275274
}
@@ -278,7 +277,7 @@ bool IR::isTypeUsed(const std::shared_ptr<Type> &type,
278277
/* stopOnTypeDefs parameter is true because because typedefs were
279278
* checked */
280279
if (s->usesType(type, true)) {
281-
if (isOutputted(s)) {
280+
if (shouldOutput(s)) {
282281
return true;
283282
}
284283
}
@@ -287,7 +286,7 @@ bool IR::isTypeUsed(const std::shared_ptr<Type> &type,
287286
/* stopOnTypeDefs parameter is true because because typedefs were
288287
* checked */
289288
if (u->usesType(type, true)) {
290-
if (isOutputted(u)) {
289+
if (shouldOutput(u)) {
291290
return true;
292291
}
293292
}
@@ -429,14 +428,14 @@ template <typename T>
429428
bool IR::hasOutputtedDeclaration(
430429
const std::vector<std::shared_ptr<T>> &declarations) const {
431430
for (const auto &declaration : declarations) {
432-
if (isOutputted(declaration)) {
431+
if (shouldOutput(declaration)) {
433432
return true;
434433
}
435434
}
436435
return false;
437436
}
438437

439438
template <typename T>
440-
bool IR::isOutputted(const std::shared_ptr<T> &type) const {
439+
bool IR::shouldOutput(const std::shared_ptr<T> &type) const {
441440
return inMainFile(*type) || isTypeUsed(type, true);
442441
}

bindgen/ir/IR.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class IR {
152152
* main file.
153153
*/
154154
template <typename T>
155-
bool isOutputted(const std::shared_ptr<T> &type) const;
155+
bool shouldOutput(const std::shared_ptr<T> &type) const;
156156

157157
/**
158158
* @tparam T Struct or Union

bindgen/ir/Struct.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ bool Struct::usesType(const std::shared_ptr<Type> &type,
156156
}
157157

158158
bool Struct::operator==(const Type &other) const {
159-
auto *structOrUnion = dynamic_cast<const StructOrUnion *>(&other);
160-
if (structOrUnion) {
161-
return this->equals(*structOrUnion);
159+
auto *s = dynamic_cast<const Struct *>(&other);
160+
if (s) {
161+
return this->equals(*s);
162162
}
163163
return false;
164164
}
@@ -198,9 +198,9 @@ std::string Union::generateHelperClass() const {
198198
std::string Union::getTypeAlias() const { return "union_" + name; }
199199

200200
bool Union::operator==(const Type &other) const {
201-
auto *structOrUnion = dynamic_cast<const StructOrUnion *>(&other);
202-
if (structOrUnion) {
203-
return this->equals(*structOrUnion);
201+
auto *u = dynamic_cast<const Union *>(&other);
202+
if (u) {
203+
return this->equals(*u);
204204
}
205205
return false;
206206
}

bindgen/ir/Struct.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ class StructOrUnion {
4040
protected:
4141
std::string name;
4242
std::vector<Field *> fields;
43-
/**
44-
* nullptr if type is generated.
45-
*/
4643
std::shared_ptr<Location> location;
4744
};
4845

bindgen/ir/TypeDef.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ TypeDef::TypeDef(std::string name, std::shared_ptr<Type> type,
1010

1111
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const TypeDef &typeDef) {
1212
if (!typeDef.getType()) {
13-
llvm::errs() << "Error: type declaration for " << typeDef.getName()
13+
llvm::errs() << "Error: type definition for " << typeDef.getName()
1414
<< " was not found.\n";
1515
llvm::errs().flush();
1616
return s;
@@ -25,8 +25,10 @@ bool TypeDef::usesType(const std::shared_ptr<Type> &type,
2525
if (stopOnTypeDefs) {
2626
return false;
2727
}
28-
return *this->type == *type ||
29-
this->type.get()->usesType(type, stopOnTypeDefs);
28+
if (!this->type) {
29+
return false;
30+
}
31+
return *this->type == *type || this->type->usesType(type, stopOnTypeDefs);
3032
}
3133

3234
std::string TypeDef::str() const { return handleReservedWords(name); }

tests/samples/include/included.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum unusedEnum { UNUSED }; // removed
3131
typedef enum unusedEnum unusedEnum; // removed
3232

3333
struct unusedStruct {
34-
enum unusedEnum e; // removed
35-
};
34+
enum unusedEnum e;
35+
}; // removed
3636

3737
enum semester { AUTUMN, SPRING };

0 commit comments

Comments
 (0)