|
1 | | -using Clang.LibClang |
2 | 1 | using Clang.Generators |
| 2 | +using Clang |
3 | 3 | using MacroTools |
4 | 4 | using GEOS_jll |
5 | 5 | using JuliaFormatter: format |
6 | 6 |
|
| 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 | + |
7 | 16 | "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) |
9 | 18 | if @capture(ex, function fname_(fargs__) |
10 | 19 | @ccall lib_.cname_(cargs__)::rettype_ |
11 | 20 | end) |
12 | | - |
13 | 21 | # bind the ccall such that we can easily wrap it |
14 | 22 | cc = :(@ccall $lib.$cname($(cargs...))::$rettype) |
15 | 23 |
|
16 | 24 | cc′ = if rettype == :Cstring |
17 | 25 | # 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) |
20 | 27 | :(unsafe_string($cc)) |
21 | 28 | else |
22 | 29 | :(transform_c_string($cc)) |
|
37 | 44 |
|
38 | 45 | function rewrite!(dag::ExprDAG) |
39 | 46 | 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) |
41 | 51 | end |
42 | 52 | end |
43 | 53 |
|
|
0 commit comments