diff --git a/slither/core/scope/scope.py b/slither/core/scope/scope.py index 784b17cb20..3f91771fbe 100644 --- a/slither/core/scope/scope.py +++ b/slither/core/scope/scope.py @@ -28,7 +28,7 @@ def _dict_contain(d1: Dict, d2: Dict) -> bool: class FileScope: def __init__(self, filename: Filename) -> None: self.filename = filename - self.accessible_scopes: List[FileScope] = [] + self.accessible_scopes: List[FileScopeToImport] = [] self.contracts: Dict[str, Contract] = {} # Custom error are a list instead of a dict @@ -243,3 +243,118 @@ def __hash__(self) -> int: return hash(self.filename.relative) # endregion + + +class FileScopeToImport: + def __init__(self, filescope: FileScope, items_to_import: List[str]) -> None: + self.filescope = filescope + self.items_to_import = items_to_import + + @property + def contracts(self) -> Dict[str, Contract]: + if len(self.items_to_import) != 0: + result = {} + for name, contract in self.filescope.contracts.items(): + if name in self.items_to_import: + result[name] = contract + return result + return self.filescope.contracts + + @property + def custom_errors(self) -> Set[CustomErrorTopLevel]: + if len(self.items_to_import) != 0: + result = set() + for custom_error in self.filescope.custom_errors: + if custom_error.name in self.items_to_import: + result.add(custom_error) + return result + return self.filescope.custom_errors + + @property + def enums(self) -> Dict[str, EnumTopLevel]: + if len(self.items_to_import) != 0: + result = {} + for name, enum in self.filescope.enums.items(): + if name in self.items_to_import: + result[name] = enum + return result + return self.filescope.enums + + @property + def events(self) -> Dict[str, EventTopLevel]: + if len(self.items_to_import) != 0: + result = {} + for name, event in self.filescope.events.items(): + if name in self.items_to_import: + result[name] = event + return result + return self.filescope.events + + @property + def functions(self) -> Set[FunctionTopLevel]: + if len(self.items_to_import) != 0: + result = set() + for function in self.filescope.functions: + if function.name in self.items_to_import: + result.add(function) + return result + return self.filescope.functions + + @property + def using_for_directives(self) -> Set[UsingForTopLevel]: + # TODO check it's correct + if len(self.items_to_import) == 0: + return self.filescope.using_for_directives + return set() + + @property + def imports(self) -> Set[Import]: + # TODO check it's correct + if len(self.items_to_import) == 0: + return self.filescope.imports + return set() + + @property + def pragmas(self) -> Set[Pragma]: + # TODO check it's correct + return self.filescope.pragmas + + @property + def structures(self) -> Dict[str, StructureTopLevel]: + if len(self.items_to_import) != 0: + result = {} + for name, structure in self.filescope.structures.items(): + if name in self.items_to_import: + result[name] = structure + return result + return self.filescope.structures + + @property + def variables(self) -> Dict[str, TopLevelVariable]: + if len(self.items_to_import) != 0: + result = {} + for name, variable in self.filescope.variables.items(): + if name in self.items_to_import: + result[name] = variable + return result + return self.filescope.variables + + @property + def renaming(self) -> Dict[str, str]: + if len(self.items_to_import) != 0: + result = {} + for name, rename in self.filescope.renaming.items(): + if name in self.items_to_import: + result[name] = rename + return result + return self.filescope.renaming + + @property + def type_aliases(self) -> Dict[str, TypeAlias]: + if len(self.items_to_import) != 0: + result = {} + for name, type_alias in self.filescope.type_aliases.items(): + if name in self.items_to_import: + result[name] = type_alias + return result + return self.filescope.type_aliases diff --git a/slither/solc_parsing/slither_compilation_unit_solc.py b/slither/solc_parsing/slither_compilation_unit_solc.py index 08ee62d804..fada372fa6 100644 --- a/slither/solc_parsing/slither_compilation_unit_solc.py +++ b/slither/solc_parsing/slither_compilation_unit_solc.py @@ -16,7 +16,7 @@ from slither.core.declarations.pragma_directive import Pragma from slither.core.declarations.structure_top_level import StructureTopLevel from slither.core.declarations.using_for_top_level import UsingForTopLevel -from slither.core.scope.scope import FileScope +from slither.core.scope.scope import FileScope, FileScopeToImport from slither.core.solidity_types import ElementaryType, TypeAliasTopLevel from slither.core.variables.top_level_variable import TopLevelVariable from slither.exceptions import SlitherException @@ -41,7 +41,7 @@ class InheritanceResolutionError(SlitherException): def _handle_import_aliases( symbol_aliases: Dict, import_directive: Import, scope: FileScope -) -> None: +) -> List[str]: """ Handle the parsing of import aliases @@ -53,16 +53,17 @@ def _handle_import_aliases( Returns: """ + to_import = [] for symbol_alias in symbol_aliases: - if "foreign" in symbol_alias and "local" in symbol_alias: + if "foreign" in symbol_alias: if isinstance(symbol_alias["foreign"], dict) and "name" in symbol_alias["foreign"]: - original_name = symbol_alias["foreign"]["name"] - local_name = symbol_alias["local"] - import_directive.renaming[local_name] = original_name - # Assuming that two imports cannot collide in renaming - scope.renaming[local_name] = original_name - + to_import.append(original_name) + if "local" in symbol_alias: + local_name = symbol_alias["local"] + import_directive.renaming[local_name] = original_name + # Assuming that two imports cannot collide in renaming + scope.renaming[local_name] = original_name # This path should only be hit for the malformed AST of solc 0.5.12 where # the foreign identifier cannot be found but is required to resolve the alias. # see https://github.com/crytic/slither/issues/1319 @@ -71,6 +72,8 @@ def _handle_import_aliases( "Cannot resolve local alias for import directive due to malformed AST. Please upgrade to solc 0.6.0 or higher." ) + return to_import + class SlitherCompilationUnitSolc(CallerContextExpression): # pylint: disable=no-self-use,too-many-instance-attributes @@ -257,6 +260,7 @@ def parse_top_level_items(self, data_loaded: Dict, filename: str) -> None: self._using_for_top_level_parser.append(usingFor_parser) elif top_level_data[self.get_key()] == "ImportDirective": + to_import = [] if self.is_compact_ast: import_directive = Import( Path( @@ -270,7 +274,7 @@ def parse_top_level_items(self, data_loaded: Dict, filename: str) -> None: import_directive.alias = top_level_data["unitAlias"] if "symbolAliases" in top_level_data: symbol_aliases = top_level_data["symbolAliases"] - _handle_import_aliases(symbol_aliases, import_directive, scope) + to_import = _handle_import_aliases(symbol_aliases, import_directive, scope) else: import_directive = Import( Path( @@ -289,7 +293,7 @@ def parse_top_level_items(self, data_loaded: Dict, filename: str) -> None: self._compilation_unit.import_directives.append(import_directive) get_imported_scope = self.compilation_unit.get_scope(import_directive.filename) - scope.accessible_scopes.append(get_imported_scope) + scope.accessible_scopes.append(FileScopeToImport(get_imported_scope, to_import)) elif top_level_data[self.get_key()] == "StructDefinition": st = StructureTopLevel(self.compilation_unit, scope)