Example #1
0
    def _parse_returns(self, returns):

        assert returns[self.get_key()] == 'ParameterList'

        self.returns_src = SourceMapping()
        self.returns_src.set_offset(returns['src'], self.contract.slither)

        if self.is_compact_ast:
            returns = returns['parameters']
        else:
            returns = returns[self.get_children('children')]

        for ret in returns:
            assert ret[self.get_key()] == 'VariableDeclaration'

            local_var = LocalVariableSolc(ret)

            local_var.set_function(self)
            local_var.set_offset(ret['src'], self.contract.slither)
            local_var.analyze(self)

            # see https://solidity.readthedocs.io/en/v0.4.24/types.html?highlight=storage%20location#data-location
            if local_var.location == 'default':
                local_var.set_location('memory')

            self._add_local_variable(local_var)
            self._returns.append(local_var)
Example #2
0
    def add_other_to_json(self, name, source_mapping, d, additional_fields={}):
        # If this a tuple with (filename, start, end), convert it to a source mapping.
        if isinstance(source_mapping, tuple):
            # Parse the source id
            (filename, start, end) = source_mapping
            source_id = next((source_unit_id for (
                source_unit_id,
                source_unit_filename) in self.slither.source_units.items()
                              if source_unit_filename == filename), -1)

            # Convert to a source mapping string
            source_mapping = f"{start}:{end}:{source_id}"

        # If this is a source mapping string, parse it.
        if isinstance(source_mapping, str):
            source_mapping_str = source_mapping
            source_mapping = SourceMapping()
            source_mapping.set_offset(source_mapping_str, self.slither)

        # If this is a source mapping object, get the underlying source mapping dictionary
        if isinstance(source_mapping, SourceMapping):
            source_mapping = source_mapping.source_mapping

        # Create the underlying element and add it to our resulting json
        element = self._create_base_element('other', name, source_mapping, {},
                                            additional_fields)
        d['elements'].append(element)
Example #3
0
    def _parse_params(self, params):
        assert params[self.get_key()] == 'ParameterList'

        self.parameters_src = SourceMapping()
        self.parameters_src.set_offset(params['src'], self.contract.slither)

        if self.is_compact_ast:
            params = params['parameters']
        else:
            params = params[self.get_children('children')]

        for param in params:
            assert param[self.get_key()] == 'VariableDeclaration'
            local_var = self._add_param(param)
            self._parameters.append(local_var)
Example #4
0
    def _parse_returns(self, returns):

        assert returns[self.get_key()] == 'ParameterList'

        self.returns_src = SourceMapping()
        self.returns_src.set_offset(returns['src'], self.contract.slither)

        if self.is_compact_ast:
            returns = returns['parameters']
        else:
            returns = returns[self.get_children('children')]

        for ret in returns:
            assert ret[self.get_key()] == 'VariableDeclaration'
            local_var = self._add_param(ret)
            self._returns.append(local_var)
Example #5
0
    def add_other(
        self,
        name: str,
        source_mapping,
        compilation_unit: "SlitherCompilationUnit",
        additional_fields: Optional[Dict] = None,
    ):
        # If this a tuple with (filename, start, end), convert it to a source mapping.
        if additional_fields is None:
            additional_fields = {}
        if isinstance(source_mapping, tuple):
            # Parse the source id
            (filename, start, end) = source_mapping
            source_id = next(
                (source_unit_id for (
                    source_unit_id,
                    source_unit_filename,
                ) in compilation_unit.source_units.items()
                 if source_unit_filename == filename),
                -1,
            )

            # Convert to a source mapping string
            source_mapping = f"{start}:{end}:{source_id}"

        # If this is a source mapping string, parse it.
        if isinstance(source_mapping, str):
            source_mapping_str = source_mapping
            source_mapping = SourceMapping()
            source_mapping.set_offset(source_mapping_str, compilation_unit)

        # If this is a source mapping object, get the underlying source mapping dictionary
        if isinstance(source_mapping, SourceMapping):
            source_mapping = source_mapping.source_mapping

        # Create the underlying element and add it to our resulting json
        element = _create_base_element("other", name, source_mapping, {},
                                       additional_fields)
        self._data["elements"].append(element)