Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
Line
Count
Source (jump to first uncovered line)
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"
12
#include "ClangModulesDeclVendor.h"
13
14
#include "lldb/Core/Module.h"
15
#include "lldb/Core/ModuleList.h"
16
#include "lldb/Symbol/CompilerDeclContext.h"
17
#include "lldb/Symbol/Function.h"
18
#include "lldb/Symbol/SymbolFile.h"
19
#include "lldb/Symbol/TaggedASTType.h"
20
#include "lldb/Target/Target.h"
21
#include "lldb/Utility/LLDBLog.h"
22
#include "lldb/Utility/Log.h"
23
#include "clang/AST/ASTContext.h"
24
#include "clang/AST/RecordLayout.h"
25
#include "clang/Basic/SourceManager.h"
26
27
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
28
#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
29
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
30
31
#include <memory>
32
#include <vector>
33
34
using namespace clang;
35
using namespace lldb_private;
36
37
// Scoped class that will remove an active lexical decl from the set when it
38
// goes out of scope.
39
namespace {
40
class ScopedLexicalDeclEraser {
41
public:
42
  ScopedLexicalDeclEraser(std::set<const clang::Decl *> &decls,
43
                          const clang::Decl *decl)
44
43.8k
      : m_active_lexical_decls(decls), m_decl(decl) {}
45
46
43.8k
  ~ScopedLexicalDeclEraser() { m_active_lexical_decls.erase(m_decl); }
47
48
private:
49
  std::set<const clang::Decl *> &m_active_lexical_decls;
50
  const clang::Decl *m_decl;
51
};
52
}
53
54
ClangASTSource::ClangASTSource(
55
    const lldb::TargetSP &target,
56
    const std::shared_ptr<ClangASTImporter> &importer)
57
12.0k
    : m_lookups_enabled(false), m_target(target), m_ast_context(nullptr),
58
12.0k
      m_ast_importer_sp(importer), m_active_lexical_decls(),
59
12.0k
      m_active_lookups() {
60
12.0k
  assert(m_ast_importer_sp && "No ClangASTImporter passed to ClangASTSource?");
61
12.0k
}
62
63
12.0k
void ClangASTSource::InstallASTContext(TypeSystemClang &clang_ast_context) {
64
12.0k
  m_ast_context = &clang_ast_context.getASTContext();
65
12.0k
  m_clang_ast_context = &clang_ast_context;
66
12.0k
  m_file_manager = &m_ast_context->getSourceManager().getFileManager();
67
12.0k
  m_ast_importer_sp->InstallMapCompleter(m_ast_context, *this);
68
12.0k
}
69
70
12.0k
ClangASTSource::~ClangASTSource() {
71
12.0k
  m_ast_importer_sp->ForgetDestination(m_ast_context);
72
73
12.0k
  if (!m_target)
74
2
    return;
75
76
  // Unregister the current ASTContext as a source for all scratch
77
  // ASTContexts in the ClangASTImporter. Without this the scratch AST might
78
  // query the deleted ASTContext for additional type information.
79
  // We unregister from *all* scratch ASTContexts in case a type got exported
80
  // to a scratch AST that isn't the best fitting scratch ASTContext.
81
12.0k
  lldb::TypeSystemClangSP scratch_ts_sp = ScratchTypeSystemClang::GetForTarget(
82
12.0k
      *m_target, ScratchTypeSystemClang::DefaultAST, false);
83
84
12.0k
  if (!scratch_ts_sp)
85
2.07k
    return;
86
87
9.94k
  ScratchTypeSystemClang *default_scratch_ast =
88
9.94k
      llvm::cast<ScratchTypeSystemClang>(scratch_ts_sp.get());
89
  // Unregister from the default scratch AST (and all sub-ASTs).
90
9.94k
  default_scratch_ast->ForgetSource(m_ast_context, *m_ast_importer_sp);
91
9.94k
}
92
93
9.94k
void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) {
94
9.94k
  if (!m_ast_context)
95
0
    return;
96
97
9.94k
  m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
98
9.94k
  m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
99
9.94k
}
100
101
// The core lookup interface.
102
bool ClangASTSource::FindExternalVisibleDeclsByName(
103
356k
    const DeclContext *decl_ctx, DeclarationName clang_decl_name) {
104
356k
  if (!m_ast_context) {
105
0
    SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
106
0
    return false;
107
0
  }
108
109
356k
  std::string decl_name(clang_decl_name.getAsString());
110
111
356k
  switch (clang_decl_name.getNameKind()) {
112
  // Normal identifiers.
113
343k
  case DeclarationName::Identifier: {
114
343k
    clang::IdentifierInfo *identifier_info =
115
343k
        clang_decl_name.getAsIdentifierInfo();
116
117
343k
    if (!identifier_info || 
identifier_info->getBuiltinID() != 0343k
) {
118
7
      SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
119
7
      return false;
120
7
    }
121
343k
  } 
break343k
;
122
123
  // Operator names.
124
343k
  case DeclarationName::CXXOperatorName:
125
1.45k
  case DeclarationName::CXXLiteralOperatorName:
126
1.45k
    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
10.6k
  case DeclarationName::CXXUsingDirective:
131
10.6k
    SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
132
10.6k
    return false;
133
134
181
  case DeclarationName::ObjCZeroArgSelector:
135
485
  case DeclarationName::ObjCOneArgSelector:
136
497
  case DeclarationName::ObjCMultiArgSelector: {
137
497
    llvm::SmallVector<NamedDecl *, 1> method_decls;
138
139
497
    NameSearchContext method_search_context(*m_clang_ast_context, method_decls,
140
497
                                            clang_decl_name, decl_ctx);
141
142
497
    FindObjCMethodDecls(method_search_context);
143
144
497
    SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, method_decls);
145
497
    return (method_decls.size() > 0);
146
485
  }
147
  // These aren't possible in the global context.
148
150
  case DeclarationName::CXXConstructorName:
149
318
  case DeclarationName::CXXDestructorName:
150
318
  case DeclarationName::CXXConversionFunctionName:
151
318
  case DeclarationName::CXXDeductionGuideName:
152
318
    SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
153
318
    return false;
154
356k
  }
155
156
345k
  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
308k
    if (!decl_name.empty() && decl_name[0] == '$') {
160
7.94k
      SetLookupsEnabled(true);
161
300k
    } else {
162
300k
      SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
163
300k
      return false;
164
300k
    }
165
308k
  }
166
167
44.6k
  ConstString const_decl_name(decl_name.c_str());
168
169
44.6k
  const char *uniqued_const_decl_name = const_decl_name.GetCString();
170
44.6k
  if (m_active_lookups.find(uniqued_const_decl_name) !=
171
44.6k
      m_active_lookups.end()) {
172
    // We are currently looking up this name...
173
0
    SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
174
0
    return false;
175
0
  }
176
44.6k
  m_active_lookups.insert(uniqued_const_decl_name);
177
44.6k
  llvm::SmallVector<NamedDecl *, 4> name_decls;
178
44.6k
  NameSearchContext name_search_context(*m_clang_ast_context, name_decls,
179
44.6k
                                        clang_decl_name, decl_ctx);
180
44.6k
  FindExternalVisibleDecls(name_search_context);
181
44.6k
  SetExternalVisibleDeclsForName(decl_ctx, clang_decl_name, name_decls);
182
44.6k
  m_active_lookups.erase(uniqued_const_decl_name);
183
44.6k
  return (name_decls.size() != 0);
184
44.6k
}
185
186
849
TagDecl *ClangASTSource::FindCompleteType(const TagDecl *decl) {
187
849
  Log *log = GetLog(LLDBLog::Expressions);
188
189
849
  if (const NamespaceDecl *namespace_context =
190
849
          dyn_cast<NamespaceDecl>(decl->getDeclContext())) {
191
652
    ClangASTImporter::NamespaceMapSP namespace_map =
192
652
        m_ast_importer_sp->GetNamespaceMap(namespace_context);
193
194
652
    if (!namespace_map)
195
628
      return nullptr;
196
197
24
    LLDB_LOGV(log, "      CTD Inspecting namespace map{0} ({1} entries)",
198
24
              namespace_map.get(), namespace_map->size());
199
200
52
    for (const ClangASTImporter::NamespaceMapItem &item : *namespace_map) {
201
52
      LLDB_LOG(log, "      CTD Searching namespace {0} in module {1}",
202
52
               item.second.GetName(), item.first->GetFileSpec().GetFilename());
203
204
52
      TypeList types;
205
206
52
      ConstString name(decl->getName());
207
208
52
      item.first->FindTypesInNamespace(name, item.second, UINT32_MAX, types);
209
210
52
      for (uint32_t ti = 0, te = types.GetSize(); ti != te; 
++ti0
) {
211
14
        lldb::TypeSP type = types.GetTypeAtIndex(ti);
212
213
14
        if (!type)
214
0
          continue;
215
216
14
        CompilerType clang_type(type->GetFullCompilerType());
217
218
14
        if (!ClangUtil::IsClangType(clang_type))
219
0
          continue;
220
221
14
        const TagType *tag_type =
222
14
            ClangUtil::GetQualType(clang_type)->getAs<TagType>();
223
224
14
        if (!tag_type)
225
0
          continue;
226
227
14
        TagDecl *candidate_tag_decl =
228
14
            const_cast<TagDecl *>(tag_type->getDecl());
229
230
14
        if (TypeSystemClang::GetCompleteDecl(
231
14
                &candidate_tag_decl->getASTContext(), candidate_tag_decl))
232
14
          return candidate_tag_decl;
233
14
      }
234
52
    }
235
197
  } else {
236
197
    TypeList types;
237
238
197
    ConstString name(decl->getName());
239
240
197
    const ModuleList &module_list = m_target->GetImages();
241
242
197
    bool exact_match = false;
243
197
    llvm::DenseSet<SymbolFile *> searched_symbol_files;
244
197
    module_list.FindTypes(nullptr, name, exact_match, UINT32_MAX,
245
197
                          searched_symbol_files, types);
246
247
197
    for (uint32_t ti = 0, te = types.GetSize(); ti != te; 
++ti0
) {
248
6
      lldb::TypeSP type = types.GetTypeAtIndex(ti);
249
250
6
      if (!type)
251
0
        continue;
252
253
6
      CompilerType clang_type(type->GetFullCompilerType());
254
255
6
      if (!ClangUtil::IsClangType(clang_type))
256
0
        continue;
257
258
6
      const TagType *tag_type =
259
6
          ClangUtil::GetQualType(clang_type)->getAs<TagType>();
260
261
6
      if (!tag_type)
262
0
        continue;
263
264
6
      TagDecl *candidate_tag_decl = const_cast<TagDecl *>(tag_type->getDecl());
265
266
      // We have found a type by basename and we need to make sure the decl
267
      // contexts are the same before we can try to complete this type with
268
      // another
269
6
      if (!TypeSystemClang::DeclsAreEquivalent(const_cast<TagDecl *>(decl),
270
6
                                               candidate_tag_decl))
271
0
        continue;
272
273
6
      if (TypeSystemClang::GetCompleteDecl(&candidate_tag_decl->getASTContext(),
274
6
                                           candidate_tag_decl))
275
6
        return candidate_tag_decl;
276
6
    }
277
197
  }
278
201
  return nullptr;
279
849
}
280
281
5.23k
void ClangASTSource::CompleteType(TagDecl *tag_decl) {
282
5.23k
  Log *log = GetLog(LLDBLog::Expressions);
283
284
5.23k
  if (log) {
285
0
    LLDB_LOG(log,
286
0
             "    CompleteTagDecl on (ASTContext*){0} Completing "
287
0
             "(TagDecl*){1} named {2}",
288
0
             m_clang_ast_context->getDisplayName(), tag_decl,
289
0
             tag_decl->getName());
290
291
0
    LLDB_LOG(log, "      CTD Before:\n{0}", ClangUtil::DumpDecl(tag_decl));
292
0
  }
293
294
5.23k
  auto iter = m_active_lexical_decls.find(tag_decl);
295
5.23k
  if (iter != m_active_lexical_decls.end())
296
0
    return;
297
5.23k
  m_active_lexical_decls.insert(tag_decl);
298
5.23k
  ScopedLexicalDeclEraser eraser(m_active_lexical_decls, tag_decl);
299
300
5.23k
  if (!m_ast_importer_sp->CompleteTagDecl(tag_decl)) {
301
    // We couldn't complete the type.  Maybe there's a definition somewhere
302
    // else that can be completed.
303
849
    if (TagDecl *alternate = FindCompleteType(tag_decl))
304
20
      m_ast_importer_sp->CompleteTagDeclWithOrigin(tag_decl, alternate);
305
849
  }
306
307
5.23k
  LLDB_LOG(log, "      [CTD] After:\n{0}", ClangUtil::DumpDecl(tag_decl));
308
5.23k
}
309
310
938
void ClangASTSource::CompleteType(clang::ObjCInterfaceDecl *interface_decl) {
311
938
  Log *log = GetLog(LLDBLog::Expressions);
312
313
938
  LLDB_LOG(log,
314
938
           "    [CompleteObjCInterfaceDecl] on (ASTContext*){0} '{1}' "
315
938
           "Completing an ObjCInterfaceDecl named {1}",
316
938
           m_ast_context, m_clang_ast_context->getDisplayName(),
317
938
           interface_decl->getName());
318
938
  LLDB_LOG(log, "      [COID] Before:\n{0}",
319
938
           ClangUtil::DumpDecl(interface_decl));
320
321
938
  ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl);
