xref: /llvm-project/lldb/test/API/functionalities/var_path/TestVarPath.py (revision 80fcecb13c388ff087a27a4b0e7ca3dd8c98eaa4)
1"""
2Make sure the getting a variable path works and doesn't crash.
3"""
4
5
6import lldb
7import lldbsuite.test.lldbutil as lldbutil
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10
11
12class TestVarPath(TestBase):
13    # If your test case doesn't stress debug info, then
14    # set this to true.  That way it won't be run once for
15    # each debug info format.
16    NO_DEBUG_INFO_TESTCASE = True
17
18    def test_frame_var(self):
19        self.build()
20        self.do_test()
21
22    def verify_point(self, frame, var_name, var_typename, x_value, y_value):
23        v = frame.GetValueForVariablePath(var_name)
24        self.assertSuccess(v.GetError(), "Make sure we find '%s'" % (var_name))
25        self.assertEqual(
26            v.GetType().GetName(),
27            var_typename,
28            "Make sure '%s' has type '%s'" % (var_name, var_typename),
29        )
30
31        if "*" in var_typename:
32            valid_prefix = var_name + "->"
33            invalid_prefix = var_name + "."
34        else:
35            valid_prefix = var_name + "."
36            invalid_prefix = var_name + "->"
37
38        valid_x_path = valid_prefix + "x"
39        valid_y_path = valid_prefix + "y"
40        invalid_x_path = invalid_prefix + "x"
41        invalid_y_path = invalid_prefix + "y"
42        invalid_m_path = invalid_prefix + "m"
43
44        v = frame.GetValueForVariablePath(valid_x_path)
45        self.assertSuccess(v.GetError(), "Make sure we find '%s'" % (valid_x_path))
46        self.assertEqual(
47            v.GetValue(),
48            str(x_value),
49            "Make sure '%s' has a value of %i" % (valid_x_path, x_value),
50        )
51        self.assertEqual(
52            v.GetType().GetName(),
53            "int",
54            "Make sure '%s' has type 'int'" % (valid_x_path),
55        )
56        v = frame.GetValueForVariablePath(invalid_x_path)
57        self.assertTrue(
58            v.GetError().Fail(), "Make sure we don't find '%s'" % (invalid_x_path)
59        )
60
61        v = frame.GetValueForVariablePath(valid_y_path)
62        self.assertSuccess(v.GetError(), "Make sure we find '%s'" % (valid_y_path))
63        self.assertEqual(
64            v.GetValue(),
65            str(y_value),
66            "Make sure '%s' has a value of %i" % (valid_y_path, y_value),
67        )
68        self.assertEqual(
69            v.GetType().GetName(),
70            "int",
71            "Make sure '%s' has type 'int'" % (valid_y_path),
72        )
73        v = frame.GetValueForVariablePath(invalid_y_path)
74        self.assertTrue(
75            v.GetError().Fail(), "Make sure we don't find '%s'" % (invalid_y_path)
76        )
77
78        v = frame.GetValueForVariablePath(invalid_m_path)
79        self.assertTrue(
80            v.GetError().Fail(), "Make sure we don't find '%s'" % (invalid_m_path)
81        )
82
83    def do_test(self):
84        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
85            self, "// Set a breakpoint here", lldb.SBFileSpec("main.cpp")
86        )
87
88        frame = thread.GetFrameAtIndex(0)
89        v = frame.GetValueForVariablePath("no_such_variable")
90        self.assertTrue(
91            v.GetError().Fail(), "Make sure we don't find 'no_such_variable'"
92        )
93
94        # Test an instance
95        self.verify_point(frame, "pt", "Point", 1, 2)
96        # Test a pointer
97        self.verify_point(frame, "pt_ptr", "Point *", 3030, 4040)
98        # Test using a pointer as an array
99        self.verify_point(frame, "pt_ptr[-1]", "Point", 1010, 2020)
100        self.verify_point(frame, "pt_ptr[0]", "Point", 3030, 4040)
101        self.verify_point(frame, "pt_ptr[1]", "Point", 5050, 6060)
102        # Test arrays
103        v = frame.GetValueForVariablePath("points")
104        self.assertSuccess(v.GetError(), "Make sure we find 'points'")
105        self.verify_point(frame, "points[0]", "Point", 1010, 2020)
106        self.verify_point(frame, "points[1]", "Point", 3030, 4040)
107        self.verify_point(frame, "points[2]", "Point", 5050, 6060)
108        v = frame.GetValueForVariablePath("points[0]+5")
109        self.assertTrue(
110            v.GetError().Fail(),
111            "Make sure we do not ignore characters between ']' and the end",
112        )
113        # Test a reference
114        self.verify_point(frame, "pt_ref", "Point &", 1, 2)
115        v = frame.GetValueForVariablePath("pt_sp")
116        self.assertSuccess(v.GetError(), "Make sure we find 'pt_sp'")
117        # Make sure we don't crash when looking for non existant child
118        # in type with synthetic children. This used to cause a crash.
119        if not self.isAArch64Windows():
120            v = frame.GetValueForVariablePath("pt_sp->not_valid_child")
121            self.assertTrue(
122                v.GetError().Fail(), "Make sure we don't find 'pt_sp->not_valid_child'"
123            )
124