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

Not emitting definition for INT8_MAX (from stdint) #446

Closed
Octogonapus opened this issue Oct 2, 2023 · 2 comments
Closed

Not emitting definition for INT8_MAX (from stdint) #446

Octogonapus opened this issue Oct 2, 2023 · 2 comments

Comments

@Octogonapus
Copy link
Contributor

Octogonapus commented Oct 2, 2023

I'm wrapping the AWS CRT packages and I have encountered some weird behavior. This C code:

// stdint.h is included indirectly through a number of headers which are also passed to Clang.jl
#define AWS_EVENT_STREAM_HEADER_NAME_LEN_MAX (INT8_MAX)

causes this wrapper to be generated:

const AWS_EVENT_STREAM_HEADER_NAME_LEN_MAX = INT8_MAX

There's no definition for INT8_MAX in the generated code. It would be great if Clang.jl could emit:

const AWS_EVENT_STREAM_HEADER_NAME_LEN_MAX = 127

or at least:

const INT8_MAX = 127
const AWS_EVENT_STREAM_HEADER_NAME_LEN_MAX = INT8_MAX

Is Clang.jl expected to fail in this case, or did I misconfigure something?

Clang.jl v0.17.6
Julia v1.9.3

@Octogonapus Octogonapus changed the title Not emitting definition for INT8_MAX (from stdlib) Not emitting definition for INT8_MAX (from stdint) Oct 2, 2023
@Gnimuc
Copy link
Member

Gnimuc commented Oct 3, 2023

Yes. It's technically impossible to do this because macro expanding is handled by the preprocessor, before the AST is created, and we can only extract info from the AST.1 The identifier INT8_MAX could be anything, it's legal to undef it and define it again. So, unless we implement a full-featured C preprocessor in Julia, it's hard to do it without any mistakes.

However, the best practice is much simpler. We can add const INT8_MAX = typemax(Int8) to the prologue file and do it once for all.

I know the ultimate solution is something like Rust's https://github.com/rust-lang/libc. But in practice, as Julia is niche at the moment, we really don't have that manpower. ;)

Footnotes

  1. to elaborate a bit more, the info is lost after the macro is "expanded" by the preprocessor. the compiler can only see 127, not INT8_MAX. The reason why Clang.jl can handle #define AWS_EVENT_STREAM_HEADER_NAME_LEN_MAX (INT8_MAX) is that it uses a heuristic way to parse the C code.)

@Octogonapus
Copy link
Contributor Author

Fair enough

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