Пример #1
0
    def test_insert_phi_function(self):
        as_tree = ast.parse(
            ms("""\
                    a = 3           # 1st
                    if a > 3:       #  |
                        a = 3       # 2nd
                        b = 3
                    else:           # 3rd
                        z = 4       #  |
                    # expected phi func for 'a' here
                    y = a           # 4th
                    """))
        cfg_real = Cfg(as_tree)
        cfg_real.fill_df()
        cfg_real.gather_initial_info()
        cfg_real.ins_phi_function_semi_pruned()

        expected_phi_list_for_block_3 = {'a'}
        self.assertSetEqual(cfg_real.block_list[-1].phi,
                            expected_phi_list_for_block_3)
Пример #2
0
    def test_insert_phi_function_semi_pruned(self):
        """
                        Note: '|' with no arrows means pointing down

                         A
                         |
                         B   <------|
                      /    \        |
                     C      F       |
                     |    /  \      |
                     |    G   I     |
                     |    \   /     |
                     |      H       |
                      \    /        |
                        D-----------|
                        |
                        E
                """
        blocks, ast_string = th.build_blocks_arb(block_links={
            'A': ['B'],
            'B': ['C', 'F'],
            'C': ['D'],
            'D': ['E', 'B'],
            'E': [],
            'F': ['G', 'I'],
            'G': ['H'],
            'H': ['D'],
            'I': ['H']
        },
                                                 code={
                                                     'A':
                                                     ms("""\
                                                            i = 1
                                                            """),
                                                     'B':
                                                     ms("""\
                                                            a = temp_0
                                                            c = temp_1
                                                            """),
                                                     'C':
                                                     ms("""\
                                                            b = temp_2
                                                            c = temp_3
                                                            d = temp_4
                                                            """),
                                                     'D':
                                                     ms("""\
                                                            y = a + b
                                                            z = c + d
                                                            i = i + 1
                                                            if i < 100:
                                                                pass
                                                            """),
                                                     'E':
                                                     "return\n",
                                                     'F':
                                                     ms("""\
                                                            a = temp_5
                                                            d = temp_6
                                                            if a < d:
                                                                pass
                                                            """),
                                                     'G':
                                                     ms("""\
                                                            d = temp
                                                            """),
                                                     'H':
                                                     ms("""\
                                                            b = temp
                                                            """),
                                                     'I':
                                                     ms("""\
                                                            c = temp
                                                            """)
                                                 })
        as_tree = ast.parse(ast_string)
        cfg_real = Cfg()
        cfg_real.block_list = blocks
        cfg_real.as_tree = as_tree
        cfg_real.root = cfg_real.block_list[0]
        cfg_real.fill_df()
        cfg_real.gather_initial_info()
        cfg_real.ins_phi_function_semi_pruned()

        expected_phi_list = {
            'A': set(),
            'B': {'a', 'b', 'c', 'd', 'i'},
            'C': set(),
            'D': {'a', 'b', 'c', 'd'},
            'E': set(),
            'F': set(),
            'G': set(),
            'H': {'c', 'd'},
            'I': set()
        }

        self.assertPhiListEqual(cfg_real.block_list, expected_phi_list)