Skip to content

Commit 6fb13d0

Browse files
committed
Add test support with catch
1 parent d08f23d commit 6fb13d0

File tree

6 files changed

+13157
-35
lines changed

6 files changed

+13157
-35
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
set(LLVM_LINK_COMPONENTS support)
22

3+
add_compile_options(-fexceptions)
4+
35
add_clang_executable(scalaBindgen
46
ScalaBindgen.cpp
57
ScalaBindgen.h
@@ -8,6 +10,8 @@ add_clang_executable(scalaBindgen
810
HeaderManager.h
911
HeaderManager.cpp
1012
Utils.h
13+
catch/catch.hpp
14+
SimpleTypeTests.cpp
1115
)
1216

1317
target_link_libraries(scalaBindgen

ScalaBindgen.cpp

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#include "ScalaBindgen.h"
22

3+
#define CATCH_CONFIG_RUNNER
4+
#include "catch/catch.hpp"
5+
6+
37
#define SCALA_NATIVE_MAX_STRUCT_FIELDS 22
48

9+
510
static llvm::cl::OptionCategory Category("Binding Generator");
611
static llvm::cl::extrahelp CommonHelp(clang::tooling::CommonOptionsParser::HelpMessage);
712
static llvm::cl::extrahelp MoreHelp("\nProduce Bindings for scala native. Please specify lib name wit parameter name\n");
@@ -128,45 +133,52 @@ bool TreeVisitor::VisitRecordDecl(clang::RecordDecl *record){
128133
return false;
129134
}
130135

136+
int main(int argc, char *argv[]) {
131137

138+
if(argc <= 1 ){
132139

133-
int main(int argc, const char **argv) {
134-
clang::tooling::CommonOptionsParser op(argc, argv, Category);
135-
clang::tooling::ClangTool Tool(op.getCompilations(), op.getSourcePathList());
140+
int result = Catch::Session().run( argc, argv );
141+
return result;
136142

137-
auto lib = LibName.getValue();
138-
if(lib == ""){
139-
llvm::errs() << "Error: Please specify the lib name using -name paramter\n";
140-
return -1;
141-
}
143+
} else{
142144

143-
auto stdhead = StdHeaders.getValue();
144-
if(stdhead != ""){
145-
headerMan.LoadConfig(stdhead);
146-
}
145+
clang::tooling::CommonOptionsParser op(argc, (const char**)argv, Category);
146+
clang::tooling::ClangTool Tool(op.getCompilations(), op.getSourcePathList());
147+
148+
auto lib = LibName.getValue();
149+
if(lib == ""){
150+
llvm::errs() << "Error: Please specify the lib name using -name paramter\n";
151+
return -1;
152+
}
147153

148-
declarations = "";
149-
enums = "";
154+
auto stdhead = StdHeaders.getValue();
155+
if(stdhead != ""){
156+
headerMan.LoadConfig(stdhead);
157+
}
150158

159+
declarations = "";
160+
enums = "";
151161

152-
int result = Tool.run(clang::tooling::newFrontendActionFactory<ExampleFrontendAction>().get());
153162

154-
llvm::outs() << "import scala.scalanative._\n"
155-
<< "import scala.scalanative.native.Nat._\n\n";
163+
int result = Tool.run(clang::tooling::newFrontendActionFactory<ExampleFrontendAction>().get());
156164

157-
if(declarations != ""){
158-
llvm::outs() << "@native.link(\"" << lib << "\")\n"
159-
<< "@native.extern\n"
160-
<< "object " << lib << " {\n"
161-
<< declarations
162-
<< "}\n\n";
163-
}
165+
llvm::outs() << "import scala.scalanative._\n"
166+
<< "import scala.scalanative.native.Nat._\n\n";
164167

165-
if(enums != ""){
166-
llvm::outs() << "object " << lib << "Enums {\n"
167-
<< enums
168-
<< "}\n";
169-
}
168+
if(declarations != ""){
169+
llvm::outs() << "@native.link(\"" << lib << "\")\n"
170+
<< "@native.extern\n"
171+
<< "object " << lib << " {\n"
172+
<< declarations
173+
<< "}\n\n";
174+
}
170175

171-
return result;
176+
if(enums != ""){
177+
llvm::outs() << "object " << lib << "Enums {\n"
178+
<< enums
179+
<< "}\n";
180+
}
181+
182+
return result;
183+
}
172184
}

ScalaBindgen.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include "TypeTranslator.h"
33
#include "HeaderManager.h"
44

5-
65
#include "clang/Driver/Options.h"
76
#include "clang/Basic/LangOptions.h"
87
#include "clang/AST/AST.h"
@@ -16,7 +15,8 @@
1615
#include "clang/Tooling/Tooling.h"
1716
#include "llvm/Support/CommandLine.h"
1817

19-
18+
extern std::string declarations;
19+
extern std::string enums;
2020

2121
class TreeVisitor : public clang::RecursiveASTVisitor<TreeVisitor> {
2222
private:

SimpleTypeTests.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "ScalaBindgen.h"
2+
3+
4+
#include "clang/Tooling/Tooling.h"
5+
#include "catch/catch.hpp"
6+
7+
#include <iostream>
8+
9+
std::string Translate(std::string code){
10+
declarations = "";
11+
enums = "";
12+
auto* action = new ExampleFrontendAction;
13+
clang::tooling::runToolOnCode(action, code);
14+
return declarations;
15+
}
16+
17+
TEST_CASE("native types", "[Type]" ) {
18+
19+
std::map<std::string, std::string> types = {
20+
{"void", "Unit"},
21+
//{"bool", "native.CBool"},
22+
{"char", "native.CChar"},
23+
{"signed char", "native.CSignedChar"},
24+
{"unsigned char", "native.CUnsignedChar"},
25+
{"short", "native.CShort"},
26+
{"unsigned short", "native.CUnsignedShort"},
27+
{"int", "native.CInt"},
28+
//{"long int", "native.CLongInt"},
29+
{"unsigned int", "native.CUnsignedInt"},
30+
//{"unsigned long int", "native.CUnsignedLongInt"},
31+
{"long", "native.CLong"},
32+
{"unsigned long", "native.CUnsignedLong"},
33+
{"long long", "native.CLongLong"},
34+
{"unsigned long long", "native.CUnsignedLongLong"},
35+
//{"size_t", "native.CSize"},
36+
//{"ptrdiff_t", "native.CPtrDiff"},
37+
{"wchar_t", "native.CWideChar"},
38+
{"char16_t", "native.CChar16"},
39+
{"char32_t", "native.CChar32"},
40+
{"float", "native.CFloat"},
41+
{"double", "native.CDouble"},
42+
{"void*", "native.Ptr[Byte]"},
43+
{"int*", "native.Ptr[native.CInt]"},
44+
//{"char*", "native.CString"},
45+
//{"int (*a)(int)", "native.CFunctionPtr1[native.CInt, native.CInt]"},
46+
//{"struct { int x, y; }*", "native.Ptr[native.CStruct2[native.CInt, native.CInt]]"}
47+
};
48+
49+
for(const auto& kv: types){
50+
std::string code = "typedef " + kv.first + " a;\n";
51+
std::string answer = "\ttype a = " + kv.second + "\n";
52+
53+
REQUIRE(answer == Translate(code));
54+
}
55+
56+
}

TypeTranslator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ std::string TypeTranslator::TranslateFunctionPointer(const clang::QualType& qtpe
4444

4545
for(const clang::QualType& param: fc->param_types()){
4646
params += Translate(param, avoid);
47-
params += ",";
47+
params += ", ";
4848
counter++;
4949
}
5050

5151
if(params != ""){
5252
//remove last ,
53-
params = params.substr(0, params.size()-1);
53+
params = params.substr(0, params.size()-2);
5454

55-
return std::string("native.CFunctionPtr") + std::to_string(counter) + "[" + params + "," + ret + "]";
55+
return std::string("native.CFunctionPtr") + std::to_string(counter) + "[" + params + ", " + ret + "]";
5656
} else{
5757
return std::string("native.CFunctionPtr") + std::to_string(counter) + "[" + ret + "]";
5858
}

0 commit comments

Comments
 (0)