Skip to content

Instantly share code, notes, and snippets.

@kalebo
Created May 3, 2021 20:59
Show Gist options
  • Save kalebo/9696a4b0cb0e042bb922b5b3f1d1c6d3 to your computer and use it in GitHub Desktop.
Save kalebo/9696a4b0cb0e042bb922b5b3f1d1c6d3 to your computer and use it in GitHub Desktop.
Finds the include path for probematic files in a DAG from CL output
import sys
from pathlib import Path
from functools import partial, reduce
from pprint import pprint
eval_path = partial(reduce, lambda d, k: d[k])
def extract_includes(output_path):
results = []
with open(output_path, 'r', encoding='utf-16') as f:
while True:
line = f.readline()
if not line:
break
_, sep, include = line.partition("Note: including file:")
if include:
results.append(include)
return results
def build_tree(includes):
tree = dict()
cur_path = []
def level(inc_line):
return (len(inc_line) - len(inc_line.lstrip(' '))) -1 # there is a leading space for all
for i in includes:
lvl = level(i)
if lvl > len(cur_path):
pass # We don't need to back out anything on the path
elif lvl == len(cur_path):
if cur_path: # handle root sibling case
cur_path.pop()
elif lvl < len(cur_path):
cur_path = cur_path[:lvl - 1]
key = i.strip()
eval_path(cur_path, tree).setdefault(key, {})
cur_path.append(key)
return tree
test = """\
level 1
level 2
level 2.1
level 2.1.1
level 3
level 3.1
level 3.2
level 3.2.1""".splitlines()
def dfs(tree, pattern):
results = []
_dfs(tree, pattern, [], results)
return results
def _dfs(tree, pattern, path, results):
if path and pattern in path[-1]:
results.append(path)
for k in eval_path(path, tree).keys():
_dfs(tree, pattern, [*path, k], results)
if __name__ == "__main__":
cl_output = Path(sys.argv[1])
includes = extract_includes(cl_output)
#ttree = build_tree(test)
itree = build_tree(includes)
pprint(dfs(itree, 'dtype'))
pprint(dfs(itree, 'system_category_win32'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment