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

structs created in wrong order #351

Closed
danielbrogren opened this issue Nov 24, 2021 · 8 comments
Closed

structs created in wrong order #351

danielbrogren opened this issue Nov 24, 2021 · 8 comments

Comments

@danielbrogren
Copy link

In c I have the following structs

typedef struct a_tag {
int xxx;
uint8_t *ppp;
} a_t;

typedef struct l1_dl_sch_tag {
a_t yyy[2];
} l1_dl_sch_t;

When checking the generated.jl, some of my structs are in the correct order but some of them are in the wrong order so that l1_dl_sch_tag is defined before a_tag which leads to error: a_tag not defined.

When changing the name of the l1_dl_sch_tag, then it works fine and they are in the correct order again.

@Gnimuc
Copy link
Member

Gnimuc commented Nov 24, 2021

Which version are you using? I can't reproduce the issue with the latest release.

shell> cat test.h
#include <stdint.h> 

typedef struct a_tag {
int xxx;
uint8_t *ppp;
} a_t;

typedef struct l1_dl_sch_tag {
a_t yyy[2];
} l1_dl_sch_t;

julia> ctx = create_context("test.h", args);
[ Info: Parsing headers...

julia> build!(ctx)
[ Info: Processing header: test.h
[ Info: Building the DAG...
┌ Warning: default libname: ":libxxx" is being used, did you forget to set `library_name` in the toml file?
└ @ Clang.Generators ~/.julia/dev/Clang/src/generator/audit.jl:16
[ Info: Emit Julia expressions...
struct a_tag
    xxx::Cint
    ppp::Ptr{UInt8}
end

const a_t = a_tag

struct l1_dl_sch_tag
    yyy::NTuple{2, a_t}
end

const l1_dl_sch_t = l1_dl_sch_tag

[ Info: Done!
Context(...)

@Gnimuc
Copy link
Member

Gnimuc commented Nov 24, 2021

Wait, did you run pkg> add Clang on Julia 1.7?

I just noticed that the command above will install an old version(v0.8) of Clang.jl with Julia 1.7. To use the latest Clang.jl, you need to run pkg> dev Clang and manually switch to this branch #347.

This is indeed a problem. Maybe we should submit a fix to the "General" registry.

@danielbrogren
Copy link
Author

My c-header file was much more complex then this so this was just a part of the file. I can not send the entire file due to that it contains company information. Sorry!

I was running Julia 1.6.1

Manisfest:
[[Clang]]
deps = ["CEnum", "Clang_jll", "Downloads", "Pkg", "TOML"]
git-tree-sha1 = "1a485576e0bf5658a04a570c3215743b0b3069d6"
uuid = "40e3b903-d033-50b4-a0cc-940c62c95e31"
version = "0.14.1"

@Gnimuc
Copy link
Member

Gnimuc commented Nov 24, 2021

Well, I can't debug this without a minimal working example(MWE).

You could either make a simplified version of the header without any company info or debug this on your own.

The hint is to check the adj field of the l1_dl_sch_tag node after the resolve dependency pass, like ctx.dag.nodes[ctx.dag.tags["l1_dl_sch_tag"]].adj, adj contains a vector of node indices, you could loop it through and check whether it contains a_t or not.

@danielbrogren
Copy link
Author

I think I figured it out. I had an include that messed thinks up a bit for me.

@Gnimuc
Copy link
Member

Gnimuc commented Nov 27, 2021

@danielbrogren would be great if you could share some details. I'm thinking about writing some FAQs for trouble-shooting so that new users won't hit the same issue when using this package. :)

@danielbrogren
Copy link
Author

Hi.
The problem i faced was something like this


a.h

typedef struct foo_tag foo_t;


b.h

#include a.h

typedef struct bar_tag{
int zzz;
} bar_t;

typedef struct foo_tag {
bar_t yyy;
} foo_t;


Since a.h was included in b.h the foo_t was defined before bar_t.

In 1.3 I later noticed that we solved it like this
header_wrapped(root, current) =
(root == current || basename(current) == "a.h")

Would be good wit a validation of the generated api.jl directly after it is created.

@Gnimuc
Copy link
Member

Gnimuc commented Nov 30, 2021

Yeah. In Julia 1.3(Clang.jl v0.14-), tag dependencies are not correctly handled. That's why I wrote this package in v0.14.

julia> using Clang.Generators

julia> args = get_default_args("x86_64-linux-gnu")
5-element Vector{String}:
 "-isystem/Users/gnimuc/.julia/ar" ⋯ 56 bytes ⋯ "/x86_64-linux-gnu/4.8.5/include"
 "-isystem/Users/gnimuc/.julia/ar" ⋯ 62 bytes ⋯ "4-linux-gnu/4.8.5/include-fixed"
 "-isystem/Users/gnimuc/.julia/ar" ⋯ 42 bytes ⋯ "e3d045/x86_64-linux-gnu/include"
 "-isystem/Users/gnimuc/.julia/ar" ⋯ 55 bytes ⋯ "-linux-gnu/sys-root/usr/include"
 "--target=x86_64-unknown-linux-gnu"

shell> cat a.h
typedef struct foo_tag foo_t;

shell> cat b.h
#include "a.h"

typedef struct bar_tag{
int zzz;
} bar_t;

typedef struct foo_tag {
bar_t yyy;
} foo_t;

julia> ctx = create_context("b.h", args);
[ Info: Found dependent header: a.h
[ Info: Parsing headers...

julia> build!(ctx)
[ Info: Processing header: b.h
[ Info: Building the DAG...
┌ Warning: default libname: ":libxxx" is being used, did you forget to set `library_name` in the toml file?
└ @ Clang.Generators ~/.julia/dev/Clang/src/generator/audit.jl:16
[ Info: Emit Julia expressions...
struct bar_tag
    zzz::Cint
end

const bar_t = bar_tag

struct foo_tag
    yyy::bar_t
end

const foo_t = foo_tag

[ Info: Done!
Context(...)

@Gnimuc Gnimuc closed this as completed Nov 30, 2021
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