From 9c7f71fa124318971f09772585a10123d5dba613 Mon Sep 17 00:00:00 2001 From: rajeevgopalakrishna Date: Fri, 24 May 2019 17:13:05 +0530 Subject: [PATCH] Adds multiline formatting changes. --- .../slither_format/format_constable_states.py | 5 +- .../format_constant_function.py | 7 +- .../format_external_function.py | 8 +- .../format_naming_convention.py | 252 +++++++++++++----- utils/slither_format/format_pragma.py | 5 +- utils/slither_format/format_solc_version.py | 8 +- utils/slither_format/format_unused_state.py | 7 +- 7 files changed, 215 insertions(+), 77 deletions(-) diff --git a/utils/slither_format/format_constable_states.py b/utils/slither_format/format_constable_states.py index 6f221fce7..ca0cf9cb2 100644 --- a/utils/slither_format/format_constable_states.py +++ b/utils/slither_format/format_constable_states.py @@ -10,7 +10,10 @@ class FormatConstableStates: @staticmethod def format(slither, patches, elements): for element in elements: - FormatConstableStates.create_patch(slither, patches, element['source_mapping']['filename_absolute'], element['source_mapping']['filename_relative'], element['name'], "constant " + element['name'], element['source_mapping']['start'], element['source_mapping']['start'] + element['source_mapping']['length']) + FormatConstableStates.create_patch(slither, patches, element['source_mapping']['filename_absolute'], \ + element['source_mapping']['filename_relative'], element['name'], \ + "constant " + element['name'], element['source_mapping']['start'], \ + element['source_mapping']['start'] + element['source_mapping']['length']) @staticmethod def create_patch(slither, patches, in_file, in_file_relative, match_text, replace_text, modify_loc_start, modify_loc_end): diff --git a/utils/slither_format/format_constant_function.py b/utils/slither_format/format_constant_function.py index 7be746772..936216f2b 100644 --- a/utils/slither_format/format_constant_function.py +++ b/utils/slither_format/format_constant_function.py @@ -17,7 +17,12 @@ class FormatConstantFunction: if target_contract: for function in target_contract.functions: if function.name == element['name']: - FormatConstantFunction.create_patch(slither, patches, element['source_mapping']['filename_absolute'], element['source_mapping']['filename_relative'], ["view","pure","constant"], "", int(function.parameters_src.source_mapping['start']), int(function.returns_src.source_mapping['start'])) + FormatConstantFunction.create_patch(slither, patches, \ + element['source_mapping']['filename_absolute'], \ + element['source_mapping']['filename_relative'], \ + ["view","pure","constant"], "", \ + int(function.parameters_src.source_mapping['start']), \ + int(function.returns_src.source_mapping['start'])) break @staticmethod diff --git a/utils/slither_format/format_external_function.py b/utils/slither_format/format_external_function.py index 031a7947c..21d4bce27 100644 --- a/utils/slither_format/format_external_function.py +++ b/utils/slither_format/format_external_function.py @@ -18,7 +18,12 @@ class FormatExternalFunction: # to external because external function parameters are allocated in calldata region which is # non-modifiable. See https://solidity.readthedocs.io/en/develop/types.html#data-location if not FormatExternalFunction.function_parameters_written(function): - FormatExternalFunction.create_patch(slither, patches, element['source_mapping']['filename_absolute'], element['source_mapping']['filename_relative'], "public", "external", int(function.parameters_src.source_mapping['start']), int(function.returns_src.source_mapping['start'])) + FormatExternalFunction.create_patch(slither, patches, \ + element['source_mapping']['filename_absolute'], \ + element['source_mapping']['filename_relative'], \ + "public", "external", \ + int(function.parameters_src.source_mapping['start']), \ + int(function.returns_src.source_mapping['start'])) break @staticmethod @@ -27,7 +32,6 @@ class FormatExternalFunction: old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] m = re.search(r'((\spublic)\s+)|(\spublic)$|(\)public)$', old_str_of_interest.decode('utf-8')) if m is None: - print("None: " + old_str_of_interest.decode('utf-8')) # No visibility specifier exists; public by default. patches[in_file_relative].append({ "file" : in_file, diff --git a/utils/slither_format/format_naming_convention.py b/utils/slither_format/format_naming_convention.py index aacdd3f82..2a902a314 100644 --- a/utils/slither_format/format_naming_convention.py +++ b/utils/slither_format/format_naming_convention.py @@ -15,39 +15,77 @@ class FormatNamingConvention: def format(slither, patches, elements): for element in elements: if (element['additional_fields']['target'] == "parameter"): - FormatNamingConvention.create_patch(slither, patches, element['additional_fields']['target'], element['name'], element['type_specific_fields']['parent']['name'], element['type_specific_fields']['parent']['type_specific_fields']['parent']['name'], element['source_mapping']['filename_absolute'], element['source_mapping']['filename_relative'], element['source_mapping']['start'],(element['source_mapping']['start']+element['source_mapping']['length'])) - elif (element['additional_fields']['target'] == "modifier" or element['additional_fields']['target'] == "function" or element['additional_fields']['target'] == "event" or element['additional_fields']['target'] == "variable" or element['additional_fields']['target'] == "variable_constant" or element['additional_fields']['target'] == "enum" or element['additional_fields']['target'] == "structure"): - FormatNamingConvention.create_patch(slither, patches, element['additional_fields']['target'], element['name'], element['name'], element['type_specific_fields']['parent']['name'], element['source_mapping']['filename_absolute'], element['source_mapping']['filename_relative'], element['source_mapping']['start'],(element['source_mapping']['start']+element['source_mapping']['length'])) + FormatNamingConvention.create_patch(slither, patches, element['additional_fields']['target'], \ + element['name'], element['type_specific_fields']['parent']['name'], \ + element['type_specific_fields']['parent']['type_specific_fields'] + ['parent']['name'], element['source_mapping']['filename_absolute'], \ + element['source_mapping']['filename_relative'], \ + element['source_mapping']['start'],(element['source_mapping']['start'] + + element['source_mapping']['length'])) + elif (element['additional_fields']['target'] == "modifier" or + element['additional_fields']['target'] == "function" or + element['additional_fields']['target'] == "event" or + element['additional_fields']['target'] == "variable" or + element['additional_fields']['target'] == "variable_constant" or + element['additional_fields']['target'] == "enum" or + element['additional_fields']['target'] == "structure"): + FormatNamingConvention.create_patch(slither, patches, element['additional_fields']['target'], \ + element['name'], element['name'], \ + element['type_specific_fields']['parent']['name'], \ + element['source_mapping']['filename_absolute'], \ + element['source_mapping']['filename_relative'], \ + element['source_mapping']['start'],(element['source_mapping']['start'] + + element['source_mapping']['length'])) else: - FormatNamingConvention.create_patch(slither, patches, element['additional_fields']['target'], element['name'], element['name'], element['name'], element['source_mapping']['filename_absolute'], element['source_mapping']['filename_relative'], element['source_mapping']['start'],(element['source_mapping']['start']+element['source_mapping']['length'])) + FormatNamingConvention.create_patch(slither, patches, element['additional_fields']['target'], \ + element['name'], element['name'], element['name'], \ + element['source_mapping']['filename_absolute'], \ + element['source_mapping']['filename_relative'], \ + element['source_mapping']['start'],(element['source_mapping']['start'] + + element['source_mapping']['length'])) @staticmethod - def create_patch(slither, patches, _target, name, function_name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end): + def create_patch(slither, patches, _target, name, function_name, contract_name, in_file, in_file_relative, + modify_loc_start, modify_loc_end): if _target == "contract": - FormatNamingConvention.create_patch_contract_definition(slither, patches, name, in_file, in_file_relative, modify_loc_start, modify_loc_end) + FormatNamingConvention.create_patch_contract_definition(slither, patches, name, in_file, in_file_relative, + modify_loc_start, modify_loc_end) FormatNamingConvention.create_patch_contract_uses(slither, patches, name, in_file, in_file_relative) elif _target == "structure": - FormatNamingConvention.create_patch_struct_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end) + FormatNamingConvention.create_patch_struct_definition(slither, patches, name, contract_name, in_file, + in_file_relative, modify_loc_start, modify_loc_end) FormatNamingConvention.create_patch_struct_uses(slither, patches, name, contract_name, in_file, in_file_relative) elif _target == "event": - FormatNamingConvention.create_patch_event_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end) + FormatNamingConvention.create_patch_event_definition(slither, patches, name, contract_name, in_file, + in_file_relative, modify_loc_start, modify_loc_end) FormatNamingConvention.create_patch_event_calls(slither, patches, name, contract_name, in_file, in_file_relative) elif _target == "function": if name != contract_name: - FormatNamingConvention.create_patch_function_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end) - FormatNamingConvention.create_patch_function_calls(slither, patches, name, contract_name, in_file, in_file_relative) + FormatNamingConvention.create_patch_function_definition(slither, patches, name, contract_name, in_file, + in_file_relative, modify_loc_start, modify_loc_end) + FormatNamingConvention.create_patch_function_calls(slither, patches, name, contract_name, in_file, + in_file_relative) elif _target == "parameter": - FormatNamingConvention.create_patch_parameter_declaration(slither, patches, name, function_name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end) - FormatNamingConvention.create_patch_parameter_uses(slither, patches, name, function_name, contract_name, in_file, in_file_relative) + FormatNamingConvention.create_patch_parameter_declaration(slither, patches, name, function_name, contract_name, + in_file, in_file_relative, modify_loc_start, + modify_loc_end) + FormatNamingConvention.create_patch_parameter_uses(slither, patches, name, function_name, contract_name, + in_file, in_file_relative) elif _target == "variable_constant" or _target == "variable": - FormatNamingConvention.create_patch_state_variable_declaration(slither, patches, _target, name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end) - FormatNamingConvention.create_patch_state_variable_uses(slither, patches, _target, name, contract_name, in_file, in_file_relative) + FormatNamingConvention.create_patch_state_variable_declaration(slither, patches, _target, name, contract_name, + in_file, in_file_relative, modify_loc_start, + modify_loc_end) + FormatNamingConvention.create_patch_state_variable_uses(slither, patches, _target, name, contract_name, in_file, + in_file_relative) elif _target == "enum": - FormatNamingConvention.create_patch_enum_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end) + FormatNamingConvention.create_patch_enum_definition(slither, patches, name, contract_name, in_file, + in_file_relative, modify_loc_start, modify_loc_end) FormatNamingConvention.create_patch_enum_uses(slither, patches, name, contract_name, in_file, in_file_relative) elif _target == "modifier": - FormatNamingConvention.create_patch_modifier_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end) - FormatNamingConvention.create_patch_modifier_uses(slither, patches, name, contract_name, in_file, in_file_relative) + FormatNamingConvention.create_patch_modifier_definition(slither, patches, name, contract_name, in_file, + in_file_relative, modify_loc_start, modify_loc_end) + FormatNamingConvention.create_patch_modifier_uses(slither, patches, name, contract_name, in_file, + in_file_relative) else: logger.error(red("Unknown naming convention! " + _target)) sys.exit(-1) @@ -58,7 +96,8 @@ class FormatNamingConvention: old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] m = re.match(r'(.*)'+"contract"+r'(.*)'+name, old_str_of_interest.decode('utf-8')) old_str_of_interest = in_file_str[modify_loc_start:modify_loc_start+m.span()[1]] - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+"contract"+r'(.*)'+name, r'\1'+"contract"+r'\2'+name.capitalize(), old_str_of_interest.decode('utf-8'), 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+"contract"+r'(.*)'+name, r'\1'+"contract"+r'\2'+name.capitalize(), + old_str_of_interest.decode('utf-8'), 1) if num_repl != 0: patch = { "file" : in_file, @@ -85,8 +124,10 @@ class FormatNamingConvention: svs = contract.variables for sv in svs: if (str(sv.type) == name): - old_str_of_interest = in_file_str[sv.source_mapping['start']:(sv.source_mapping['start']+sv.source_mapping['length'])] - (new_str_of_interest, num_repl) = re.subn(name, name.capitalize(),old_str_of_interest.decode('utf-8'), 1) + old_str_of_interest = in_file_str[sv.source_mapping['start']:(sv.source_mapping['start'] + + sv.source_mapping['length'])] + (new_str_of_interest, num_repl) = re.subn(name, name.capitalize(), + old_str_of_interest.decode('utf-8'), 1) patch = { "file" : in_file, "detector" : "naming-convention (contract state variable)", @@ -103,7 +144,9 @@ class FormatNamingConvention: for fm in fms: for v in fm.variables: if (str(v.type) == name): - old_str_of_interest = in_file_str[v.source_mapping['start']:(v.source_mapping['start']+v.source_mapping['length'])].decode('utf-8').split('=')[0] + old_str_of_interest = in_file_str[v.source_mapping['start']:(v.source_mapping['start'] + + v.source_mapping['length'])] + old_str_of_interest = old_str_of_interest.decode('utf-8').split('=')[0] (new_str_of_interest, num_repl) = re.subn(name, name.capitalize(),old_str_of_interest, 1) patch = { "file" : in_file, @@ -120,10 +163,12 @@ class FormatNamingConvention: for node in function.nodes: for ir in node.irs: if isinstance(ir, NewContract) and ir.contract_name == name: - old_str_of_interest = in_file_str[node.source_mapping['start']:node.source_mapping['start'] + node.source_mapping['length']] + old_str_of_interest = in_file_str[node.source_mapping['start']:node.source_mapping['start'] + + node.source_mapping['length']] m = re.search("new"+r'(.*)'+name, old_str_of_interest.decode('utf-8')) old_str_of_interest = old_str_of_interest.decode('utf-8')[m.span()[0]:] - (new_str_of_interest, num_repl) = re.subn("new"+r'(.*)'+name, "new"+r'\1'+name[0].upper()+name[1:], old_str_of_interest, 1) + (new_str_of_interest, num_repl) = re.subn("new"+r'(.*)'+name, "new"+r'\1'+name[0].upper() + + name[1:], old_str_of_interest, 1) if num_repl != 0: patch = { "file" : in_file, @@ -140,7 +185,8 @@ class FormatNamingConvention: sys.exit(-1) @staticmethod - def create_patch_modifier_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end): + def create_patch_modifier_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, + modify_loc_end): target_contract = slither.get_contract_from_name(contract_name) if not target_contract: logger.error(red("Contract not found?!")) @@ -151,7 +197,8 @@ class FormatNamingConvention: old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] m = re.match(r'(.*)'+"modifier"+r'(.*)'+name, old_str_of_interest.decode('utf-8')) old_str_of_interest = in_file_str[modify_loc_start:modify_loc_start+m.span()[1]] - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+"modifier"+r'(.*)'+name, r'\1'+"modifier"+r'\2'+name[0].lower()+name[1:], old_str_of_interest.decode('utf-8'), 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+"modifier"+r'(.*)'+name, r'\1'+"modifier"+r'\2' + + name[0].lower()+name[1:], old_str_of_interest.decode('utf-8'), 1) if num_repl != 0: patch = { "file" : in_file, @@ -178,8 +225,10 @@ class FormatNamingConvention: for m in function.modifiers: if (m.name == name): in_file_str = slither.source_code[in_file].encode('utf-8') - old_str_of_interest = in_file_str[int(function.parameters_src.source_mapping['start']):int(function.returns_src.source_mapping['start'])] - (new_str_of_interest, num_repl) = re.subn(name, name[0].lower()+name[1:],old_str_of_interest.decode('utf-8'),1) + old_str_of_interest = in_file_str[int(function.parameters_src.source_mapping['start']): + int(function.returns_src.source_mapping['start'])] + (new_str_of_interest, num_repl) = re.subn(name, name[0].lower()+name[1:], + old_str_of_interest.decode('utf-8'),1) if num_repl != 0: patch = { "file" : in_file, @@ -196,7 +245,8 @@ class FormatNamingConvention: sys.exit(-1) @staticmethod - def create_patch_function_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end): + def create_patch_function_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, + modify_loc_end): target_contract = slither.get_contract_from_name(contract_name) if not target_contract: logger.error(red("Contract not found?!")) @@ -207,7 +257,8 @@ class FormatNamingConvention: old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] m = re.match(r'(.*)'+"function"+r'\s*'+name, old_str_of_interest.decode('utf-8')) old_str_of_interest = in_file_str[modify_loc_start:modify_loc_start+m.span()[1]] - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+"function"+r'(.*)'+name, r'\1'+"function"+r'\2'+name[0].lower()+name[1:], old_str_of_interest.decode('utf-8'), 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+"function"+r'(.*)'+name, r'\1'+"function"+r'\2'+ + name[0].lower()+name[1:], old_str_of_interest.decode('utf-8'), 1) if num_repl != 0: patch = { "file" : in_file, @@ -234,15 +285,19 @@ class FormatNamingConvention: called_function = str(external_call.called).split('.')[-1] if called_function == high_level_call[1].name: in_file_str = slither.source_code[in_file].encode('utf-8') - old_str_of_interest = in_file_str[int(external_call.source_mapping['start']):int(external_call.source_mapping['start'])+int(external_call.source_mapping['length'])] + old_str_of_interest = in_file_str[int(external_call.source_mapping['start']): + int(external_call.source_mapping['start']) + + int(external_call.source_mapping['length'])] called_function_name = old_str_of_interest.decode('utf-8').split('.')[-1] fixed_function_name = called_function_name[0].lower() + called_function_name[1:] - new_string = '.'.join(old_str_of_interest.decode('utf-8').split('.')[:-1]) + '.' + fixed_function_name + new_string = '.'.join(old_str_of_interest.decode('utf-8').split('.')[:-1]) + '.' + \ + fixed_function_name patch = { "file" : in_file, "detector" : "naming-convention (function calls)", "start" : external_call.source_mapping['start'], - "end" : int(external_call.source_mapping['start']) + int(external_call.source_mapping['length']), + "end" : int(external_call.source_mapping['start']) + + int(external_call.source_mapping['length']), "old_string" : old_str_of_interest.decode('utf-8'), "new_string" : new_string } @@ -251,12 +306,20 @@ class FormatNamingConvention: for internal_call in node.internal_calls_as_expressions: if (str(internal_call.called) == name): in_file_str = slither.source_code[in_file].encode('utf-8') - old_str_of_interest = in_file_str[int(internal_call.source_mapping['start']):int(internal_call.source_mapping['start'])+int(internal_call.source_mapping['length'])].decode('utf-8').split('(')[0] + old_str_of_interest = in_file_str[int(internal_call.source_mapping['start']): + int(internal_call.source_mapping['start']) + + int(internal_call.source_mapping['length'])] + old_str_of_interest = old_str_of_interest.decode('utf-8').split('(')[0] patch = { "file" : in_file, "detector" : "naming-convention (function calls)", "start" : internal_call.source_mapping['start'], - "end" : int(internal_call.source_mapping['start']) + int(internal_call.source_mapping['length']) - len('('.join(in_file_str[int(internal_call.source_mapping['start']):int(internal_call.source_mapping['start'])+int(internal_call.source_mapping['length'])].decode('utf-8').split('(')[1:])) - 1, + "end" : int(internal_call.source_mapping['start']) + + int(internal_call.source_mapping['length']) - + len('('.join(in_file_str[int(internal_call.source_mapping['start']): + int(internal_call.source_mapping['start']) + + int(internal_call.source_mapping['length'])] \ + .decode('utf-8').split('(')[1:])) - 1, "old_string" : old_str_of_interest, "new_string" : old_str_of_interest[0].lower()+old_str_of_interest[1:] } @@ -264,7 +327,8 @@ class FormatNamingConvention: patches[in_file_relative].append(patch) @staticmethod - def create_patch_event_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end): + def create_patch_event_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, + modify_loc_end): target_contract = slither.get_contract_from_name(contract_name) if not target_contract: logger.error(red("Contract not found?!")) @@ -274,7 +338,9 @@ class FormatNamingConvention: event_name = name.split('(')[0] in_file_str = slither.source_code[in_file].encode('utf-8') old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+"event"+r'(.*)'+event_name, r'\1'+"event"+r'\2'+event_name[0].capitalize()+event_name[1:], old_str_of_interest.decode('utf-8'), 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+"event"+r'(.*)'+event_name, r'\1'+"event"+r'\2' + + event_name[0].capitalize()+event_name[1:], + old_str_of_interest.decode('utf-8'), 1) if num_repl != 0: patch = { "file" : in_file, @@ -303,20 +369,24 @@ class FormatNamingConvention: for call in node.internal_calls_as_expressions: if (str(call.called) == event_name): in_file_str = slither.source_code[in_file].encode('utf-8') - old_str_of_interest = in_file_str[int(call.source_mapping['start']):int(call.source_mapping['start'])+int(call.source_mapping['length'])] + old_str_of_interest = in_file_str[int(call.source_mapping['start']): + int(call.source_mapping['start']) + + int(call.source_mapping['length'])] patch = { "file" : in_file, "detector" : "naming-convention (event calls)", "start" : call.source_mapping['start'], "end" : int(call.source_mapping['start']) + int(call.source_mapping['length']), "old_string" : old_str_of_interest.decode('utf-8'), - "new_string" : old_str_of_interest.decode('utf-8')[0].capitalize()+old_str_of_interest.decode('utf-8')[1:] + "new_string" : old_str_of_interest.decode('utf-8')[0].capitalize() + + old_str_of_interest.decode('utf-8')[1:] } if not patch in patches[in_file_relative]: patches[in_file_relative].append(patch) @staticmethod - def create_patch_parameter_declaration(slither, patches, name, function_name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end): + def create_patch_parameter_declaration(slither, patches, name, function_name, contract_name, in_file, in_file_relative, + modify_loc_start, modify_loc_end): target_contract = slither.get_contract_from_name(contract_name) if not target_contract: logger.error(red("Contract not found?!")) @@ -326,9 +396,11 @@ class FormatNamingConvention: in_file_str = slither.source_code[in_file].encode('utf-8') old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] if(name[0] == '_'): - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name+r'(.*)', r'\1'+name[0]+name[1].upper()+name[2:]+r'\2', old_str_of_interest.decode('utf-8'), 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name+r'(.*)', r'\1'+name[0]+name[1].upper() + + name[2:]+r'\2', old_str_of_interest.decode('utf-8'), 1) else: - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name+r'(.*)', r'\1'+'_'+name[0].upper()+name[1:]+r'\2', old_str_of_interest.decode('utf-8'), 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name+r'(.*)', r'\1'+'_'+name[0].upper() + + name[1:]+r'\2', old_str_of_interest.decode('utf-8'), 1) if num_repl != 0: patch = { "file" : in_file, @@ -354,14 +426,22 @@ class FormatNamingConvention: for node in function.nodes: vars = node._expression_vars_written + node._expression_vars_read for v in vars: - if isinstance(v, Identifier) and str(v) == name and [str(lv) for lv in (node._local_vars_read+node._local_vars_written) if str(lv) == name]: + if isinstance(v, Identifier) and str(v) == name and [str(lv) for lv in + (node._local_vars_read + + node._local_vars_written) + if str(lv) == name]: modify_loc_start = int(v.source_mapping['start']) modify_loc_end = int(v.source_mapping['start']) + int(v.source_mapping['length']) old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] if(name[0] == '_'): - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name+r'(.*)', r'\1'+name[0]+name[1].upper()+name[2:]+r'\2', old_str_of_interest.decode('utf-8'), 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name+r'(.*)', + r'\1'+name[0]+name[1].upper()+name[2:] + + r'\2', old_str_of_interest.decode('utf-8'), + 1) else: - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name+r'(.*)', r'\1'+'_'+name[0].upper()+name[1:]+r'\2', old_str_of_interest.decode('utf-8'), 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name+r'(.*)', r'\1'+'_' + + name[0].upper()+name[1:]+r'\2', + old_str_of_interest.decode('utf-8'), 1) if num_repl != 0: patch = { "file" : in_file, @@ -381,17 +461,25 @@ class FormatNamingConvention: for modifier in function._expression_modifiers: for arg in modifier.arguments: if str(arg) == name: - old_str_of_interest = in_file_str[modifier.source_mapping['start']:modifier.source_mapping['start']+modifier.source_mapping['length']] - old_str_of_interest_beyond_modifier_name = old_str_of_interest.decode('utf-8').split('(')[1] + old_str_of_interest = in_file_str[modifier.source_mapping['start']: + modifier.source_mapping['start'] + + modifier.source_mapping['length']] + old_str_of_interest_beyond_modifier_name = old_str_of_interest.decode('utf-8')\ + .split('(')[1] if(name[0] == '_'): - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name+r'(.*)', r'\1'+name[0]+name[1].upper()+name[2:]+r'\2', old_str_of_interest_beyond_modifier_name, 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name+r'(.*)', r'\1'+name[0]+ + name[1].upper()+name[2:]+r'\2', + old_str_of_interest_beyond_modifier_name, 1) else: - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name+r'(.*)', r'\1'+'_'+name[0].upper()+name[1:]+r'\2', old_str_of_interest_beyond_modifier_name, 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name+r'(.*)', r'\1'+'_'+ + name[0].upper()+name[1:]+r'\2', + old_str_of_interest_beyond_modifier_name, 1) if num_repl != 0: patch = { "file" : in_file, "detector" : "naming-convention (parameter uses)", - "start" : modifier.source_mapping['start'] + len(old_str_of_interest.decode('utf-8').split('(')[0]) + 1, + "start" : modifier.source_mapping['start'] + + len(old_str_of_interest.decode('utf-8').split('(')[0]) + 1, "end" : modifier.source_mapping['start'] + modifier.source_mapping['length'], "old_string" : old_str_of_interest_beyond_modifier_name, "new_string" : new_str_of_interest @@ -403,7 +491,8 @@ class FormatNamingConvention: sys.exit(-1) @staticmethod - def create_patch_state_variable_declaration(slither, patches, _target, name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end): + def create_patch_state_variable_declaration(slither, patches, _target, name, contract_name, in_file, in_file_relative, + modify_loc_start, modify_loc_end): for contract in slither.contracts: if (contract.name == contract_name): for var in contract.state_variables: @@ -440,7 +529,10 @@ class FormatNamingConvention: for node in fm.nodes: vars = node._expression_vars_written + node._expression_vars_read for v in vars: - if isinstance(v, Identifier) and str(v) == name and [str(sv) for sv in (node._state_vars_read+node._state_vars_written) if str(sv) == name]: + if isinstance(v, Identifier) and str(v) == name and [str(sv) for sv in + (node._state_vars_read + + node._state_vars_written) + if str(sv) == name]: modify_loc_start = int(v.source_mapping['start']) modify_loc_end = int(v.source_mapping['start']) + int(v.source_mapping['length']) in_file_str = slither.source_code[in_file].encode('utf-8') @@ -462,14 +554,17 @@ class FormatNamingConvention: patches[in_file_relative].append(patch) @staticmethod - def create_patch_enum_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end): + def create_patch_enum_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, + modify_loc_end): for contract in slither.contracts: if (contract.name == contract_name): for enum in contract.enums: if (enum.name == name): in_file_str = slither.source_code[in_file].encode('utf-8') old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+"enum"+r'(.*)'+name, r'\1'+"enum"+r'\2'+name[0].capitalize()+name[1:], old_str_of_interest.decode('utf-8'), 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+"enum"+r'(.*)'+name, r'\1'+"enum"+r'\2'+ + name[0].capitalize()+name[1:], + old_str_of_interest.decode('utf-8'), 1) if num_repl != 0: patch = { "file" : in_file, @@ -498,8 +593,10 @@ class FormatNamingConvention: svs = contract.variables for sv in svs: if (str(sv.type) == contract_name + "." + name): - old_str_of_interest = in_file_str[sv.source_mapping['start']:(sv.source_mapping['start']+sv.source_mapping['length'])] - (new_str_of_interest, num_repl) = re.subn(name, name.capitalize(),old_str_of_interest.decode('utf-8'), 1) + old_str_of_interest = in_file_str[sv.source_mapping['start']:(sv.source_mapping['start']+ + sv.source_mapping['length'])] + (new_str_of_interest, num_repl) = re.subn(name, name.capitalize(), + old_str_of_interest.decode('utf-8'), 1) patch = { "file" : in_file, "detector" : "naming-convention (enum use)", @@ -517,8 +614,10 @@ class FormatNamingConvention: # Enum declarations for v in fm.variables: if (str(v.type) == contract_name + "." + name): - old_str_of_interest = in_file_str[v.source_mapping['start']:(v.source_mapping['start']+v.source_mapping['length'])] - (new_str_of_interest, num_repl) = re.subn(name, name.capitalize(),old_str_of_interest.decode('utf-8'), 1) + old_str_of_interest = in_file_str[v.source_mapping['start']:(v.source_mapping['start']+ + v.source_mapping['length'])] + (new_str_of_interest, num_repl) = re.subn(name, name.capitalize(), + old_str_of_interest.decode('utf-8'), 1) patch = { "file" : in_file, "detector" : "naming-convention (enum use)", @@ -535,17 +634,27 @@ class FormatNamingConvention: for ir in node.irs: if isinstance(ir, Member): if str(ir.variable_left) == name: - old_str_of_interest = in_file_str[node.source_mapping['start']:(node.source_mapping['start']+node.source_mapping['length'])].decode('utf-8').split('=')[1] + old_str_of_interest = in_file_str[node.source_mapping['start']: + (node.source_mapping['start']+ + node.source_mapping['length'])].decode('utf-8')\ + .split('=')[1] m = re.search(r'(.*)'+name, old_str_of_interest) old_str_of_interest = old_str_of_interest[m.span()[0]:] - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name, r'\1'+name[0].upper()+name[1:], old_str_of_interest, 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+name, r'\1'+name[0].upper()+name[1:], + old_str_of_interest, 1) if num_repl != 0: patch = { "file" : in_file, "detector" : "naming-convention (enum use)", - "start" : node.source_mapping['start'] + len(in_file_str[node.source_mapping['start']:(node.source_mapping['start']+node.source_mapping['length'])].decode('utf-8').split('=')[0]) + 1 + m.span()[0], - "end" : node.source_mapping['start'] + len(in_file_str[node.source_mapping['star\ -t']:(node.source_mapping['start']+node.source_mapping['length'])].decode('utf-8').split('=')[0]) + 1 + m.span()[0] + len(old_str_of_interest), + "start" : node.source_mapping['start'] + + len(in_file_str[node.source_mapping['start']: + (node.source_mapping['start']+ + node.source_mapping['length'])].decode('utf-8').split('=')[0]) + + 1 + m.span()[0], + "end" : node.source_mapping['start'] + + len(in_file_str[node.source_mapping['start']:(node.source_mapping['start']+ + node.source_mapping['length'])].\ + decode('utf-8').split('=')[0]) + 1 + m.span()[0] + len(old_str_of_interest), "old_string" : old_str_of_interest, "new_string" : new_str_of_interest } @@ -557,14 +666,17 @@ t']:(node.source_mapping['start']+node.source_mapping['length'])].decode('utf-8' # To-do: Check any other place/way where enum type is used @staticmethod - def create_patch_struct_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, modify_loc_end): + def create_patch_struct_definition(slither, patches, name, contract_name, in_file, in_file_relative, modify_loc_start, + modify_loc_end): for contract in slither.contracts: if (contract.name == contract_name): for struct in contract.structures: if (struct.name == name): in_file_str = slither.source_code[in_file].encode('utf-8') old_str_of_interest = in_file_str[modify_loc_start:modify_loc_end] - (new_str_of_interest, num_repl) = re.subn(r'(.*)'+"struct"+r'(.*)'+name, r'\1'+"struct"+r'\2'+name[0].capitalize()+name[1:], old_str_of_interest.decode('utf-8'), 1) + (new_str_of_interest, num_repl) = re.subn(r'(.*)'+"struct"+r'(.*)'+name, r'\1'+"struct"+r'\2'+ + name[0].capitalize()+name[1:], + old_str_of_interest.decode('utf-8'), 1) if num_repl != 0: patch = { "file" : in_file, @@ -593,8 +705,10 @@ t']:(node.source_mapping['start']+node.source_mapping['length'])].decode('utf-8' svs = contract.variables for sv in svs: if (str(sv.type) == contract_name + "." + name): - old_str_of_interest = in_file_str[sv.source_mapping['start']:(sv.source_mapping['start']+sv.source_mapping['length'])] - (new_str_of_interest, num_repl) = re.subn(name, name.capitalize(),old_str_of_interest.decode('utf-8'), 1) + old_str_of_interest = in_file_str[sv.source_mapping['start']:(sv.source_mapping['start']+ + sv.source_mapping['length'])] + (new_str_of_interest, num_repl) = re.subn(name, name.capitalize(), + old_str_of_interest.decode('utf-8'), 1) patch = { "file" : in_file, "detector" : "naming-convention (struct use)", @@ -611,8 +725,10 @@ t']:(node.source_mapping['start']+node.source_mapping['length'])].decode('utf-8' for fm in fms: for v in fm.variables: if (str(v.type) == contract_name + "." + name): - old_str_of_interest = in_file_str[v.source_mapping['start']:(v.source_mapping['start']+v.source_mapping['length'])] - (new_str_of_interest, num_repl) = re.subn(name, name.capitalize(),old_str_of_interest.decode('utf-8'), 1) + old_str_of_interest = in_file_str[v.source_mapping['start']:(v.source_mapping['start']+ + v.source_mapping['length'])] + (new_str_of_interest, num_repl) = re.subn(name, name.capitalize(), + old_str_of_interest.decode('utf-8'), 1) patch = { "file" : in_file, "detector" : "naming-convention (struct use)", diff --git a/utils/slither_format/format_pragma.py b/utils/slither_format/format_pragma.py index 4f486e656..db4dce905 100644 --- a/utils/slither_format/format_pragma.py +++ b/utils/slither_format/format_pragma.py @@ -25,7 +25,10 @@ class FormatPragma: versions_used.append(''.join(element['type_specific_fields']['directive'][1:])) solc_version_replace = FormatPragma.analyse_versions(versions_used) for element in elements: - FormatPragma.create_patch(slither, patches, element['source_mapping']['filename_absolute'], element['source_mapping']['filename_relative'], solc_version_replace, element['source_mapping']['start'], element['source_mapping']['start'] + element['source_mapping']['length']) + FormatPragma.create_patch(slither, patches, element['source_mapping']['filename_absolute'], \ + element['source_mapping']['filename_relative'], solc_version_replace, \ + element['source_mapping']['start'], element['source_mapping']['start'] + \ + element['source_mapping']['length']) @staticmethod def analyse_versions(used_solc_versions): diff --git a/utils/slither_format/format_solc_version.py b/utils/slither_format/format_solc_version.py index 73bb5c323..6584020ee 100644 --- a/utils/slither_format/format_solc_version.py +++ b/utils/slither_format/format_solc_version.py @@ -21,8 +21,12 @@ class FormatSolcVersion: @staticmethod def format(slither, patches, elements): for element in elements: - solc_version_replace = FormatSolcVersion.determine_solc_version_replacement(''.join(element['type_specific_fields']['directive'][1:])) - FormatSolcVersion.create_patch(slither, patches, element['source_mapping']['filename_absolute'], element['source_mapping']['filename_relative'], solc_version_replace, element['source_mapping']['start'], element['source_mapping']['start'] + element['source_mapping']['length']) + solc_version_replace = FormatSolcVersion.determine_solc_version_replacement( + ''.join(element['type_specific_fields']['directive'][1:])) + FormatSolcVersion.create_patch(slither, patches, element['source_mapping']['filename_absolute'], \ + element['source_mapping']['filename_relative'], solc_version_replace, \ + element['source_mapping']['start'], element['source_mapping']['start'] + + element['source_mapping']['length']) @staticmethod def determine_solc_version_replacement(used_solc_version): diff --git a/utils/slither_format/format_unused_state.py b/utils/slither_format/format_unused_state.py index 1f8921b21..3fc40b19c 100644 --- a/utils/slither_format/format_unused_state.py +++ b/utils/slither_format/format_unused_state.py @@ -4,7 +4,9 @@ class FormatUnusedState: def format(slither, patches, elements): for element in elements: if element['type'] == "variable": - FormatUnusedState.create_patch(slither, patches, element['source_mapping']['filename_absolute'], element['source_mapping']['filename_relative'], element['source_mapping']['start']) + FormatUnusedState.create_patch(slither, patches, element['source_mapping']['filename_absolute'], \ + element['source_mapping']['filename_relative'], \ + element['source_mapping']['start']) @staticmethod def create_patch(slither, patches, in_file, in_file_relative, modify_loc_start): @@ -15,7 +17,8 @@ class FormatUnusedState: "detector" : "unused-state", "start" : modify_loc_start, "end" : modify_loc_start + len(old_str_of_interest.decode('utf-8').partition(';')[0]) + 1, - "old_string" : old_str_of_interest.decode('utf-8').partition(';')[0] + old_str_of_interest.decode('utf-8').partition(';')[1], + "old_string" : old_str_of_interest.decode('utf-8').partition(';')[0] + + old_str_of_interest.decode('utf-8').partition(';')[1], "new_string" : "" })