def _convert_UPPER_CASE_WITH_UNDERSCORES(name, slither): if _name_already_use(slither, name.upper()): raise FormatImpossible( f'{name} cannot be converted to {name.upper()} (already used)') if name.upper() in SOLIDITY_KEYWORDS: raise FormatImpossible( f'{name} cannot be converted to {name.upper()} (Solidity keyword)') return name.upper()
def _convert_UPPER_CASE_WITH_UNDERSCORES( name, compilation_unit: SlitherCompilationUnit): if _name_already_use(compilation_unit, name.upper()): raise FormatImpossible( f"{name} cannot be converted to {name.upper()} (already used)") if name.upper() in SOLIDITY_KEYWORDS: raise FormatImpossible( f"{name} cannot be converted to {name.upper()} (Solidity keyword)") return name.upper()
def _convert_CapWords(original_name, slither): name = original_name.capitalize() while "_" in name: offset = name.find("_") if len(name) > offset: name = name[0:offset] + name[offset + 1].upper() + name[offset + 1 :] if _name_already_use(slither, name): raise FormatImpossible(f"{original_name} cannot be converted to {name} (already used)") if name in SOLIDITY_KEYWORDS: raise FormatImpossible(f"{original_name} cannot be converted to {name} (Solidity keyword)") return name
def _convert_mixedCase(original_name, slither): name = original_name if isinstance(name, bytes): name = name.decode("utf8") while "_" in name: offset = name.find("_") if len(name) > offset: name = name[0:offset] + name[offset + 1].upper() + name[offset + 2 :] name = name[0].lower() + name[1:] if _name_already_use(slither, name): raise FormatImpossible(f"{original_name} cannot be converted to {name} (already used)") if name in SOLIDITY_KEYWORDS: raise FormatImpossible(f"{original_name} cannot be converted to {name} (Solidity keyword)") return name
def _analyse_versions(used_solc_versions): replace_solc_versions = list() for version in used_solc_versions: replace_solc_versions.append(_determine_solc_version_replacement(version)) if not all(version == replace_solc_versions[0] for version in replace_solc_versions): raise FormatImpossible("Multiple incompatible versions!") return replace_solc_versions[0]
def _determine_solc_version_replacement(used_solc_version): versions = PATTERN.findall(used_solc_version) if len(versions) == 1: version = versions[0] minor_version = ".".join(version[2:])[2] if minor_version == "4": return "pragma solidity " + REPLACEMENT_VERSIONS[0] + ";" if minor_version == "5": return "pragma solidity " + REPLACEMENT_VERSIONS[1] + ";" raise FormatImpossible("Unknown version!") if len(versions) == 2: version_right = versions[1] minor_version_right = ".".join(version_right[2:])[2] if minor_version_right == "4": # Replace with 0.4.25 return "pragma solidity " + REPLACEMENT_VERSIONS[0] + ";" if minor_version_right in ["5", "6"]: # Replace with 0.5.3 return "pragma solidity " + REPLACEMENT_VERSIONS[1] + ";" raise FormatImpossible("Unknown version!")
def format(slither, result): elements = result['elements'] for element in elements: # TODO: decide if this should be changed in the constant detector contract_name = element['type_specific_fields']['parent']['name'] contract = slither.get_contract_from_name(contract_name) var = contract.get_state_variable_from_name(element['name']) if not var.expression: raise FormatImpossible( f'{var.name} is uninitialized and cannot become constant.') _patch( slither, result, element['source_mapping']['filename_absolute'], element['name'], "constant " + element['name'], element['source_mapping']['start'], element['source_mapping']['start'] + element['source_mapping']['length'])
def _determine_solc_version_replacement(used_solc_version): versions = PATTERN.findall(used_solc_version) if len(versions) == 1: version = versions[0] minor_version = '.'.join(version[2:])[2] if minor_version == '4': return "pragma solidity " + REPLACEMENT_VERSIONS[0] + ';' elif minor_version == '5': return "pragma solidity " + REPLACEMENT_VERSIONS[1] + ';' else: raise FormatImpossible("Unknown version!") elif len(versions) == 2: version_right = versions[1] minor_version_right = '.'.join(version_right[2:])[2] if minor_version_right == '4': # Replace with 0.4.25 return "pragma solidity " + REPLACEMENT_VERSIONS[0] + ';' elif minor_version_right in ['5', '6']: # Replace with 0.5.3 return "pragma solidity " + REPLACEMENT_VERSIONS[1] + ';'
def custom_format(compilation_unit: SlitherCompilationUnit, result): elements = result["elements"] for element in elements: # TODO: decide if this should be changed in the constant detector contract_name = element["type_specific_fields"]["parent"]["name"] contract = compilation_unit.get_contract_from_name(contract_name) var = contract.get_state_variable_from_name(element["name"]) if not var.expression: raise FormatImpossible( f"{var.name} is uninitialized and cannot become constant.") _patch( compilation_unit, result, element["source_mapping"]["filename_absolute"], element["name"], "constant " + element["name"], element["source_mapping"]["start"], element["source_mapping"]["start"] + element["source_mapping"]["length"], )