!406 add strip compare in pedt

Merge pull request !406 from xujian/master
This commit is contained in:
xujian 2024-06-17 07:34:11 +00:00 committed by Gitee
commit bc0ffe11b5
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 56 additions and 44 deletions

View File

@ -2,7 +2,7 @@ import re
class CodeStrip: class CodeStrip:
@classmethod @classmethod
def remove_if0_blocks(cls,code): def c_remove_if0_blocks(cls,code):
stack = [] stack = []
lines = code.split('\n') lines = code.split('\n')
result = [] result = []
@ -23,7 +23,7 @@ class CodeStrip:
return '\n'.join(result) return '\n'.join(result)
@classmethod @classmethod
def remove_comments_and_spaces(cls, code): def c_remove_comments_and_spaces(cls, code):
pattern_single_comment = r'//.*' pattern_single_comment = r'//.*'
pattern_multi_comment = r'/\*[\s\S]*?\*/' pattern_multi_comment = r'/\*[\s\S]*?\*/'
code = re.sub(pattern_single_comment, '', code, flags=re.MULTILINE) code = re.sub(pattern_single_comment, '', code, flags=re.MULTILINE)
@ -31,15 +31,16 @@ class CodeStrip:
code = re.sub(r'\n\s*\n', '\n', code) code = re.sub(r'\n\s*\n', '\n', code)
return code.strip() return code.strip()
@classmethod @classmethod
def code_strip(cls, code): def code_strip(cls,code,type):
code=cls.remove_comments_and_spaces(code) if type == "c" or type == "cpp":
code=cls.remove_if0_blocks(code) code=cls.c_remove_comments_and_spaces(code)
code=cls.c_remove_if0_blocks(code)
return code return code
else:
return ""
with open('source.c', 'r') as file: #with open('source.c', 'r') as file:
code = file.read() # code = file.read()
#processed_code = CodeStrip.code_strip(code,"c")
processed_code = CodeStrip.code_strip(code) #with open('source_processed.c', 'w') as file:
# file.write(processed_code)
with open('source_processed.c', 'w') as file:
file.write(processed_code)

View File

@ -92,37 +92,18 @@ def parse_patch(patch_file):
patch_fragment="" patch_fragment=""
return patch_changes return patch_changes
def strip_compare(source_code,patch): def fuzzy_compare(source_code,patch):
Debug.info("################before strip##############")
Debug.info(source_code)
Debug.info("#########################################")
code=CodeStrip.remove_comments_and_spaces(source_code)
source_code=CodeStrip.remove_if0_blocks(source_code)
Debug.info("################after strip##############")
Debug.info(source_code)
Debug.info("#########################################")
return False return False
def linebyline_compare(source_code,patch): def linebyline_compare(source_code,patch):
return False return False
def tokens_compare(source_file,source_code,patch): def tokens_compare(code_type,source_code,patch):
Debug.info(source_file) tokens=Tokenize(patch,code_type).get_tokens()
if source_file.endswith(".c"):
tokens=Tokenize(patch,"c").get_tokens()
elif source_file.endswith(".cpp"):
tokens=Tokenize(patch,"cpp").get_tokens()
else:
return False
Debug.info(tokens) Debug.info(tokens)
return False return False
# 简单匹配
def simple_compare(source_code, patch): def simple_compare(source_code, patch):
#Debug.debug("#####################src######################")
#Debug.debug(source_code)
#Debug.debug("#####################patch######################")
#Debug.debug(patch)
if patch in source_code: if patch in source_code:
Debug.debug("patch find") Debug.debug("patch find")
return True return True
@ -130,24 +111,54 @@ def simple_compare(source_code, patch):
Debug.debug("patch not find") Debug.debug("patch not find")
return False return False
def source_strip(code_type,file_content,fragment):
file_content_strip=""
fragment_strip=""
file_content_strip=CodeStrip.code_strip(file_content,code_type)
fragment_strip=CodeStrip.code_strip(fragment,code_type)
return file_content_strip,fragment_strip
def compare_patch(source_file,patch): def compare_patch(source_file,patch):
code_type=""
if source_file.endswith(".c"):
code_type="c"
elif source_file.endswith(".cpp"):
code_type="cpp"
with open(source_file, 'r') as file: with open(source_file, 'r') as file:
file_content = file.read() file_content = file.read()
for code_change in patch["changes"]: for code_change in patch["changes"]:
start_line = code_change["start_line"] start_line = code_change["start_line"]
num_lines = code_change["num_lines"] num_lines = code_change["num_lines"]
fragment = code_change["patch_fragment"] fragment = code_change["patch_fragment"]
Debug.debug("#####################src######################")
Debug.debug(file_content)
Debug.debug("#####################src end######################")
Debug.debug("#####################patch######################")
Debug.debug(fragment)
Debug.debug("#####################patch end######################")
if simple_compare(file_content,fragment): if simple_compare(file_content,fragment):
return True continue
elif strip_compare(file_content,fragment):
return True file_content_strip,fragment_strip=source_strip(code_type,file_content,fragment)
elif tokens_compare(source_file,file_content,fragment):
return True if file_content_strip and fragment_strip:
elif linebyline_compare(file_content,fragment): if simple_compare(file_content_strip,fragment_strip):
return True continue
else: if code_type:
if tokens_compare(code_type,file_content_strip,fragment_strip):
continue
if linebyline_compare(file_content_strip,fragment_strip):
continue
if fuzzy_compare(file_content,fragment):
continue
return False return False
return True
def sacn_dir(project_dir,patch_changes): def sacn_dir(project_dir,patch_changes):
# 遍历项目目录下的所有文件 # 遍历项目目录下的所有文件
#0 初始状态 1找到补丁 -1未找到补丁 #0 初始状态 1找到补丁 -1未找到补丁