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