From e11faa69ccdc2be8af147e1d2f3329a58a902ab3 Mon Sep 17 00:00:00 2001 From: John Pennycook Date: Wed, 4 Sep 2024 10:14:54 +0100 Subject: [PATCH 1/2] Remove unused walkers TreePrinter, SourcePrinter and PreprocessedSourcePrinter are not used by any other code and can therefore be removed. Signed-off-by: John Pennycook --- codebasin/walkers/source_printer.py | 94 ----------------------------- codebasin/walkers/tree_printer.py | 37 ------------ 2 files changed, 131 deletions(-) delete mode 100644 codebasin/walkers/source_printer.py delete mode 100644 codebasin/walkers/tree_printer.py diff --git a/codebasin/walkers/source_printer.py b/codebasin/walkers/source_printer.py deleted file mode 100644 index d4eba97..0000000 --- a/codebasin/walkers/source_printer.py +++ /dev/null @@ -1,94 +0,0 @@ -# Copyright (C) 2021-2024 Intel Corporation -# SPDX-License-Identifier: BSD-3-Clause - -from codebasin.preprocessor import ( - CodeNode, - DirectiveNode, - FileNode, - Lexer, - MacroExpander, -) -from codebasin.walkers.tree_walker import TreeWalker - - -class SourcePrinter(TreeWalker): - """ - TreeWalker that prints preprocessed source. - """ - - def __init__(self, _tree): - super().__init__(_tree, None) - - def walk(self): - """ - Walk the tree, printing each node. - """ - self.__print_nodes(self.tree.root) - - def __print_nodes(self, node): - """ - Print this specific node, then descend into its children nodes. - """ - if not isinstance(node, FileNode): - print("\n".join(node.spelling())) - - for child in node.children: - self.__print_nodes(child) - - -class PreprocessedSourcePrinter(TreeWalker): - """ - TreeWalker that prints preprocessed source code. - """ - - def __init__(self, _tree, _node_associations, _platform, _state, _expand): - super().__init__(_tree, _node_associations) - self.platform = _platform - self.state = _state - self.expand_macros = _expand - - def walk(self): - """ - Walk the tree, printing the result of each node after preprocessing. - """ - self.__print_nodes(self.tree.root, self._node_associations) - - def __print_nodes(self, _node, _map): - """ - Print this specific node, then descend into its children nodes. - """ - descend = True - if isinstance(_node, CodeNode): - association = _map[_node] - - # Re-evaluating the node during this walk is required to - # define/undefine macros appropriately - eval_args = { - "platform": self.platform, - "filename": self.tree.root.filename, - "state": self.state, - } - descend = _node.evaluate_for_platform(**eval_args) - expander = MacroExpander(self.platform) - if association and not isinstance(_node, DirectiveNode): - node_lines = _node.spelling() - output_lines = [] - for line in node_lines: - if self.expand_macros: - tokens = Lexer(line).tokenize() - expansion = expander.expand(tokens) - for tok in expansion: - if tok.prev_white: - output_lines.append(" ") - output_lines.append(str(tok)) - else: - output_lines.append(line) - output_lines.append("\n") - print("".join(output_lines)) - else: - # Replace unused code with whitespace - print() - - if descend: - for child in _node.children: - self.__print_nodes(child, _map) diff --git a/codebasin/walkers/tree_printer.py b/codebasin/walkers/tree_printer.py deleted file mode 100644 index 73d0054..0000000 --- a/codebasin/walkers/tree_printer.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright (C) 2019-2024 Intel Corporation -# SPDX-License-Identifier: BSD-3-Clause - -import logging - -from codebasin.walkers.tree_walker import TreeWalker - -log = logging.getLogger("codebasin") - - -class TreePrinter(TreeWalker): - """ - Specific TreeWalker that prints the nodes for the tree - (with appropriate indentation). - """ - - def walk(self): - """ - Walk the tree, printing each node. - """ - self.__print_nodes(self.tree.root, 0) - - def __print_nodes(self, node, level): - """ - Print this specific node, then descend into it's children nodes. - """ - association = self._node_associations[node] - if association: - platform = ", ".join(association) - else: - platform = "" - - spacing = " " * (level) - print(f"{spacing}{node} -- Platforms: {platform}") - - for child in node.children: - self.__print_nodes(child, level + 1) From e9aa310c828aa097a096d37bdb864fd14b492337 Mon Sep 17 00:00:00 2001 From: John Pennycook Date: Wed, 4 Sep 2024 15:06:17 +0100 Subject: [PATCH 2/2] Remove Exporter TreeWalker Exporter is not used by any other code and can therefore be removed. Although we intend to reintroduce export functionality as part of the planned cbicov utility, that utility should not use the TreeWalker abstraction. Signed-off-by: John Pennycook --- codebasin/walkers/exporter.py | 57 ----------------------------------- 1 file changed, 57 deletions(-) delete mode 100644 codebasin/walkers/exporter.py diff --git a/codebasin/walkers/exporter.py b/codebasin/walkers/exporter.py deleted file mode 100644 index d5db7b8..0000000 --- a/codebasin/walkers/exporter.py +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright (C) 2019-2024 Intel Corporation -# SPDX-License-Identifier: BSD-3-Clause - -import collections -import logging - -from codebasin import util -from codebasin.preprocessor import CodeNode, FileNode -from codebasin.walkers.tree_walker import TreeWalker - -log = logging.getLogger("codebasin") - - -class Exporter(TreeWalker): - """ - Build a per-platform list of mappings. - """ - - def __init__(self, codebase): - super().__init__(None, None) - self.codebase = codebase - self.exports = None - - def walk(self, state): - self.exports = collections.defaultdict( - lambda: collections.defaultdict(list), - ) - for fn in state.get_filenames(): - hashed_fn = util.compute_file_hash(fn) - self._export_node( - hashed_fn, - state.get_tree(fn).root, - state.get_map(fn), - ) - return self.exports - - def _export_node(self, _filename, _node, _map): - # Do not export files that the user does not consider to be part of - # the codebase - if isinstance(_node, FileNode) and _node.filename not in self.codebase: - return - - if isinstance(_node, CodeNode): - association = _map[_node] - for p in frozenset(association): - start_line = _node.start_line - end_line = _node.end_line - num_lines = _node.num_lines - self.exports[p][_filename].append( - (start_line, end_line, num_lines), - ) - - next_filename = _filename - if isinstance(_node, FileNode): - next_filename = util.compute_file_hash(_node.filename) - for child in _node.children: - self._export_node(next_filename, child, _map)