Bug Summary

File:build/llvm-toolchain-snapshot-16~++20221003111214+1fa2019828ca/lldb/source/Plugins/SymbolFile/NativePDB/PdbAstBuilder.cpp
Warning:line 1392, column 5
Dereference of null pointer

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