From 3cda270b4fa252743e615711c0f91de1a4f66e32 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Fri, 13 Dec 2024 15:01:39 -0700 Subject: [PATCH] Switch scanner CPPDEFINES replacement algorithm Now using the "modified" approach from the PR discussion: unroll the dict comprehension and as we process replacements keep track if changes were made, rather than doing the relatively more expensive dict-vs-dict comparison at the end of each loop. Signed-off-by: Mats Wichmann --- CHANGES.txt | 3 --- SCons/Scanner/C.py | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 2609355a9..9bf0edfc5 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -167,9 +167,6 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER their values taken from the default in the variable description (if a variable was set to the same value as the default in one of the input sources, it is not included in this list). - - The C scanner now does (limited) macro replacement on the values in - CPPDEFINES, to improve results of conditional source file inclusion - (issue #4523). - The (optional) C Conditional Scanner now does limited macro replacement on the contents of CPPDEFINES, to improve finding deps that are conditionally included. Previously replacement was only diff --git a/SCons/Scanner/C.py b/SCons/Scanner/C.py index e111778c9..1d7e101e4 100644 --- a/SCons/Scanner/C.py +++ b/SCons/Scanner/C.py @@ -114,8 +114,19 @@ def _replace(mapping: Dict) -> Dict: old_ns = mapping loops = 0 while loops < 5: # don't recurse forever in case there's circular data - ns = {k: old_ns[v] if v in old_ns else v for k, v in old_ns.items()} - if old_ns == ns: + # this was originally written as a dict comprehension, but unrolling + # lets us add a finer-grained check for whether another loop is + # needed, rather than comparing two dicts to see if one changed. + again = False + ns = {} + for k, v in old_ns.items(): + if v in old_ns: + ns[k] = old_ns[v] + if not again and ns[k] != v: + again = True + else: + ns[k] = v + if not again: break old_ns = ns loops += 1