Skip to content

Commit c9a39a8

Browse files
committed
[lldb] Add a display name to ClangASTContext instances
Summary: I often struggle to understand what exactly LLDB is doing by looking at our expression evaluation logging as our messages look like this: ``` CompleteTagDecl[2] on (ASTContext*)0x7ff31f01d240 Completing (TagDecl*)0x7ff31f01d568 named DeclName1 ``` From the log messages it's unclear what this ASTContext is. Is it the scratch context, the expression context, some decl vendor context or a context from a module? The pointer value isn't helpful for anyone unless I'm in a debugger where I could inspect the memory at the address. But even with a debugger it's not easy to figure out what this ASTContext is without having deeper understanding about all the different ASTContext instances in LLDB (e.g., valid SourceLocation from the file system usually means that this is the Objective-C decl vendor, a file name from multiple expressions is probably the scratch context, etc.). This patch adds a name field to ClangASTContext instances that we can use to store a name which can be used for logging and debugging. With this our log messages now look like this: ``` CompleteTagDecl[2] on scratch ASTContext. Completing (TagDecl*)0x7ff31f01d568 named Foo ``` We can now also just print a ClangASTContext from the debugger and see a useful name in the `m_display_name` field, e.g. ``` m_display_name = "AST for /Users/user/test/main.o"; ``` Reviewers: shafik, labath, JDevlieghere, mib Reviewed By: shafik Subscribers: clayborg, lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D72391
1 parent 2e25d75 commit c9a39a8

File tree

10 files changed

+104
-66
lines changed

10 files changed

+104
-66
lines changed

