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
196 LLDB_LOGV(log, " CTD Inspecting namespace map{0:x} ({1} entries)",
197 namespace_map.get(), namespace_map->size());
198
199 for (const ClangASTImporter::NamespaceMapItem &item : *namespace_map) {
200 LLDB_LOG(log, " CTD Searching namespace {0} in module {1}",
201 item.second.GetName(), item.first->GetFileSpec().GetFilename());
202
203 ConstString name(decl->getName());
204
205 // Create a type matcher using the CompilerDeclContext for the namespace
206 // as the context (item.second) and search for the name inside of this
207 // context.
208 TypeQuery query(item.second, name);
209 TypeResults results;
210 item.first->FindTypes(query, results);
211
212 for (const lldb::TypeSP &type_sp : results.GetTypeMap().Types()) {
213 CompilerType clang_type(type_sp->GetFullCompilerType());
214
215 if (!ClangUtil::IsClangType(clang_type))
216 continue;
217
218 const TagType *tag_type =
219 ClangUtil::GetQualType(clang_type)->getAs<TagType>();
220
221 if (!tag_type)
222 continue;
223
224 TagDecl *candidate_tag_decl =
225 tag_type->getDecl()->getDefinitionOrSelf();
226
228 &candidate_tag_decl->getASTContext(), candidate_tag_decl))
229 return candidate_tag_decl;
230 }
231 }
232 } else {
233 const ModuleList &module_list = m_target->GetImages();
234 // Create a type matcher using a CompilerDecl. Each TypeSystem class knows
235 // how to fill out a CompilerContext array using a CompilerDecl.
236 TypeQuery query(CompilerDecl(m_clang_ast_context, (void *)decl));
237 TypeResults results;
238 module_list.FindTypes(nullptr, query, results);
239 for (const lldb::TypeSP &type_sp : results.GetTypeMap().Types()) {
240
241 CompilerType clang_type(type_sp->GetFullCompilerType());
242
243 if (!ClangUtil::IsClangType(clang_type))
244 continue;
245
246 const TagType *tag_type =
247 ClangUtil::GetQualType(clang_type)->getAs<TagType>();
248
249 if (!tag_type)
250 continue;
251
252 TagDecl *candidate_tag_decl = tag_type->getDecl()->getDefinitionOrSelf();
253
254 if (TypeSystemClang::GetCompleteDecl(&candidate_tag_decl->getASTContext(),
255 candidate_tag_decl))
256 return candidate_tag_decl;
257 }
258 }
259 return nullptr;
260}
261
262void ClangASTSource::CompleteType(TagDecl *tag_decl) {
264
265 if (log) {
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
275 auto iter = m_active_lexical_decls.find(tag_decl);
276 if (iter != m_active_lexical_decls.end())
277 return;
278 m_active_lexical_decls.insert(tag_decl);
279 ScopedLexicalDeclEraser eraser(m_active_lexical_decls, tag_decl);
280
281 if (!m_ast_importer_sp->CompleteTagDecl(tag_decl)) {
282 // We couldn't complete the type. Maybe there's a definition somewhere
283 // else that can be completed.
284 if (TagDecl *alternate = FindCompleteType(tag_decl))
285 m_ast_importer_sp->CompleteTagDeclWithOrigin(tag_decl, alternate);
286 }
287
288 LLDB_LOG(log, " [CTD] After:\n{0}", ClangUtil::DumpDecl(tag_decl));
289}
290
291void ClangASTSource::CompleteType(clang::ObjCInterfaceDecl *interface_decl) {
293
294 LLDB_LOG(log,
295 " [CompleteObjCInterfaceDecl] on (ASTContext*){0:x} '{1}' "
296 "Completing an ObjCInterfaceDecl named {2}",
297 m_ast_context, m_clang_ast_context->getDisplayName(),
298 interface_decl->getName());
299 LLDB_LOG(log, " [COID] Before:\n{0}",
300 ClangUtil::DumpDecl(interface_decl));
301
302 ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl);
303
304 if (original.Valid()) {
305 if (ObjCInterfaceDecl *original_iface_decl =
306 dyn_cast<ObjCInterfaceDecl>(original.decl)) {
307 ObjCInterfaceDecl *complete_iface_decl =
308 GetCompleteObjCInterface(original_iface_decl);
309
310 if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) {
311 m_ast_importer_sp->SetDeclOrigin(interface_decl, complete_iface_decl);
312 }
313 }
314 }
315
316 m_ast_importer_sp->CompleteObjCInterfaceDecl(interface_decl);
317
318 if (interface_decl->getSuperClass() &&
319 interface_decl->getSuperClass() != interface_decl)
320 CompleteType(interface_decl->getSuperClass());
321
322 LLDB_LOG(log, " [COID] After:");
323 LLDB_LOG(log, " [COID] {0}", ClangUtil::DumpDecl(interface_decl));
324}
325
327 const clang::ObjCInterfaceDecl *interface_decl) {
328 lldb::ProcessSP process(m_target->GetProcessSP());
329
330 if (!process)
331 return nullptr;
332
333 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
334
335 if (!language_runtime)
336 return nullptr;
337
338 ConstString class_name(interface_decl->getNameAsString().c_str());
339
340 lldb::TypeSP complete_type_sp(
341 language_runtime->LookupInCompleteClassCache(class_name));
342
343 if (!complete_type_sp)
344 return nullptr;
345
346 TypeFromUser complete_type =
347 TypeFromUser(complete_type_sp->GetFullCompilerType());
348 lldb::opaque_compiler_type_t complete_opaque_type =
349 complete_type.GetOpaqueQualType();
350
351 if (!complete_opaque_type)
352 return nullptr;
353
354 const clang::Type *complete_clang_type =
355 QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
356 const ObjCInterfaceType *complete_interface_type =
357 dyn_cast<ObjCInterfaceType>(complete_clang_type);
358
359 if (!complete_interface_type)
360 return nullptr;
361
362 ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());
363
364 return complete_iface_decl;
365}
366
368 const DeclContext *decl_context,
369 llvm::function_ref<bool(Decl::Kind)> predicate,
371
373
374 const Decl *context_decl = dyn_cast<Decl>(decl_context);
375
376 if (!context_decl)
377 return;
378
379 auto iter = m_active_lexical_decls.find(context_decl);
380 if (iter != m_active_lexical_decls.end())
381 return;
382 m_active_lexical_decls.insert(context_decl);
383 ScopedLexicalDeclEraser eraser(m_active_lexical_decls, context_decl);
384
385 if (log) {
386 if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
387 LLDB_LOG(log,
388 "FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in "
389 "'{2}' ({3}Decl*){4}",
390 m_ast_context, m_clang_ast_context->getDisplayName(),
391 context_named_decl->getNameAsString().c_str(),
392 context_decl->getDeclKindName(),
393 static_cast<const void *>(context_decl));
394 else if (context_decl)
395 LLDB_LOG(log,
396 "FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in "
397 "({2}Decl*){3}",
398 m_ast_context, m_clang_ast_context->getDisplayName(),
399 context_decl->getDeclKindName(),
400 static_cast<const void *>(context_decl));
401 else
402 LLDB_LOG(log,
403 "FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in a "
404 "NULL context",
405 m_ast_context, m_clang_ast_context->getDisplayName());
406 }
407
408 ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(context_decl);
409
410 if (!original.Valid())
411 return;
412
413 LLDB_LOG(log, " FELD Original decl (ASTContext*){0:x} (Decl*){1:x}:\n{2}",
414 static_cast<void *>(original.ctx),
415 static_cast<void *>(original.decl),
416 ClangUtil::DumpDecl(original.decl));
417
418 if (ObjCInterfaceDecl *original_iface_decl =
419 dyn_cast<ObjCInterfaceDecl>(original.decl)) {
420 ObjCInterfaceDecl *complete_iface_decl =
421 GetCompleteObjCInterface(original_iface_decl);
422
423 if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) {
424 original.decl = complete_iface_decl;
425 original.ctx = &complete_iface_decl->getASTContext();
426
427 m_ast_importer_sp->SetDeclOrigin(context_decl, complete_iface_decl);
428 }
429 }
430
431 if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original.decl)) {
432 ExternalASTSource *external_source = original.ctx->getExternalSource();
433
434 if (external_source)
435 external_source->CompleteType(original_tag_decl);
436 }
437
438 const DeclContext *original_decl_context =
439 dyn_cast<DeclContext>(original.decl);
440
441 if (!original_decl_context)
442 return;
443
444 // Indicates whether we skipped any Decls of the original DeclContext.
445 bool SkippedDecls = false;
446 for (Decl *decl : original_decl_context->decls()) {
447 // The predicate function returns true if the passed declaration kind is
448 // the one we are looking for.
449 // See clang::ExternalASTSource::FindExternalLexicalDecls()
450 if (predicate(decl->getKind())) {
451 if (log) {
452 std::string ast_dump = ClangUtil::DumpDecl(decl);
453 if (const NamedDecl *context_named_decl =
454 dyn_cast<NamedDecl>(context_decl))
455 LLDB_LOG(log, " FELD Adding [to {0}Decl {1}] lexical {2}Decl {3}",
456 context_named_decl->getDeclKindName(),
457 context_named_decl->getName(), decl->getDeclKindName(),
458 ast_dump);
459 else
460 LLDB_LOG(log, " FELD Adding lexical {0}Decl {1}",
461 decl->getDeclKindName(), ast_dump);
462 }
463
464 Decl *copied_decl = CopyDecl(decl);
465
466 if (!copied_decl)
467 continue;
468
469 // FIXME: We should add the copied decl to the 'decls' list. This would
470 // add the copied Decl into the DeclContext and make sure that we
471 // correctly propagate that we added some Decls back to Clang.
472 // By leaving 'decls' empty we incorrectly return false from
473 // DeclContext::LoadLexicalDeclsFromExternalStorage which might cause
474 // lookup issues later on.
475 // We can't just add them for now as the ASTImporter already added the
476 // decl into the DeclContext and this would add it twice.
477
478 if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl)) {
479 QualType copied_field_type = copied_field->getType();
480
481 m_ast_importer_sp->RequireCompleteType(copied_field_type);
482 }
483 } else {
484 SkippedDecls = true;
485 }
486 }
487
488 // CopyDecl may build a lookup table which may set up ExternalLexicalStorage
489 // to false. However, since we skipped some of the external Decls we must
490 // set it back!
491 if (SkippedDecls) {
492 decl_context->setHasExternalLexicalStorage(true);
493 // This sets HasLazyExternalLexicalLookups to true. By setting this bit we
494 // ensure that the lookup table is rebuilt, which means the external source
495 // is consulted again when a clang::DeclContext::lookup is called.
496 const_cast<DeclContext *>(decl_context)->setMustBuildLookupTable();
497 }
498}
499
501 assert(m_ast_context);
502
503 const ConstString name(context.m_decl_name.getAsString().c_str());
504
506
507 if (log) {
508 if (!context.m_decl_context)
509 LLDB_LOG(log,
510 "ClangASTSource::FindExternalVisibleDecls on "
511 "(ASTContext*){0:x} '{1}' for '{2}' in a NULL DeclContext",
512 m_ast_context, m_clang_ast_context->getDisplayName(), name);
513 else if (const NamedDecl *context_named_decl =
514 dyn_cast<NamedDecl>(context.m_decl_context))
515 LLDB_LOG(log,
516 "ClangASTSource::FindExternalVisibleDecls on "
517 "(ASTContext*){0:x} '{1}' for '{2}' in '{3}'",
518 m_ast_context, m_clang_ast_context->getDisplayName(), name,
519 context_named_decl->getName());
520 else
521 LLDB_LOG(log,
522 "ClangASTSource::FindExternalVisibleDecls on "
523 "(ASTContext*){0:x} '{1}' for '{2}' in a '{3}'",
524 m_ast_context, m_clang_ast_context->getDisplayName(), name,
525 context.m_decl_context->getDeclKindName());
526 }
527
528 if (isa<NamespaceDecl>(context.m_decl_context)) {
529 LookupInNamespace(context);
530 } else if (isa<ObjCInterfaceDecl>(context.m_decl_context)) {
532 } else if (!isa<TranslationUnitDecl>(context.m_decl_context)) {
533 // we shouldn't be getting FindExternalVisibleDecls calls for these
534 return;
535 } else {
536 CompilerDeclContext namespace_decl;
537
538 LLDB_LOG(log, " CAS::FEVD Searching the root namespace");
539
540 FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl);
541 }
542
543 if (!context.m_namespace_map->empty()) {
544 if (log && log->GetVerbose())
545 LLDB_LOG(log, " CAS::FEVD Registering namespace map {0:x} ({1} entries)",
546 context.m_namespace_map.get(), context.m_namespace_map->size());
547
548 NamespaceDecl *clang_namespace_decl =
549 AddNamespace(context, context.m_namespace_map);
550
551 if (clang_namespace_decl)
552 clang_namespace_decl->setHasExternalVisibleStorage();
553 }
554}
555
557 return m_clang_ast_context->getSema();
558}
559
561 bool ignore_all_dollar_names) {
562 static const ConstString id_name("id");
563 static const ConstString Class_name("Class");
564
565 if (m_ast_context->getLangOpts().ObjC)
566 if (name == id_name || name == Class_name)
567 return true;
568
569 StringRef name_string_ref = name.GetStringRef();
570
571 // The ClangASTSource is not responsible for finding $-names.
572 return name_string_ref.empty() ||
573 (ignore_all_dollar_names && name_string_ref.starts_with("$")) ||
574 name_string_ref.starts_with("_$");
575}
576
578 NameSearchContext &context, lldb::ModuleSP module_sp,
579 CompilerDeclContext &namespace_decl) {
580 assert(m_ast_context);
581
583
584 SymbolContextList sc_list;
585
586 const ConstString name(context.m_decl_name.getAsString().c_str());
587 if (IgnoreName(name, true))
588 return;
589
590 if (!m_target)
591 return;
592
593 FillNamespaceMap(context, module_sp, namespace_decl);
594
595 if (context.m_found_type)
596 return;
597
598 lldb::TypeSP type_sp;
599 TypeResults results;
600 if (module_sp && namespace_decl) {
601 // Match the name in the specified decl context.
602 TypeQuery query(namespace_decl, name, TypeQueryOptions::e_find_one);
603 module_sp->FindTypes(query, results);
604 type_sp = results.GetFirstType();
605 } else {
606 // Match the exact name of the type at the root level.
607 TypeQuery query(name.GetStringRef(), TypeQueryOptions::e_exact_match |
608 TypeQueryOptions::e_find_one);
609 m_target->GetImages().FindTypes(nullptr, query, results);
610 type_sp = results.GetFirstType();
611 }
612
613 if (type_sp) {
614 if (log) {
615 const char *name_string = type_sp->GetName().GetCString();
616
617 LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\": {1}", name,
618 (name_string ? name_string : "<anonymous>"));
619 }
620
621 CompilerType full_type = type_sp->GetFullCompilerType();
622
623 CompilerType copied_clang_type(GuardedCopyType(full_type));
624
625 if (!copied_clang_type) {
626 LLDB_LOG(log, " CAS::FEVD - Couldn't export a type");
627 } else {
628
629 context.AddTypeDecl(copied_clang_type);
630
631 context.m_found_type = true;
632 }
633 }
634
635 if (!context.m_found_type) {
636 // Try the modules next.
637 FindDeclInModules(context, name);
638 }
639
640 if (!context.m_found_type && m_ast_context->getLangOpts().ObjC) {
641 FindDeclInObjCRuntime(context, name);
642 }
643}
644
646 NameSearchContext &context, lldb::ModuleSP module_sp,
647 const CompilerDeclContext &namespace_decl) {
648 const ConstString name(context.m_decl_name.getAsString().c_str());
649 if (IgnoreName(name, true))
650 return;
651
653
654 if (module_sp && namespace_decl) {
655 CompilerDeclContext found_namespace_decl;
656
657 if (SymbolFile *symbol_file = module_sp->GetSymbolFile()) {
658 found_namespace_decl = symbol_file->FindNamespace(name, namespace_decl);
659
660 if (found_namespace_decl) {
661 context.m_namespace_map->push_back(
662 std::pair<lldb::ModuleSP, CompilerDeclContext>(
663 module_sp, found_namespace_decl));
664
665 LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}", name,
666 module_sp->GetFileSpec().GetFilename());
667 }
668 }
669 return;
670 }
671
672 for (lldb::ModuleSP image : m_target->GetImages().Modules()) {
673 if (!image)
674 continue;
675
676 CompilerDeclContext found_namespace_decl;
677
678 SymbolFile *symbol_file = image->GetSymbolFile();
679
680 if (!symbol_file)
681 continue;
682
683 // If namespace_decl is not valid, 'FindNamespace' would look for
684 // any namespace called 'name' (ignoring parent contexts) and return
685 // the first one it finds. Thus if we're doing a qualified lookup only
686 // consider root namespaces. E.g., in an expression ::A::B::Foo, the
687 // lookup of ::A will result in a qualified lookup. Note, namespace
688 // disambiguation for function calls are handled separately in
689 // SearchFunctionsInSymbolContexts.
690 const bool find_root_namespaces =
691 context.m_decl_context &&
692 context.m_decl_context->shouldUseQualifiedLookup();
693 found_namespace_decl = symbol_file->FindNamespace(
694 name, namespace_decl, /* only root namespaces */ find_root_namespaces);
695
696 if (found_namespace_decl) {
697 context.m_namespace_map->push_back(
698 std::pair<lldb::ModuleSP, CompilerDeclContext>(image,
699 found_namespace_decl));
700
701 LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}", name,
702 image->GetFileSpec().GetFilename());
703 }
704 }
705}
706
708 NameSearchContext &context, ObjCInterfaceDecl *original_interface_decl,
709 const char *log_info) {
710 const DeclarationName &decl_name(context.m_decl_name);
711 clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
712
713 Selector original_selector;
714
715 if (decl_name.isObjCZeroArgSelector()) {
716 const IdentifierInfo *ident =
717 &original_ctx->Idents.get(decl_name.getAsString());
718 original_selector = original_ctx->Selectors.getSelector(0, &ident);
719 } else if (decl_name.isObjCOneArgSelector()) {
720 const std::string &decl_name_string = decl_name.getAsString();
721 std::string decl_name_string_without_colon(decl_name_string.c_str(),
722 decl_name_string.length() - 1);
723 const IdentifierInfo *ident =
724 &original_ctx->Idents.get(decl_name_string_without_colon);
725 original_selector = original_ctx->Selectors.getSelector(1, &ident);
726 } else {
727 SmallVector<const IdentifierInfo *, 4> idents;
728
729 clang::Selector sel = decl_name.getObjCSelector();
730
731 unsigned num_args = sel.getNumArgs();
732
733 for (unsigned i = 0; i != num_args; ++i) {
734 idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
735 }
736
737 original_selector =
738 original_ctx->Selectors.getSelector(num_args, idents.data());
739 }
740
741 DeclarationName original_decl_name(original_selector);
742
743 llvm::SmallVector<NamedDecl *, 1> methods;
744
745 TypeSystemClang::GetCompleteDecl(original_ctx, original_interface_decl);
746
747 if (ObjCMethodDecl *instance_method_decl =
748 original_interface_decl->lookupInstanceMethod(original_selector)) {
749 methods.push_back(instance_method_decl);
750 } else if (ObjCMethodDecl *class_method_decl =
751 original_interface_decl->lookupClassMethod(
752 original_selector)) {
753 methods.push_back(class_method_decl);
754 }
755
756 if (methods.empty()) {
757 return false;
758 }
759
760 for (NamedDecl *named_decl : methods) {
761 if (!named_decl)
762 continue;
763
764 ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl);
765
766 if (!result_method)
767 continue;
768
769 Decl *copied_decl = CopyDecl(result_method);
770
771 if (!copied_decl)
772 continue;
773
774 ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
775
776 if (!copied_method_decl)
777 continue;
778
780
781 LLDB_LOG(log, " CAS::FOMD found ({0}) {1}", log_info,
782 ClangUtil::DumpDecl(copied_method_decl));
783
784 context.AddNamedDecl(copied_method_decl);
785 }
786
787 return true;
788}
789
791 ConstString name) {
793
794 std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
796 if (!modules_decl_vendor)
797 return;
798
799 bool append = false;
800 uint32_t max_matches = 1;
801 std::vector<CompilerDecl> decls;
802
803 if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
804 return;
805
806 LLDB_LOG(log, " CAS::FEVD Matching entity found for \"{0}\" in the modules",
807 name);
808
809 auto *const decl_from_modules =
810 llvm::cast<NamedDecl>(ClangUtil::GetDecl(decls[0]));
811
812 if (llvm::isa<clang::TypeDecl>(decl_from_modules) ||
813 llvm::isa<clang::ObjCContainerDecl>(decl_from_modules) ||
814 llvm::isa<clang::EnumConstantDecl>(decl_from_modules)) {
815 clang::Decl *copied_decl = CopyDecl(decl_from_modules);
816 clang::NamedDecl *copied_named_decl =
817 copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr;
818
819 if (!copied_named_decl) {
820 LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the modules");
821
822 return;
823 }
824
825 context.AddNamedDecl(copied_named_decl);
826
827 context.m_found_type = true;
828 }
829}
830
832 ConstString name) {
834
835 lldb::ProcessSP process(m_target->GetProcessSP());
836
837 if (!process)
838 return;
839
840 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
841
842 if (!language_runtime)
843 return;
844
845 DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
846
847 if (!decl_vendor)
848 return;
849
850 bool append = false;
851 uint32_t max_matches = 1;
852 std::vector<CompilerDecl> decls;
853
854 auto *clang_decl_vendor = llvm::cast<DeclVendor>(decl_vendor);
855 if (!clang_decl_vendor->FindDecls(name, append, max_matches, decls))
856 return;
857
858 LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\" in the runtime",
859 name);
860
861 clang::Decl *copied_decl = CopyDecl(ClangUtil::GetDecl(decls[0]));
862 clang::NamedDecl *copied_named_decl =
863 copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : nullptr;
864
865 if (!copied_named_decl) {
866 LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the runtime");
867
868 return;
869 }
870
871 context.AddNamedDecl(copied_named_decl);
872}
873
876
877 const DeclarationName &decl_name(context.m_decl_name);
878 const DeclContext *decl_ctx(context.m_decl_context);
879
880 const ObjCInterfaceDecl *interface_decl =
881 dyn_cast<ObjCInterfaceDecl>(decl_ctx);
882
883 if (!interface_decl)
884 return;
885
886 do {
887 ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl);
888
889 if (!original.Valid())
890 break;
891
892 ObjCInterfaceDecl *original_interface_decl =
893 dyn_cast<ObjCInterfaceDecl>(original.decl);
894
895 if (FindObjCMethodDeclsWithOrigin(context, original_interface_decl,
896 "at origin"))
897 return; // found it, no need to look any further
898 } while (false);
899
900 StreamString ss;
901
902 if (decl_name.isObjCZeroArgSelector()) {
903 ss.Printf("%s", decl_name.getAsString().c_str());
904 } else if (decl_name.isObjCOneArgSelector()) {
905 ss.Printf("%s", decl_name.getAsString().c_str());
906 } else {
907 clang::Selector sel = decl_name.getObjCSelector();
908
909 for (unsigned i = 0, e = sel.getNumArgs(); i != e; ++i) {
910 llvm::StringRef r = sel.getNameForSlot(i);
911 ss.Printf("%s:", r.str().c_str());
912 }
913 }
914 ss.Flush();
915
916 if (ss.GetString().contains("$__lldb"))
917 return; // we don't need any results
918
919 ConstString selector_name(ss.GetString());
920
921 LLDB_LOG(log,
922 "ClangASTSource::FindObjCMethodDecls on (ASTContext*){0:x} '{1}' "
923 "for selector [{2} {3}]",
924 m_ast_context, m_clang_ast_context->getDisplayName(),
925 interface_decl->getName(), selector_name);
926 SymbolContextList sc_list;
927
928 ModuleFunctionSearchOptions function_options;
929 function_options.include_symbols = false;
930 function_options.include_inlines = false;
931
932 std::string interface_name = interface_decl->getNameAsString();
933
934 do {
935 StreamString ms;
936 ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
937 ms.Flush();
938 ConstString instance_method_name(ms.GetString());
939
940 sc_list.Clear();
941 m_target->GetImages().FindFunctions(instance_method_name,
942 lldb::eFunctionNameTypeFull,
943 function_options, sc_list);
944
945 if (sc_list.GetSize())
946 break;
947
948 ms.Clear();
949 ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
950 ms.Flush();
951 ConstString class_method_name(ms.GetString());
952
953 sc_list.Clear();
954 m_target->GetImages().FindFunctions(class_method_name,
955 lldb::eFunctionNameTypeFull,
956 function_options, sc_list);
957
958 if (sc_list.GetSize())
959 break;
960
961 // Fall back and check for methods in categories. If we find methods this
962 // way, we need to check that they're actually in categories on the desired
963 // class.
964
965 SymbolContextList candidate_sc_list;
966
967 m_target->GetImages().FindFunctions(selector_name,
968 lldb::eFunctionNameTypeSelector,
969 function_options, candidate_sc_list);
970
971 for (const SymbolContext &candidate_sc : candidate_sc_list) {
972 if (!candidate_sc.function)
973 continue;
974
975 const char *candidate_name = candidate_sc.function->GetName().AsCString();
976
977 const char *cursor = candidate_name;
978
979 if (*cursor != '+' && *cursor != '-')
980 continue;
981
982 ++cursor;
983
984 if (*cursor != '[')
985 continue;
986
987 ++cursor;
988
989 size_t interface_len = interface_name.length();
990
991 if (strncmp(cursor, interface_name.c_str(), interface_len))
992 continue;
993
994 cursor += interface_len;
995
996 if (*cursor == ' ' || *cursor == '(')
997 sc_list.Append(candidate_sc);
998 }
999 } while (false);
1000
1001 if (sc_list.GetSize()) {
1002 // We found a good function symbol. Use that.
1003
1004 for (const SymbolContext &sc : sc_list) {
1005 if (!sc.function)
1006 continue;
1007
1008 CompilerDeclContext function_decl_ctx = sc.function->GetDeclContext();
1009 if (!function_decl_ctx)
1010 continue;
1011
1012 ObjCMethodDecl *method_decl =
1014
1015 if (!method_decl)
1016 continue;
1017
1018 ObjCInterfaceDecl *found_interface_decl =
1019 method_decl->getClassInterface();
1020
1021 if (!found_interface_decl)
1022 continue;
1023
1024 if (found_interface_decl->getName() == interface_decl->getName()) {
1025 Decl *copied_decl = CopyDecl(method_decl);
1026
1027 if (!copied_decl)
1028 continue;
1029
1030 ObjCMethodDecl *copied_method_decl =
1031 dyn_cast<ObjCMethodDecl>(copied_decl);
1032
1033 if (!copied_method_decl)
1034 continue;
1035
1036 LLDB_LOG(log, " CAS::FOMD found (in symbols)\n{0}",
1037 ClangUtil::DumpDecl(copied_method_decl));
1038
1039 context.AddNamedDecl(copied_method_decl);
1040 }
1041 }
1042
1043 return;
1044 }
1045
1046 // Try the debug information.
1047
1048 do {
1049 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(
1050 const_cast<ObjCInterfaceDecl *>(interface_decl));
1051
1052 if (!complete_interface_decl)
1053 break;
1054
1055 // We found the complete interface. The runtime never needs to be queried
1056 // in this scenario.
1057
1058 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(
1059 complete_interface_decl);
1060
1061 if (complete_interface_decl == interface_decl)
1062 break; // already checked this one
1063
1064 LLDB_LOG(log,
1065 "CAS::FOPD trying origin "
1066 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",
1067 complete_interface_decl, &complete_iface_decl->getASTContext());
1068
1069 FindObjCMethodDeclsWithOrigin(context, complete_interface_decl,
1070 "in debug info");
1071
1072 return;
1073 } while (false);
1074
1075 do {
1076 // Check the modules only if the debug information didn't have a complete
1077 // interface.
1078
1079 if (std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1081 ConstString interface_name(interface_decl->getNameAsString().c_str());
1082 bool append = false;
1083 uint32_t max_matches = 1;
1084 std::vector<CompilerDecl> decls;
1085
1086 if (!modules_decl_vendor->FindDecls(interface_name, append, max_matches,
1087 decls))
1088 break;
1089
1090 ObjCInterfaceDecl *interface_decl_from_modules =
1091 dyn_cast<ObjCInterfaceDecl>(ClangUtil::GetDecl(decls[0]));
1092
1093 if (!interface_decl_from_modules)
1094 break;
1095
1096 if (FindObjCMethodDeclsWithOrigin(context, interface_decl_from_modules,
1097 "in modules"))
1098 return;
1099 }
1100 } while (false);
1101
1102 do {
1103 // Check the runtime only if the debug information didn't have a complete
1104 // interface and the modules don't get us anywhere.
1105
1106 lldb::ProcessSP process(m_target->GetProcessSP());
1107
1108 if (!process)
1109 break;
1110
1111 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
1112
1113 if (!language_runtime)
1114 break;
1115
1116 DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
1117
1118 if (!decl_vendor)
1119 break;
1120
1121 ConstString interface_name(interface_decl->getNameAsString().c_str());
1122 bool append = false;
1123 uint32_t max_matches = 1;
1124 std::vector<CompilerDecl> decls;
1125
1126 auto *clang_decl_vendor = llvm::cast<DeclVendor>(decl_vendor);
1127 if (!clang_decl_vendor->FindDecls(interface_name, append, max_matches,
1128 decls))
1129 break;
1130
1131 ObjCInterfaceDecl *runtime_interface_decl =
1132 dyn_cast<ObjCInterfaceDecl>(ClangUtil::GetDecl(decls[0]));
1133
1134 if (!runtime_interface_decl)
1135 break;
1136
1137 FindObjCMethodDeclsWithOrigin(context, runtime_interface_decl,
1138 "in runtime");
1139 } while (false);
1140}
1141
1143 NameSearchContext &context,
1144 DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl) {
1146
1147 if (origin_iface_decl.IsInvalid())
1148 return false;
1149
1150 std::string name_str = context.m_decl_name.getAsString();
1151 StringRef name(name_str);
1152 IdentifierInfo &name_identifier(
1153 origin_iface_decl->getASTContext().Idents.get(name));
1154
1155 DeclFromUser<ObjCPropertyDecl> origin_property_decl(
1156 origin_iface_decl->FindPropertyDeclaration(
1157 &name_identifier, ObjCPropertyQueryKind::OBJC_PR_query_instance));
1158
1159 bool found = false;
1160
1161 if (origin_property_decl.IsValid()) {
1162 DeclFromParser<ObjCPropertyDecl> parser_property_decl(
1163 origin_property_decl.Import(m_ast_context, *m_ast_importer_sp));
1164 if (parser_property_decl.IsValid()) {
1165 LLDB_LOG(log, " CAS::FOPD found\n{0}",
1166 ClangUtil::DumpDecl(parser_property_decl.decl));
1167
1168 context.AddNamedDecl(parser_property_decl.decl);
1169 found = true;
1170 }
1171 }
1172
1173 DeclFromUser<ObjCIvarDecl> origin_ivar_decl(
1174 origin_iface_decl->getIvarDecl(&name_identifier));
1175
1176 if (origin_ivar_decl.IsValid()) {
1177 DeclFromParser<ObjCIvarDecl> parser_ivar_decl(
1178 origin_ivar_decl.Import(m_ast_context, *m_ast_importer_sp));
1179 if (parser_ivar_decl.IsValid()) {
1180 LLDB_LOG(log, " CAS::FOPD found\n{0}",
1181 ClangUtil::DumpDecl(parser_ivar_decl.decl));
1182
1183 context.AddNamedDecl(parser_ivar_decl.decl);
1184 found = true;
1185 }
1186 }
1187
1188 return found;
1189}
1190
1193
1195 cast<ObjCInterfaceDecl>(context.m_decl_context));
1196 DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(
1197 parser_iface_decl.GetOrigin(*m_ast_importer_sp));
1198
1199 ConstString class_name(parser_iface_decl->getNameAsString().c_str());
1200
1201 LLDB_LOG(log,
1202 "ClangASTSource::FindObjCPropertyAndIvarDecls on "
1203 "(ASTContext*){0:x} '{1}' for '{2}.{3}'",
1204 m_ast_context, m_clang_ast_context->getDisplayName(),
1205 parser_iface_decl->getName(), context.m_decl_name.getAsString());
1206
1207 if (FindObjCPropertyAndIvarDeclsWithOrigin(context, origin_iface_decl))
1208 return;
1209
1210 LLDB_LOG(log,
1211 "CAS::FOPD couldn't find the property on origin "
1212 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}, searching "
1213 "elsewhere...",
1214 origin_iface_decl.decl, &origin_iface_decl->getASTContext());
1215
1216 SymbolContext null_sc;
1217 TypeList type_list;
1218
1219 do {
1220 ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(
1221 const_cast<ObjCInterfaceDecl *>(parser_iface_decl.decl));
1222
1223 if (!complete_interface_decl)
1224 break;
1225
1226 // We found the complete interface. The runtime never needs to be queried
1227 // in this scenario.
1228
1229 DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(
1230 complete_interface_decl);
1231
1232 if (complete_iface_decl.decl == origin_iface_decl.decl)
1233 break; // already checked this one
1234
1235 LLDB_LOG(log,
1236 "CAS::FOPD trying origin "
1237 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",
1238 complete_iface_decl.decl, &complete_iface_decl->getASTContext());
1239
1240 FindObjCPropertyAndIvarDeclsWithOrigin(context, complete_iface_decl);
1241
1242 return;
1243 } while (false);
1244
1245 do {
1246 // Check the modules only if the debug information didn't have a complete
1247 // interface.
1248
1249 std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1251
1252 if (!modules_decl_vendor)
1253 break;
1254
1255 bool append = false;
1256 uint32_t max_matches = 1;
1257 std::vector<CompilerDecl> decls;
1258
1259 if (!modules_decl_vendor->FindDecls(class_name, append, max_matches, decls))
1260 break;
1261
1262 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_modules(
1263 dyn_cast<ObjCInterfaceDecl>(ClangUtil::GetDecl(decls[0])));
1264
1265 if (!interface_decl_from_modules.IsValid())
1266 break;
1267
1268 LLDB_LOG(log,
1269 "CAS::FOPD[{0:x}] trying module "
1270 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",
1271 interface_decl_from_modules.decl,
1272 &interface_decl_from_modules->getASTContext());
1273
1275 interface_decl_from_modules))
1276 return;
1277 } while (false);
1278
1279 do {
1280 // Check the runtime only if the debug information didn't have a complete
1281 // interface and nothing was in the modules.
1282
1283 lldb::ProcessSP process(m_target->GetProcessSP());
1284
1285 if (!process)
1286 return;
1287
1288 ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
1289
1290 if (!language_runtime)
1291 return;
1292
1293 DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
1294
1295 if (!decl_vendor)
1296 break;
1297
1298 bool append = false;
1299 uint32_t max_matches = 1;
1300 std::vector<CompilerDecl> decls;
1301
1302 auto *clang_decl_vendor = llvm::cast<DeclVendor>(decl_vendor);
1303 if (!clang_decl_vendor->FindDecls(class_name, append, max_matches, decls))
1304 break;
1305
1306 DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_runtime(
1307 dyn_cast<ObjCInterfaceDecl>(ClangUtil::GetDecl(decls[0])));
1308
1309 if (!interface_decl_from_runtime.IsValid())
1310 break;
1311
1312 LLDB_LOG(log,
1313 "CAS::FOPD[{0:x}] trying runtime "
1314 "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}...",
1315 interface_decl_from_runtime.decl,
1316 &interface_decl_from_runtime->getASTContext());
1317
1319 interface_decl_from_runtime))
1320 return;
1321 } while (false);
1322}
1323
1325 const NamespaceDecl *namespace_context =
1326 dyn_cast<NamespaceDecl>(context.m_decl_context);
1327
1329
1330 ClangASTImporter::NamespaceMapSP namespace_map =
1331 m_ast_importer_sp->GetNamespaceMap(namespace_context);
1332
1333 LLDB_LOGV(log, " 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 NameSearchContext &context,
1437 ClangASTImporter::NamespaceMapSP &namespace_decls) {
1438 if (!namespace_decls)
1439 return nullptr;
1440
1441 const CompilerDeclContext &namespace_decl = namespace_decls->begin()->second;
1442
1443 clang::ASTContext *src_ast =
1445 if (!src_ast)
1446 return nullptr;
1447 clang::NamespaceDecl *src_namespace_decl =
1449
1450 if (!src_namespace_decl)
1451 return nullptr;
1452
1453 Decl *copied_decl = CopyDecl(src_namespace_decl);
1454
1455 if (!copied_decl)
1456 return nullptr;
1457
1458 NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1459
1460 if (!copied_namespace_decl)
1461 return nullptr;
1462
1463 context.m_decls.push_back(copied_namespace_decl);
1464
1465 m_ast_importer_sp->RegisterNamespaceMap(copied_namespace_decl,
1466 namespace_decls);
1467
1468 return dyn_cast<NamespaceDecl>(copied_decl);
1469}
1470
1471clang::Decl *ClangASTSource::CopyDecl(Decl *src_decl) {
1472 return m_ast_importer_sp->CopyDecl(m_ast_context, src_decl);
1473}
1474
1476 return m_ast_importer_sp->GetDeclOrigin(decl);
1477}
1478
1480 auto src_ast = src_type.GetTypeSystem<TypeSystemClang>();
1481 if (!src_ast)
1482 return {};
1483
1484 QualType copied_qual_type = ClangUtil::GetQualType(
1485 m_ast_importer_sp->CopyType(*m_clang_ast_context, src_type));
1486
1487 if (copied_qual_type.getAsOpaquePtr() &&
1488 copied_qual_type->getCanonicalTypeInternal().isNull())
1489 // this shouldn't happen, but we're hardening because the AST importer
1490 // seems to be generating bad types on occasion.
1491 return {};
1492
1493 return m_clang_ast_context->GetType(copied_qual_type);
1494}
1495
1496std::shared_ptr<ClangModulesDeclVendor>
1498 auto persistent_vars = llvm::cast<ClangPersistentVariables>(
1499 m_target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
1500 return persistent_vars->GetClangModulesDeclVendor();
1501}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
#define LLDB_LOGV(log,...)
Definition Log.h:383
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.
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.
clang::NamespaceDecl * AddNamespace(NameSearchContext &context, ClangASTImporter::NamespaceMapSP &namespace_decls)
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()
bool GetVerbose() const
Definition Log.cpp:326
A collection class for Module objects.
Definition ModuleList.h:104
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:363
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:89
@ 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.