Coverage for src / rejuvenation / remove_unused_variable.py: 91%

33 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-09-09 14:04 +0000

1# This script demonstrates the use of the syntax_tree library to parse and rewrite C code. 

2# It specifically showcases the replacement of if-else statements with ternary operators. 

3from more_itertools import flatten 

4 

5from renaissance.integrations.clang import ClangASTNode 

6from renaissance.integrations.clang.clang_json_ast_node import ClangJsonASTNode 

7from renaissance.integrations.types import CompoundStatement, VariableDef 

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_ast_type 

17 

18example_code = """ 

19 int a = 1; 

20 int b = 2; 

21 int c = 3; 

22 int d = 4; 

23 void x(int a) { 

24 } 

25 void f(){ 

26 int unused = 0; 

27 int unused2 = 0; //must be removed 

28 if (a==1) { 

29 int unused = 0; 

30 int unused2 = 0; //should be kept 

31 int c = unused2; 

32 x(c); 

33 } 

34 } 

35 """ 

36expected_result_refactor = """ 

37 int a = 1; 

38 int b = 2; 

39 int c = 3; 

40 int d = 4; 

41 void x(int a) { 

42 } 

43 void f(){ 

44 if (a==1) { 

45 int unused2 = 0; //should be kept 

46 int c = unused2; 

47 x(c); 

48 } 

49 }""".strip() 

50 

51 

52def remove_unused_variable_using_refactor_method(node_type1: type[ASTNode]): 

53 factory = ASTFactory(node_type1, []) 

54 # create translation unit 

55 atu = factory.create_from_text(example_code, "test.c") 

56 # create a Refactor 

57 refactor = ASTProcessor(atu, factory, in_memory=True) 

58 

59 CleanupRefactoring.remove_unused_variables(refactor) 

60 result = refactor.apply_to_string().strip() 

61 # print the rewritten code 

62 print(f"Using cleanup refactoring results {node_type1.__name__}:") 

63 print(result) 

64 

65 return result, expected_result_refactor 

66 

67 

68def remove_unused_variable_low_level(node_type1: type[ASTNode]): 

69 factory = ASTFactory(ClangJsonASTNode, []) 

70 # Create a pattern factory (using the factory (hence also its args) 

71 # create translation unit 

72 atu = factory.create_from_text(example_code, "test.c") 

73 

74 # create an ASTRewriter 

75 rewriter = ASTRewriter(atu) 

76 

77 ASTShower.show_node(atu) 

78 # search matches and replace them 

79 funcs = flatten(find_ast_type(func, VariableDef) for func in (find_ast_type(atu, CompoundStatement))) 

80 [rewriter.remove(node.parent, True, True) for node in funcs if len(node.referenced_by) == 0] 

81 

82 # print the rewritten code 

83 print(f"Low level results using {node_type1.__name__}:") 

84 result = rewriter.apply_to_string().strip() 

85 print(result) 

86 return result, expected_result_refactor 

87 

88 

89if __name__ == "__main__": 

90 for node_type in [ClangASTNode, ClangJsonASTNode]: 

91 remove_unused_variable_low_level(node_type) 

92 remove_unused_variable_using_refactor_method(node_type)