Skip to content

Commit 7c61f98

Browse files
authored
Merge pull request #113 from kornilova-l/only-declarations-from-original-header
Filter functions, extern vars, defines and unused types from included headers
2 parents 87b7d3c + 7e45389 commit 7c61f98

29 files changed

+482
-187
lines changed

bindgen/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ add_executable(bindgen
3636
defines/DefineFinderActionFactory.h
3737
TypeTranslator.h
3838
TypeTranslator.cpp
39-
HeaderManager.h
40-
HeaderManager.cpp
4139
CycleDetection.h
4240
Utils.h
4341
ir/IR.h
@@ -72,6 +70,10 @@ add_executable(bindgen
7270
ir/types/FunctionPointerType.h
7371
ir/types/ArrayType.cpp
7472
ir/types/ArrayType.h
73+
ir/Location.h
74+
ir/Location.cpp
75+
ir/LocationManager.h
76+
ir/LocationManager.cpp
7577
)
7678

7779
if (STATIC_LINKING)

bindgen/HeaderManager.cpp

Lines changed: 0 additions & 32 deletions
This file was deleted.

bindgen/HeaderManager.h

Lines changed: 0 additions & 15 deletions
This file was deleted.

bindgen/Main.cpp

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "defines/DefineFinderActionFactory.h"
2+
#include "ir/LocationManager.h"
23
#include "visitor/ScalaFrontendActionFactory.h"
34
#include <clang/Tooling/CommonOptionsParser.h>
45

@@ -11,19 +12,7 @@ int main(int argc, const char *argv[]) {
1112

1213
llvm::cl::opt<std::string> LibName("name", llvm::cl::cat(Category),
1314
llvm::cl::desc("Library name"));
14-
llvm::cl::opt<std::string> StdHeaders(
15-
"std-headers", llvm::cl::cat(Category),
16-
llvm::cl::desc("Path to a file with the list of headers for which "
17-
"bindings\n"
18-
"will not be generated. "
19-
"The list contains header files names\n"
20-
"and package names that contain bindings for these "
21-
"headers.\nExample:\n"
22-
"math.h=scala.scalanative.native.math\n"
23-
"stdlib.h=scala.scalanative.native.stdlib"));
24-
llvm::cl::opt<bool> PrintHeadersLocation(
25-
"location", llvm::cl::cat(Category),
26-
llvm::cl::desc("Print list of parsed headers"));
15+
2716
llvm::cl::opt<std::string> ExcludePrefix(
2817
"exclude-prefix", llvm::cl::cat(Category),
2918
llvm::cl::desc("Functions and unused typedefs will be removed if their "
@@ -41,6 +30,12 @@ int main(int argc, const char *argv[]) {
4130
clang::tooling::ClangTool Tool(op.getCompilations(),
4231
op.getSourcePathList());
4332

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+
4439
auto libName = LibName.getValue();
4540
if (libName.empty()) {
4641
llvm::errs()
@@ -64,14 +59,10 @@ int main(int argc, const char *argv[]) {
6459
objectName = "nativeLib";
6560
}
6661

67-
auto stdhead = StdHeaders.getValue();
68-
if (!stdhead.empty()) {
69-
headerMan.LoadConfig(stdhead);
70-
}
71-
72-
locations.clear();
62+
char *resolved = realpath(op.getSourcePathList()[0].c_str(), nullptr);
63+
LocationManager locationManager(resolved);
7364

74-
IR ir(libName, linkName, objectName, Package.getValue());
65+
IR ir(libName, linkName, objectName, Package.getValue(), locationManager);
7566

7667
DefineFinderActionFactory defineFinderActionFactory(ir);
7768
int result = Tool.run(&defineFinderActionFactory);
@@ -82,15 +73,8 @@ int main(int argc, const char *argv[]) {
8273
ScalaFrontendActionFactory actionFactory(ir);
8374
result = Tool.run(&actionFactory);
8475

85-
auto printLoc = PrintHeadersLocation.getValue();
86-
if (printLoc) {
87-
for (const auto &location : locations) {
88-
llvm::outs() << location.c_str();
89-
}
90-
} else {
91-
ir.generate(ExcludePrefix.getValue());
92-
llvm::outs() << ir;
93-
}
76+
ir.generate(ExcludePrefix.getValue());
77+
llvm::outs() << ir;
9478
llvm::outs().flush();
9579
return result;
9680
}

bindgen/TypeTranslator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ TypeTranslator::translateStructOrUnionOrEnum(const clang::QualType &qtpe) {
102102
/* type is not yet defined.
103103
* TypeDef with nullptr will be created.
104104
* nullptr will be replaced by actual type when the type is declared. */
105-
typeDef = ir.addTypeDef(nameWithoutSpace, nullptr);
105+
typeDef = ir.addTypeDef(nameWithoutSpace, nullptr, nullptr);
106106
return typeDef;
107107
}
108108

bindgen/defines/DefineFinder.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ DefineFinder::DefineFinder(IR &ir, const clang::CompilerInstance &compiler,
1111
void DefineFinder::MacroDefined(const clang::Token &macroNameTok,
1212
const clang::MacroDirective *md) {
1313
clang::SourceManager &sm = compiler.getSourceManager();
14+
if (!sm.isInMainFile(md->getLocation())) {
15+
/* include defines only from the original header */
16+
return;
17+
}
1418
if (sm.isWrittenInMainFile(macroNameTok.getLocation()) && md->isDefined() &&
1519
!md->getMacroInfo()->isFunctionLike()) {
1620
/* save defines only from the given header.
@@ -100,6 +104,9 @@ void DefineFinder::MacroUndefined(const clang::Token &macroNameTok,
100104
const clang::MacroDefinition &md,
101105
const clang::MacroDirective *undef) {
102106
clang::SourceManager &sm = compiler.getSourceManager();
107+
if (!sm.isInMainFile(undef->getLocation())) {
108+
return;
109+
}
103110
if (sm.isWrittenInMainFile(macroNameTok.getLocation()) &&
104111
md.getMacroInfo() && !md.getMacroInfo()->isFunctionLike()) {
105112
std::string macroName = macroNameTok.getIdentifierInfo()->getName();

bindgen/ir/Enum.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ std::string Enumerator::getName() { return name; }
88
int64_t Enumerator::getValue() { return value; }
99

1010
Enum::Enum(std::string name, std::string type,
11-
std::vector<Enumerator> enumerators)
11+
std::vector<Enumerator> enumerators,
12+
std::shared_ptr<Location> location)
1213
: PrimitiveType(std::move(type)), name(std::move(name)),
13-
enumerators(std::move(enumerators)) {}
14+
enumerators(std::move(enumerators)), location(std::move(location)) {}
1415

1516
bool Enum::isAnonymous() const { return name.empty(); }
1617

1718
std::shared_ptr<TypeDef> Enum::generateTypeDef() {
18-
assert(!isAnonymous());
19-
return std::make_shared<TypeDef>("enum_" + name, shared_from_this());
19+
return std::make_shared<TypeDef>(getTypeAlias(), shared_from_this(),
20+
nullptr);
2021
}
2122

2223
llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) {
@@ -49,3 +50,10 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e) {
4950
}
5051

5152
std::string Enum::getName() const { return name; }
53+
54+
std::string Enum::getTypeAlias() const {
55+
assert(!isAnonymous());
56+
return "enum_" + name;
57+
}
58+
59+
std::shared_ptr<Location> Enum::getLocation() const { return location; }

bindgen/ir/Enum.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class Enumerator {
2121
class Enum : public PrimitiveType, public std::enable_shared_from_this<Enum> {
2222
public:
2323
Enum(std::string name, std::string type,
24-
std::vector<Enumerator> enumerators);
24+
std::vector<Enumerator> enumerators,
25+
std::shared_ptr<Location> location);
2526

2627
bool isAnonymous() const;
2728

@@ -31,9 +32,14 @@ class Enum : public PrimitiveType, public std::enable_shared_from_this<Enum> {
3132

3233
friend llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const Enum &e);
3334

35+
std::string getTypeAlias() const;
36+
37+
std::shared_ptr<Location> getLocation() const;
38+
3439
private:
3540
std::string name; // might be empty
3641
std::vector<Enumerator> enumerators;
42+
std::shared_ptr<Location> location;
3743
};
3844

3945
#endif // SCALA_NATIVE_BINDGEN_ENUM_H

0 commit comments

Comments
 (0)