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