From e664b5dc9dae36c380494bacecab33b7dca5b13e Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Mon, 19 Mar 2018 21:14:19 +0000 Subject: [PATCH] [SymbolFilePDB] Remove a few null pointer checks by passing ref Reviewers: zturner, rnk, lldb-commits Reviewed By: zturner Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D44455 llvm-svn: 327908 --- .../source/Plugins/SymbolFile/PDB/PDBASTParser.cpp | 29 ++++----- .../Plugins/SymbolFile/PDB/SymbolFilePDB.cpp | 70 ++++++++++------------ lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h | 10 ++-- 3 files changed, 46 insertions(+), 63 deletions(-) diff --git a/lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp b/lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp index bf353a4..e26d46d 100644 --- a/lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp +++ b/lldb/source/Plugins/SymbolFile/PDB/PDBASTParser.cpp @@ -94,24 +94,20 @@ lldb::Encoding TranslateEnumEncoding(PDB_VariantType type) { } CompilerType GetBuiltinTypeForPDBEncodingAndBitSize( - ClangASTContext *clang_ast, const PDBSymbolTypeBuiltin *pdb_type, + ClangASTContext &clang_ast, const PDBSymbolTypeBuiltin &pdb_type, Encoding encoding, uint32_t width) { - if (!pdb_type) - return CompilerType(); - if (!clang_ast) - return CompilerType(); - auto *ast = clang_ast->getASTContext(); + auto *ast = clang_ast.getASTContext(); if (!ast) return CompilerType(); - switch (pdb_type->getBuiltinType()) { + switch (pdb_type.getBuiltinType()) { default: break; case PDB_BuiltinType::None: return CompilerType(); case PDB_BuiltinType::Void: - return clang_ast->GetBasicType(eBasicTypeVoid); + return clang_ast.GetBasicType(eBasicTypeVoid); case PDB_BuiltinType::Bool: - return clang_ast->GetBasicType(eBasicTypeBool); + return clang_ast.GetBasicType(eBasicTypeBool); case PDB_BuiltinType::Long: if (width == ast->getTypeSize(ast->LongTy)) return CompilerType(ast, ast->LongTy); @@ -141,15 +137,12 @@ CompilerType GetBuiltinTypeForPDBEncodingAndBitSize( } // If there is no match on PDB_BuiltinType, fall back to default search // by encoding and width only - return clang_ast->GetBuiltinTypeForEncodingAndBitSize(encoding, width); + return clang_ast.GetBuiltinTypeForEncodingAndBitSize(encoding, width); } -ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin *pdb_type, +ConstString GetPDBBuiltinTypeName(const PDBSymbolTypeBuiltin &pdb_type, CompilerType &compiler_type) { - if (!pdb_type) - return compiler_type.GetTypeName(); - - PDB_BuiltinType kind = pdb_type->getBuiltinType(); + PDB_BuiltinType kind = pdb_type.getBuiltinType(); switch (kind) { default: break; case PDB_BuiltinType::Currency: @@ -262,7 +255,7 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) { CompilerType builtin_type; if (bytes > 0) builtin_type = GetBuiltinTypeForPDBEncodingAndBitSize( - &m_ast, underlying_type_up.get(), encoding, bytes * 8); + m_ast, *underlying_type_up, encoding, bytes * 8); else builtin_type = m_ast.GetBasicType(eBasicTypeInt); // FIXME: PDB does not have information about scoped enumeration (Enum Class). @@ -418,7 +411,7 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) { uint64_t bytes = builtin_type->getLength(); Encoding encoding = TranslateBuiltinEncoding(builtin_kind); CompilerType builtin_ast_type = GetBuiltinTypeForPDBEncodingAndBitSize( - &m_ast, builtin_type, encoding, bytes * 8); + m_ast, *builtin_type, encoding, bytes * 8); if (builtin_type->isConstType()) builtin_ast_type = builtin_ast_type.AddConstModifier(); @@ -426,7 +419,7 @@ lldb::TypeSP PDBASTParser::CreateLLDBTypeFromPDBType(const PDBSymbol &type) { if (builtin_type->isVolatileType()) builtin_ast_type = builtin_ast_type.AddVolatileModifier(); - auto type_name = GetPDBBuiltinTypeName(builtin_type, builtin_ast_type); + auto type_name = GetPDBBuiltinTypeName(*builtin_type, builtin_ast_type); return std::make_shared( builtin_type->getSymIndexId(), m_ast.GetSymbolFile(), type_name, diff --git a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp index 871da7a..314708e 100644 --- a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp +++ b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp @@ -205,15 +205,12 @@ uint32_t SymbolFilePDB::GetNumCompileUnits() { } void SymbolFilePDB::GetCompileUnitIndex( - const llvm::pdb::PDBSymbolCompiland *pdb_compiland, + const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) { - if (!pdb_compiland) - return; - auto results_up = m_global_scope_up->findAllChildren(); if (!results_up) return; - auto uid = pdb_compiland->getSymIndexId(); + auto uid = pdb_compiland.getSymIndexId(); for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { auto compiland_up = results_up->getChildAtIndex(cu_idx); if (!compiland_up) @@ -266,32 +263,31 @@ SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) { lldb_private::Function * SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc( - const PDBSymbolFunc *pdb_func, + const PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc) { - assert(pdb_func != nullptr); lldbassert(sc.comp_unit && sc.module_sp.get()); - auto file_vm_addr = pdb_func->getVirtualAddress(); + auto file_vm_addr = pdb_func.getVirtualAddress(); if (file_vm_addr == LLDB_INVALID_ADDRESS) return nullptr; - auto func_length = pdb_func->getLength(); + auto func_length = pdb_func.getLength(); AddressRange func_range = AddressRange(file_vm_addr, func_length, sc.module_sp->GetSectionList()); if (!func_range.GetBaseAddress().IsValid()) return nullptr; - lldb_private::Type* func_type = ResolveTypeUID(pdb_func->getSymIndexId()); + lldb_private::Type* func_type = ResolveTypeUID(pdb_func.getSymIndexId()); if (!func_type) return nullptr; - user_id_t func_type_uid = pdb_func->getSignatureId(); + user_id_t func_type_uid = pdb_func.getSignatureId(); Mangled mangled = GetMangledForPDBFunc(pdb_func); FunctionSP func_sp = std::make_shared(sc.comp_unit, - pdb_func->getSymIndexId(), + pdb_func.getSymIndexId(), func_type_uid, mangled, func_type, @@ -315,7 +311,7 @@ size_t SymbolFilePDB::ParseCompileUnitFunctions( auto func_sp = sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId()); if (!func_sp) { - if (ParseCompileUnitFunctionForPDBFunc(pdb_func_up.get(), sc)) + if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc)) ++func_added; } } @@ -591,7 +587,7 @@ SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, auto func_uid = pdb_func->getSymIndexId(); sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); if (sc.function == nullptr) - sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc); + sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc); if (sc.function) { resolved_flags |= eSymbolContextFunction; if (resolve_scope & eSymbolContextBlock) { @@ -765,7 +761,7 @@ uint32_t SymbolFilePDB::ResolveSymbolContext( if (sc.function == nullptr) { auto pdb_func = llvm::dyn_cast(symbol_up.get()); assert(pdb_func); - sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc); + sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc); } if (sc.function && (resolve_scope & eSymbolContextBlock)) { Block &block = sc.function->GetBlock(true); @@ -803,13 +799,11 @@ SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression ®ex, return uint32_t(); } -bool SymbolFilePDB::ResolveFunction(llvm::pdb::PDBSymbolFunc *pdb_func, +bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func, bool include_inlines, lldb_private::SymbolContextList &sc_list) { - if (!pdb_func) - return false; lldb_private::SymbolContext sc; - auto file_vm_addr = pdb_func->getVirtualAddress(); + auto file_vm_addr = pdb_func.getVirtualAddress(); if (file_vm_addr == LLDB_INVALID_ADDRESS) return false; @@ -825,7 +819,7 @@ bool SymbolFilePDB::ResolveFunction(llvm::pdb::PDBSymbolFunc *pdb_func, auto *func = llvm::dyn_cast(symbol_up.get()); assert(func); - sc.function = ParseCompileUnitFunctionForPDBFunc(func, sc); + sc.function = ParseCompileUnitFunctionForPDBFunc(*func, sc); if (!sc.function) return false; @@ -839,7 +833,7 @@ bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines, m_session_up->getConcreteSymbolById(uid); if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute())) return false; - return ResolveFunction(pdb_func_up.get(), include_inlines, sc_list); + return ResolveFunction(*pdb_func_up, include_inlines, sc_list); } void SymbolFilePDB::CacheFunctionNames() { @@ -1163,14 +1157,11 @@ lldb_private::TypeList *SymbolFilePDB::GetTypeList() { } void -SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol *pdb_symbol, +SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol, uint32_t type_mask, TypeCollection &type_collection) { - if (!pdb_symbol) - return; - bool can_parse = false; - switch (pdb_symbol->getSymTag()) { + switch (pdb_symbol.getSymTag()) { case PDB_SymType::ArrayType: can_parse = ((type_mask & eTypeClassArray) != 0); break; @@ -1192,7 +1183,7 @@ SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol *pdb_symbol, can_parse = ((type_mask & eTypeClassTypedef) != 0); break; case PDB_SymType::UDT: { - auto *udt = llvm::dyn_cast(pdb_symbol); + auto *udt = llvm::dyn_cast(&pdb_symbol); assert(udt); can_parse = (udt->getUdtKind() != PDB_UdtType::Interface && ((type_mask & (eTypeClassClass | eTypeClassStruct | @@ -1202,7 +1193,7 @@ SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol *pdb_symbol, } if (can_parse) { - if (auto *type = ResolveTypeUID(pdb_symbol->getSymIndexId())) { + if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) { auto result = std::find(type_collection.begin(), type_collection.end(), type); if (result == type_collection.end()) @@ -1210,9 +1201,9 @@ SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol *pdb_symbol, } } - auto results_up = pdb_symbol->findAllChildren(); + auto results_up = pdb_symbol.findAllChildren(); while (auto symbol_up = results_up->getNext()) - GetTypesForPDBSymbol(symbol_up.get(), type_mask, type_collection); + GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection); } size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, @@ -1224,13 +1215,15 @@ size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, sc_scope->CalculateSymbolContextCompileUnit() : nullptr; if (cu) { auto compiland_up = GetPDBCompilandByUID(cu->GetID()); - GetTypesForPDBSymbol(compiland_up.get(), type_mask, type_collection); + if (!compiland_up) + return 0; + GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); } else { for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { auto cu_sp = ParseCompileUnitAtIndex(cu_idx); if (cu_sp.get()) { - auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()); - GetTypesForPDBSymbol(compiland_up.get(), type_mask, type_collection); + if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID())) + GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); } } } @@ -1306,7 +1299,7 @@ SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, uint32_t index) { m_comp_units.insert(std::make_pair(id, cu_sp)); if (index == UINT32_MAX) - GetCompileUnitIndex(compiland_up.get(), index); + GetCompileUnitIndex(*compiland_up, index); lldbassert(index != UINT32_MAX); m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex( index, cu_sp); @@ -1459,13 +1452,10 @@ lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress( } Mangled -SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc *pdb_func) { +SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) { Mangled mangled; - if (!pdb_func) - return mangled; - - auto func_name = pdb_func->getName(); - auto func_undecorated_name = pdb_func->getUndecoratedName(); + auto func_name = pdb_func.getName(); + auto func_undecorated_name = pdb_func.getUndecoratedName(); std::string func_decorated_name; // Seek from public symbols for non-static function's decorated name if any. diff --git a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h index ba9fa75..f688d2a 100644 --- a/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h +++ b/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.h @@ -188,14 +188,14 @@ private: typedef std::vector TypeCollection; void - GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol *pdb_symbol, + GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol, uint32_t type_mask, TypeCollection &type_collection); lldb_private::Function* ParseCompileUnitFunctionForPDBFunc( - const llvm::pdb::PDBSymbolFunc *pdb_func, + const llvm::pdb::PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc); - void GetCompileUnitIndex(const llvm::pdb::PDBSymbolCompiland *pdb_compiland, + void GetCompileUnitIndex(const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index); std::string GetSourceFileNameForPDBCompiland( @@ -205,9 +205,9 @@ private: GetPDBCompilandByUID(uint32_t uid); lldb_private::Mangled - GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc *pdb_func); + GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func); - bool ResolveFunction(llvm::pdb::PDBSymbolFunc *pdb_func, + bool ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func, bool include_inlines, lldb_private::SymbolContextList &sc_list); -- 2.7.4