Skip to content

Commit 83ff0a8

Browse files
committed
Add main function
1 parent fed6799 commit 83ff0a8

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

main.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from mainTool.CPG import *
2+
import argparse
3+
import sys
4+
import os
5+
import json
6+
7+
8+
suffixs: Set[str] = {'c', 'cxx', 'c++', 'cpp', 'cc', 'cp', 'hxx', 'h', 'hpp'}
9+
10+
def show_files(base_path, all_files: List[str]):
11+
"""
12+
遍历当前目录所有py文件及文件夹
13+
:param path:
14+
:param all_files:
15+
"""
16+
file_list = os.listdir(base_path)
17+
# 准备循环判断每个元素是否是文件夹还是文件,是文件的话,把名称传入list,是文件夹的话,递归
18+
for file in file_list:
19+
# 利用os.path.join()方法取得路径全名,并存入cur_path变量,否则每次只能遍历一层目录
20+
cur_path = os.path.join(base_path, file)
21+
# 判断是否是文件夹
22+
if os.path.isdir(cur_path):
23+
show_files(cur_path, all_files)
24+
else:
25+
suffix = file.split('.')[-1]
26+
if suffix in suffixs:
27+
all_files.append(cur_path)
28+
29+
def _argparse():
30+
parser = argparse.ArgumentParser(description='user guide for CppCodeAnalyzer')
31+
parser.add_argument('-f', '--file', required=False, type=str,
32+
help='specify c file to be parsed')
33+
parser.add_argument('-d', '--dir', required=False, type=str,
34+
help='specify dir which contains source files to be parsed')
35+
parser.add_argument('-c', '--calleeInfos', required=False, type=str,
36+
help='specify dir which contains source files to be parsed',
37+
default="resources/calleeInfos.json")
38+
parser.add_argument('-o', '--output', required=False, type=str, help='specify dir which store parsing results')
39+
return parser.parse_args()
40+
41+
if __name__ == '__main__':
42+
parser = _argparse() # main这里引入命令行参数函数
43+
44+
# 参数解析过程
45+
if parser.file is None and parser.dir is None:
46+
sys.stderr.write("please specify file or dir to parse")
47+
exit(-1)
48+
elif parser.file is not None and parser.dir is not None:
49+
sys.stderr.write("please do not specify file and dir in the same time")
50+
exit(-1)
51+
52+
potential_output_dir = "" # 默认解析结果存放的文件夹位置
53+
file2parse: List[str] = list()
54+
# 如果指定检测一个文件,那么默认解析结果存放在输入文件同一个文件夹下
55+
if parser.file is not None:
56+
parts: List[str] = parser.file.split(os.path.sep)
57+
potential_output_dir = os.path.sep.join(parts[:-1])
58+
file2parse.append(parser.file)
59+
# 指定检测文件夹,那么解析结果存放在该文件夹下
60+
elif parser.dir is not None:
61+
potential_output_dir = parser.dir
62+
show_files(potential_output_dir, file2parse)
63+
64+
if len(file2parse) == 0:
65+
sys.stderr.write("the dir must at least contains one c file")
66+
exit(-1)
67+
68+
output_dir = ""
69+
# 没有设定解析结果存放位置的话就用默认值
70+
if parser.output is not None:
71+
output_dir = parser.output
72+
else:
73+
output_dir = potential_output_dir
74+
75+
# 加载callee信息
76+
calleeInfs: Dict[str, Dict] = json.load(open(parser.calleeInfos, 'r', encoding='utf-8'))
77+
calleeInfos: CalleeInfos = initialCalleeInfos(calleeInfs)
78+
79+
converter: CFGToUDGConverter = CFGToUDGConverter()
80+
astAnalyzer: ASTDefUseAnalyzer = ASTDefUseAnalyzer()
81+
astAnalyzer.calleeInfos = calleeInfos
82+
converter.astAnalyzer = astAnalyzer
83+
defUseConverter: CFGAndUDGToDefUseCFG = CFGAndUDGToDefUseCFG()
84+
ddgCreator: DDGCreator = DDGCreator()
85+
86+
# start analysing
87+
result_cpgs: List[CPG] = list()
88+
successful_count = 0
89+
90+
for i, file_name in enumerate(file2parse):
91+
print(f"{i} / {len(file2parse)} -- {file_name}")
92+
# ddgCreator.clear()
93+
try:
94+
cpgs: List[CPG] = fileParse(file_name, converter, defUseConverter, ddgCreator)
95+
result_cpgs.extend(cpgs)
96+
successful_count += 1
97+
except Exception as e:
98+
print(f"syntax error might appear in {file_name}")
99+
100+
output_file = output_dir + "result.json"
101+
json_cpgs: List[Dict] = [cpg.toSerializedJson() for cpg in result_cpgs]
102+
json.dump(json_cpgs, open(output_file, 'w', encoding='utf-8'), indent=2)

mainTool/CPG.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ def fileParse(fileName: str, udgConverter: CFGToUDGConverter,
200200
def initialCalleeInfos(calleeInfs: Dict) -> CalleeInfos:
201201
calleeInfos: CalleeInfos = CalleeInfos()
202202

203-
defInfos = calleeInfs["ArgDef"]
203+
defInfos = calleeInfs["ArgDefs"]
204204
for funcName, argNs in defInfos.items():
205205
for argN in argNs:
206206
calleeInfos.addArgDef(funcName, argN)
207207

208-
useInfos = calleeInfs["ArgUse"]
208+
useInfos = calleeInfs["ArgUses"]
209209
for funcName, argNs in useInfos.items():
210210
for argN in argNs:
211211
calleeInfos.addArgUse(funcName, argN)

0 commit comments

Comments
 (0)