1""" 2Test the printing of anonymous and named namespace variables. 3""" 4 5 6import lldb 7from lldbsuite.test.decorators import * 8from lldbsuite.test.lldbtest import * 9from lldbsuite.test import lldbutil 10 11 12class NamespaceBreakpointTestCase(TestBase): 13 @expectedFailureAll(bugnumber="llvm.org/pr28548", compiler="gcc") 14 @expectedFailureAll(oslist=["windows"]) 15 def test_breakpoints_func_auto(self): 16 """Test that we can set breakpoints correctly by basename to find all functions whose basename is "func".""" 17 self.build() 18 19 names = ["func()", "func(int)", "A::B::func()", "A::func()", "A::func(int)"] 20 21 # Create a target by the debugger. 22 exe = self.getBuildArtifact("a.out") 23 target = self.dbg.CreateTarget(exe) 24 self.assertTrue(target, VALID_TARGET) 25 module_list = lldb.SBFileSpecList() 26 module_list.Append(lldb.SBFileSpec(exe, False)) 27 cu_list = lldb.SBFileSpecList() 28 # Set a breakpoint by name "func" which should pick up all functions 29 # whose basename is "func" 30 bp = target.BreakpointCreateByName( 31 "func", lldb.eFunctionNameTypeAuto, module_list, cu_list 32 ) 33 for bp_loc in bp: 34 name = bp_loc.GetAddress().GetFunction().GetName() 35 self.assertIn( 36 name, 37 names, 38 "make sure breakpoint locations are correct for 'func' with eFunctionNameTypeAuto", 39 ) 40 41 @expectedFailureAll(bugnumber="llvm.org/pr28548", compiler="gcc") 42 def test_breakpoints_func_full(self): 43 """Test that we can set breakpoints correctly by fullname to find all functions whose fully qualified name is "func" 44 (no namespaces).""" 45 self.build() 46 47 names = ["func()", "func(int)"] 48 49 # Create a target by the debugger. 50 exe = self.getBuildArtifact("a.out") 51 target = self.dbg.CreateTarget(exe) 52 self.assertTrue(target, VALID_TARGET) 53 module_list = lldb.SBFileSpecList() 54 module_list.Append(lldb.SBFileSpec(exe, False)) 55 cu_list = lldb.SBFileSpecList() 56 57 # Set a breakpoint by name "func" whose fullly qualified named matches "func" which 58 # should pick up only functions whose basename is "func" and has no 59 # containing context 60 bp = target.BreakpointCreateByName( 61 "func", lldb.eFunctionNameTypeFull, module_list, cu_list 62 ) 63 for bp_loc in bp: 64 name = bp_loc.GetAddress().GetFunction().GetName() 65 self.assertIn( 66 name, 67 names, 68 "make sure breakpoint locations are correct for 'func' with eFunctionNameTypeFull", 69 ) 70 71 def test_breakpoints_a_func_full(self): 72 """Test that we can set breakpoints correctly by fullname to find all functions whose fully qualified name is "A::func".""" 73 self.build() 74 75 names = ["A::func()", "A::func(int)"] 76 77 # Create a target by the debugger. 78 exe = self.getBuildArtifact("a.out") 79 target = self.dbg.CreateTarget(exe) 80 self.assertTrue(target, VALID_TARGET) 81 module_list = lldb.SBFileSpecList() 82 module_list.Append(lldb.SBFileSpec(exe, False)) 83 cu_list = lldb.SBFileSpecList() 84 85 # Set a breakpoint by name "A::func" whose fullly qualified named matches "A::func" which 86 # should pick up only functions whose basename is "func" and is 87 # contained in the "A" namespace 88 bp = target.BreakpointCreateByName( 89 "A::func", lldb.eFunctionNameTypeFull, module_list, cu_list 90 ) 91 for bp_loc in bp: 92 name = bp_loc.GetAddress().GetFunction().GetName() 93 self.assertIn( 94 name, 95 names, 96 "make sure breakpoint locations are correct for 'A::func' with eFunctionNameTypeFull", 97 ) 98 99 100class NamespaceTestCase(TestBase): 101 def setUp(self): 102 # Call super's setUp(). 103 TestBase.setUp(self) 104 # Find the line numbers for declarations of namespace variables i and 105 # j. 106 self.line_var_i = line_number( 107 "main.cpp", "// Find the line number for anonymous namespace variable i." 108 ) 109 self.line_var_j = line_number( 110 "main.cpp", "// Find the line number for named namespace variable j." 111 ) 112 # And the line number to break at. 113 self.line_break = line_number("main.cpp", "// Set break point at this line.") 114 # Break inside do {} while and evaluate value 115 self.line_break_ns1 = line_number("main.cpp", "// Evaluate ns1::value") 116 self.line_break_ns2 = line_number("main.cpp", "// Evaluate ns2::value") 117 118 def runToBkpt(self, command): 119 self.runCmd(command, RUN_SUCCEEDED) 120 # The stop reason of the thread should be breakpoint. 121 self.expect( 122 "thread list", 123 STOPPED_DUE_TO_BREAKPOINT, 124 substrs=["stopped", "stop reason = breakpoint"], 125 ) 126 127 # rdar://problem/8668674 128 @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764") 129 def test_with_run_command(self): 130 """Test that anonymous and named namespace variables display correctly.""" 131 self.build() 132 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET) 133 134 lldbutil.run_break_set_by_file_and_line( 135 self, 136 "main.cpp", 137 self.line_break_ns1, 138 num_expected_locations=1, 139 loc_exact=True, 140 ) 141 lldbutil.run_break_set_by_file_and_line( 142 self, 143 "main.cpp", 144 self.line_break_ns2, 145 num_expected_locations=1, 146 loc_exact=True, 147 ) 148 lldbutil.run_break_set_by_file_and_line( 149 self, "main.cpp", self.line_break, num_expected_locations=1, loc_exact=True 150 ) 151 152 self.runToBkpt("run") 153 # Evaluate ns1::value 154 self.expect_expr("value", result_value="100") 155 156 self.runToBkpt("continue") 157 # Evaluate ns2::value 158 self.expect_expr("value", result_value="200") 159 160 self.runToBkpt("continue") 161 # On Mac OS X, gcc 4.2 emits the wrong debug info with respect to 162 # types. 163 slist = ["(int) a = 12", "anon_uint", "a_uint", "b_uint", "y_uint"] 164 if self.platformIsDarwin() and self.getCompiler() in ["clang"]: 165 slist = [ 166 "(int) a = 12", 167 "::my_uint_t", 168 "anon_uint = 0", 169 "(A::uint_t) a_uint = 1", 170 "(A::B::uint_t) b_uint = 2", 171 "(Y::uint_t) y_uint = 3", 172 ] 173 174 # 'frame variable' displays the local variables with type information. 175 self.expect("frame variable", VARIABLES_DISPLAYED_CORRECTLY, substrs=slist) 176 177 # 'frame variable' with basename 'i' should work. 178 self.expect( 179 "frame variable --show-declaration --show-globals i", 180 startstr="main.cpp:%d: (int) (anonymous namespace)::i = 3" 181 % self.line_var_i, 182 ) 183 # main.cpp:12: (int) (anonymous namespace)::i = 3 184 185 # 'frame variable' with basename 'j' should work, too. 186 self.expect( 187 "frame variable --show-declaration --show-globals j", 188 startstr="main.cpp:%d: (int) A::B::j = 4" % self.line_var_j, 189 ) 190 # main.cpp:19: (int) A::B::j = 4 191 192 # 'frame variable' should support address-of operator. 193 self.runCmd("frame variable &i") 194 195 # 'frame variable' with fully qualified name 'A::B::j' should work. 196 self.expect( 197 "frame variable A::B::j", 198 VARIABLES_DISPLAYED_CORRECTLY, 199 startstr="(int) A::B::j = 4", 200 patterns=[" = 4"], 201 ) 202 203 # So should the anonymous namespace case. 204 self.expect( 205 "frame variable '(anonymous namespace)::i'", 206 VARIABLES_DISPLAYED_CORRECTLY, 207 startstr="(int) (anonymous namespace)::i = 3", 208 patterns=[" = 3"], 209 ) 210 211 # Search for a type in an anonymous namespace, both with and without the 212 # namespace prefix. 213 self.expect("type lookup -- my_uint_t", substrs=["unsigned int"]) 214 self.expect("type lookup -- (anonymous namespace)::my_uint_t", 215 substrs=["unsigned int"]) 216 217 # rdar://problem/8660275 218 # test/namespace: 'expression -- i+j' not working 219 # This has been fixed. 220 self.expect_expr("i + j", result_type="int", result_value="7") 221 # (int) $2 = 7 222 223 self.expect_expr("i", result_value="3") 224 self.expect_expr("j", result_value="4") 225 226 # rdar://problem/8668674 227 # expression command with fully qualified namespace for a variable does 228 # not work 229 self.expect_expr("::i", result_value="3") 230 self.expect_expr("A::B::j", result_value="4") 231 232 # expression command with function in anonymous namespace 233 self.expect_expr("myanonfunc(3)", result_value="6") 234 235 # global namespace qualification with function in anonymous namespace 236 self.expect_expr("myanonfunc(4)", result_value="8") 237 238 self.expect( 239 "expression myanonfunc", 240 patterns=["\(anonymous namespace\)::myanonfunc\(int\)"], 241 ) 242 243 self.expect( 244 "expression variadic_sum", 245 patterns=["\(anonymous namespace\)::variadic_sum\(int, ...\)"], 246 ) 247 248 self.expect_expr("::B::Bar b; b.x()", result_type="int", result_value="42") 249 self.expect_expr("A::B::Bar b; b.y()", result_type="int", result_value="137") 250 self.expect_expr( 251 "::NS1::NS2::Foo{}.bar() == -2 && ::NS2::Foo{}.bar() == -3", 252 result_type="bool", 253 result_value="true", 254 ) 255 # FIXME: C++ unqualified namespace lookups currently not supported when instantiating types. 256 self.expect_expr( 257 "NS2::Foo{}.bar() == -3", result_type="bool", result_value="false" 258 ) 259 self.expect_expr( 260 "((::B::Bar*)&::B::bar)->x()", result_type="int", result_value="42" 261 ) 262 263 self.expect_expr("InAnon1::var_in_anon", result_type="int", result_value="10") 264 self.expect_expr( 265 "InAnon1::InAnon2::var_in_anon", result_type="int", result_value="5" 266 ) 267 self.expect_expr( 268 "InAnon1::inline_ns::var_in_anon", result_type="int", result_value="15" 269 ) 270 self.expect_expr( 271 "InAnon1::inline_ns::InAnon2::var_in_anon", 272 result_type="int", 273 result_value="5", 274 ) 275