Bug Summary

File:build/source/lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp
Warning:line 1238, column 23
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name PdbAstBuilder.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-16/lib/clang/16.0.0 -isystem /usr/include/libxml2 -I tools/lldb/source/Plugins/SymbolFile/NativePDB -I /build/source/lldb/source/Plugins/SymbolFile/NativePDB -I /build/source/lldb/include -I tools/lldb/include -I include -I /build/source/llvm/include -I /usr/include/python3.9 -I /build/source/clang/include -I tools/lldb/../clang/include -I /build/source/lldb/source -I tools/lldb/source -D HAVE_ROUND -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/source/= -fcoverage-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/source/= -source-date-epoch 1670066131 -O2 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -Wno-misleading-indentation -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-strict-aliasing -Wno-stringop-truncation -std=c++17 -fdeprecated-macro -fdebug-compilation-dir=/build/source/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/source/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-12-03-132955-15984-1 -x c++ /build/source/lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp
1#include "PdbAstBuilder.h"
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 "Plugins/ExpressionParser/Clang/ClangASTMetadata.h"
18#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
19#include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
20#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
21#include "lldb/Core/Module.h"
22#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Utility/LLDBAssert.h"
24#include "PdbUtil.h"
25#include "UdtRecordCompleter.h"
26#include "SymbolFileNativePDB.h"
27
28using namespace lldb_private;
29using namespace lldb_private::npdb;
30using namespace llvm::codeview;
31using namespace llvm::pdb;
32
33namespace {
34struct CreateMethodDecl : public TypeVisitorCallbacks {
35 CreateMethodDecl(PdbIndex &m_index, TypeSystemClang &m_clang,
36 TypeIndex func_type_index,
37 clang::FunctionDecl *&function_decl,
38 lldb::opaque_compiler_type_t parent_ty,
39 llvm::StringRef proc_name, CompilerType func_ct)
40 : m_index(m_index), m_clang(m_clang), func_type_index(func_type_index),
41 function_decl(function_decl), parent_ty(parent_ty),
42 proc_name(proc_name), func_ct(func_ct) {}
43 PdbIndex &m_index;
44 TypeSystemClang &m_clang;
45 TypeIndex func_type_index;
46 clang::FunctionDecl *&function_decl;
47 lldb::opaque_compiler_type_t parent_ty;
48 llvm::StringRef proc_name;
49 CompilerType func_ct;
50
51 llvm::Error visitKnownMember(CVMemberRecord &cvr,
52 OverloadedMethodRecord &overloaded) override {
53 TypeIndex method_list_idx = overloaded.MethodList;
54
55 CVType method_list_type = m_index.tpi().getType(method_list_idx);
56 assert(method_list_type.kind() == LF_METHODLIST)(static_cast <bool> (method_list_type.kind() == LF_METHODLIST
) ? void (0) : __assert_fail ("method_list_type.kind() == LF_METHODLIST"
, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 56, __extension__ __PRETTY_FUNCTION__))
;
57
58 MethodOverloadListRecord method_list;
59 llvm::cantFail(TypeDeserializer::deserializeAs<MethodOverloadListRecord>(
60 method_list_type, method_list));
61
62 for (const OneMethodRecord &method : method_list.Methods) {
63 if (method.getType().getIndex() == func_type_index.getIndex())
64 AddMethod(overloaded.Name, method.getAccess(), method.getOptions(),
65 method.Attrs);
66 }
67
68 return llvm::Error::success();
69 }
70
71 llvm::Error visitKnownMember(CVMemberRecord &cvr,
72 OneMethodRecord &record) override {
73 AddMethod(record.getName(), record.getAccess(), record.getOptions(),
74 record.Attrs);
75 return llvm::Error::success();
76 }
77
78 void AddMethod(llvm::StringRef name, MemberAccess access,
79 MethodOptions options, MemberAttributes attrs) {
80 if (name != proc_name || function_decl)
81 return;
82 lldb::AccessType access_type = TranslateMemberAccess(access);
83 bool is_virtual = attrs.isVirtual();
84 bool is_static = attrs.isStatic();
85 bool is_artificial = (options & MethodOptions::CompilerGenerated) ==
86 MethodOptions::CompilerGenerated;
87 function_decl = m_clang.AddMethodToCXXRecordType(
88 parent_ty, proc_name,
89 /*mangled_name=*/nullptr, func_ct, /*access=*/access_type,
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::TTK_Class;
101 case TypeRecordKind::Struct:
102 return clang::TTK_Struct;
103 case TypeRecordKind::Union:
104 return clang::TTK_Union;
105 case TypeRecordKind::Interface:
106 return clang::TTK_Interface;
107 case TypeRecordKind::Enum:
108 return clang::TTK_Enum;
109 default:
110 lldbassert(false && "Invalid tag record kind!")lldb_private::lldb_assert(static_cast<bool>(false &&
"Invalid tag record kind!"), "false && \"Invalid tag record kind!\""
, __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 110)
;
111 return clang::TTK_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 llvm::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 llvm::None;
154 }
155}
156
157static bool IsAnonymousNamespaceName(llvm::StringRef name) {
158 return name == "`anonymous namespace'" || name == "`anonymous-namespace'";
159}
160
161PdbAstBuilder::PdbAstBuilder(TypeSystemClang &clang) : m_clang(clang) {}
162
163lldb_private::CompilerDeclContext PdbAstBuilder::GetTranslationUnitDecl() {
164 return ToCompilerDeclContext(*m_clang.GetTranslationUnitDecl());
165}
166
167std::pair<clang::DeclContext *, std::string>
168PdbAstBuilder::CreateDeclInfoForType(const TagRecord &record, TypeIndex ti) {
169 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
170 m_clang.GetSymbolFile()->GetBackingSymbolFile());
171 // FIXME: Move this to GetDeclContextContainingUID.
172 if (!record.hasUniqueName())
173 return CreateDeclInfoForUndecoratedName(record.Name);
174
175 llvm::ms_demangle::Demangler demangler;
176 StringView sv(record.UniqueName.begin(), record.UniqueName.size());
177 llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
178 if (demangler.Error)
179 return {m_clang.GetTranslationUnitDecl(), std::string(record.UniqueName)};
180
181 llvm::ms_demangle::IdentifierNode *idn =
182 ttn->QualifiedName->getUnqualifiedIdentifier();
183 std::string uname = idn->toString(llvm::ms_demangle::OF_NoTagSpecifier);
184
185 llvm::ms_demangle::NodeArrayNode *name_components =
186 ttn->QualifiedName->Components;
187 llvm::ArrayRef<llvm::ms_demangle::Node *> scopes(name_components->Nodes,
188 name_components->Count - 1);
189
190 clang::DeclContext *context = m_clang.GetTranslationUnitDecl();
191
192 // If this type doesn't have a parent type in the debug info, then the best we
193 // can do is to say that it's either a series of namespaces (if the scope is
194 // non-empty), or the translation unit (if the scope is empty).
195 llvm::Optional<TypeIndex> parent_index = pdb->GetParentType(ti);
196 if (!parent_index) {
197 if (scopes.empty())
198 return {context, uname};
199
200 // If there is no parent in the debug info, but some of the scopes have
201 // template params, then this is a case of bad debug info. See, for
202 // example, llvm.org/pr39607. We don't want to create an ambiguity between
203 // a NamespaceDecl and a CXXRecordDecl, so instead we create a class at
204 // global scope with the fully qualified name.
205 if (AnyScopesHaveTemplateParams(scopes))
206 return {context, std::string(record.Name)};
207
208 for (llvm::ms_demangle::Node *scope : scopes) {
209 auto *nii = static_cast<llvm::ms_demangle::NamedIdentifierNode *>(scope);
210 std::string str = nii->toString();
211 context = GetOrCreateNamespaceDecl(str.c_str(), *context);
212 }
213 return {context, uname};
214 }
215
216 // Otherwise, all we need to do is get the parent type of this type and
217 // recurse into our lazy type creation / AST reconstruction logic to get an
218 // LLDB TypeSP for the parent. This will cause the AST to automatically get
219 // the right DeclContext created for any parent.
220 clang::QualType parent_qt = GetOrCreateType(*parent_index);
221 if (parent_qt.isNull())
222 return {nullptr, ""};
223
224 context = clang::TagDecl::castToDeclContext(parent_qt->getAsTagDecl());
225 return {context, uname};
226}
227
228static bool isLocalVariableType(SymbolKind K) {
229 switch (K) {
230 case S_REGISTER:
231 case S_REGREL32:
232 case S_LOCAL:
233 return true;
234 default:
235 break;
236 }
237 return false;
238}
239
240clang::Decl *PdbAstBuilder::GetOrCreateSymbolForId(PdbCompilandSymId id) {
241 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
242 m_clang.GetSymbolFile()->GetBackingSymbolFile());
243 PdbIndex &index = pdb->GetIndex();
244 CVSymbol cvs = index.ReadSymbolRecord(id);
245
246 if (isLocalVariableType(cvs.kind())) {
247 clang::DeclContext *scope = GetParentDeclContext(id);
248 if (!scope)
249 return nullptr;
250 clang::Decl *scope_decl = clang::Decl::castFromDeclContext(scope);
251 PdbCompilandSymId scope_id =
252 PdbSymUid(m_decl_to_status[scope_decl].uid).asCompilandSym();
253 return GetOrCreateVariableDecl(scope_id, id);
254 }
255
256 switch (cvs.kind()) {
257 case S_GPROC32:
258 case S_LPROC32:
259 return GetOrCreateFunctionDecl(id);
260 case S_GDATA32:
261 case S_LDATA32:
262 case S_GTHREAD32:
263 case S_CONSTANT:
264 // global variable
265 return nullptr;
266 case S_BLOCK32:
267 return GetOrCreateBlockDecl(id);
268 case S_INLINESITE:
269 return GetOrCreateInlinedFunctionDecl(id);
270 default:
271 return nullptr;
272 }
273}
274
275llvm::Optional<CompilerDecl> PdbAstBuilder::GetOrCreateDeclForUid(PdbSymUid uid) {
276 if (clang::Decl *result = TryGetDecl(uid))
277 return ToCompilerDecl(*result);
278
279 clang::Decl *result = nullptr;
280 switch (uid.kind()) {
281 case PdbSymUidKind::CompilandSym:
282 result = GetOrCreateSymbolForId(uid.asCompilandSym());
283 break;
284 case PdbSymUidKind::Type: {
285 clang::QualType qt = GetOrCreateType(uid.asTypeSym());
286 if (qt.isNull())
287 return llvm::None;
288 if (auto *tag = qt->getAsTagDecl()) {
289 result = tag;
290 break;
291 }
292 return llvm::None;
293 }
294 default:
295 return llvm::None;
296 }
297
298 if (!result)
299 return llvm::None;
300 m_uid_to_decl[toOpaqueUid(uid)] = result;
301 return ToCompilerDecl(*result);
302}
303
304clang::DeclContext *PdbAstBuilder::GetOrCreateDeclContextForUid(PdbSymUid uid) {
305 if (uid.kind() == PdbSymUidKind::CompilandSym) {
306 if (uid.asCompilandSym().offset == 0)
307 return FromCompilerDeclContext(GetTranslationUnitDecl());
308 }
309 auto option = GetOrCreateDeclForUid(uid);
310 if (!option)
311 return nullptr;
312 clang::Decl *decl = FromCompilerDecl(*option);
313 if (!decl)
314 return nullptr;
315
316 return clang::Decl::castToDeclContext(decl);
317}
318
319std::pair<clang::DeclContext *, std::string>
320PdbAstBuilder::CreateDeclInfoForUndecoratedName(llvm::StringRef name) {
321 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
322 m_clang.GetSymbolFile()->GetBackingSymbolFile());
323 PdbIndex &index = pdb->GetIndex();
324 MSVCUndecoratedNameParser parser(name);
325 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers();
326
327 auto *context = FromCompilerDeclContext(GetTranslationUnitDecl());
328
329 llvm::StringRef uname = specs.back().GetBaseName();
330 specs = specs.drop_back();
331 if (specs.empty())
332 return {context, std::string(name)};
333
334 llvm::StringRef scope_name = specs.back().GetFullName();
335
336 // It might be a class name, try that first.
337 std::vector<TypeIndex> types = index.tpi().findRecordsByName(scope_name);
338 while (!types.empty()) {
339 clang::QualType qt = GetOrCreateType(types.back());
340 if (qt.isNull())
341 continue;
342 clang::TagDecl *tag = qt->getAsTagDecl();
343 if (tag)
344 return {clang::TagDecl::castToDeclContext(tag), std::string(uname)};
345 types.pop_back();
346 }
347
348 // If that fails, treat it as a series of namespaces.
349 for (const MSVCUndecoratedNameSpecifier &spec : specs) {
350 std::string ns_name = spec.GetBaseName().str();
351 context = GetOrCreateNamespaceDecl(ns_name.c_str(), *context);
352 }
353 return {context, std::string(uname)};
354}
355
356clang::DeclContext *PdbAstBuilder::GetParentDeclContext(PdbSymUid uid) {
357 // We must do this *without* calling GetOrCreate on the current uid, as
358 // that would be an infinite recursion.
359 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
360 m_clang.GetSymbolFile()->GetBackingSymbolFile());
361 PdbIndex& index = pdb->GetIndex();
362 switch (uid.kind()) {
363 case PdbSymUidKind::CompilandSym: {
364 llvm::Optional<PdbCompilandSymId> scope =
365 pdb->FindSymbolScope(uid.asCompilandSym());
366 if (scope)
367 return GetOrCreateDeclContextForUid(*scope);
368
369 CVSymbol sym = index.ReadSymbolRecord(uid.asCompilandSym());
370 return CreateDeclInfoForUndecoratedName(getSymbolName(sym)).first;
371 }
372 case PdbSymUidKind::Type: {
373 // It could be a namespace, class, or global. We don't support nested
374 // functions yet. Anyway, we just need to consult the parent type map.
375 PdbTypeSymId type_id = uid.asTypeSym();
376 llvm::Optional<TypeIndex> parent_index = pdb->GetParentType(type_id.index);
377 if (!parent_index)
378 return FromCompilerDeclContext(GetTranslationUnitDecl());
379 return GetOrCreateDeclContextForUid(PdbTypeSymId(*parent_index));
380 }
381 case PdbSymUidKind::FieldListMember:
382 // In this case the parent DeclContext is the one for the class that this
383 // member is inside of.
384 break;
385 case PdbSymUidKind::GlobalSym: {
386 // If this refers to a compiland symbol, just recurse in with that symbol.
387 // The only other possibilities are S_CONSTANT and S_UDT, in which case we
388 // need to parse the undecorated name to figure out the scope, then look
389 // that up in the TPI stream. If it's found, it's a type, othewrise it's
390 // a series of namespaces.
391 // FIXME: do this.
392 CVSymbol global = index.ReadSymbolRecord(uid.asGlobalSym());
393 switch (global.kind()) {
394 case SymbolKind::S_GDATA32:
395 case SymbolKind::S_LDATA32:
396 return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first;;
397 case SymbolKind::S_PROCREF:
398 case SymbolKind::S_LPROCREF: {
399 ProcRefSym ref{global.kind()};
400 llvm::cantFail(
401 SymbolDeserializer::deserializeAs<ProcRefSym>(global, ref));
402 PdbCompilandSymId cu_sym_id{ref.modi(), ref.SymOffset};
403 return GetParentDeclContext(cu_sym_id);
404 }
405 case SymbolKind::S_CONSTANT:
406 case SymbolKind::S_UDT:
407 return CreateDeclInfoForUndecoratedName(getSymbolName(global)).first;
408 default:
409 break;
410 }
411 break;
412 }
413 default:
414 break;
415 }
416 return FromCompilerDeclContext(GetTranslationUnitDecl());
417}
418
419bool PdbAstBuilder::CompleteType(clang::QualType qt) {
420 if (qt.isNull())
421 return false;
422 clang::TagDecl *tag = qt->getAsTagDecl();
423 if (qt->isArrayType()) {
424 const clang::Type *element_type = qt->getArrayElementTypeNoTypeQual();
425 tag = element_type->getAsTagDecl();
426 }
427 if (!tag)
428 return false;
429
430 return CompleteTagDecl(*tag);
431}
432
433bool PdbAstBuilder::CompleteTagDecl(clang::TagDecl &tag) {
434 // If this is not in our map, it's an error.
435 auto status_iter = m_decl_to_status.find(&tag);
436 lldbassert(status_iter != m_decl_to_status.end())lldb_private::lldb_assert(static_cast<bool>(status_iter
!= m_decl_to_status.end()), "status_iter != m_decl_to_status.end()"
, __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 436)
;
437
438 // If it's already complete, just return.
439 DeclStatus &status = status_iter->second;
440 if (status.resolved)
441 return true;
442
443 PdbTypeSymId type_id = PdbSymUid(status.uid).asTypeSym();
444 PdbIndex &index = static_cast<SymbolFileNativePDB *>(
445 m_clang.GetSymbolFile()->GetBackingSymbolFile())
446 ->GetIndex();
447 lldbassert(IsTagRecord(type_id, index.tpi()))lldb_private::lldb_assert(static_cast<bool>(IsTagRecord
(type_id, index.tpi())), "IsTagRecord(type_id, index.tpi())",
__FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 447)
;
448
449 clang::QualType tag_qt = m_clang.getASTContext().getTypeDeclType(&tag);
450 TypeSystemClang::SetHasExternalStorage(tag_qt.getAsOpaquePtr(), false);
451
452 TypeIndex tag_ti = type_id.index;
453 CVType cvt = index.tpi().getType(tag_ti);
454 if (cvt.kind() == LF_MODIFIER)
455 tag_ti = LookThroughModifierRecord(cvt);
456
457 PdbTypeSymId best_ti = GetBestPossibleDecl(tag_ti, index.tpi());
458 cvt = index.tpi().getType(best_ti.index);
459 lldbassert(IsTagRecord(cvt))lldb_private::lldb_assert(static_cast<bool>(IsTagRecord
(cvt)), "IsTagRecord(cvt)", __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 459)
;
460
461 if (IsForwardRefUdt(cvt)) {
462 // If we can't find a full decl for this forward ref anywhere in the debug
463 // info, then we have no way to complete it.
464 return false;
465 }
466
467 TypeIndex field_list_ti = GetFieldListIndex(cvt);
468 CVType field_list_cvt = index.tpi().getType(field_list_ti);
469 if (field_list_cvt.kind() != LF_FIELDLIST)
470 return false;
471 FieldListRecord field_list;
472 if (llvm::Error error = TypeDeserializer::deserializeAs<FieldListRecord>(
473 field_list_cvt, field_list))
474 llvm::consumeError(std::move(error));
475
476 // Visit all members of this class, then perform any finalization necessary
477 // to complete the class.
478 CompilerType ct = ToCompilerType(tag_qt);
479 UdtRecordCompleter completer(best_ti, ct, tag, *this, index, m_decl_to_status,
480 m_cxx_record_map);
481 llvm::Error error =
482 llvm::codeview::visitMemberRecordStream(field_list.Data, completer);
483 completer.complete();
484
485 m_decl_to_status[&tag].resolved = true;
486 if (error) {
487 llvm::consumeError(std::move(error));
488 return false;
489 }
490 return true;
491}
492
493clang::QualType PdbAstBuilder::CreateSimpleType(TypeIndex ti) {
494 if (ti == TypeIndex::NullptrT())
495 return GetBasicType(lldb::eBasicTypeNullPtr);
496
497 if (ti.getSimpleMode() != SimpleTypeMode::Direct) {
498 clang::QualType direct_type = GetOrCreateType(ti.makeDirect());
499 if (direct_type.isNull())
500 return {};
501 return m_clang.getASTContext().getPointerType(direct_type);
502 }
503
504 if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated)
505 return {};
506
507 lldb::BasicType bt = GetCompilerTypeForSimpleKind(ti.getSimpleKind());
508 if (bt == lldb::eBasicTypeInvalid)
509 return {};
510
511 return GetBasicType(bt);
512}
513
514clang::QualType PdbAstBuilder::CreatePointerType(const PointerRecord &pointer) {
515 clang::QualType pointee_type = GetOrCreateType(pointer.ReferentType);
516
517 // This can happen for pointers to LF_VTSHAPE records, which we shouldn't
518 // create in the AST.
519 if (pointee_type.isNull())
520 return {};
521
522 if (pointer.isPointerToMember()) {
523 MemberPointerInfo mpi = pointer.getMemberInfo();
524 clang::QualType class_type = GetOrCreateType(mpi.ContainingType);
525 if (class_type.isNull())
526 return {};
527 if (clang::TagDecl *tag = class_type->getAsTagDecl()) {
528 clang::MSInheritanceAttr::Spelling spelling;
529 switch (mpi.Representation) {
530 case llvm::codeview::PointerToMemberRepresentation::SingleInheritanceData:
531 case llvm::codeview::PointerToMemberRepresentation::
532 SingleInheritanceFunction:
533 spelling =
534 clang::MSInheritanceAttr::Spelling::Keyword_single_inheritance;
535 break;
536 case llvm::codeview::PointerToMemberRepresentation::
537 MultipleInheritanceData:
538 case llvm::codeview::PointerToMemberRepresentation::
539 MultipleInheritanceFunction:
540 spelling =
541 clang::MSInheritanceAttr::Spelling::Keyword_multiple_inheritance;
542 break;
543 case llvm::codeview::PointerToMemberRepresentation::
544 VirtualInheritanceData:
545 case llvm::codeview::PointerToMemberRepresentation::
546 VirtualInheritanceFunction:
547 spelling =
548 clang::MSInheritanceAttr::Spelling::Keyword_virtual_inheritance;
549 break;
550 case llvm::codeview::PointerToMemberRepresentation::Unknown:
551 spelling =
552 clang::MSInheritanceAttr::Spelling::Keyword_unspecified_inheritance;
553 break;
554 default:
555 spelling = clang::MSInheritanceAttr::Spelling::SpellingNotCalculated;
556 break;
557 }
558 tag->addAttr(clang::MSInheritanceAttr::CreateImplicit(
559 m_clang.getASTContext(), spelling));
560 }
561 return m_clang.getASTContext().getMemberPointerType(
562 pointee_type, class_type.getTypePtr());
563 }
564
565 clang::QualType pointer_type;
566 if (pointer.getMode() == PointerMode::LValueReference)
567 pointer_type = m_clang.getASTContext().getLValueReferenceType(pointee_type);
568 else if (pointer.getMode() == PointerMode::RValueReference)
569 pointer_type = m_clang.getASTContext().getRValueReferenceType(pointee_type);
570 else
571 pointer_type = m_clang.getASTContext().getPointerType(pointee_type);
572
573 if ((pointer.getOptions() & PointerOptions::Const) != PointerOptions::None)
574 pointer_type.addConst();
575
576 if ((pointer.getOptions() & PointerOptions::Volatile) != PointerOptions::None)
577 pointer_type.addVolatile();
578
579 if ((pointer.getOptions() & PointerOptions::Restrict) != PointerOptions::None)
580 pointer_type.addRestrict();
581
582 return pointer_type;
583}
584
585clang::QualType
586PdbAstBuilder::CreateModifierType(const ModifierRecord &modifier) {
587 clang::QualType unmodified_type = GetOrCreateType(modifier.ModifiedType);
588 if (unmodified_type.isNull())
589 return {};
590
591 if ((modifier.Modifiers & ModifierOptions::Const) != ModifierOptions::None)
592 unmodified_type.addConst();
593 if ((modifier.Modifiers & ModifierOptions::Volatile) != ModifierOptions::None)
594 unmodified_type.addVolatile();
595
596 return unmodified_type;
597}
598
599clang::QualType PdbAstBuilder::CreateRecordType(PdbTypeSymId id,
600 const TagRecord &record) {
601 clang::DeclContext *context = nullptr;
602 std::string uname;
603 std::tie(context, uname) = CreateDeclInfoForType(record, id.index);
604 if (!context)
605 return {};
606
607 clang::TagTypeKind ttk = TranslateUdtKind(record);
608 lldb::AccessType access =
609 (ttk == clang::TTK_Class) ? lldb::eAccessPrivate : lldb::eAccessPublic;
610
611 ClangASTMetadata metadata;
612 metadata.SetUserID(toOpaqueUid(id));
613 metadata.SetIsDynamicCXXType(false);
614
615 CompilerType ct =
616 m_clang.CreateRecordType(context, OptionalClangModuleID(), access, uname,
617 ttk, lldb::eLanguageTypeC_plus_plus, &metadata);
618
619 lldbassert(ct.IsValid())lldb_private::lldb_assert(static_cast<bool>(ct.IsValid(
)), "ct.IsValid()", __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 619)
;
620
621 TypeSystemClang::StartTagDeclarationDefinition(ct);
622
623 // Even if it's possible, don't complete it at this point. Just mark it
624 // forward resolved, and if/when LLDB needs the full definition, it can
625 // ask us.
626 clang::QualType result =
627 clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
628
629 TypeSystemClang::SetHasExternalStorage(result.getAsOpaquePtr(), true);
630 return result;
631}
632
633clang::Decl *PdbAstBuilder::TryGetDecl(PdbSymUid uid) const {
634 auto iter = m_uid_to_decl.find(toOpaqueUid(uid));
635 if (iter != m_uid_to_decl.end())
636 return iter->second;
637 return nullptr;
638}
639
640clang::NamespaceDecl *
641PdbAstBuilder::GetOrCreateNamespaceDecl(const char *name,
642 clang::DeclContext &context) {
643 return m_clang.GetUniqueNamespaceDeclaration(
644 IsAnonymousNamespaceName(name) ? nullptr : name, &context,
645 OptionalClangModuleID());
646}
647
648clang::BlockDecl *
649PdbAstBuilder::GetOrCreateBlockDecl(PdbCompilandSymId block_id) {
650 if (clang::Decl *decl = TryGetDecl(block_id))
651 return llvm::dyn_cast<clang::BlockDecl>(decl);
652
653 clang::DeclContext *scope = GetParentDeclContext(block_id);
654
655 clang::BlockDecl *block_decl =
656 m_clang.CreateBlockDeclaration(scope, OptionalClangModuleID());
657 m_uid_to_decl.insert({toOpaqueUid(block_id), block_decl});
658
659 DeclStatus status;
660 status.resolved = true;
661 status.uid = toOpaqueUid(block_id);
662 m_decl_to_status.insert({block_decl, status});
663
664 return block_decl;
665}
666
667clang::VarDecl *PdbAstBuilder::CreateVariableDecl(PdbSymUid uid, CVSymbol sym,
668 clang::DeclContext &scope) {
669 VariableInfo var_info = GetVariableNameInfo(sym);
670 clang::QualType qt = GetOrCreateType(var_info.type);
671 if (qt.isNull())
672 return nullptr;
673
674 clang::VarDecl *var_decl = m_clang.CreateVariableDeclaration(
675 &scope, OptionalClangModuleID(), var_info.name.str().c_str(), qt);
676
677 m_uid_to_decl[toOpaqueUid(uid)] = var_decl;
678 DeclStatus status;
679 status.resolved = true;
680 status.uid = toOpaqueUid(uid);
681 m_decl_to_status.insert({var_decl, status});
682 return var_decl;
683}
684
685clang::VarDecl *
686PdbAstBuilder::GetOrCreateVariableDecl(PdbCompilandSymId scope_id,
687 PdbCompilandSymId var_id) {
688 if (clang::Decl *decl = TryGetDecl(var_id))
689 return llvm::dyn_cast<clang::VarDecl>(decl);
690
691 clang::DeclContext *scope = GetOrCreateDeclContextForUid(scope_id);
692 if (!scope)
693 return nullptr;
694
695 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
696 m_clang.GetSymbolFile()->GetBackingSymbolFile());
697 PdbIndex &index = pdb->GetIndex();
698 CVSymbol sym = index.ReadSymbolRecord(var_id);
699 return CreateVariableDecl(PdbSymUid(var_id), sym, *scope);
700}
701
702clang::VarDecl *PdbAstBuilder::GetOrCreateVariableDecl(PdbGlobalSymId var_id) {
703 if (clang::Decl *decl = TryGetDecl(var_id))
704 return llvm::dyn_cast<clang::VarDecl>(decl);
705
706 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
707 m_clang.GetSymbolFile()->GetBackingSymbolFile());
708 PdbIndex &index = pdb->GetIndex();
709 CVSymbol sym = index.ReadSymbolRecord(var_id);
710 auto context = FromCompilerDeclContext(GetTranslationUnitDecl());
711 return CreateVariableDecl(PdbSymUid(var_id), sym, *context);
712}
713
714clang::TypedefNameDecl *
715PdbAstBuilder::GetOrCreateTypedefDecl(PdbGlobalSymId id) {
716 if (clang::Decl *decl = TryGetDecl(id))
717 return llvm::dyn_cast<clang::TypedefNameDecl>(decl);
718
719 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
720 m_clang.GetSymbolFile()->GetBackingSymbolFile());
721 PdbIndex &index = pdb->GetIndex();
722 CVSymbol sym = index.ReadSymbolRecord(id);
723 lldbassert(sym.kind() == S_UDT)lldb_private::lldb_assert(static_cast<bool>(sym.kind() ==
S_UDT), "sym.kind() == S_UDT", __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 723)
;
724 UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
725
726 clang::DeclContext *scope = GetParentDeclContext(id);
727
728 PdbTypeSymId real_type_id{udt.Type, false};
729 clang::QualType qt = GetOrCreateType(real_type_id);
730 if (qt.isNull() || !scope)
731 return nullptr;
732
733 std::string uname = std::string(DropNameScope(udt.Name));
734
735 CompilerType ct = ToCompilerType(qt).CreateTypedef(
736 uname.c_str(), ToCompilerDeclContext(*scope), 0);
737 clang::TypedefNameDecl *tnd = m_clang.GetAsTypedefDecl(ct);
738 DeclStatus status;
739 status.resolved = true;
740 status.uid = toOpaqueUid(id);
741 m_decl_to_status.insert({tnd, status});
742 return tnd;
743}
744
745clang::QualType PdbAstBuilder::GetBasicType(lldb::BasicType type) {
746 CompilerType ct = m_clang.GetBasicType(type);
747 return clang::QualType::getFromOpaquePtr(ct.GetOpaqueQualType());
748}
749
750clang::QualType PdbAstBuilder::CreateType(PdbTypeSymId type) {
751 if (type.index.isSimple())
752 return CreateSimpleType(type.index);
753
754 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
755 m_clang.GetSymbolFile()->GetBackingSymbolFile());
756 PdbIndex &index = pdb->GetIndex();
757 CVType cvt = index.tpi().getType(type.index);
758
759 if (cvt.kind() == LF_MODIFIER) {
760 ModifierRecord modifier;
761 llvm::cantFail(
762 TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier));
763 return CreateModifierType(modifier);
764 }
765
766 if (cvt.kind() == LF_POINTER) {
767 PointerRecord pointer;
768 llvm::cantFail(
769 TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer));
770 return CreatePointerType(pointer);
771 }
772
773 if (IsTagRecord(cvt)) {
774 CVTagRecord tag = CVTagRecord::create(cvt);
775 if (tag.kind() == CVTagRecord::Union)
776 return CreateRecordType(type.index, tag.asUnion());
777 if (tag.kind() == CVTagRecord::Enum)
778 return CreateEnumType(type.index, tag.asEnum());
779 return CreateRecordType(type.index, tag.asClass());
780 }
781
782 if (cvt.kind() == LF_ARRAY) {
783 ArrayRecord ar;
784 llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar));
785 return CreateArrayType(ar);
786 }
787
788 if (cvt.kind() == LF_PROCEDURE) {
789 ProcedureRecord pr;
790 llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr));
791 return CreateFunctionType(pr.ArgumentList, pr.ReturnType, pr.CallConv);
792 }
793
794 if (cvt.kind() == LF_MFUNCTION) {
795 MemberFunctionRecord mfr;
796 llvm::cantFail(
797 TypeDeserializer::deserializeAs<MemberFunctionRecord>(cvt, mfr));
798 return CreateFunctionType(mfr.ArgumentList, mfr.ReturnType, mfr.CallConv);
799 }
800
801 return {};
802}
803
804clang::QualType PdbAstBuilder::GetOrCreateType(PdbTypeSymId type) {
805 if (type.index.isNoneType())
806 return {};
807
808 lldb::user_id_t uid = toOpaqueUid(type);
809 auto iter = m_uid_to_type.find(uid);
810 if (iter != m_uid_to_type.end())
811 return iter->second;
812
813 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
814 m_clang.GetSymbolFile()->GetBackingSymbolFile());
815 PdbIndex &index = pdb->GetIndex();
816 PdbTypeSymId best_type = GetBestPossibleDecl(type, index.tpi());
817
818 clang::QualType qt;
819 if (best_type.index != type.index) {
820 // This is a forward decl. Call GetOrCreate on the full decl, then map the
821 // forward decl id to the full decl QualType.
822 clang::QualType qt = GetOrCreateType(best_type);
823 if (qt.isNull())
824 return {};
825 m_uid_to_type[toOpaqueUid(type)] = qt;
826 return qt;
827 }
828
829 // This is either a full decl, or a forward decl with no matching full decl
830 // in the debug info.
831 qt = CreateType(type);
832 if (qt.isNull())
833 return {};
834
835 m_uid_to_type[toOpaqueUid(type)] = qt;
836 if (IsTagRecord(type, index.tpi())) {
837 clang::TagDecl *tag = qt->getAsTagDecl();
838 lldbassert(m_decl_to_status.count(tag) == 0)lldb_private::lldb_assert(static_cast<bool>(m_decl_to_status
.count(tag) == 0), "m_decl_to_status.count(tag) == 0", __FUNCTION__
, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 838)
;
839
840 DeclStatus &status = m_decl_to_status[tag];
841 status.uid = uid;
842 status.resolved = false;
843 }
844 return qt;
845}
846
847clang::FunctionDecl *
848PdbAstBuilder::CreateFunctionDecl(PdbCompilandSymId func_id,
849 llvm::StringRef func_name, TypeIndex func_ti,
850 CompilerType func_ct, uint32_t param_count,
851 clang::StorageClass func_storage,
852 bool is_inline, clang::DeclContext *parent) {
853 clang::FunctionDecl *function_decl = nullptr;
854 if (parent->isRecord()) {
855 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
856 m_clang.GetSymbolFile()->GetBackingSymbolFile());
857 PdbIndex &index = pdb->GetIndex();
858 clang::QualType parent_qt = llvm::cast<clang::TypeDecl>(parent)
859 ->getTypeForDecl()
860 ->getCanonicalTypeInternal();
861 lldb::opaque_compiler_type_t parent_opaque_ty =
862 ToCompilerType(parent_qt).GetOpaqueQualType();
863 // FIXME: Remove this workaround.
864 auto iter = m_cxx_record_map.find(parent_opaque_ty);
865 if (iter != m_cxx_record_map.end()) {
866 if (iter->getSecond().contains({func_name, func_ct})) {
867 return nullptr;
868 }
869 }
870
871 CVType cvt = index.tpi().getType(func_ti);
872 MemberFunctionRecord func_record(static_cast<TypeRecordKind>(cvt.kind()));
873 llvm::cantFail(TypeDeserializer::deserializeAs<MemberFunctionRecord>(
874 cvt, func_record));
875 TypeIndex class_index = func_record.getClassType();
876
877 CVType parent_cvt = index.tpi().getType(class_index);
878 TagRecord tag_record = CVTagRecord::create(parent_cvt).asTag();
879 // If it's a forward reference, try to get the real TypeIndex.
880 if (tag_record.isForwardRef()) {
881 llvm::Expected<TypeIndex> eti =
882 index.tpi().findFullDeclForForwardRef(class_index);
883 if (eti) {
884 tag_record = CVTagRecord::create(index.tpi().getType(*eti)).asTag();
885 }
886 }
887 if (!tag_record.FieldList.isSimple()) {
888 CVType field_list_cvt = index.tpi().getType(tag_record.FieldList);
889 FieldListRecord field_list;
890 if (llvm::Error error = TypeDeserializer::deserializeAs<FieldListRecord>(
891 field_list_cvt, field_list))
892 llvm::consumeError(std::move(error));
893 CreateMethodDecl process(index, m_clang, func_ti, function_decl,
894 parent_opaque_ty, func_name, func_ct);
895 if (llvm::Error err = visitMemberRecordStream(field_list.Data, process))
896 llvm::consumeError(std::move(err));
897 }
898
899 if (!function_decl) {
900 function_decl = m_clang.AddMethodToCXXRecordType(
901 parent_opaque_ty, func_name,
902 /*mangled_name=*/nullptr, func_ct,
903 /*access=*/lldb::AccessType::eAccessPublic,
904 /*is_virtual=*/false, /*is_static=*/false,
905 /*is_inline=*/false, /*is_explicit=*/false,
906 /*is_attr_used=*/false, /*is_artificial=*/false);
907 }
908 m_cxx_record_map[parent_opaque_ty].insert({func_name, func_ct});
909 } else {
910 function_decl = m_clang.CreateFunctionDeclaration(
911 parent, OptionalClangModuleID(), func_name, func_ct, func_storage,
912 is_inline);
913 CreateFunctionParameters(func_id, *function_decl, param_count);
914 }
915 return function_decl;
916}
917
918clang::FunctionDecl *
919PdbAstBuilder::GetOrCreateInlinedFunctionDecl(PdbCompilandSymId inlinesite_id) {
920 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
921 m_clang.GetSymbolFile()->GetBackingSymbolFile());
922 PdbIndex &index = pdb->GetIndex();
923 CompilandIndexItem *cii =
924 index.compilands().GetCompiland(inlinesite_id.modi);
925 CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(inlinesite_id.offset);
926 InlineSiteSym inline_site(static_cast<SymbolRecordKind>(sym.kind()));
927 cantFail(SymbolDeserializer::deserializeAs<InlineSiteSym>(sym, inline_site));
928
929 // Inlinee is the id index to the function id record that is inlined.
930 PdbTypeSymId func_id(inline_site.Inlinee, true);
931 // Look up the function decl by the id index to see if we have created a
932 // function decl for a different inlinesite that refers the same function.
933 if (clang::Decl *decl = TryGetDecl(func_id))
934 return llvm::dyn_cast<clang::FunctionDecl>(decl);
935 clang::FunctionDecl *function_decl =
936 CreateFunctionDeclFromId(func_id, inlinesite_id);
937 if (function_decl == nullptr)
938 return nullptr;
939
940 // Use inline site id in m_decl_to_status because it's expected to be a
941 // PdbCompilandSymId so that we can parse local variables info after it.
942 uint64_t inlinesite_uid = toOpaqueUid(inlinesite_id);
943 DeclStatus status;
944 status.resolved = true;
945 status.uid = inlinesite_uid;
946 m_decl_to_status.insert({function_decl, status});
947 // Use the index in IPI stream as uid in m_uid_to_decl, because index in IPI
948 // stream are unique and there could be multiple inline sites (different ids)
949 // referring the same inline function. This avoid creating multiple same
950 // inline function delcs.
951 uint64_t func_uid = toOpaqueUid(func_id);
952 lldbassert(m_uid_to_decl.count(func_uid) == 0)lldb_private::lldb_assert(static_cast<bool>(m_uid_to_decl
.count(func_uid) == 0), "m_uid_to_decl.count(func_uid) == 0",
__FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 952)
;
953 m_uid_to_decl[func_uid] = function_decl;
954 return function_decl;
955}
956
957clang::FunctionDecl *
958PdbAstBuilder::CreateFunctionDeclFromId(PdbTypeSymId func_tid,
959 PdbCompilandSymId func_sid) {
960 lldbassert(func_tid.is_ipi)lldb_private::lldb_assert(static_cast<bool>(func_tid.is_ipi
), "func_tid.is_ipi", __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 960)
;
961 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
962 m_clang.GetSymbolFile()->GetBackingSymbolFile());
963 PdbIndex &index = pdb->GetIndex();
964 CVType func_cvt = index.ipi().getType(func_tid.index);
965 llvm::StringRef func_name;
966 TypeIndex func_ti;
967 clang::DeclContext *parent = nullptr;
968 switch (func_cvt.kind()) {
969 case LF_MFUNC_ID: {
970 MemberFuncIdRecord mfr;
971 cantFail(
972 TypeDeserializer::deserializeAs<MemberFuncIdRecord>(func_cvt, mfr));
973 func_name = mfr.getName();
974 func_ti = mfr.getFunctionType();
975 PdbTypeSymId class_type_id(mfr.ClassType, false);
976 parent = GetOrCreateDeclContextForUid(class_type_id);
977 break;
978 }
979 case LF_FUNC_ID: {
980 FuncIdRecord fir;
981 cantFail(TypeDeserializer::deserializeAs<FuncIdRecord>(func_cvt, fir));
982 func_name = fir.getName();
983 func_ti = fir.getFunctionType();
984 parent = FromCompilerDeclContext(GetTranslationUnitDecl());
985 if (!fir.ParentScope.isNoneType()) {
986 CVType parent_cvt = index.ipi().getType(fir.ParentScope);
987 if (parent_cvt.kind() == LF_STRING_ID) {
988 StringIdRecord sir;
989 cantFail(
990 TypeDeserializer::deserializeAs<StringIdRecord>(parent_cvt, sir));
991 parent = GetOrCreateNamespaceDecl(sir.String.data(), *parent);
992 }
993 }
994 break;
995 }
996 default:
997 lldbassert(false && "Invalid function id type!")lldb_private::lldb_assert(static_cast<bool>(false &&
"Invalid function id type!"), "false && \"Invalid function id type!\""
, __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 997)
;
998 }
999 clang::QualType func_qt = GetOrCreateType(func_ti);
1000 if (func_qt.isNull() || !parent)
1001 return nullptr;
1002 CompilerType func_ct = ToCompilerType(func_qt);
1003 uint32_t param_count =
1004 llvm::cast<clang::FunctionProtoType>(func_qt)->getNumParams();
1005 return CreateFunctionDecl(func_sid, func_name, func_ti, func_ct, param_count,
1006 clang::SC_None, true, parent);
1007}
1008
1009clang::FunctionDecl *
1010PdbAstBuilder::GetOrCreateFunctionDecl(PdbCompilandSymId func_id) {
1011 if (clang::Decl *decl = TryGetDecl(func_id))
1012 return llvm::dyn_cast<clang::FunctionDecl>(decl);
1013
1014 clang::DeclContext *parent = GetParentDeclContext(PdbSymUid(func_id));
1015 if (!parent)
1016 return nullptr;
1017 std::string context_name;
1018 if (clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(parent)) {
1019 context_name = ns->getQualifiedNameAsString();
1020 } else if (clang::TagDecl *tag = llvm::dyn_cast<clang::TagDecl>(parent)) {
1021 context_name = tag->getQualifiedNameAsString();
1022 }
1023
1024 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1025 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1026 PdbIndex &index = pdb->GetIndex();
1027 CVSymbol cvs = index.ReadSymbolRecord(func_id);
1028 ProcSym proc(static_cast<SymbolRecordKind>(cvs.kind()));
1029 llvm::cantFail(SymbolDeserializer::deserializeAs<ProcSym>(cvs, proc));
1030
1031 PdbTypeSymId type_id(proc.FunctionType);
1032 clang::QualType qt = GetOrCreateType(type_id);
1033 if (qt.isNull())
1034 return nullptr;
1035
1036 clang::StorageClass storage = clang::SC_None;
1037 if (proc.Kind == SymbolRecordKind::ProcSym)
1038 storage = clang::SC_Static;
1039
1040 const clang::FunctionProtoType *func_type =
1041 llvm::dyn_cast<clang::FunctionProtoType>(qt);
1042
1043 CompilerType func_ct = ToCompilerType(qt);
1044
1045 llvm::StringRef proc_name = proc.Name;
1046 proc_name.consume_front(context_name);
1047 proc_name.consume_front("::");
1048 clang::FunctionDecl *function_decl =
1049 CreateFunctionDecl(func_id, proc_name, proc.FunctionType, func_ct,
1050 func_type->getNumParams(), storage, false, parent);
1051 if (function_decl == nullptr)
1052 return nullptr;
1053
1054 lldbassert(m_uid_to_decl.count(toOpaqueUid(func_id)) == 0)lldb_private::lldb_assert(static_cast<bool>(m_uid_to_decl
.count(toOpaqueUid(func_id)) == 0), "m_uid_to_decl.count(toOpaqueUid(func_id)) == 0"
, __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 1054)
;
1055 m_uid_to_decl[toOpaqueUid(func_id)] = function_decl;
1056 DeclStatus status;
1057 status.resolved = true;
1058 status.uid = toOpaqueUid(func_id);
1059 m_decl_to_status.insert({function_decl, status});
1060
1061 return function_decl;
1062}
1063
1064void PdbAstBuilder::CreateFunctionParameters(PdbCompilandSymId func_id,
1065 clang::FunctionDecl &function_decl,
1066 uint32_t param_count) {
1067 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1068 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1069 PdbIndex &index = pdb->GetIndex();
1070 CompilandIndexItem *cii = index.compilands().GetCompiland(func_id.modi);
1071 CVSymbolArray scope =
1072 cii->m_debug_stream.getSymbolArrayForScope(func_id.offset);
1073
1074 scope.drop_front();
1075 auto begin = scope.begin();
1076 auto end = scope.end();
1077 std::vector<clang::ParmVarDecl *> params;
1078 for (uint32_t i = 0; i < param_count && begin != end;) {
1079 uint32_t record_offset = begin.offset();
1080 CVSymbol sym = *begin++;
1081
1082 TypeIndex param_type;
1083 llvm::StringRef param_name;
1084 switch (sym.kind()) {
1085 case S_REGREL32: {
1086 RegRelativeSym reg(SymbolRecordKind::RegRelativeSym);
1087 cantFail(SymbolDeserializer::deserializeAs<RegRelativeSym>(sym, reg));
1088 param_type = reg.Type;
1089 param_name = reg.Name;
1090 break;
1091 }
1092 case S_REGISTER: {
1093 RegisterSym reg(SymbolRecordKind::RegisterSym);
1094 cantFail(SymbolDeserializer::deserializeAs<RegisterSym>(sym, reg));
1095 param_type = reg.Index;
1096 param_name = reg.Name;
1097 break;
1098 }
1099 case S_LOCAL: {
1100 LocalSym local(SymbolRecordKind::LocalSym);
1101 cantFail(SymbolDeserializer::deserializeAs<LocalSym>(sym, local));
1102 if ((local.Flags & LocalSymFlags::IsParameter) == LocalSymFlags::None)
1103 continue;
1104 param_type = local.Type;
1105 param_name = local.Name;
1106 break;
1107 }
1108 case S_BLOCK32:
1109 case S_INLINESITE:
1110 case S_INLINESITE2:
1111 // All parameters should come before the first block/inlinesite. If that
1112 // isn't the case, then perhaps this is bad debug info that doesn't
1113 // contain information about all parameters.
1114 return;
1115 default:
1116 continue;
1117 }
1118
1119 PdbCompilandSymId param_uid(func_id.modi, record_offset);
1120 clang::QualType qt = GetOrCreateType(param_type);
1121 if (qt.isNull())
1122 return;
1123
1124 CompilerType param_type_ct = m_clang.GetType(qt);
1125 clang::ParmVarDecl *param = m_clang.CreateParameterDeclaration(
1126 &function_decl, OptionalClangModuleID(), param_name.str().c_str(),
1127 param_type_ct, clang::SC_None, true);
1128 lldbassert(m_uid_to_decl.count(toOpaqueUid(param_uid)) == 0)lldb_private::lldb_assert(static_cast<bool>(m_uid_to_decl
.count(toOpaqueUid(param_uid)) == 0), "m_uid_to_decl.count(toOpaqueUid(param_uid)) == 0"
, __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 1128)
;
1129
1130 m_uid_to_decl[toOpaqueUid(param_uid)] = param;
1131 params.push_back(param);
1132 ++i;
1133 }
1134
1135 if (!params.empty() && params.size() == param_count)
1136 m_clang.SetFunctionParameters(&function_decl, params);
1137}
1138
1139clang::QualType PdbAstBuilder::CreateEnumType(PdbTypeSymId id,
1140 const EnumRecord &er) {
1141 clang::DeclContext *decl_context = nullptr;
1142 std::string uname;
1143 std::tie(decl_context, uname) = CreateDeclInfoForType(er, id.index);
1144 if (!decl_context)
1145 return {};
1146
1147 clang::QualType underlying_type = GetOrCreateType(er.UnderlyingType);
1148 if (underlying_type.isNull())
1149 return {};
1150
1151 Declaration declaration;
1152 CompilerType enum_ct = m_clang.CreateEnumerationType(
1153 uname, decl_context, OptionalClangModuleID(), declaration,
1154 ToCompilerType(underlying_type), er.isScoped());
1155
1156 TypeSystemClang::StartTagDeclarationDefinition(enum_ct);
1157 TypeSystemClang::SetHasExternalStorage(enum_ct.GetOpaqueQualType(), true);
1158
1159 return clang::QualType::getFromOpaquePtr(enum_ct.GetOpaqueQualType());
1160}
1161
1162clang::QualType PdbAstBuilder::CreateArrayType(const ArrayRecord &ar) {
1163 clang::QualType element_type = GetOrCreateType(ar.ElementType);
1164
1165 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1166 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1167 PdbIndex &index = pdb->GetIndex();
1168 uint64_t element_size = GetSizeOfType({ar.ElementType}, index.tpi());
1169 if (element_type.isNull() || element_size == 0)
1170 return {};
1171 uint64_t element_count = ar.Size / element_size;
1172
1173 CompilerType array_ct = m_clang.CreateArrayType(ToCompilerType(element_type),
1174 element_count, false);
1175 return clang::QualType::getFromOpaquePtr(array_ct.GetOpaqueQualType());
1176}
1177
1178clang::QualType PdbAstBuilder::CreateFunctionType(
1179 TypeIndex args_type_idx, TypeIndex return_type_idx,
1180 llvm::codeview::CallingConvention calling_convention) {
1181 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1182 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1183 PdbIndex &index = pdb->GetIndex();
1184 TpiStream &stream = index.tpi();
1185 CVType args_cvt = stream.getType(args_type_idx);
1186 ArgListRecord args;
1187 llvm::cantFail(
1188 TypeDeserializer::deserializeAs<ArgListRecord>(args_cvt, args));
1189
1190 llvm::ArrayRef<TypeIndex> arg_indices = llvm::makeArrayRef(args.ArgIndices);
1191 bool is_variadic = IsCVarArgsFunction(arg_indices);
1192 if (is_variadic)
1193 arg_indices = arg_indices.drop_back();
1194
1195 std::vector<CompilerType> arg_types;
1196 arg_types.reserve(arg_indices.size());
1197
1198 for (TypeIndex arg_index : arg_indices) {
1199 clang::QualType arg_type = GetOrCreateType(arg_index);
1200 if (arg_type.isNull())
1201 continue;
1202 arg_types.push_back(ToCompilerType(arg_type));
1203 }
1204
1205 clang::QualType return_type = GetOrCreateType(return_type_idx);
1206 if (return_type.isNull())
1207 return {};
1208
1209 llvm::Optional<clang::CallingConv> cc =
1210 TranslateCallingConvention(calling_convention);
1211 if (!cc)
1212 return {};
1213
1214 CompilerType return_ct = ToCompilerType(return_type);
1215 CompilerType func_sig_ast_type = m_clang.CreateFunctionType(
1216 return_ct, arg_types.data(), arg_types.size(), is_variadic, 0, *cc);
1217
1218 return clang::QualType::getFromOpaquePtr(
1219 func_sig_ast_type.GetOpaqueQualType());
1220}
1221
1222static bool isTagDecl(clang::DeclContext &context) {
1223 return llvm::isa<clang::TagDecl>(&context);
1224}
1225
1226static bool isFunctionDecl(clang::DeclContext &context) {
1227 return llvm::isa<clang::FunctionDecl>(&context);
1228}
1229
1230static bool isBlockDecl(clang::DeclContext &context) {
1231 return llvm::isa<clang::BlockDecl>(&context);
1232}
1233
1234void PdbAstBuilder::ParseNamespace(clang::DeclContext &context) {
1235 clang::NamespaceDecl *ns = llvm::dyn_cast<clang::NamespaceDecl>(&context);
4
Assuming the object is not a 'CastReturnType'
5
'ns' initialized to a null pointer value
1236 if (m_parsed_namespaces.contains(ns))
6
Assuming the condition is false
7
Taking false branch
1237 return;
1238 std::string qname = ns->getQualifiedNameAsString();
8
Called C++ object pointer is null
1239 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1240 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1241 PdbIndex &index = pdb->GetIndex();
1242 TypeIndex ti{index.tpi().TypeIndexBegin()};
1243 for (const CVType &cvt : index.tpi().typeArray()) {
1244 PdbTypeSymId tid{ti};
1245 ++ti;
1246
1247 if (!IsTagRecord(cvt))
1248 continue;
1249
1250 CVTagRecord tag = CVTagRecord::create(cvt);
1251
1252 // Call CreateDeclInfoForType unconditionally so that the namespace info
1253 // gets created. But only call CreateRecordType if the namespace name
1254 // matches.
1255 clang::DeclContext *context = nullptr;
1256 std::string uname;
1257 std::tie(context, uname) = CreateDeclInfoForType(tag.asTag(), tid.index);
1258 if (!context || !context->isNamespace())
1259 continue;
1260
1261 clang::NamespaceDecl *ns = llvm::cast<clang::NamespaceDecl>(context);
1262 llvm::StringRef ns_name = ns->getName();
1263 if (ns_name.startswith(qname)) {
1264 ns_name = ns_name.drop_front(qname.size());
1265 if (ns_name.startswith("::"))
1266 GetOrCreateType(tid);
1267 }
1268 }
1269 ParseAllFunctionsAndNonLocalVars();
1270 m_parsed_namespaces.insert(ns);
1271}
1272
1273void PdbAstBuilder::ParseAllTypes() {
1274 llvm::call_once(m_parse_all_types, [this]() {
1275 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1276 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1277 PdbIndex &index = pdb->GetIndex();
1278 TypeIndex ti{index.tpi().TypeIndexBegin()};
1279 for (const CVType &cvt : index.tpi().typeArray()) {
1280 PdbTypeSymId tid{ti};
1281 ++ti;
1282
1283 if (!IsTagRecord(cvt))
1284 continue;
1285
1286 GetOrCreateType(tid);
1287 }
1288 });
1289}
1290
1291void PdbAstBuilder::ParseAllFunctionsAndNonLocalVars() {
1292 llvm::call_once(m_parse_functions_and_non_local_vars, [this]() {
1293 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1294 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1295 PdbIndex &index = pdb->GetIndex();
1296 uint32_t module_count = index.dbi().modules().getModuleCount();
1297 for (uint16_t modi = 0; modi < module_count; ++modi) {
1298 CompilandIndexItem &cii = index.compilands().GetOrCreateCompiland(modi);
1299 const CVSymbolArray &symbols = cii.m_debug_stream.getSymbolArray();
1300 auto iter = symbols.begin();
1301 while (iter != symbols.end()) {
1302 PdbCompilandSymId sym_id{modi, iter.offset()};
1303
1304 switch (iter->kind()) {
1305 case S_GPROC32:
1306 case S_LPROC32:
1307 GetOrCreateFunctionDecl(sym_id);
1308 iter = symbols.at(getScopeEndOffset(*iter));
1309 break;
1310 case S_GDATA32:
1311 case S_GTHREAD32:
1312 case S_LDATA32:
1313 case S_LTHREAD32:
1314 GetOrCreateVariableDecl(PdbCompilandSymId(modi, 0), sym_id);
1315 ++iter;
1316 break;
1317 default:
1318 ++iter;
1319 continue;
1320 }
1321 }
1322 }
1323 });
1324}
1325
1326static CVSymbolArray skipFunctionParameters(clang::Decl &decl,
1327 const CVSymbolArray &symbols) {
1328 clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>(&decl);
1329 if (!func_decl)
1330 return symbols;
1331 unsigned int params = func_decl->getNumParams();
1332 if (params == 0)
1333 return symbols;
1334
1335 CVSymbolArray result = symbols;
1336
1337 while (!result.empty()) {
1338 if (params == 0)
1339 return result;
1340
1341 CVSymbol sym = *result.begin();
1342 result.drop_front();
1343
1344 if (!isLocalVariableType(sym.kind()))
1345 continue;
1346
1347 --params;
1348 }
1349 return result;
1350}
1351
1352void PdbAstBuilder::ParseBlockChildren(PdbCompilandSymId block_id) {
1353 SymbolFileNativePDB *pdb = static_cast<SymbolFileNativePDB *>(
1354 m_clang.GetSymbolFile()->GetBackingSymbolFile());
1355 PdbIndex &index = pdb->GetIndex();
1356 CVSymbol sym = index.ReadSymbolRecord(block_id);
1357 lldbassert(sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32 ||lldb_private::lldb_assert(static_cast<bool>(sym.kind() ==
S_GPROC32 || sym.kind() == S_LPROC32 || sym.kind() == S_BLOCK32
|| sym.kind() == S_INLINESITE), "sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32 || sym.kind() == S_BLOCK32 || sym.kind() == S_INLINESITE"
, __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 1358)
1358 sym.kind() == S_BLOCK32 || sym.kind() == S_INLINESITE)lldb_private::lldb_assert(static_cast<bool>(sym.kind() ==
S_GPROC32 || sym.kind() == S_LPROC32 || sym.kind() == S_BLOCK32
|| sym.kind() == S_INLINESITE), "sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32 || sym.kind() == S_BLOCK32 || sym.kind() == S_INLINESITE"
, __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 1358)
;
1359 CompilandIndexItem &cii =
1360 index.compilands().GetOrCreateCompiland(block_id.modi);
1361 CVSymbolArray symbols =
1362 cii.m_debug_stream.getSymbolArrayForScope(block_id.offset);
1363
1364 // Function parameters should already have been created when the function was
1365 // parsed.
1366 if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32)
1367 symbols =
1368 skipFunctionParameters(*m_uid_to_decl[toOpaqueUid(block_id)], symbols);
1369
1370 symbols.drop_front();
1371 auto begin = symbols.begin();
1372 while (begin != symbols.end()) {
1373 PdbCompilandSymId child_sym_id(block_id.modi, begin.offset());
1374 GetOrCreateSymbolForId(child_sym_id);
1375 if (begin->kind() == S_BLOCK32 || begin->kind() == S_INLINESITE) {
1376 ParseBlockChildren(child_sym_id);
1377 begin = symbols.at(getScopeEndOffset(*begin));
1378 }
1379 ++begin;
1380 }
1381}
1382
1383void PdbAstBuilder::ParseDeclsForSimpleContext(clang::DeclContext &context) {
1384
1385 clang::Decl *decl = clang::Decl::castFromDeclContext(&context);
1386 lldbassert(decl)lldb_private::lldb_assert(static_cast<bool>(decl), "decl"
, __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 1386)
;
1387
1388 auto iter = m_decl_to_status.find(decl);
1389 lldbassert(iter != m_decl_to_status.end())lldb_private::lldb_assert(static_cast<bool>(iter != m_decl_to_status
.end()), "iter != m_decl_to_status.end()", __FUNCTION__, "lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp"
, 1389)
;
1390
1391 if (auto *tag = llvm::dyn_cast<clang::TagDecl>(&context)) {
1392 CompleteTagDecl(*tag);
1393 return;
1394 }
1395
1396 if (isFunctionDecl(context) || isBlockDecl(context)) {
1397 PdbCompilandSymId block_id = PdbSymUid(iter->second.uid).asCompilandSym();
1398 ParseBlockChildren(block_id);
1399 }
1400}
1401
1402void PdbAstBuilder::ParseDeclsForContext(clang::DeclContext &context) {
1403 // Namespaces aren't explicitly represented in the debug info, and the only
1404 // way to parse them is to parse all type info, demangling every single type
1405 // and trying to reconstruct the DeclContext hierarchy this way. Since this
1406 // is an expensive operation, we have to special case it so that we do other
1407 // work (such as parsing the items that appear within the namespaces) at the
1408 // same time.
1409 if (context.isTranslationUnit()) {
1
Taking false branch
1410 ParseAllTypes();
1411 ParseAllFunctionsAndNonLocalVars();
1412 return;
1413 }
1414
1415 if (context.isNamespace()) {
2
Taking true branch
1416 ParseNamespace(context);
3
Calling 'PdbAstBuilder::ParseNamespace'
1417 return;
1418 }
1419
1420 if (isTagDecl(context) || isFunctionDecl(context) || isBlockDecl(context)) {
1421 ParseDeclsForSimpleContext(context);
1422 return;
1423 }
1424}
1425
1426CompilerDecl PdbAstBuilder::ToCompilerDecl(clang::Decl &decl) {
1427 return m_clang.GetCompilerDecl(&decl);
1428}
1429
1430CompilerType PdbAstBuilder::ToCompilerType(clang::QualType qt) {
1431 return {m_clang.weak_from_this(), qt.getAsOpaquePtr()};
1432}
1433
1434CompilerDeclContext
1435PdbAstBuilder::ToCompilerDeclContext(clang::DeclContext &context) {
1436 return m_clang.CreateDeclContext(&context);
1437}
1438
1439clang::Decl * PdbAstBuilder::FromCompilerDecl(CompilerDecl decl) {
1440 return ClangUtil::GetDecl(decl);
1441}
1442
1443clang::DeclContext *
1444PdbAstBuilder::FromCompilerDeclContext(CompilerDeclContext context) {
1445 return static_cast<clang::DeclContext *>(context.GetOpaqueDeclContext());
1446}
1447
1448void PdbAstBuilder::Dump(Stream &stream) {
1449 m_clang.Dump(stream.AsRawOstream());
1450}