diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp index 264b2e84114077..7097ab2dcb230b 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp @@ -54,6 +54,7 @@ #include "Plugins/ExpressionParser/Clang/ClangUserExpression.h" #include "Plugins/ExpressionParser/Clang/ClangUtil.h" #include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h" +#include "lldb/Core/Debugger.h" #include "lldb/Core/DumpDataExtractor.h" #include "lldb/Core/Module.h" #include "lldb/Core/PluginManager.h" @@ -697,10 +698,20 @@ void TypeSystemClang::CreateASTContext() { TargetInfo *target_info = getTargetInfo(); if (target_info) m_ast_up->InitBuiltinTypes(*target_info); - else if (auto *log = GetLog(LLDBLog::Expressions)) - LLDB_LOG(log, - "Failed to initialize builtin ASTContext types for target '{0}'", - m_target_triple); + else { + std::string err = + llvm::formatv( + "Failed to initialize builtin ASTContext types for target '{0}'. " + "Printing variables may behave unexpectedly.", + m_target_triple) + .str(); + + LLDB_LOG(GetLog(LLDBLog::Expressions), err.c_str()); + + static std::once_flag s_uninitialized_target_warning; + Debugger::ReportWarning(std::move(err), /*debugger_id=*/std::nullopt, + &s_uninitialized_target_warning); + } GetASTMap().Insert(m_ast_up.get(), this); @@ -749,6 +760,10 @@ CompilerType TypeSystemClang::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding, size_t bit_size) { ASTContext &ast = getASTContext(); + + if (!ast.VoidPtrTy) + return {}; + switch (encoding) { case eEncodingInvalid: if (QualTypeMatchesBitSize(bit_size, ast, ast.VoidPtrTy)) @@ -891,6 +906,9 @@ CompilerType TypeSystemClang::GetBuiltinTypeForDWARFEncodingAndBitSize( llvm::StringRef type_name, uint32_t dw_ate, uint32_t bit_size) { ASTContext &ast = getASTContext(); + if (!ast.VoidPtrTy) + return {}; + switch (dw_ate) { default: break; @@ -2335,6 +2353,9 @@ CompilerType TypeSystemClang::GetIntTypeFromBitSize(size_t bit_size, bool is_signed) { clang::ASTContext &ast = getASTContext(); + if (!ast.VoidPtrTy) + return {}; + if (is_signed) { if (bit_size == ast.getTypeSize(ast.SignedCharTy)) return GetType(ast.SignedCharTy); @@ -2376,6 +2397,9 @@ CompilerType TypeSystemClang::GetIntTypeFromBitSize(size_t bit_size, } CompilerType TypeSystemClang::GetPointerSizedIntType(bool is_signed) { + if (!getASTContext().VoidPtrTy) + return {}; + return GetIntTypeFromBitSize( getASTContext().getTypeSize(getASTContext().VoidPtrTy), is_signed); } @@ -7453,6 +7477,13 @@ clang::FieldDecl *TypeSystemClang::AddFieldToRecordType( clang::Expr *bit_width = nullptr; if (bitfield_bit_size != 0) { + if (clang_ast.IntTy.isNull()) { + LLDB_LOG( + GetLog(LLDBLog::Expressions), + "{0} failed: builtin ASTContext types have not been initialized"); + return nullptr; + } + llvm::APInt bitfield_bit_size_apint(clang_ast.getTypeSize(clang_ast.IntTy), bitfield_bit_size); bit_width = new (clang_ast) diff --git a/lldb/unittests/Symbol/TestTypeSystemClang.cpp b/lldb/unittests/Symbol/TestTypeSystemClang.cpp index 7d64e1cdd56f64..0733e42bb46331 100644 --- a/lldb/unittests/Symbol/TestTypeSystemClang.cpp +++ b/lldb/unittests/Symbol/TestTypeSystemClang.cpp @@ -13,6 +13,7 @@ #include "lldb/Core/Declaration.h" #include "lldb/Host/FileSystem.h" #include "lldb/Host/HostInfo.h" +#include "lldb/lldb-enumerations.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/ExprCXX.h" @@ -231,6 +232,37 @@ TEST_F(TestTypeSystemClang, TestBuiltinTypeForEncodingAndBitSize) { VerifyEncodingAndBitSize(*m_ast, eEncodingIEEE754, 64); } +TEST_F(TestTypeSystemClang, TestBuiltinTypeForEmptyTriple) { + // Test that we can access type-info of builtin Clang AST + // types without crashing even when the target triple is + // empty. + + TypeSystemClang ast("empty triple AST", llvm::Triple{}); + + // This test only makes sense if the builtin ASTContext types were + // not initialized. + ASSERT_TRUE(ast.getASTContext().VoidPtrTy.isNull()); + + EXPECT_FALSE(ast.GetBuiltinTypeByName(ConstString("int")).IsValid()); + EXPECT_FALSE(ast.GetBuiltinTypeForDWARFEncodingAndBitSize( + "char", dwarf::DW_ATE_signed_char, 8) + .IsValid()); + EXPECT_FALSE(ast.GetBuiltinTypeForEncodingAndBitSize(lldb::eEncodingUint, 8) + .IsValid()); + EXPECT_FALSE(ast.GetPointerSizedIntType(/*is_signed=*/false)); + EXPECT_FALSE(ast.GetIntTypeFromBitSize(8, /*is_signed=*/false)); + + CompilerType record_type = ast.CreateRecordType( + nullptr, OptionalClangModuleID(), lldb::eAccessPublic, "Record", + llvm::to_underlying(clang::TagTypeKind::Struct), + lldb::eLanguageTypeC_plus_plus, std::nullopt); + TypeSystemClang::StartTagDeclarationDefinition(record_type); + EXPECT_EQ(ast.AddFieldToRecordType(record_type, "field", record_type, + eAccessPublic, /*bitfield_bit_size=*/8), + nullptr); + TypeSystemClang::CompleteTagDeclarationDefinition(record_type); +} + TEST_F(TestTypeSystemClang, TestDisplayName) { TypeSystemClang ast("some name", llvm::Triple()); EXPECT_EQ("some name", ast.getDisplayName());