!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:
@classmethod
def remove_if0_blocks(cls,code):
def c_remove_if0_blocks(cls,code):
stack = []
lines = code.split('\n')
result = []
@ -23,7 +23,7 @@ class CodeStrip:
return '\n'.join(result)
@classmethod
def remove_comments_and_spaces(cls, code):
def c_remove_comments_and_spaces(cls, code):
pattern_single_comment = r'//.*'
pattern_multi_comment = r'/\*[\s\S]*?\*/'
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)
return code.strip()
@classmethod
def code_strip(cls, code):
code=cls.remove_comments_and_spaces(code)
code=cls.remove_if0_blocks(code)
def code_strip(cls,code,type):
if type == "c" or type == "cpp":
code=cls.c_remove_comments_and_spaces(code)
code=cls.c_remove_if0_blocks(code)
return code
else:
return ""
with open('source.c', 'r') as file:
code = file.read()
processed_code = CodeStrip.code_strip(code)
with open('source_processed.c', 'w') as file:
file.write(processed_code)
#with open('source.c', 'r') as file:
# code = file.read()
#processed_code = CodeStrip.code_strip(code,"c")
#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=""
return patch_changes
def strip_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("#########################################")
def fuzzy_compare(source_code,patch):
return False
def linebyline_compare(source_code,patch):
return False
def tokens_compare(source_file,source_code,patch):
Debug.info(source_file)
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
def tokens_compare(code_type,source_code,patch):
tokens=Tokenize(patch,code_type).get_tokens()
Debug.info(tokens)
return False
# 简单匹配
def simple_compare(source_code, patch):
#Debug.debug("#####################src######################")
#Debug.debug(source_code)
#Debug.debug("#####################patch######################")
#Debug.debug(patch)
if patch in source_code:
Debug.debug("patch find")
return True
@ -130,24 +111,54 @@ def simple_compare(source_code, patch):
Debug.debug("patch not find")
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):
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:
file_content = file.read()
for code_change in patch["changes"]:
start_line = code_change["start_line"]
num_lines = code_change["num_lines"]
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):
return True
elif strip_compare(file_content,fragment):
return True
elif tokens_compare(source_file,file_content,fragment):
return True
elif linebyline_compare(file_content,fragment):
return True
else:
continue
file_content_strip,fragment_strip=source_strip(code_type,file_content,fragment)
if file_content_strip and fragment_strip:
if simple_compare(file_content_strip,fragment_strip):
continue
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 True
def sacn_dir(project_dir,patch_changes):
# 遍历项目目录下的所有文件
#0 初始状态 1找到补丁 -1未找到补丁