Skip to content

Commit a8467bd

Browse files
committed
Better handle variadic functions
1 parent 62150a1 commit a8467bd

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

SimpleTypeTests.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,40 @@ TEST_CASE("struct pointer", "[Type]"){
6565
REQUIRE(answ == Translate(code));
6666
}
6767

68+
TEST_CASE("func no args", "[Func]"){
69+
std::string code = "int foo();";
70+
std::string answ = "\tdef foo(): native.CInt = native.extern\n";
71+
REQUIRE(answ == Translate(code));
72+
}
73+
74+
TEST_CASE("func void args", "[Func]"){
75+
std::string code = "int foo(void);";
76+
std::string answ = "\tdef foo(): native.CInt = native.extern\n";
77+
REQUIRE(answ == Translate(code));
78+
}
79+
80+
TEST_CASE("func 1 arg", "[Func]"){
81+
std::string code = "void foo(int a);";
82+
std::string answ = "\tdef foo(a: native.CInt): Unit = native.extern\n";
83+
REQUIRE(answ == Translate(code));
84+
}
85+
86+
TEST_CASE("func 2 args", "[Func]"){
87+
std::string code = "void foo(float a, int b);";
88+
std::string answ = "\tdef foo(a: native.CFloat, b: native.CInt): Unit = native.extern\n";
89+
REQUIRE(answ == Translate(code));
90+
}
91+
92+
TEST_CASE("func anonymous args", "[Func]"){
93+
std::string code = "void foo(float, int);";
94+
std::string answ = "\tdef foo(anonymous0: native.CFloat, anonymous1: native.CInt): Unit = native.extern\n";
95+
REQUIRE(answ == Translate(code));
96+
}
97+
98+
TEST_CASE("func variadic args", "[Func]"){
99+
std::string code = "double foo(double a, void* b, ...);";
100+
std::string answ = "\tdef foo(a: native.CDouble, b: native.Ptr[Byte], varArgs: native.CVararg*): native.CDouble = native.extern\n";
101+
REQUIRE(answ == Translate(code));
102+
}
103+
104+

TreeVisitor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ bool TreeVisitor::VisitFunctionDecl(clang::FunctionDecl *func) {
3535
params = params.substr(0, params.size()-2);
3636
}
3737

38-
declarations += "\tdef " + funcName + "(" + params + "): " + retType + " = native.extern\n";
38+
//Note the C Iso require at least one argument in a variadic function, so the comma is fine
39+
std::string variad = func->isVariadic() ? ", varArgs: native.CVararg*" : "";
40+
41+
declarations += "\tdef " + funcName + "(" + params + variad + "): " + retType + " = native.extern\n";
3942
return true;
4043
}
4144

0 commit comments

Comments
 (0)