-
Notifications
You must be signed in to change notification settings - Fork 378
Use C++ fixed-type enumeration syntax under C23 (or higher) as well #1156
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
913df52
Use C++ fixed-type enumeration syntax under C23 (or higher) as well
garrison 172da53
Fix typo
garrison 8b8a203
Fix correctness and update tests
garrison 6d6f2ad
Apply suggestion from @garrison
garrison abaef72
Merge branch 'main' into c23-fixed-enums
garrison File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -680,15 +680,20 @@ impl Enum { | |
| } | ||
| write!(out, " {tag_name}"); | ||
|
|
||
| if config.cpp_compatible_c() { | ||
| out.new_line(); | ||
| out.write("#ifdef __cplusplus"); | ||
| out.new_line(); | ||
| write!(out, " : {prim}"); | ||
| out.new_line(); | ||
| out.write("#endif // __cplusplus"); | ||
| out.new_line(); | ||
| } | ||
| // Emit typed enum syntax (valid in C23 or later or C++) | ||
| let cond = if config.cpp_compatible_c() { | ||
| "defined(__cplusplus) || __STDC_VERSION__ >= 202311L" | ||
| } else { | ||
| "__STDC_VERSION__ >= 202311L" | ||
| }; | ||
|
|
||
| out.new_line(); | ||
| write!(out, "#if {cond}"); | ||
| out.new_line(); | ||
| write!(out, " : {prim}"); | ||
| out.new_line(); | ||
| write!(out, "#endif // {cond}"); | ||
| out.new_line(); | ||
| } else { | ||
| if config.style.generate_typedef() { | ||
| out.write("typedef "); | ||
|
|
@@ -759,17 +764,39 @@ impl Enum { | |
| } | ||
|
|
||
| // Emit typedef specifying the tag enum's size if necessary. | ||
| // In C++ enums can "inherit" from numeric types (`enum E: uint8_t { ... }`), | ||
| // but in C `typedef uint8_t E` is the only way to give a fixed size to `E`. | ||
| // In C++ or C23 enums can "inherit" from numeric types (`enum E: uint8_t { ... }`), | ||
| // but in older versions of C `typedef uint8_t E` is the only way to give a fixed size to `E`. | ||
| if let Some(prim) = size { | ||
| if config.cpp_compatible_c() { | ||
| out.new_line_if_not_start(); | ||
| out.write("#ifndef __cplusplus"); | ||
| } | ||
|
|
||
| if config.language != Language::Cxx { | ||
| if config.language == Language::C { | ||
| out.new_line(); | ||
| out.write("#if __STDC_VERSION__ >= 202311L"); | ||
|
|
||
| out.new_line(); | ||
| write!( | ||
| out, | ||
| "{} enum {} {};", | ||
| config.language.typedef(), | ||
| tag_name, | ||
| tag_name | ||
| ); | ||
|
|
||
| out.new_line(); | ||
| out.write("#else"); | ||
| } | ||
|
|
||
| out.new_line(); | ||
| write!(out, "{} {} {};", config.language.typedef(), prim, tag_name); | ||
|
|
||
| if config.language == Language::C { | ||
| out.new_line(); | ||
| out.write("#endif // __STDC_VERSION__ >= 202311L"); | ||
| } | ||
|
Comment on lines
+776
to
+799
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this change has a very similar issue -- I believe this will cause multiple-defines errors if our build script strips out the |
||
| } | ||
|
|
||
| if config.cpp_compatible_c() { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, unconditionally outputting this causes issues with some tools that parse header files.
In particular, Python's
cffihas very limited support for preprocessor macros and does not support#ifconditionals at all. For some conditionals this is trivial to work around (such as header guards -- you can remove them before gettingcffito parse them) but conditionals like this do not have a nice workaround.If you try to use them, you get this error:
cyphar/libpathrs#382 is an example of how this can cause build failures in a downstream project -- our build scripts strip out any non-
#definemacros (which is the only thingcffisupports, and even then it's quite limited) so the error you actually get looks more like:But the core issue is the same. It would be nice if this output could be made configurable so that we can continue to use
cbindgen-generated headers withcffi. Otherwise we will have to pin an older version...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be ok with reviewing a patch to make this opt-in / opt-out.
Though, have you considered
unifdefperhaps? I think that'd work?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not a bad idea (and I must admit I hadn't considered it) -- unfortunately shelling out to
unifdeffrom the setup script is a little dodgy, it might not work for some packaging setups (distros would probably be okay with the workaround but I think it would be problematic for Python packaging and people doing local from-source installs).It would be easier to implement that in Python, at which point it might actually end up being less effort to just add support for basic
#if/#ifdefconditionals incffi...What would you think about a patch which let you specify a C version to generate (to not make this switch too special-purpose) and so if you pick C99 mode (for instance) you don't get these
#ifconditionals but if you pick C23 mode you do? (To be fair, reading it back this is a little funky -- if you are picking C23 mode you wouldn't need the conditional. Maybe a "max" and "min" C version? 🤷.)Or would you prefer something more like an option for "limited C" and "full-on C"? (That would make things easier for me in the future, I must admit! 😅)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think something like
allow_fixed_size_c_enumsor something, defaulting totrueand avoiding the ifdefs would be a bit more consistent with how we deal with other features likeconstexprin c++.It'd be a breaking change but it'd remove the ifdefs altogether from the generated code? I guess the ifdef route was taken here mostly because we already had a
__cplusplusif def for C++-compatible-C but...