Skip to content

Commit 3dc1f7e

Browse files
nraynaudvisr
authored andcommitted
get the const char* return type by the clang API
1 parent 0da4282 commit 3dc1f7e

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

gen/generator.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
using Clang.LibClang
21
using Clang.Generators
2+
using Clang
33
using MacroTools
44
using GEOS_jll
55
using JuliaFormatter: format
66

7+
function return_type_is_const_char(cursor::Clang.CLFunctionDecl)
8+
result_type = Clang.getCanonicalType(Clang.getCursorResultType(cursor))
9+
if result_type isa CLPointer
10+
destination = Clang.getPointeeType(result_type)
11+
return destination isa CLChar_S && Clang.isConstQualifiedType(destination)
12+
end
13+
return false
14+
end
15+
716
"Functions that return a Cstring are wrapped in unsafe_string to return a String"
8-
function rewrite(ex::Expr)
17+
function rewrite(ex::Expr, cursor::Clang.CLFunctionDecl)
918
if @capture(ex, function fname_(fargs__)
1019
@ccall lib_.cname_(cargs__)::rettype_
1120
end)
12-
1321
# bind the ccall such that we can easily wrap it
1422
cc = :(@ccall $lib.$cname($(cargs...))::$rettype)
1523

1624
cc′ = if rettype == :Cstring
1725
# do not try to free a const char *
18-
# this is the only function returning such type
19-
if cname == :GEOSversion
26+
if return_type_is_const_char(cursor)
2027
:(unsafe_string($cc))
2128
else
2229
:(transform_c_string($cc))
@@ -37,7 +44,10 @@ end
3744

3845
function rewrite!(dag::ExprDAG)
3946
for n in dag.nodes
40-
map!(rewrite, n.exprs, n.exprs)
47+
if !isa(n.cursor, Clang.CLFunctionDecl)
48+
continue
49+
end
50+
map!(e -> rewrite(e, n.cursor), n.exprs, n.exprs)
4151
end
4252
end
4353

0 commit comments

Comments
 (0)