@@ -4530,12 +4530,12 @@ fn (mut g FlatGen) collect_preserved_header_file(path string, include_dirs []str
45304530 }
45314531 g.preserved_header_files_seen[real_path] = true
45324532 text := os.read_file(real_path) or { return }
4533- possible_text := c_header_possible_active_text (text, g.c_flags, g.c99_mode, g.target)
4534- g.collect_inlined_c_structs(possible_text )
4535- g.collect_inlined_c_fns(possible_text )
4536- g.collect_inlined_c_declared_fns(possible_text )
4533+ active_text := c_header_definitely_active_text (text, g.c_flags, g.c99_mode, g.target)
4534+ g.collect_inlined_c_structs(active_text )
4535+ g.collect_inlined_c_fns(active_text )
4536+ g.collect_inlined_c_declared_fns(active_text )
45374537 mut in_block_comment := false
4538- for line in possible_text .split_into_lines() {
4538+ for line in active_text .split_into_lines() {
45394539 clean, next_in_block_comment := c_preprocessor_directive_scan_line(line, in_block_comment)
45404540 in_block_comment = next_in_block_comment
45414541 if c_directive_name(clean) !in ['include', 'import'] {
@@ -4559,17 +4559,17 @@ fn (mut g FlatGen) collect_preserved_header_file(path string, include_dirs []str
45594559 }
45604560}
45614561
4562- // c_header_possible_active_text removes declarations from preprocessor branches
4563- // known to be inactive for the current target and C flags. Unknown branches stay
4564- // in the scan so a declaration that the real compiler may see remains authoritative .
4565- fn c_header_possible_active_text (text string, flags []string, c99_mode bool, target pref.Target) string {
4562+ // c_header_definitely_active_text retains declarations only from preprocessor
4563+ // branches known to be active for the current target and C flags. A declaration
4564+ // in an unresolved branch cannot safely suppress a generated C prototype .
4565+ fn c_header_definitely_active_text (text string, flags []string, c99_mode bool, target pref.Target) string {
45664566 mut defined := map[string]bool{}
45674567 mut undefined := map[string]bool{}
45684568 mut uncertain := map[string]bool{}
4569- // A preserved header inherits macros from its including source and parent
4570- // headers. That context is not carried through this lightweight recursive
4571- // scan, so an otherwise unknown macro must keep both branches possible .
4572- mut external_macros_possible := true
4569+ // Preinclude headers begin before generated source declarations. An earlier
4570+ // nested include can still introduce unknown macros, which makes later
4571+ // conditions unresolved and therefore unsuitable for prototype suppression .
4572+ mut external_macros_possible := false
45734573 mut i := 0
45744574 for i < flags.len {
45754575 clean := trimmed_space(flags[i])
@@ -4685,19 +4685,17 @@ fn c_header_possible_active_text(text string, flags []string, c99_mode bool, tar
46854685 output.writeln('')
46864686 continue
46874687 }
4688- mut possibly_active := true
46894688 mut definitely_active := true
46904689 for depth in 0 .. condition_known.len {
46914690 if condition_known[depth] && !condition_active[depth] {
4692- possibly_active = false
46934691 definitely_active = false
46944692 break
46954693 }
46964694 if !condition_known[depth] {
46974695 definitely_active = false
46984696 }
46994697 }
4700- if !possibly_active {
4698+ if !definitely_active {
47014699 output.writeln('')
47024700 continue
47034701 }
@@ -4717,10 +4715,6 @@ fn c_header_possible_active_text(text string, flags []string, c99_mode bool, tar
47174715 defined.delete(macro_name)
47184716 undefined[macro_name] = true
47194717 }
4720- } else {
4721- defined.delete(macro_name)
4722- undefined.delete(macro_name)
4723- uncertain[macro_name] = true
47244718 }
47254719 }
47264720 }
@@ -4732,8 +4726,8 @@ fn c_header_possible_active_text(text string, flags []string, c99_mode bool, tar
47324726fn c_preprocessor_ifdef_macro_state(name string, defined map[string]bool, undefined map[string]bool, uncertain map[string]bool, external_macros_possible bool, strict_iso_mode bool, target pref.Target) (bool, bool) {
47334727 if external_macros_possible && name !in defined && name !in undefined && name !in uncertain
47344728 && name !in ['__linux__', '__linux', 'linux', 'unix', '__APPLE__', '__MACH__', '_WIN32', '_WIN64', '__FreeBSD__', '__OpenBSD__', '__NetBSD__'] {
4735- // An earlier include can define an otherwise unknown macro. Keep both
4736- // branches in that case so the declaration scan never drops active code .
4729+ // An earlier include can define an otherwise unknown macro, so its state
4730+ // cannot be inferred by this lightweight scan.
47374731 return false, true
47384732 }
47394733 return c_preprocessor_macro_state(name, defined, undefined, uncertain, strict_iso_mode, target)
0 commit comments