blob: b0c64580d3cdf8be592714529465188e7ac9839d [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
Daniel Malea658fd572013-03-04 23:15:08 +000039 @skipIfGcc # llvm.org/pr15036: crashes during DWARF parsing when built with GCC
Johnny Chen15f247a2012-02-03 20:43:00 +000040 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000041 @dwarf_test
Johnny Chen15f247a2012-02-03 20:43:00 +000042 def test_SBType_template_aspects_with_dwarf(self):
Johnny Chen9dad8ae2012-02-03 20:50:56 +000043 """Test APIs for getting template arguments from an SBType."""
Johnny Chen15f247a2012-02-03 20:43:00 +000044 self.buildDwarf()
45 self.sbtype_template_apis()
46
Johnny Chen77ca1a42010-10-12 22:53:02 +000047 def setUp(self):
Johnny Chenaadcef52010-10-14 17:31:24 +000048 # Call super's setUp().
49 TestBase.setUp(self)
Johnny Chen77ca1a42010-10-12 22:53:02 +000050 # Find the line number to break inside main().
Johnny Chen15f247a2012-02-03 20:43:00 +000051 self.source = 'main.cpp'
52 self.line = line_number(self.source, '// Set break point at this line.')
Johnny Chen77ca1a42010-10-12 22:53:02 +000053
Johnny Chen5fede4b2011-10-31 23:35:33 +000054 def step_stl_exprs(self):
55 """Test some expressions involving STL data types."""
Johnny Chen377a8ed2010-07-28 21:24:31 +000056 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chenff3d01d2010-08-20 21:03:09 +000057
Johnny Chen377a8ed2010-07-28 21:24:31 +000058 # The following two lines, if uncommented, will enable loggings.
59 #self.ci.HandleCommand("log enable -f /tmp/lldb.log lldb default", res)
60 #self.assertTrue(res.Succeeded())
Johnny Chenff3d01d2010-08-20 21:03:09 +000061
62 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
Johnny Chen377a8ed2010-07-28 21:24:31 +000063
Johnny Chen78c0aeb2010-10-12 23:01:50 +000064 # rdar://problem/8543077
65 # test/stl: clang built binaries results in the breakpoint locations = 3,
66 # is this a problem with clang generated debug info?
Jim Ingham63dfc722012-09-22 00:05:11 +000067 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
Johnny Chen377a8ed2010-07-28 21:24:31 +000068
Johnny Chen5ee88192010-08-27 23:47:36 +000069 self.runCmd("run", RUN_SUCCEEDED)
Johnny Chen377a8ed2010-07-28 21:24:31 +000070
71 # Stop at 'std::string hello_world ("Hello World!");'.
Johnny Chenff3d01d2010-08-20 21:03:09 +000072 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
Johnny Chen77ca1a42010-10-12 22:53:02 +000073 substrs = ['main.cpp:%d' % self.line,
Johnny Chenff3d01d2010-08-20 21:03:09 +000074 'stop reason = breakpoint'])
Johnny Chen377a8ed2010-07-28 21:24:31 +000075
76 # The breakpoint should have a hit count of 1.
Caroline Tice79042b32011-02-04 22:59:41 +000077 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
Johnny Chenff3d01d2010-08-20 21:03:09 +000078 substrs = [' resolved, hit count = 1'])
Johnny Chen377a8ed2010-07-28 21:24:31 +000079
Johnny Chen9a1e9af2011-10-31 23:28:52 +000080 # Now try some expressions....
Johnny Chen377a8ed2010-07-28 21:24:31 +000081
Johnny Chen9a1e9af2011-10-31 23:28:52 +000082 self.runCmd('expr for (int i = 0; i < hello_world.length(); ++i) { (void)printf("%c\\n", hello_world[i]); }')
83
Johnny Chen5fede4b2011-10-31 23:35:33 +000084 # rdar://problem/10373783
Johnny Chen9b547242011-11-14 18:33:39 +000085 # rdar://problem/10400981
Johnny Chen9a1e9af2011-10-31 23:28:52 +000086 self.expect('expr associative_array.size()',
87 substrs = [' = 3'])
88 self.expect('expr associative_array.count(hello_world)',
89 substrs = [' = 1'])
90 self.expect('expr associative_array[hello_world]',
91 substrs = [' = 1'])
92 self.expect('expr associative_array["hello"]',
93 substrs = [' = 2'])
Johnny Chen377a8ed2010-07-28 21:24:31 +000094
Johnny Chen15f247a2012-02-03 20:43:00 +000095 def sbtype_template_apis(self):
96 """Test APIs for getting template arguments from an SBType."""
97 exe = os.path.join(os.getcwd(), 'a.out')
98
99 # Create a target by the debugger.
100 target = self.dbg.CreateTarget(exe)
101 self.assertTrue(target, VALID_TARGET)
102
103 # Create the breakpoint inside function 'main'.
104 breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
105 self.assertTrue(breakpoint, VALID_BREAKPOINT)
106
107 # Now launch the process, and do not stop at entry point.
108 process = target.LaunchSimple(None, None, os.getcwd())
109 self.assertTrue(process, PROCESS_IS_VALID)
110
111 # Get Frame #0.
112 self.assertTrue(process.GetState() == lldb.eStateStopped)
113 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +0000114 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition")
Johnny Chen15f247a2012-02-03 20:43:00 +0000115 frame0 = thread.GetFrameAtIndex(0)
116
117 # Get the type for variable 'associative_array'.
118 associative_array = frame0.FindVariable('associative_array')
119 self.DebugSBValue(associative_array)
120 self.assertTrue(associative_array, VALID_VARIABLE)
121 map_type = associative_array.GetType()
122 self.DebugSBType(map_type)
123 self.assertTrue(map_type, VALID_TYPE)
124 num_template_args = map_type.GetNumberOfTemplateArguments()
125 self.assertTrue(num_template_args > 0)
126
127 # We expect the template arguments to contain at least 'string' and 'int'.
128 expected_types = { 'string': False, 'int': False }
129 for i in range(num_template_args):
130 t = map_type.GetTemplateArgumentType(i)
131 self.DebugSBType(t)
132 self.assertTrue(t, VALID_TYPE)
133 name = t.GetName()
134 if 'string' in name:
135 expected_types['string'] = True
136 elif 'int' == name:
137 expected_types['int'] = True
138
139 # Check that both entries of the dictionary have 'True' as the value.
140 self.assertTrue(all(expected_types.values()))
141
Johnny Chen377a8ed2010-07-28 21:24:31 +0000142
143if __name__ == '__main__':
Johnny Chena2124952010-08-05 21:23:45 +0000144 import atexit
Johnny Chen377a8ed2010-07-28 21:24:31 +0000145 lldb.SBDebugger.Initialize()
Johnny Chena2124952010-08-05 21:23:45 +0000146 atexit.register(lambda: lldb.SBDebugger.Terminate())
Johnny Chen73258832010-08-05 23:42:46 +0000147 unittest2.main()