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