blob: 8b4f55666fbed1ae523c515a15b76d970eeff7a6 [file] [log] [blame]
Johnny Chen377a8ed2010-07-28 21:24:31 +00001"""
Johnny Chen5fede4b2011-10-31 23:35:33 +00002Test some expressions involving STL data types.
Johnny Chen377a8ed2010-07-28 21:24:31 +00003"""
4
5import os, time
Johnny Chen73258832010-08-05 23:42:46 +00006import unittest2
Johnny Chen377a8ed2010-07-28 21:24:31 +00007import lldb
Johnny Chen15f247a2012-02-03 20:43:00 +00008import lldbutil
Johnny Chen17941842010-08-09 23:44:24 +00009from lldbtest import *
Johnny Chen377a8ed2010-07-28 21:24:31 +000010
Johnny Chencbb4be02010-09-01 19:59:58 +000011class STLTestCase(TestBase):
Johnny Chen377a8ed2010-07-28 21:24:31 +000012
Johnny Chen8d798eb2011-06-25 21:07:03 +000013 mydir = os.path.join("lang", "cpp", "stl")
Johnny Chen377a8ed2010-07-28 21:24:31 +000014
Johnny Chen9b547242011-11-14 18:33:39 +000015 # rdar://problem/10400981
Johnny Chen5fede4b2011-10-31 23:35:33 +000016 @unittest2.expectedFailure
Johnny Chen165a0792010-09-07 18:27:35 +000017 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chen24086bc2012-04-06 19:54:10 +000018 @dsym_test
Johnny Chen165a0792010-09-07 18:27:35 +000019 def test_with_dsym(self):
Johnny Chen5fede4b2011-10-31 23:35:33 +000020 """Test some expressions involving STL data types."""
Johnny Chen165a0792010-09-07 18:27:35 +000021 self.buildDsym()
Johnny Chen5fede4b2011-10-31 23:35:33 +000022 self.step_stl_exprs()
Johnny Chen165a0792010-09-07 18:27:35 +000023
Johnny Chen9b547242011-11-14 18:33:39 +000024 # rdar://problem/10400981
Johnny Chen5fede4b2011-10-31 23:35:33 +000025 @unittest2.expectedFailure
Johnny Chen24086bc2012-04-06 19:54:10 +000026 @dwarf_test
Johnny Chen165a0792010-09-07 18:27:35 +000027 def test_with_dwarf(self):
Johnny Chen5fede4b2011-10-31 23:35:33 +000028 """Test some expressions involving STL data types."""
Johnny Chen165a0792010-09-07 18:27:35 +000029 self.buildDwarf()
Johnny Chen5fede4b2011-10-31 23:35:33 +000030 self.step_stl_exprs()
Johnny Chen165a0792010-09-07 18:27:35 +000031
Johnny Chen15f247a2012-02-03 20:43:00 +000032 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000033 @dsym_test
Johnny Chen15f247a2012-02-03 20:43:00 +000034 def test_SBType_template_aspects_with_dsym(self):
Johnny Chen9dad8ae2012-02-03 20:50:56 +000035 """Test APIs for getting template arguments from an SBType."""
Johnny Chen15f247a2012-02-03 20:43:00 +000036 self.buildDsym()
37 self.sbtype_template_apis()
38
39 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000040 @dwarf_test
Johnny Chen15f247a2012-02-03 20:43:00 +000041 def test_SBType_template_aspects_with_dwarf(self):
Johnny Chen9dad8ae2012-02-03 20:50:56 +000042 """Test APIs for getting template arguments from an SBType."""
Johnny Chen15f247a2012-02-03 20:43:00 +000043 self.buildDwarf()
44 self.sbtype_template_apis()
45
Johnny Chen77ca1a42010-10-12 22:53:02 +000046 def setUp(self):
Johnny Chenaadcef52010-10-14 17:31:24 +000047 # Call super's setUp().
48 TestBase.setUp(self)
Johnny Chen77ca1a42010-10-12 22:53:02 +000049 # Find the line number to break inside main().
Johnny Chen15f247a2012-02-03 20:43:00 +000050 self.source = 'main.cpp'
51 self.line = line_number(self.source, '// Set break point at this line.')
Johnny Chen77ca1a42010-10-12 22:53:02 +000052
Johnny Chen5fede4b2011-10-31 23:35:33 +000053 def step_stl_exprs(self):
54 """Test some expressions involving STL data types."""
Johnny Chen377a8ed2010-07-28 21:24:31 +000055 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chenff3d01d2010-08-20 21:03:09 +000056
Johnny Chen377a8ed2010-07-28 21:24:31 +000057 # The following two lines, if uncommented, will enable loggings.
58 #self.ci.HandleCommand("log enable -f /tmp/lldb.log lldb default", res)
59 #self.assertTrue(res.Succeeded())
Johnny Chenff3d01d2010-08-20 21:03:09 +000060
61 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
Johnny Chen377a8ed2010-07-28 21:24:31 +000062
Johnny Chen78c0aeb2010-10-12 23:01:50 +000063 # rdar://problem/8543077
64 # test/stl: clang built binaries results in the breakpoint locations = 3,
65 # is this a problem with clang generated debug info?
Johnny Chen15f247a2012-02-03 20:43:00 +000066 self.expect("breakpoint set -f %s -l %d" % (self.source, self.line),
Johnny Chen77ca1a42010-10-12 22:53:02 +000067 BREAKPOINT_CREATED,
68 startstr = "Breakpoint created: 1: file ='main.cpp', line = %d" %
69 self.line)
Johnny Chen377a8ed2010-07-28 21:24:31 +000070
Johnny Chen5ee88192010-08-27 23:47:36 +000071 self.runCmd("run", RUN_SUCCEEDED)
Johnny Chen377a8ed2010-07-28 21:24:31 +000072
73 # Stop at 'std::string hello_world ("Hello World!");'.
Johnny Chenff3d01d2010-08-20 21:03:09 +000074 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Johnny Chen77ca1a42010-10-12 22:53:02 +000075 substrs = ['main.cpp:%d' % self.line,
Johnny Chenff3d01d2010-08-20 21:03:09 +000076 'stop reason = breakpoint'])
Johnny Chen377a8ed2010-07-28 21:24:31 +000077
78 # The breakpoint should have a hit count of 1.
Caroline Tice79042b32011-02-04 22:59:41 +000079 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
Johnny Chenff3d01d2010-08-20 21:03:09 +000080 substrs = [' resolved, hit count = 1'])
Johnny Chen377a8ed2010-07-28 21:24:31 +000081
Johnny Chen9a1e9af2011-10-31 23:28:52 +000082 # Now try some expressions....
Johnny Chen377a8ed2010-07-28 21:24:31 +000083
Johnny Chen9a1e9af2011-10-31 23:28:52 +000084 self.runCmd('expr for (int i = 0; i < hello_world.length(); ++i) { (void)printf("%c\\n", hello_world[i]); }')
85
Johnny Chen5fede4b2011-10-31 23:35:33 +000086 # rdar://problem/10373783
Johnny Chen9b547242011-11-14 18:33:39 +000087 # rdar://problem/10400981
Johnny Chen9a1e9af2011-10-31 23:28:52 +000088 self.expect('expr associative_array.size()',
89 substrs = [' = 3'])
90 self.expect('expr associative_array.count(hello_world)',
91 substrs = [' = 1'])
92 self.expect('expr associative_array[hello_world]',
93 substrs = [' = 1'])
94 self.expect('expr associative_array["hello"]',
95 substrs = [' = 2'])
Johnny Chen377a8ed2010-07-28 21:24:31 +000096
Johnny Chen15f247a2012-02-03 20:43:00 +000097 def sbtype_template_apis(self):
98 """Test APIs for getting template arguments from an SBType."""
99 exe = os.path.join(os.getcwd(), 'a.out')
100
101 # Create a target by the debugger.
102 target = self.dbg.CreateTarget(exe)
103 self.assertTrue(target, VALID_TARGET)
104
105 # Create the breakpoint inside function 'main'.
106 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
107 self.assertTrue(breakpoint, VALID_BREAKPOINT)
108
109 # Now launch the process, and do not stop at entry point.
110 process = target.LaunchSimple(None, None, os.getcwd())
111 self.assertTrue(process, PROCESS_IS_VALID)
112
113 # Get Frame #0.
114 self.assertTrue(process.GetState() == lldb.eStateStopped)
115 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
116 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
117 frame0 = thread.GetFrameAtIndex(0)
118
119 # Get the type for variable 'associative_array'.
120 associative_array = frame0.FindVariable('associative_array')
121 self.DebugSBValue(associative_array)
122 self.assertTrue(associative_array, VALID_VARIABLE)
123 map_type = associative_array.GetType()
124 self.DebugSBType(map_type)
125 self.assertTrue(map_type, VALID_TYPE)
126 num_template_args = map_type.GetNumberOfTemplateArguments()
127 self.assertTrue(num_template_args > 0)
128
129 # We expect the template arguments to contain at least 'string' and 'int'.
130 expected_types = { 'string': False, 'int': False }
131 for i in range(num_template_args):
132 t = map_type.GetTemplateArgumentType(i)
133 self.DebugSBType(t)
134 self.assertTrue(t, VALID_TYPE)
135 name = t.GetName()
136 if 'string' in name:
137 expected_types['string'] = True
138 elif 'int' == name:
139 expected_types['int'] = True
140
141 # Check that both entries of the dictionary have 'True' as the value.
142 self.assertTrue(all(expected_types.values()))
143
Johnny Chen377a8ed2010-07-28 21:24:31 +0000144
145if __name__ == '__main__':
Johnny Chena2124952010-08-05 21:23:45 +0000146 import atexit
Johnny Chen377a8ed2010-07-28 21:24:31 +0000147 lldb.SBDebugger.Initialize()
Johnny Chena2124952010-08-05 21:23:45 +0000148 atexit.register(lambda: lldb.SBDebugger.Terminate())
Johnny Chen73258832010-08-05 23:42:46 +0000149 unittest2.main()