322
323
938
  if (original.Valid()) {
324
938
    if (ObjCInterfaceDecl *original_iface_decl =
325
938
            dyn_cast<ObjCInterfaceDecl>(original.decl)) {
326
938
      ObjCInterfaceDecl *complete_iface_decl =
327
938
          GetCompleteObjCInterface(original_iface_decl);
328
329
938
      if (complete_iface_decl && 
(complete_iface_decl != original_iface_decl)245
) {
330
24
        m_ast_importer_sp->SetDeclOrigin(interface_decl, complete_iface_decl);
331
24
      }
332
938
    }
333
938
  }
334
335
938
  m_ast_importer_sp->CompleteObjCInterfaceDecl(interface_decl);
336
337
938
  if (interface_decl->getSuperClass() &&
338
938
      
interface_decl->getSuperClass() != interface_decl495
)
339
495
    CompleteType(interface_decl->getSuperClass());
340
341
938
  LLDB_LOG(log, "      [COID] After:");
342
938
  LLDB_LOG(log, "      [COID] {0}", ClangUtil::DumpDecl(interface_decl));
343
938
}
344
345
clang::ObjCInterfaceDecl *ClangASTSource::GetCompleteObjCInterface(
346
1.72k
    const clang::ObjCInterfaceDecl *interface_decl) {
347
1.72k
  lldb::ProcessSP process(m_target->GetProcessSP());
348
349
1.72k
  if (!process)
350
0
    return nullptr;
351
352
1.72k
  ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
353
354
1.72k
  if (!language_runtime)
355
0
    return nullptr;
356
357
1.72k
  ConstString class_name(interface_decl->getNameAsString().c_str());
358
359
1.72k
  lldb::TypeSP complete_type_sp(
360
1.72k
      language_runtime->LookupInCompleteClassCache(class_name));
361
362
1.72k
  if (!complete_type_sp)
363
1.11k
    return nullptr;
364
365
606
  TypeFromUser complete_type =
366
606
      TypeFromUser(complete_type_sp->GetFullCompilerType());
367
606
  lldb::opaque_compiler_type_t complete_opaque_type =
368
606
      complete_type.GetOpaqueQualType();
369
370
606
  if (!complete_opaque_type)
371
0
    return nullptr;
372
373
606
  const clang::Type *complete_clang_type =
374
606
      QualType::getFromOpaquePtr(complete_opaque_type).getTypePtr();
375
606
  const ObjCInterfaceType *complete_interface_type =
376
606
      dyn_cast<ObjCInterfaceType>(complete_clang_type);
377
378
606
  if (!complete_interface_type)
379
0
    return nullptr;
380
381
606
  ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl());
382
383
606
  return complete_iface_decl;
384
606
}
385
386
void ClangASTSource::FindExternalLexicalDecls(
387
    const DeclContext *decl_context,
388
    llvm::function_ref<bool(Decl::Kind)> predicate,
389
41.5k
    llvm::SmallVectorImpl<Decl *> &decls) {
390
391
41.5k
  Log *log = GetLog(LLDBLog::Expressions);
392
393
41.5k
  const Decl *context_decl = dyn_cast<Decl>(decl_context);
394
395
41.5k
  if (!context_decl)
396
0
    return;
397
398
41.5k
  auto iter = m_active_lexical_decls.find(context_decl);
399
41.5k
  if (iter != m_active_lexical_decls.end())
400
2.96k
    return;
401
38.6k
  m_active_lexical_decls.insert(context_decl);
402
38.6k
  ScopedLexicalDeclEraser eraser(m_active_lexical_decls, context_decl);
403
404
38.6k
  if (log) {
405
0
    if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(context_decl))
406
0
      LLDB_LOG(log,
407
0
               "FindExternalLexicalDecls on (ASTContext*){0} '{1}' in "
408
0
               "'{2}' ({3}Decl*){4}",
409
0
               m_ast_context, m_clang_ast_context->getDisplayName(),
410
0
               context_named_decl->getNameAsString().c_str(),
411
0
               context_decl->getDeclKindName(),
412
0
               static_cast<const void *>(context_decl));
413
0
    else if (context_decl)
414
0
      LLDB_LOG(log,
415
0
               "FindExternalLexicalDecls on (ASTContext*){0} '{1}' in "
416
0
               "({2}Decl*){3}",
417
0
               m_ast_context, m_clang_ast_context->getDisplayName(),
418
0
               context_decl->getDeclKindName(),
419
0
               static_cast<const void *>(context_decl));
420
0
    else
421
0
      LLDB_LOG(log,
422
0
               "FindExternalLexicalDecls on (ASTContext*){0} '{1}' in a "
423
0
               "NULL context",
424
0
               m_ast_context, m_clang_ast_context->getDisplayName());
425
0
  }
426
427
38.6k
  ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(context_decl);
428
429
38.6k
  if (!original.Valid())
430
1.31k
    return;
431
432
37.2k
  LLDB_LOG(log, "  FELD Original decl {0} (Decl*){1:x}:\n{2}",
433
37.2k
           static_cast<void *>(original.ctx),
434
37.2k
           static_cast<void *>(original.decl),
435
37.2k
           ClangUtil::DumpDecl(original.decl));
436
437
37.2k
  if (ObjCInterfaceDecl *original_iface_decl =
438
37.2k
          dyn_cast<ObjCInterfaceDecl>(original.decl)) {
439
287
    ObjCInterfaceDecl *complete_iface_decl =
440
287
        GetCompleteObjCInterface(original_iface_decl);
441
442
287
    if (complete_iface_decl && 
(complete_iface_decl != original_iface_decl)128
) {
443
14
      original.decl = complete_iface_decl;
444
14
      original.ctx = &complete_iface_decl->getASTContext();
445
446
14
      m_ast_importer_sp->SetDeclOrigin(context_decl, complete_iface_decl);
447
14
    }
448
287
  }
449
450
37.2k
  if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original.decl)) {
451
37.0k
    ExternalASTSource *external_source = original.ctx->getExternalSource();
452
453
37.0k
    if (external_source)
454
37.0k
      external_source->CompleteType(original_tag_decl);
455
37.0k
  }
456
457
37.2k
  const DeclContext *original_decl_context =
458
37.2k
      dyn_cast<DeclContext>(original.decl);
459
460
37.2k
  if (!original_decl_context)
461
0
    return;
462
463
  // Indicates whether we skipped any Decls of the original DeclContext.
464
37.2k
  bool SkippedDecls = false;
465
415k
  for (Decl *decl : original_decl_context->decls()) {
466
    // The predicate function returns true if the passed declaration kind is
467
    // the one we are looking for.
468
    // See clang::ExternalASTSource::FindExternalLexicalDecls()
469
415k
    if (predicate(decl->getKind())) {
470
317k
      if (log) {
471
0
        std::string ast_dump = ClangUtil::DumpDecl(decl);
472
0
        if (const NamedDecl *context_named_decl =
473
0
                dyn_cast<NamedDecl>(context_decl))
474
0
          LLDB_LOG(log, "  FELD Adding [to {0}Decl {1}] lexical {2}Decl {3}",
475
0
                   context_named_decl->getDeclKindName(),
476
0
                   context_named_decl->getName(), decl->getDeclKindName(),
477
0
                   ast_dump);
478
0
        else
479
0
          LLDB_LOG(log, "  FELD Adding lexical {0}Decl {1}",
480
0
                   decl->getDeclKindName(), ast_dump);
481
0
      }
482
483
317k
      Decl *copied_decl = CopyDecl(decl);
484
485
317k
      if (!copied_decl)
486
4
        continue;
487
488
      // FIXME: We should add the copied decl to the 'decls' list. This would
489
      // add the copied Decl into the DeclContext and make sure that we
490
      // correctly propagate that we added some Decls back to Clang.
491
      // By leaving 'decls' empty we incorrectly return false from
492
      // DeclContext::LoadLexicalDeclsFromExternalStorage which might cause
493
      // lookup issues later on.
494
      // We can't just add them for now as the ASTImporter already added the
495
      // decl into the DeclContext and this would add it twice.
496
497
317k
      if (FieldDecl *copied_field = dyn_cast<FieldDecl>(copied_decl)) {
498
24.2k
        QualType copied_field_type = copied_field->getType();
499
500
24.2k
        m_ast_importer_sp->RequireCompleteType(copied_field_type);
501
24.2k
      }
502
317k
    } else {
503
98.0k
      SkippedDecls = true;
504
98.0k
    }
505
415k
  }
506
507
  // CopyDecl may build a lookup table which may set up ExternalLexicalStorage
508
  // to false.  However, since we skipped some of the external Decls we must
509
  // set it back!
510
37.2k
  if (SkippedDecls) {
511
10.6k
    decl_context->setHasExternalLexicalStorage(true);
512
    // This sets HasLazyExternalLexicalLookups to true.  By setting this bit we
513
    // ensure that the lookup table is rebuilt, which means the external source
514
    // is consulted again when a clang::DeclContext::lookup is called.
515
10.6k
    const_cast<DeclContext *>(decl_context)->setMustBuildLookupTable();
516
10.6k
  }
517
37.2k
}
518
519
37.4k
void ClangASTSource::FindExternalVisibleDecls(NameSearchContext &context) {
520
37.4k
  assert(m_ast_context);
521
522
37.4k
  const ConstString name(context.m_decl_name.getAsString().c_str());
523
524
37.4k
  Log *log = GetLog(LLDBLog::Expressions);
525
526
37.4k
  if (log) {
527
46
    if (!context.m_decl_context)
528
0
      LLDB_LOG(log,
529
46
               "ClangASTSource::FindExternalVisibleDecls on "
530
46
               "(ASTContext*){0} '{1}' for '{2}' in a NULL DeclContext",
531
46
               m_ast_context, m_clang_ast_context->getDisplayName(), name);
532
46
    else if (const NamedDecl *context_named_decl =
533
46
                 dyn_cast<NamedDecl>(context.m_decl_context))
534
0
      LLDB_LOG(log,
535
46
               "ClangASTSource::FindExternalVisibleDecls on "
536
46
               "(ASTContext*){0} '{1}' for '{2}' in '{3}'",
537
46
               m_ast_context, m_clang_ast_context->getDisplayName(), name,
538
46
               context_named_decl->getName());
539
46
    else
540
46
      LLDB_LOG(log,
541
46
               "ClangASTSource::FindExternalVisibleDecls on "
542
46
               "(ASTContext*){0} '{1}' for '{2}' in a '{3}'",
543
46
               m_ast_context, m_clang_ast_context->getDisplayName(), name,
544
46
               context.m_decl_context->getDeclKindName());
545
46
  }
546
547
37.4k
  if (isa<NamespaceDecl>(context.m_decl_context)) {
548
398
    LookupInNamespace(context);
549
37.0k
  } else if (isa<ObjCInterfaceDecl>(context.m_decl_context)) {
550
523
    FindObjCPropertyAndIvarDecls(context);
551
36.4k
  } else if (!isa<TranslationUnitDecl>(context.m_decl_context)) {
552
    // we shouldn't be getting FindExternalVisibleDecls calls for these
553
4.01k
    return;
554
32.4k
  } else {
555
32.4k
    CompilerDeclContext namespace_decl;
556
557
32.4k
    LLDB_LOG(log, "  CAS::FEVD Searching the root namespace");
558
559
32.4k
    FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl);
560
32.4k
  }
561
562
33.4k
  if (!context.m_namespace_map->empty()) {
563
232
    if (log && 
log->GetVerbose()0
)
564
0
      LLDB_LOG(log, "  CAS::FEVD Registering namespace map {0} ({1} entries)",
565
232
               context.m_namespace_map.get(), context.m_namespace_map->size());
566
567
232
    NamespaceDecl *clang_namespace_decl =
568
232
        AddNamespace(context, context.m_namespace_map);
569
570
232
    if (clang_namespace_decl)
571
232
      clang_namespace_decl->setHasExternalVisibleStorage();
572
232
  }
573
33.4k
}
574
575
0
clang::Sema *ClangASTSource::getSema() {
576
0
  return m_clang_ast_context->getSema();
577
0
}
578
579
bool ClangASTSource::IgnoreName(const ConstString name,
580
82.7k
                                bool ignore_all_dollar_names) {
581
82.7k
  static const ConstString id_name("id");
582
82.7k
  static const ConstString Class_name("Class");
583
584
82.7k
  if (m_ast_context->getLangOpts().ObjC)
585
76.0k
    if (name == id_name || 
name == Class_name75.9k
)
586
272
      return true;
587
588
82.5k
  StringRef name_string_ref = name.GetStringRef();
589
590
  // The ClangASTSource is not responsible for finding $-names.
591
82.5k
  return name_string_ref.empty() ||
592
82.5k
         (ignore_all_dollar_names && 
name_string_ref.startswith("$")43.0k
) ||
593
82.5k
         
name_string_ref.startswith("_$")60.2k
;
594
82.7k
}
595
596
void ClangASTSource::FindExternalVisibleDecls(
597
    NameSearchContext &context, lldb::ModuleSP module_sp,
598
32.9k
    CompilerDeclContext &namespace_decl) {
599
32.9k
  assert(m_ast_context);
600
601
32.9k
  Log *log = GetLog(LLDBLog::Expressions);
602
603
32.9k
  SymbolContextList sc_list;
604
605
32.9k
  const ConstString name(context.m_decl_name.getAsString().c_str());
606
32.9k
  if (IgnoreName(name, true))
607
22.7k
    return;
608
609
10.2k
  if (!m_target)
610
1
    return;
611
612
10.2k
  FillNamespaceMap(context, module_sp, namespace_decl);
613
614
10.2k
  if (context.m_found_type)
615
18
    return;
616
617
10.2k
  TypeList types;
618
10.2k
  const bool exact_match = true;
619
10.2k
  llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files;
620
10.2k
  if (module_sp && 
namespace_decl451
)
621
451
    module_sp->FindTypesInNamespace(name, namespace_decl, 1, types);
622
9.76k
  else {
623
9.76k
    m_target->GetImages().FindTypes(module_sp.get(), name, exact_match, 1,
624
9.76k
                                    searched_symbol_files, types);
625
9.76k
  }
626
627
10.2k
  if (size_t num_types = types.GetSize()) {
628
625
    for (size_t ti = 0; ti < num_types; 
++ti0
) {
629
625
      lldb::TypeSP type_sp = types.GetTypeAtIndex(ti);
630
631
625
      if (log) {
632
0
        const char *name_string = type_sp->GetName().GetCString();
633
634
0
        LLDB_LOG(log, "  CAS::FEVD Matching type found for \"{0}\": {1}", name,
635
0
                 (name_string ? name_string : "<anonymous>"));
636
0
      }
637
638
625
      CompilerType full_type = type_sp->GetFullCompilerType();
639
640
625
      CompilerType copied_clang_type(GuardedCopyType(full_type));
641
642
625
      if (!copied_clang_type) {
643
0
        LLDB_LOG(log, "  CAS::FEVD - Couldn't export a type");
644
645
0
        continue;
646
0
      }
647
648
625
      context.AddTypeDecl(copied_clang_type);
649
650
625
      context.m_found_type = true;
651
625
      break;
652
625
    }
653
625
  }
654
655
10.2k
  if (!context.m_found_type) {
656
    // Try the modules next.
657
9.59k
    FindDeclInModules(context, name);
658
9.59k
  }
659
660
10.2k
  if (!context.m_found_type) {
661
9.58k
    FindDeclInObjCRuntime(context, name);
662
9.58k
  }
663
10.2k
}
664
665
void ClangASTSource::FillNamespaceMap(
666
    NameSearchContext &context, lldb::ModuleSP module_sp,
667
10.2k
    const CompilerDeclContext &namespace_decl) {
668
10.2k
  const ConstString name(context.m_decl_name.getAsString().c_str());
669
10.2k
  if (IgnoreName(name, true))
670
0
    return;
671
672
10.2k
  Log *log = GetLog(LLDBLog::Expressions);
673
674
10.2k
  if (module_sp && 
namespace_decl469
) {
675
469
    CompilerDeclContext found_namespace_decl;
676
677
469
    if (SymbolFile *symbol_file = module_sp->GetSymbolFile()) {
678
469
      found_namespace_decl = symbol_file->FindNamespace(name, namespace_decl);
679
680
469
      if (found_namespace_decl) {
681
60
        context.m_namespace_map->push_back(
682
60
            std::pair<lldb::ModuleSP, CompilerDeclContext>(
683
60
                module_sp, found_namespace_decl));
684
685
60
        LLDB_LOG(log, "  CAS::FEVD Found namespace {0} in module {1}", name,
686
60
                 module_sp->GetFileSpec().GetFilename());
687
60
      }
688
469
    }
689
469
    return;
690
469
  }
691
692
475k
  
for (lldb::ModuleSP image : m_target->GetImages().Modules())9.76k
{
693
475k
    if (!image)
694
0
      continue;
695
696
475k
    CompilerDeclContext found_namespace_decl;
697
698
475k
    SymbolFile *symbol_file = image->GetSymbolFile();
699
700
475k
    if (!symbol_file)
701
831
      continue;
702
703
    // If namespace_decl is not valid, 'FindNamespace' would look for
704
    // any namespace called 'name' (ignoring parent contexts) and return
705
    // the first one it finds. Thus if we're doing a qualified lookup only
706
    // consider root namespaces. E.g., in an expression ::A::B::Foo, the
707
    // lookup of ::A will result in a qualified lookup. Note, namespace
708
    // disambiguation for function calls are handled separately in
709
    // SearchFunctionsInSymbolContexts.
710
474k
    const bool find_root_namespaces =
711
474k
        context.m_decl_context &&
712
474k
        context.m_decl_context->shouldUseQualifiedLookup();
713
474k
    found_namespace_decl = symbol_file->FindNamespace(
714
474k
        name, namespace_decl, /* only root namespaces */ find_root_namespaces);
715
716
474k
    if (found_namespace_decl) {
717
174
      context.m_namespace_map->push_back(
718
174
          std::pair<lldb::ModuleSP, CompilerDeclContext>(image,
719
174
                                                         found_namespace_decl));
720
721
174
      LLDB_LOG(log, "  CAS::FEVD Found namespace {0} in module {1}", name,
722
174
               image->GetFileSpec().GetFilename());
723
174
    }
724
474k
  }
725
9.76k
}
726
727
template <class D> class TaggedASTDecl {
728
public:
729
12.8k
  TaggedASTDecl() : decl(nullptr) {}
Unexecuted instantiation: TaggedASTDecl<clang::ObjCPropertyDecl>::TaggedASTDecl()
Unexecuted instantiation: TaggedASTDecl<clang::ObjCIvarDecl>::TaggedASTDecl()
Unexecuted instantiation: TaggedASTDecl<clang::ObjCInterfaceDecl const>::TaggedASTDecl()
TaggedASTDecl<clang::RecordDecl const>::TaggedASTDecl()
Line
Count
Source
729
12.8k
  TaggedASTDecl() : decl(nullptr) {}
Unexecuted instantiation: TaggedASTDecl<clang::FieldDecl>::TaggedASTDecl()
Unexecuted instantiation: TaggedASTDecl<clang::CXXRecordDecl>::TaggedASTDecl()
730
167k
  TaggedASTDecl(D *_decl) : decl(_decl) {}
TaggedASTDecl<clang::ObjCPropertyDecl>::TaggedASTDecl(clang::ObjCPropertyDecl*)
Line
Count
Source
730
983
  TaggedASTDecl(D *_decl) : decl(_decl) {}
TaggedASTDecl<clang::Decl>::TaggedASTDecl(clang::Decl*)
Line
Count
Source
730
27.6k
  TaggedASTDecl(D *_decl) : decl(_decl) {}
TaggedASTDecl<clang::ObjCIvarDecl>::TaggedASTDecl(clang::ObjCIvarDecl*)
Line
Count
Source
730
1.01k
  TaggedASTDecl(D *_decl) : decl(_decl) {}
TaggedASTDecl<clang::ObjCInterfaceDecl const>::TaggedASTDecl(clang::ObjCInterfaceDecl const*)
Line
Count
Source
730
1.68k
  TaggedASTDecl(D *_decl) : decl(_decl) {}
TaggedASTDecl<clang::RecordDecl const>::TaggedASTDecl(clang::RecordDecl const*)
Line
Count
Source
730
47.4k
  TaggedASTDecl(D *_decl) : decl(_decl) {}
TaggedASTDecl<clang::CXXRecordDecl const>::TaggedASTDecl(clang::CXXRecordDecl const*)
Line
Count
Source
730
17.2k
  TaggedASTDecl(D *_decl) : decl(_decl) {}
TaggedASTDecl<clang::RecordDecl>::TaggedASTDecl(clang::RecordDecl*)
Line
Count
Source
730
8.33k
  TaggedASTDecl(D *_decl) : decl(_decl) {}
TaggedASTDecl<clang::CXXRecordDecl>::TaggedASTDecl(clang::CXXRecordDecl*)
Line
Count
Source
730
25.0k
  TaggedASTDecl(D *_decl) : decl(_decl) {}
TaggedASTDecl<clang::FieldDecl>::TaggedASTDecl(clang::FieldDecl*)
Line
Count
Source
730
38.3k
  TaggedASTDecl(D *_decl) : decl(_decl) {}
731
122k
  bool IsValid() const { return (decl != nullptr); }
TaggedASTDecl<clang::ObjCPropertyDecl>::IsValid() const
Line
Count
Source
731
983
  bool IsValid() const { return (decl != nullptr); }
TaggedASTDecl<clang::Decl>::IsValid() const
Line
Count
Source
731
27.6k
  bool IsValid() const { return (decl != nullptr); }
TaggedASTDecl<clang::ObjCIvarDecl>::IsValid() const
Line
Count
Source
731
1.01k
  bool IsValid() const { return (decl != nullptr); }
TaggedASTDecl<clang::ObjCInterfaceDecl const>::IsValid() const
Line
Count
Source
731
1.35k
  bool IsValid() const { return (decl != nullptr); }
TaggedASTDecl<clang::RecordDecl const>::IsValid() const
Line
Count
Source
731
30.1k
  bool IsValid() const { return (decl != nullptr); }
TaggedASTDecl<clang::CXXRecordDecl const>::IsValid() const
Line
Count
Source
731
17.2k
  bool IsValid() const { return (decl != nullptr); }
TaggedASTDecl<clang::RecordDecl>::IsValid() const
Line
Count
Source
731
8.33k
  bool IsValid() const { return (decl != nullptr); }
TaggedASTDecl<clang::CXXRecordDecl>::IsValid() const
Line
Count
Source
731
16.6k
  bool IsValid() const { return (decl != nullptr); }
TaggedASTDecl<clang::FieldDecl>::IsValid() const
Line
Count
Source
731
19.1k
  bool IsValid() const { return (decl != nullptr); }
732
102k
  bool IsInvalid() const { return !IsValid(); }
TaggedASTDecl<clang::ObjCInterfaceDecl const>::IsInvalid() const
Line
Count
Source
732
947
  bool IsInvalid() const { return !IsValid(); }
TaggedASTDecl<clang::Decl>::IsInvalid() const
Line
Count
Source
732
27.6k
  bool IsInvalid() const { return !IsValid(); }
TaggedASTDecl<clang::RecordDecl const>::IsInvalid() const
Line
Count
Source
732
30.1k
  bool IsInvalid() const { return !IsValid(); }
TaggedASTDecl<clang::RecordDecl>::IsInvalid() const
Line
Count
Source
732
8.33k
  bool IsInvalid() const { return !IsValid(); }
TaggedASTDecl<clang::CXXRecordDecl>::IsInvalid() const
Line
Count
Source
732
16.6k
  bool IsInvalid() const { return !IsValid(); }
TaggedASTDecl<clang::FieldDecl>::IsInvalid() const
Line
Count
Source
732
19.1k
  bool IsInvalid() const { return !IsValid(); }
733
141k
  D *operator->() const { return decl; }
TaggedASTDecl<clang::ObjCInterfaceDecl const>::operator->() const
Line
Count
Source
733
3.36k
  D *operator->() const { return decl; }
TaggedASTDecl<clang::RecordDecl const>::operator->() const
Line
Count
Source
733
69.1k
  D *operator->() const { return decl; }
TaggedASTDecl<clang::CXXRecordDecl const>::operator->() const
Line
Count
Source
733
69.1k
  D *operator->() const { return decl; }
734
  D *decl;
735
};
736
737
template <class D2, template <class D> class TD, class D1>
738
25.6k
TD<D2> DynCast(TD<D1> source) {
739
25.6k
  return TD<D2>(dyn_cast<D2>(source.decl));
740
25.6k
}
DeclFromUser<clang::CXXRecordDecl const> DynCast<clang::CXXRecordDecl const, DeclFromUser, clang::RecordDecl const>(clang::CXXRecordDecl const<clang::RecordDecl const>)
Line
Count
Source
738
17.2k
TD<D2> DynCast(TD<D1> source) {
739
17.2k
  return TD<D2>(dyn_cast<D2>(source.decl));
740
17.2k
}
DeclFromUser<clang::CXXRecordDecl> DynCast<clang::CXXRecordDecl, DeclFromUser, clang::RecordDecl>(clang::CXXRecordDecl<clang::RecordDecl>)
Line
Count
Source
738
8.33k
TD<D2> DynCast(TD<D1> source) {
739
8.33k
  return TD<D2>(dyn_cast<D2>(source.decl));
740
8.33k
}
Unexecuted instantiation: DeclFromParser<clang::CXXRecordDecl const> DynCast<clang::CXXRecordDecl const, DeclFromParser, clang::RecordDecl const>(clang::CXXRecordDecl const<clang::RecordDecl const>)
Unexecuted instantiation: DeclFromParser<clang::CXXRecordDecl> DynCast<clang::CXXRecordDecl, DeclFromParser, clang::RecordDecl>(clang::CXXRecordDecl<clang::RecordDecl>)
741
742
template <class D = Decl> class DeclFromParser;
743
template <class D = Decl> class DeclFromUser;
744
745
template <class D> class DeclFromParser : public TaggedASTDecl<D> {
746
public:
747
0
  DeclFromParser() : TaggedASTDecl<D>() {}
Unexecuted instantiation: DeclFromParser<clang::ObjCPropertyDecl>::DeclFromParser()
Unexecuted instantiation: DeclFromParser<clang::ObjCIvarDecl>::DeclFromParser()
Unexecuted instantiation: DeclFromParser<clang::FieldDecl>::DeclFromParser()
Unexecuted instantiation: DeclFromParser<clang::CXXRecordDecl>::DeclFromParser()
748
85.8k
  DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromParser<clang::Decl>::DeclFromParser(clang::Decl*)
Line
Count
Source
748
27.6k
  DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromParser<clang::ObjCPropertyDecl>::DeclFromParser(clang::ObjCPropertyDecl*)
Line
Count
Source
748
36
  DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromParser<clang::ObjCIvarDecl>::DeclFromParser(clang::ObjCIvarDecl*)
Line
Count
Source
748
71
  DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromParser<clang::ObjCInterfaceDecl const>::DeclFromParser(clang::ObjCInterfaceDecl const*)
Line
Count
Source
748
523
  DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromParser<clang::RecordDecl const>::DeclFromParser(clang::RecordDecl const*)
Line
Count
Source
748
30.1k
  DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromParser<clang::FieldDecl>::DeclFromParser(clang::FieldDecl*)
Line
Count
Source
748
19.1k
  DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromParser<clang::CXXRecordDecl>::DeclFromParser(clang::CXXRecordDecl*)
Line
Count
Source
748
8.33k
  DeclFromParser(D *_decl) : TaggedASTDecl<D>(_decl) {}
Unexecuted instantiation: DeclFromParser<clang::CXXRecordDecl const>::DeclFromParser(clang::CXXRecordDecl const*)
Unexecuted instantiation: DeclFromParser<clang::RecordDecl>::DeclFromParser(clang::RecordDecl*)
749
750
  DeclFromUser<D> GetOrigin(ClangASTSource &source);
751
};
752
753
template <class D> class DeclFromUser : public TaggedASTDecl<D> {
754
public:
755
12.8k
  DeclFromUser() : TaggedASTDecl<D>() {}
Unexecuted instantiation: DeclFromUser<clang::ObjCInterfaceDecl const>::DeclFromUser()
DeclFromUser<clang::RecordDecl const>::DeclFromUser()
Line
Count
Source
755
12.8k
  DeclFromUser() : TaggedASTDecl<D>() {}
756
81.8k
  DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromUser<clang::ObjCPropertyDecl>::DeclFromUser(clang::ObjCPropertyDecl*)
Line
Count
Source
756
947
  DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromUser<clang::ObjCIvarDecl>::DeclFromUser(clang::ObjCIvarDecl*)
Line
Count
Source
756
947
  DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromUser<clang::ObjCInterfaceDecl const>::DeclFromUser(clang::ObjCInterfaceDecl const*)
Line
Count
Source
756
1.16k
  DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromUser<clang::RecordDecl const>::DeclFromUser(clang::RecordDecl const*)
Line
Count
Source
756
17.2k
  DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromUser<clang::CXXRecordDecl const>::DeclFromUser(clang::CXXRecordDecl const*)
Line
Count
Source
756
17.2k
  DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromUser<clang::RecordDecl>::DeclFromUser(clang::RecordDecl*)
Line
Count
Source
756
8.33k
  DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromUser<clang::CXXRecordDecl>::DeclFromUser(clang::CXXRecordDecl*)
Line
Count
Source
756
16.6k
  DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {}
DeclFromUser<clang::FieldDecl>::DeclFromUser(clang::FieldDecl*)
Line
Count
Source
756
19.1k
  DeclFromUser(D *_decl) : TaggedASTDecl<D>(_decl) {}
757
758
  DeclFromParser<D> Import(ClangASTSource &source);
759
};
760
761
template <class D>
762
30.6k
DeclFromUser<D> DeclFromParser<D>::GetOrigin(ClangASTSource &source) {
763
30.6k
  ClangASTImporter::DeclOrigin origin = source.GetDeclOrigin(this->decl);
764
30.6k
  if (!origin.Valid())
765
12.8k
    return DeclFromUser<D>();
766
17.8k
  return DeclFromUser<D>(dyn_cast<D>(origin.decl));
767
30.6k
}
DeclFromParser<clang::ObjCInterfaceDecl const>::GetOrigin(lldb_private::ClangASTSource&)
Line
Count
Source
762
523
DeclFromUser<D> DeclFromParser<D>::GetOrigin(ClangASTSource &source) {
763
523
  ClangASTImporter::DeclOrigin origin = source.GetDeclOrigin(this->decl);
764
523
  if (!origin.Valid())
765
0
    return DeclFromUser<D>();
766
523
  return DeclFromUser<D>(dyn_cast<D>(origin.decl));
767
523
}
DeclFromParser<clang::RecordDecl const>::GetOrigin(lldb_private::ClangASTSource&)
Line
Count
Source
762
30.1k
DeclFromUser<D> DeclFromParser<D>::GetOrigin(ClangASTSource &source) {
763
30.1k
  ClangASTImporter::DeclOrigin origin = source.GetDeclOrigin(this->decl);
764
30.1k
  if (!origin.Valid())
765
12.8k
    return DeclFromUser<D>();
766
17.2k
  return DeclFromUser<D>(dyn_cast<D>(origin.decl));
767
30.1k
}
768
769
template <class D>
770
27.6k
DeclFromParser<D> DeclFromUser<D>::Import(ClangASTSource &source) {
771
27.6k
  DeclFromParser<> parser_generic_decl(source.CopyDecl(this->decl));
772
27.6k
  if (parser_generic_decl.IsInvalid())
773
0
    return DeclFromParser<D>();
774
27.6k
  return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
775
27.6k
}
DeclFromUser<clang::ObjCPropertyDecl>::Import(lldb_private::ClangASTSource&)
Line
Count
Source
770
36
DeclFromParser<D> DeclFromUser<D>::Import(ClangASTSource &source) {
771
36
  DeclFromParser<> parser_generic_decl(source.CopyDecl(this->decl));
772
36
  if (parser_generic_decl.IsInvalid())
773
0
    return DeclFromParser<D>();
774
36
  return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
775
36
}
DeclFromUser<clang::ObjCIvarDecl>::Import(lldb_private::ClangASTSource&)
Line
Count
Source
770
71
DeclFromParser<D> DeclFromUser<D>::Import(ClangASTSource &source) {
771
71
  DeclFromParser<> parser_generic_decl(source.CopyDecl(this->decl));
772
71
  if (parser_generic_decl.IsInvalid())
773
0
    return DeclFromParser<D>();
774
71
  return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
775
71
}
DeclFromUser<clang::FieldDecl>::Import(lldb_private::ClangASTSource&)
Line
Count
Source
770
19.1k
DeclFromParser<D> DeclFromUser<D>::Import(ClangASTSource &source) {
771
19.1k
  DeclFromParser<> parser_generic_decl(source.CopyDecl(this->decl));
772
19.1k
  if (parser_generic_decl.IsInvalid())
773
0
    return DeclFromParser<D>();
774
19.1k
  return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
775
19.1k
}
DeclFromUser<clang::CXXRecordDecl>::Import(lldb_private::ClangASTSource&)
Line
Count
Source
770
8.33k
DeclFromParser<D> DeclFromUser<D>::Import(ClangASTSource &source) {
771
8.33k
  DeclFromParser<> parser_generic_decl(source.CopyDecl(this->decl));
772
8.33k
  if (parser_generic_decl.IsInvalid())
773
0
    return DeclFromParser<D>();
774
8.33k
  return DeclFromParser<D>(dyn_cast<D>(parser_generic_decl.decl));
775
8.33k
}
776
777
bool ClangASTSource::FindObjCMethodDeclsWithOrigin(
778
    NameSearchContext &context, ObjCInterfaceDecl *original_interface_decl,
779
573
    const char *log_info) {
780
573
  const DeclarationName &decl_name(context.m_decl_name);
781
573
  clang::ASTContext *original_ctx = &original_interface_decl->getASTContext();
782
783
573
  Selector original_selector;
784
785
573
  if (decl_name.isObjCZeroArgSelector()) {
786
202
    IdentifierInfo *ident = &original_ctx->Idents.get(decl_name.getAsString());
787
202
    original_selector = original_ctx->Selectors.getSelector(0, &ident);
788
371
  } else if (decl_name.isObjCOneArgSelector()) {
789
349
    const std::string &decl_name_string = decl_name.getAsString();
790
349
    std::string decl_name_string_without_colon(decl_name_string.c_str(),
791
349
                                               decl_name_string.length() - 1);
792
349
    IdentifierInfo *ident =
793
349
        &original_ctx->Idents.get(decl_name_string_without_colon);
794
349
    original_selector = original_ctx->Selectors.getSelector(1, &ident);
795
349
  } else {
796
22
    SmallVector<IdentifierInfo *, 4> idents;
797
798
22
    clang::Selector sel = decl_name.getObjCSelector();
799
800
22
    unsigned num_args = sel.getNumArgs();
801
802
72
    for (unsigned i = 0; i != num_args; 
++i50
) {
803
50
      idents.push_back(&original_ctx->Idents.get(sel.getNameForSlot(i)));
804
50
    }
805
806
22
    original_selector =
807
22
        original_ctx->Selectors.getSelector(num_args, idents.data());
808
22
  }
809
810
573
  DeclarationName original_decl_name(original_selector);
811
812
573
  llvm::SmallVector<NamedDecl *, 1> methods;
813
814
573
  TypeSystemClang::GetCompleteDecl(original_ctx, original_interface_decl);
815
816
573
  if (ObjCMethodDecl *instance_method_decl =
817
573
          original_interface_decl->lookupInstanceMethod(original_selector)) {
818
241
    methods.push_back(instance_method_decl);
819
332
  } else if (ObjCMethodDecl *class_method_decl =
820
332
                 original_interface_decl->lookupClassMethod(
821
332
                     original_selector)) {
822
62
    methods.push_back(class_method_decl);
823
62
  }
824
825
573
  if (methods.empty()) {
826
270
    return false;
827
270
  }
828
829
303
  for (NamedDecl *named_decl : methods) {
830
303
    if (!named_decl)
831
0
      continue;
832
833
303
    ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(named_decl);
834
835
303
    if (!result_method)
836
0
      continue;
837
838
303
    Decl *copied_decl = CopyDecl(result_method);
839
840
303
    if (!copied_decl)
841
0
      continue;
842
843
303
    ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(copied_decl);
844
845
303
    if (!copied_method_decl)
846
0
      continue;
847
848
303
    Log *log = GetLog(LLDBLog::Expressions);
849
850
303
    LLDB_LOG(log, "  CAS::FOMD found ({0}) {1}", log_info,
851
303
             ClangUtil::DumpDecl(copied_method_decl));
852
853
303
    context.AddNamedDecl(copied_method_decl);
854
303
  }
855
856
303
  return true;
857
573
}
858
859
void ClangASTSource::FindDeclInModules(NameSearchContext &context,
860
9.59k
                                       ConstString name) {
861
9.59k
  Log *log = GetLog(LLDBLog::Expressions);
862
863
9.59k
  std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
864
9.59k
      GetClangModulesDeclVendor();
865
9.59k
  if (!modules_decl_vendor)
866
11
    return;
867
868
9.58k
  bool append = false;
869
9.58k
  uint32_t max_matches = 1;
870
9.58k
  std::vector<clang::NamedDecl *> decls;
871
872
9.58k
  if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
873
9.55k
    return;
874
875
27
  LLDB_LOG(log, "  CAS::FEVD Matching entity found for \"{0}\" in the modules",
876
27
           name);
877
878
27
  clang::NamedDecl *const decl_from_modules = decls[0];
879
880
27
  if (llvm::isa<clang::TypeDecl>(decl_from_modules) ||
881
27
      
llvm::isa<clang::ObjCContainerDecl>(decl_from_modules)25
||
882
27
      
llvm::isa<clang::EnumConstantDecl>(decl_from_modules)19
) {
883
8
    clang::Decl *copied_decl = CopyDecl(decl_from_modules);
884
8
    clang::NamedDecl *copied_named_decl =
885
8
        copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : 
nullptr0
;
886
887
8
    if (!copied_named_decl) {
888
0
      LLDB_LOG(log, "  CAS::FEVD - Couldn't export a type from the modules");
889
890
0
      return;
891
0
    }
892
893
8
    context.AddNamedDecl(copied_named_decl);
894
895
8
    context.m_found_type = true;
896
8
  }
897
27
}
898
899
void ClangASTSource::FindDeclInObjCRuntime(NameSearchContext &context,
900
9.58k
                                           ConstString name) {
901
9.58k
  Log *log = GetLog(LLDBLog::Expressions);
902
903
9.58k
  lldb::ProcessSP process(m_target->GetProcessSP());
904
905
9.58k
  if (!process)
906
331
    return;
907
908
9.25k
  ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
909
910
9.25k
  if (!language_runtime)
911
2
    return;
912
913
9.25k
  DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
914
915
9.25k
  if (!decl_vendor)
916
0
    return;
917
918
9.25k
  bool append = false;
919
9.25k
  uint32_t max_matches = 1;
920
9.25k
  std::vector<clang::NamedDecl *> decls;
921
922
9.25k
  auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor);
923
9.25k
  if (!clang_decl_vendor->FindDecls(name, append, max_matches, decls))
924
9.17k
    return;
925
926
78
  LLDB_LOG(log, "  CAS::FEVD Matching type found for \"{0}\" in the runtime",
927
78
           name);
928
929
78
  clang::Decl *copied_decl = CopyDecl(decls[0]);
930
78
  clang::NamedDecl *copied_named_decl =
931
78
      copied_decl ? dyn_cast<clang::NamedDecl>(copied_decl) : 
nullptr0
;
932
933
78
  if (!copied_named_decl) {
934
0
    LLDB_LOG(log, "  CAS::FEVD - Couldn't export a type from the runtime");
935
936
0
    return;
937
0
  }
938
939
78
  context.AddNamedDecl(copied_named_decl);
940
78
}
941
942
497
void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) {
943
497
  Log *log = GetLog(LLDBLog::Expressions);
944
945
497
  const DeclarationName &decl_name(context.m_decl_name);
946
497
  const DeclContext *decl_ctx(context.m_decl_context);
947
948
497
  const ObjCInterfaceDecl *interface_decl =
949
497
      dyn_cast<ObjCInterfaceDecl>(decl_ctx);
950
951
497
  if (!interface_decl)
952
1
    return;
953
954
496
  do {
955
496
    ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl);
956
957
496
    if (!original.Valid())
958
0
      break;
959
960
496
    ObjCInterfaceDecl *original_interface_decl =
961
496
        dyn_cast<ObjCInterfaceDecl>(original.decl);
962
963
496
    if (FindObjCMethodDeclsWithOrigin(context, original_interface_decl,
964
496
                                      "at origin"))
965
238
      return; // found it, no need to look any further
966
496
  } while (
false258
);
967
968
258
  StreamString ss;
969
970
258
  if (decl_name.isObjCZeroArgSelector()) {
971
21
    ss.Printf("%s", decl_name.getAsString().c_str());
972
237
  } else if (decl_name.isObjCOneArgSelector()) {
973
227
    ss.Printf("%s", decl_name.getAsString().c_str());
974
227
  } else {
975
10
    clang::Selector sel = decl_name.getObjCSelector();
976
977
32
    for (unsigned i = 0, e = sel.getNumArgs(); i != e; 
++i22
) {
978
22
      llvm::StringRef r = sel.getNameForSlot(i);
979
22
      ss.Printf("%s:", r.str().c_str());
980
22
    }
981
10
  }
982
258
  ss.Flush();
983
984
258
  if (ss.GetString().contains("$__lldb"))
985
182
    return; // we don't need any results
986
987
76
  ConstString selector_name(ss.GetString());
988
989
76
  LLDB_LOG(log,
990
76
           "ClangASTSource::FindObjCMethodDecls on (ASTContext*){0} '{1}' "
991
76
           "for selector [{2} {3}]",
992
76
           m_ast_context, m_clang_ast_context->getDisplayName(),
993
76
           interface_decl->getName(), selector_name);
994
76
  SymbolContextList sc_list;
995
996
76
  ModuleFunctionSearchOptions function_options;
997
76
  function_options.include_symbols = false;
998
76
  function_options.include_inlines = false;
999
1000
76
  std::string interface_name = interface_decl->getNameAsString();
1001
1002
76
  do {
1003
76
    StreamString ms;
1004
76
    ms.Printf("-[%s %s]", interface_name.c_str(), selector_name.AsCString());
1005
76
    ms.Flush();
1006
76
    ConstString instance_method_name(ms.GetString());
1007
1008
76
    sc_list.Clear();
1009
76
    m_target->GetImages().FindFunctions(instance_method_name,
1010
76
                                        lldb::eFunctionNameTypeFull,
1011
76
                                        function_options, sc_list);
1012
1013
76
    if (sc_list.GetSize())
1014
0
      break;
1015
1016
76
    ms.Clear();
1017
76
    ms.Printf("+[%s %s]", interface_name.c_str(), selector_name.AsCString());
1018
76
    ms.Flush();
1019
76
    ConstString class_method_name(ms.GetString());
1020
1021
76
    sc_list.Clear();
1022
76
    m_target->GetImages().FindFunctions(class_method_name,
1023
76
                                        lldb::eFunctionNameTypeFull,
1024
76
                                        function_options, sc_list);
1025
1026
76
    if (sc_list.GetSize())
1027
0
      break;
1028
1029
    // Fall back and check for methods in categories.  If we find methods this
1030
    // way, we need to check that they're actually in categories on the desired
1031
    // class.
1032
1033
76
    SymbolContextList candidate_sc_list;
1034
1035
76
    m_target->GetImages().FindFunctions(selector_name,
1036
76
                                        lldb::eFunctionNameTypeSelector,
1037
76
                                        function_options, candidate_sc_list);
1038
1039
76
    for (const SymbolContext &candidate_sc : candidate_sc_list) {
1040
4
      if (!candidate_sc.function)
1041
0
        continue;
1042
1043
4
      const char *candidate_name = candidate_sc.function->GetName().AsCString();
1044
1045
4
      const char *cursor = candidate_name;
1046
1047
4
      if (*cursor != '+' && *cursor != '-')
1048
0
        continue;
1049
1050
4
      ++cursor;
1051
1052
4
      if (*cursor != '[')
1053
0
        continue;
1054
1055
4
      ++cursor;
1056
1057
4
      size_t interface_len = interface_name.length();
1058
1059
4
      if (strncmp(cursor, interface_name.c_str(), interface_len))
1060
4
        continue;
1061
1062
0
      cursor += interface_len;
1063
1064
0
      if (*cursor == ' ' || *cursor == '(')
1065
0
        sc_list.Append(candidate_sc);
1066
0
    }
1067
76
  } while (false);
1068
1069
76
  if (sc_list.GetSize()) {
1070
    // We found a good function symbol.  Use that.
1071
1072
0
    for (const SymbolContext &sc : sc_list) {
1073
0
      if (!sc.function)
1074
0
        continue;
1075
1076
0
      CompilerDeclContext function_decl_ctx = sc.function->GetDeclContext();
1077
0
      if (!function_decl_ctx)
1078
0
        continue;
1079
1080
0
      ObjCMethodDecl *method_decl =
1081
0
          TypeSystemClang::DeclContextGetAsObjCMethodDecl(function_decl_ctx);
1082
1083
0
      if (!method_decl)
1084
0
        continue;
1085
1086
0
      ObjCInterfaceDecl *found_interface_decl =
1087
0
          method_decl->getClassInterface();
1088
1089
0
      if (!found_interface_decl)
1090
0
        continue;
1091
1092
0
      if (found_interface_decl->getName() == interface_decl->getName()) {
1093
0
        Decl *copied_decl = CopyDecl(method_decl);
1094
1095
0
        if (!copied_decl)
1096
0
          continue;
1097
1098
0
        ObjCMethodDecl *copied_method_decl =
1099
0
            dyn_cast<ObjCMethodDecl>(copied_decl);
1100
1101
0
        if (!copied_method_decl)
1102
0
          continue;
1103
1104
0
        LLDB_LOG(log, "  CAS::FOMD found (in symbols)\n{0}",
1105
0
                 ClangUtil::DumpDecl(copied_method_decl));
1106
1107
0
        context.AddNamedDecl(copied_method_decl);
1108
0
      }
1109
0
    }
1110
1111
0
    return;
1112
0
  }
1113
1114
  // Try the debug information.
1115
1116
76
  do {
1117
76
    ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(
1118
76
        const_cast<ObjCInterfaceDecl *>(interface_decl));
1119
1120
76
    if (!complete_interface_decl)
1121
71
      break;
1122
1123
    // We found the complete interface.  The runtime never needs to be queried
1124
    // in this scenario.
1125
1126
5
    DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(
1127
5
        complete_interface_decl);
1128
1129
5
    if (complete_interface_decl == interface_decl)
1130
0
      break; // already checked this one
1131
1132
5
    LLDB_LOG(log,
1133
5
             "CAS::FOPD trying origin "
1134
5
             "(ObjCInterfaceDecl*){0}/(ASTContext*){1}...",
1135
5
             complete_interface_decl, &complete_iface_decl->getASTContext());
1136
1137
5
    FindObjCMethodDeclsWithOrigin(context, complete_interface_decl,
1138
5
                                  "in debug info");
1139
1140
5
    return;
1141
5
  } while (
false0
);
1142
1143
71
  do {
1144
    // Check the modules only if the debug information didn't have a complete
1145
    // interface.
1146
1147
71
    if (std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1148
71
            GetClangModulesDeclVendor()) {
1149
71
      ConstString interface_name(interface_decl->getNameAsString().c_str());
1150
71
      bool append = false;
1151
71
      uint32_t max_matches = 1;
1152
71
      std::vector<clang::NamedDecl *> decls;
1153
1154
71
      if (!modules_decl_vendor->FindDecls(interface_name, append, max_matches,
1155
71
                                          decls))
1156
68
        break;
1157
1158
3
      ObjCInterfaceDecl *interface_decl_from_modules =
1159
3
          dyn_cast<ObjCInterfaceDecl>(decls[0]);
1160
1161
3
      if (!interface_decl_from_modules)
1162
0
        break;
1163
1164
3
      if (FindObjCMethodDeclsWithOrigin(context, interface_decl_from_modules,
1165
3
                                        "in modules"))
1166
2
        return;
1167
3
    }
1168
71
  } while (
false1
);
1169
1170
69
  do {
1171
    // Check the runtime only if the debug information didn't have a complete
1172
    // interface and the modules don't get us anywhere.
1173
1174
69
    lldb::ProcessSP process(m_target->GetProcessSP());
1175
1176
69
    if (!process)
1177
0
      break;
1178
1179
69
    ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
1180
1181
69
    if (!language_runtime)
1182
0
      break;
1183
1184
69
    DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
1185
1186
69
    if (!decl_vendor)
1187
0
      break;
1188
1189
69
    ConstString interface_name(interface_decl->getNameAsString().c_str());
1190
69
    bool append = false;
1191
69
    uint32_t max_matches = 1;
1192
69
    std::vector<clang::NamedDecl *> decls;
1193
1194
69
    auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor);
1195
69
    if (!clang_decl_vendor->FindDecls(interface_name, append, max_matches,
1196
69
                                      decls))
1197
0
      break;
1198
1199
69
    ObjCInterfaceDecl *runtime_interface_decl =
1200
69
        dyn_cast<ObjCInterfaceDecl>(decls[0]);
1201
1202
69
    if (!runtime_interface_decl)
1203
0
      break;
1204
1205
69
    FindObjCMethodDeclsWithOrigin(context, runtime_interface_decl,
1206
69
                                  "in runtime");
1207
69
  } while (false);
1208
69
}
1209
1210
static bool FindObjCPropertyAndIvarDeclsWithOrigin(
1211
    NameSearchContext &context, ClangASTSource &source,
1212
947
    DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl) {
1213
947
  Log *log = GetLog(LLDBLog::Expressions);
1214
1215
947
  if (origin_iface_decl.IsInvalid())
1216
0
    return false;
1217
1218
947
  std::string name_str = context.m_decl_name.getAsString();
1219
947
  StringRef name(name_str);
1220
947
  IdentifierInfo &name_identifier(
1221
947
      origin_iface_decl->getASTContext().Idents.get(name));
1222
1223
947
  DeclFromUser<ObjCPropertyDecl> origin_property_decl(
1224
947
      origin_iface_decl->FindPropertyDeclaration(
1225
947
          &name_identifier, ObjCPropertyQueryKind::OBJC_PR_query_instance));
1226
1227
947
  bool found = false;
1228
1229
947
  if (origin_property_decl.IsValid()) {
1230
36
    DeclFromParser<ObjCPropertyDecl> parser_property_decl(
1231
36
        origin_property_decl.Import(source));
1232
36
    if (parser_property_decl.IsValid()) {
1233
36
      LLDB_LOG(log, "  CAS::FOPD found\n{0}",
1234
36
               ClangUtil::DumpDecl(parser_property_decl.decl));
1235
1236
36
      context.AddNamedDecl(parser_property_decl.decl);
1237
36
      found = true;
1238
36
    }
1239
36
  }
1240
1241
947
  DeclFromUser<ObjCIvarDecl> origin_ivar_decl(
1242
947
      origin_iface_decl->getIvarDecl(&name_identifier));
1243
1244
947
  if (origin_ivar_decl.IsValid()) {
1245
71
    DeclFromParser<ObjCIvarDecl> parser_ivar_decl(
1246
71
        origin_ivar_decl.Import(source));
1247
71
    if (parser_ivar_decl.IsValid()) {
1248
71
      LLDB_LOG(log, "  CAS::FOPD found\n{0}",
1249
71
               ClangUtil::DumpDecl(parser_ivar_decl.decl));
1250
1251
71
      context.AddNamedDecl(parser_ivar_decl.decl);
1252
71
      found = true;
1253
71
    }
1254
71
  }
1255
1256
947
  return found;
1257
947
}
1258
1259
523
void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) {
1260
523
  Log *log = GetLog(LLDBLog::Expressions);
1261
1262
523
  DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl(
1263
523
      cast<ObjCInterfaceDecl>(context.m_decl_context));
1264
523
  DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl(
1265
523
      parser_iface_decl.GetOrigin(*this));
1266
1267
523
  ConstString class_name(parser_iface_decl->getNameAsString().c_str());
1268
1269
523
  LLDB_LOG(log,
1270
523
           "ClangASTSource::FindObjCPropertyAndIvarDecls on "
1271
523
           "(ASTContext*){0} '{1}' for '{2}.{3}'",
1272
523
           m_ast_context, m_clang_ast_context->getDisplayName(),
1273
523
           parser_iface_decl->getName(), context.m_decl_name.getAsString());
1274
1275
523
  if (FindObjCPropertyAndIvarDeclsWithOrigin(context, *this, origin_iface_decl))
1276
101
    return;
1277
1278
422
  LLDB_LOG(log,
1279
422
           "CAS::FOPD couldn't find the property on origin "
1280
422
           "(ObjCInterfaceDecl*){0}/(ASTContext*){1}, searching "
1281
422
           "elsewhere...",
1282
422
           origin_iface_decl.decl, &origin_iface_decl->getASTContext());
1283
1284
422
  SymbolContext null_sc;
1285
422
  TypeList type_list;
1286
1287
422
  do {
1288
422
    ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface(
1289
422
        const_cast<ObjCInterfaceDecl *>(parser_iface_decl.decl));
1290
1291
422
    if (!complete_interface_decl)
1292
194
      break;
1293
1294
    // We found the complete interface.  The runtime never needs to be queried
1295
    // in this scenario.
1296
1297
228
    DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl(
1298
228
        complete_interface_decl);
1299
1300
228
    if (complete_iface_decl.decl == origin_iface_decl.decl)
1301
210
      break; // already checked this one
1302
1303
18
    LLDB_LOG(log,
1304
18
             "CAS::FOPD trying origin "
1305
18
             "(ObjCInterfaceDecl*){0}/(ASTContext*){1}...",
1306
18
             complete_iface_decl.decl, &complete_iface_decl->getASTContext());
1307
1308
18
    FindObjCPropertyAndIvarDeclsWithOrigin(context, *this, complete_iface_decl);
1309
1310
18
    return;
1311
228
  } while (
false0
);
1312
1313
404
  do {
1314
    // Check the modules only if the debug information didn't have a complete
1315
    // interface.
1316
1317
404
    std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1318
404
        GetClangModulesDeclVendor();
1319
1320
404
    if (!modules_decl_vendor)
1321
0
      break;
1322
1323
404
    bool append = false;
1324
404
    uint32_t max_matches = 1;
1325
404
    std::vector<clang::NamedDecl *> decls;
1326
1327
404
    if (!modules_decl_vendor->FindDecls(class_name, append, max_matches, decls))
1328
400
      break;
1329
1330
4
    DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_modules(
1331
4
        dyn_cast<ObjCInterfaceDecl>(decls[0]));
1332
1333
4
    if (!interface_decl_from_modules.IsValid())
1334
0
      break;
1335
1336
4
    LLDB_LOG(log,
1337
4
             "CAS::FOPD[{0}] trying module "
1338
4
             "(ObjCInterfaceDecl*){0}/(ASTContext*){1}...",
1339
4
             interface_decl_from_modules.decl,
1340
4
             &interface_decl_from_modules->getASTContext());
1341
1342
4
    if (FindObjCPropertyAndIvarDeclsWithOrigin(context, *this,
1343
4
                                               interface_decl_from_modules))
1344
2
      return;
1345
4
  } while (
false2
);
1346
1347
402
  do {
1348
    // Check the runtime only if the debug information didn't have a complete
1349
    // interface and nothing was in the modules.
1350
1351
402
    lldb::ProcessSP process(m_target->GetProcessSP());
1352
1353
402
    if (!process)
1354
0
      return;
1355
1356
402
    ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(*process));
1357
1358
402
    if (!language_runtime)
1359
0
      return;
1360
1361
402
    DeclVendor *decl_vendor = language_runtime->GetDeclVendor();
1362
1363
402
    if (!decl_vendor)
1364
0
      break;
1365
1366
402
    bool append = false;
1367
402
    uint32_t max_matches = 1;
1368
402
    std::vector<clang::NamedDecl *> decls;
1369
1370
402
    auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(decl_vendor);
1371
402
    if (!clang_decl_vendor->FindDecls(class_name, append, max_matches, decls))
1372
0
      break;
1373
1374
402
    DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_runtime(
1375
402
        dyn_cast<ObjCInterfaceDecl>(decls[0]));
1376
1377
402
    if (!interface_decl_from_runtime.IsValid())
1378
0
      break;
1379
1380
402
    LLDB_LOG(log,
1381
402
             "CAS::FOPD[{0}] trying runtime "
1382
402
             "(ObjCInterfaceDecl*){0}/(ASTContext*){1}...",
1383
402
             interface_decl_from_runtime.decl,
1384
402
             &interface_decl_from_runtime->getASTContext());
1385
1386
402
    if (FindObjCPropertyAndIvarDeclsWithOrigin(context, *this,
1387
402
                                               interface_decl_from_runtime))
1388
2
      return;
1389
402
  } while (
false400
);
1390
402
}
1391
1392
398
void ClangASTSource::LookupInNamespace(NameSearchContext &context) {
1393
398
  const NamespaceDecl *namespace_context =
1394
398
      dyn_cast<NamespaceDecl>(context.m_decl_context);
1395
1396
398
  Log *log = GetLog(LLDBLog::Expressions);
1397
1398
398
  ClangASTImporter::NamespaceMapSP namespace_map =
1399
398
      m_ast_importer_sp->GetNamespaceMap(namespace_context);
1400
1401
398
  LLDB_LOGV(log, "  CAS::FEVD Inspecting namespace map {0} ({1} entries)",
1402
398
            namespace_map.get(), namespace_map->size());
1403
1404
398
  if (!namespace_map)
1405
0
    return;
1406
1407
398
  for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(),
1408
398
                                                e = namespace_map->end();
1409
896
       i != e; 
++i498
) {
1410
498
    LLDB_LOG(log, "  CAS::FEVD Searching namespace {0} in module {1}",
1411
498
             i->second.GetName(), i->first->GetFileSpec().GetFilename());
1412
1413
498
    FindExternalVisibleDecls(context, i->first, i->second);
1414
498
  }
