1"""
2Test scopes in C++.
3"""
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class TestCppScopes(TestBase):
11
12    mydir = TestBase.compute_mydir(__file__)
13
14    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")
15    def test_all_but_c(self):
16        self.do_test(False)
17
18    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")
19    def test_c(self):
20        self.do_test(True)
21
22    def do_test(self, test_c):
23        self.build()
24
25        # Get main source file
26        src_file = os.path.join(self.getSourceDir(), "main.cpp")
27        src_file_spec = lldb.SBFileSpec(src_file)
28        self.assertTrue(src_file_spec.IsValid(), "Main source file")
29
30        # Get the path of the executable
31        exe_path = self.getBuildArtifact("a.out")
32
33        # Load the executable
34        target = self.dbg.CreateTarget(exe_path)
35        self.assertTrue(target.IsValid(), VALID_TARGET)
36
37        # Break on main function
38        main_breakpoint = target.BreakpointCreateBySourceRegex(
39            "// break here", src_file_spec)
40        self.assertTrue(
41            main_breakpoint.IsValid() and main_breakpoint.GetNumLocations() >= 1,
42            VALID_BREAKPOINT)
43
44        # Launch the process
45        args = None
46        env = None
47        process = target.LaunchSimple(
48            args, env, self.get_process_working_directory())
49        self.assertTrue(process.IsValid(), PROCESS_IS_VALID)
50
51        # Get the thread of the process
52        self.assertTrue(
53            process.GetState() == lldb.eStateStopped,
54            PROCESS_STOPPED)
55        thread = lldbutil.get_stopped_thread(
56            process, lldb.eStopReasonBreakpoint)
57
58        # Get current fream of the thread at the breakpoint
59        frame = thread.GetSelectedFrame()
60
61        # Test result for scopes of variables
62
63        global_variables = frame.GetVariables(True, True, True, False)
64        global_variables_assert = {
65            'A::a': 1111,
66            'B::a': 2222,
67            'C::a': 3333,
68            '::a': 4444,
69            'a': 4444
70        }
71
72        self.assertTrue(
73            global_variables.GetSize() == 4,
74            "target variable returns all variables")
75        for variable in global_variables:
76            name = variable.GetName()
77            self.assertTrue(
78                name in global_variables_assert,
79                "target variable returns wrong variable " + name)
80
81        for name in global_variables_assert:
82            if name is "C::a" and not test_c:
83                continue
84            if name is not "C::a" and test_c:
85                continue
86
87            value = frame.EvaluateExpression(name)
88            assert_value = global_variables_assert[name]
89            self.assertTrue(
90                value.IsValid() and value.GetValueAsSigned() == assert_value,
91                name + " = " + str(assert_value))
92