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
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
from tree_sitter import Language, Parser
import tree_sitter_rust as tsrust
import sys
import os
import re
import shutil

RS_LANGUAGE = Language(tsrust.language(), "rust")
parser = Parser()
parser.set_language(RS_LANGUAGE)
# call_functions = []

# 获取impl定义
query_impl_defin_text = """
(
    (impl_item) @impl.defin
)
"""


# 获取[test]定义


query_test_defin_text = """
(
    (attribute_item) @test.defin
)
"""

# 获取函数定义
query_function_defin_text = """
(
    (function_item) @function.defin
)
"""

query_function_name_text = """
(function_item
  (identifier) @function.name)
"""

# 获取macro定义
query_macro_defin_text = """
(
    (macro_definition) @macro.defin
)
"""

query_macro_name_text = """
(macro_definition
  (identifier) @macro.name)
"""

# 获取struct定义
query_struct_defin_text = """
(
    (struct_item) @struct.defin
)
"""
query_struct_name_text = """
(struct_item
    (type_identifier) @struct.name
)
"""

# 调用的函数
query_call_function_text = """
(
    (call_expression) @function.call
)
"""
query_call_function_name_text = """
(
  (field_identifier) @function.call_name
)
"""



# 调用的macro
query_call_macro_text = """
(
    (macro_invocation) @macro.call
)
"""
query_call_macro_name_text = """
(macro_invocation
  (identifier) @macro.call_name)
"""

# test case中如果出于macro调用里的函数会从call_expression变成identifier
query_call_macro_call_function_text = """
(
    (identifier) @macro.call_function
)
"""

# 调用的数据类型
query_call_vars_type_text = """
(
    (type_identifier) @call_vars.type
)
"""

# 调用的变量
query_call_vars_text = """
(
    (expression_statement) @call_vars.exp
)
(
    (let_declaration) @call_vars.let
)
"""

query_call_vars_name_text = """
(
    (field_identifier) @call_vars.let
)
"""

query_import_text = """
(
    (use_declaration) @use.name
)
"""

# Create a query object
query_test_defin = RS_LANGUAGE.query(query_test_defin_text)
query_impl_defin = RS_LANGUAGE.query(query_impl_defin_text)
query_function_defin = RS_LANGUAGE.query(query_function_defin_text)
query_function_name = RS_LANGUAGE.query(query_function_name_text)
query_macro_defin = RS_LANGUAGE.query(query_macro_defin_text)
query_macro_name = RS_LANGUAGE.query(query_macro_name_text)
query_struct_defin = RS_LANGUAGE.query(query_struct_defin_text)
query_struct_name = RS_LANGUAGE.query(query_struct_name_text)
query_call_function = RS_LANGUAGE.query(query_call_function_text)
query_call_function_name = RS_LANGUAGE.query(query_call_function_name_text)
query_call_macro = RS_LANGUAGE.query(query_call_macro_text)
query_call_macro_name = RS_LANGUAGE.query(query_call_macro_name_text)
query_call_macro_call_function = RS_LANGUAGE.query(query_call_macro_call_function_text)
query_call_vars = RS_LANGUAGE.query(query_call_vars_text)
query_call_vars_name = RS_LANGUAGE.query(query_call_vars_name_text)
query_call_vars_type = RS_LANGUAGE.query(query_call_vars_type_text)
query_import = RS_LANGUAGE.query(query_import_text)



def traverse_call(node, source_code, call_functions):
    if node.type == "call":
        call_functions.append(node)
        function_call_code = source_code[node.start_byte:node.end_byte].decode('utf-8')
        # print(f"Function call (traversal): {function_call_code}")
    for child in node.children:
        traverse_call(child, source_code, call_functions)


def traverse(node, source_code, depth=0):
    # Get the node text
    node_text = source_code[node.start_byte:node.end_byte].decode('utf-8')
    for child in node.children:
        traverse(child, source_code, depth + 1)

# Execute the query to get the captures


