From cb7e223b636be823b8739eade87853ed1242d47c Mon Sep 17 00:00:00 2001 From: disinvite Date: Sun, 29 Oct 2023 18:51:02 -0400 Subject: [PATCH] Find bad comments in one pass, add awareness of TEMPLATE --- tools/checkorder/checkorder.py | 16 ++++------------ tools/isledecomp/isledecomp/parser/parser.py | 8 ++++++-- tools/isledecomp/isledecomp/parser/util.py | 12 +++++------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/tools/checkorder/checkorder.py b/tools/checkorder/checkorder.py index e1172290..b4c94707 100644 --- a/tools/checkorder/checkorder.py +++ b/tools/checkorder/checkorder.py @@ -5,7 +5,6 @@ from isledecomp.dir import walk_source_dir from isledecomp.parser import find_code_blocks from isledecomp.parser.util import ( - match_offset_comment, is_exact_offset_comment ) @@ -16,16 +15,6 @@ def sig_truncate(sig: str) -> str: return f"{sig[:47]}{'...' if len(sig) >= 50 else ''}" -def get_inexact_offset_comments(stream: TextIO) -> [tuple]: - """Read the file stream and return the line number and string - for any offset comments that don't exactly match the template.""" - return ([ - (line_no, line.strip()) - for line_no, line in enumerate(stream) - if match_offset_comment(line) and not is_exact_offset_comment(line) - ]) - - def check_file(filename: str, verbose: bool = False) -> bool: """Open and read the given file, then check whether the code blocks are in order. If verbose, print each block.""" @@ -35,7 +24,10 @@ def check_file(filename: str, verbose: bool = False) -> bool: # TODO: Should combine these checks if/when we refactor. # This is just for simplicity / proof of concept. f.seek(os.SEEK_SET, 0) - bad_comments = get_inexact_offset_comments(f) + + bad_comments = [(block.start_line, block.offset_comment) + for block in code_blocks + if not is_exact_offset_comment(block.offset_comment)] just_offsets = [block.offset for block in code_blocks] sorted_offsets = sorted(just_offsets) diff --git a/tools/isledecomp/isledecomp/parser/parser.py b/tools/isledecomp/isledecomp/parser/parser.py index 1a6421de..d8aa6fbf 100644 --- a/tools/isledecomp/isledecomp/parser/parser.py +++ b/tools/isledecomp/isledecomp/parser/parser.py @@ -26,6 +26,7 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]: blocks = [] offset = None + offset_comment = None function_sig = None start_line = None state = ReaderState.WANT_OFFSET @@ -39,7 +40,8 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]: block = CodeBlock(offset=offset, signature=function_sig, start_line=start_line, - end_line=line_no) + end_line=line_no, + offset_comment=offset_comment) blocks.append(block) state = ReaderState.WANT_OFFSET @@ -50,7 +52,8 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]: block = CodeBlock(offset=offset, signature=function_sig, start_line=start_line, - end_line=line_no - 1) + end_line=line_no - 1, + offset_comment=offset_comment) blocks.append(block) state = ReaderState.WANT_OFFSET @@ -67,6 +70,7 @@ def find_code_blocks(stream: TextIO) -> List[CodeBlock]: match = match_offset_comment(line) if match is not None: offset = int(match, 16) + offset_comment = line.strip() start_line = line_no state = ReaderState.WANT_SIG diff --git a/tools/isledecomp/isledecomp/parser/util.py b/tools/isledecomp/isledecomp/parser/util.py index d93cb013..d69b6c2e 100644 --- a/tools/isledecomp/isledecomp/parser/util.py +++ b/tools/isledecomp/isledecomp/parser/util.py @@ -4,20 +4,18 @@ CodeBlock = namedtuple('CodeBlock', - ['offset', 'signature', 'start_line', 'end_line']) - - -FunctionOffset = namedtuple('FunctionOffset', - ['raw', 'address', 'is_stub']) + ['offset', 'signature', 'start_line', 'end_line', + 'offset_comment']) # To match a reasonable variance of formatting for the offset comment -offsetCommentRegex = re.compile(r'//\s*OFFSET:\s*\w+\s+(?:0x)?([a-f0-9]+)', +offsetCommentRegex = re.compile(r'//\s*OFFSET:\s*\w+\s+(?:0x)?([a-f0-9]+)(\s+STUB)?', flags=re.I) # To match the exact syntax (text upper case, hex lower case, with spaces) # that is used in most places -offsetCommentExactRegex = re.compile(r'^// OFFSET: [A-Z0-9]+ (0x[a-f0-9]+)(?: STUB)?$') +# TODO: template and stub mutually exclusive? +offsetCommentExactRegex = re.compile(r'^// OFFSET: [A-Z0-9]+ (0x[a-f0-9]+)(?: STUB| TEMPLATE)?$') def is_blank_or_comment(line: str) -> bool: