JuliaInterop / Clang.jl

C binding generator and Julia interface to libclang
https://juliainterop.github.io/Clang.jl/
MIT License
224 stars 68 forks source link

Error: error thrown. Last cursor available in context.cursor_stack #245

Closed bjarthur closed 3 years ago

bjarthur commented 4 years ago

i get the same error as https://github.com/JuliaInterop/Clang.jl/issues/234, but the solution there does not seem to apply here.

julia> wc = init(common_file="common.jl", headers=["NIDAQmx.h"])
WrapContext(Index(Ptr{Nothing} @0x000000002f5ef6e0, 0, 1), ["NIDAQmx.h"], "", "common.jl", String[], String[], Clang.var"#49#60"(), Clang.var"#42#53"(), Clang.var"#41#52"{String}(""), Clang.var"#50#61"(), OrderedCollections.OrderedDict{Symbol,ExprUnit}(), DataStructures.DefaultOrderedDict{String,Array{Any,N} where N,Clang.var"#45#56"}(), Clang.InternalOptions(true, false), Clang.var"#51#62"())

julia> run(wc)
[ Info: wrapping header: NIDAQmx.h ...
┌ Error: error thrown. Last cursor available in context.cursor_stack.
└ @ Clang C:\Users\arthurb\.julia\packages\Clang\CiPzM\src\compat.jl:125
ERROR: AssertionError: ptr_ref[] != C_NULL
Stacktrace:
 [1] TokenList(::Ptr{Nothing}, ::Clang.LibClang.CXSourceRange) at C:\Users\arthurb\.julia\packages\Clang\CiPzM\src\token.jl:13
 [2] tokenize(::CLMacroDefinition) at C:\Users\arthurb\.julia\packages\Clang\CiPzM\src\token.jl:100
 [3] wrap!(::DefaultContext, ::CLMacroDefinition) at C:\Users\arthurb\.julia\packages\Clang\CiPzM\src\wrap_c.jl:389
 [4] run(::WrapContext, ::Bool) at C:\Users\arthurb\.julia\packages\Clang\CiPzM\src\compat.jl:122
 [5] run(::WrapContext) at C:\Users\arthurb\.julia\packages\Clang\CiPzM\src\compat.jl:89
 [6] top-level scope at REPL[11]:1

NIDAQmx.h is here

this is with julia 1.3.1 and the clang 0.9.1.

any suggestions as to how i can fix this? thanks.

Gnimuc commented 4 years ago

julia> wc = init(common_file="common.jl", headers=["NIDAQmx.h"])

I don't think this is the recommended way to use the init function. If there is no limitation on which header should be wrapped, Clang will try to wrap every header that indirectly used in the target header(e.g. libc), so this can be fixed by adding header_wrapped = (root, current)->root == current.

using Clang

NIDAQmx_H = joinpath(@__DIR__, "NIDAQmx.h")
wc = init(; headers = [NIDAQmx_H],
                  output_file = "NIDAQmx.jl",
                  common_file = "common.jl",
                  clang_includes = vcat(CLANG_INCLUDE),
                  clang_args = map(x->"-I"*x, find_std_headers()),
                  header_wrapped = (root, current)->root == current,
                  header_library = x->"nidaqmx",
                  clang_diagnostics = true)
run(wc)
bjarthur commented 4 years ago

thanks! that runs without errors.

however, there is now a typedef which is not getting translated to julia properly. specifically:

typedef struct CVITime { uInt64 lsb; int64 msb; } CVITime;
typedef union CVIAbsoluteTime { CVITime cviTime; uInt32 u32Data[4]; } CVIAbsoluteTime;

comes out as:

struct CVIAbsoluteTime
    cviTime::CVITime
end

simple to fix by adding struct CVITime; lsb::uInt64; msb::int64; end, but shouldn't Clang have done this automatically?

Gnimuc commented 4 years ago

Looks like it hit a corner case when wrapping that typedef struct(the code defines a struct firstly and then typedef a type with the same name...).

I'm still looking for a good way to overhaul the struct/union wrapping logic. Unfortunately, I don't have enough bandwidth to start for now. As a result, it's highly recommended to manually check the generated code for non-trivial unions as it might contain alignment bugs. You might also be interested in https://github.com/JuliaInterop/Clang.jl/issues/233#issuecomment-523260973 on how to manually map C unions in Julia.

Gnimuc commented 4 years ago

oh, wait! Is this the same issue as https://github.com/JuliaInterop/Clang.jl/issues/249 which was fixed a while ago by https://github.com/JuliaInterop/Clang.jl/pull/250?

Gnimuc commented 4 years ago

@bjarthur, it looks like @wsphillips is also working on this wrapper: https://github.com/wsphillips/NIDAQ.jl.

Gnimuc commented 3 years ago

I think this has already been fixed by #250.