LLDB mainline
ClangASTSource.cpp
Go to the documentation of this file.
1//===-- ClangASTSource.cpp ------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ClangASTSource.h"
10
12
13#include "lldb/Core/Module.h"
19#include "lldb/Target/Target.h"
21#include "lldb/Utility/Log.h"
22#include "clang/AST/ASTContext.h"
23#include "clang/Basic/SourceManager.h"
24
28
29#include <memory>
30#include <vector>
31
32using namespace clang;
33using namespace lldb_private;
34
35// Scoped class that will remove an active lexical decl from the set when it
36// goes out of scope.
37namespace {
38class ScopedLexicalDeclEraser {
39public:
40 ScopedLexicalDeclEraser(std::set<const clang::Decl *> &decls,
41 const clang::Decl *decl)
42 : m_active_lexical_decls(decls), m_decl(decl) {}
43
44 ~ScopedLexicalDeclEraser() { m_active_lexical_decls.erase(m_decl); }
45
46private:
47 std::set<const clang::Decl *> &m_active_lexical_decls;
48 const clang::Decl *m_decl;
49};
50}
51
53 const lldb::TargetSP &target,
54 const std::shared_ptr<ClangASTImporter> &importer)
55 : m_lookups_enabled(false), m_target(target), m_ast_context(nullptr),
58 assert(m_ast_importer_sp && "No ClangASTImporter passed to ClangASTSource?");
59}
60
62 m_ast_context = &clang_ast_context.getASTContext();
63 m_clang_ast_context = &clang_ast_context;
64 m_file_manager = &m_ast_context->getSourceManager().getFileManager();
65 m_ast_importer_sp->InstallMapCompleter(m_ast_context, *this);
66}
67
69 m_ast_importer_sp->ForgetDestination(m_ast_context);
70
71 if (!m_target)
72 return;
73
74 // Unregister the current ASTContext as a source for all scratch
75 // ASTContexts in the ClangASTImporter. Without this the scratch AST might
76 // query the deleted ASTContext for additional type information.
77 // We unregister from *all* scratch ASTContexts in case a type got exported
78 // to a scratch AST that isn't the best fitting scratch ASTContext.
81
82 if (!scratch_ts_sp)
83 return;
84
85 ScratchTypeSystemClang *default_scratch_ast =
86 llvm::cast<ScratchTypeSystemClang>(scratch_ts_sp.get());
87 // Unregister from the default scratch AST (and all sub-ASTs).
88 default_scratch_ast->ForgetSource(m_ast_context, *m_ast_importer_sp);
89}
90
91void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) {
92 if (!m_ast_context)
93 return;
94
95 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
96 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
97}
98
99// The core lookup interface.
101 const DeclContext *decl_ctx, DeclarationName clang_decl_name,
102 const clang::DeclContext *original_dc) {
103 if (!m_ast_context) {
104 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
105 return false;
106 }
107
108 std::string decl_name(clang_decl_name.getAsString());
109
110 switch (clang_decl_name.getNameKind()) {
111 // Normal identifiers.
112 case DeclarationName::Identifier: {
113 clang::IdentifierInfo *identifier_info =
114 clang_decl_name.getAsIdentifierInfo();
115
116 if (!identifier_info || identifier_info->getBuiltinID() != 0) {
117 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
118 return false;
119 }
120 } break;
121
122 // Operator names.
123 case DeclarationName::CXXOperatorName:
124 case DeclarationName::CXXLiteralOperatorName:
125 break;
126
127 // Using directives found in this context.
128 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
129 case DeclarationName::CXXUsingDirective:
130 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
131 return false;
132
133 case DeclarationName::ObjCZeroArgSelector:
134 case DeclarationName::ObjCOneArgSelector:
135 case DeclarationName::ObjCMultiArgSelector: {
136 llvm::SmallVector<NamedDecl *, 1> method_decls;
137
138 NameSearchContext method_search_context(*m_clang_ast_context, method_decls,
139 clang_decl_name, decl_ctx);
140
141 FindObjCMethodDecls(method_search_context);
142
143 SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, method_decls);
144 return (method_decls.size() > 0);
145 }
146 // These aren't possible in the global context.
147 case DeclarationName::CXXConstructorName:
148 case DeclarationName::CXXDestructorName:
149 case DeclarationName::CXXConversionFunctionName:
150 case DeclarationName::CXXDeductionGuideName:
151 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
152 return false;
153 }
154
155 if (!GetLookupsEnabled()) {
156 // Wait until we see a '$' at the start of a name before we start doing any
157 // lookups so we can avoid lookup up all of the builtin types.
158 if (!decl_name.empty() && decl_name[0] == '$') {
159 SetLookupsEnabled(true);
160 } else {
161 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
162 return false;
163 }
164 }
165
166 ConstString const_decl_name(decl_name.c_str());
167
168 const char *uniqued_const_decl_name = const_decl_name.GetCString();
169 if (m_active_lookups.find(uniqued_const_decl_name) !=
170 m_active_lookups.end()) {
171 // We are currently looking up this name...
172 SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
173 return false;
174 }
175 m_active_lookups.insert(uniqued_const_decl_name);
176 llvm::SmallVector<NamedDecl *, 4> name_decls;
177 NameSearchContext name_search_context(*m_clang_ast_context, name_decls,
178 clang_decl_name, decl_ctx);
179 FindExternalVisibleDecls(name_search_context);
180 SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, name_decls);
181 m_active_lookups.erase(uniqued_const_decl_name);
182 return (name_decls.size() != 0);
183}
184
185TagDecl *ClangASTSource::FindCompleteType(const TagDecl *decl) {
187
188 if (const NamespaceDecl *namespace_context =
189 dyn_cast<NamespaceDecl>(decl->getDeclContext())) {
191 m_ast_importer_sp->GetNamespaceMap(namespace_context);
192
193 if (!namespace_map)
194 return nullptr;
195
197 " CTD Inspecting namespace map{0:x} ({1} entries)",
198 namespace_map.get(), namespace_map->size());
199
200 for (const ClangASTImporter::NamespaceMapItem &item : *namespace_map) {
201 LLDB_LOG(log, " CTD Searching namespace {0} in module {1}",
202 item.second.GetName(), item.first->GetFileSpec().GetFilename());
203
204 ConstString name(decl->getName());
205
206 // Create a type matcher using the CompilerDeclContext for the namespace
207 // as the context (item.second) and search for the name inside of this
208 // context.
209 TypeQuery query(item.second, name);
210 TypeResults results;
211 item.first->FindTypes(query, results);
212
213 for (const lldb::TypeSP &type_sp : results.GetTypeMap().Types()) {
214 CompilerType clang_type(type_sp->GetFullCompilerType());
215
216 if (!ClangUtil::IsClangType(clang_type))
217 continue;
218
219 const TagType *tag_type =
220 ClangUtil::GetQualType(clang_type)->getAs<TagType>();
221
222 if (!tag_type)
223 continue;
224
225 TagDecl *candidate_tag_decl =
226 tag_type->getDecl()->getDefinitionOrSelf();
227
229 &candidate_tag_decl->getASTContext(), candidate_tag_decl))
230 return candidate_tag_decl;
231 }
232 }
233 } else {
234 const ModuleList &module_list = m_target->GetImages();
235 // Create a type matcher using a CompilerDecl. Each TypeSystem class knows
236 // how to fill out a CompilerContext array using a CompilerDecl.
237 TypeQuery query(CompilerDecl(m_clang_ast_context, (void *)decl));
238 TypeResults results;
239 module_list.FindTypes(nullptr, query, results);
240 for (const lldb::TypeSP &type_sp : results.GetTypeMap().Types()) {
241
242 CompilerType clang_type(type_sp->GetFullCompilerType());
243
244 if (!ClangUtil::IsClangType(clang_type))
245 continue;
246
247 const TagType *tag_type =
248 ClangUtil::GetQualType(clang_type)->getAs<TagType>();
249
250 if (!tag_type)
251 continue;
252
253 TagDecl *candidate_tag_decl = tag_type->getDecl()->getDefinitionOrSelf();
254
255 if (TypeSystemClang::GetCompleteDecl(&candidate_tag_decl->getASTContext(),
256 candidate_tag_decl))
257 return candidate_tag_decl;
258 }
259 }
260 return nullptr;
261}
262
263void ClangASTSource::CompleteType(TagDecl *tag_decl) {
265
266 LLDB_LOG(log,
267 " CompleteTagDecl on (ASTContext*){0} Completing "
268 "(TagDecl*){1:x} named {2}",
270 tag_decl->getName());
271
272 LLDB_LOG(log, " CTD Before:\n{0}", ClangUtil::DumpDecl(tag_decl));
273
274 auto iter = m_active_lexical_decls.find(tag_decl);
275 if (iter != m_active_lexical_decls.end())
276 return;
277 m_active_lexical_decls.insert(tag_decl);
278 ScopedLexicalDeclEraser eraser(m_active_lexical_decls, tag_decl);
279
280 if (!m_ast_importer_sp->CompleteTagDecl(tag_decl)) {
281 // We couldn't complete the type. Maybe there's a definition somewhere
282 // else that can be completed.
283 if (TagDecl *alternate = FindCompleteType(tag_decl))
284 m_ast_importer_sp->CompleteTagDeclWithOrigin(tag_decl, alternate);
285 }
286
287 LLDB_LOG(log, " [CTD] After:\n{0}", ClangUtil::DumpDecl(tag_decl));
288}
289
290void ClangASTSource::CompleteType(clang::ObjCInterfaceDecl *interface_decl) {
292
293 LLDB_LOG(log,
294 " [CompleteObjCInterfaceDecl] on (ASTContext*){0:x} '{1}' "
295 "Completing an ObjCInterfaceDecl named {2}",
296 m_ast_context, m_clang_ast_context->getDisplayName(),
297 interface_decl->getName());
298 LLDB_LOG(log, " [COID] Before:\n{0}",
299 ClangUtil::DumpDecl(interface_decl));
300
301 ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl);
302
303 if (original.Valid()) {
304 if (ObjCInterfaceDecl *original_iface_decl =
305 dyn_cast<ObjCInterfaceDecl>(original.decl)) {
306 ObjCInterfaceDecl *complete_iface_decl =
307 GetCompleteObjCInterface(original_iface_decl);
308
309 if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) {
310 m_ast_importer_sp->SetDeclOrigin(interface_decl, complete_iface_decl);
311 }
312 }
313 }
314
315 m_ast_importer_sp->CompleteObjCInterfaceDecl(interface_decl);
316
317 if (interface_decl->getSuperClass() &&
318 interface_decl->getSuperClass() != interface_decl)
319 CompleteType(interface_decl->getSuperClass());
320
321 LLDB_LOG(log, " [COID] After:");
322 LLDB_LOG(log, " [COID] {0}", ClangUtil::DumpDecl(interface_decl));
323}
324
326 const clang::ObjCInterfaceDecl *interface_decl) {
327 lldb::ProcessSP process(m_target->GetProcessSP());
328
329 if (!process)
330 return nullptr;
331
332 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
333
334 if (!language_runtime)
335 return nullptr;
336
337 ConstString class_name(interface_decl->getNameAsString().c_str());
338
339 lldb::TypeSP complete_type_sp(
340 language_runtime->LookupInCompleteClassCache(class_name));
341
342 if (!complete_type_sp)
343 return nullptr;
344
345 TypeFromUser complete_type =
346 TypeFromUser(complete_type_sp->GetFullCompilerType());
347 lldb::opaque_compiler_type_t complete_opaque_type =
348 complete_type.GetOpaqueQualType();
349
350 if (!complete_opaque_type)
351 return nullptr;
352
353 const clang::Type *complete_clang_type =
354 QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
355 const ObjCInterfaceType *complete_interface_type =
356 dyn_cast<ObjCInterfaceType>(complete_clang_type);
357
358 if (!complete_interface_type)
359 return nullptr;
360
361 ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());
362
363 return complete_iface_decl;
364}
365
367 const DeclContext *decl_context,
368 llvm::function_ref<bool(Decl::Kind)> predicate,
370
372
373 const Decl *context_decl = dyn_cast<Decl>(decl_context);
374
375 if (!context_decl)
376 return;
377
378 auto iter = m_active_lexical_decls.find(context_decl);
379 if (iter != m_active_lexical_decls.end())
380 return;
381 m_active_lexical_decls.insert(context_decl);
382 ScopedLexicalDeclEraser eraser(m_active_lexical_decls, context_decl);
383
384 if (log) {
385 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
386 LLDB_LOG(log,
387 "FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in "
388 "'{2}' ({3}Decl*){4}",
389 m_ast_context, m_clang_ast_context->getDisplayName(),
390 context_named_decl->getNameAsString().c_str(),
391 context_decl->getDeclKindName(),
392 static_cast<const void *>(context_decl));
393 else if (context_decl)
394 LLDB_LOG(log,
395 "FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in "
396 "({2}Decl*){3}",
397 m_ast_context, m_clang_ast_context->getDisplayName(),
398 context_decl->getDeclKindName(),
399 static_cast<const void *>(context_decl));
400 else
401 LLDB_LOG(log,
402 "FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in a "
403 "NULL context",
404 m_ast_context, m_clang_ast_context->getDisplayName());
405 }
406
407 ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(context_decl);
408
409 if (!original.Valid())
410 return;
411
412 LLDB_LOG(log, " FELD Original decl (ASTContext*){0:x} (Decl*){1:x}:\n{2}",
413 static_cast<void *>(original.ctx),
414 static_cast<void *>(original.decl),
415 ClangUtil::DumpDecl(original.decl));
416
417 if (ObjCInterfaceDecl *original_iface_decl =
418 dyn_cast<ObjCInterfaceDecl>(original.decl)) {
419 ObjCInterfaceDecl *complete_iface_decl =
420 GetCompleteObjCInterface(original_iface_decl);
421
422 if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) {
423 original.decl = complete_iface_decl;
424 original.ctx = &complete_iface_decl->getASTContext();
425
426 m_ast_importer_sp->SetDeclOrigin(context_decl, complete_iface_decl);
427 }
428 }
429
430 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original.decl)) {
431 ExternalASTSource *external_source = original.ctx->getExternalSource();
432
433 if (external_source)
434 external_source->CompleteType(original_tag_decl);
435 }
436
437 const DeclContext *original_decl_context =
438 dyn_cast<DeclContext>(original.decl);
439
440 if (!original_decl_context)
441 return;
442
443 // Indicates whether we skipped any Decls of the original DeclContext.
444 bool SkippedDecls = false;
445 for (Decl *decl : original_decl_context->decls()) {
446 // The predicate function returns true if the passed declaration kind is
447 // the one we are looking for.
448 // See clang::ExternalASTSource::FindExternalLexicalDecls()
449 if (predicate(decl->getKind())) {
450 if (log) {
451 std::string ast_dump = ClangUtil::DumpDecl(decl);
452 if (const NamedDecl *context_named_decl =
453 dyn_cast<NamedDecl>(context_decl))
454 LLDB_LOG(log, " FELD Adding [to {0}Decl {1}] lexical {2}Decl {3}",
455 context_named_decl->getDeclKindName(),
456 context_named_decl->getName(), decl->getDeclKindName(),
457 ast_dump);
458 else
459 LLDB_LOG(log, " FELD Adding lexical {0}Decl {1}",
460 decl->getDeclKindName(), ast_dump);
461 }
462
463 Decl *copied_decl = CopyDecl(decl);
464
465 if (!copied_decl)
466 continue;
467
468 // FIXME: We should add the copied decl to the 'decls' list. This would
469 // add the copied Decl into the DeclContext and make sure that we
470 // correctly propagate that we added some Decls back to Clang.
471 // By leaving 'decls' empty we incorrectly return false from
472 // DeclContext::LoadLexicalDeclsFromExternalStorage which might cause
473 // lookup issues later on.
474 // We can't just add them for now as the ASTImporter already added the
475 // decl into the DeclContext and this would add it twice.
476
477 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl)) {
478 QualType copied_field_type = copied_field->getType();
479
480 m_ast_importer_sp->RequireCompleteType(copied_field_type);
481 }
482 } else {
483 SkippedDecls = true;
484 }
485 }
486
487 // CopyDecl may build a lookup table which may set up ExternalLexicalStorage
488 // to false. However, since we skipped some of the external Decls we must
489 // set it back!
490 if (SkippedDecls) {
491 decl_context->setHasExternalLexicalStorage(true);
492 // This sets HasLazyExternalLexicalLookups to true. By setting this bit we
493 // ensure that the lookup table is rebuilt, which means the external source
494 // is consulted again when a clang::DeclContext::lookup is called.
495 const_cast<DeclContext *>(decl_context)->setMustBuildLookupTable();
496 }
497}
498
500 assert(m_ast_context);
501
502 const ConstString name(context.m_decl_name.getAsString().c_str());
503
505
506 if (log) {
507 if (!context.m_decl_context)
508 LLDB_LOG(log,
509 "ClangASTSource::FindExternalVisibleDecls on "
510 "(ASTContext*){0:x} '{1}' for '{2}' in a NULL DeclContext",
511 m_ast_context, m_clang_ast_context->getDisplayName(), name);
512 else if (const NamedDecl *context_named_decl =
513 dyn_cast<NamedDecl>(context.m_decl_context))
514 LLDB_LOG(log,
515 "ClangASTSource::FindExternalVisibleDecls on "
516 "(ASTContext*){0:x} '{1}' for '{2}' in '{3}'",
517 m_ast_context, m_clang_ast_context->getDisplayName(), name,
518 context_named_decl->getName());
519 else
520 LLDB_LOG(log,
521 "ClangASTSource::FindExternalVisibleDecls on "
522 "(ASTContext*){0:x} '{1}' for '{2}' in a '{3}'",
523 m_ast_context, m_clang_ast_context->getDisplayName(), name,
524 context.m_decl_context->getDeclKindName());
525 }
526
527 if (isa<NamespaceDecl>(context.m_decl_context)) {
528 LookupInNamespace(context);
529 } else if (isa<ObjCInterfaceDecl>(context.m_decl_context)) {
531 } else if (!isa<TranslationUnitDecl>(context.m_decl_context)) {
532 // we shouldn't be getting FindExternalVisibleDecls calls for these
533 return;
534 } else {
535 CompilerDeclContext namespace_decl;
536
537 LLDB_LOG(log, " CAS::FEVD Searching the root namespace");
538
539 FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl);
540 }
541
542 if (!context.m_namespace_map->empty()) {
544 log, " CAS::FEVD Registering namespace map {0:x} ({1} entries)",
545 context.m_namespace_map.get(), context.m_namespace_map->size());
546
547 NamespaceDecl *clang_namespace_decl = AddNamespace(context);
548
549 if (clang_namespace_decl)
550 clang_namespace_decl->setHasExternalVisibleStorage();
551 }
552}
553
555 return m_clang_ast_context->getSema();
556}
557
559 bool ignore_all_dollar_names) {
560 static const ConstString id_name("id");
561 static const ConstString Class_name("Class");
562
563 if (m_ast_context->getLangOpts().ObjC)
564 if (name == id_name || name == Class_name)
565 return true;
566
567 StringRef name_string_ref = name.GetStringRef();
568
569 // The ClangASTSource is not responsible for finding $-names.
570 return name_string_ref.empty() ||
571 (ignore_all_dollar_names && name_string_ref.starts_with("$")) ||
572 name_string_ref.starts_with("_$");
573}
574
576 NameSearchContext &context, lldb::ModuleSP module_sp,
577 CompilerDeclContext &namespace_decl) {
578 assert(m_ast_context);
579
581
582 SymbolContextList sc_list;
583
584 const ConstString name(context.m_decl_name.getAsString().c_str());
585 if (IgnoreName(name, true))
586 return;
587
588 if (!m_target)
589 return;
590
591 FillNamespaceMap(context, module_sp, namespace_decl);
592
593 if (context.m_found_type)
594 return;
595
596 lldb::TypeSP type_sp;
597 TypeResults results;
598 if (module_sp && namespace_decl) {
599 // Match the name in the specified decl context.
600 TypeQuery query(namespace_decl, name, TypeQueryOptions::e_find_one);
601 module_sp->FindTypes(query, results);
602 type_sp = results.GetFirstType();
603 } else {
604 // Match the exact name of the type at the root level.
605 TypeQuery query(name.GetStringRef(), TypeQueryOptions::e_exact_match |
606 TypeQueryOptions::e_find_one);
607 m_target->GetImages().FindTypes(nullptr, query, results);
608 type_sp = results.GetFirstType();
609 }
610
611 if (type_sp) {
612 if (log) {
613 const char *name_string = type_sp->GetName().GetCString();
614
615 LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\": {1}", name,
616 (name_string ? name_string : "<anonymous>"));
617 }
618
619 CompilerType full_type = type_sp->GetFullCompilerType();
620
621 CompilerType copied_clang_type(GuardedCopyType(full_type));
622
623 if (!copied_clang_type) {
624 LLDB_LOG(log, " CAS::FEVD - Couldn't export a type");
625 } else {
626
627 context.AddTypeDecl(copied_clang_type);
628
629 context.m_found_type = true;
630 }
631 }
632
633 if (!context.m_found_type) {
634 // Try the modules next.
635 FindDeclInModules(context, name);
636 }
637
638 if (!context.m_found_type && m_ast_context->getLangOpts().ObjC) {
639 FindDeclInObjCRuntime(context, name);
640 }
641}
642
644 NameSearchContext &context, lldb::ModuleSP module_sp,
645 const CompilerDeclContext &namespace_decl) {
646 const ConstString name(context.m_decl_name.getAsString().c_str());
647 if (IgnoreName(name, true))
648 return;
649
651
652 if (module_sp && namespace_decl) {
653 CompilerDeclContext found_namespace_decl;
654
655 if (SymbolFile *symbol_file = module_sp->GetSymbolFile()) {
656 found_namespace_decl = symbol_file->FindNamespace(name, namespace_decl);
657
658 if (found_namespace_decl) {
659 context.m_namespace_map->push_back(
660 std::pair<lldb::ModuleSP, CompilerDeclContext>(
661 module_sp, found_namespace_decl));
662
663 LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}", name,
664 module_sp->GetFileSpec().GetFilename());
665 }
666 }
667 return;
668 }
669
670 for (lldb::ModuleSP image : m_target->GetImages().Modules()) {
671 if (!image)
672 continue;
673
674 CompilerDeclContext found_namespace_decl;
675
676 SymbolFile *symbol_file = image->GetSymbolFile();
677
678 if (!symbol_file)
679 continue;
680
681 // If namespace_decl is not valid, 'FindNamespace' would look for
682 // any namespace called 'name' (ignoring parent contexts) and return
683 // the first one it finds. Thus if we're doing a qualified lookup only
684 // consider root namespaces. E.g., in an expression ::A::B::Foo, the
685 // lookup of ::A will result in a qualified lookup. Note, namespace
686 // disambiguation for function calls are handled separately in
687 // SearchFunctionsInSymbolContexts.
688 const bool find_root_namespaces =
689 context.m_decl_context &&
690 context.m_decl_context->shouldUseQualifiedLookup();
691 found_namespace_decl = symbol_file->FindNamespace(
692 name, namespace_decl, /* only root namespaces */ find_root_namespaces);
693
694 if (found_namespace_decl) {
695 context.m_namespace_map->push_back(
696 std::pair<lldb::ModuleSP, CompilerDeclContext>(image,
697 found_namespace_decl));
698
699 LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}", name,
700 image->GetFileSpec().GetFilename());
701 }
702 }
703}
704
706 NameSearchContext &context, ObjCInterfaceDecl *original_interface_decl,
707 const char *log_info) {
708 const DeclarationName &decl_name(context.m_decl_name);
709 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
710
711 Selector original_selector;
712
713 if (decl_name.isObjCZeroArgSelector()) {
714 const IdentifierInfo *ident =
715 &original_ctx->Idents.get(decl_name.getAsString());
716 original_selector = original_ctx->Selectors.getSelector(0, &ident);
717 } else if (decl_name.isObjCOneArgSelector()) {
718 const std::string &decl_name_string = decl_name.getAsString();
719 std::string decl_name_string_without_colon(decl_name_string.c_str(),
720 decl_name_string.length() - 1);
721 const IdentifierInfo *ident =
722 &original_ctx->Idents.get(decl_name_string_without_colon);
723 original_selector = original_ctx->Selectors.getSelector(1, &ident);
724 } else {
725 SmallVector<const IdentifierInfo *, 4> idents;
726
727 clang::Selector sel = decl_name.getObjCSelector();
728
729 unsigned num_args = sel.getNumArgs();
730
731 for (unsigned i = 0; i != num_args; ++i) {
732 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
733 }
734
735 original_selector =
736 original_ctx->Selectors.getSelector(num_args, idents.data());
737 }
738
739 DeclarationName original_decl_name(original_selector);
740
741 llvm::SmallVector<NamedDecl *, 1> methods;
742
743 TypeSystemClang::GetCompleteDecl(original_ctx, original_interface_decl);
744
745 if (ObjCMethodDecl *instance_method_decl =
746 original_interface_decl->lookupInstanceMethod(original_selector)) {
747 methods.push_back(instance_method_decl);
748 } else if (ObjCMethodDecl *class_method_decl =
749 original_interface_decl->lookupClassMethod(
750 original_selector)) {
751 methods.push_back(class_method_decl);
752 }
753
754 if (methods.empty()) {
755 return false;
756 }
757
758 for (NamedDecl *named_decl : methods) {
759 if (!named_decl)
760 continue;
761
762 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl);
763
764 if (!result_method)
765 continue;
766
767 Decl *copied_decl = CopyDecl(result_method);
768
769 if (!copied_decl)
770 continue;
771
772 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
773
774 if (!copied_method_decl)
775 continue;
776
778
779 LLDB_LOG(log, " CAS::FOMD found ({0}) {1}", log_info,
780 ClangUtil::DumpDecl(copied_method_decl));
781
782 context.AddNamedDecl(copied_method_decl);
783 }
784
785 return true;
786}
787
789 ConstString name) {
791
792 std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
794 if (!modules_decl_vendor)
795 return;
796
797 bool append = false;
798 uint32_t max_matches = 1;
799 std::vector<CompilerDecl> decls;
800
801 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
802 return;
803
804 LLDB_LOG(log, " CAS::FEVD Matching entity found for \"{0}\" in the modules",
805 name);
806
807 auto *const decl_from_modules =
808 llvm::cast<NamedDecl>(ClangUtil::GetDecl(decls[0]));
809
810 if (llvm::isa<clang::TypeDecl>(decl_from_modules) ||
811 llvm::isa<clang::ObjCContainerDecl>(decl_from_modules) ||
812 llvm::isa<clang::EnumConstantDecl>(decl_from_modules)) {
813 clang::Decl *copied_decl = CopyDecl(decl_from_modules);
814 clang::NamedDecl *copied_named_decl =
815 copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr;
816
817 if (!copied_named_decl) {
818 LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the modules");
819
820 return;
821 }
822
823 context.AddNamedDecl(copied_named_decl);
824
825 context.m_found_type = true;
826 }
827}
828
830 ConstString name) {
832
833 lldb::ProcessSP process(m_target->GetProcessSP());
834
835 if (!process)
836 return;
837
838 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
839
840 if (!language_runtime)
841 return;
842
843 DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
844
845 if (!decl_vendor)
846 return;
847
848 bool append = false;
849 uint32_t max_matches = 1;
850 std::vector<CompilerDecl> decls;
851
852 auto *clang_decl_vendor = llvm::cast<DeclVendor>(decl_vendor);
853 if (!clang_decl_vendor->FindDecls(name, append, max_matches, decls))
854 return;
855
856 LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\" in the runtime",
857 name);
858
859 clang::Decl *copied_decl = CopyDecl(ClangUtil::GetDecl(decls[0]));
860 clang::NamedDecl *copied_named_decl =
861 copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr;
862
863 if (!copied_named_decl) {
864 LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the runtime");
865
866 return;
867 }
868
869 context.AddNamedDecl(copied_named_decl);
870}
871
874
875 const DeclarationName &decl_name(context.m_decl_name);
876 const DeclContext *decl_ctx(context.m_decl_context);
877
878 const ObjCInterfaceDecl *interface_decl =
879 dyn_cast<ObjCInterfaceDecl>(decl_ctx);
880
881 if (!interface_decl)
882 return;
883
884 do {
885 ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl);
886
887 if (!original.Valid())
888 break;
889
890 ObjCInterfaceDecl *original_interface_decl =
891 dyn_cast<ObjCInterfaceDecl>(original.decl);
892
893 if (FindObjCMethodDeclsWithOrigin(context, original_interface_decl,
894 "at origin"))
895 return; // found it, no need to look any further
896 } while (false);
897
898 StreamString ss;
899
900 if (decl_name.isObjCZeroArgSelector()) {
901 ss.Printf("%s", decl_name.getAsString().c_str());
902 } else if (decl_name.isObjCOneArgSelector()) {
903 ss.Printf("%s", decl_name.getAsString().c_str());
904 } else {
905 clang::Selector sel = decl_name.getObjCSelector();
906
907 for (unsigned i = 0, e = sel.getNumArgs(); i != e; ++i) {
908 llvm::StringRef r = sel.getNameForSlot(i);
909 ss.Printf("%s:", r.str().c_str());
910 }
911 }
912 ss.Flush();
913
914 if (ss.GetString().contains("$__lldb"))
915 return; // we don't need any results
916
917 ConstString selector_name(ss.GetString());
918
919 LLDB_LOG(log,
920 "ClangASTSource::FindObjCMethodDecls on (ASTContext*){0:x} '{1}' "
921 "for selector [{2} {3}]",
922 m_ast_context, m_clang_ast_context->getDisplayName(),
923 interface_decl->getName(), selector_name);
924 SymbolContextList sc_list;
925
926 ModuleFunctionSearchOptions function_options;
927 function_options.include_symbols = false;
928 function_options.include_inlines = false;
929
930 std::string interface_name = interface_decl->getNameAsString();
931
932 do {
933 StreamString ms;
934 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
935 ms.Flush();
936 ConstString instance_method_name(ms.GetString());
937
938 sc_list.Clear();
939 m_target->GetImages().FindFunctions(instance_method_name,
940 lldb::eFunctionNameTypeFull,
941 function_options, sc_list);
942
943 if (sc_list.GetSize())
944 break;
945
946 ms.Clear();
947 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
948 ms.Flush();
949 ConstString class_method_name(ms.GetString());
950
951 sc_list.Clear();
952 m_target->GetImages().FindFunctions(class_method_name,
953 lldb::eFunctionNameTypeFull,
954 function_options, sc_list);
955
956 if (sc_list.GetSize())
957 break;
958
959 // Fall back and check for methods in categories. If we find methods this
960 // way, we need to check that they're actually in categories on the desired
961 // class.
962
963 SymbolContextList candidate_sc_list;
964
965 m_target->GetImages().FindFunctions(selector_name,
966 lldb::eFunctionNameTypeSelector,
967 function_options, candidate_sc_list);
968
969 for (const SymbolContext &candidate_sc : candidate_sc_list) {
970 if (!candidate_sc.function)
971 continue;
972
973 const char *candidate_name = candidate_sc.function->GetName().AsCString();
974
975 const char *cursor = candidate_name;
976
977 if (*cursor != '+' && *cursor != '-')
978 continue;
979
980 ++cursor;
981
982 if (*cursor != '[')
983 continue;
984
985 ++cursor;
986
987 size_t interface_len = interface_name.length();
988
989 if (strncmp(cursor, interface_name.c_str(), interface_len))
990 continue;
991
992 cursor += interface_len;
993
994 if (*cursor == ' ' || *cursor == '(')
995 sc_list.Append(candidate_sc);
996 }
997 } while (false);
998
999 if (sc_list.GetSize()) {
1000 // We found a good function symbol. Use that.
1001
1002 for (const SymbolContext &sc : sc_list) {
1003 if (!sc.function)
1004 continue;
1005
1006 CompilerDeclContext function_decl_ctx = sc.function->GetDeclContext();
1007 if (!function_decl_ctx)
1008 continue;
1009
1010 ObjCMethodDecl *method_decl =
1012
1013 if (!method_decl)
1014 continue;
1015
1016 ObjCInterfaceDecl *found_interface_decl =
1017 method_decl->getClassInterface();
1018
1019 if (!found_interface_decl)
1020 continue;
1021
1022 if (found_interface_decl->getName() == interface_decl->getName()) {
1023 Decl *copied_decl = CopyDecl(method_decl);
1024
1025 if (!copied_decl)
1026 continue;
1027
1028 ObjCMethodDecl *copied_method_decl =
1029 dyn_cast<ObjCMethodDecl>(copied_decl);
1030
1031 if (!copied_method_decl)
1032 continue;
1033
1034 LLDB_LOG(log, " CAS::FOMD found (in symbols)\n{0}",
1035 ClangUtil::DumpDecl(copied_method_decl));
1036
1037 context.AddNamedDecl(copied_method_decl);
1038 }
1039 }
1040
1041 return;
1042 }
1043
1044 // Try the debug information.
1045
1046 do {
1047 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(
1048 const_cast<ObjCInterfaceDecl *>(interface_decl));
1049
1050 if (!complete_interface_decl)
1051 break;
1052
1053 // We found the complete interface. The runtime never needs to be queried
1054 // in this scenario.
1055
1056 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(
1057 complete_interface_decl);
1058
1059 if (complete_interface_decl == interface_decl)
1060 break; // already checked this one
1061
1062 LLDB_LOG(log,
1063 "CAS::FOPD trying origin "
1064 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",
1065 complete_interface_decl, &complete_iface_decl->getASTContext());
1066
1067 FindObjCMethodDeclsWithOrigin(context, complete_interface_decl,
1068 "in debug info");
1069
1070 return;
1071 } while (false);
1072
1073 do {
1074 // Check the modules only if the debug information didn't have a complete
1075 // interface.
1076
1077 if (std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1079 ConstString interface_name(interface_decl->getNameAsString().c_str());
1080 bool append = false;
1081 uint32_t max_matches = 1;
1082 std::vector<CompilerDecl> decls;
1083
1084 if (!modules_decl_vendor->FindDecls(interface_name, append, max_matches,
1085 decls))
1086 break;
1087
1088 ObjCInterfaceDecl *interface_decl_from_modules =
1089 dyn_cast<ObjCInterfaceDecl>(ClangUtil::GetDecl(decls[0]));
1090
1091 if (!interface_decl_from_modules)
1092 break;
1093
1094 if (FindObjCMethodDeclsWithOrigin(context, interface_decl_from_modules,
1095 "in modules"))
1096 return;
1097 }
1098 } while (false);
1099
1100 do {
1101 // Check the runtime only if the debug information didn't have a complete
1102 // interface and the modules don't get us anywhere.
1103
1104 lldb::ProcessSP process(m_target->GetProcessSP());
1105
1106 if (!process)
1107 break;
1108
1109 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
1110
1111 if (!language_runtime)
1112 break;
1113
1114 DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
1115
1116 if (!decl_vendor)
1117 break;
1118
1119 ConstString interface_name(interface_decl->getNameAsString().c_str());
1120 bool append = false;
1121 uint32_t max_matches = 1;
1122 std::vector<CompilerDecl> decls;
1123
1124 auto *clang_decl_vendor = llvm::cast<DeclVendor>(decl_vendor);
1125 if (!clang_decl_vendor->FindDecls(interface_name, append, max_matches,
1126 decls))
1127 break;
1128
1129 ObjCInterfaceDecl *runtime_interface_decl =
1130 dyn_cast<ObjCInterfaceDecl>(ClangUtil::GetDecl(decls[0]));
1131
1132 if (!runtime_interface_decl)
1133 break;
1134
1135 FindObjCMethodDeclsWithOrigin(context, runtime_interface_decl,
1136 "in runtime");
1137 } while (false);
1138}
1139
1141 NameSearchContext &context,
1142 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl) {
1144
1145 if (origin_iface_decl.IsInvalid())
1146 return false;
1147
1148 std::string name_str = context.m_decl_name.getAsString();
1149 StringRef name(name_str);
1150 IdentifierInfo &name_identifier(
1151 origin_iface_decl->getASTContext().Idents.get(name));
1152
1153 DeclFromUser<ObjCPropertyDecl> origin_property_decl(
1154 origin_iface_decl->FindPropertyDeclaration(
1155 &name_identifier, ObjCPropertyQueryKind::OBJC_PR_query_instance));
1156
1157 bool found = false;
1158
1159 if (origin_property_decl.IsValid()) {
1160 DeclFromParser<ObjCPropertyDecl> parser_property_decl(
1161 origin_property_decl.Import(m_ast_context, *m_ast_importer_sp));
1162 if (parser_property_decl.IsValid()) {
1163 LLDB_LOG(log, " CAS::FOPD found\n{0}",
1164 ClangUtil::DumpDecl(parser_property_decl.decl));
1165
1166 context.AddNamedDecl(parser_property_decl.decl);
1167 found = true;
1168 }
1169 }
1170
1171 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(
1172 origin_iface_decl->getIvarDecl(&name_identifier));
1173
1174 if (origin_ivar_decl.IsValid()) {
1175 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(
1176 origin_ivar_decl.Import(m_ast_context, *m_ast_importer_sp));
1177 if (parser_ivar_decl.IsValid()) {
1178 LLDB_LOG(log, " CAS::FOPD found\n{0}",
1179 ClangUtil::DumpDecl(parser_ivar_decl.decl));
1180
1181 context.AddNamedDecl(parser_ivar_decl.decl);
1182 found = true;
1183 }
1184 }
1185
1186 return found;
1187}
1188
1191
1193 cast<ObjCInterfaceDecl>(context.m_decl_context));
1194 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(
1195 parser_iface_decl.GetOrigin(*m_ast_importer_sp));
1196
1197 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
1198
1199 LLDB_LOG(log,
1200 "ClangASTSource::FindObjCPropertyAndIvarDecls on "
1201 "(ASTContext*){0:x} '{1}' for '{2}.{3}'",
1202 m_ast_context, m_clang_ast_context->getDisplayName(),
1203 parser_iface_decl->getName(), context.m_decl_name.getAsString());
1204
1205 if (FindObjCPropertyAndIvarDeclsWithOrigin(context, origin_iface_decl))
1206 return;
1207
1208 LLDB_LOG(log,
1209 "CAS::FOPD couldn't find the property on origin "
1210 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}, searching "
1211 "elsewhere...",
1212 origin_iface_decl.decl, &origin_iface_decl->getASTContext());
1213
1214 SymbolContext null_sc;
1215 TypeList type_list;
1216
1217 do {
1218 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(
1219 const_cast<ObjCInterfaceDecl *>(parser_iface_decl.decl));
1220
1221 if (!complete_interface_decl)
1222 break;
1223
1224 // We found the complete interface. The runtime never needs to be queried
1225 // in this scenario.
1226
1227 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(
1228 complete_interface_decl);
1229
1230 if (complete_iface_decl.decl == origin_iface_decl.decl)
1231 break; // already checked this one
1232
1233 LLDB_LOG(log,
1234 "CAS::FOPD trying origin "
1235 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",
1236 complete_iface_decl.decl, &complete_iface_decl->getASTContext());
1237
1238 FindObjCPropertyAndIvarDeclsWithOrigin(context, complete_iface_decl);
1239
1240 return;
1241 } while (false);
1242
1243 do {
1244 // Check the modules only if the debug information didn't have a complete
1245 // interface.
1246
1247 std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1249
1250 if (!modules_decl_vendor)
1251 break;
1252
1253 bool append = false;
1254 uint32_t max_matches = 1;
1255 std::vector<CompilerDecl> decls;
1256
1257 if (!modules_decl_vendor->FindDecls(class_name, append, max_matches, decls))
1258 break;
1259
1260 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_modules(
1261 dyn_cast<ObjCInterfaceDecl>(ClangUtil::GetDecl(decls[0])));
1262
1263 if (!interface_decl_from_modules.IsValid())
1264 break;
1265
1266 LLDB_LOG(log,
1267 "CAS::FOPD[{0:x}] trying module "
1268 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",
1269 interface_decl_from_modules.decl,
1270 &interface_decl_from_modules->getASTContext());
1271
1273 interface_decl_from_modules))
1274 return;
1275 } while (false);
1276
1277 do {
1278 // Check the runtime only if the debug information didn't have a complete
1279 // interface and nothing was in the modules.
1280
1281 lldb::ProcessSP process(m_target->GetProcessSP());
1282
1283 if (!process)
1284 return;
1285
1286 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
1287
1288 if (!language_runtime)
1289 return;
1290
1291 DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
1292
1293 if (!decl_vendor)
1294 break;
1295
1296 bool append = false;
1297 uint32_t max_matches = 1;
1298 std::vector<CompilerDecl> decls;
1299
1300 auto *clang_decl_vendor = llvm::cast<DeclVendor>(decl_vendor);
1301 if (!clang_decl_vendor->FindDecls(class_name, append, max_matches, decls))
1302 break;
1303
1304 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_runtime(
1305 dyn_cast<ObjCInterfaceDecl>(ClangUtil::GetDecl(decls[0])));
1306
1307 if (!interface_decl_from_runtime.IsValid())
1308 break;
1309
1310 LLDB_LOG(log,
1311 "CAS::FOPD[{0:x}] trying runtime "
1312 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",
1313 interface_decl_from_runtime.decl,
1314 &interface_decl_from_runtime->getASTContext());
1315
1317 interface_decl_from_runtime))
1318 return;
1319 } while (false);
1320}
1321
1323 const NamespaceDecl *namespace_context =
1324 dyn_cast<NamespaceDecl>(context.m_decl_context);
1325
1327
1328 ClangASTImporter::NamespaceMapSP namespace_map =
1329 m_ast_importer_sp->GetNamespaceMap(namespace_context);
1330
1331 LLDB_LOG_VERBOSE(log,
1332 " CAS::FEVD Inspecting namespace map {0:x} ({1} entries)",
1333 namespace_map.get(), namespace_map->size());
1334
1335 if (!namespace_map)
1336 return;
1337
1338 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(),
1339 e = namespace_map->end();
1340 i != e; ++i) {
1341 LLDB_LOG(log, " CAS::FEVD Searching namespace {0} in module {1}",
1342 i->second.GetName(), i->first->GetFileSpec().GetFilename());
1343
1344 FindExternalVisibleDecls(context, i->first, i->second);
1345 }
1346}
1347
1349 const RecordDecl *record, uint64_t &size, uint64_t &alignment,
1350 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
1351 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
1352 &base_offsets,
1353 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
1354 &virtual_base_offsets) {
1355 return m_ast_importer_sp->importRecordLayoutFromOrigin(
1356 record, size, alignment, field_offsets, base_offsets,
1357 virtual_base_offsets);
1358}
1359
1361 ClangASTImporter::NamespaceMapSP &namespace_map, ConstString name,
1362 ClangASTImporter::NamespaceMapSP &parent_map) const {
1363
1365
1366 if (log) {
1367 if (parent_map && parent_map->size())
1368 LLDB_LOG(log,
1369 "CompleteNamespaceMap on (ASTContext*){0:x} '{1}' Searching "
1370 "for namespace {2} in namespace {3}",
1371 m_ast_context, m_clang_ast_context->getDisplayName(), name,
1372 parent_map->begin()->second.GetName());
1373 else
1374 LLDB_LOG(log,
1375 "CompleteNamespaceMap on (ASTContext*){0} '{1}' Searching "
1376 "for namespace {2}",
1377 m_ast_context, m_clang_ast_context->getDisplayName(), name);
1378 }
1379
1380 if (parent_map) {
1381 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(),
1382 e = parent_map->end();
1383 i != e; ++i) {
1384 CompilerDeclContext found_namespace_decl;
1385
1386 lldb::ModuleSP module_sp = i->first;
1387 CompilerDeclContext module_parent_namespace_decl = i->second;
1388
1389 SymbolFile *symbol_file = module_sp->GetSymbolFile();
1390
1391 if (!symbol_file)
1392 continue;
1393
1394 found_namespace_decl =
1395 symbol_file->FindNamespace(name, module_parent_namespace_decl);
1396
1397 if (!found_namespace_decl)
1398 continue;
1399
1400 namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(
1401 module_sp, found_namespace_decl));
1402
1403 LLDB_LOG(log, " CMN Found namespace {0} in module {1}", name,
1404 module_sp->GetFileSpec().GetFilename());
1405 }
1406 } else {
1407 CompilerDeclContext null_namespace_decl;
1408 for (lldb::ModuleSP image : m_target->GetImages().Modules()) {
1409 if (!image)
1410 continue;
1411
1412 CompilerDeclContext found_namespace_decl;
1413
1414 SymbolFile *symbol_file = image->GetSymbolFile();
1415
1416 if (!symbol_file)
1417 continue;
1418
1419 found_namespace_decl =
1420 symbol_file->FindNamespace(name, null_namespace_decl);
1421
1422 if (!found_namespace_decl)
1423 continue;
1424
1425 namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(
1426 image, found_namespace_decl));
1427
1428 LLDB_LOG(log, " CMN[{0}] Found namespace {0} in module {1}", name,
1429 image->GetFileSpec().GetFilename());
1430 }
1431 }
1432}
1433
1435 if (!context.m_namespace_map)
1436 return nullptr;
1437
1438 const CompilerDeclContext &namespace_decl =
1439 context.m_namespace_map->begin()->second;
1440
1441 clang::ASTContext *src_ast =
1443 if (!src_ast)
1444 return nullptr;
1445 clang::NamespaceDecl *src_namespace_decl =
1447
1448 if (!src_namespace_decl)
1449 return nullptr;
1450
1451 Decl *copied_decl = CopyDecl(src_namespace_decl);
1452
1453 if (!copied_decl)
1454 return nullptr;
1455
1456 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1457
1458 if (!copied_namespace_decl)
1459 return nullptr;
1460
1461 context.m_decls.push_back(copied_namespace_decl);
1462
1463 m_ast_importer_sp->RegisterNamespaceMap(copied_namespace_decl,
1464 context.m_namespace_map);
1465
1466 return dyn_cast<NamespaceDecl>(copied_decl);
1467}
1468
1469clang::Decl *ClangASTSource::CopyDecl(Decl *src_decl) {
1470 return m_ast_importer_sp->CopyDecl(m_ast_context, src_decl);
1471}
1472
1474 return m_ast_importer_sp->GetDeclOrigin(decl);
1475}
1476
1478 auto src_ast = src_type.GetTypeSystem<TypeSystemClang>();
1479 if (!src_ast)
1480 return {};
1481
1482 QualType copied_qual_type = ClangUtil::GetQualType(
1483 m_ast_importer_sp->CopyType(*m_clang_ast_context, src_type));
1484
1485 if (copied_qual_type.getAsOpaquePtr() &&
1486 copied_qual_type->getCanonicalTypeInternal().isNull())
1487 // this shouldn't happen, but we're hardening because the AST importer
1488 // seems to be generating bad types on occasion.
1489 return {};
1490
1491 return m_clang_ast_context->GetType(copied_qual_type);
1492}
1493
1494std::shared_ptr<ClangModulesDeclVendor>
1496 auto persistent_vars = llvm::cast<ClangPersistentVariables>(
1497 m_target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
1498 return persistent_vars->GetClangModulesDeclVendor();
1499}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
#define LLDB_LOG_VERBOSE(log,...)
Definition Log.h:376
std::shared_ptr< NamespaceMap > NamespaceMapSP
std::pair< lldb::ModuleSP, CompilerDeclContext > NamespaceMapItem
std::set< const char * > m_active_lookups
void LookupInNamespace(NameSearchContext &context)
Performs lookup into a namespace.
void SetLookupsEnabled(bool lookups_enabled)
clang::TagDecl * FindCompleteType(const clang::TagDecl *decl)
clang::ASTContext * m_ast_context
The AST context requests are coming in for.
ClangASTSource(const lldb::TargetSP &target, const std::shared_ptr< ClangASTImporter > &importer)
Constructor.
std::set< const clang::Decl * > m_active_lexical_decls
void FindExternalLexicalDecls(const clang::DeclContext *DC, llvm::function_ref< bool(clang::Decl::Kind)> IsKindWeWant, llvm::SmallVectorImpl< clang::Decl * > &Decls) override
Enumerate all Decls in a given lexical context.
bool layoutRecordType(const clang::RecordDecl *Record, uint64_t &Size, uint64_t &Alignment, llvm::DenseMap< const clang::FieldDecl *, uint64_t > &FieldOffsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &BaseOffsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &VirtualBaseOffsets) override
Specify the layout of the contents of a RecordDecl.
clang::Decl * CopyDecl(clang::Decl *src_decl)
Copies a single Decl into the parser's AST context.
bool IgnoreName(const ConstString name, bool ignore_all_dollar_names)
Returns true if a name should be ignored by name lookup.
virtual void FindExternalVisibleDecls(NameSearchContext &context)
The worker function for FindExternalVisibleDeclsByName.
void FindObjCPropertyAndIvarDecls(NameSearchContext &context)
Find all Objective-C properties and ivars with a given name.
clang::NamespaceDecl * AddNamespace(NameSearchContext &context)
ClangASTImporter::DeclOrigin GetDeclOrigin(const clang::Decl *decl)
Determined the origin of a single Decl, if it can be found.
bool FindObjCPropertyAndIvarDeclsWithOrigin(NameSearchContext &context, DeclFromUser< const clang::ObjCInterfaceDecl > &origin_iface_decl)
bool FindObjCMethodDeclsWithOrigin(NameSearchContext &context, clang::ObjCInterfaceDecl *original_interface_decl, const char *log_info)
std::shared_ptr< ClangModulesDeclVendor > GetClangModulesDeclVendor()
void CompleteNamespaceMap(ClangASTImporter::NamespaceMapSP &namespace_map, ConstString name, ClangASTImporter::NamespaceMapSP &parent_map) const override
Look up the modules containing a given namespace and put the appropriate entries in the namespace map...
void FindDeclInObjCRuntime(NameSearchContext &context, ConstString name)
void CompleteType(clang::TagDecl *Tag) override
Complete a TagDecl.
~ClangASTSource() override
Destructor.
void FillNamespaceMap(NameSearchContext &context, lldb::ModuleSP module_sp, const CompilerDeclContext &namespace_decl)
Fills the namespace map of the given NameSearchContext.
CompilerType GuardedCopyType(const CompilerType &src_type)
A wrapper for TypeSystemClang::CopyType that sets a flag that indicates that we should not respond to...
void FindDeclInModules(NameSearchContext &context, ConstString name)
bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC, clang::DeclarationName Name, const clang::DeclContext *OriginalDC) override
Look up all Decls that match a particular name.
std::shared_ptr< ClangASTImporter > m_ast_importer_sp
The target's AST importer.
void InstallASTContext(TypeSystemClang &ast_context)
TypeSystemClang * m_clang_ast_context
The TypeSystemClang for m_ast_context.
void FindObjCMethodDecls(NameSearchContext &context)
Find all Objective-C methods matching a given selector.
const lldb::TargetSP m_target
The target to use in finding variables and types.
clang::ObjCInterfaceDecl * GetCompleteObjCInterface(const clang::ObjCInterfaceDecl *interface_decl)
Look for the complete version of an Objective-C interface, and return it if found.
void StartTranslationUnit(clang::ASTConsumer *Consumer) override
Called on entering a translation unit.
clang::FileManager * m_file_manager
The file manager paired with the AST context.
Represents a generic declaration context in a program.
Represents a generic declaration such as a function declaration.
Generic representation of a type in a programming language.
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
lldb::opaque_compiler_type_t GetOpaqueQualType() const
A uniqued constant string class.
Definition ConstString.h:40
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
const char * GetCString() const
Get the string value as a C string.
DeclFromUser< D > GetOrigin(ClangASTImporter &importer)
DeclFromParser< D > Import(clang::ASTContext *dest_ctx, ClangASTImporter &importer)
virtual DeclVendor * GetDeclVendor()
A collection class for Module objects.
Definition ModuleList.h:125
void FindTypes(Module *search_first, const TypeQuery &query, lldb_private::TypeResults &results) const
Find types using a type-matching object that contains all search parameters.
lldb::TypeSP LookupInCompleteClassCache(ConstString &name)
static ObjCLanguageRuntime * Get(Process &process)
The TypeSystemClang instance used for the scratch ASTContext in a lldb::Target.
static lldb::TypeSystemClangSP GetForTarget(Target &target, std::optional< IsolatedASTKind > ast_kind=DefaultAST, bool create_on_demand=true)
Returns the scratch TypeSystemClang for the given target.
void ForgetSource(clang::ASTContext *src_ctx, ClangASTImporter &importer)
Unregisters the given ASTContext as a source from the scratch AST (and all sub-ASTs).
static const std::nullopt_t DefaultAST
Alias for requesting the default scratch TypeSystemClang in GetForTarget.
void Flush() override
Flush the stream.
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
Defines a list of symbol context objects.
uint32_t GetSize() const
Get accessor for a symbol context list size.
void Append(const SymbolContext &sc)
Append a new symbol context to the list.
void Clear()
Clear the object's state.
Defines a symbol context baton that can be handed other debug core functions.
Provides public interface for all SymbolFiles.
Definition SymbolFile.h:51
virtual CompilerDeclContext FindNamespace(ConstString name, const CompilerDeclContext &parent_decl_ctx, bool only_root_namespaces=false)
Finds a namespace of name name and whose parent context is parent_decl_ctx.
Definition SymbolFile.h:366
TypeIterable Types() const
Definition TypeMap.h:50
A class that contains all state required for type lookups.
Definition Type.h:104
This class tracks the state and results of a TypeQuery.
Definition Type.h:344
TypeMap & GetTypeMap()
Definition Type.h:386
lldb::TypeSP GetFirstType() const
Definition Type.h:385
A TypeSystem implementation based on Clang.
llvm::StringRef getDisplayName() const
Returns the display name of this TypeSystemClang that indicates what purpose it serves in LLDB.
static clang::ASTContext * DeclContextGetTypeSystemClang(const CompilerDeclContext &dc)
static clang::ObjCMethodDecl * DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc)
static clang::NamespaceDecl * DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc)
bool GetCompleteDecl(clang::Decl *decl)
clang::ASTContext & getASTContext() const
Returns the clang::ASTContext instance managed by this TypeSystemClang.
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
TaggedASTType< 1 > TypeFromUser
void * opaque_compiler_type_t
Definition lldb-types.h:90
@ eLanguageTypeC
Non-standardized C, such as K&R.
std::shared_ptr< lldb_private::Type > TypeSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::TypeSystemClang > TypeSystemClangSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP
static clang::QualType GetQualType(const CompilerType &ct)
Definition ClangUtil.cpp:36
static std::string DumpDecl(const clang::Decl *d)
Returns a textual representation of the given Decl's AST.
Definition ClangUtil.cpp:68
static clang::Decl * GetDecl(const CompilerDecl &decl)
Returns the clang::Decl of the given CompilerDecl.
Definition ClangUtil.cpp:31
static bool IsClangType(const CompilerType &ct)
Definition ClangUtil.cpp:17
Options used by Module::FindFunctions.
Definition Module.h:66
bool include_inlines
Include inlined functions.
Definition Module.h:70
bool include_symbols
Include the symbol table.
Definition Module.h:68
clang::NamedDecl * AddTypeDecl(const CompilerType &compiler_type)
Create a TypeDecl with the name being searched for and the provided type and register it in the right...
const clang::DeclarationName m_decl_name
The name being looked for.
llvm::SmallVectorImpl< clang::NamedDecl * > & m_decls
The list of declarations already constructed.
const clang::DeclContext * m_decl_context
The DeclContext to put declarations into.
void AddNamedDecl(clang::NamedDecl *decl)
Add a NamedDecl to the list of results.
ClangASTImporter::NamespaceMapSP m_namespace_map
The mapping of all namespaces found for this request back to their modules.