def get_source_code(target_file_path):

    with open(target_file_path, 'r', encoding='utf-8', errors='ignore') as input_file:
        source_code = input_file.read()

    # 转化成bytes!!否则在出现中文注释时,根据偏移获得对应内容会出错
    source_code = bytes(source_code, "utf-8")

    return source_code

def get_source_code_and_path(target_file_path):
    with open(target_file_path, 'r', encoding='utf-8', errors='ignore') as input_file:
        content = input_file.read()
        
        content = content.split("------")[0]

        pattern = r'<path>(.*?)</path>'
        function_path = re.findall(pattern, content, re.DOTALL)[0].strip()

        pattern = r'<function>(.*?)</function>'
        source_code = re.findall(pattern, content, re.DOTALL)[0].strip()

    # 转化成bytes!!否则在出现中文注释时,根据偏移获得对应内容会出错
    source_code = bytes(source_code, "utf-8")

    return source_code, function_path

def get_call_macro(node, source_code):
    call_macro_names = set()
    # 获取依赖数据类型
    call_macro_captures = query_call_macro.captures(node)
    for call_macro_capture in call_macro_captures:
        call_macro_node , _ = call_macro_capture

        # 获取call macro
        call_macro_name_capture = query_call_macro_name.captures(call_macro_node)
        try:
            call_macro_name_node , _ = call_macro_name_capture[0]
            call_macro_code = source_code[call_macro_name_node.start_byte:call_macro_name_node.end_byte].decode()
            call_macro_names.add(call_macro_code)
        except:
            pass

        # 获取call macro中调用的函数
        call_macro_call_function_capture = query_call_macro_call_function.captures(call_macro_node)
        for call_macro_call_function_node, _ in call_macro_call_function_capture:
            call_macro_call_function_code = source_code[call_macro_call_function_node.start_byte:call_macro_call_function_node.end_byte].decode()
            call_macro_names.add(call_macro_call_function_code)

    return call_macro_names

def get_call_function(node, source_code):
    call_function_names = set()
    call_function_captures = query_call_function.captures(node)

    

    for call_function_capture in call_function_captures:
        call_function_node, _ = call_function_capture

        # call function
        call_function_var_code = source_code[call_function_node.start_byte:call_function_node.end_byte].decode()
        call_function_var_code = call_function_var_code.split("=")
        for call_function_var in call_function_var_code:
            if "(" in call_function_var and ")" in call_function_var:
                call_function_var = call_function_var.replace("\n", "")
                call_function_var = call_function_var.split("self.")[-1]
                pattern = r'([a-zA-Z_][a-zA-Z0-9_:]*)\.|([a-zA-Z_][a-zA-Z0-9_:]*\([^\)]*\))'
                call_function_var = re.findall(pattern, call_function_var)
                call_function = [match[1].split("(")[0] for match in call_function_var if match[1]]

                call_function_names.update(call_function)
            
    return call_function_names

def get_call_vars_type(node, source_code):
    call_vars_type_name = set()
    # 获取依赖数据类型
    vars_type_captures = query_call_vars_type.captures(node)
    for vars_type_capture in vars_type_captures:
        vars_type_node , _ = vars_type_capture
        vars_type_code = source_code[vars_type_node.start_byte:vars_type_node.end_byte].decode()
        call_vars_type_name.add(vars_type_code)
    return call_vars_type_name

def get_file_function_dependency(target_file_path):
    
    Dependency_func = {}
    Dependency_vars = {}

    source_code = get_source_code(target_file_path)
    function_path = target_file_path
    tree = parser.parse(source_code)


    test_function_captures = query_test_defin.captures(tree.root_node)
    for capture in test_function_captures:
        node, _ = capture

        attribute_name = source_code[node.start_byte:node.end_byte].decode()
        if "test" not in attribute_name:
            continue

        function_node = node.next_sibling 
        function_name_captures = query_function_name.captures(function_node)
        for capture in function_name_captures:
            function_name_node, _ = capture
            function_name = source_code[function_name_node.start_byte:function_name_node.end_byte].decode()
            # 先获取全部函数再减去impl函数
            if function_name in Dependency_func.keys():
                continue
            
            # 从测试文件中提取覆盖的函数
            # if not function_name.startswith("test"):
                # print(function_name)
                # continue

            call_function_names = get_call_function(function_node, source_code)
            call_macro_names = get_call_macro(function_node, source_code)

            call_function_names.update(call_macro_names)
            Dependency_func[function_name] = call_function_names


    return Dependency_func, Dependency_vars, function_path


