Coverage for src/rejuvenation/remove_unused_variable.py: 85%
34 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-10-02 13:12 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-10-02 13:12 +0000
1"""AI: Example script demonstrating removal of unused variables from C source via the cleanup refactoring recipe."""
3from more_itertools import flatten
5from renaissance.integrations.clang import ClangASTNode
6from renaissance.integrations.clang.clang_json_ast_node import ClangJsonASTNode
7from renaissance.integrations.clang.predicates import is_clang_compound_statement
8from renaissance.recipes import CleanupRefactoring
9from renaissance.syntax_tree import (
10 ASTFactory,
11 ASTNode,
12 ASTProcessor,
13 ASTRewriter,
14 ASTShower,
15)
16from renaissance.syntax_tree.ast_finder import find_nodes
17from renaissance.syntax_tree.semantic_kind import SemanticKind
19example_code = """
20 int a = 1;
21 int b = 2;
22 int c = 3;
23 int d = 4;
24 void x(int a) {
25 }
26 void f(){
27 int unused = 0;
28 int unused2 = 0; //must be removed
29 if (a==1) {
30 int unused = 0;
31 int unused2 = 0; //should be kept
32 int c = unused2;
33 x(c);
34 }
35 }
36 """
37expected_result_refactor = """
38 int a = 1;
39 int b = 2;
40 int c = 3;
41 int d = 4;
42 void x(int a) {
43 }
44 void f(){
45 if (a==1) {
46 int unused2 = 0; //should be kept
47 int c = unused2;
48 x(c);
49 }
50 }""".strip()
53def remove_unused_variable_using_refactor_method(node_type1: type[ASTNode]):
54 """AI: Remove unused variables from the example code via `CleanupRefactoring.remove_unused_variables`."""
55 factory = ASTFactory(node_type1, [])
56 # create translation unit
57 atu = factory.create_from_text(example_code, "test.c")
58 # create a Refactor
59 refactor = ASTProcessor(atu, factory, in_memory=True)
61 CleanupRefactoring.remove_unused_variables(refactor)
62 result = refactor.apply_to_string().strip()
63 # print the rewritten code
64 print(f"Using cleanup refactoring results {node_type1.__name__}:")
65 print(result)
67 return result, expected_result_refactor
70def remove_unused_variable_low_level(node_type1: type[ASTNode]):
71 """AI: Remove unused variables from the example code using low-level pattern matching directly."""
72 factory = ASTFactory(ClangJsonASTNode, [])
73 # Create a pattern factory (using the factory (hence also its args)
74 # create translation unit
75 atu = factory.create_from_text(example_code, "test.c")
77 # create an ASTRewriter
78 rewriter = ASTRewriter(atu)
80 ASTShower.show_node(atu)
81 # search matches and replace them
82 funcs = flatten(
83 find_nodes(func, lambda node: node.semantic_kind is SemanticKind.DECLARATION)
84 for func in find_nodes(atu, is_clang_compound_statement)
85 )
86 [rewriter.remove(node.parent, True, True) for node in funcs if len(node.referenced_by) == 0]
88 # print the rewritten code
89 print(f"Low level results using {node_type1.__name__}:")
90 result = rewriter.apply_to_string().strip()
91 print(result)
92 return result, expected_result_refactor
95if __name__ == "__main__":
96 for node_type in [ClangASTNode, ClangJsonASTNode]:
97 remove_unused_variable_low_level(node_type)
98 remove_unused_variable_using_refactor_method(node_type)