[clang] Add tracking source deduction guide for the explicitly-written
[llvm-project.git] / lldb / test / API / lang / cpp / class_static / TestStaticVariables.py
blob04678ec018bdf46a3e1369193232d37dfb5058ab
1 """
2 Test display and Python APIs on file and class static variables.
3 """
5 import lldb
6 from lldbsuite.test.decorators import *
7 from lldbsuite.test.lldbtest import *
8 from lldbsuite.test import lldbutil
11 class StaticVariableTestCase(TestBase):
12 def setUp(self):
13 # Call super's setUp().
14 TestBase.setUp(self)
15 # Find the line number to break at.
16 self.line = line_number("main.cpp", "// Set break point at this line.")
18 def test_with_run_command(self):
19 """Test that file and class static variables display correctly."""
20 self.build()
21 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
23 lldbutil.run_break_set_by_file_and_line(
24 self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True
27 self.runCmd("run", RUN_SUCCEEDED)
29 # The stop reason of the thread should be breakpoint.
30 self.expect(
31 "thread list",
32 STOPPED_DUE_TO_BREAKPOINT,
33 substrs=["stopped", "stop reason = breakpoint"],
36 # Global variables are no longer displayed with the "frame variable"
37 # command.
38 self.expect(
39 "target variable A::g_points",
40 VARIABLES_DISPLAYED_CORRECTLY,
41 patterns=["\(PointType\[[1-9]*\]\) A::g_points = {"],
43 self.expect(
44 "target variable g_points",
45 VARIABLES_DISPLAYED_CORRECTLY,
46 substrs=["(PointType[2]) g_points"],
49 # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points.
50 # A::g_points is an array of two elements.
51 if self.platformIsDarwin() or self.getPlatform() == "linux":
52 self.expect(
53 "target variable A::g_points[1].x",
54 VARIABLES_DISPLAYED_CORRECTLY,
55 startstr="(int) A::g_points[1].x = 11",
58 @expectedFailureAll(
59 compiler=["gcc"], bugnumber="Compiler emits incomplete debug info"
61 @expectedFailureAll(
62 compiler=["clang"], compiler_version=["<", "3.9"], bugnumber="llvm.org/pr20550"
64 def test_with_run_command_complete(self):
65 """
66 Test that file and class static variables display correctly with
67 complete debug information.
68 """
69 self.build()
70 target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
71 self.assertTrue(target, VALID_TARGET)
73 # Global variables are no longer displayed with the "frame variable"
74 # command.
75 self.expect(
76 "target variable A::g_points",
77 VARIABLES_DISPLAYED_CORRECTLY,
78 patterns=[
79 "\(PointType\[[1-9]*\]\) A::g_points = {",
80 "(x = 1, y = 2)",
81 "(x = 11, y = 22)",
85 # Ensure that we take the context into account and only print
86 # A::g_points.
87 self.expect(
88 "target variable A::g_points",
89 VARIABLES_DISPLAYED_CORRECTLY,
90 matching=False,
91 patterns=["(x = 3, y = 4)", "(x = 33, y = 44)"],
94 # Finally, ensure that we print both points when not specifying a
95 # context.
96 self.expect(
97 "target variable g_points",
98 VARIABLES_DISPLAYED_CORRECTLY,
99 substrs=[
100 "(PointType[2]) g_points",
101 "(x = 1, y = 2)",
102 "(x = 11, y = 22)",
103 "(x = 3, y = 4)",
104 "(x = 33, y = 44)",
108 def build_value_check(self, var_name, values):
109 children_1 = [
110 ValueCheck(name="x", value=values[0], type="int"),
111 ValueCheck(name="y", value=values[1], type="int"),
113 children_2 = [
114 ValueCheck(name="x", value=values[2], type="int"),
115 ValueCheck(name="y", value=values[3], type="int"),
117 elem_0 = ValueCheck(
118 name="[0]", value=None, type="PointType", children=children_1
120 elem_1 = ValueCheck(
121 name="[1]", value=None, type="PointType", children=children_2
123 value_check = ValueCheck(
124 name=var_name, value=None, type="PointType[2]", children=[elem_0, elem_1]
127 return value_check
129 @expectedFailureAll(
130 compiler=["gcc"], bugnumber="Compiler emits incomplete debug info"
132 @expectedFailureAll(
133 compiler=["clang"], compiler_version=["<", "3.9"], bugnumber="llvm.org/pr20550"
135 @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")
136 @add_test_categories(["pyapi"])
137 def test_with_python_FindValue(self):
138 """Test Python APIs on file and class static variables."""
139 self.build()
140 exe = self.getBuildArtifact("a.out")
142 target = self.dbg.CreateTarget(exe)
143 self.assertTrue(target, VALID_TARGET)
145 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
146 self.assertTrue(breakpoint, VALID_BREAKPOINT)
148 # Now launch the process, and do not stop at entry point.
149 process = target.LaunchSimple(None, None, self.get_process_working_directory())
150 self.assertTrue(process, PROCESS_IS_VALID)
152 # The stop reason of the thread should be breakpoint.
153 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
154 self.assertIsNotNone(thread)
156 # Get the SBValue of 'A::g_points' and 'g_points'.
157 frame = thread.GetFrameAtIndex(0)
159 # arguments => False
160 # locals => False
161 # statics => True
162 # in_scope_only => False
163 valList = frame.GetVariables(False, False, True, False)
165 # Build ValueCheckers for the values we're going to find:
166 value_check_A = self.build_value_check("A::g_points", ["1", "2", "11", "22"])
167 value_check_none = self.build_value_check("g_points", ["3", "4", "33", "44"])
168 value_check_AA = self.build_value_check("AA::g_points", ["5", "6", "55", "66"])
170 for val in valList:
171 self.DebugSBValue(val)
172 name = val.GetName()
173 self.assertIn(name, ["g_points", "A::g_points", "AA::g_points"])
175 if name == "A::g_points":
176 self.assertEqual(val.GetValueType(), lldb.eValueTypeVariableGlobal)
177 value_check_A.check_value(self, val, "Got A::g_points right")
178 if name == "g_points":
179 self.assertEqual(val.GetValueType(), lldb.eValueTypeVariableStatic)
180 value_check_none.check_value(self, val, "Got g_points right")
181 if name == "AA::g_points":
182 self.assertEqual(val.GetValueType(), lldb.eValueTypeVariableGlobal)
183 value_check_AA.check_value(self, val, "Got AA::g_points right")
185 # SBFrame.FindValue() should also work.
186 val = frame.FindValue("A::g_points", lldb.eValueTypeVariableGlobal)
187 self.DebugSBValue(val)
188 value_check_A.check_value(self, val, "FindValue also works")
190 # Also exercise the "parameter" and "local" scopes while we are at it.
191 val = frame.FindValue("argc", lldb.eValueTypeVariableArgument)
192 self.DebugSBValue(val)
193 self.assertEqual(val.GetName(), "argc")
195 val = frame.FindValue("argv", lldb.eValueTypeVariableArgument)
196 self.DebugSBValue(val)
197 self.assertEqual(val.GetName(), "argv")
199 val = frame.FindValue("hello_world", lldb.eValueTypeVariableLocal)
200 self.DebugSBValue(val)
201 self.assertEqual(val.GetName(), "hello_world")
203 # This test tests behavior that's been broken for a very long time..
204 # The fix for it is in the accelerator table part of the DWARF reader,
205 # and I fixed the version that the names accelerator uses, but I don't
206 # know how to fix it on systems that don't use that. There isn't a
207 # decorator for that - not sure how to construct that so I'm limiting the
208 # test do Darwin for now.
209 @expectedFailureAll(
210 compiler=["gcc"], bugnumber="Compiler emits incomplete debug info"
212 @skipUnlessDarwin
213 @add_test_categories(["pyapi"])
214 def test_with_python_FindGlobalVariables(self):
215 """Test Python APIs on file and class static variables."""
216 self.build()
217 exe = self.getBuildArtifact("a.out")
219 target = self.dbg.CreateTarget(exe)
220 self.assertTrue(target, VALID_TARGET)
222 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
223 self.assertTrue(breakpoint, VALID_BREAKPOINT)
225 # Now launch the process, and do not stop at entry point.
226 process = target.LaunchSimple(None, None, self.get_process_working_directory())
227 self.assertTrue(process, PROCESS_IS_VALID)
229 # The stop reason of the thread should be breakpoint.
230 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
231 self.assertIsNotNone(thread)
233 # Get the SBValue of 'A::g_points' and 'g_points'.
234 frame = thread.GetFrameAtIndex(0)
236 # Build ValueCheckers for the values we're going to find:
237 value_check_A = self.build_value_check("A::g_points", ["1", "2", "11", "22"])
238 value_check_none = self.build_value_check("g_points", ["3", "4", "33", "44"])
239 value_check_AA = self.build_value_check("AA::g_points", ["5", "6", "55", "66"])
241 # We should also be able to get class statics from FindGlobalVariables.
242 # eMatchTypeStartsWith should only find A:: not AA::
243 val_list = target.FindGlobalVariables("A::", 10, lldb.eMatchTypeStartsWith)
244 self.assertEqual(val_list.GetSize(), 1, "Found only one match")
245 val = val_list[0]
246 value_check_A.check_value(self, val, "FindGlobalVariables starts with")
248 # Regex should find both
249 val_list = target.FindGlobalVariables("A::", 10, lldb.eMatchTypeRegex)
250 self.assertEqual(val_list.GetSize(), 2, "Found A & AA")
251 found_a = False
252 found_aa = False
253 for val in val_list:
254 name = val.GetName()
255 if name == "A::g_points":
256 value_check_A.check_value(self, val, "AA found by regex")
257 found_a = True
258 elif name == "AA::g_points":
259 value_check_AA.check_value(self, val, "A found by regex")
260 found_aa = True
262 self.assertTrue(found_a, "Regex search found A::g_points")
263 self.assertTrue(found_aa, "Regex search found AA::g_points")
265 # Regex lowercase should find both as well.
266 val_list = target.FindGlobalVariables(
267 "a::g_points", 10, lldb.eMatchTypeRegexInsensitive
269 self.assertEqual(val_list.GetSize(), 2, "Found A & AA")
271 # Normal search for full name should find one, but it looks like we don't match
272 # on identifier boundaries here yet:
273 val_list = target.FindGlobalVariables("A::g_points", 10, lldb.eMatchTypeNormal)
274 self.assertEqual(
275 val_list.GetSize(), 2, "We aren't matching on name boundaries yet"
278 # Normal search for g_points should find 3 - FindGlobalVariables doesn't distinguish
279 # between file statics and globals:
280 val_list = target.FindGlobalVariables("g_points", 10, lldb.eMatchTypeNormal)
281 self.assertEqual(val_list.GetSize(), 3, "Found all three g_points")