blob: f984eed86e4a793478f8b82cb6433135d2b633ae [file] [log] [blame]
Paul Herman10bc1a42015-08-18 22:46:57 +00001"""
2Test scopes in C++.
3"""
4import lldb
5from lldbtest import *
6import lldbutil
7
8class TestCppScopes(TestBase):
Chaoren Lin464be902015-08-19 04:08:56 +00009
Paul Herman10bc1a42015-08-18 22:46:57 +000010 mydir = TestBase.compute_mydir(__file__)
Chaoren Lin464be902015-08-19 04:08:56 +000011
Chaoren Lin9a379c12015-08-19 06:21:09 +000012 @expectedFailureDarwin
Zachary Turner6e8cbc02015-09-11 19:59:39 +000013 @expectedFailureWindows("llvm.org/pr24764")
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000014 def test_with_run_command(self):
15 self.build()
Paul Herman10bc1a42015-08-18 22:46:57 +000016
Paul Herman10bc1a42015-08-18 22:46:57 +000017 # Get main source file
18 src_file = "main.cpp"
19 src_file_spec = lldb.SBFileSpec(src_file)
20 self.assertTrue(src_file_spec.IsValid(), "Main source file")
Chaoren Lin464be902015-08-19 04:08:56 +000021
Paul Herman10bc1a42015-08-18 22:46:57 +000022 # Get the path of the executable
Chaoren Lin464be902015-08-19 04:08:56 +000023 cwd = os.getcwd()
Paul Herman10bc1a42015-08-18 22:46:57 +000024 exe_file = "a.out"
25 exe_path = os.path.join(cwd, exe_file)
Chaoren Lin464be902015-08-19 04:08:56 +000026
Paul Herman10bc1a42015-08-18 22:46:57 +000027 # Load the executable
28 target = self.dbg.CreateTarget(exe_path)
29 self.assertTrue(target.IsValid(), VALID_TARGET)
30
31 # Break on main function
32 main_breakpoint = target.BreakpointCreateBySourceRegex("// break here", src_file_spec)
33 self.assertTrue(main_breakpoint.IsValid() and main_breakpoint.GetNumLocations() >= 1, VALID_BREAKPOINT)
34
35 # Launch the process
36 args = None
37 env = None
Chaoren Lin464be902015-08-19 04:08:56 +000038 process = target.LaunchSimple(args, env, self.get_process_working_directory())
Paul Herman10bc1a42015-08-18 22:46:57 +000039 self.assertTrue(process.IsValid(), PROCESS_IS_VALID)
40
41 # Get the thread of the process
42 self.assertTrue(process.GetState() == lldb.eStateStopped, PROCESS_STOPPED)
43 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
44
Chaoren Lin464be902015-08-19 04:08:56 +000045 # Get current fream of the thread at the breakpoint
Paul Herman10bc1a42015-08-18 22:46:57 +000046 frame = thread.GetSelectedFrame()
47
Chaoren Lin464be902015-08-19 04:08:56 +000048 # Test result for scopes of variables
Paul Herman10bc1a42015-08-18 22:46:57 +000049
50 global_variables = frame.GetVariables(True, True, True, False)
Chaoren Lin464be902015-08-19 04:08:56 +000051 global_variables_assert = {
Paul Herman10bc1a42015-08-18 22:46:57 +000052 'A::a': 1111,
53 'B::a': 2222,
54 'C::a': 3333,
55 '::a': 4444,
56 'a': 4444
57 }
58
59 self.assertTrue(global_variables.GetSize() == 4, "target variable returns all variables")
60 for variable in global_variables:
Chaoren Lin464be902015-08-19 04:08:56 +000061 name = variable.GetName()
Paul Herman10bc1a42015-08-18 22:46:57 +000062 self.assertTrue(name in global_variables_assert, "target variable returns wrong variable " + name)
63
64 for name in global_variables_assert:
65 value = frame.EvaluateExpression(name)
66 assert_value = global_variables_assert[name]
67 self.assertTrue(value.IsValid() and value.GetValueAsSigned() == assert_value, name + " = " + str(assert_value))