def _calculate_intra_method(self):
     logger.debug("Finding intra method pairs in class {c}",
                  c=self.class_name)
     total_intramethod_pairs = set()
     for name, method_cfg in self.methods.items():
         intramethod_pairs = rd.definition_use_pairs(method_cfg.cfg.g)
         total_intramethod_pairs.update(only_lines(intramethod_pairs))
     return total_intramethod_pairs
    def test_def_use_pairs(self):
        sample_function = nx.max_weight_matching

        cfg: CFG = du.try_create_cfg_with_definitions_and_uses(sample_function)
        pairs = rd.definition_use_pairs(cfg.g)

        # for b in um.grouper_it(7, pairs):
        #     print(", ".join([str((p.definition.line, p.use.line)) for p in b])+", ")

        pairs_expected = [
            (255, 256), (255, 257), (255, 257), (256, 257), (258, 257), (253, 257), (256, 258),
            (260, 259), (254, 259), (256, 259), (258, 316), (253, 316), (724, 725), (735, 736),
            (735, 736), (735, 737), (791, 752), (740, 752), (755, 756), (755, 759), (759, 760),
            (755, 760), (755, 763), (759, 764), (763, 765), (764, 765), (755, 768), (759, 768),
            (755, 769), (759, 769), (769, 770), (755, 772), (759, 772), (759, 772), (755, 772),
            (755, 773), (759, 773), (764, 774), (759, 777), (755, 777), (764, 778), (362, 782),
            (755, 782), (759, 782), (782, 783), (399, 786), (782, 786), (755, 786), (759, 786),
            (634, 790), (755, 790), (759, 790), (759, 793), (764, 798), (759, 799), (755, 800),
            (759, 800), (759, 800), (764, 801), (763, 804), (769, 805), (846, 805), (763, 805),
            (755, 806), (759, 806), (763, 806), (759, 807), (759, 811), (769, 812), (846, 812),
            (759, 812), (755, 813), (759, 813), (759, 813), (791, 815), (740, 815), (832, 833),
            (832, 834), (832, 835), (822, 836), (827, 836), (838, 836), (835, 836), (823, 836),
            (837, 836), (828, 836), (835, 837), (832, 839), (843, 844), (843, 844), (843, 845),
            (843, 846), (260, 847), (254, 847), (846, 848), (846, 849), (846, 851), (854, 852),
            (822, 852), (838, 852), (827, 852), (849, 852), (851, 852), (823, 852), (853, 852),
            (837, 852), (828, 852), (849, 853), (851, 853), (843, 855), (858, 859), (858, 859),
            (854, 860), (822, 860), (827, 860), (862, 860), (838, 860), (858, 860), (823, 860),
            (853, 860), (837, 860), (828, 860), (861, 860), (858, 861), (858, 863), (854, 865),
            (822, 865), (862, 865), (838, 865), (827, 865), (874, 875), (874, 877), (823, 877),
            (853, 877), (837, 877), (828, 877), (861, 877), (871, 877), (874, 878), (874, 880),
            (823, 880), (853, 880), (837, 880), (828, 880), (861, 880), (871, 880), (881, 882),
            (881, 883), (881, 885), (823, 885), (853, 885), (837, 885), (828, 885), (861, 885),
            (871, 885), (881, 886), (881, 888), (823, 888), (853, 888), (837, 888), (828, 888),
            (861, 888), (871, 888), (854, 891), (822, 891), (862, 891), (870, 891), (838, 891),
            (827, 891), (854, 894), (822, 894), (827, 894), (862, 894), (870, 894), (838, 894),
            (839, 896), (855, 896), (823, 896), (896, 897), (896, 898), (896, 898), (896, 898),
            (896, 898), (896, 899), (854, 900), (822, 900), (827, 900), (862, 900), (870, 900),
            (838, 900), (839, 902), (855, 902), (823, 902), (902, 903), (902, 903), (902, 903),
            (902, 903), (902, 904), (902, 905), (854, 906), (822, 906), (827, 906), (862, 906),
            (870, 906), (838, 906), (863, 908), (823, 908), (913, 914), (913, 914), (791, 917),
            (740, 917), (921, 922), (921, 924), (921, 924), (921, 925), (921, 926), (260, 929),
            (254, 929), (667, 930),
        ]

        self.assertEqual(len(pairs), len(pairs_expected))

        def determine(el):
            for pair in pairs:
                if pair.definition.line == el[0] and pair.use.line == el[1]:
                    return True
            return False

        not_found_pairs = list(filterfalse(determine, pairs_expected))
        self.assertEqual(0, len(not_found_pairs))
    def test_intermethod_in_init_linked_list(self):
        self.module_cfg = ModuleCFG(str(linked_list))
        self.cls_cfg = self.module_cfg.class_cfgs["LinkedList"]

        interclass_only_method_cfg = self.cls_cfg.methods["__init__"]
        inter_method_pairs = rd.definition_use_pairs(interclass_only_method_cfg.extended_cfg.g,
                                                     intermethod_only=True,
                                                     object_vars_only=True
                                                     )

        self.assertEqual(3, len(inter_method_pairs))
 def _calculate_intermethod(self):
     logger.debug("Finding inter method pairs in class {c}",
                  c=self.class_name)
     total_intermethod_pairs = set()
     for name, method_cfg in self.methods.items():
         intermethod_pairs = rd.definition_use_pairs(
             method_cfg.extended_cfg.g,
             intermethod_only=True,
             object_vars_only=True)
         total_intermethod_pairs.update(only_lines(intermethod_pairs))
     return total_intermethod_pairs
 def test_intermethod_pairs_include_some_interclass_pairs(self):
     interclass_only_method_cfg = self.cls_cfg.methods["interclass_only"]
     ic1_method_cfg = self.cls_cfg.methods["ic1"]
     ic2_method_cfg = self.cls_cfg.methods["ic2"]
     inter_method_pairs = rd.definition_use_pairs(interclass_only_method_cfg.extended_cfg.g,
                                                  object_vars_only=True,
                                                  intermethod_only=True
                                                  )
     inter_class_pairs = ic.inter_class_def_use_pairs_cfg(ic1_method_cfg.extended_cfg,
                                                      ic2_method_cfg.extended_cfg
                                                      )
     self.assertEqual(1, len(inter_class_pairs))
     self.assertEqual(2, len(inter_method_pairs))
     self.assertEqual(2, len(set(inter_method_pairs) - set(inter_class_pairs)))
