@@ -29,8 +29,6 @@ use quote::ToTokens;
2929use std:: borrow:: Cow ;
3030use std:: cell:: { Cell , RefCell } ;
3131use std:: collections:: { BTreeSet , HashMap as StdHashMap } ;
32- use std:: fs:: OpenOptions ;
33- use std:: io:: Write ;
3432use std:: mem;
3533use std:: path:: Path ;
3634
@@ -2054,8 +2052,11 @@ If you encounter an error missing from this list, please file an issue or a PR!"
20542052
20552053 let mut header_names_to_compile = Vec :: new ( ) ;
20562054 let mut header_paths = Vec :: new ( ) ;
2057- let mut header_contents = String :: new ( ) ;
2058- for input_header in & self . options . input_headers {
2055+ let mut header_includes = Vec :: new ( ) ;
2056+ let single_header = self . options ( ) . input_headers . last ( ) . cloned ( ) ?;
2057+ for input_header in & self . options . input_headers
2058+ [ ..self . options . input_headers . len ( ) - 1 ]
2059+ {
20592060 let path = Path :: new ( input_header. as_ref ( ) ) ;
20602061 if let Some ( header_path) = path. parent ( ) {
20612062 if header_path == Path :: new ( "" ) {
@@ -2067,50 +2068,32 @@ If you encounter an error missing from this list, please file an issue or a PR!"
20672068 header_paths. push ( "." ) ;
20682069 }
20692070 let header_name = path. file_name ( ) ?. to_str ( ) ?;
2071+ header_includes. push ( header_name. to_string ( ) ) ;
20702072 header_names_to_compile
20712073 . push ( header_name. split ( ".h" ) . next ( ) ?. to_string ( ) ) ;
2072- header_contents +=
2073- format ! ( "\n #include <{header_name}>" ) . as_str ( ) ;
20742074 }
2075- let header_to_precompile = format ! (
2075+ let pch = format ! (
20762076 "{}/{}" ,
20772077 match self . options( ) . clang_macro_fallback_build_dir {
20782078 Some ( ref path) => path. as_os_str( ) . to_str( ) ?,
20792079 None => "." ,
20802080 } ,
2081- header_names_to_compile. join( "-" ) + "-precompile.h"
2081+ header_names_to_compile. join( "-" ) + "-precompile.h.pch "
20822082 ) ;
2083- let pch = header_to_precompile. clone ( ) + ".pch" ;
2084-
2085- let mut header_to_precompile_file = OpenOptions :: new ( )
2086- . create ( true )
2087- . truncate ( true )
2088- . write ( true )
2089- . open ( & header_to_precompile)
2090- . ok ( ) ?;
2091- header_to_precompile_file
2092- . write_all ( header_contents. as_bytes ( ) )
2093- . ok ( ) ?;
2094-
2095- let mut c_args = Vec :: new ( ) ;
2083+
2084+ let mut c_args = self . options . fallback_clang_args . clone ( ) ;
20962085 c_args. push ( "-x" . to_string ( ) . into_boxed_str ( ) ) ;
20972086 c_args. push ( "c-header" . to_string ( ) . into_boxed_str ( ) ) ;
20982087 for header_path in header_paths {
20992088 c_args. push ( format ! ( "-I{header_path}" ) . into_boxed_str ( ) ) ;
21002089 }
2101- c_args. extend (
2102- self . options
2103- . clang_args
2104- . iter ( )
2105- . filter ( |next| {
2106- !self . options . input_headers . contains ( next) &&
2107- next. as_ref ( ) != "-include"
2108- } )
2109- . cloned ( ) ,
2110- ) ;
2090+ for header_include in header_includes {
2091+ c_args. push ( "-include" . to_string ( ) . into_boxed_str ( ) ) ;
2092+ c_args. push ( header_include. into_boxed_str ( ) ) ;
2093+ }
21112094 let mut tu = clang:: TranslationUnit :: parse (
21122095 & index,
2113- & header_to_precompile ,
2096+ & single_header ,
21142097 & c_args,
21152098 & [ ] ,
21162099 clang_sys:: CXTranslationUnit_ForSerialization ,
@@ -2121,23 +2104,18 @@ If you encounter an error missing from this list, please file an issue or a PR!"
21212104 "-include-pch" . to_string( ) . into_boxed_str( ) ,
21222105 pch. clone( ) . into_boxed_str( ) ,
21232106 ] ;
2124- c_args. extend (
2125- self . options
2126- . clang_args
2127- . clone ( )
2128- . iter ( )
2129- . filter ( |next| {
2130- !self . options . input_headers . contains ( next) &&
2131- next. as_ref ( ) != "-include"
2132- } )
2133- . cloned ( ) ,
2134- ) ;
2135- self . fallback_tu = Some ( clang:: FallbackTranslationUnit :: new (
2136- file,
2137- header_to_precompile,
2138- pch,
2139- & c_args,
2140- ) ?) ;
2107+ let mut skip_next = false ;
2108+ for arg in self . options . fallback_clang_args . iter ( ) {
2109+ if arg. as_ref ( ) == "-include" {
2110+ skip_next = true ;
2111+ } else if skip_next {
2112+ skip_next = false ;
2113+ } else {
2114+ c_args. push ( arg. clone ( ) )
2115+ }
2116+ }
2117+ self . fallback_tu =
2118+ Some ( clang:: FallbackTranslationUnit :: new ( file, pch, & c_args) ?) ;
21412119 }
21422120
21432121 self . fallback_tu . as_mut ( )
0 commit comments