• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Tests imported namespaces in C++.
3"""
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class TestCppNsImport(TestBase):
11
12    mydir = TestBase.compute_mydir(__file__)
13
14    def test_with_run_command(self):
15        """Tests imported namespaces in C++."""
16        self.build()
17
18        # Get main source file
19        src_file = os.path.join(self.getSourceDir(), "main.cpp")
20        src_file_spec = lldb.SBFileSpec(src_file)
21        self.assertTrue(src_file_spec.IsValid(), "Main source file")
22
23        # Get the path of the executable
24        exe_path = self.getBuildArtifact("a.out")
25
26        # Load the executable
27        target = self.dbg.CreateTarget(exe_path)
28        self.assertTrue(target.IsValid(), VALID_TARGET)
29
30        # Break on main function
31        break_0 = target.BreakpointCreateBySourceRegex(
32            "// break 0", src_file_spec)
33        self.assertTrue(
34            break_0.IsValid() and break_0.GetNumLocations() >= 1,
35            VALID_BREAKPOINT)
36        break_1 = target.BreakpointCreateBySourceRegex(
37            "// break 1", src_file_spec)
38        self.assertTrue(
39            break_1.IsValid() and break_1.GetNumLocations() >= 1,
40            VALID_BREAKPOINT)
41
42        # Launch the process
43        args = None
44        env = None
45        process = target.LaunchSimple(
46            args, env, self.get_process_working_directory())
47        self.assertTrue(process.IsValid(), PROCESS_IS_VALID)
48
49        # Get the thread of the process
50        self.assertTrue(
51            process.GetState() == lldb.eStateStopped,
52            PROCESS_STOPPED)
53        thread = lldbutil.get_stopped_thread(
54            process, lldb.eStopReasonBreakpoint)
55
56        # Get current fream of the thread at the breakpoint
57        frame = thread.GetSelectedFrame()
58
59        # Test imported namespaces
60        test_result = frame.EvaluateExpression("n")
61        self.assertTrue(
62            test_result.IsValid() and test_result.GetValueAsSigned() == 1,
63            "n = 1")
64
65        test_result = frame.EvaluateExpression("N::n")
66        self.assertTrue(
67            test_result.IsValid() and test_result.GetValueAsSigned() == 1,
68            "N::n = 1")
69
70        test_result = frame.EvaluateExpression("nested")
71        self.assertTrue(
72            test_result.IsValid() and test_result.GetValueAsSigned() == 3,
73            "nested = 3")
74
75        test_result = frame.EvaluateExpression("anon")
76        self.assertTrue(
77            test_result.IsValid() and test_result.GetValueAsSigned() == 2,
78            "anon = 2")
79
80        test_result = frame.EvaluateExpression("global")
81        self.assertTrue(
82            test_result.IsValid() and test_result.GetValueAsSigned() == 4,
83            "global = 4")
84
85        test_result = frame.EvaluateExpression("fun_var")
86        self.assertTrue(
87            test_result.IsValid() and test_result.GetValueAsSigned() == 9,
88            "fun_var = 9")
89
90        test_result = frame.EvaluateExpression("Fun::fun_var")
91        self.assertTrue(
92            test_result.IsValid() and test_result.GetValueAsSigned() == 0,
93            "Fun::fun_var = 0")
94
95        test_result = frame.EvaluateExpression("not_imported")
96        self.assertTrue(
97            test_result.IsValid() and test_result.GetValueAsSigned() == 35,
98            "not_imported = 35")
99
100        # Currently there is no way to distinguish between "::imported" and "imported" in ClangExpressionDeclMap so this fails
101        #test_result = frame.EvaluateExpression("::imported")
102        #self.assertTrue(test_result.IsValid() and test_result.GetValueAsSigned() == 89, "::imported = 89")
103
104        test_result = frame.EvaluateExpression("Imported::imported")
105        self.assertTrue(
106            test_result.IsValid() and test_result.GetValueAsSigned() == 99,
107            "Imported::imported = 99")
108
109        test_result = frame.EvaluateExpression("imported")
110        self.assertTrue(
111            test_result.IsValid() and test_result.GetValueAsSigned() == 99,
112            "imported = 99")
113
114        test_result = frame.EvaluateExpression("single")
115        self.assertTrue(
116            test_result.IsValid() and test_result.GetValueAsSigned() == 3,
117            "single = 3")
118
119        # Continue to second breakpoint
120        process.Continue()
121
122        # Get the thread of the process
123        self.assertTrue(
124            process.GetState() == lldb.eStateStopped,
125            PROCESS_STOPPED)
126        thread = lldbutil.get_stopped_thread(
127            process, lldb.eStopReasonBreakpoint)
128
129        # Get current fream of the thread at the breakpoint
130        frame = thread.GetSelectedFrame()
131
132        # Test function inside namespace
133        test_result = frame.EvaluateExpression("fun_var")
134        self.assertTrue(
135            test_result.IsValid() and test_result.GetValueAsSigned() == 5,
136            "fun_var = 5")
137