Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lobingera opened this issue Sep 1, 2019 · 11 comments
Closed

Comments

@lobingera
Copy link

julia> c = init(output_file="gen_new.jl",headers=["x1.h"])
WrapContext(Index(Ptr{Nothing} @0x0000000002a5b9f0, 0, 1), ["x1.h"], "gen_new.jl", "gen_new.jl", String[], String[], getfield(Clang, Symbol("##49#60"))(), getfield(Clang, Symbol("##42#53"))(), getfield(Clang, Symbol("##44#55")){String,String}("", "gen_new.jl"), getfield(Clang, Symbol("##50#61"))(), OrderedCollections.OrderedDict{Symbol,ExprUnit}(), DataStructures.DefaultOrderedDict{String,Array{Any,N} where N,getfield(Clang, Symbol("##45#56"))}(), Clang.InternalOptions(true, false), getfield(Clang, Symbol("##51#62"))())

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

julia> c.cursor_stack
ERROR: type WrapContext has no field cursor_stack
Stacktrace:
 [1] getproperty(::Any, ::Symbol) at ./Base.jl:20
@Gnimuc
Copy link
Member

Gnimuc commented Sep 1, 2019

Looks like something happened in the process of tokenization. Could you share the content of "x1.h"?

@lobingera
Copy link
Author

https://gist.github.com/lobingera/ec5ee777f459eb923972 (note the date ...)

Actually i wanted to point out, that the error message has a problem, as c.cursor_stack seems to be existing (anymore), so it's not visible to the user where to find the stop in the input

@lobingera
Copy link
Author

was #129

@Gnimuc
Copy link
Member

Gnimuc commented Sep 1, 2019

sorry, that’s a old copy-paste bug when I was refactoring the code.

@lobingera
Copy link
Author

and where would i find the cursor 'state' ?

@Gnimuc
Copy link
Member

Gnimuc commented Sep 1, 2019

there is no way to retrieve that info when using init-run API. the context will be gc-ed when run exits. could you rewrite it based on this template: https://github.com/JuliaInterop/Clang.jl/blob/master/gen/generator.jl ?

@lobingera
Copy link
Author

Seriously? Clang cannot report (by default) where it stopped?

@Gnimuc
Copy link
Member

Gnimuc commented Sep 1, 2019

what I meant is: since there is a bug in the implementation of current init-run API, there is no way to extract that info(generated by Clang) in julia. there is no bug in the above-mentioned template code, the cursor state can be retrieved.

@Gnimuc
Copy link
Member

Gnimuc commented Sep 1, 2019

the init-run API is actually implemented via DefaultContext, we keep these two functions for backward compatibility reasons.

@Gnimuc
Copy link
Member

Gnimuc commented Sep 3, 2019

@lobingera sorry for the late reply. I tried to wrap https://github.com/stapelberg/libxcb using the following script:

using Clang

XCB_INCLUDE = joinpath(@__DIR__, "..", "xcb") |> normpath
XCB_HEADERS = [joinpath(XCB_INCLUDE, header) for header in readdir(XCB_INCLUDE) if endswith(header, ".h")]

# create a work context
ctx = DefaultContext()

# parse headers
parse_headers!(ctx, XCB_HEADERS,
               args=["-I", joinpath(XCB_INCLUDE, ".."), map(x->"-I"*x, find_std_headers())...],
               includes=vcat(XCB_HEADERS, CLANG_INCLUDE),
               )

# settings
ctx.libname = "libxcb"
ctx.options["is_function_strictly_typed"] = false
ctx.options["is_struct_mutable"] = false

# write output
api_file = joinpath(@__DIR__, "libxcb_api.jl")
api_stream = open(api_file, "w")

for trans_unit in ctx.trans_units
    root_cursor = getcursor(trans_unit)
    push!(ctx.cursor_stack, root_cursor)
    header = spelling(root_cursor)
    @info "wrapping header: $header ..."
    # loop over all of the child cursors and wrap them, if appropriate.
    ctx.children = children(root_cursor)
    for (i, child) in enumerate(ctx.children)
        child_name = name(child)
        child_header = filename(child)
        ctx.children_index = i
        # choose which cursor to wrap
        startswith(child_name, "__") && continue  # skip compiler definitions
        child_name in keys(ctx.common_buffer) && continue  # already wrapped
        child_header != header && continue  # skip if cursor filename is not in the headers to be wrapped

        wrap!(ctx, child)
    end
    @info "writing $(api_file)"
    println(api_stream, "# Julia wrapper for header: $(basename(header))")
    println(api_stream, "# Automatically generated using Clang.jl\n")
    print_buffer(api_stream, ctx.api_buffer)
    empty!(ctx.api_buffer)  # clean up api_buffer for the next header
end
close(api_stream)

# write "common" definitions: types, typealiases, etc.
common_file = joinpath(@__DIR__, "libxcb_common.jl")
open(common_file, "w") do f
    println(f, "# Automatically generated using Clang.jl\n")
    print_buffer(f, dump_to_buffer(ctx.common_buffer))
end

# uncomment the following code to generate dependency and template files
# copydeps(dirname(api_file))
# print_template(joinpath(dirname(api_file), "LibTemplate.jl"))

there are some expansion-to-defined warnings and an error:

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:331:6: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:331:6: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:540:6: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:540:6: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
/Users/gnimuc/Codes/Hatchery/xcb/xcb.h:168:10: fatal error: 'xproto.h' file not found
/Users/gnimuc/Codes/Hatchery/xcb/xcb_windefs.h:34:10: fatal error: 'winsock2.h' file not found
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:331:6: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:331:6: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:540:6: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/pthread.h:540:6: warning: macro expansion producing 'defined' has undefined behavior [-Wexpansion-to-defined]
/Users/gnimuc/Codes/Hatchery/xcb/xcb.h:168:10: fatal error: 'xproto.h' file not found

/Users/gnimuc/Codes/Hatchery/xcb/xcb.h:168:10: fatal error: 'xproto.h' file not found

I didn't build libxcb, so the generated header file xproto.h was missing.

@lobingera
Copy link
Author

Thank you for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants