Coverage Report

Created: 2022-01-18 06:27

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