Skip to content

Commit

Permalink
Switch scanner CPPDEFINES replacement algorithm
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
mwichmann committed Dec 14, 2024
1 parent 0cb8737 commit 3cda270
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
3 changes: 0 additions & 3 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions SCons/Scanner/C.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3cda270

Please sign in to comment.