Skip to content

Commit 851172f

Browse files
committed
Switch to the new system to handle standard headers
1 parent a8467bd commit 851172f

File tree

8 files changed

+178
-78
lines changed

8 files changed

+178
-78
lines changed

HeaderManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void HeaderManager::LoadConfig(const std::string& path){
2323
}
2424

2525
bool HeaderManager::IsStandard(const std::string& path){
26-
return false;
26+
return !!headers.count(path);
2727
}
2828

2929

Main.cpp

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ static llvm::cl::extrahelp CommonHelp(clang::tooling::CommonOptionsParser::HelpM
99
static llvm::cl::extrahelp MoreHelp("\nProduce Bindings for scala native. Please specify lib name wit parameter name\n");
1010
static llvm::cl::opt<std::string> LibName("name", llvm::cl::cat(Category));
1111
static llvm::cl::opt<std::string> StdHeaders("stdHeaders", llvm::cl::cat(Category));
12+
static llvm::cl::opt<bool> PrintHeadersLocation ("location", llvm::cl::cat(Category));
1213

1314

1415
int main(int argc, char *argv[]) {
@@ -37,33 +38,43 @@ int main(int argc, char *argv[]) {
3738
declarations = "";
3839
enums = "";
3940
helpers = "";
41+
locations.clear();
4042

4143
int result = Tool.run(clang::tooling::newFrontendActionFactory<ScalaFrontendAction>().get());
4244

43-
llvm::outs() << "import scala.scalanative._\n"
44-
<< "import scala.scalanative.native.Nat._\n\n";
45-
46-
if(declarations != ""){
47-
llvm::outs() << "@native.link(\"" << lib << "\")\n"
48-
<< "@native.extern\n"
49-
<< "object " << lib << " {\n"
50-
<< declarations
51-
<< "}\n\n"
52-
<< "import " + lib + "._\n\n";
53-
}
54-
55-
if(enums != ""){
56-
llvm::outs() << "object " << lib << "Enums {\n"
57-
<< enums
58-
<< "}\n\n";
45+
auto printLoc = PrintHeadersLocation.getValue();
46+
47+
if(printLoc){
48+
for(const auto& location: locations){
49+
llvm::outs() << location;
50+
}
51+
52+
} else {
53+
if(declarations != "" || enums != "")
54+
llvm::outs() << "import scala.scalanative._\n"
55+
<< "import scala.scalanative.native.Nat._\n\n";
56+
57+
if(declarations != ""){
58+
llvm::outs() << "@native.link(\"" << lib << "\")\n"
59+
<< "@native.extern\n"
60+
<< "object " << lib << " {\n"
61+
<< declarations
62+
<< "}\n\n"
63+
<< "import " + lib + "._\n\n";
64+
}
65+
66+
if(enums != ""){
67+
llvm::outs() << "object " << lib << "Enums {\n"
68+
<< enums
69+
<< "}\n\n";
70+
}
71+
72+
if(helpers != ""){
73+
llvm::outs() << "object " << lib << "Helpers {\n"
74+
<< helpers
75+
<< "}\n\n";
76+
}
5977
}
60-
61-
if(helpers != ""){
62-
llvm::outs() << "object " << lib << "Helpers {\n"
63-
<< helpers
64-
<< "}\n\n";
65-
}
66-
6778
return result;
6879
}
6980
}

SimpleTypeTests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ std::string Translate(std::string code){
99
declarations = "";
1010
enums = "";
1111
helpers = "";
12+
locations.clear();
1213
auto* action = new ScalaFrontendAction;
1314
clang::tooling::runToolOnCode(action, code, "input.h");
1415
return declarations;

TreeConsumer.h

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,22 @@ class TreeConsumer : public clang::ASTConsumer {
1717

1818
public:
1919

20-
std::vector<std::string> stdheaders{"assert.h","complex.h","ctype.h","errno.h","fenv.h","float.h","inttypes.h","iso646.h","limits.h",
21-
"locale.h","math.h","setjmp.h","signal.h","stdalign.h","stdarg.h","stdatomic.h","stdbool.h","stddef.h",
22-
"stdint.h","stdio.h","stdlib.h","stdnoreturn.h","string.h","tgmath.h","threads.h","time.h","uchar.h",
23-
"wchar.h","wctype.h","aio.h","inet.h","assert.h","complex.h","cpio.h","ctype.h","dirent.h","dlfcn.h",
24-
"errno.h","fcntl.h","fenv.h","float.h","fmtmsg.h","fnmatch.h","ftw.h","glob.h","grp.h","iconv.h",
25-
"inttypes.h","iso646.h","langinfo.h","libgen.h","limits.h","locale.h","math.h","monetary.h","mqueue.h",
26-
"ndbm.h","if.h","netdb.h","in.h","tcp.h","nl_types.h","poll.h","pthread.h","pwd.h",
27-
"regex.h","sched.h","search.h","semaphore.h","setjmp.h","signal.h","spawn.h","stdarg.h","stdbool.h",
28-
"stddef.h","stdint.h","stdio.h","stdlib.h","string.h","strings.h","stropts.h","ipc.h","mman.h",
29-
"msg.h","resource.h","select.h","sem.h","shm.h","socket.h","stat.h",
30-
"statvfs.h","time.h","times.h","types.h","uio.h","un.h","utsname.h",
31-
"wait.h","syslog.h","tar.h","termios.h","tgmath.h","time.h","trace.h","ulimit.h","unistd.h",
32-
"utime.h","utmpx.h","wchar.h","wctype.h","wordexp.h",
33-
34-
"siginfo.h", "sigset.h", "sigaction.h", "sigcontext.h", "sigthread.h", "ucontext.h",
35-
"pthreadtypes.h", "libio.h", "__stddef_max_align_t.h", "_G_config.h", "sys_errlist.h",
36-
"sysmacros.h", "xlocale.h", "socket_type.h", "sockaddr.h", "sigstack.h"
37-
};
38-
39-
/* with folders
40-
std::vector<std::string> stdheaders{"assert.h","complex.h","ctype.h","errno.h","fenv.h","float.h","inttypes.h","iso646.h","limits.h",
41-
"locale.h","math.h","setjmp.h","signal.h","stdalign.h","stdarg.h","stdatomic.h","stdbool.h","stddef.h",
42-
"stdint.h","stdio.h","stdlib.h","stdnoreturn.h","string.h","tgmath.h","threads.h","time.h","uchar.h",
43-
"wchar.h","wctype.h","aio.h","arpa/inet.h","assert.h","complex.h","cpio.h","ctype.h","dirent.h","dlfcn.h",
44-
"errno.h","fcntl.h","fenv.h","float.h","fmtmsg.h","fnmatch.h","ftw.h","glob.h","grp.h","iconv.h",
45-
"inttypes.h","iso646.h","langinfo.h","libgen.h","limits.h","locale.h","math.h","monetary.h","mqueue.h",
46-
"ndbm.h","net/if.h","netdb.h","netinet/in.h","netinet/tcp.h","nl_types.h","poll.h","pthread.h","pwd.h",
47-
"regex.h","sched.h","search.h","semaphore.h","setjmp.h","signal.h","spawn.h","stdarg.h","stdbool.h",
48-
"stddef.h","stdint.h","stdio.h","stdlib.h","string.h","strings.h","stropts.h","sys/ipc.h","sys/mman.h",
49-
"sys/msg.h","sys/resource.h","sys/select.h","sys/sem.h","sys/shm.h","sys/socket.h","sys/stat.h",
50-
"sys/statvfs.h","sys/time.h","sys/times.h","sys/types.h","sys/uio.h","sys/un.h","sys/utsname.h",
51-
"sys/wait.h","syslog.h","tar.h","termios.h","tgmath.h","time.h","trace.h","ulimit.h","unistd.h",
52-
"utime.h","utmpx.h","wchar.h","wctype.h","wordexp.h"};
53-
*/
54-
5520
explicit TreeConsumer(clang::CompilerInstance *CI) : visitor(new TreeVisitor(CI)), smanager(CI->getASTContext().getSourceManager()) {}
5621

57-
58-
5922
virtual bool HandleTopLevelDecl(clang::DeclGroupRef DG) {
6023
// a DeclGroupRef may have multiple Decls, so we iterate through each one
6124
for (clang::DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; i++) {
6225
clang::Decl *D = *i;
6326
std::string fpath = smanager.getFilename(D->getLocation()).str();
64-
//llvm::errs() << "DEBUG: " << fpath << "\n";
65-
if(std::find(stdheaders.begin(), stdheaders.end(), basename(fpath)) == stdheaders.end() && fpath != ""){
66-
visitor->TraverseDecl(D); // recursively visit each AST node in Decl "D"
27+
locations.insert(basename(fpath) + "\n");
28+
29+
if(!headerMan.IsStandard(basename(fpath)) && fpath != ""){
30+
visitor->TraverseDecl(D);
6731
}
32+
6833
}
6934
return true;
7035
}
7136

72-
// this replaces "HandleTopLevelDecl"
73-
// override this to call our ExampleVisitor on the entire source file
74-
/*virtual void HandleTranslationUnit(clang::ASTContext &Context) {
75-
//we can use ASTContext to get the TranslationUnitDecl, which is
76-
//a single Decl that collectively represents the entire source file
77-
visitor->TraverseDecl(Context.getTranslationUnitDecl());
78-
}*/
7937

8038
};

TreeVisitor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ HeaderManager headerMan;
88
std::string declarations;
99
std::string enums;
1010
std::string helpers;
11+
std::set<std::string> locations;
1112

1213
bool TreeVisitor::VisitFunctionDecl(clang::FunctionDecl *func) {
1314
std::string funcName = func->getNameInfo().getName().getAsString();

TreeVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern HeaderManager headerMan;
1717
extern std::string declarations;
1818
extern std::string enums;
1919
extern std::string helpers;
20+
extern std::set<std::string> locations;
2021

2122
class TreeVisitor : public clang::RecursiveASTVisitor<TreeVisitor> {
2223
private:

TypeTranslator.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,10 @@ std::string TypeTranslator::TranslateFunctionPointer(const clang::QualType& qtpe
4949
counter++;
5050
}
5151

52-
if(params != ""){
53-
//remove last ,
54-
params = params.substr(0, params.size()-2);
52+
std::string variad = fc->isVariadic() ? "varArgs: native.CVararg*, " : "";
53+
54+
return std::string("native.CFunctionPtr") + std::to_string(counter) + "[" + params + variad + ret + "]";
5555

56-
return std::string("native.CFunctionPtr") + std::to_string(counter) + "[" + params + ", " + ret + "]";
57-
} else{
58-
return std::string("native.CFunctionPtr") + std::to_string(counter) + "[" + ret + "]";
59-
}
6056
} else {
6157
llvm::errs() << "Unsupported function pointer type: " << qtpe.getAsString() << "\n";
6258
exit(-1);

stdHeaders.config

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
2+
assert.h=
3+
complex.h=scala.scalanative.native.complex
4+
ctype.h=scala.scalanative.native.ctype
5+
errno.h=scala.scalanative.native.errno
6+
fenv.h=
7+
float.h=
8+
inttypes.h=
9+
iso646.h=
10+
limits.h=
11+
locale.h=
12+
math.h=scala.scalanative.native.math
13+
setjmp.h=
14+
signal.h=scala.scalanative.native.signal
15+
stdalign.h=
16+
stdarg.h=
17+
stdatomic.h=
18+
stdbool.h=
19+
stddef.h=
20+
stdint.h=
21+
stdio.h=scala.scalanative.native.stdio
22+
stdlib.h=scala.scalanative.native.stdlib
23+
stdnoreturn.h=
24+
string.h=scala.scalanative.native.string
25+
tgmath.h=
26+
threads.h=
27+
time.h=
28+
uchar.h=
29+
wchar.h=
30+
wctype.h=
31+
32+
33+
aio.h=
34+
inet.h=scala.scalanative.posix.arpa.inet
35+
assert.h=
36+
complex.h=scala.scalanative.native.complex
37+
cpio.h=scala.scalanative.posix.cpio
38+
ctype.h=
39+
dirent.h=scala.scalanative.posix.dirent
40+
dlfcn.h=
41+
errno.h=scala.scalanative.posix.errno
42+
fcntl.h=scala.scalanative.posix.fcntl
43+
fenv.h=
44+
float.h=
45+
fmtmsg.h=
46+
fnmatch.h=
47+
ftw.h=
48+
glob.h=
49+
grp.h=scala.scalanative.posix.grp
50+
iconv.h=
51+
inttypes.h=scala.scalanative.posix.inttypes
52+
iso646.h=
53+
langinfo.h=
54+
libgen.h=
55+
limits.h=scala.scalanative.posix.limits
56+
locale.h=
57+
math.h=
58+
monetary.h=
59+
mqueue.h=
60+
ndbm.h=
61+
if.h=
62+
netdb.h=
63+
in.h=scala.scalanative.posix.netinet.in
64+
tcp.h=
65+
nl_types.h=
66+
poll.h=
67+
pthread.h=
68+
pwd.h=scala.scalanative.posix.pwd
69+
regex.h=scala.scalanative.posix.regex
70+
sched.h=
71+
search.h=
72+
semaphore.h=
73+
setjmp.h=
74+
signal.h=
75+
spawn.h=
76+
stdarg.h=
77+
stdbool.h=
78+
stddef.h=
79+
stdint.h=
80+
stdio.h=
81+
stdlib.h=scala.scalanative.posix.stdlib
82+
string.h=
83+
strings.h=
84+
stropts.h=
85+
ipc.h=
86+
mman.h=
87+
msg.h=
88+
resource.h=
89+
select.h=
90+
sem.h=
91+
shm.h=
92+
socket.h=scala.scalanative.posix.sys.socket
93+
stat.h=scala.scalanative.posix.sys.stat
94+
statvfs.h=
95+
time.h=scala.scalanative.posix.sys.time
96+
times.h=
97+
types.h=
98+
uio.h=scala.scalanative.posix.sys.uio
99+
un.h=
100+
utsname.h=
101+
wait.h=
102+
syslog.h=scala.scalanative.posix.syslog
103+
tar.h=
104+
termios.h=scala.scalanative.posix.termios
105+
tgmath.h=
106+
time.h=
107+
trace.h=
108+
ulimit.h=
109+
unistd.h=scala.scalanative.posix.unistd
110+
utime.h=scala.scalanative.posix.utime
111+
utmpx.h=
112+
wchar.h=
113+
wctype.h=
114+
wordexp.h=
115+
116+
siginfo.h=
117+
sigset.h=
118+
sigaction.h=
119+
sigcontext.h=
120+
sigthread.h=
121+
ucontext.h=
122+
pthreadtypes.h=
123+
libio.h=
124+
__stddef_max_align_t.h=
125+
_G_config.h=
126+
sys_errlist.h"=
127+
sysmacros.h=
128+
xlocale.h=
129+
socket_type.h=
130+
sockaddr.h=
131+
sigstack.h=
132+
sys_errlist.h=

0 commit comments

Comments
 (0)