lldb/include/lldb/Symbol/ClangASTContext.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,20 @@ class ClangASTContext : public TypeSystem {
5757

5858
/// Constructs a ClangASTContext with an ASTContext using the given triple.
5959
///
60+
/// \param name The name for the ClangASTContext (for logging purposes)
6061
/// \param triple The llvm::Triple used for the ASTContext. The triple defines
6162
/// certain characteristics of the ASTContext and its types
6263
/// (e.g., whether certain primitive types exist or what their
6364
/// signedness is).
64-
explicit ClangASTContext(llvm::Triple triple);
65+
explicit ClangASTContext(llvm::StringRef name, llvm::Triple triple);
6566

6667
/// Constructs a ClangASTContext that uses an existing ASTContext internally.
6768
/// Useful when having an existing ASTContext created by Clang.
6869
///
70+
/// \param name The name for the ClangASTContext (for logging purposes)
6971
/// \param existing_ctxt An existing ASTContext.
70-
explicit ClangASTContext(clang::ASTContext &existing_ctxt);
72+
explicit ClangASTContext(llvm::StringRef name,
73+
clang::ASTContext &existing_ctxt);
7174

7275
~ClangASTContext() override;
7376

@@ -104,6 +107,10 @@ class ClangASTContext : public TypeSystem {
104107
return llvm::dyn_cast<ClangASTContext>(&type_system_or_err.get());
105108
}
106109

110+
/// Returns the display name of this ClangASTContext that indicates what
111+
/// purpose it serves in LLDB. Used for example in logs.
112+
llvm::StringRef getDisplayName() const { return m_display_name; }
113+
107114
clang::ASTContext &getASTContext();
108115

109116
clang::MangleContext *getMangleContext();
@@ -947,6 +954,10 @@ class ClangASTContext : public TypeSystem {
947954
std::unique_ptr<clang::MangleContext> m_mangle_ctx_up;
948955
uint32_t m_pointer_byte_size = 0;
949956
bool m_ast_owned = false;
957+
/// A string describing what this ClangASTContext represents (e.g.,
958+
/// AST for debug information, an expression, some other utility ClangAST).
959+
/// Useful for logging and debugging.
960+
std::string m_display_name;
950961

951962
typedef llvm::DenseMap<const clang::Decl *, ClangASTMetadata> DeclMetadataMap;
952963
/// Maps Decls to their associated ClangASTMetadata.

lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ void ClangASTSource::CompleteType(TagDecl *tag_decl) {
203203
LLDB_LOG(log,
204204
" CompleteTagDecl[{0}] on (ASTContext*){1} Completing "
205205
"(TagDecl*){2} named {3}",
206-
current_id, static_cast<void *>(m_ast_context),
207-
static_cast<void *>(tag_decl), tag_decl->getName());
206+
current_id, m_clang_ast_context->getDisplayName(), tag_decl,
207+
tag_decl->getName());
208208

209209
LLDB_LOG(log, " CTD[%u] Before:\n{0}", current_id,
210210
ClangUtil::DumpDecl(tag_decl));
@@ -336,9 +336,10 @@ void ClangASTSource::CompleteType(clang::ObjCInterfaceDecl *interface_decl) {
336336
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
337337

338338
LLDB_LOG(log,
339-
" [CompleteObjCInterfaceDecl] on (ASTContext*){0} Completing "
340-
"an ObjCInterfaceDecl named {1}",
341-
m_ast_context, interface_decl->getName());
339+
" [CompleteObjCInterfaceDecl] on (ASTContext*){0} '{1}' "
340+
"Completing an ObjCInterfaceDecl named {1}",
341+
m_ast_context, m_clang_ast_context->getDisplayName(),
342+
interface_decl->getName());
342343
LLDB_LOG(log, " [COID] Before:\n{0}",
343344
ClangUtil::DumpDecl(interface_decl));
344345

@@ -441,35 +442,36 @@ void ClangASTSource::FindExternalLexicalDecls(
441442
if (log) {
442443
if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
443444
LLDB_LOG(log,
444-
"FindExternalLexicalDecls[{0}] on (ASTContext*){1} in '{2}' "
445-
"(%sDecl*){3}",
446-
current_id, static_cast<void *>(m_ast_context),
445+
"FindExternalLexicalDecls[{0}] on (ASTContext*){1} '{2}' in "
446+
"'{3}' (%sDecl*){4}",
447+
current_id, m_ast_context, m_clang_ast_context->getDisplayName(),
447448
context_named_decl->getNameAsString().c_str(),
448449
context_decl->getDeclKindName(),
449450
static_cast<const void *>(context_decl));
450451
else if (context_decl)
451-
LLDB_LOG(
452-
log,
453-
"FindExternalLexicalDecls[{0}] on (ASTContext*){1} in ({2}Decl*){3}",
454-
current_id, static_cast<void *>(m_ast_context),
455-
context_decl->getDeclKindName(),
456-
static_cast<const void *>(context_decl));
452+
LLDB_LOG(log,
453+
"FindExternalLexicalDecls[{0}] on (ASTContext*){1} '{2}' in "
454+
"({3}Decl*){4}",
455+
current_id, m_ast_context, m_clang_ast_context->getDisplayName(),
456+
context_decl->getDeclKindName(),
457+
static_cast<const void *>(context_decl));
457458
else
458-
LLDB_LOG(
459-
log,
460-
"FindExternalLexicalDecls[{0}] on (ASTContext*){1} in a NULL context",
461-
current_id, static_cast<const void *>(m_ast_context));
459+
LLDB_LOG(log,
460+
"FindExternalLexicalDecls[{0}] on (ASTContext*){1} '{2}' in a "
461+
"NULL context",
462+
current_id, m_ast_context,
463+
m_clang_ast_context->getDisplayName());
462464
}
463465

464466
ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(context_decl);
465467

466468
if (!original.Valid())
467469
return;
468470

469-
LLDB_LOG(
470-
log, " FELD[{0}] Original decl (ASTContext*){1:x} (Decl*){2:x}:\n{3}",
471-
current_id, static_cast<void *>(original.ctx),
472-
static_cast<void *>(original.decl), ClangUtil::DumpDecl(original.decl));
471+
LLDB_LOG(log, " FELD[{0}] Original decl {1} (Decl*){2:x}:\n{3}", current_id,
472+
static_cast<void *>(original.ctx),
473+
static_cast<void *>(original.decl),
474+
ClangUtil::DumpDecl(original.decl));
473475

474476
if (ObjCInterfaceDecl *original_iface_decl =
475477
dyn_cast<ObjCInterfaceDecl>(original.decl)) {
@@ -563,20 +565,22 @@ void ClangASTSource::FindExternalVisibleDecls(NameSearchContext &context) {
563565
if (!context.m_decl_context)
564566
LLDB_LOG(log,
565567
"ClangASTSource::FindExternalVisibleDecls[{0}] on "
566-
"(ASTContext*){1} for '{2}' in a NULL DeclContext",
567-
current_id, m_ast_context, name);
568+
"(ASTContext*){1} '{2}' for '{3}' in a NULL DeclContext",
569+
current_id, m_ast_context, m_clang_ast_context->getDisplayName(),
570+
name);
568571
else if (const NamedDecl *context_named_decl =
569572
dyn_cast<NamedDecl>(context.m_decl_context))
570573
LLDB_LOG(log,
571574
"ClangASTSource::FindExternalVisibleDecls[{0}] on "
572-
"(ASTContext*){1} for '{2}' in '{3}'",
573-
current_id, m_ast_context, name, context_named_decl->getName());
575+
"(ASTContext*){1} '{2}' for '{3}' in '{4}'",
576+
current_id, m_ast_context, m_clang_ast_context->getDisplayName(),
577+
name, context_named_decl->getName());
574578
else
575579
LLDB_LOG(log,
576580
"ClangASTSource::FindExternalVisibleDecls[{0}] on "
577-
"(ASTContext*){1} for '{2}' in a '{3}'",
578-
current_id, m_ast_context, name,
579-
context.m_decl_context->getDeclKindName());
581+
"(ASTContext*){1} '{2}' for '{3}' in a '{4}'",
582+
current_id, m_ast_context, m_clang_ast_context->getDisplayName(),
583+
name, context.m_decl_context->getDeclKindName());
580584
}
581585

582586
context.m_namespace_map = std::make_shared<ClangASTImporter::NamespaceMap>();
@@ -1042,9 +1046,10 @@ void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) {
10421046
ConstString selector_name(ss.GetString());
10431047

10441048
LLDB_LOG(log,
1045-
"ClangASTSource::FindObjCMethodDecls[{0}] on (ASTContext*){1} "
1046-
"for selector [{2} {3}]",
1047-
current_id, m_ast_context, interface_decl->getName(), selector_name);
1049+
"ClangASTSource::FindObjCMethodDecls[{0}] on (ASTContext*){1} '{2}' "
1050+
"for selector [{3} {4}]",
1051+
current_id, m_ast_context, m_clang_ast_context->getDisplayName(),
1052+
interface_decl->getName(), selector_name);
10481053
SymbolContextList sc_list;
10491054

