Coverage for src/rejuvenation/refactor_with_nested_compositions.py: 94%

49 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-16 16:04 +0000

1# Copyright (C) 2024-2026 TNO 

2# 

3# Licensed under the terms specified in the LICENSE file in the root directory 

4# of this repository, also available at: 

5# https://github.com/TNO/Renaissance.Py/blob/main/LICENSE 

6 

7"""AI: Example script demonstrating nested pattern replacements when rewriting C source.""" 

8 

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

10# It specifically showcases nested replacements and multiple patterns. 

11import textwrap 

12 

13from renaissance.integrations.clang import ClangASTNode, CPatternFactory 

14from renaissance.syntax_tree import ASTFactory, ASTRewriter, ASTShower 

15from renaissance.syntax_tree.ast_finder import find_semantic_kind 

16from renaissance.syntax_tree.match_finder import find_all 

17from renaissance.syntax_tree.semantic_kind import SemanticKind 

18 

19example_code = """ 

20void f1(int a, int b, int c); 

21void f2(int a, int c); 

22void f(){ 

23 const int a = 1; 

24 const int b = 2; 

25 int isAOne = a==1; 

26 int c = 0, d=0; 

27 if (a==1) { 

28 d++; 

29 if(a==1){ 

30 d++; 

31 c=d; 

32 f1(a,b,c); 

33 } 

34 } 

35 if (a==2) { 

36 c++; 

37 f1(a,b,c); 

38 } 

39 f1(a,b,c); 

40} 

41""".strip() 

42 

43 

44def refactor_with_nested_compositions(args): 

45 """AI: Demonstrate nested pattern composition by rewriting calls inside matched if-statement bodies.""" 

46 # the first argument is the code to be parsed 

47 code = args[1] if len(args) > 1 else "" 

48 

49 # Create a factory args from the command line are passed to the factory for example -I/usr/include 

50 factory = ASTFactory(ClangASTNode, args if not code else args[1:]) 

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

52 # create translation unit 

53 atu = factory.create(code) if code else factory.create_from_text(example_code, "example.c") 

54 # create a pattern factory atu is passed to the pattern factory for use of all # includes, #defines and declarations 

55 pattern_factory = CPatternFactory(factory, atu) 

56 # create a pattern that matches an if statement with a==1 as the condition and a block of statements as the body 

57 # the type is important so it's declared as const int a 

58 pattern1 = pattern_factory.create_statements("if(a==1){$$stmts;}", extra_declarations=["const int a;"]) 

59 # for pattern 2 we create a fully functional c snippet with a call to f1 

60 # note that the f1 declaration is derived from the atu 

61 pattern2 = pattern_factory.create("int $a,$b,$c; void fff() {f1($a,$b,$c);}") 

62 ASTShower.show_node(pattern1[0], include_properties=True) 

63 

64 # we only want to search the call expression as a pattern so it's searched using the kind 

65 pattern2 = find_semantic_kind(pattern2, SemanticKind.CALL) 

66 

67 # the replacement code strip indent is used to be agnostic to the indentation of the replacement 

68 pattern1replacement = textwrap.dedent(""" 

69 //changed if expr to const 

70 if(isAOne){ 

71 $$stmts; 

72 }""") 

73 

74 pattern2replacement = "\n//changed function f1 to f2\nf2($a,$c);" 

75 

76 # show node and patterns enable include properties to show the properties of the nodes 

77 include_properties = True 

78 ASTShower.show_node(atu, include_properties) 

79 ASTShower.show_node(pattern1[0], include_properties) 

80 ASTShower.show_node(pattern2[0], include_properties) 

81 

82 result1 = None 

83 while atu: 

84 # create an ASTRewriter 

85 rewriter = ASTRewriter(atu) 

86 

87 def raw(nodes): 

88 res = "" 

89 for node in nodes: 

90 res += node.text 

91 return res + "\n" 

92 

93 # create a refactoring that use different replacement code for different patterns 

94 def refactor(match1): 

95 print(f"peek: f{match1.signature}") 

96 if match1.patterns == pattern1: 

97 replacement_text = pattern1replacement 

98 for repl_snippet in match1.expansions: 

99 replacement_text = replacement_text.replace(repl_snippet, raw(match1.expansions[repl_snippet])) 

100 else: 

101 replacement_text = pattern2replacement 

102 for repl_snippet in match1.expansions: 

103 replacement_text = replacement_text.replace(repl_snippet, match1.expansions[repl_snippet][0].signature) 

104 return rewriter.replace(replacement_text, match1.nodes) 

105 

106 # search matches for pattern1 and pattern2 and replace them using the refactor function 

107 for match in find_all(atu.children, pattern1, pattern2): 

108 refactor(match) 

109 

110 # print the rewritten code 

111 result1 = rewriter.apply_to_string() 

112 atu = factory.create_from_text(result1, "example.c") if rewriter.has_changed() else None 

113 return result1 

114 

115 

116if __name__ == "__main__": 

117 import sys 

118 

119 result = refactor_with_nested_compositions(sys.argv) 

120 print(result)