• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""
2Test some expressions involving STL data types.
3"""
4
5import os, time
6import unittest2
7import lldb
8import lldbutil
9from lldbtest import *
10
11class STLTestCase(TestBase):
12
13    mydir = os.path.join("lang", "cpp", "stl")
14
15    # rdar://problem/10400981
16    @unittest2.expectedFailure
17    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
18    @dsym_test
19    def test_with_dsym(self):
20        """Test some expressions involving STL data types."""
21        self.buildDsym()
22        self.step_stl_exprs()
23
24    # rdar://problem/10400981
25    @unittest2.expectedFailure
26    @dwarf_test
27    def test_with_dwarf(self):
28        """Test some expressions involving STL data types."""
29        self.buildDwarf()
30        self.step_stl_exprs()
31
32    @python_api_test
33    @dsym_test
34    def test_SBType_template_aspects_with_dsym(self):
35        """Test APIs for getting template arguments from an SBType."""
36        self.buildDsym()
37        self.sbtype_template_apis()
38
39    @skipIfGcc # llvm.org/pr15036: crashes during DWARF parsing when built with GCC
40    @expectedFailureIcc # icc 13.1 and 14-beta do not emit DW_TAG_template_type_parameter
41    @python_api_test
42    @dwarf_test
43    def test_SBType_template_aspects_with_dwarf(self):
44        """Test APIs for getting template arguments from an SBType."""
45        self.buildDwarf()
46        self.sbtype_template_apis()
47
48    def setUp(self):
49        # Call super's setUp().
50        TestBase.setUp(self)
51        # Find the line number to break inside main().
52        self.source = 'main.cpp'
53        self.line = line_number(self.source, '// Set break point at this line.')
54
55    def step_stl_exprs(self):
56        """Test some expressions involving STL data types."""
57        exe = os.path.join(os.getcwd(), "a.out")
58
59        # The following two lines, if uncommented, will enable loggings.
60        #self.ci.HandleCommand("log enable -f /tmp/lldb.log lldb default", res)
61        #self.assertTrue(res.Succeeded())
62
63        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
64
65        # rdar://problem/8543077
66        # test/stl: clang built binaries results in the breakpoint locations = 3,
67        # is this a problem with clang generated debug info?
68        lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
69
70        self.runCmd("run", RUN_SUCCEEDED)
71
72        # Stop at 'std::string hello_world ("Hello World!");'.
73        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
74            substrs = ['main.cpp:%d' % self.line,
75                       'stop reason = breakpoint'])
76
77        # The breakpoint should have a hit count of 1.
78        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
79            substrs = [' resolved, hit count = 1'])
80
81        # Now try some expressions....
82
83        self.runCmd('expr for (int i = 0; i < hello_world.length(); ++i) { (void)printf("%c\\n", hello_world[i]); }')
84
85        # rdar://problem/10373783
86        # rdar://problem/10400981
87        self.expect('expr associative_array.size()',
88            substrs = [' = 3'])
89        self.expect('expr associative_array.count(hello_world)',
90            substrs = [' = 1'])
91        self.expect('expr associative_array[hello_world]',
92            substrs = [' = 1'])
93        self.expect('expr associative_array["hello"]',
94            substrs = [' = 2'])
95
96    def sbtype_template_apis(self):
97        """Test APIs for getting template arguments from an SBType."""
98        exe = os.path.join(os.getcwd(), 'a.out')
99
100        # Create a target by the debugger.
101        target = self.dbg.CreateTarget(exe)
102        self.assertTrue(target, VALID_TARGET)
103
104        # Create the breakpoint inside function 'main'.
105        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
106        self.assertTrue(breakpoint, VALID_BREAKPOINT)
107
108        # Now launch the process, and do not stop at entry point.
109        process = target.LaunchSimple(None, None, os.getcwd())
110        self.assertTrue(process, PROCESS_IS_VALID)
111
112        # Get Frame #0.
113        self.assertTrue(process.GetState() == lldb.eStateStopped)
114        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
115        self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition")
116        frame0 = thread.GetFrameAtIndex(0)
117
118        # Get the type for variable 'associative_array'.
119        associative_array = frame0.FindVariable('associative_array')
120        self.DebugSBValue(associative_array)
121        self.assertTrue(associative_array, VALID_VARIABLE)
122        map_type = associative_array.GetType()
123        self.DebugSBType(map_type)
124        self.assertTrue(map_type, VALID_TYPE)
125        num_template_args = map_type.GetNumberOfTemplateArguments()
126        self.assertTrue(num_template_args > 0)
127
128        # We expect the template arguments to contain at least 'string' and 'int'.
129        expected_types = { 'string': False, 'int': False }
130        for i in range(num_template_args):
131            t = map_type.GetTemplateArgumentType(i)
132            self.DebugSBType(t)
133            self.assertTrue(t, VALID_TYPE)
134            name = t.GetName()
135            if 'string' in name:
136                expected_types['string'] = True
137            elif 'int' == name:
138                expected_types['int'] = True
139
140        # Check that both entries of the dictionary have 'True' as the value.
141        self.assertTrue(all(expected_types.values()))
142
143
144if __name__ == '__main__':
145    import atexit
146    lldb.SBDebugger.Initialize()
147    atexit.register(lambda: lldb.SBDebugger.Terminate())
148    unittest2.main()
149