LLDB mainline
PdbAstBuilderClang.cpp
Go to the documentation of this file.
2
3#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
4#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
5#include "llvm/DebugInfo/CodeView/RecordName.h"
6#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
7#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
8#include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
9#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
10#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
11#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
12#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
13#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
14#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
15#include "llvm/Demangle/MicrosoftDemangle.h"
16
17#include "PdbUtil.h"
22#include "SymbolFileNativePDB.h"
23#include "UdtRecordCompleter.h"
24#include "lldb/Core/Module.h"
27#include <optional>
28#include <string_view>
29
30using namespace lldb_private;
31using namespace lldb_private::npdb;
32using namespace llvm::codeview;
33using namespace llvm::pdb;
34
35namespace {
36struct CreateMethodDecl : public TypeVisitorCallbacks {
37 CreateMethodDecl(PdbIndex &m_index, TypeSystemClang &m_clang,
38 TypeIndex func_type_index,
39 clang::FunctionDecl *&function_decl,
41 llvm::StringRef proc_name, ConstString mangled_name,
42 CompilerType func_ct)
43 : m_index(m_index), m_clang(m_clang), func_type_index(func_type_index),
44 function_decl(function_decl), parent_ty(parent_ty),
45 proc_name(proc_name), mangled_name(mangled_name), func_ct(func_ct) {}
46 PdbIndex &m_index;
47 TypeSystemClang &m_clang;
48 TypeIndex func_type_index;
49 clang::FunctionDecl *&function_decl;
51 llvm::StringRef proc_name;
52 ConstString mangled_name;
53 CompilerType func_ct;
54
55 llvm::Error visitKnownMember(CVMemberRecord &cvr,
56 OverloadedMethodRecord &overloaded) override {
57 TypeIndex method_list_idx = overloaded.MethodList;
58
59 CVType method_list_type = m_index.tpi().getType(method_list_idx);
60 assert(method_list_type.kind() == LF_METHODLIST);
61
62 MethodOverloadListRecord method_list;
63 llvm::cantFail(TypeDeserializer::deserializeAs<MethodOverloadListRecord>(
64 method_list_type, method_list));
65
66 for (const OneMethodRecord &method : method_list.Methods) {
67 if (method.getType().getIndex() == func_type_index.getIndex())
68 AddMethod(overloaded.Name, method.getOptions(), method.Attrs);
69 }
70
71 return llvm::Error::success();
72 }
73
74 llvm::Error visitKnownMember(CVMemberRecord &cvr,
75 OneMethodRecord &record) override {
76 AddMethod(record.getName(), record.getOptions(), record.Attrs);
77 return llvm::Error::success();
78 }
79
80 void AddMethod(llvm::StringRef name, MethodOptions options,
81 MemberAttributes attrs) {
82 if (name != proc_name || function_decl)
83 return;
84 bool is_virtual = attrs.isVirtual();
85 bool is_static = attrs.isStatic();
86 bool is_artificial = (options & MethodOptions::CompilerGenerated) ==
87 MethodOptions::CompilerGenerated;
88 function_decl = m_clang.AddMethodToCXXRecordType(
89 parent_ty, proc_name, mangled_name, func_ct,
90 /*is_virtual=*/is_virtual, /*is_static=*/is_static,
91 /*is_inline=*/false, /*is_explicit=*/false,
92 /*is_attr_used=*/false, /*is_artificial=*/is_artificial);
93 }
94};
95} // namespace
96
97static clang::TagTypeKind TranslateUdtKind(const TagRecord &cr) {
98 switch (cr.Kind) {
99 case TypeRecordKind::Class:
100 return clang::TagTypeKind::Class;
101 case TypeRecordKind::Struct:
102 return clang::TagTypeKind::Struct;
103 case TypeRecordKind::Union:
104 return clang::TagTypeKind::Union;
105 case TypeRecordKind::Interface:
106 return clang::TagTypeKind::Interface;
107 case TypeRecordKind::Enum:
108 return clang::TagTypeKind::Enum;
109 default:
110 lldbassert(false && "Invalid tag record kind!");
111 return clang::TagTypeKind::Struct;
112 }
113}
114
115static bool IsCVarArgsFunction(llvm::ArrayRef<TypeIndex> args) {
116 if (args.empty())
117 return false;
118 return args.back() == TypeIndex::None();
119}
120
121static bool
122AnyScopesHaveTemplateParams(llvm::ArrayRef<llvm::ms_demangle::Node *> scopes) {
123 for (llvm::ms_demangle::Node *n : scopes) {
124 auto *idn = static_cast<llvm::ms_demangle::IdentifierNode *>(n);
125 if (idn->TemplateParams)
126 return true;
127 }
128 return false;
129}
130
131static std::optional<clang::CallingConv>
132TranslateCallingConvention(llvm::codeview::CallingConvention conv) {
133 using CC = llvm::codeview::CallingConvention;
134 switch (conv) {
135
136 case CC::NearC:
137 case CC::FarC:
138 return clang::CallingConv::CC_C;
139 case CC::NearPascal:
140 case CC::FarPascal:
141 return clang::CallingConv::CC_X86Pascal;
142 case CC::NearFast:
143 case CC::FarFast:
144 return clang::CallingConv::CC_X86FastCall;
145 case CC::NearStdCall:
146 case CC::FarStdCall:
147 return clang::CallingConv::CC_X86StdCall;
148 case CC::ThisCall:
149 return clang::CallingConv::CC_X86ThisCall;
150 case CC::NearVector:
151 return clang::CallingConv::CC_X86VectorCall;
152 default:
153 return std::nullopt;
154 }
155}
156
157static bool IsAnonymousNamespaceName(llvm::StringRef name) {
158 return name == "`anonymous namespace'" || name == "`anonymous-namespace'";
159}
160
163
167
168std::pair<clang::DeclContext *, std::string>
170 TypeIndex ti) {
172 m_clang.GetSymbolFile()->GetBackingSymbolFile());
173 // FIXME: Move this to GetDeclContextContainingUID.
174 if (!record.hasUniqueName())
175 return CreateDeclInfoForUndecoratedName(record.Name);
176
177 llvm::ms_demangle::Demangler demangler;
178 std::string_view sv(record.UniqueName.begin(), record.UniqueName.size());
179 llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
180 if (demangler.Error)
181 return CreateDeclInfoForUndecoratedName(record.Name);
182
183 llvm::ms_demangle::IdentifierNode *idn =
184 ttn->QualifiedName->getUnqualifiedIdentifier();
185 std::string uname = idn->toString(llvm::ms_demangle::OF_NoTagSpecifier);
186
187 llvm::ms_demangle::NodeArrayNode *name_components =
188 ttn->QualifiedName->Components;
189 llvm::ArrayRef<llvm::ms_demangle::Node *> scopes(name_components->Nodes,
190 name_components->Count - 1);
191
192 clang::DeclContext *context = m_clang.GetTranslationUnitDecl();
193
194 // If this type doesn't have a parent type in the debug info, then the best we
195 // can do is to say that it's either a series of namespaces (if the scope is
196 // non-empty), or the translation unit (if the scope is empty).
197 std::optional<TypeIndex> parent_index = pdb->GetParentType(ti);
198 if (!parent_index) {
199 if (scopes.empty())
200 return {context, uname};
201
202 // If there is no parent in the debug info, but some of the scopes have
203 // template params, then this is a case of bad debug info. See, for
204 // example, llvm.org/pr39607. We don't want to create an ambiguity between
205 // a NamespaceDecl and a CXXRecordDecl, so instead we create a class at
206 // global scope with the fully qualified name.
207 if (AnyScopesHaveTemplateParams(scopes))
208 return {context, std::string(record.Name)};
209
210 for (llvm::ms_demangle::Node *scope : scopes) {
211 auto *nii = static_cast<llvm::ms_demangle::NamedIdentifierNode *>(scope);
212 std::string str = nii->toString();
213 context = GetOrCreateNamespaceDecl(str.c_str(), *context);
214 }
215 return {context, uname};
216 }
217
218 // Otherwise, all we need to do is get the parent type of this type and
219 // recurse into our lazy type creation / AST reconstruction logic to get an
220 // LLDB TypeSP for the parent. This will cause the AST to automatically get
221 // the right DeclContext created for any parent.
222 clang::QualType parent_qt = GetOrCreateClangType(*parent_index);
223 if (parent_qt.isNull())
224 return {nullptr, ""};
225
226 context = clang::TagDecl::castToDeclContext(parent_qt->getAsTagDecl());
227 return {context, uname};
228}
229
230static bool isLocalVariableType(SymbolKind K) {
231 switch (K) {
232 case S_REGISTER:
233 case S_REGREL32:
234 case S_REGREL32_INDIR:
235 case S_LOCAL:
236 return true;
237 default:
238 break;
239 }
240 return false;
241}
242
245 m_clang.GetSymbolFile()->GetBackingSymbolFile());
246 PdbIndex &index = pdb->GetIndex();
247 CVSymbol cvs = index.ReadSymbolRecord(id);
248
249 if (isLocalVariableType(cvs.kind())) {
250 clang::DeclContext *scope = GetParentClangDeclContext(id);
251 if (!scope)
252 return nullptr;
253 clang::Decl *scope_decl = clang::Decl::castFromDeclContext(scope);
254 PdbCompilandSymId scope_id =
255 PdbSymUid(m_decl_to_status[scope_decl].uid).asCompilandSym();
256 return GetOrCreateVariableDecl(scope_id, id);
257 }
258
259 switch (cvs.kind()) {
260 case S_GPROC32:
261 case S_LPROC32:
262 return GetOrCreateFunctionDecl(id);
263 case S_GDATA32:
264 case S_LDATA32:
265 case S_GTHREAD32:
266 case S_CONSTANT:
267 // global variable
268 return nullptr;
269 case S_BLOCK32:
270 return GetOrCreateBlockDecl(id);
271 case S_INLINESITE:
273 default:
274 return nullptr;
275 }
276}
277
279 if (clang::Decl *result = TryGetDecl(uid))
280 return ToCompilerDecl(result);
281
282 clang::Decl *result = nullptr;
283 switch (uid.kind()) {
286 break;
287 case PdbSymUidKind::Type: {
288 clang::QualType qt = GetOrCreateClangType(uid.asTypeSym());
289 if (qt.isNull())
290 return CompilerDecl();
291 if (auto *tag = qt->getAsTagDecl()) {
292 result = tag;
293 break;
294 }
295 return CompilerDecl();
296 }
297 default:
298 return CompilerDecl();
299 }
300
301 if (!result)
302 return CompilerDecl();
303 m_uid_to_decl[toOpaqueUid(uid)] = result;
304 return ToCompilerDecl(result);
305}
306
307clang::DeclContext *
309 if (uid.kind() == PdbSymUidKind::CompilandSym) {
310 if (uid.asCompilandSym().offset == 0)
312 }
313 clang::Decl *decl = FromCompilerDecl(GetOrCreateDeclForUid(uid));
314 if (!decl)
315 return nullptr;
316
317 return clang::Decl::castToDeclContext(decl);
318}
319
324
325std::pair<clang::DeclContext *, std::string>
328 m_clang.GetSymbolFile()->GetBackingSymbolFile());
329 PdbIndex &index = pdb->GetIndex();
330 MSVCUndecoratedNameParser parser(name);
331 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers();
332
334
335 llvm::StringRef uname = specs.back().GetBaseName();
336 specs = specs.drop_back();
337 if (specs.empty())
338 return {context, std::string(name)};
339
340 llvm::StringRef scope_name = specs.back().GetFullName();
341
342 // It might be a class name, try that first.
343 std::vector<TypeIndex> types = index.tpi().findRecordsByName(scope_name);
344 while (!types.empty()) {
345 clang::QualType qt = GetOrCreateClangType(types.back());
346 if (qt.isNull())
347 continue;
348 clang::TagDecl *tag = qt->getAsTagDecl();
349 if (tag)
350 return {clang::TagDecl::castToDeclContext(tag), std::string(uname)};
351 types.pop_back();
352 }
353
354 // If that fails, treat it as a series of namespaces.
355 for (const MSVCUndecoratedNameSpecifier &spec : specs) {
356 std::string ns_name = spec.GetBaseName().str();
357 context = GetOrCreateNamespaceDecl(ns_name.c_str(), *context);
358 }
359 return {context, std::string(uname)};
360}
361
362clang::DeclContext *
364 // We must do this *without* calling GetOrCreate on the current uid, as
365 // that would be an infinite recursion.
367 m_clang.GetSymbolFile()->GetBackingSymbolFile());
368 PdbIndex &index = pdb->GetIndex();
369 switch (uid.kind()) {
371 std::optional<PdbCompilandSymId> scope =
372 pdb->FindSymbolScope(uid.asCompilandSym());
373 if (scope)
375
376 CVSymbol sym = index.ReadSymbolRecord(uid.asCompilandSym());
377 return CreateDeclInfoForUndecoratedName(getSymbolName(sym)).first;
378 }
379 case PdbSymUidKind::Type: {
380 // It could be a namespace, class, or global. We don't support nested
381 // functions yet. Anyway, we just need to consult the parent type map.
382 PdbTypeSymId type_id = uid.asTypeSym();
383 std::optional<TypeIndex> parent_index = pdb->GetParentType(type_id.index);
384 if (!parent_index)
386 return GetOrCreateClangDeclContextForUid(PdbTypeSymId(*parent_index));
387 }
389 // In this case the parent DeclContext is the one for the class that this
390 // member is inside of.
391 break;
393 // If this refers to a compiland symbol, just recurse in with that symbol.
394 // The only other possibilities are S_CONSTANT and S_UDT, in which case we
395 // need to parse the undecorated name to figure out the scope, then look
396 // that up in the TPI stream. If it's found, it's a type, othewrise it's
397 // a series of namespaces.
398 // FIXME: do this.
399 CVSymbol global = index.ReadSymbolRecord(uid.asGlobalSym());
400 switch (global.kind()) {
401 case SymbolKind::S_GDATA32:
402 case SymbolKind::S_LDATA32:
403 return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first;
404 case SymbolKind::S_PROCREF:
405 case SymbolKind::S_LPROCREF: {
406 ProcRefSym ref{global.kind()};
407 llvm::cantFail(
408 SymbolDeserializer::deserializeAs<ProcRefSym>(global, ref));
409 PdbCompilandSymId cu_sym_id{ref.modi(), ref.SymOffset};
410 return GetParentClangDeclContext(cu_sym_id);
411 }
412 case SymbolKind::S_CONSTANT:
413 case SymbolKind::S_UDT:
414 return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first;
415 default:
416 break;
417 }
418 break;
419 }
420 default:
421 break;
422 }
424}
425
429
431 if (GetClangASTImporter().CanImport(ct))
433
434 clang::QualType qt = FromCompilerType(ct);
435 if (qt.isNull())
436 return false;
437 clang::TagDecl *tag = qt->getAsTagDecl();
438 if (qt->isArrayType()) {
439 const clang::Type *element_type = qt->getArrayElementTypeNoTypeQual();
440 tag = element_type->getAsTagDecl();
441 }
442 if (!tag)
443 return false;
444
445 return CompleteTagDecl(*tag);
446}
447
448bool PdbAstBuilderClang::CompleteTagDecl(clang::TagDecl &tag) {
449 // If this is not in our map, it's an error.
450 auto status_iter = m_decl_to_status.find(&tag);
451 lldbassert(status_iter != m_decl_to_status.end());
452
453 // If it's already complete, just return.
454 DeclStatus &status = status_iter->second;
455 if (status.resolved)
456 return true;
457
458 PdbTypeSymId type_id = PdbSymUid(status.uid).asTypeSym();
459 PdbIndex &index = static_cast<SymbolFileNativePDB *>(
460 m_clang.GetSymbolFile()->GetBackingSymbolFile())
461 ->GetIndex();
462 lldbassert(IsTagRecord(type_id, index.tpi()));
463
464 clang::QualType tag_qt = m_clang.getASTContext().getCanonicalTagType(&tag);
465 TypeSystemClang::SetHasExternalStorage(tag_qt.getAsOpaquePtr(), false);
466
467 TypeIndex tag_ti = type_id.index;
468 CVType cvt = index.tpi().getType(tag_ti);
469 if (cvt.kind() == LF_MODIFIER)
470 tag_ti = LookThroughModifierRecord(cvt);
471
472 PdbTypeSymId best_ti = GetBestPossibleDecl(tag_ti, index.tpi());
473 cvt = index.tpi().getType(best_ti.index);
475
476 if (IsForwardRefUdt(cvt)) {
477 // If we can't find a full decl for this forward ref anywhere in the debug
478 // info, then we have no way to complete it.
479 return false;
480 }
481
482 TypeIndex field_list_ti = GetFieldListIndex(cvt);
483 CVType field_list_cvt = index.tpi().getType(field_list_ti);
484 if (field_list_cvt.kind() != LF_FIELDLIST)
485 return false;
486 FieldListRecord field_list;
487 if (llvm::Error error = TypeDeserializer::deserializeAs<FieldListRecord>(
488 field_list_cvt, field_list))
489 llvm::consumeError(std::move(error));
490
491 // Visit all members of this class, then perform any finalization necessary
492 // to complete the class.
493 CompilerType ct = ToCompilerType(tag_qt);
494 UdtRecordCompleter completer(best_ti, ct, tag, *this, index, m_decl_to_status,
496 llvm::Error error =
497 llvm::codeview::visitMemberRecordStream(field_list.Data, completer);
498 completer.complete();
499
500 m_decl_to_status[&tag].resolved = true;
501 if (error) {
502 llvm::consumeError(std::move(error));
503 return false;
504 }
505 return true;
506}
507
509 if (ti == TypeIndex::NullptrT())
511
512 if (ti.getSimpleMode() != SimpleTypeMode::Direct) {
513 clang::QualType direct_type = GetOrCreateClangType(ti.makeDirect());
514 if (direct_type.isNull())
515 return {};
516 return m_clang.getASTContext().getPointerType(direct_type);
517 }
518
519 if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated)
520 return {};
521
522 lldb::BasicType bt = GetCompilerTypeForSimpleKind(ti.getSimpleKind());
523 if (bt == lldb::eBasicTypeInvalid)
524 return {};
525
526 return GetBasicType(bt);
527}
528
529clang::QualType
530PdbAstBuilderClang::CreatePointerType(const PointerRecord &pointer) {
531 clang::QualType pointee_type = GetOrCreateClangType(pointer.ReferentType);
532
533 // This can happen for pointers to LF_VTSHAPE records, which we shouldn't
534 // create in the AST.
535 if (pointee_type.isNull())
536 return {};
537
538 if (pointer.isPointerToMember()) {
539 MemberPointerInfo mpi = pointer.getMemberInfo();
540 clang::QualType class_type = GetOrCreateClangType(mpi.ContainingType);
541 if (class_type.isNull())
542 return {};
543 if (clang::TagDecl *tag = class_type->getAsTagDecl()) {
544 clang::MSInheritanceAttr::Spelling spelling;
545 switch (mpi.Representation) {
546 case llvm::codeview::PointerToMemberRepresentation::SingleInheritanceData:
547 case llvm::codeview::PointerToMemberRepresentation::
548 SingleInheritanceFunction:
549 spelling =
550 clang::MSInheritanceAttr::Spelling::Keyword_single_inheritance;
551 break;
552 case llvm::codeview::PointerToMemberRepresentation::
553 MultipleInheritanceData:
554 case llvm::codeview::PointerToMemberRepresentation::
555 MultipleInheritanceFunction:
556 spelling =
557 clang::MSInheritanceAttr::Spelling::Keyword_multiple_inheritance;
558 break;
559 case llvm::codeview::PointerToMemberRepresentation::
560 VirtualInheritanceData:
561 case llvm::codeview::PointerToMemberRepresentation::
562 VirtualInheritanceFunction:
563 spelling =
564 clang::MSInheritanceAttr::Spelling::Keyword_virtual_inheritance;
565 break;
566 case llvm::codeview::PointerToMemberRepresentation::Unknown:
567 spelling =
568 clang::MSInheritanceAttr::Spelling::Keyword_unspecified_inheritance;
569 break;
570 default:
571 spelling = clang::MSInheritanceAttr::Spelling::SpellingNotCalculated;
572 break;
573 }
574 tag->addAttr(clang::MSInheritanceAttr::CreateImplicit(
575 m_clang.getASTContext(), spelling));
576 }
577 return m_clang.getASTContext().getMemberPointerType(
578 pointee_type, /*Qualifier=*/std::nullopt,
579 class_type->getAsCXXRecordDecl());
580 }
581
582 clang::QualType pointer_type;
583 if (pointer.getMode() == PointerMode::LValueReference)
584 pointer_type = m_clang.getASTContext().getLValueReferenceType(pointee_type);
585 else if (pointer.getMode() == PointerMode::RValueReference)
586 pointer_type = m_clang.getASTContext().getRValueReferenceType(pointee_type);
587 else
588 pointer_type = m_clang.getASTContext().getPointerType(pointee_type);
589
590 if ((pointer.getOptions() & PointerOptions::Const) != PointerOptions::None)
591 pointer_type.addConst();
592
593 if ((pointer.getOptions() & PointerOptions::Volatile) != PointerOptions::None)
594 pointer_type.addVolatile();
595
596 if ((pointer.getOptions() & PointerOptions::Restrict) != PointerOptions::None)
597 pointer_type.addRestrict();
598
599 return pointer_type;
600}
601
602clang::QualType
603PdbAstBuilderClang::CreateModifierType(const ModifierRecord &modifier) {
604 clang::QualType unmodified_type = GetOrCreateClangType(modifier.ModifiedType);
605 if (unmodified_type.isNull())
606 return {};
607
608 if ((modifier.Modifiers & ModifierOptions::Const) != ModifierOptions::None)
609 unmodified_type.addConst();
610 if ((modifier.Modifiers & ModifierOptions::Volatile) != ModifierOptions::None)
611 unmodified_type.addVolatile();
612
613 return unmodified_type;
614}
615
617 const TagRecord &record) {
618 clang::DeclContext *context = nullptr;
619 std::string uname;
620 std::tie(context, uname) = CreateDeclInfoForType(record, id.index);
621 if (!context)
622 return {};
623
624 clang::TagTypeKind ttk = TranslateUdtKind(record);
625 ClangASTMetadata metadata;
626 metadata.SetUserID(toOpaqueUid(id));
627 metadata.SetIsDynamicCXXType(false);
628
629 CompilerType ct = m_clang.CreateRecordType(
630 context, OptionalClangModuleID(), uname, llvm::to_underlying(ttk),
632
633 lldbassert(ct.IsValid());
634
636
637 // Even if it's possible, don't complete it at this point. Just mark it
638 // forward resolved, and if/when LLDB needs the full definition, it can
639 // ask us.
640 clang::QualType result =
641 clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
642
643 TypeSystemClang::SetHasExternalStorage(result.getAsOpaquePtr(), true);
644 return result;
645}
646
648 auto iter = m_uid_to_decl.find(toOpaqueUid(uid));
649 if (iter != m_uid_to_decl.end())
650 return iter->second;
651 return nullptr;
652}
653
654clang::NamespaceDecl *
656 clang::DeclContext &context) {
657 clang::NamespaceDecl *ns = m_clang.GetUniqueNamespaceDeclaration(
658 IsAnonymousNamespaceName(name) ? nullptr : name, &context,
660 m_known_namespaces.insert(ns);
661 m_parent_to_namespaces[&context].insert(ns);
662 return ns;
663}
664
665clang::BlockDecl *
667 if (clang::Decl *decl = TryGetDecl(block_id))
668 return llvm::dyn_cast<clang::BlockDecl>(decl);
669
670 clang::DeclContext *scope = GetParentClangDeclContext(block_id);
671
672 clang::BlockDecl *block_decl =
673 m_clang.CreateBlockDeclaration(scope, OptionalClangModuleID());
674 m_uid_to_decl.insert({toOpaqueUid(block_id), block_decl});
675
676 DeclStatus status;
677 status.resolved = true;
678 status.uid = toOpaqueUid(block_id);
679 m_decl_to_status.insert({block_decl, status});
680
681 return block_decl;
682}
683
684clang::VarDecl *
686 clang::DeclContext &scope) {
687 VariableInfo var_info = GetVariableNameInfo(sym);
688 clang::QualType qt = GetOrCreateClangType(var_info.type);
689 if (qt.isNull())
690 return nullptr;
691
692 clang::VarDecl *var_decl = m_clang.CreateVariableDeclaration(
693 &scope, OptionalClangModuleID(), var_info.name.str().c_str(), qt);
694
695 m_uid_to_decl[toOpaqueUid(uid)] = var_decl;
696 DeclStatus status;
697 status.resolved = true;
698 status.uid = toOpaqueUid(uid);
699 m_decl_to_status.insert({var_decl, status});
700 return var_decl;
701}
702
703clang::VarDecl *
705 PdbCompilandSymId var_id) {
706 if (clang::Decl *decl = TryGetDecl(var_id))
707 return llvm::dyn_cast<clang::VarDecl>(decl);
708
709 clang::DeclContext *scope = GetOrCreateClangDeclContextForUid(scope_id);
710 if (!scope)
711 return nullptr;
712
714 m_clang.GetSymbolFile()->GetBackingSymbolFile());
715 PdbIndex &index = pdb->GetIndex();
716 CVSymbol sym = index.ReadSymbolRecord(var_id);
717 return CreateVariableDecl(PdbSymUid(var_id), sym, *scope);
718}
719
720clang::VarDecl *
722 if (clang::Decl *decl = TryGetDecl(var_id))
723 return llvm::dyn_cast<clang::VarDecl>(decl);
724
726 m_clang.GetSymbolFile()->GetBackingSymbolFile());
727 PdbIndex &index = pdb->GetIndex();
728 CVSymbol sym = index.ReadSymbolRecord(var_id);
730 return CreateVariableDecl(PdbSymUid(var_id), sym, *context);
731}
732
734 if (clang::Decl *decl = TryGetDecl(id)) {
735 if (auto *tnd = llvm::dyn_cast<clang::TypedefNameDecl>(decl))
736 return ToCompilerType(m_clang.getASTContext().getTypeDeclType(tnd));
737 return CompilerType();
738 }
739
741 m_clang.GetSymbolFile()->GetBackingSymbolFile());
742 PdbIndex &index = pdb->GetIndex();
743 CVSymbol sym = index.ReadSymbolRecord(id);
744 lldbassert(sym.kind() == S_UDT);
745 UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
746
747 clang::DeclContext *scope = GetParentClangDeclContext(id);
748
749 PdbTypeSymId real_type_id{udt.Type, false};
750 clang::QualType qt = GetOrCreateClangType(real_type_id);
751 if (qt.isNull() || !scope)
752 return CompilerType();
753
754 std::string uname = std::string(DropNameScope(udt.Name));
755
757 uname.c_str(), ToCompilerDeclContext(scope), 0);
758 DeclStatus status;
759 status.resolved = true;
760 status.uid = toOpaqueUid(id);
761 m_decl_to_status.insert({m_clang.GetAsTypedefDecl(ct), status});
762 return ct;
763}
764
766 CompilerType ct = m_clang.GetBasicType(type);
767 return clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
768}
769
771 if (type.index.isSimple())
772 return CreateSimpleType(type.index);
773
775 m_clang.GetSymbolFile()->GetBackingSymbolFile());
776 PdbIndex &index = pdb->GetIndex();
777 CVType cvt = index.tpi().getType(type.index);
778
779 if (cvt.kind() == LF_MODIFIER) {
780 ModifierRecord modifier;
781 llvm::cantFail(
782 TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier));
783 return CreateModifierType(modifier);
784 }
785
786 if (cvt.kind() == LF_POINTER) {
787 PointerRecord pointer;
788 llvm::cantFail(
789 TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer));
790 return CreatePointerType(pointer);
791 }
792
793 if (IsTagRecord(cvt)) {
795 if (tag.kind() == CVTagRecord::Union)
796 return CreateRecordType(type.index, tag.asUnion());
797 if (tag.kind() == CVTagRecord::Enum)
798 return CreateEnumType(type.index, tag.asEnum());
799 return CreateRecordType(type.index, tag.asClass());
800 }
801
802 if (cvt.kind() == LF_ARRAY) {
803 ArrayRecord ar;
804 llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar));
805 return CreateArrayType(ar);
806 }
807
808 if (cvt.kind() == LF_PROCEDURE) {
809 ProcedureRecord pr;
810 llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr));
811 return CreateFunctionType(pr.ArgumentList, pr.ReturnType, pr.CallConv);
812 }
813
814 if (cvt.kind() == LF_MFUNCTION) {
815 MemberFunctionRecord mfr;
816 llvm::cantFail(
817 TypeDeserializer::deserializeAs<MemberFunctionRecord>(cvt, mfr));
818 return CreateFunctionType(mfr.ArgumentList, mfr.ReturnType, mfr.CallConv);
819 }
820
821 return {};
822}
823
825 if (type.index.isNoneType())
826 return {};
827
828 lldb::user_id_t uid = toOpaqueUid(type);
829 auto iter = m_uid_to_type.find(uid);
830 if (iter != m_uid_to_type.end())
831 return iter->second;
832
834 m_clang.GetSymbolFile()->GetBackingSymbolFile());
835 PdbIndex &index = pdb->GetIndex();
836 PdbTypeSymId best_type = GetBestPossibleDecl(type, index.tpi());
837
838 clang::QualType qt;
839 if (best_type.index != type.index) {
840 // This is a forward decl. Call GetOrCreate on the full decl, then map the
841 // forward decl id to the full decl QualType.
842 clang::QualType qt = GetOrCreateClangType(best_type);
843 if (qt.isNull())
844 return {};
845 m_uid_to_type[toOpaqueUid(type)] = qt;
846 return qt;
847 }
848
849 // This is either a full decl, or a forward decl with no matching full decl
850 // in the debug info.
851 qt = CreateType(type);
852 if (qt.isNull())
853 return {};
854
855 m_uid_to_type[toOpaqueUid(type)] = qt;
856 if (IsTagRecord(type, index.tpi())) {
857 clang::TagDecl *tag = qt->getAsTagDecl();
858 lldbassert(m_decl_to_status.count(tag) == 0);
859
860 DeclStatus &status = m_decl_to_status[tag];
861 status.uid = uid;
862 status.resolved = false;
863 }
864 return qt;
865}
866
868 clang::QualType qt = GetOrCreateClangType(type);
869 if (qt.isNull())
870 return {};
871 return ToCompilerType(qt);
872}
873
875 PdbCompilandSymId func_id, llvm::StringRef func_name, TypeIndex func_ti,
876 CompilerType func_ct, uint32_t param_count,
877 clang::StorageClass func_storage, bool is_inline,
878 clang::DeclContext *parent) {
879 clang::FunctionDecl *function_decl = nullptr;
880 if (parent->isRecord()) {
882 m_clang.GetSymbolFile()->GetBackingSymbolFile());
883 PdbIndex &index = pdb->GetIndex();
884 clang::CanQualType parent_qt =
885 m_clang.getASTContext().getCanonicalTypeDeclType(
886 llvm::cast<clang::TypeDecl>(parent));
887 lldb::opaque_compiler_type_t parent_opaque_ty =
889 // FIXME: Remove this workaround.
890 auto iter = m_cxx_record_map.find(parent_opaque_ty);
891 if (iter != m_cxx_record_map.end()) {
892 if (iter->getSecond().contains({func_name, func_ct})) {
893 return nullptr;
894 }
895 }
896
897 CVType cvt = index.tpi().getType(func_ti);
898 MemberFunctionRecord func_record(static_cast<TypeRecordKind>(cvt.kind()));
899 llvm::cantFail(TypeDeserializer::deserializeAs<MemberFunctionRecord>(
900 cvt, func_record));
901 TypeIndex class_index = func_record.getClassType();
902
903 CVType parent_cvt = index.tpi().getType(class_index);
904 TagRecord tag_record = CVTagRecord::create(parent_cvt).asTag();
905 // If it's a forward reference, try to get the real TypeIndex.
906 if (tag_record.isForwardRef()) {
907 llvm::Expected<TypeIndex> eti =
908 index.tpi().findFullDeclForForwardRef(class_index);
909 if (eti) {
910 tag_record = CVTagRecord::create(index.tpi().getType(*eti)).asTag();
911 }
912 }
913
914 ConstString mangled_name(
915 pdb->FindMangledFunctionName(func_id).value_or(llvm::StringRef()));
916
917 if (!tag_record.FieldList.isSimple()) {
918 CVType field_list_cvt = index.tpi().getType(tag_record.FieldList);
919 FieldListRecord field_list;
920 if (llvm::Error error = TypeDeserializer::deserializeAs<FieldListRecord>(
921 field_list_cvt, field_list))
922 llvm::consumeError(std::move(error));
923 CreateMethodDecl process(index, m_clang, func_ti, function_decl,
924 parent_opaque_ty, func_name, mangled_name,
925 func_ct);
926 if (llvm::Error err = visitMemberRecordStream(field_list.Data, process))
927 llvm::consumeError(std::move(err));
928 }
929
930 if (!function_decl) {
931 function_decl = m_clang.AddMethodToCXXRecordType(
932 parent_opaque_ty, func_name, mangled_name, func_ct,
933 /*is_virtual=*/false, /*is_static=*/false,
934 /*is_inline=*/false, /*is_explicit=*/false,
935 /*is_attr_used=*/false, /*is_artificial=*/false);
936 }
937 m_cxx_record_map[parent_opaque_ty].insert({func_name, func_ct});
938 } else {
939 function_decl = m_clang.CreateFunctionDeclaration(
940 parent, OptionalClangModuleID(), func_name, func_ct, func_storage,
941 is_inline, /*asm_label=*/{});
942 CreateFunctionParameters(func_id, *function_decl, param_count);
943 }
944 return function_decl;
945}
946
948 PdbCompilandSymId inlinesite_id) {
950 m_clang.GetSymbolFile()->GetBackingSymbolFile());
951 PdbIndex &index = pdb->GetIndex();
952 CompilandIndexItem *cii = index.compilands().GetCompiland(inlinesite_id.modi);
953 CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(inlinesite_id.offset);
954 InlineSiteSym inline_site(static_cast<SymbolRecordKind>(sym.kind()));
955 cantFail(SymbolDeserializer::deserializeAs<InlineSiteSym>(sym, inline_site));
956
957 // Inlinee is the id index to the function id record that is inlined.
958 PdbTypeSymId func_id(inline_site.Inlinee, true);
959 // Look up the function decl by the id index to see if we have created a
960 // function decl for a different inlinesite that refers the same function.
961 if (clang::Decl *decl = TryGetDecl(func_id))
962 return llvm::dyn_cast<clang::FunctionDecl>(decl);
963 clang::FunctionDecl *function_decl =
964 CreateFunctionDeclFromId(func_id, inlinesite_id);
965 if (function_decl == nullptr)
966 return nullptr;
967
968 // Use inline site id in m_decl_to_status because it's expected to be a
969 // PdbCompilandSymId so that we can parse local variables info after it.
970 uint64_t inlinesite_uid = toOpaqueUid(inlinesite_id);
971 DeclStatus status;
972 status.resolved = true;
973 status.uid = inlinesite_uid;
974 m_decl_to_status.insert({function_decl, status});
975 // Use the index in IPI stream as uid in m_uid_to_decl, because index in IPI
976 // stream are unique and there could be multiple inline sites (different ids)
977 // referring the same inline function. This avoid creating multiple same
978 // inline function delcs.
979 uint64_t func_uid = toOpaqueUid(func_id);
980 lldbassert(m_uid_to_decl.count(func_uid) == 0);
981 m_uid_to_decl[func_uid] = function_decl;
982 return function_decl;
983}
984
985clang::FunctionDecl *
987 PdbCompilandSymId func_sid) {
988 lldbassert(func_tid.is_ipi);
990 m_clang.GetSymbolFile()->GetBackingSymbolFile());
991 PdbIndex &index = pdb->GetIndex();
992 std::optional<CVType> func_cvt =
993 index.ipi().typeCollection().tryGetType(func_tid.index);
994 if (!func_cvt)
995 return nullptr;
996 llvm::StringRef func_name;
997 TypeIndex func_ti;
998 clang::DeclContext *parent = nullptr;
999 switch (func_cvt->kind()) {
1000 case LF_MFUNC_ID: {
1001 MemberFuncIdRecord mfr;
1002 cantFail(
1003 TypeDeserializer::deserializeAs<MemberFuncIdRecord>(*func_cvt, mfr));
1004 func_name = mfr.getName();
1005 func_ti = mfr.getFunctionType();
1006 PdbTypeSymId class_type_id(mfr.ClassType, false);
1007 parent = GetOrCreateClangDeclContextForUid(class_type_id);
1008 break;
1009 }
1010 case LF_FUNC_ID: {
1011 FuncIdRecord fir;
1012 cantFail(TypeDeserializer::deserializeAs<FuncIdRecord>(*func_cvt, fir));
1013 func_name = fir.getName();
1014 func_ti = fir.getFunctionType();
1016 if (!fir.ParentScope.isNoneType()) {
1017 CVType parent_cvt = index.ipi().getType(fir.ParentScope);
1018 if (parent_cvt.kind() == LF_STRING_ID) {
1019 StringIdRecord sir;
1020 cantFail(
1021 TypeDeserializer::deserializeAs<StringIdRecord>(parent_cvt, sir));
1022 parent = GetOrCreateNamespaceDecl(sir.String.data(), *parent);
1023 }
1024 }
1025 break;
1026 }
1027 default:
1028 lldbassert(false && "Invalid function id type!");
1029 }
1030 clang::QualType func_qt = GetOrCreateClangType(func_ti);
1031 if (func_qt.isNull() || !parent)
1032 return nullptr;
1033 CompilerType func_ct = ToCompilerType(func_qt);
1034 uint32_t param_count =
1035 llvm::cast<clang::FunctionProtoType>(func_qt)->getNumParams();
1036 return CreateFunctionDecl(func_sid, func_name, func_ti, func_ct, param_count,
1037 clang::SC_None, true, parent);
1038}
1039
1040clang::FunctionDecl *
1042 if (clang::Decl *decl = TryGetDecl(func_id))
1043 return llvm::dyn_cast<clang::FunctionDecl>(decl);
1044
1045 clang::DeclContext *parent = GetParentClangDeclContext(PdbSymUid(func_id));
1046 if (!parent)
1047 return nullptr;
1048 std::string context_name;
1049 if (clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(parent)) {
1050 context_name = ns->getQualifiedNameAsString();
1051 } else if (clang::TagDecl *tag = llvm::dyn_cast<clang::TagDecl>(parent)) {
1052 context_name = tag->getQualifiedNameAsString();
1053 }
1054
1056 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1057 PdbIndex &index = pdb->GetIndex();
1058 CVSymbol cvs = index.ReadSymbolRecord(func_id);
1059 ProcSym proc(static_cast<SymbolRecordKind>(cvs.kind()));
1060 llvm::cantFail(SymbolDeserializer::deserializeAs<ProcSym>(cvs, proc));
1061
1062 PdbTypeSymId type_id(proc.FunctionType);
1063 clang::QualType qt = GetOrCreateClangType(type_id);
1064 if (qt.isNull())
1065 return nullptr;
1066
1067 clang::StorageClass storage = clang::SC_None;
1068 if (proc.Kind == SymbolRecordKind::ProcSym)
1069 storage = clang::SC_Static;
1070
1071 const clang::FunctionProtoType *func_type =
1072 llvm::dyn_cast<clang::FunctionProtoType>(qt);
1073
1074 CompilerType func_ct = ToCompilerType(qt);
1075
1076 llvm::StringRef proc_name = proc.Name;
1077 proc_name.consume_front(context_name);
1078 proc_name.consume_front("::");
1079 clang::FunctionDecl *function_decl =
1080 CreateFunctionDecl(func_id, proc_name, proc.FunctionType, func_ct,
1081 func_type->getNumParams(), storage, false, parent);
1082 if (function_decl == nullptr)
1083 return nullptr;
1084
1085 lldbassert(m_uid_to_decl.count(toOpaqueUid(func_id)) == 0);
1086 m_uid_to_decl[toOpaqueUid(func_id)] = function_decl;
1087 DeclStatus status;
1088 status.resolved = true;
1089 status.uid = toOpaqueUid(func_id);
1090 m_decl_to_status.insert({function_decl, status});
1091
1092 return function_decl;
1093}
1094
1098
1103
1107
1109 PdbCompilandSymId var_id) {
1110 GetOrCreateVariableDecl(scope_id, var_id);
1111}
1112
1116
1118 PdbCompilandSymId func_id, clang::FunctionDecl &function_decl,
1119 uint32_t param_count) {
1121 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1122 PdbIndex &index = pdb->GetIndex();
1123 CompilandIndexItem *cii = index.compilands().GetCompiland(func_id.modi);
1124 CVSymbolArray scope =
1125 cii->m_debug_stream.getSymbolArrayForScope(func_id.offset);
1126
1127 scope.drop_front();
1128 auto begin = scope.begin();
1129 auto end = scope.end();
1130 std::vector<clang::ParmVarDecl *> params;
1131 for (uint32_t i = 0; i < param_count && begin != end;) {
1132 uint32_t record_offset = begin.offset();
1133 CVSymbol sym = *begin++;
1134
1135 TypeIndex param_type;
1136 llvm::StringRef param_name;
1137 switch (sym.kind()) {
1138 case S_REGREL32: {
1139 RegRelativeSym reg(SymbolRecordKind::RegRelativeSym);
1140 cantFail(SymbolDeserializer::deserializeAs<RegRelativeSym>(sym, reg));
1141 param_type = reg.Type;
1142 param_name = reg.Name;
1143 break;
1144 }
1145 case S_REGREL32_INDIR: {
1146 RegRelativeIndirSym reg(SymbolRecordKind::RegRelativeIndirSym);
1147 cantFail(
1148 SymbolDeserializer::deserializeAs<RegRelativeIndirSym>(sym, reg));
1149 param_type = reg.Type;
1150 param_name = reg.Name;
1151 break;
1152 }
1153 case S_REGISTER: {
1154 RegisterSym reg(SymbolRecordKind::RegisterSym);
1155 cantFail(SymbolDeserializer::deserializeAs<RegisterSym>(sym, reg));
1156 param_type = reg.Index;
1157 param_name = reg.Name;
1158 break;
1159 }
1160 case S_LOCAL: {
1161 LocalSym local(SymbolRecordKind::LocalSym);
1162 cantFail(SymbolDeserializer::deserializeAs<LocalSym>(sym, local));
1163 if ((local.Flags & LocalSymFlags::IsParameter) == LocalSymFlags::None)
1164 continue;
1165 param_type = local.Type;
1166 param_name = local.Name;
1167 break;
1168 }
1169 case S_BLOCK32:
1170 case S_INLINESITE:
1171 case S_INLINESITE2:
1172 // All parameters should come before the first block/inlinesite. If that
1173 // isn't the case, then perhaps this is bad debug info that doesn't
1174 // contain information about all parameters.
1175 return;
1176 default:
1177 continue;
1178 }
1179
1180 PdbCompilandSymId param_uid(func_id.modi, record_offset);
1181 clang::QualType qt = GetOrCreateClangType(param_type);
1182 if (qt.isNull())
1183 return;
1184
1185 CompilerType param_type_ct = m_clang.GetType(qt);
1186 clang::ParmVarDecl *param = m_clang.CreateParameterDeclaration(
1187 &function_decl, OptionalClangModuleID(), param_name.str().c_str(),
1188 param_type_ct, clang::SC_None, true);
1189 lldbassert(m_uid_to_decl.count(toOpaqueUid(param_uid)) == 0);
1190
1191 m_uid_to_decl[toOpaqueUid(param_uid)] = param;
1192 params.push_back(param);
1193 ++i;
1194 }
1195
1196 if (!params.empty() && params.size() == param_count)
1197 function_decl.setParams(params);
1198}
1199
1201 const EnumRecord &er) {
1202 clang::DeclContext *decl_context = nullptr;
1203 std::string uname;
1204 std::tie(decl_context, uname) = CreateDeclInfoForType(er, id.index);
1205 if (!decl_context)
1206 return {};
1207
1208 clang::QualType underlying_type = GetOrCreateClangType(er.UnderlyingType);
1209 if (underlying_type.isNull())
1210 return {};
1211
1212 Declaration declaration;
1213 CompilerType enum_ct = m_clang.CreateEnumerationType(
1214 uname, decl_context, OptionalClangModuleID(), declaration,
1215 ToCompilerType(underlying_type), er.isScoped());
1216
1219
1220 return clang::QualType::getFromOpaquePtr(enum_ct.GetOpaqueQualType());
1221}
1222
1223clang::QualType PdbAstBuilderClang::CreateArrayType(const ArrayRecord &ar) {
1224 clang::QualType element_type = GetOrCreateClangType(ar.ElementType);
1226
1228 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1229 PdbIndex &index = pdb->GetIndex();
1230 uint64_t element_size = GetSizeOfType({ar.ElementType}, index.tpi());
1231 if (element_type.isNull() || element_size == 0)
1232 return {};
1233 uint64_t element_count = ar.Size / element_size;
1234
1235 CompilerType array_ct = m_clang.CreateArrayType(ToCompilerType(element_type),
1236 element_count, false);
1237 return clang::QualType::getFromOpaquePtr(array_ct.GetOpaqueQualType());
1238}
1239
1241 TypeIndex args_type_idx, TypeIndex return_type_idx,
1242 llvm::codeview::CallingConvention calling_convention) {
1244 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1245 PdbIndex &index = pdb->GetIndex();
1246 TpiStream &stream = index.tpi();
1247 CVType args_cvt = stream.getType(args_type_idx);
1248 ArgListRecord args;
1249 llvm::cantFail(
1250 TypeDeserializer::deserializeAs<ArgListRecord>(args_cvt, args));
1251
1252 llvm::ArrayRef<TypeIndex> arg_indices = llvm::ArrayRef(args.ArgIndices);
1253 bool is_variadic = IsCVarArgsFunction(arg_indices);
1254 if (is_variadic)
1255 arg_indices = arg_indices.drop_back();
1256
1257 std::vector<CompilerType> arg_types;
1258 arg_types.reserve(arg_indices.size());
1259
1260 for (TypeIndex arg_index : arg_indices) {
1261 clang::QualType arg_type = GetOrCreateClangType(arg_index);
1262 if (arg_type.isNull())
1263 continue;
1264 arg_types.push_back(ToCompilerType(arg_type));
1265 }
1266
1267 clang::QualType return_type = GetOrCreateClangType(return_type_idx);
1268 if (return_type.isNull())
1269 return {};
1270
1271 std::optional<clang::CallingConv> cc =
1272 TranslateCallingConvention(calling_convention);
1273 if (!cc)
1274 return {};
1275
1276 CompilerType return_ct = ToCompilerType(return_type);
1277 CompilerType func_sig_ast_type =
1278 m_clang.CreateFunctionType(return_ct, arg_types, is_variadic, 0, *cc);
1279
1280 return clang::QualType::getFromOpaquePtr(
1281 func_sig_ast_type.GetOpaqueQualType());
1282}
1283
1284static bool isTagDecl(clang::DeclContext &context) {
1285 return llvm::isa<clang::TagDecl>(&context);
1286}
1287
1288static bool isFunctionDecl(clang::DeclContext &context) {
1289 return llvm::isa<clang::FunctionDecl>(&context);
1290}
1291
1292static bool isBlockDecl(clang::DeclContext &context) {
1293 return llvm::isa<clang::BlockDecl>(&context);
1294}
1295
1296void PdbAstBuilderClang::ParseNamespace(clang::DeclContext &context) {
1297 clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(&context);
1298 if (m_parsed_namespaces.contains(ns))
1299 return;
1300 std::string qname = ns->getQualifiedNameAsString();
1302 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1303 PdbIndex &index = pdb->GetIndex();
1304 TypeIndex ti{index.tpi().TypeIndexBegin()};
1305 for (const CVType &cvt : index.tpi().typeArray()) {
1306 PdbTypeSymId tid{ti};
1307 ++ti;
1308
1309 if (!IsTagRecord(cvt))
1310 continue;
1311
1313
1314 // Call CreateDeclInfoForType unconditionally so that the namespace info
1315 // gets created. But only call CreateRecordType if the namespace name
1316 // matches.
1317 clang::DeclContext *context = nullptr;
1318 std::string uname;
1319 std::tie(context, uname) = CreateDeclInfoForType(tag.asTag(), tid.index);
1320 if (!context || !context->isNamespace())
1321 continue;
1322
1323 clang::NamespaceDecl *ns = llvm::cast<clang::NamespaceDecl>(context);
1324 llvm::StringRef ns_name = ns->getName();
1325 if (ns_name.starts_with(qname)) {
1326 ns_name = ns_name.drop_front(qname.size());
1327 if (ns_name.starts_with("::"))
1329 }
1330 }
1332 m_parsed_namespaces.insert(ns);
1333}
1334
1336 llvm::call_once(m_parse_all_types, [this]() {
1338 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1339 PdbIndex &index = pdb->GetIndex();
1340 TypeIndex ti{index.tpi().TypeIndexBegin()};
1341 for (const CVType &cvt : index.tpi().typeArray()) {
1342 PdbTypeSymId tid{ti};
1343 ++ti;
1344
1345 if (!IsTagRecord(cvt))
1346 continue;
1347
1349 }
1350 });
1351}
1352
1354 llvm::call_once(m_parse_functions_and_non_local_vars, [this]() {
1356 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1357 PdbIndex &index = pdb->GetIndex();
1358 uint32_t module_count = index.dbi().modules().getModuleCount();
1359 for (uint16_t modi = 0; modi < module_count; ++modi) {
1361 const CVSymbolArray &symbols = cii.m_debug_stream.getSymbolArray();
1362 auto iter = symbols.begin();
1363 while (iter != symbols.end()) {
1364 PdbCompilandSymId sym_id{modi, iter.offset()};
1365
1366 switch (iter->kind()) {
1367 case S_GPROC32:
1368 case S_LPROC32:
1370 iter = symbols.at(getScopeEndOffset(*iter));
1371 break;
1372 case S_GDATA32:
1373 case S_GTHREAD32:
1374 case S_LDATA32:
1375 case S_LTHREAD32:
1377 ++iter;
1378 break;
1379 default:
1380 ++iter;
1381 continue;
1382 }
1383 }
1384 }
1385 });
1386}
1387
1388static CVSymbolArray skipFunctionParameters(clang::Decl &decl,
1389 const CVSymbolArray &symbols) {
1390 clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>(&decl);
1391 if (!func_decl)
1392 return symbols;
1393 unsigned int params = func_decl->getNumParams();
1394 if (params == 0)
1395 return symbols;
1396
1397 CVSymbolArray result = symbols;
1398
1399 while (!result.empty()) {
1400 if (params == 0)
1401 return result;
1402
1403 CVSymbol sym = *result.begin();
1404 result.drop_front();
1405
1406 if (!isLocalVariableType(sym.kind()))
1407 continue;
1408
1409 --params;
1410 }
1411 return result;
1412}
1413
1416 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1417 PdbIndex &index = pdb->GetIndex();
1418 CVSymbol sym = index.ReadSymbolRecord(block_id);
1419 lldbassert(sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32 ||
1420 sym.kind() == S_BLOCK32 || sym.kind() == S_INLINESITE);
1421 CompilandIndexItem &cii =
1422 index.compilands().GetOrCreateCompiland(block_id.modi);
1423 CVSymbolArray symbols =
1424 cii.m_debug_stream.getSymbolArrayForScope(block_id.offset);
1425
1426 // Function parameters should already have been created when the function was
1427 // parsed.
1428 if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32)
1429 symbols =
1430 skipFunctionParameters(*m_uid_to_decl[toOpaqueUid(block_id)], symbols);
1431
1432 symbols.drop_front();
1433 auto begin = symbols.begin();
1434 while (begin != symbols.end()) {
1435 PdbCompilandSymId child_sym_id(block_id.modi, begin.offset());
1436 GetOrCreateSymbolForId(child_sym_id);
1437 if (begin->kind() == S_BLOCK32 || begin->kind() == S_INLINESITE) {
1438 ParseBlockChildren(child_sym_id);
1439 begin = symbols.at(getScopeEndOffset(*begin));
1440 }
1441 ++begin;
1442 }
1443}
1444
1446 clang::DeclContext &context) {
1447
1448 clang::Decl *decl = clang::Decl::castFromDeclContext(&context);
1449 lldbassert(decl);
1450
1451 auto iter = m_decl_to_status.find(decl);
1452 lldbassert(iter != m_decl_to_status.end());
1453
1454 if (auto *tag = llvm::dyn_cast<clang::TagDecl>(&context)) {
1455 CompleteTagDecl(*tag);
1456 return;
1457 }
1458
1459 if (isFunctionDecl(context) || isBlockDecl(context)) {
1460 PdbCompilandSymId block_id = PdbSymUid(iter->second.uid).asCompilandSym();
1461 ParseBlockChildren(block_id);
1462 }
1463}
1464
1466 clang::DeclContext *dc = FromCompilerDeclContext(context);
1467 if (!dc)
1468 return;
1469
1470 // Namespaces aren't explicitly represented in the debug info, and the only
1471 // way to parse them is to parse all type info, demangling every single type
1472 // and trying to reconstruct the DeclContext hierarchy this way. Since this
1473 // is an expensive operation, we have to special case it so that we do other
1474 // work (such as parsing the items that appear within the namespaces) at the
1475 // same time.
1476 if (dc->isTranslationUnit()) {
1477 ParseAllTypes();
1479 return;
1480 }
1481
1482 if (dc->isNamespace()) {
1483 ParseNamespace(*dc);
1484 return;
1485 }
1486
1487 if (isTagDecl(*dc) || isFunctionDecl(*dc) || isBlockDecl(*dc)) {
1489 return;
1490 }
1491}
1492
1494 return m_clang.GetCompilerDecl(decl);
1495}
1496
1498 return m_clang.GetType(qt);
1499}
1500
1504
1506PdbAstBuilderClang::ToCompilerDeclContext(clang::DeclContext *context) {
1507 return m_clang.CreateDeclContext(context);
1508}
1509
1511 if (decl.GetTypeSystem() != nullptr)
1512 return ClangUtil::GetDecl(decl);
1513 return nullptr;
1514}
1515
1516clang::DeclContext *
1518 return static_cast<clang::DeclContext *>(context.GetOpaqueDeclContext());
1519}
1520
1521void PdbAstBuilderClang::Dump(Stream &stream, llvm::StringRef filter,
1522 bool show_color) {
1523 m_clang.Dump(stream.AsRawOstream(), filter, show_color);
1524}
1525
1528 llvm::StringRef name) {
1529 clang::DeclContext *parent = FromCompilerDeclContext(parent_ctx);
1530 NamespaceSet *set;
1531
1532 if (parent) {
1533 auto it = m_parent_to_namespaces.find(parent);
1534 if (it == m_parent_to_namespaces.end())
1535 return {};
1536
1537 set = &it->second;
1538 } else {
1539 // In this case, search through all known namespaces
1540 set = &m_known_namespaces;
1541 }
1542 assert(set);
1543
1544 for (clang::NamespaceDecl *namespace_decl : *set)
1545 if (namespace_decl->getName() == name)
1546 return ToCompilerDeclContext(namespace_decl);
1547
1548 for (clang::NamespaceDecl *namespace_decl : *set)
1549 if (namespace_decl->isAnonymousNamespace())
1550 return FindNamespaceDecl(ToCompilerDeclContext(namespace_decl), name);
1551
1552 return {};
1553}
static llvm::raw_ostream & error(Stream &strm)
#define lldbassert(x)
Definition LLDBAssert.h:16
static bool isFunctionDecl(clang::DeclContext &context)
static bool isBlockDecl(clang::DeclContext &context)
static CVSymbolArray skipFunctionParameters(clang::Decl &decl, const CVSymbolArray &symbols)
static std::optional< clang::CallingConv > TranslateCallingConvention(llvm::codeview::CallingConvention conv)
static bool isTagDecl(clang::DeclContext &context)
static bool isLocalVariableType(SymbolKind K)
static bool IsAnonymousNamespaceName(llvm::StringRef name)
static clang::TagTypeKind TranslateUdtKind(const TagRecord &cr)
static bool IsCVarArgsFunction(llvm::ArrayRef< TypeIndex > args)
static bool AnyScopesHaveTemplateParams(llvm::ArrayRef< llvm::ms_demangle::Node * > scopes)
llvm::ArrayRef< MSVCUndecoratedNameSpecifier > GetSpecifiers() const
bool CompleteType(const CompilerType &compiler_type)
void SetUserID(lldb::user_id_t user_id)
void SetIsDynamicCXXType(std::optional< bool > b)
Represents a generic declaration context in a program.
Represents a generic declaration such as a function declaration.
TypeSystem * GetTypeSystem() const
Generic representation of a type in a programming language.
lldb::opaque_compiler_type_t GetOpaqueQualType() const
CompilerType CreateTypedef(const char *name, const CompilerDeclContext &decl_ctx, uint32_t payload) const
Create a typedef to this type using "name" as the name of the typedef this type is valid and the type...
A uniqued constant string class.
Definition ConstString.h:40
A class that describes the declaration location of a lldb object.
Definition Declaration.h:24
A stream class that can stream formatted output to a file.
Definition Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:418
A TypeSystem implementation based on Clang.
static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type, bool has_extern)
static bool StartTagDeclarationDefinition(const CompilerType &type)
static void RequireCompleteType(CompilerType type)
Complete a type from debug info, or mark it as forcefully completed if there is no definition of the ...
CompilandIndexItem & GetOrCreateCompiland(uint16_t modi)
const CompilandIndexItem * GetCompiland(uint16_t modi) const
clang::Decl * GetOrCreateSymbolForId(PdbCompilandSymId id)
std::pair< clang::DeclContext *, std::string > CreateDeclInfoForType(const llvm::codeview::TagRecord &record, TypeIndex ti)
void ParseDeclsForSimpleContext(clang::DeclContext &context)
CompilerDeclContext ToCompilerDeclContext(clang::DeclContext *context)
clang::FunctionDecl * GetOrCreateInlinedFunctionDecl(PdbCompilandSymId inlinesite_id)
llvm::DenseMap< clang::DeclContext *, NamespaceSet > m_parent_to_namespaces
void CreateFunctionParameters(PdbCompilandSymId func_id, clang::FunctionDecl &function_decl, uint32_t param_count)
bool CompleteType(CompilerType ct) override
clang::QualType GetBasicType(lldb::BasicType type)
clang::Decl * TryGetDecl(PdbSymUid uid) const
clang::QualType CreateType(PdbTypeSymId type)
clang::QualType CreateFunctionType(TypeIndex args_type_idx, TypeIndex return_type_idx, llvm::codeview::CallingConvention calling_convention)
clang::QualType CreateArrayType(const llvm::codeview::ArrayRecord &array)
llvm::DenseMap< clang::Decl *, DeclStatus > m_decl_to_status
llvm::DenseMap< lldb::opaque_compiler_type_t, llvm::SmallSet< std::pair< llvm::StringRef, CompilerType >, 8 > > m_cxx_record_map
clang::FunctionDecl * CreateFunctionDeclFromId(PdbTypeSymId func_tid, PdbCompilandSymId func_sid)
CompilerType GetOrCreateType(PdbTypeSymId type) override
clang::QualType CreatePointerType(const llvm::codeview::PointerRecord &pointer)
clang::QualType CreateModifierType(const llvm::codeview::ModifierRecord &modifier)
std::pair< clang::DeclContext *, std::string > CreateDeclInfoForUndecoratedName(llvm::StringRef uname)
clang::FunctionDecl * GetOrCreateFunctionDecl(PdbCompilandSymId func_id)
clang::NamespaceDecl * GetOrCreateNamespaceDecl(const char *name, clang::DeclContext &context)
void EnsureVariable(PdbCompilandSymId scope_id, PdbCompilandSymId var_id) override
clang::VarDecl * CreateVariableDecl(PdbSymUid uid, llvm::codeview::CVSymbol sym, clang::DeclContext &scope)
void ParseDeclsForContext(CompilerDeclContext context) override
CompilerDecl ToCompilerDecl(clang::Decl *decl)
void EnsureFunction(PdbCompilandSymId func_id) override
llvm::DenseMap< lldb::user_id_t, clang::QualType > m_uid_to_type
void EnsureBlock(PdbCompilandSymId block_id) override
llvm::DenseMap< lldb::user_id_t, clang::Decl * > m_uid_to_decl
CompilerDecl GetOrCreateDeclForUid(PdbSymUid uid) override
CompilerDeclContext FindNamespaceDecl(CompilerDeclContext parent_ctx, llvm::StringRef name) override
void ParseNamespace(clang::DeclContext &parent)
llvm::DenseSet< clang::NamespaceDecl * > NamespaceSet
clang::VarDecl * GetOrCreateVariableDecl(PdbCompilandSymId scope_id, PdbCompilandSymId var_id)
clang::BlockDecl * GetOrCreateBlockDecl(PdbCompilandSymId block_id)
clang::QualType CreateSimpleType(TypeIndex ti)
clang::DeclContext * GetParentClangDeclContext(PdbSymUid uid)
clang::QualType FromCompilerType(CompilerType ct)
clang::QualType CreateEnumType(PdbTypeSymId id, const llvm::codeview::EnumRecord &record)
CompilerType GetOrCreateTypedefType(PdbGlobalSymId id) override
clang::QualType CreateRecordType(PdbTypeSymId id, const llvm::codeview::TagRecord &record)
CompilerDeclContext GetParentDeclContext(PdbSymUid uid) override
clang::DeclContext * FromCompilerDeclContext(CompilerDeclContext context)
clang::FunctionDecl * CreateFunctionDecl(PdbCompilandSymId func_id, llvm::StringRef func_name, TypeIndex func_ti, CompilerType func_ct, uint32_t param_count, clang::StorageClass func_storage, bool is_inline, clang::DeclContext *parent)
CompilerDeclContext GetOrCreateDeclContextForUid(PdbSymUid uid) override
void EnsureInlinedFunction(PdbCompilandSymId inlinesite_id) override
CompilerType ToCompilerType(clang::QualType qt)
clang::QualType GetOrCreateClangType(PdbTypeSymId type)
void ParseBlockChildren(PdbCompilandSymId block_id)
void Dump(Stream &stream, llvm::StringRef filter, bool show_color) override
clang::Decl * FromCompilerDecl(CompilerDecl decl)
clang::DeclContext * GetOrCreateClangDeclContextForUid(PdbSymUid uid)
PdbIndex - Lazy access to the important parts of a PDB file.
Definition PdbIndex.h:47
llvm::pdb::TpiStream & ipi()
Definition PdbIndex.h:127
CompileUnitIndex & compilands()
Definition PdbIndex.h:142
llvm::pdb::DbiStream & dbi()
Definition PdbIndex.h:121
llvm::codeview::CVSymbol ReadSymbolRecord(PdbCompilandSymId cu_sym) const
Definition PdbIndex.cpp:187
llvm::pdb::TpiStream & tpi()
Definition PdbIndex.h:124
PdbGlobalSymId asGlobalSym() const
PdbCompilandSymId asCompilandSym() const
PdbTypeSymId asTypeSym() const
PdbSymUidKind kind() const
llvm::StringRef DropNameScope(llvm::StringRef name)
Definition PdbUtil.cpp:600
uint64_t toOpaqueUid(const T &cid)
Definition PdbSymUid.h:114
lldb::BasicType GetCompilerTypeForSimpleKind(llvm::codeview::SimpleTypeKind kind)
VariableInfo GetVariableNameInfo(llvm::codeview::CVSymbol symbol)
bool IsTagRecord(llvm::codeview::CVType cvt)
Definition PdbUtil.cpp:518
llvm::codeview::TypeIndex LookThroughModifierRecord(llvm::codeview::CVType modifier)
bool IsForwardRefUdt(llvm::codeview::CVType cvt)
llvm::codeview::TypeIndex GetFieldListIndex(llvm::codeview::CVType cvt)
size_t GetSizeOfType(PdbTypeSymId id, llvm::pdb::TpiStream &tpi)
Definition PdbUtil.cpp:1114
PdbTypeSymId GetBestPossibleDecl(PdbTypeSymId id, llvm::pdb::TpiStream &tpi)
A class that represents a running process on the host machine.
void * opaque_compiler_type_t
Definition lldb-types.h:90
BasicType
Basic types enumeration for the public API SBType::GetBasicType().
@ eLanguageTypeC_plus_plus
ISO C++:1998.
uint64_t user_id_t
Definition lldb-types.h:82
static clang::QualType GetQualType(const CompilerType &ct)
Definition ClangUtil.cpp:36
static clang::Decl * GetDecl(const CompilerDecl &decl)
Returns the clang::Decl of the given CompilerDecl.
Definition ClangUtil.cpp:31
const llvm::codeview::UnionRecord & asUnion() const
Definition PdbUtil.h:62
static CVTagRecord create(llvm::codeview::CVType type)
Definition PdbUtil.cpp:198
const llvm::codeview::ClassRecord & asClass() const
Definition PdbUtil.h:52
const llvm::codeview::EnumRecord & asEnum() const
Definition PdbUtil.h:57
const llvm::codeview::TagRecord & asTag() const
Definition PdbUtil.h:44
Represents a single compile unit.
llvm::pdb::ModuleDebugStreamRef m_debug_stream
llvm::codeview::TypeIndex index
Definition PdbSymUid.h:76
llvm::codeview::TypeIndex type
Definition PdbUtil.h:114