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