10501055
const bool include_symbols = false;
@@ -1337,9 +1342,9 @@ void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) {
13371342

13381343
LLDB_LOG(log,
13391344
"ClangASTSource::FindObjCPropertyAndIvarDecls[{0}] on "
1340-
"(ASTContext*){1} for '{2}.{3}'",
1341-
current_id, m_ast_context, parser_iface_decl->getName(),
1342-
context.m_decl_name.getAsString());
1345+
"(ASTContext*){1} '{2}' for '{3}.{4}'",
1346+
current_id, m_ast_context, m_clang_ast_context->getDisplayName(),
1347+
parser_iface_decl->getName(), context.m_decl_name.getAsString());
13431348

13441349
if (FindObjCPropertyAndIvarDeclsWithOrigin(
13451350
current_id, context, *this, origin_iface_decl))
@@ -1553,9 +1558,10 @@ bool ClangASTSource::layoutRecordType(const RecordDecl *record, uint64_t &size,
15531558
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
15541559

15551560
LLDB_LOG(log,
1556-
"LayoutRecordType[{0}] on (ASTContext*){1} for (RecordDecl*){2} "
1557-
"[name = '{3}']",
1558-
current_id, m_ast_context, record, record->getName());
1561+
"LayoutRecordType[{0}] on (ASTContext*){1} '{2}' for (RecordDecl*)"
1562+
"{3} [name = '{4}']",
1563+
current_id, m_ast_context, m_clang_ast_context->getDisplayName(),
1564+
record, record->getName());
15591565

