Pragma, Preprocessor: add "beforeInclude" hook, extend "once"#1010
Conversation
This logs when macros get #undef'ed, useful when debugging guards, etc.
|
This was discovered during our migration of Ghostty to Zig 0.16.0 (current PR is ghostty-org/ghostty#12726). It appears that the Managed to isolate the case to what is seen in the test here. A reproduction in clang seems to be fine, so I'm guessing this is generally the expected behavior. Let me know if this needs anything! |
|
The test looks good. The fix works as a targeted solution to the problem; it would be nice if the pragma system itself could express this sort of thing (as it stands, a consumer of aro can provide their own pragmas, but they would not be able to provide a custom pragma that behaved the same as I'm imagining something like another I don't think implementing that is a blocker, especially since this issue is preventing use on a popular real-world library, just a thought for how we could improve the pragma system. |
|
@ehaas I think this would be a trivial thing (or at least not a complicated thing) to implement to start out. I'll update the PR in a bit! |
6792610 to
76e397e
Compare
76e397e to
53c59c7
Compare
|
@ehaas PR updated - commentary in the commit and PR description! |
53c59c7 to
0a310f1
Compare
This adds the "beforeInclude" pragma hook, which fires before a file is included. Its current and only implementation is in the "once" pragma, which has been extended to use it to ensure that "#pragma once" causes future inclusions of a file to be skipped outright, versus processed until the pragma is hit again. This prevents scenarios where the directive has not been placed at the top of the file. This is a practice that is carried out in at least the GTK headers, and the current control flow causes inner-inclusion guards to be hit when they should not be. Note that since this is a deep-preprocessor event, it has not been added to the compilation-scoped PragmaEvent set, and just implemented as a hook local in the include function. If more event hooks get added in the future, it might be a good idea to review the event system to allow for scope-agnostic event types and payloads.
0a310f1 to
e24416e
Compare
Co-authored-by: Veikka Tuominen <git@vexu.eu>
|
Thanks to the both of you! |
This adds the
beforeIncludepragma hook, which fires before a file is included. Its current and only implementation is in the "once" pragma, which has been extended to use it to ensure that#pragma oncecauses future inclusions of a file to be skipped outright, versus processed until the pragma is hit again.This prevents scenarios where the directive has not been placed at the top of the file. This is a practice that is carried out in at least the GTK headers, and the current control flow causes inner-inclusion guards
to be hit when they should not be.
Note that since this is a deep-preprocessor event, it has not been added to the compilation-scoped
PragmaEventset, and just implemented as a hook local in the include function. If more event hooks get added in the future, it might be a good idea to review the event system to allow for scope-agnostic event types and payloads.