1"""
2Test that SBType returns SBModule of executable file but not
3of compile unit's object file.
4"""
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9import lldbsuite.test.lldbutil as lldbutil
10
11
12class TestTypeGetModule(TestBase):
13
14    def find_module(self, target, name):
15        num_modules = target.GetNumModules()
16        index = 0
17        result = lldb.SBModule()
18
19        while index < num_modules:
20            module = target.GetModuleAtIndex(index)
21            if module.GetFileSpec().GetFilename() == name:
22                result = module
23                break
24
25            index += 1
26
27        self.assertTrue(result.IsValid())
28        return result
29
30    def find_comp_unit(self, exe_module, name):
31        num_comp_units = exe_module.GetNumCompileUnits()
32        index = 0
33        result = lldb.SBCompileUnit()
34
35        while index < num_comp_units:
36            comp_unit = exe_module.GetCompileUnitAtIndex(index)
37            if comp_unit.GetFileSpec().GetFilename() == name:
38                result = comp_unit
39                break
40
41            index += 1
42
43        self.assertTrue(result.IsValid())
44        return result
45
46    def find_type(self, type_list, name):
47        num_types = type_list.GetSize()
48        index = 0
49        result = lldb.SBType()
50
51        while index < num_types:
52            type = type_list.GetTypeAtIndex(index)
53            if type.GetName() == name:
54                result = type
55                break
56
57            index += 1
58
59        self.assertTrue(result.IsValid())
60        return result
61
62    def test(self):
63        self.build()
64        target  = lldbutil.run_to_breakpoint_make_target(self)
65        exe_module = self.find_module(target, 'a.out')
66
67        num_comp_units = exe_module.GetNumCompileUnits()
68        self.assertGreaterEqual(num_comp_units, 3)
69
70        comp_unit = self.find_comp_unit(exe_module, 'compile_unit1.c')
71        cu_type = self.find_type(comp_unit.GetTypes(), 'compile_unit1_type')
72        self.assertEqual(exe_module, cu_type.GetModule())
73
74        comp_unit = self.find_comp_unit(exe_module, 'compile_unit2.c')
75        cu_type = self.find_type(comp_unit.GetTypes(), 'compile_unit2_type')
76        self.assertEqual(exe_module, cu_type.GetModule())
77