1415
398
}
1416
1417
typedef llvm::DenseMap<const FieldDecl *, uint64_t> FieldOffsetMap;
1418
typedef llvm::DenseMap<const CXXRecordDecl *, CharUnits> BaseOffsetMap;
1419
1420
template <class D, class O>
1421
static bool ImportOffsetMap(llvm::DenseMap<const D *, O> &destination_map,
1422
                            llvm::DenseMap<const D *, O> &source_map,
1423
51.8k
                            ClangASTSource &source) {
1424
  // When importing fields into a new record, clang has a hard requirement that
1425
  // fields be imported in field offset order.  Since they are stored in a
1426
  // DenseMap with a pointer as the key type, this means we cannot simply
1427
  // iterate over the map, as the order will be non-deterministic.  Instead we
1428
  // have to sort by the offset and then insert in sorted order.
1429
51.8k
  typedef llvm::DenseMap<const D *, O> MapType;
1430
51.8k
  typedef typename MapType::value_type PairType;
1431
51.8k
  std::vector<PairType> sorted_items;
1432
51.8k
  sorted_items.reserve(source_map.size());
1433
51.8k
  sorted_items.assign(source_map.begin(), source_map.end());
1434
51.8k
  llvm::sort(sorted_items, llvm::less_second());
1435
1436
51.8k
  for (const auto &item : sorted_items) {
1437
27.5k
    DeclFromUser<D> user_decl(const_cast<D *>(item.first));
1438
27.5k
    DeclFromParser<D> parser_decl(user_decl.Import(source));
1439
27.5k
    if (parser_decl.IsInvalid())
1440
0
      return false;
1441
27.5k
    destination_map.insert(
1442
27.5k
        std::pair<const D *, O>(parser_decl.decl, item.second));
1443
27.5k
  }
1444
1445
51.8k
  return true;
1446
51.8k
}
ClangASTSource.cpp:bool ImportOffsetMap<clang::FieldDecl, unsigned long long>(llvm::DenseMap<clang::FieldDecl const*, unsigned long long, llvm::DenseMapInfo<clang::FieldDecl const*, void>, llvm::detail::DenseMapPair<clang::FieldDecl const*, unsigned long long> >&, llvm::DenseMap<clang::FieldDecl const*, unsigned long long, llvm::DenseMapInfo<clang::FieldDecl const*, void>, llvm::detail::DenseMapPair<clang::FieldDecl const*, unsigned long long> >&, lldb_private::ClangASTSource&)
Line
Count
Source
1423
17.2k
                            ClangASTSource &source) {
1424
  // When importing fields into a new record, clang has a hard requirement that
1425
  // fields be imported in field offset order.  Since they are stored in a
1426
  // DenseMap with a pointer as the key type, this means we cannot simply
1427
  // iterate over the map, as the order will be non-deterministic.  Instead we
1428
  // have to sort by the offset and then insert in sorted order.
1429
17.2k
  typedef llvm::DenseMap<const D *, O> MapType;
1430
17.2k
  typedef typename MapType::value_type PairType;
1431
17.2k
  std::vector<PairType> sorted_items;
1432
17.2k
  sorted_items.reserve(source_map.size());
1433
17.2k
  sorted_items.assign(source_map.begin(), source_map.end());
1434
17.2k
  llvm::sort(sorted_items, llvm::less_second());
1435
1436
19.1k
  for (const auto &item : sorted_items) {
1437
19.1k
    DeclFromUser<D> user_decl(const_cast<D *>(item.first));
1438
19.1k
    DeclFromParser<D> parser_decl(user_decl.Import(source));
1439
19.1k
    if (parser_decl.IsInvalid())
1440
0
      return false;
1441
19.1k
    destination_map.insert(
1442
19.1k
        std::pair<const D *, O>(parser_decl.decl, item.second));
1443
19.1k
  }
1444
1445
17.2k
  return true;
1446
17.2k
}
ClangASTSource.cpp:bool ImportOffsetMap<clang::CXXRecordDecl, clang::CharUnits>(llvm::DenseMap<clang::CXXRecordDecl const*, clang::CharUnits, llvm::DenseMapInfo<clang::CXXRecordDecl const*, void>, llvm::detail::DenseMapPair<clang::CXXRecordDecl const*, clang::CharUnits> >&, llvm::DenseMap<clang::CXXRecordDecl const*, clang::CharUnits, llvm::DenseMapInfo<clang::CXXRecordDecl const*, void>, llvm::detail::DenseMapPair<clang::CXXRecordDecl const*, clang::CharUnits> >&, lldb_private::ClangASTSource&)
Line
Count
Source
1423
34.5k
                            ClangASTSource &source) {
1424
  // When importing fields into a new record, clang has a hard requirement that
1425
  // fields be imported in field offset order.  Since they are stored in a
1426
  // DenseMap with a pointer as the key type, this means we cannot simply
1427
  // iterate over the map, as the order will be non-deterministic.  Instead we
1428
  // have to sort by the offset and then insert in sorted order.
1429
34.5k
  typedef llvm::DenseMap<const D *, O> MapType;
1430
34.5k
  typedef typename MapType::value_type PairType;
1431
34.5k
  std::vector<PairType> sorted_items;
1432
34.5k
  sorted_items.reserve(source_map.size());
1433
34.5k
  sorted_items.assign(source_map.begin(), source_map.end());
1434
34.5k
  llvm::sort(sorted_items, llvm::less_second());
1435
1436
34.5k
  for (const auto &item : sorted_items) {
1437
8.33k
    DeclFromUser<D> user_decl(const_cast<D *>(item.first));
1438
8.33k
    DeclFromParser<D> parser_decl(user_decl.Import(source));
1439
8.33k
    if (parser_decl.IsInvalid())
1440
0
      return false;
1441
8.33k
    destination_map.insert(
1442
8.33k
        std::pair<const D *, O>(parser_decl.decl, item.second));
1443
8.33k
  }
1444
1445
34.5k
  return true;
1446
34.5k
}
1447
1448
template <bool IsVirtual>
1449
bool ExtractBaseOffsets(const ASTRecordLayout &record_layout,
1450
                        DeclFromUser<const CXXRecordDecl> &record,
1451
34.5k
                        BaseOffsetMap &base_offsets) {
1452
34.5k
  for (CXXRecordDecl::base_class_const_iterator
1453
34.5k
           bi = (IsVirtual ? 
record->vbases_begin()17.2k
:
record->bases_begin()17.2k
),
1454
34.5k
           be = (IsVirtual ? 
record->vbases_end()17.2k
:
record->bases_end()17.2k
);
1455
42.9k
       bi != be; 
++bi8.35k
) {
1456
8.35k
    if (!IsVirtual && 
bi->isVirtual()8.33k
)
1457
12
      continue;
1458
1459
8.33k
    const clang::Type *origin_base_type = bi->getType().getTypePtr();
1460
8.33k
    const clang::RecordType *origin_base_record_type =
1461
8.33k
        origin_base_type->getAs<RecordType>();
1462
1463
8.33k
    if (!origin_base_record_type)
1464
0
      return false;
1465
1466
8.33k
    DeclFromUser<RecordDecl> origin_base_record(
1467
8.33k
        origin_base_record_type->getDecl());
1468
1469
8.33k
    if (origin_base_record.IsInvalid())
1470
0
      return false;
1471
1472
8.33k
    DeclFromUser<CXXRecordDecl> origin_base_cxx_record(
1473
8.33k
        DynCast<CXXRecordDecl>(origin_base_record));
1474
1475
8.33k
    if (origin_base_cxx_record.IsInvalid())
1476
0
      return false;
1477
1478
8.33k
    CharUnits base_offset;
1479
1480
8.33k
    if (IsVirtual)
1481
16
      base_offset =
1482
16
          record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1483
8.32k
    else
1484
8.32k
      base_offset =
1485
8.32k
          record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1486
1487
8.33k
    base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(
1488
8.33k
        origin_base_cxx_record.decl, base_offset));
1489
8.33k
  }
1490
1491
34.5k
  return true;
1492
34.5k
}
bool ExtractBaseOffsets<false>(clang::ASTRecordLayout const&, DeclFromUser<clang::CXXRecordDecl const>&, llvm::DenseMap<clang::CXXRecordDecl const*, clang::CharUnits, llvm::DenseMapInfo<clang::CXXRecordDecl const*, void>, llvm::detail::DenseMapPair<clang::CXXRecordDecl const*, clang::CharUnits> >&)
Line
Count
Source
1451
17.2k
                        BaseOffsetMap &base_offsets) {
1452
17.2k
  for (CXXRecordDecl::base_class_const_iterator
1453
17.2k
           bi = (IsVirtual ? 
record->vbases_begin()0
: record->bases_begin()),
1454
17.2k
           be = (IsVirtual ? 
record->vbases_end()0
: record->bases_end());
1455
25.6k
       bi != be; 
++bi8.33k
) {
1456
8.33k
    if (!IsVirtual && bi->isVirtual())
1457
12
      continue;
1458
1459
8.32k
    const clang::Type *origin_base_type = bi->getType().getTypePtr();
1460
8.32k
    const clang::RecordType *origin_base_record_type =
1461
8.32k
        origin_base_type->getAs<RecordType>();
1462
1463
8.32k
    if (!origin_base_record_type)
1464
0
      return false;
1465
1466
8.32k
    DeclFromUser<RecordDecl> origin_base_record(
1467
8.32k
        origin_base_record_type->getDecl());
1468
1469
8.32k
    if (origin_base_record.IsInvalid())
1470
0
      return false;
1471
1472
8.32k
    DeclFromUser<CXXRecordDecl> origin_base_cxx_record(
1473
8.32k
        DynCast<CXXRecordDecl>(origin_base_record));
1474
1475
8.32k
    if (origin_base_cxx_record.IsInvalid())
1476
0
      return false;
1477
1478
8.32k
    CharUnits base_offset;
1479
1480
8.32k
    if (IsVirtual)
1481
0
      base_offset =
1482
0
          record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1483
8.32k
    else
1484
8.32k
      base_offset =
1485
8.32k
          record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1486
1487
8.32k
    base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(
1488
8.32k
        origin_base_cxx_record.decl, base_offset));
1489
8.32k
  }
1490
1491
17.2k
  return true;
