-
Notifications
You must be signed in to change notification settings - Fork 68
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
Rework the package -- Take 1 #278
Conversation
…her minor updates
Also fix an early for-loop escape bug
I'm trying this out, and it looks amazing ❤️ The only thing I've run in to so far is handling of edit: specifically, looks like that only happens for return types, function args work fine. |
hi @ihnorton, could you share the C code that triggers the issue? I simplified the rule for translating function return types, but this change may be wrong for some corner cases. |
The signatures are here. The driver is exactly as you posted here but changed the names and lib path. Looking closer, I see what is going on (overlooked the warning comment 🙃 ): these are all classified as Lines 208 to 222 in 01bf3e8
master , and the return type was translated correctly as UInt64 etc.)
|
Thanks for the hint. Should now be fixed by df52021. |
Works perfectly now, thanks! |
This also serves as an example on how to use Clang.jl for cross-compiling.
This rework effort is looking great! Is it on purpose that in
Also, I have been unlucky that the C library I am trying to generate bindings for has a function which has a parameter named
I got around it by: import Clang.Generators.RESERVED_WORDS
# ...
push!(RESERVED_WORDS, "Ptr") Does this warrant enough of a use-case to expose a function/macro to add other words which may conflict? Similar to |
I'm mainly running https://github.com/Gnimuc/GeneratorScripts for meaningful testing of the generated files, so here only the generator's public API is tested for now.
@vrzh, thanks for the feedback! Without adding new features, a better fix would be writing a rewriter for that specific function like this one. If you'd like to have this feature in Clang.jl, I'd suggest that we just add a new entry in the toml file like: [codegen]
function_argument_conflict_symbols = ["Ptr"] # should be empty by default and prefix the argument names with "_" according to this list in the codegen.jl. |
@vrzh, OK, I just implemented that in the latest commit, could you give it a try? |
Doesn't seem to work for me. I am new to Julia so most likely my fault. I tried the following in a clean Julia environment.
using Pkg
Pkg.add(PackageSpec(name="Clang", rev="4fb6690"))
using Clang.Generators
using Clang.LibClang.Clang_jll
headers = [ "test.h" ]
options = load_options(joinpath(@__DIR__, "test.toml"))
args = [ "-I." ]
# import Clang.Generators.RESERVED_WORDS
# push!(RESERVED_WORDS, "Ptr")
ctx = create_context(headers, args, options)
build!(ctx)
[general]
library_name = "libtest"
[codegen]
function_argument_conflict_symbols = ["Ptr"]
void f(int * x, int Ptr); Output: function f(x, Ptr)
ccall((:f, libtest), Cvoid, (Ptr{Cint}, Cint), x, Ptr)
end I agree with you that a rewriter is the best way to go (as you could also have C function names, C struct names, etc. which also clash), but an entry in the toml file as you suggest is the easiest for new users. |
@vrzh Oops! Should be fixed now.
As C doesn't have any name mangling stuff, a common C library usually has its own manually mangled prefixes, so generally speaking, function/struct name collisions rarely happen in practice. |
Perfect! Works great, thanks a lot. |
This PR is too long, so let's merge this to master. |
Well, I ended up reworking the whole package.
Features added by this PR:
Generators
module for wrapper generation. The context now maintains a DAG of the original AST of the C code. This DAG can be used for CTU analysis. The oldwrap
methods are split into different passes, this makes it easier to add features to the generator, e.g. printing useful C comments(fix Function and other signatures in comments #195).__attribute__
/bitfield
/nested anonymous
) are generated as a memory-pool-like struct. The size is queried from libclang, so it guarantees that issues like Potential wrong alignments and sizes of structs with C unions #238 won't happen(assume the code is system-independent). (fix Potential wrong alignments and sizes of structs with C unions #238, fix no methodvalue(::CLPackedAttr)
#224)create_context
only accepts 3 arguments(headers, compiler arguments, options) instead of 8 arguments. Additional options are loaded from a .toml file. By default, the generator prints a complete module to a single file.In short, the goal is to make the wrapper generator easier to extend for new features.
As mentioned above, this PR is breaking. But I guess it's not that hard to upgrade the old scripts. I'm going to manually submit upgrading PRs to the downstream repos, which can also serve as a validation of the correctness of this PR.
TODO list:
getproperty
/setproperty
for structs with special layoutSupport vararg functions with the new@ccall
macro(callback to support possible vararg definitions? #17, WARNING: Skipping VarArg Function CLCursor (Clang.cindex.FunctionDecl) #146)RegisterLibClang.jl
as a standalone packageOther bugfixes: fix #280, fix #279, fix #136, fix #126