15601566
DeclFromParser<const RecordDecl> parser_record(record);
15611567
DeclFromUser<const RecordDecl> origin_record(
@@ -1676,15 +1682,16 @@ void ClangASTSource::CompleteNamespaceMap(
16761682
if (log) {
16771683
if (parent_map && parent_map->size())
16781684
LLDB_LOG(log,
1679-
"CompleteNamespaceMap[{0}] on (ASTContext*){1} Searching for "
1680-
"namespace {2} in namespace {3}",
1681-
current_id, m_ast_context, name,
1682-
parent_map->begin()->second.GetName());
1685+
"CompleteNamespaceMap[{0}] on (ASTContext*){1} '{2}' Searching "
1686+
"for namespace {3} in namespace {4}",
1687+
current_id, m_ast_context, m_clang_ast_context->getDisplayName(),
1688+
name, parent_map->begin()->second.GetName());
16831689
else
16841690
LLDB_LOG(log,
1685-
"CompleteNamespaceMap[{0}] on (ASTContext*){1} Searching for "
1686-
"namespace {2}",
1687-
current_id, m_ast_context, name);
1691+
"CompleteNamespaceMap[{0}] on (ASTContext*){1} '{2}' Searching "
1692+
"for namespace {3}",
1693+
current_id, m_ast_context, m_clang_ast_context->getDisplayName(),
1694+
name);
16881695
}
16891696

16901697
if (parent_map) {

lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,8 @@ ClangExpressionParser::ClangExpressionParser(
606606
m_compiler->createASTContext();
607607
clang::ASTContext &ast_context = m_compiler->getASTContext();
608608

609-
m_ast_context.reset(new ClangASTContext(ast_context));
609+
m_ast_context.reset(new ClangASTContext(
610+
"Expression ASTContext for '" + m_filename + "'", ast_context));
610611

611612
std::string module_name("$__lldb_module");
612613

lldb/source/Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ ClangModulesDeclVendorImpl::ClangModulesDeclVendorImpl(
160160
m_parser(std::move(parser)) {
161161

162162
// Initialize our ClangASTContext.
163-
m_ast_context.reset(new ClangASTContext(m_compiler_instance->getASTContext()));
163+
m_ast_context.reset(
164+
new ClangASTContext("ClangModulesDeclVendor ASTContext",
165+
m_compiler_instance->getASTContext()));
164166
}
165167

166168
void ClangModulesDeclVendorImpl::ReportModuleExportsHelper(

lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCDeclVendor.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,9 @@ class lldb_private::AppleObjCExternalASTSource
143143

144144
AppleObjCDeclVendor::AppleObjCDeclVendor(ObjCLanguageRuntime &runtime)
145145
: ClangDeclVendor(eAppleObjCDeclVendor), m_runtime(runtime),
146-
m_ast_ctx(runtime.GetProcess()
147-
->GetTarget()
148-
.GetArchitecture()
149-
.GetTriple()),
146+
m_ast_ctx(
147+
"AppleObjCDeclVendor AST",
148+
runtime.GetProcess()->GetTarget().GetArchitecture().GetTriple()),
150149
m_type_realizer_sp(m_runtime.GetEncodingToType()) {
151150
m_external_source = new AppleObjCExternalASTSource(*this);
152151
llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> external_source_owning_ptr(

lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTypeEncodingParser.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ AppleObjCTypeEncodingParser::AppleObjCTypeEncodingParser(
2323
ObjCLanguageRuntime &runtime)
2424
: ObjCLanguageRuntime::EncodingToType(), m_runtime(runtime) {
2525
if (!m_scratch_ast_ctx_up)
26-
m_scratch_ast_ctx_up.reset(new ClangASTContext(runtime.GetProcess()
27-
->GetTarget()
28-
.GetArchitecture()
29-
.GetTriple()));
26+
m_scratch_ast_ctx_up.reset(new ClangASTContext(
27+
"AppleObjCTypeEncodingParser ASTContext",
28+
runtime.GetProcess()->GetTarget().GetArchitecture().GetTriple()));
3029
}
3130

3231
std::string AppleObjCTypeEncodingParser::ReadStructName(StringLexer &type) {

lldb/source/Symbol/ClangASTContext.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,15 +499,19 @@ static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
499499
Opts.NoInlineDefine = !Opt;
500500
}
501501

502-
ClangASTContext::ClangASTContext(llvm::Triple target_triple) {
502+
ClangASTContext::ClangASTContext(llvm::StringRef name,
503+
llvm::Triple target_triple) {
504+
m_display_name = name.str();
503505
if (!target_triple.str().empty())
504506
SetTargetTriple(target_triple.str());
505507
// The caller didn't pass an ASTContext so create a new one for this
506508
// ClangASTContext.
507509
CreateASTContext();
508510
}
509511

510-
ClangASTContext::ClangASTContext(ASTContext &existing_ctxt) {
512+
ClangASTContext::ClangASTContext(llvm::StringRef name,
513+
ASTContext &existing_ctxt) {
514+
m_display_name = name.str();
511515
SetTargetTriple(existing_ctxt.getTargetInfo().getTriple().str());
512516

513517
m_ast_up.reset(&existing_ctxt);
@@ -556,9 +560,11 @@ lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
556560
}
557561
}
558562

559-
if (module)
560-
return std::make_shared<ClangASTContext>(triple);
561-
else if (target && target->IsValid())
563+
if (module) {
564+
std::string ast_name =
565+
"ASTContext for '" + module->GetFileSpec().GetPath() + "'";
566+
return std::make_shared<ClangASTContext>(ast_name, triple);
567+
} else if (target && target->IsValid())
562568
return std::make_shared<ClangASTContextForExpressions>(*target, triple);
563569
return lldb::TypeSystemSP();
564570
}
@@ -9252,7 +9258,8 @@ ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
92529258

92539259
ClangASTContextForExpressions::ClangASTContextForExpressions(
92549260
Target &target, llvm::Triple triple)
9255-
: ClangASTContext(triple), m_target_wp(target.shared_from_this()),
9261+
: ClangASTContext("scratch ASTContext", triple),
9262+
m_target_wp(target.shared_from_this()),
92569263
m_persistent_variables(new ClangPersistentVariables) {
92579264
m_scratch_ast_source_up.reset(new ClangASTSource(
92589265
target.shared_from_this(), target.GetClangASTImporter()));

lldb/unittests/Symbol/TestClangASTContext.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class TestClangASTContext : public testing::Test {
2626
SubsystemRAII<FileSystem, HostInfo> subsystems;
2727

2828
void SetUp() override {
29-
m_ast.reset(new ClangASTContext(HostInfo::GetTargetTriple()));
29+
m_ast.reset(
30+
new ClangASTContext("test ASTContext", HostInfo::GetTargetTriple()));
3031
}
3132

3233
void TearDown() override { m_ast.reset(); }
@@ -220,6 +221,16 @@ TEST_F(TestClangASTContext, TestBuiltinTypeForEncodingAndBitSize) {
220221
VerifyEncodingAndBitSize(*m_ast, eEncodingIEEE754, 64);
221222
}
222223

224+
TEST_F(TestClangASTContext, TestDisplayName) {
225+
ClangASTContext ast("some name", llvm::Triple());
226+
EXPECT_EQ("some name", ast.getDisplayName());
227+
}
228+
229+
TEST_F(TestClangASTContext, TestDisplayNameEmpty) {
230+
ClangASTContext ast("", llvm::Triple());
231+
EXPECT_EQ("", ast.getDisplayName());
232+
}
233+
223234
TEST_F(TestClangASTContext, TestIsClangType) {
224235
clang::ASTContext &context = m_ast->getASTContext();
225236
lldb::opaque_compiler_type_t bool_ctype =

lldb/unittests/SymbolFile/DWARF/DWARFASTParserClangTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class DWARFASTParserClangStub : public DWARFASTParserClang {
3939
// defining here, causing this test to fail, feel free to delete it.
4040
TEST_F(DWARFASTParserClangTests,
4141
EnsureAllDIEsInDeclContextHaveBeenParsedParsesOnlyMatchingEntries) {
42-
ClangASTContext ast_ctx(HostInfoBase::GetTargetTriple());
42+
ClangASTContext ast_ctx("dummy ASTContext", HostInfoBase::GetTargetTriple());
4343
DWARFASTParserClangStub ast_parser(ast_ctx);
4444

4545
DWARFUnit *unit = nullptr;

lldb/unittests/TestingSupport/Symbol/ClangTestUtils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ inline clang::DeclarationName getDeclarationName(ClangASTContext &ast,
2222
}
2323

2424
inline std::unique_ptr<ClangASTContext> createAST() {
25-
return std::make_unique<ClangASTContext>(HostInfo::GetTargetTriple());
25+
return std::make_unique<ClangASTContext>("test ASTContext",
26+
HostInfo::GetTargetTriple());
2627
}
2728

2829
inline CompilerType createRecord(ClangASTContext &ast, llvm::StringRef name) {

0 commit comments

Comments
 (0)