Skip to content

Commit f6e0567

Browse files
committed
[lldb] Add a test for how we lazily create Clang AST nodes
Summary: One of the ways we try to make LLDB faster is by only creating the Clang declarations (and loading the associated types) when we actually need them for something. For example an evaluated expression might need to load types to type check and codegen the expression. Currently this mechanism isn't really tested, so we currently have no way to know how many Clang nodes we load and when we load them. In general there seems to be some confusion when and why certain Clang nodes are created. As we are about to make some changes to the code which is creating Clang AST nodes we probably should have a test that at least checks that the current behaviour doesn't change. It also serves as some kind of documentation on the current behaviour. The test in this patch is just evaluating some expressions and checks which Clang nodes are created due to this in the module AST. The check happens by looking at the AST dump of the current module and then scanning it for the declarations we are looking for. I'm aware that there are things missing in this test (inheritance, template parameters, non-expression evaluation commands) but I'll expand it in follow up patches. Also this test found two potential bugs in LLDB which are documented near the respective asserts in the test: 1. LLDB seems to always load all types of local variables even when we don't reference them in the expression. We had patches that tried to prevent this but it seems that didn't work as well as it should have (even though we don't complete these types). 2. We always seem to complete the first field of any record we run into. This has the funny side effect that LLDB is faster when all classes in a project have an arbitrary `char unused;` as their first member. We probably want to fix this. Reviewers: shafik Subscribers: abidh, JDevlieghere, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D71056
1 parent a383969 commit f6e0567

File tree

3 files changed

+307
-0
lines changed

3 files changed

+307
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CXX_SOURCES := main.cpp
2+
include Makefile.rules
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
from lldbsuite.test.decorators import *
2+
from lldbsuite.test.lldbtest import *
3+
from lldbsuite.test import lldbutil
4+
5+
"""
6+
This test ensures that we only create Clang AST nodes in our module AST
7+
when we actually need them.
8+
9+
All tests in this file behave like this:
10+
1. Use LLDB to do something (expression evaluation, breakpoint setting, etc.).
11+
2. Check that certain Clang AST nodes were not loaded during the previous
12+
step.
13+
"""
14+
15+
class TestCase(TestBase):
16+
17+
NO_DEBUG_INFO_TESTCASE = True
18+
mydir = TestBase.compute_mydir(__file__)
19+
20+
def setUp(self):
21+
TestBase.setUp(self)
22+
# Only build this test once.
23+
self.build()
24+
25+
# Clang declaration kind we are looking for.
26+
class_decl_kind = "CXXRecordDecl"
27+
# FIXME: This shouldn't be a CXXRecordDecl, but that's how we model
28+
# structs in Clang.
29+
struct_decl_kind = "CXXRecordDecl"
30+
31+
# The decls we use in this program in the format that
32+
# decl_in_line and decl_completed_in_line expect (which is a pair of
33+
# node type and the unqualified declaration name.
34+
struct_first_member_decl = [struct_decl_kind, "StructFirstMember"]
35+
struct_behind_ptr_decl = [struct_decl_kind, "StructBehindPointer"]
36+
struct_behind_ref_decl = [struct_decl_kind, "StructBehindRef"]
37+
struct_member_decl = [struct_decl_kind, "StructMember"]
38+
some_struct_decl = [struct_decl_kind, "SomeStruct"]
39+
other_struct_decl = [struct_decl_kind, "OtherStruct"]
40+
class_in_namespace_decl = [class_decl_kind, "ClassInNamespace"]
41+
class_we_enter_decl = [class_decl_kind, "ClassWeEnter"]
42+
class_member_decl = [struct_decl_kind, "ClassMember"]
43+
unused_class_member_decl = [struct_decl_kind, "UnusedClassMember"]
44+
unused_class_member_ptr_decl = [struct_decl_kind, "UnusedClassMemberPtr"]
45+
46+
def assert_no_decls_loaded(self):
47+
"""
48+
Asserts that no known declarations in this test are loaded
49+
into the module's AST.
50+
"""
51+
self.assert_decl_not_loaded(self.struct_first_member_decl)
52+
self.assert_decl_not_loaded(self.struct_behind_ptr_decl)
53+
self.assert_decl_not_loaded(self.struct_behind_ref_decl)
54+
self.assert_decl_not_loaded(self.struct_member_decl)
55+
self.assert_decl_not_loaded(self.some_struct_decl)
56+
self.assert_decl_not_loaded(self.other_struct_decl)
57+
self.assert_decl_not_loaded(self.class_in_namespace_decl)
58+
self.assert_decl_not_loaded(self.class_member_decl)
59+
self.assert_decl_not_loaded(self.unused_class_member_decl)
60+
61+
def get_ast_dump(self):
62+
"""Returns the dumped Clang AST of the module as a string"""
63+
res = lldb.SBCommandReturnObject()
64+
ci = self.dbg.GetCommandInterpreter()
65+
ci.HandleCommand('target modules dump ast a.out', res)
66+
self.assertTrue(res.Succeeded())
67+
return res.GetOutput()
68+
69+
def decl_in_line(self, line, decl):
70+
"""
71+
Returns true iff the given line declares the given Clang decl.
72+
The line is expected to be in the form of Clang's AST dump.
73+
"""
74+
line = line.rstrip() + "\n"
75+
decl_kind = "-" + decl[0] + " "
76+
# Either the decl is somewhere in the line or at the end of
77+
# the line.
78+
decl_name = " " + decl[1] + " "
79+
decl_name_eol = " " + decl[1] + "\n"
80+
if not decl_kind in line:
81+
return False
82+
return decl_name in line or decl_name_eol in line
83+
84+
def decl_completed_in_line(self, line, decl):
85+
"""
86+
Returns true iff the given line declares the given Clang decl and
87+
the decl was completed (i.e., it has no undeserialized declarations
88+
in it).
89+
"""
90+
return self.decl_in_line(line, decl) and not "<undeserialized declarations>" in line
91+
92+
# The following asserts are used for checking if certain Clang declarations
93+
# were loaded or not since the target was created.
94+
95+
def assert_decl_loaded(self, decl):
96+
"""
97+
Asserts the given decl is currently loaded.
98+
Note: This test is about checking that types/declarations are not
99+
loaded. If this assert fails it is usually fine to turn it into a
100+
assert_decl_not_loaded or assert_decl_not_completed assuming LLDB's
101+
functionality has not suffered by not loading this declaration.
102+
"""
103+
ast = self.get_ast_dump()
104+
found = False
105+
for line in ast.splitlines():
106+
if self.decl_in_line(line, decl):
107+
found = True
108+
self.assertTrue(self.decl_completed_in_line(line, decl),
109+
"Should have called assert_decl_not_completed")
110+
self.assertTrue(found, "Declaration no longer loaded " + str(decl) +
111+
".\nAST:\n" + ast)
112+
113+
def assert_decl_not_completed(self, decl):
114+
"""
115+
Asserts that the given decl is currently not completed in the module's
116+
AST. It may be loaded but then can can only contain undeserialized
117+
declarations.
118+
"""
119+
ast = self.get_ast_dump()
120+
found = False
121+
for line in ast.splitlines():
122+
error_msg = "Unexpected completed decl: '" + line + "'.\nAST:\n" + ast
123+
self.assertFalse(self.decl_completed_in_line(line, decl), error_msg)
124+
125+
def assert_decl_not_loaded(self, decl):
126+
"""
127+
Asserts that the given decl is currently not loaded in the module's
128+
AST.
129+
"""
130+
ast = self.get_ast_dump()
131+
found = False
132+
for line in ast.splitlines():
133+
error_msg = "Unexpected loaded decl: '" + line + "'\nAST:\n" + ast
134+
self.assertFalse(self.decl_in_line(line, decl), error_msg)
135+
136+
137+
def clean_setup(self, location):
138+
"""
139+
Runs to the line with the source line with the given location string
140+
and ensures that our module AST is empty.
141+
"""
142+
lldbutil.run_to_source_breakpoint(self,
143+
"// Location: " + location, lldb.SBFileSpec("main.cpp"))
144+
# Make sure no declarations are loaded initially.
145+
self.assert_no_decls_loaded()
146+
147+
@add_test_categories(["dwarf"])
148+
def test_arithmetic_expression_in_main(self):
149+
""" Runs a simple arithmetic expression which should load nothing. """
150+
self.clean_setup(location="multiple locals function")
151+
152+
self.expect("expr 1 + (int)2.0", substrs=['(int) $0'])
153+
154+
# This should not have loaded any decls.
155+
self.assert_no_decls_loaded()
156+
157+
@add_test_categories(["dwarf"])
158+
def test_printing_local_variable_in_other_struct_func(self):
159+
"""
160+
Prints a local variable and makes sure no unrelated types are loaded.
161+
"""
162+
self.clean_setup(location="other struct function")
163+
164+
self.expect("expr other_struct_var", substrs=['(OtherStruct) $0'])
165+
# The decl we run on was loaded.
166+
self.assert_decl_loaded(self.other_struct_decl)
167+
168+
# This should not have loaded anything else.
169+
self.assert_decl_not_loaded(self.some_struct_decl)
170+
self.assert_decl_not_loaded(self.class_in_namespace_decl)
171+
172+
@add_test_categories(["dwarf"])
173+
def test_printing_struct_with_multiple_locals(self):
174+
"""
175+
Prints a local variable and checks that we don't load other local
176+
variables.
177+
"""
178+
self.clean_setup(location="multiple locals function")
179+
180+
self.expect("expr struct_var", substrs=['(SomeStruct) $0'])
181+
182+
# We loaded SomeStruct and its member types for printing.
183+
self.assert_decl_loaded(self.some_struct_decl)
184+
self.assert_decl_loaded(self.struct_behind_ptr_decl)
185+
self.assert_decl_loaded(self.struct_behind_ref_decl)
186+
187+
# FIXME: We don't use these variables, but we seem to load all local
188+
# local variables.
189+
self.assert_decl_not_completed(self.other_struct_decl)
190+
self.assert_decl_not_completed(self.class_in_namespace_decl)
191+
192+
@add_test_categories(["dwarf"])
193+
def test_addr_of_struct(self):
194+
"""
195+
Prints the address of a local variable (which is a struct).
196+
"""
197+
self.clean_setup(location="multiple locals function")
198+
199+
self.expect("expr &struct_var", substrs=['(SomeStruct *) $0'])
200+
201+
# We loaded SomeStruct.
202+
self.assert_decl_loaded(self.some_struct_decl)
203+
204+
# The member declarations should not be completed.
205+
self.assert_decl_not_completed(self.struct_behind_ptr_decl)
206+
self.assert_decl_not_completed(self.struct_behind_ref_decl)
207+
208+
# FIXME: The first member was behind a pointer so it shouldn't be
209+
# completed. Somehow LLDB really wants to load the first member, so
210+
# that is why have it defined here.
211+
self.assert_decl_loaded(self.struct_first_member_decl)
212+
213+
# FIXME: We don't use these variables, but we seem to load all local
214+
# local variables.
215+
self.assert_decl_not_completed(self.other_struct_decl)
216+
self.assert_decl_not_completed(self.class_in_namespace_decl)
217+
218+
@add_test_categories(["dwarf"])
219+
def test_class_function_access_member(self):
220+
self.clean_setup(location="class function")
221+
222+
self.expect("expr member", substrs=['(ClassMember) $0'])
223+
224+
# We loaded the current class we touched.
225+
self.assert_decl_loaded(self.class_we_enter_decl)
226+
# We loaded the unused members of this class.
227+
self.assert_decl_loaded(self.unused_class_member_decl)
228+
self.assert_decl_not_completed(self.unused_class_member_ptr_decl)
229+
# We loaded the member we used.
230+
self.assert_decl_loaded(self.class_member_decl)
231+
232+
# This should not have loaded anything else.
233+
self.assert_decl_not_loaded(self.other_struct_decl)
234+
self.assert_decl_not_loaded(self.some_struct_decl)
235+
self.assert_decl_not_loaded(self.class_in_namespace_decl)
236+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//----------------------------------------------------------------------------//
2+
// Struct loading declarations.
3+
4+
struct StructFirstMember { int i; };
5+
struct StructBehindPointer { int i; };
6+
struct StructBehindRef { int i; };
7+
struct StructMember { int i; };
8+
9+
StructBehindRef struct_instance;
10+
11+
struct SomeStruct {
12+
StructFirstMember *first;
13+
StructBehindPointer *ptr;
14+
StructMember member;
15+
StructBehindRef &ref = struct_instance;
16+
};
17+
18+
struct OtherStruct {
19+
int member_int;
20+
};
21+
22+
//----------------------------------------------------------------------------//
23+
// Class loading declarations.
24+
25+
struct ClassMember { int i; };
26+
struct UnusedClassMember { int i; };
27+
struct UnusedClassMemberPtr { int i; };
28+
29+
namespace NS {
30+
class ClassInNamespace {
31+
int i;
32+
};
33+
class ClassWeEnter {
34+
public:
35+
int dummy; // Prevent bug where LLDB always completes first member.
36+
ClassMember member;
37+
UnusedClassMember unused_member;
38+
UnusedClassMemberPtr *unused_member_ptr;
39+
int enteredFunction() {
40+
return member.i; // Location: class function
41+
}
42+
};
43+
};
44+
45+
//----------------------------------------------------------------------------//
46+
// Function we can stop in.
47+
48+
int functionWithOtherStruct() {
49+
OtherStruct other_struct_var;
50+
other_struct_var.member_int++; // Location: other struct function
51+
return other_struct_var.member_int;
52+
}
53+
54+
int functionWithMultipleLocals() {
55+
SomeStruct struct_var;
56+
OtherStruct other_struct_var;
57+
NS::ClassInNamespace namespace_class;
58+
other_struct_var.member_int++; // Location: multiple locals function
59+
return other_struct_var.member_int;
60+
}
61+
62+
int main(int argc, char **argv) {
63+
NS::ClassWeEnter c;
64+
c.enteredFunction();
65+
66+
functionWithOtherStruct();
67+
functionWithMultipleLocals();
68+
return 0;
69+
}

0 commit comments

Comments
 (0)