Debian Codegraph @

lldb/test/API/commands/frame/var-dil/expr/Casts/TestFrameVarDILCast.py

raw
1 """
2 Make sure 'frame var' using DIL parser/evaultor works for C-Style casts.
3 """
4
5 import lldb
6 from lldbsuite.test.lldbtest import *
7 from lldbsuite.test.decorators import *
8 from lldbsuite.test import lldbutil
9
10 import os
11 import shutil
12 import time
13
14
15 class TestFrameVarDILCast(TestBase):
16 def test_type_cast(self):
17 self.build()
18 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
19 self, "Set a breakpoint here", lldb.SBFileSpec("main.cpp")
20 )
21
22 self.runCmd("settings set target.experimental.use-DIL true")
23
24 Is32Bit = False
25 if self.target().GetAddressByteSize() == 4:
26 Is32Bit = True
27
28 # TestCastBUiltins
29
30 self.expect_var_path("(int)1", value="1", type="int")
31 self.expect_var_path("(long long)1", value="1", type="long long")
32 self.expect_var_path("(unsigned long)1", value="1", type="unsigned long")
33 if Is32Bit:
34 self.expect_var_path("(char*)1", value="0x00000001", type="char *")
35 self.expect_var_path(
36 "(long long**)1", value="0x00000001", type="long long **"
37 )
38 else:
39 self.expect_var_path("(char*)1", value="0x0000000000000001", type="char *")
40 self.expect_var_path(
41 "(long long**)1", value="0x0000000000000001", type="long long **"
42 )
43
44 self.expect(
45 "frame variable '(long&*)1'",
46 error=True,
47 substrs=[
48 "'type name' declared as a pointer to a reference of type 'long &'"
49 ],
50 )
51
52 self.expect(
53 "frame variable '(long& &)1'",
54 error=True,
55 substrs=["type name declared as a reference to a reference"],
56 )
57
58 self.expect(
59 "frame variable '(long 1)1'",
60 error=True,
61 substrs=["expected 'r_paren', got: <'1' (integer_constant)>"],
62 )
63
64 # TestCastBasicType
65
66 # Test with integer literals.
67 self.expect_var_path("(char)1", type="char", value="'\\x01'")
68 self.expect_var_path("(long long)1", type="long long", value="1")
69 self.expect_var_path("(short)65534", type="short", value="-2")
70 self.expect_var_path(
71 "(unsigned short)100000", type="unsigned short", value="34464"
72 )
73 self.expect_var_path("(int)false", type="int", value="0")
74 self.expect_var_path("(int)true", type="int", value="1")
75 self.expect_var_path("(float)1", type="float", value="1")
76 self.expect_var_path("(float)1.1", type="float", value="1.10000002")
77 self.expect_var_path("(float)1.1f", type="float", value="1.10000002")
78 self.expect_var_path("(float)false", type="float", value="0")
79 self.expect_var_path("(float)true", type="float", value="1")
80 self.expect_var_path("(double)1", type="double", value="1")
81 self.expect_var_path("(double)1.1", type="double", value="1.1000000000000001")
82 self.expect_var_path("(double)1.1f", type="double", value="1.1000000238418579")
83 self.expect_var_path("(double)false", type="double", value="0")
84 self.expect_var_path("(double)true", type="double", value="1")
85 self.expect_var_path("(int)1.1", type="int", value="1")
86 self.expect_var_path("(int)1.1f", type="int", value="1")
87 self.expect_var_path("(long)1.1", type="long", value="1")
88 self.expect_var_path("(bool)0", type="bool", value="false")
89 self.expect_var_path("(bool)0.0", type="bool", value="false")
90 self.expect_var_path("(bool)0.0f", type="bool", value="false")
91 self.expect_var_path("(bool)3", type="bool", value="true")
92
93 self.expect(
94 "frame variable '&(int)1'",
95 error=True,
96 substrs=["'result' doesn't have a valid address"],
97 )
98
99 # Test with variables.
100 self.expect_var_path("(char)a", type="char", value="'\\x01'")
101 self.expect_var_path("(unsigned char)na", type="unsigned char", value="'\\xff'")
102 self.expect_var_path("(short)na", type="short", value="-1")
103 self.expect_var_path("(long long)a", type="long long", value="1")
104 self.expect_var_path("(float)a", type="float", value="1")
105 self.expect_var_path("(float)f", type="float", value="1.10000002")
106 self.expect_var_path("(double)f", type="double", value="1.1000000238418579")
107 self.expect_var_path("(int)f", type="int", value="1")
108 self.expect_var_path("(long)f", type="long", value="1")
109 self.expect_var_path("(bool)finf", type="bool", value="true")
110 self.expect_var_path("(bool)fnan", type="bool", value="true")
111 self.expect_var_path("(bool)fsnan", type="bool", value="true")
112 self.expect_var_path("(bool)fmax", type="bool", value="true")
113 self.expect_var_path("(bool)fdenorm", type="bool", value="true")
114 self.expect(
115 "frame variable '(int)ns_foo_'",
116 error=True,
117 substrs=["cannot convert 'ns::Foo' to 'int'"],
118 )
119
120 self.expect_var_path("(int)myint_", type="int", value="1")
121 self.expect_var_path("(int)ns_myint_", type="int", value="2")
122 self.expect_var_path("(long long)myint_", type="long long", value="1")
123 self.expect_var_path("(long long)ns_myint_", type="long long", value="2")
124
125 # Test with pointers and arrays.
126 self.expect_var_path("(long long)ap", type="long long")
127 self.expect_var_path("(unsigned long long)vp", type="unsigned long long")
128 self.expect_var_path("(long long)arr", type="long long")
129 self.expect_var_path("(bool)ap", type="bool", value="true")
130 self.expect_var_path("(bool)(int*)0x00000000", type="bool", value="false")
131 self.expect_var_path("(bool)std_nullptr_t", value="false")
132 self.expect_var_path("(bool)arr", type="bool", value="true")
133 self.expect(
134 "frame variable '(char)ap'",
135 error=True,
136 substrs=["cast from pointer to smaller type 'char' loses information"],
137 )
138
139 if Is32Bit:
140 self.expect_var_path("(int)arr", type="int")
141 else:
142 self.expect(
143 "frame variable '(int)arr'",
144 error=True,
145 substrs=["cast from pointer to smaller type 'int' loses information"],
146 )
147
148 self.expect(
149 "frame variable '(float)ap'",
150 error=True,
151 substrs=["Cast from 'int *' to 'float' is not allowed"],
152 )
153 self.expect(
154 "frame variable '(float)arr'",
155 error=True,
156 substrs=["Cast from 'int *' to 'float' is not allowed"],
157 )
158
159 # TestCastPointer
160 self.expect_var_path("(void*)&a", type="void *")
161 self.expect_var_path("(void*)ap", type="void *")
162 self.expect_var_path("(long long*)vp", type="long long *")
163 self.expect_var_path("(short int*)vp", type="short *")
164 self.expect_var_path("(unsigned long long*)vp", type="unsigned long long *")
165 self.expect_var_path("(unsigned short int*)vp", type="unsigned short *")
166
167 if Is32Bit:
168 self.expect_var_path("(void*)0", type="void *", value="0x00000000")
169 self.expect_var_path("(void*)1", type="void *", value="0x00000001")
170 self.expect_var_path("(void*)a", type="void *", value="0x00000001")
171 self.expect_var_path("(void*)na", type="void *", value="0xffffffff")
172 else:
173 self.expect_var_path("(void*)0", type="void *", value="0x0000000000000000")
174 self.expect_var_path("(void*)1", type="void *", value="0x0000000000000001")
175 self.expect_var_path("(void*)a", type="void *", value="0x0000000000000001")
176 self.expect_var_path("(void*)na", type="void *", value="0xffffffffffffffff")
177
178 self.expect(
179 "frame variable '(char*) 1.0'",
180 error=True,
181 substrs=["cannot cast from type 'double' to pointer type 'char *'"],
182 )
183
184 self.expect_var_path("*(int*)(void*)ap", type="int", value="1")
185
186 self.expect(
187 "frame variable '(int& &)ap'",
188 error=True,
189 substrs=["type name declared as a reference to a reference"],
190 )
191 self.expect(
192 "frame variable '(int&*)ap'",
193 error=True,
194 substrs=[
195 "'type name' declared as a pointer to a reference of type 'int &'"
196 ],
197 )
198
199 if Is32Bit:
200 self.expect_var_path("(void *)0", type="void *", value="0x00000000")
201 else:
202 self.expect_var_path("(void *)0", type="void *", value="0x0000000000000000")
203
204 self.expect(
205 "frame variable '(int)std_nullptr_t'",
206 error=True,
207 substrs=["cast from pointer to smaller type 'int' loses information"],
208 )
209
210 if Is32Bit:
211 self.expect_var_path(
212 "(void*)std_nullptr_t", type="void *", value="0x00000000"
213 )
214 self.expect_var_path(
215 "(char*)std_nullptr_t", type="char *", value="0x00000000"
216 )
217 else:
218 self.expect_var_path(
219 "(void*)std_nullptr_t", type="void *", value="0x0000000000000000"
220 )
221 self.expect_var_path(
222 "(char*)std_nullptr_t", type="char *", value="0x0000000000000000"
223 )
224
225 # TestCastArray
226 self.expect_var_path("(int*)arr_1d", type="int *")
227 self.expect_var_path("(char*)arr_1d", type="char *")
228 self.expect_var_path("((char*)arr_1d)[0]", type="char", value="'\\x01'")
229 self.expect_var_path("((char*)arr_1d)[1]", type="char", value="'\\0'")
230
231 # 2D arrays.
232 self.expect_var_path("(int*)arr_2d", type="int *")
233 self.expect_var_path("((int*)arr_2d)[1]", type="int", value="2")
234 self.expect_var_path("((int*)arr_2d)[2]", type="int", value="3")
235 self.expect_var_path("((int*)arr_2d[1])[1]", type="int", value="5")