1492
17.2k
}
bool ExtractBaseOffsets<true>(clang::ASTRecordLayout const&, DeclFromUser<clang::CXXRecordDecl const>&, llvm::DenseMap<clang::CXXRecordDecl const*, clang::CharUnits, llvm::DenseMapInfo<clang::CXXRecordDecl const*, void>, llvm::detail::DenseMapPair<clang::CXXRecordDecl const*, clang::CharUnits> >&)
Line
Count
Source
1451
17.2k
                        BaseOffsetMap &base_offsets) {
1452
17.2k
  for (CXXRecordDecl::base_class_const_iterator
1453
17.2k
           bi = (IsVirtual ? record->vbases_begin() : 
record->bases_begin()0
),
1454
17.2k
           be = (IsVirtual ? record->vbases_end() : 
record->bases_end()0
);
1455
17.3k
       bi != be; 
++bi16
) {
1456
16
    if (!IsVirtual && 
bi->isVirtual()0
)
1457
0
      continue;
1458
1459
16
    const clang::Type *origin_base_type = bi->getType().getTypePtr();
1460
16
    const clang::RecordType *origin_base_record_type =
1461
16
        origin_base_type->getAs<RecordType>();
1462
1463
16
    if (!origin_base_record_type)
1464
0
      return false;
1465
1466
16
    DeclFromUser<RecordDecl> origin_base_record(
1467
16
        origin_base_record_type->getDecl());
1468
1469
16
    if (origin_base_record.IsInvalid())
1470
0
      return false;
1471
1472
16
    DeclFromUser<CXXRecordDecl> origin_base_cxx_record(
1473
16
        DynCast<CXXRecordDecl>(origin_base_record));
1474
1475
16
    if (origin_base_cxx_record.IsInvalid())
1476
0
      return false;
1477
1478
16
    CharUnits base_offset;
1479
1480
16
    if (IsVirtual)
1481
16
      base_offset =
1482
16
          record_layout.getVBaseClassOffset(origin_base_cxx_record.decl);
1483
0
    else
1484
0
      base_offset =
1485
0
          record_layout.getBaseClassOffset(origin_base_cxx_record.decl);
1486
1487
16
    base_offsets.insert(std::pair<const CXXRecordDecl *, CharUnits>(
1488
16
        origin_base_cxx_record.decl, base_offset));
1489
16
  }
1490
1491
17.2k
  return true;
1492
17.2k
}
1493
1494
bool ClangASTSource::layoutRecordType(const RecordDecl *record, uint64_t &size,
1495
                                      uint64_t &alignment,
1496
                                      FieldOffsetMap &field_offsets,
1497
                                      BaseOffsetMap &base_offsets,
1498
30.1k
                                      BaseOffsetMap &virtual_base_offsets) {
1499
1500
30.1k
  Log *log = GetLog(LLDBLog::Expressions);
1501
1502
30.1k
  LLDB_LOG(log,
1503
30.1k
           "LayoutRecordType on (ASTContext*){0} '{1}' for (RecordDecl*)"
1504
30.1k
           "{2} [name = '{3}']",
1505
30.1k
           m_ast_context, m_clang_ast_context->getDisplayName(), record,
1506
30.1k
           record->getName());
1507
1508
30.1k
  DeclFromParser<const RecordDecl> parser_record(record);
1509
30.1k
  DeclFromUser<const RecordDecl> origin_record(
1510
30.1k
      parser_record.GetOrigin(*this));
1511
1512
30.1k
  if (origin_record.IsInvalid())
1513
12.8k
    return false;
1514
1515
17.2k
  FieldOffsetMap origin_field_offsets;
1516
17.2k
  BaseOffsetMap origin_base_offsets;
1517
17.2k
  BaseOffsetMap origin_virtual_base_offsets;
1518
1519
17.2k
  TypeSystemClang::GetCompleteDecl(
1520
17.2k
      &origin_record->getASTContext(),
1521
17.2k
      const_cast<RecordDecl *>(origin_record.decl));
1522
1523
17.2k
  clang::RecordDecl *definition = origin_record.decl->getDefinition();
1524
17.2k
  if (!definition || !definition->isCompleteDefinition())
1525
0
    return false;
1526
1527
17.2k
  const ASTRecordLayout &record_layout(
1528
17.2k
      origin_record->getASTContext().getASTRecordLayout(origin_record.decl));
1529
1530
17.2k
  int field_idx = 0, field_count = record_layout.getFieldCount();
1531
1532
17.2k
  for (RecordDecl::field_iterator fi = origin_record->field_begin(),
1533
17.2k
                                  fe = origin_record->field_end();
1534
36.4k
       fi != fe; 
++fi19.1k
) {
1535
19.1k
    if (field_idx >= field_count)
1536
0
      return false; // Layout didn't go well.  Bail out.
1537
1538
19.1k
    uint64_t field_offset = record_layout.getFieldOffset(field_idx);
1539
1540
19.1k
    origin_field_offsets.insert(
1541
19.1k
        std::pair<const FieldDecl *, uint64_t>(*fi, field_offset));
1542
1543
19.1k
    field_idx++;
1544
19.1k
  }
1545
1546
17.2k
  lldbassert(&record->getASTContext() == m_ast_context);
1547
1548
17.2k
  DeclFromUser<const CXXRecordDecl> origin_cxx_record(
1549
17.2k
      DynCast<const CXXRecordDecl>(origin_record));
1550
1551
17.2k
  if (origin_cxx_record.IsValid()) {
1552
17.2k
    if (!ExtractBaseOffsets<false>(record_layout, origin_cxx_record,
1553
17.2k
                                   origin_base_offsets) ||
1554
17.2k
        !ExtractBaseOffsets<true>(record_layout, origin_cxx_record,
1555
17.2k
                                  origin_virtual_base_offsets))
1556
0
      return false;
1557
17.2k
  }
1558
1559
17.2k
  if (!ImportOffsetMap(field_offsets, origin_field_offsets, *this) ||
1560
17.2k
      !ImportOffsetMap(base_offsets, origin_base_offsets, *this) ||
1561
17.2k
      !ImportOffsetMap(virtual_base_offsets, origin_virtual_base_offsets,
1562
17.2k
                       *this))
1563
0
    return false;
1564
1565
17.2k
  size = record_layout.getSize().getQuantity() * m_ast_context->getCharWidth();
1566
17.2k
  alignment = record_layout.getAlignment().getQuantity() *
1567
17.2k
              m_ast_context->getCharWidth();
1568
1569
17.2k
  if (log) {
1570
0
    LLDB_LOG(log, "LRT returned:");
1571
0
    LLDB_LOG(log, "LRT   Original = (RecordDecl*){0}",
1572
0
             static_cast<const void *>(origin_record.decl));
1573
0
    LLDB_LOG(log, "LRT   Size = {0}", size);
1574
0
    LLDB_LOG(log, "LRT   Alignment = {0}", alignment);
1575
0
    LLDB_LOG(log, "LRT   Fields:");
1576
0
    for (RecordDecl::field_iterator fi = record->field_begin(),
1577
0
                                    fe = record->field_end();
1578
0
         fi != fe; ++fi) {
1579
0
      LLDB_LOG(log,
1580
0
               "LRT     (FieldDecl*){0}, Name = '{1}', Type = '{2}', Offset = "
1581
0
               "{3} bits",
1582
0
               *fi, fi->getName(), fi->getType().getAsString(),
1583
0
               field_offsets[*fi]);
1584
0
    }
1585
0
    DeclFromParser<const CXXRecordDecl> parser_cxx_record =
1586
0
        DynCast<const CXXRecordDecl>(parser_record);
1587
0
    if (parser_cxx_record.IsValid()) {
1588
0
      LLDB_LOG(log, "LRT   Bases:");
1589
0
      for (CXXRecordDecl::base_class_const_iterator
1590
0
               bi = parser_cxx_record->bases_begin(),
1591
0
               be = parser_cxx_record->bases_end();
1592
0
           bi != be; ++bi) {
1593
0
        bool is_virtual = bi->isVirtual();
1594
1595
0
        QualType base_type = bi->getType();
1596
0
        const RecordType *base_record_type = base_type->getAs<RecordType>();
1597
0
        DeclFromParser<RecordDecl> base_record(base_record_type->getDecl());
1598
0
        DeclFromParser<CXXRecordDecl> base_cxx_record =
1599
0
            DynCast<CXXRecordDecl>(base_record);
1600
1601
0
        LLDB_LOG(log,
1602
0
                 "LRT     {0}(CXXRecordDecl*){1}, Name = '{2}', Offset = "
1603
0
                 "{3} chars",
1604
0
                 (is_virtual ? "Virtual " : ""), base_cxx_record.decl,
1605
0
                 base_cxx_record.decl->getName(),
1606
0
                 (is_virtual
1607
0
                      ? virtual_base_offsets[base_cxx_record.decl].getQuantity()
1608
0
                      : base_offsets[base_cxx_record.decl].getQuantity()));
1609
0
      }
1610
0
    } else {
1611
0
      LLDB_LOG(log, "LRD   Not a CXXRecord, so no bases");
1612
0
    }
1613
0
  }
1614
1615
17.2k
  return true;
1616
17.2k
}
1617
1618
void ClangASTSource::CompleteNamespaceMap(
1619
    ClangASTImporter::NamespaceMapSP &namespace_map, ConstString name,
1620
16.7k
    ClangASTImporter::NamespaceMapSP &parent_map) const {
1621
1622
16.7k
  Log *log = GetLog(LLDBLog::Expressions);
1623
1624
16.7k
  if (log) {
1625
0
    if (parent_map && parent_map->size())
1626
0
      LLDB_LOG(log,
1627
0
               "CompleteNamespaceMap on (ASTContext*){0} '{1}' Searching "
1628
0
               "for namespace {2} in namespace {3}",
1629
0
               m_ast_context, m_clang_ast_context->getDisplayName(), name,
1630
0
               parent_map->begin()->second.GetName());
1631
0
    else
1632
0
      LLDB_LOG(log,
1633
0
               "CompleteNamespaceMap on (ASTContext*){0} '{1}' Searching "
1634
0
               "for namespace {2}",
1635
0
               m_ast_context, m_clang_ast_context->getDisplayName(), name);
1636
0
  }
1637
1638
16.7k
  if (parent_map) {
1639
8.12k
    for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(),
1640
8.12k
                                                  e = parent_map->end();
1641
16.2k
         i != e; 
++i8.12k
) {
1642
8.12k
      CompilerDeclContext found_namespace_decl;
1643
1644
8.12k
      lldb::ModuleSP module_sp = i->first;
1645
8.12k
      CompilerDeclContext module_parent_namespace_decl = i->second;
1646
1647
8.12k
      SymbolFile *symbol_file = module_sp->GetSymbolFile();
1648
1649
8.12k
      if (!symbol_file)
1650
0
        continue;
1651
1652
8.12k
      found_namespace_decl =
1653
8.12k
          symbol_file->FindNamespace(name, module_parent_namespace_decl);
1654
1655
8.12k
      if (!found_namespace_decl)
1656
48
        continue;
1657
1658
8.07k
      namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(
1659
8.07k
          module_sp, found_namespace_decl));
1660
1661
8.07k
      LLDB_LOG(log, "  CMN Found namespace {0} in module {1}", name,
1662
8.07k
               module_sp->GetFileSpec().GetFilename());
1663
8.07k
    }
1664
8.65k
  } else {
1665
8.65k
    CompilerDeclContext null_namespace_decl;
1666
403k
    for (lldb::ModuleSP image : m_target->GetImages().Modules()) {
1667
403k
      if (!image)
1668
0
        continue;
1669
1670
403k
      CompilerDeclContext found_namespace_decl;
1671
1672
403k
      SymbolFile *symbol_file = image->GetSymbolFile();
1673
1674
403k
      if (!symbol_file)
1675
12
        continue;
1676
1677
403k
      found_namespace_decl =
1678
403k
          symbol_file->FindNamespace(name, null_namespace_decl);
1679
1680
403k
      if (!found_namespace_decl)
1681
394k
        continue;
1682
1683
8.83k
      namespace_map->push_back(std::pair<lldb::ModuleSP, CompilerDeclContext>(
1684
8.83k
          image, found_namespace_decl));
1685
1686
8.83k
      LLDB_LOG(log, "  CMN[{0}] Found namespace {0} in module {1}", name,
1687
8.83k
               image->GetFileSpec().GetFilename());
1688
8.83k
    }
1689
8.65k
  }
1690
16.7k
}
1691
1692
NamespaceDecl *ClangASTSource::AddNamespace(
1693
    NameSearchContext &context,
1694
232
    ClangASTImporter::NamespaceMapSP &namespace_decls) {
1695
232
  if (!namespace_decls)
1696
0
    return nullptr;
1697
1698
232
  const CompilerDeclContext &namespace_decl = namespace_decls->begin()->second;
1699
1700
232
  clang::ASTContext *src_ast =
1701
232
      TypeSystemClang::DeclContextGetTypeSystemClang(namespace_decl);
1702
232
  if (!src_ast)
1703
0
    return nullptr;
1704
232
  clang::NamespaceDecl *src_namespace_decl =
1705
232
      TypeSystemClang::DeclContextGetAsNamespaceDecl(namespace_decl);
1706
1707
232
  if (!src_namespace_decl)
1708
0
    return nullptr;
1709
1710
232
  Decl *copied_decl = CopyDecl(src_namespace_decl);
1711
1712
232
  if (!copied_decl)
1713
0
    return nullptr;
1714
1715
232
  NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(copied_decl);
1716
1717
232
  if (!copied_namespace_decl)
1718
0
    return nullptr;
1719
1720
232
  context.m_decls.push_back(copied_namespace_decl);
1721
1722
232
  m_ast_importer_sp->RegisterNamespaceMap(copied_namespace_decl,
1723
232
                                          namespace_decls);
1724
1725
232
  return dyn_cast<NamespaceDecl>(copied_decl);
1726
232
}
1727
1728
345k
clang::Decl *ClangASTSource::CopyDecl(Decl *src_decl) {
1729
345k
  return m_ast_importer_sp->CopyDecl(m_ast_context, src_decl);
1730
345k
}
1731
1732
30.6k
ClangASTImporter::DeclOrigin ClangASTSource::GetDeclOrigin(const clang::Decl *decl) {
1733
30.6k
  return m_ast_importer_sp->GetDeclOrigin(decl);
1734
30.6k
}
1735
1736
15.1k
CompilerType ClangASTSource::GuardedCopyType(const CompilerType &src_type) {
1737
15.1k
  auto ts = src_type.GetTypeSystem();
1738
15.1k
  auto src_ast = ts.dyn_cast_or_null<TypeSystemClang>();
1739
15.1k
  if (!src_ast)
1740
0
    return {};
1741
1742
15.1k
  QualType copied_qual_type = ClangUtil::GetQualType(
1743
15.1k
      m_ast_importer_sp->CopyType(*m_clang_ast_context, src_type));
1744
1745
15.1k
  if (copied_qual_type.getAsOpaquePtr() &&
1746
15.1k
      copied_qual_type->getCanonicalTypeInternal().isNull())
1747
    // this shouldn't happen, but we're hardening because the AST importer
1748
    // seems to be generating bad types on occasion.
1749
0
    return {};
1750
1751
15.1k
  return m_clang_ast_context->GetType(copied_qual_type);
1752
15.1k
}
1753
1754
std::shared_ptr<ClangModulesDeclVendor>
1755
15.8k
ClangASTSource::GetClangModulesDeclVendor() {
1756
15.8k
  auto persistent_vars = llvm::cast<ClangPersistentVariables>(
1757
15.8k
      m_target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
1758
15.8k
  return persistent_vars->GetClangModulesDeclVendor();
1759
15.8k
}