def filtered_os_walk(top):
    for root, dirs, files in os.walk(top):
        # 过滤掉名字以"."开头的目录和名字包含"test"的目录
        dirs[:] = [d for d in dirs if not d.startswith('.') and 'test' not in d]
        yield root, dirs, files

def get_function_defin(node, source_code, function_name_to_code, project_functions, file_path):
    function_defin_captures = query_function_defin.captures(node)
    function_names = set()
    for function_defin_capture in function_defin_captures:
        function_defin_node , _ = function_defin_capture
        function_code = source_code[function_defin_node.start_byte:function_defin_node.end_byte].decode()
        function_name_captures = query_function_name.captures(function_defin_node)
        function_name_node, _ = function_name_captures[0]
        function_name = source_code[function_name_node.start_byte:function_name_node.end_byte].decode()

        function_names.add(function_name)
        # 以@为分隔符,将函数的文件路径加入,防止在单个项目中存在多个同名函数
        function_name_to_code[file_path + "@" + function_name] = function_code
    project_functions[file_path] = function_names

def get_struct_defin(node, source_code, struct_name_to_code, project_structs, file_path):
    struct_names = set()
    struct_defin_captures = query_struct_defin.captures(node)
    for struct_defin_capture in struct_defin_captures:
        struct_defin_node , _ = struct_defin_capture
        struct_defin_code = source_code[struct_defin_node.start_byte:struct_defin_node.end_byte].decode()
        struct_name_captures = query_struct_name.captures(struct_defin_node)
        sturct_name_node, _ = struct_name_captures[0]
        sturct_name = source_code[sturct_name_node.start_byte:sturct_name_node.end_byte].decode()
        struct_names.add(sturct_name)
        struct_name_to_code[file_path + "@" + sturct_name] = struct_defin_code

    project_structs[file_path] = struct_names

def get_macro_defin(node, source_code, macro_name_to_code, project_macros, file_path):
    macro_names = set()
    macro_defin_captures = query_macro_defin.captures(node)
    for macro_defin_capture in macro_defin_captures:
        macro_defin_node , _ = macro_defin_capture
        macro_defin_code = source_code[macro_defin_node.start_byte:macro_defin_node.end_byte].decode()
        macro_name_captures = query_macro_name.captures(macro_defin_node)
        macro_name_node, _ = macro_name_captures[0]
        macro_name = source_code[macro_name_node.start_byte:macro_name_node.end_byte].decode()

        macro_names.add(macro_name)

        macro_name_to_code[file_path + "@" + macro_name] = macro_defin_code
    project_macros[file_path] = macro_names

# 读取项目
def get_project_functions(project_path):

    project_functions = {}
    project_structs = {}
    project_macros = {}

    function_name_to_code = {}
    struct_name_to_code = {}
    macro_name_to_code = {}

    project_imports = {}
    project_vars = {}

    # 手动添加
    project_structs["IString"] = ["IString"]
    struct_name_to_code["IString@IString"] = "pub type IString = ::string_cache::Atom<IStringStaticSet>;"

    # for current_path, _, files in filtered_os_walk(project_path):
    for current_path, _, files in os.walk(project_path):
        for file in files:
            # if "test" in file:
            #     continue
            if file.endswith(".rs"):

                file_path = os.path.join(current_path, file)
                source_code = get_source_code(file_path)
                tree = parser.parse(source_code)
                
                # get function defin
                get_function_defin(tree.root_node, source_code, function_name_to_code, project_functions, file_path)
                 
                # get struct defin
                get_struct_defin(tree.root_node, source_code, struct_name_to_code, project_structs, file_path)
                
                # get macro defin
                get_macro_defin(tree.root_node, source_code, macro_name_to_code, project_macros, file_path)

                # get import
                import_codes = []
                import_captures = query_import.captures(tree.root_node)
                for import_capture in import_captures:
                    import_node, _ = import_capture
                    import_code = source_code[import_node.start_byte:import_node.end_byte].decode().split("use")[-1].strip()
                    import_codes.append(import_code)
                project_imports[file_path] = import_codes

    # 将macro归入function
    for file_path, macros in project_macros.items():
        if file_path in project_functions.keys():
            project_functions[file_path].update(macros)  
    function_name_to_code = {**macro_name_to_code, **function_name_to_code}

    return project_imports, project_functions, function_name_to_code, project_structs, struct_name_to_code

