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