def inter_class_def_use_pairs_cfg(cfg1: CFG, cfg2: CFG):
    reach_out_1 = rd.reaching_definitions(cfg1.g, object_vars_only=True)

    cfg1_exit = cfg1.exit_label
    cfg2_entry = cfg2.entry_label

    reach_out_cfg1_exit = reach_out_1[cfg1_exit]

    # reach_out_cfg1_exit = {definition for definition in reach_out_cfg1_exit
    #                        if definition.varname[:5] == "self."}

    initial_set = {cfg2_entry: reach_out_cfg1_exit}

    pairs = rd.definition_use_pairs(cfg2.g, initial_set=initial_set, object_vars_only=True)

    return pairs
Beispiel #7
0
 def create(function: Function, calls=None, filter_self=True):
     cfg = du.try_create_cfg_with_definitions_and_uses(
         function.func,
         definition_line=function.first_line,
         args=function.argument_names
     )
     if not cfg:
         logger.warning("Could not create cfg for function {f}", f=function.func.__name__)
         return None
     if calls:
         cfg.expose_call_sites(calls)
     pairs = rd.definition_use_pairs(cfg.g)
     c_uses, p_uses = get_all_c_all_p_uses(function.tree)
     m = FunctionCFG(cfg=cfg,
                     pairs=pairs,
                     c_uses=c_uses,
                     p_uses=p_uses,
                     line_start=function.first_line,
                     line_end=function.end_line,
                     filter_self=filter_self)
     return m