fix: clang_macro_fallback with header_contents() and implicit --target#3353
Open
zRedShift wants to merge 3 commits intorust-lang:mainfrom
Open
fix: clang_macro_fallback with header_contents() and implicit --target#3353zRedShift wants to merge 3 commits intorust-lang:mainfrom
clang_macro_fallback with header_contents() and implicit --target#3353zRedShift wants to merge 3 commits intorust-lang:mainfrom
Conversation
Fixes rust-lang#3351 clang_macro_fallback() silently produced no output when headers were provided via header_contents() instead of header(). This happened because try_ensure_fallback_translation_unit() only looked at input_headers (populated by .header()), returning None when it was empty. Fix: materialize input_header_contents to disk in the fallback build directory so clang can consume them for PCH compilation. Both input_headers and input_header_contents are now included in the fallback PCH, so mixed .header() + .header_contents() setups work. The materialized paths are sanitized (roots stripped, ".." resolved) to prevent escaping the build directory, and tracked in FallbackTranslationUnit for cleanup on drop. Also stop using std::mem::take on input_header_contents in Builder::generate(), preserving the data for the fallback TU (and fixing a pre-existing bug where serialize_items in codegen couldn't see header contents). Also store the original user-provided name in input_header_contents at header_contents() time so the fallback can reconstruct relative paths without depending on the current working directory.
Fixes rust-lang#3352 When bindgen infers the target triple from the Rust target (i.e., no explicit --target in clang_args), it inserts --target= into clang_args at Bindings::generate() time. However, fallback_clang_args was already populated from clang_args earlier in Builder::generate(), before this insertion. This meant the fallback translation unit used for clang_macro_fallback would use the host architecture instead of the target architecture. For cross-compilation, this caused sizeof-dependent macros (e.g., ioctl constants using _IOR/_IOW) to evaluate with the host's struct layouts instead of the target's, producing silently wrong values. Fix: when inserting the inferred target into clang_args, also insert it into fallback_clang_args.
19158d9 to
a499369
Compare
clang 9's clang_Cursor_Evaluate returns CXEval_UnExposed instead of CXEval_Int for macro-expanded expressions loaded through a PCH, so clang_macro_fallback produces no constants on that version. The existing header-based fallback tests already handle this with clang-9-specific empty expectation files; apply the same pattern to the new inline tests by skipping when libclang 9 is detected.
a499369 to
e9fcc7c
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #3351 and #3352.
Two bugs in
clang_macro_fallback:header_contents()silently skipped (clang_macro_fallback()silently skipped when usingheader_contents()#3351):try_ensure_fallback_translation_unit()only checkedinput_headers(populated by.header()). When headers were provided via.header_contents(),input_headerswas empty, the fallback returnedNone, and every macro that needed it was silently dropped. Additionally,Builder::generate()usedstd::mem::take()oninput_header_contents, so even if the fallback checked it, the data was already gone.Implicit
--targetnot propagated (clang_macro_fallbackdoesn't propagate implicit--targetto fallback translation unit #3352):fallback_clang_argswas populated fromclang_argsinBuilder::generate()beforeBindings::generate()inserted the inferred--target=triple. Cross-compilation would evaluatesizeof-dependent macros (e.g. ioctl constants via_IOR/_IOW) with the host's struct layouts instead of the target's.