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.Format("-[{0} {1}]", interface_name, selector_name);
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.Format("+[{0} {1}]", interface_name, selector_name);
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 =
974 candidate_sc.function->GetName().AsCString(nullptr);
975
976 const char *cursor = candidate_name;
977
978 if (*cursor != '+' && *cursor != '-')
979 continue;
980
981 ++cursor;
982
983 if (*cursor != '[')
984 continue;
985
986 ++cursor;
987
988 size_t interface_len = interface_name.length();
989
990 if (strncmp(cursor, interface_name.c_str(), interface_len))
991 continue;
992
993 cursor += interface_len;
994
995 if (*cursor == ' ' || *cursor == '(')
996 sc_list.Append(candidate_sc);
997 }
998 } while (false);
999
1000 if (sc_list.GetSize()) {
1001 // We found a good function symbol. Use that.
1002
1003 for (const SymbolContext &sc : sc_list) {
1004 if (!sc.function)
1005 continue;
1006
1007 CompilerDeclContext function_decl_ctx = sc.function->GetDeclContext();
1008 if (!function_decl_ctx)
1009 continue;
1010
1011 ObjCMethodDecl *method_decl =
1013
1014 if (!method_decl)
1015 continue;
1016
1017 ObjCInterfaceDecl *found_interface_decl =
1018 method_decl->getClassInterface();
1019
1020 if (!found_interface_decl)
1021 continue;
1022
1023 if (found_interface_decl->getName() == interface_decl->getName()) {
1024 Decl *copied_decl = CopyDecl(method_decl);
1025
1026 if (!copied_decl)
1027 continue;
1028
1029 ObjCMethodDecl *copied_method_decl =
1030 dyn_cast<ObjCMethodDecl>(copied_decl);
1031
1032 if (!copied_method_decl)
1033 continue;
1034
1035 LLDB_LOG(log, " CAS::FOMD found (in symbols)\n{0}",
1036 ClangUtil::DumpDecl(copied_method_decl));
1037
1038 context.AddNamedDecl(copied_method_decl);
1039 }
1040 }
1041
1042 return;
1043 }
1044
1045 // Try the debug information.
1046
1047 do {
1048 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(
1049 const_cast<ObjCInterfaceDecl *>(interface_decl));
1050
1051 if (!complete_interface_decl)
1052 break;
1053
1054 // We found the complete interface. The runtime never needs to be queried
1055 // in this scenario.
1056
1057 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(
1058 complete_interface_decl);
1059
1060 if (complete_interface_decl == interface_decl)
1061 break; // already checked this one
1062
1063 LLDB_LOG(log,
1064 "CAS::FOPD trying origin "
1065 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",
1066 complete_interface_decl, &complete_iface_decl->getASTContext());
1067
1068 FindObjCMethodDeclsWithOrigin(context, complete_interface_decl,
1069 "in debug info");
1070
1071 return;
1072 } while (false);
1073
1074 do {
1075 // Check the modules only if the debug information didn't have a complete
1076 // interface.
1077
1078 if (std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1080 ConstString interface_name(interface_decl->getNameAsString().c_str());
1081 bool append = false;
1082 uint32_t max_matches = 1;
1083 std::vector<CompilerDecl> decls;
1084
1085 if (!modules_decl_vendor->FindDecls(interface_name, append, max_matches,
1086 decls))
1087 break;
1088
1089 ObjCInterfaceDecl *interface_decl_from_modules =
1090 dyn_cast<ObjCInterfaceDecl>(ClangUtil::GetDecl(decls[0]));
1091
1092 if (!interface_decl_from_modules)
1093 break;
1094
1095 if (FindObjCMethodDeclsWithOrigin(context, interface_decl_from_modules,
1096 "in modules"))
1097 return;
1098 }
1099 } while (false);
1100
1101 do {
1102 // Check the runtime only if the debug information didn't have a complete
1103 // interface and the modules don't get us anywhere.
1104
1105 lldb::ProcessSP process(m_target->GetProcessSP());
1106
1107 if (!process)
1108 break;
1109
1110 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
1111
1112 if (!language_runtime)
1113 break;
1114
1115 DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
1116
1117 if (!decl_vendor)
1118 break;
1119
1120 ConstString interface_name(interface_decl->getNameAsString().c_str());
1121 bool append = false;
1122 uint32_t max_matches = 1;
1123 std::vector<CompilerDecl> decls;
1124
1125 auto *clang_decl_vendor = llvm::cast<DeclVendor>(decl_vendor);
1126 if (!clang_decl_vendor->FindDecls(interface_name, append, max_matches,
1127 decls))
1128 break;
1129
1130 ObjCInterfaceDecl *runtime_interface_decl =
1131 dyn_cast<ObjCInterfaceDecl>(ClangUtil::GetDecl(decls[0]));
1132
1133 if (!runtime_interface_decl)
1134 break;
1135
1136 FindObjCMethodDeclsWithOrigin(context, runtime_interface_decl,
1137 "in runtime");
1138 } while (false);
1139}
1140
1142 NameSearchContext &context,
1143 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl) {
1145
1146 if (origin_iface_decl.IsInvalid())
1147 return false;
1148
1149 std::string name_str = context.m_decl_name.getAsString();
1150 StringRef name(name_str);
1151 IdentifierInfo &name_identifier(
1152 origin_iface_decl->getASTContext().Idents.get(name));
1153
1154 DeclFromUser<ObjCPropertyDecl> origin_property_decl(
1155 origin_iface_decl->FindPropertyDeclaration(
1156 &name_identifier, ObjCPropertyQueryKind::OBJC_PR_query_instance));
1157
1158 bool found = false;
1159
1160 if (origin_property_decl.IsValid()) {
1161 DeclFromParser<ObjCPropertyDecl> parser_property_decl(
1162 origin_property_decl.Import(m_ast_context, *m_ast_importer_sp));
1163 if (parser_property_decl.IsValid()) {
1164 LLDB_LOG(log, " CAS::FOPD found\n{0}",
1165 ClangUtil::DumpDecl(parser_property_decl.decl));
1166
1167 context.AddNamedDecl(parser_property_decl.decl);
1168 found = true;
1169 }
1170 }
1171
1172 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(
1173 origin_iface_decl->getIvarDecl(&name_identifier));
1174
1175 if (origin_ivar_decl.IsValid()) {
1176 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(
1177 origin_ivar_decl.Import(m_ast_context, *m_ast_importer_sp));
1178 if (parser_ivar_decl.IsValid()) {
1179 LLDB_LOG(log, " CAS::FOPD found\n{0}",
1180 ClangUtil::DumpDecl(parser_ivar_decl.decl));
1181
1182 context.AddNamedDecl(parser_ivar_decl.decl);
1183 found = true;
1184 }
1185 }
1186
1187 return found;
1188}
1189
1192
1194 cast<ObjCInterfaceDecl>(context.m_decl_context));
1195 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(
1196 parser_iface_decl.GetOrigin(*m_ast_importer_sp));
1197
1198 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
1199
1200 LLDB_LOG(log,
1201 "ClangASTSource::FindObjCPropertyAndIvarDecls on "
1202 "(ASTContext*){0:x} '{1}' for '{2}.{3}'",
1203 m_ast_context, m_clang_ast_context->getDisplayName(),
1204 parser_iface_decl->getName(), context.m_decl_name.getAsString());
1205
1206 if (FindObjCPropertyAndIvarDeclsWithOrigin(context, origin_iface_decl))
1207 return;
1208
1209 LLDB_LOG(log,
1210 "CAS::FOPD couldn't find the property on origin "
1211 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}, searching "
1212 "elsewhere...",
1213 origin_iface_decl.decl, &origin_iface_decl->getASTContext());
1214
1215 SymbolContext null_sc;
1216 TypeList type_list;
1217
1218 do {
1219 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(
1220 const_cast<ObjCInterfaceDecl *>(parser_iface_decl.decl));
1221
1222 if (!complete_interface_decl)
1223 break;
1224
1225 // We found the complete interface. The runtime never needs to be queried
1226 // in this scenario.
1227
1228 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(
1229 complete_interface_decl);
1230
1231 if (complete_iface_decl.decl == origin_iface_decl.decl)
1232 break; // already checked this one
1233
1234 LLDB_LOG(log,
1235 "CAS::FOPD trying origin "
1236 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",
1237 complete_iface_decl.decl, &complete_iface_decl->getASTContext());
1238
1239 FindObjCPropertyAndIvarDeclsWithOrigin(context, complete_iface_decl);
1240
1241 return;
1242 } while (false);
1243
1244 do {
1245 // Check the modules only if the debug information didn't have a complete
1246 // interface.
1247
1248 std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1250
1251 if (!modules_decl_vendor)
1252 break;
1253
1254 bool append = false;
1255 uint32_t max_matches = 1;
1256 std::vector<CompilerDecl> decls;
1257
1258 if (!modules_decl_vendor->FindDecls(class_name, append, max_matches, decls))
1259 break;
1260
1261 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_modules(
1262 dyn_cast<ObjCInterfaceDecl>(ClangUtil::GetDecl(decls[0])));
1263
1264 if (!interface_decl_from_modules.IsValid())
1265 break;
1266
1267 LLDB_LOG(log,
1268 "CAS::FOPD[{0:x}] trying module "
1269 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",
1270 interface_decl_from_modules.decl,
1271 &interface_decl_from_modules->getASTContext());
1272
1274 interface_decl_from_modules))
1275 return;
1276 } while (false);
1277
1278 do {
1279 // Check the runtime only if the debug information didn't have a complete
1280 // interface and nothing was in the modules.
1281
1282 lldb::ProcessSP process(m_target->GetProcessSP());
1283
1284 if (!process)
1285 return;
1286
1287 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
1288
1289 if (!language_runtime)
1290 return;
1291
1292 DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
1293
1294 if (!decl_vendor)
1295 break;
1296
1297 bool append = false;
1298 uint32_t max_matches = 1;
1299 std::vector<CompilerDecl> decls;
1300
1301 auto *clang_decl_vendor = llvm::cast<DeclVendor>(decl_vendor);
1302 if (!clang_decl_vendor->FindDecls(class_name, append, max_matches, decls))
1303 break;
1304
1305 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_runtime(
1306 dyn_cast<ObjCInterfaceDecl>(ClangUtil::GetDecl(decls[0])));
1307
1308 if (!interface_decl_from_runtime.IsValid())
1309 break;
1310
1311 LLDB_LOG(log,
1312 "CAS::FOPD[{0:x}] trying runtime "
1313 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",
1314 interface_decl_from_runtime.decl,
1315 &interface_decl_from_runtime->getASTContext());
1316
1318 interface_decl_from_runtime))
1319 return;
1320 } while (false);
1321}
1322
1324 const NamespaceDecl *namespace_context =
1325 dyn_cast<NamespaceDecl>(context.m_decl_context);
1326
1328
1329 ClangASTImporter::NamespaceMapSP namespace_map =
1330 m_ast_importer_sp->GetNamespaceMap(namespace_context);
1331
1332 LLDB_LOG_VERBOSE(log,
1333 " CAS::FEVD Inspecting namespace map {0:x} ({1} entries)",
1334 namespace_map.get(), namespace_map->size());
1335
1336 if (!namespace_map)
1337 return;
1338
1339 for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(),
1340 e = namespace_map->end();
1341 i != e; ++i) {
1342 LLDB_LOG(log, " CAS::FEVD Searching namespace {0} in module {1}",
1343 i->second.GetName(), i->first->GetFileSpec().GetFilename());
1344
1345 FindExternalVisibleDecls(context, i->first, i->second);
1346 }
1347}
1348
1350 const RecordDecl *record, uint64_t &size, uint64_t &alignment,
1351 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
1352 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
1353 &base_offsets,
1354 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
1355 &virtual_base_offsets) {
1356 return m_ast_importer_sp->importRecordLayoutFromOrigin(
1357 record, size, alignment, field_offsets, base_offsets,
1358 virtual_base_offsets);
1359}
1360
1362 ClangASTImporter::NamespaceMapSP &namespace_map, ConstString name,
1363 ClangASTImporter::NamespaceMapSP &parent_map) const {
1364
1366
1367 if (log) {
1368 if (parent_map && parent_map->size())
1369 LLDB_LOG(log,
1370 "CompleteNamespaceMap on (ASTContext*){0:x} '{1}' Searching "
1371 "for namespace {2} in namespace {3}",
1372 m_ast_context, m_clang_ast_context->getDisplayName(), name,
1373 parent_map->begin()->second.GetName());
1374 else
1375 LLDB_LOG(log,
1376 "CompleteNamespaceMap on (ASTContext*){0} '{1}' Searching "
1377 "for namespace {2}",
1378 m_ast_context, m_clang_ast_context->getDisplayName(), name);
1379 }
1380
1381 if (parent_map) {
1382 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(),
1383 e = parent_map->end();
1384 i != e; ++i) {
1385 CompilerDeclContext found_namespace_decl;
1386
1387 lldb::ModuleSP module_sp = i->first;
1388 CompilerDeclContext module_parent_namespace_decl = i->second;
1389
1390 SymbolFile *symbol_file = module_sp->GetSymbolFile();
1391
1392 if (!symbol_file)
1393 continue;
1394
1395 found_namespace_decl =
1396 symbol_file->FindNamespace(name, module_parent_namespace_decl);
1397
1398 if (!found_namespace_decl)
1399 continue;
1400
1401 namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(
1402 module_sp, found_namespace_decl));
1403
1404 LLDB_LOG(log, " CMN Found namespace {0} in module {1}", name,
1405 module_sp->GetFileSpec().GetFilename());
1406 }
1407 } else {
1408 CompilerDeclContext null_namespace_decl;
1409 for (lldb::ModuleSP image : m_target->GetImages().Modules()) {
1410 if (!image)
1411 continue;
1412
1413 CompilerDeclContext found_namespace_decl;
1414
1415 SymbolFile *symbol_file = image->GetSymbolFile();
1416
1417 if (!symbol_file)
1418 continue;
1419
1420 found_namespace_decl =
1421 symbol_file->FindNamespace(name, null_namespace_decl);
1422
1423 if (!found_namespace_decl)
1424 continue;
1425
1426 namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(
1427 image, found_namespace_decl));
1428
1429 LLDB_LOG(log, " CMN[{0}] Found namespace {0} in module {1}", name,
1430 image->GetFileSpec().GetFilename());
1431 }
1432 }
1433}
1434
1436 if (!context.m_namespace_map)
1437 return nullptr;
1438
1439 const CompilerDeclContext &namespace_decl =
1440 context.m_namespace_map->begin()->second;
1441
1442 clang::ASTContext *src_ast =
1444 if (!src_ast)
1445 return nullptr;
1446 clang::NamespaceDecl *src_namespace_decl =
1448
1449 if (!src_namespace_decl)
1450 return nullptr;
1451
1452 Decl *copied_decl = CopyDecl(src_namespace_decl);
1453
1454 if (!copied_decl)
1455 return nullptr;
1456
1457 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1458
1459 if (!copied_namespace_decl)
1460 return nullptr;
1461
1462 context.m_decls.push_back(copied_namespace_decl);
1463
1464 m_ast_importer_sp->RegisterNamespaceMap(copied_namespace_decl,
1465 context.m_namespace_map);
1466
1467 return dyn_cast<NamespaceDecl>(copied_decl);
1468}
1469
1470clang::Decl *ClangASTSource::CopyDecl(Decl *src_decl) {
1471 return m_ast_importer_sp->CopyDecl(m_ast_context, src_decl);
1472}
1473
1475 return m_ast_importer_sp->GetDeclOrigin(decl);
1476}
1477
1479 auto src_ast = src_type.GetTypeSystem<TypeSystemClang>();
1480 if (!src_ast)
1481 return {};
1482
1483 QualType copied_qual_type = ClangUtil::GetQualType(
1484 m_ast_importer_sp->CopyType(*m_clang_ast_context, src_type));
1485
1486 if (copied_qual_type.getAsOpaquePtr() &&
1487 copied_qual_type->getCanonicalTypeInternal().isNull())
1488 // this shouldn't happen, but we're hardening because the AST importer
1489 // seems to be generating bad types on occasion.
1490 return {};
1491
1492 return m_clang_ast_context->GetType(copied_qual_type);
1493}
1494
1495std::shared_ptr<ClangModulesDeclVendor>
1497 auto persistent_vars = llvm::cast<ClangPersistentVariables>(
1498 m_target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
1499 return persistent_vars->GetClangModulesDeclVendor();
1500}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:364
#define LLDB_LOG_VERBOSE(log,...)
Definition Log.h:371
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
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
void Format(const char *format, Args &&... args)
Forwards the arguments to llvm::formatv and writes to the stream.
Definition Stream.h:367
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:132
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:48
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:327
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.