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