pylint cleanup, remove unused code

This commit is contained in:
disinvite 2023-12-01 18:29:20 -05:00
parent d87d665127
commit a4d92984da
8 changed files with 49 additions and 55 deletions

View File

@ -19,7 +19,6 @@ def check_file(filename: str, verbose: bool = False) -> bool:
with open(filename, "r", encoding="utf-8") as f: with open(filename, "r", encoding="utf-8") as f:
parser.read_lines(f) parser.read_lines(f)
just_offsets = [block.offset for block in parser.functions] just_offsets = [block.offset for block in parser.functions]
sorted_offsets = sorted(just_offsets) sorted_offsets = sorted(just_offsets)
file_out_of_order = just_offsets != sorted_offsets file_out_of_order = just_offsets != sorted_offsets
@ -46,7 +45,7 @@ def check_file(filename: str, verbose: bool = False) -> bool:
msg = " ".join( msg = " ".join(
[ [
" " if fun.offset > prev_offset else "!", " " if fun.offset > prev_offset else "!",
f"{block.offset:08x}", f"{fun.offset:08x}",
f"{fun.end_line - fun.line_number:4} lines", f"{fun.end_line - fun.line_number:4} lines",
f"{order_lookup[fun.offset]:3}", f"{order_lookup[fun.offset]:3}",
" ", " ",

View File

@ -1,5 +1,4 @@
from dataclasses import dataclass from dataclasses import dataclass
from enum import Enum
@dataclass @dataclass

View File

@ -1,6 +1,6 @@
# C++ file parser # C++ file parser
from typing import List, TextIO, Iterable from typing import List, Iterable, Iterator
from enum import Enum from enum import Enum
from .util import ( from .util import (
DecompMarker, DecompMarker,
@ -12,7 +12,6 @@
) )
from .node import ( from .node import (
ParserAlert, ParserAlert,
ParserNode,
ParserFunction, ParserFunction,
ParserVariable, ParserVariable,
ParserVtable, ParserVtable,
@ -64,9 +63,8 @@ def insert(self, marker: DecompMarker) -> bool:
self.markers[module] = (marker.type, marker.offset) self.markers[module] = (marker.type, marker.offset)
return False return False
def iter(self): def iter(self) -> Iterator[DecompMarker]:
for module in self.markers: for module, (marker_type, offset) in self.markers.items():
(marker_type, offset) = self.markers[module]
yield DecompMarker(marker_type, module, offset) yield DecompMarker(marker_type, module, offset)
def empty(self): def empty(self):
@ -74,27 +72,26 @@ def empty(self):
class DecompParser: class DecompParser:
# pylint: disable=too-many-instance-attributes
# Could combine output lists into a single list to get under the limit,
# but not right now
def __init__(self): def __init__(self):
self.fun_markers = MarkerDict() # The lists to be populated as we parse
self.var_markers = MarkerDict() self.functions: List[ParserFunction] = []
self.tbl_markers = MarkerDict() self.vtables: List[ParserVtable] = []
self.reset() self.variables: List[ParserVariable] = []
self.alerts: List[ParserAlert] = []
def reset(self):
# Output values
self.functions = []
self.vtables = []
self.variables = []
self.alerts = []
# Internal state machine stuff
self.line_number: int = 0 self.line_number: int = 0
self.state: ReaderState = ReaderState.SEARCH self.state: ReaderState = ReaderState.SEARCH
self.last_line: str = "" self.last_line: str = ""
self.fun_markers.empty()
self.var_markers.empty() # To allow for multiple markers where code is shared across different
self.tbl_markers.empty() # modules, save lists of compatible markers that appear in sequence
self.fun_markers = MarkerDict()
self.var_markers = MarkerDict()
self.tbl_markers = MarkerDict()
# To handle functions that are entirely indented (i.e. those defined # To handle functions that are entirely indented (i.e. those defined
# in class declarations), remember how many whitespace characters # in class declarations), remember how many whitespace characters
@ -102,7 +99,7 @@ def reset(self):
# This should give us the same or better accuracy for a well-formed file. # This should give us the same or better accuracy for a well-formed file.
# The alternative is counting the curly braces on each line # The alternative is counting the curly braces on each line
# but that's probably too cumbersome. # but that's probably too cumbersome.
self.curly_indent_stops = 0 self.curly_indent_stops: int = 0
# For non-synthetic functions, save the line number where the function begins # For non-synthetic functions, save the line number where the function begins
# (i.e. where we see the curly brace) along with the function signature. # (i.e. where we see the curly brace) along with the function signature.
@ -110,6 +107,25 @@ def reset(self):
self.function_start: int = 0 self.function_start: int = 0
self.function_sig: str = "" self.function_sig: str = ""
def reset(self):
self.functions = []
self.vtables = []
self.variables = []
self.alerts = []
self.line_number = 0
self.state = ReaderState.SEARCH
self.last_line = ""
self.fun_markers.empty()
self.var_markers.empty()
self.tbl_markers.empty()
self.curly_indent_stops = 0
self.function_start = 0
self.function_sig = ""
def _recover(self): def _recover(self):
"""We hit a syntax error and need to reset temp structures""" """We hit a syntax error and need to reset temp structures"""
self.state = ReaderState.SEARCH self.state = ReaderState.SEARCH
@ -340,19 +356,3 @@ def read_line(self, line: str):
def read_lines(self, lines: Iterable): def read_lines(self, lines: Iterable):
for line in lines: for line in lines:
self.read_line(line) self.read_line(line)
def find_code_blocks(stream: TextIO) -> List[ParserNode]:
"""Read the IO stream (file) line-by-line and give the following report:
Foreach code block (function) in the file, what are its starting and
ending line numbers, and what is the given offset in the original
binary. We expect the result to be ordered by line number because we
are reading the file from start to finish."""
# TODO: this will be replaced shortly. shim for now to avoid
# making more changes elsewhere
p = DecompParser()
for line in stream:
p.read_line(line)
return p.functions

View File

@ -1,7 +1,6 @@
# C++ Parser utility functions and data structures # C++ Parser utility functions and data structures
from __future__ import annotations # python <3.10 compatibility from __future__ import annotations # python <3.10 compatibility
import re import re
from typing import List
from collections import namedtuple from collections import namedtuple
DecompMarker = namedtuple("DecompMarker", ["type", "module", "offset"]) DecompMarker = namedtuple("DecompMarker", ["type", "module", "offset"])

View File

@ -3,12 +3,11 @@
ReaderState, ReaderState,
DecompParser, DecompParser,
) )
from isledecomp.parser.util import DecompMarker
from isledecomp.parser.error import ParserError from isledecomp.parser.error import ParserError
@pytest.fixture @pytest.fixture(name="parser")
def parser(): def fixture_parser():
return DecompParser() return DecompParser()

View File

@ -1,6 +1,6 @@
import os import os
import pytest
from typing import List, TextIO from typing import List, TextIO
import pytest
from isledecomp.parser import DecompParser from isledecomp.parser import DecompParser
from isledecomp.parser.node import ParserSymbol from isledecomp.parser.node import ParserSymbol
@ -20,8 +20,8 @@ def code_blocks_are_sorted(blocks: List[ParserSymbol]) -> bool:
return just_offsets == sorted(just_offsets) return just_offsets == sorted(just_offsets)
@pytest.fixture @pytest.fixture(name="parser")
def parser(): def fixture_parser():
return DecompParser() return DecompParser()

View File

@ -3,7 +3,6 @@
ReaderState as _rs, ReaderState as _rs,
DecompParser, DecompParser,
) )
from isledecomp.parser.util import DecompMarker
from isledecomp.parser.error import ParserError as _pe from isledecomp.parser.error import ParserError as _pe
# fmt: off # fmt: off
@ -35,28 +34,28 @@
(_rs.IN_TEMPLATE, "SYNTHETIC", _rs.IN_TEMPLATE, None), (_rs.IN_TEMPLATE, "SYNTHETIC", _rs.IN_TEMPLATE, None),
(_rs.IN_TEMPLATE, "TEMPLATE", _rs.IN_TEMPLATE, None), (_rs.IN_TEMPLATE, "TEMPLATE", _rs.IN_TEMPLATE, None),
(_rs.IN_TEMPLATE, "VTABLE", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_TEMPLATE, "VTABLE", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.WANT_CURLY, "FUNCTION", _rs.SEARCH, _pe.UNEXPECTED_MARKER), (_rs.WANT_CURLY, "FUNCTION", _rs.SEARCH, _pe.UNEXPECTED_MARKER),
(_rs.WANT_CURLY, "GLOBAL", _rs.SEARCH, _pe.UNEXPECTED_MARKER), (_rs.WANT_CURLY, "GLOBAL", _rs.SEARCH, _pe.UNEXPECTED_MARKER),
(_rs.WANT_CURLY, "STUB", _rs.SEARCH, _pe.UNEXPECTED_MARKER), (_rs.WANT_CURLY, "STUB", _rs.SEARCH, _pe.UNEXPECTED_MARKER),
(_rs.WANT_CURLY, "SYNTHETIC", _rs.SEARCH, _pe.UNEXPECTED_MARKER), (_rs.WANT_CURLY, "SYNTHETIC", _rs.SEARCH, _pe.UNEXPECTED_MARKER),
(_rs.WANT_CURLY, "TEMPLATE", _rs.SEARCH, _pe.UNEXPECTED_MARKER), (_rs.WANT_CURLY, "TEMPLATE", _rs.SEARCH, _pe.UNEXPECTED_MARKER),
(_rs.WANT_CURLY, "VTABLE", _rs.SEARCH, _pe.UNEXPECTED_MARKER), (_rs.WANT_CURLY, "VTABLE", _rs.SEARCH, _pe.UNEXPECTED_MARKER),
(_rs.IN_GLOBAL, "FUNCTION", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_GLOBAL, "FUNCTION", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.IN_GLOBAL, "GLOBAL", _rs.IN_GLOBAL, None), (_rs.IN_GLOBAL, "GLOBAL", _rs.IN_GLOBAL, None),
(_rs.IN_GLOBAL, "STUB", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_GLOBAL, "STUB", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.IN_GLOBAL, "SYNTHETIC", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_GLOBAL, "SYNTHETIC", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.IN_GLOBAL, "TEMPLATE", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_GLOBAL, "TEMPLATE", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.IN_GLOBAL, "VTABLE", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_GLOBAL, "VTABLE", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.IN_FUNC_GLOBAL, "FUNCTION", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_FUNC_GLOBAL, "FUNCTION", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.IN_FUNC_GLOBAL, "GLOBAL", _rs.IN_FUNC_GLOBAL, None), (_rs.IN_FUNC_GLOBAL, "GLOBAL", _rs.IN_FUNC_GLOBAL, None),
(_rs.IN_FUNC_GLOBAL, "STUB", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_FUNC_GLOBAL, "STUB", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.IN_FUNC_GLOBAL, "SYNTHETIC", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_FUNC_GLOBAL, "SYNTHETIC", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.IN_FUNC_GLOBAL, "TEMPLATE", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_FUNC_GLOBAL, "TEMPLATE", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.IN_FUNC_GLOBAL, "VTABLE", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_FUNC_GLOBAL, "VTABLE", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.IN_VTABLE, "FUNCTION", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_VTABLE, "FUNCTION", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.IN_VTABLE, "GLOBAL", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_VTABLE, "GLOBAL", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
(_rs.IN_VTABLE, "STUB", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER), (_rs.IN_VTABLE, "STUB", _rs.SEARCH, _pe.INCOMPATIBLE_MARKER),
@ -75,7 +74,8 @@ def test_state_change_by_marker(
): ):
p = DecompParser() p = DecompParser()
p.state = state p.state = state
p._handle_marker(DecompMarker(marker_type, "TEST", 0x1234)) mock_line = f"// {marker_type}: TEST 0x1234"
p.read_line(mock_line)
assert p.state == new_state assert p.state == new_state
if expected_error is not None: if expected_error is not None:

View File

@ -1,5 +1,3 @@
from collections import namedtuple
from typing import List
import pytest import pytest
from isledecomp.parser.parser import MarkerDict from isledecomp.parser.parser import MarkerDict
from isledecomp.parser.util import ( from isledecomp.parser.util import (