From 79e28f8e457fc62c29c08212b7f7b2eb9760e6ec Mon Sep 17 00:00:00 2001 From: David Pokora Date: Wed, 7 Jul 2021 11:17:10 -0400 Subject: [PATCH 1/6] Changed get_global_offset_from_line to get_global_offset_from_line_and_character, taking an optional character position to obtain an offset for. --- crytic_compile/crytic_compile.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/crytic_compile/crytic_compile.py b/crytic_compile/crytic_compile.py index bee742d6..ea6eb540 100644 --- a/crytic_compile/crytic_compile.py +++ b/crytic_compile/crytic_compile.py @@ -88,7 +88,7 @@ def __init__(self, target: Union[str, AbstractPlatform], **kwargs: str): # We decided to favor the running time versus memory self._cached_offset_to_line: Dict[Filename, Dict[int, Tuple[int, int]]] = dict() # Lines are indexed from 1 - self._cached_line_to_offset: Dict[Filename, Dict[int, int]] = defaultdict(dict) + self._cached_line_to_offset: Dict[Filename, Dict[Tuple[int, int], int]] = defaultdict(dict) # Return the line from the line number # Note: line 1 is at index 0 @@ -221,10 +221,11 @@ def _get_cached_offset_to_line(self, file: Filename): acc = 0 lines_delimiters: Dict[int, Tuple[int, int]] = dict() for line_number, x in enumerate(source_code): - self._cached_line_to_offset[file][line_number + 1] = acc for i in range(acc, acc + len(x)): - lines_delimiters[i] = (line_number + 1, i - acc + 1) + line_and_char_position = (line_number + 1, i - acc + 1) + lines_delimiters[i] = line_and_char_position + self._cached_line_to_offset[file][line_and_char_position] = i acc += len(x) lines_delimiters[acc] = (len(source_code) + 1, 0) @@ -237,11 +238,11 @@ def get_line_from_offset(self, file: Filename, offset: int) -> Tuple[int, int]: lines_delimiters = self._cached_offset_to_line[file] return lines_delimiters[offset] - def get_global_offset_from_line(self, file: Filename, line: int) -> int: + def get_global_offset_from_line_and_character(self, file: Filename, line: int, char_position: int = 1) -> int: if file not in self._cached_line_to_offset: self._get_cached_offset_to_line(file) - return self._cached_line_to_offset[file][line] + return self._cached_line_to_offset[file][(line, char_position)] def _get_cached_line_to_code(self, file: Filename): source_code = self.src_content[file.absolute] From 6ad9272c2e4d8ea37055c5b40baae0849e90801d Mon Sep 17 00:00:00 2001 From: David Pokora Date: Wed, 7 Jul 2021 11:25:21 -0400 Subject: [PATCH 2/6] Rename get_line_from_offset to get_line_and_character_from_offset --- crytic_compile/crytic_compile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crytic_compile/crytic_compile.py b/crytic_compile/crytic_compile.py index ea6eb540..8225caea 100644 --- a/crytic_compile/crytic_compile.py +++ b/crytic_compile/crytic_compile.py @@ -231,7 +231,7 @@ def _get_cached_offset_to_line(self, file: Filename): lines_delimiters[acc] = (len(source_code) + 1, 0) self._cached_offset_to_line[file] = lines_delimiters - def get_line_from_offset(self, file: Filename, offset: int) -> Tuple[int, int]: + def get_line_and_character_from_offset(self, file: Filename, offset: int) -> Tuple[int, int]: if file not in self._cached_offset_to_line: self._get_cached_offset_to_line(file) From 4a9c4040b29bce43e17a09464a66bc8868f50910 Mon Sep 17 00:00:00 2001 From: Josselin Date: Fri, 9 Jul 2021 11:41:17 +0200 Subject: [PATCH 3/6] Run black --- crytic_compile/crytic_compile.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crytic_compile/crytic_compile.py b/crytic_compile/crytic_compile.py index a4529f91..633c7fe1 100644 --- a/crytic_compile/crytic_compile.py +++ b/crytic_compile/crytic_compile.py @@ -231,7 +231,9 @@ def _get_cached_offset_to_line(self, file: Filename) -> None: lines_delimiters[acc] = (len(source_code) + 1, 0) self._cached_offset_to_line[file] = lines_delimiters - def get_line_and_character_from_offset(self, filename: Union[Filename, str], offset: int) -> Tuple[int, int]: + def get_line_and_character_from_offset( + self, filename: Union[Filename, str], offset: int + ) -> Tuple[int, int]: if isinstance(filename, str): file = self.filename_lookup(filename) else: @@ -242,7 +244,9 @@ def get_line_and_character_from_offset(self, filename: Union[Filename, str], off lines_delimiters = self._cached_offset_to_line[file] return lines_delimiters[offset] - def get_global_offset_from_line_and_character(self, filename: Union[Filename, str], line: int, char_position: int = 1) -> int: + def get_global_offset_from_line_and_character( + self, filename: Union[Filename, str], line: int, char_position: int = 1 + ) -> int: if isinstance(filename, str): file = self.filename_lookup(filename) else: From 19b4ba04cf4455c83e74e5f3a983b840668697d9 Mon Sep 17 00:00:00 2001 From: David Pokora Date: Mon, 12 Jul 2021 15:35:38 -0400 Subject: [PATCH 4/6] Improved error output for truffle compilation --- crytic_compile/platform/truffle.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crytic_compile/platform/truffle.py b/crytic_compile/platform/truffle.py index 8fcafa4c..b3bae9f4 100755 --- a/crytic_compile/platform/truffle.py +++ b/crytic_compile/platform/truffle.py @@ -175,11 +175,11 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None: if stderr: LOGGER.error(stderr) if not os.path.isdir(os.path.join(self._target, build_directory)): - if os.path.isdir(os.path.join(self._target, "node_modules")): + if not os.path.isdir(os.path.join(self._target, "node_modules")): raise InvalidCompilation( - f"External dependencies {build_directory} {self._target} not found, please install them. (npm install)" + f"External dependencies not found, please install them. (npm install)" ) - raise InvalidCompilation("`truffle compile` failed. Can you run it?") + raise InvalidCompilation(f"`truffle compile` failed. Output:\n{stdout}") filenames = glob.glob(os.path.join(self._target, build_directory, "*.json")) optimized = None From fceb0ab35d7ab1b511614f35b20cd78b028a9bea Mon Sep 17 00:00:00 2001 From: Josselin Date: Fri, 6 Aug 2021 07:16:20 +0200 Subject: [PATCH 5/6] Fix pylint --- crytic_compile/platform/truffle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crytic_compile/platform/truffle.py b/crytic_compile/platform/truffle.py index b3bae9f4..57bfa751 100755 --- a/crytic_compile/platform/truffle.py +++ b/crytic_compile/platform/truffle.py @@ -177,7 +177,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None: if not os.path.isdir(os.path.join(self._target, build_directory)): if not os.path.isdir(os.path.join(self._target, "node_modules")): raise InvalidCompilation( - f"External dependencies not found, please install them. (npm install)" + "External dependencies not found, please install them. (npm install)" ) raise InvalidCompilation(f"`truffle compile` failed. Output:\n{stdout}") filenames = glob.glob(os.path.join(self._target, build_directory, "*.json")) From 22753b341c6518c1d8d55143dca5e22ce324a99f Mon Sep 17 00:00:00 2001 From: Josselin Date: Fri, 6 Aug 2021 07:24:58 +0200 Subject: [PATCH 6/6] Commit black changes --- crytic_compile/crytic_compile.py | 8 ++++++-- crytic_compile/platform/truffle.py | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/crytic_compile/crytic_compile.py b/crytic_compile/crytic_compile.py index 8926ec51..1707baa8 100644 --- a/crytic_compile/crytic_compile.py +++ b/crytic_compile/crytic_compile.py @@ -264,7 +264,9 @@ def _get_cached_offset_to_line(self, file: Filename) -> None: lines_delimiters[acc] = (len(source_code) + 1, 0) self._cached_offset_to_line[file] = lines_delimiters - def get_line_and_character_from_offset(self, filename: Union[Filename, str], offset: int) -> Tuple[int, int]: + def get_line_and_character_from_offset( + self, filename: Union[Filename, str], offset: int + ) -> Tuple[int, int]: """Return the line from a given offset Args: @@ -284,7 +286,9 @@ def get_line_and_character_from_offset(self, filename: Union[Filename, str], off lines_delimiters = self._cached_offset_to_line[file] return lines_delimiters[offset] - def get_global_offset_from_line_and_character(self, filename: Union[Filename, str], line: int, char_position: int = 1) -> int: + def get_global_offset_from_line_and_character( + self, filename: Union[Filename, str], line: int, char_position: int = 1 + ) -> int: """Return the global offset from a given line and charater Args: diff --git a/crytic_compile/platform/truffle.py b/crytic_compile/platform/truffle.py index 03fa4449..643382c1 100755 --- a/crytic_compile/platform/truffle.py +++ b/crytic_compile/platform/truffle.py @@ -188,7 +188,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None: raise InvalidCompilation( "External dependencies not found, please install them. (npm install)" ) - raise InvalidCompilation(f"`truffle compile` failed. Output:\n{stdout}") + raise InvalidCompilation(f"`truffle compile` failed. Can you run It? Output:\n{stdout}") filenames = glob.glob(os.path.join(self._target, build_directory, "*.json")) optimized = None