def match(project_functions, dependency_funcs, function_path, project_imports):
    # 依次进行匹配
    project_dependency_function = {}

    for target_function , call_functions in dependency_funcs.items():
        dependencies = []
        for call_function in call_functions:
            key = False
            # 先从同个文件中找
            for file_path, potential_functions in project_functions.items():
                if file_path == function_path:
                    if call_function in potential_functions:
                        dependencies.append(function_path + "@" + call_function)
                        key = True
                    break
            if key:
                continue

            # 从import中找
            for project_file_path, file_imports in project_imports.items():
                # 获得目标文件的对应import
                if project_file_path == function_path:
                    for file_path, potential_functions in project_functions.items():
                        # 获取文件名simple_path,判断该文件是否在目标文件的import中
                        # 如果在file_import中,那么目标文件可以使用该文件内的函数
                        simple_path = file_path.split("/")[-1].split(".")[0].strip()
                        
                        for file_import in file_imports:
                            if simple_path in file_import:
                                if call_function in potential_functions:
                                    dependencies.append(file_path + "@" + call_function)
                                    key = True
                                break
                        if key:
                            break
                    break
            if key:
                continue

        project_dependency_function[target_function] = dependencies

    return project_dependency_function





project_name = sys.argv[1]
target_lang = sys.argv[2]

project_path = os.path.join("projects", project_name, target_lang)
target_files_path = project_path
project_imports, project_functions, function_name_to_code, project_structs, struct_name_to_code  = get_project_functions(project_path)



unit_test_function = set()
unitTest_cnt = 0
for current_path, _, target_files in os.walk(target_files_path):
    for target_file in target_files:
        if not target_file.endswith(".rs"):
            continue
        
        
        dependency_funcs, dependency_vars, function_path = get_file_function_dependency(os.path.join(current_path, target_file))
        # Dependency, function_path = get_file_function_dependency(target_file)
        
        if not dependency_funcs:
            continue
        
        unitTest_cnt += len(dependency_funcs)
        result_function = match(project_functions, dependency_funcs, function_path, project_imports)
        

        
        for function, call_functions in result_function.items():
            unit_test_function.update(call_functions)

# 将有测试用例的函数复制到目标文件夹中
cnt = 0
functions = set()
function_with_unit_test = set()
function_files = os.listdir(f"functions/{project_name}/{target_lang}")

if not os.path.exists(f"functions_with_unitTest/{project_name}/{target_lang}"):
    os.makedirs(f"functions_with_unitTest/{project_name}/{target_lang}")

for function_file in function_files:
    with open(os.path.join(f"functions/{project_name}/{target_lang}", function_file), 'r', encoding='utf-8', errors='ignore') as input_file:
        content = input_file.read()
    

    pattern = r'<path>(.*?)</path>'
    function_path = re.findall(pattern, content, re.DOTALL)[0].strip()

    pattern = r'<function>(.*?)</function>'
    source_code = re.findall(pattern, content, re.DOTALL)[0].strip()

    pattern = r'fn (.*?)(\<.*?\>)?\('
    function_name = re.findall(pattern, source_code, re.DOTALL)[0][0].strip()

    function_name = function_path + "@" + function_name

    if function_name in unit_test_function:
        functions.add(function_name)
        cnt += 1
        shutil.copy(os.path.join(f"functions/{project_name}/{target_lang}", function_file), os.path.join(f"functions_with_unitTest/{project_name}/{target_lang}", function_file))