https://github.com/kuilpd created https://github.com/llvm/llvm-project/pull/135843
Add a function `IsValidDereferenceType` to TypeSystem. TypeSystemClang now allows arrays to be dereferenced. >From 889900ece6cccfb7cd2d8d16706bc2d5db18c381 Mon Sep 17 00:00:00 2001 From: Ilia Kuklin <ikuk...@accesssoftek.com> Date: Wed, 16 Apr 2025 00:30:51 +0500 Subject: [PATCH] [lldb][TypeSystemClang] Add a function `IsValidDereferenceType` to TypeSystem to allow arrays to be dereferenced in C/C++. --- lldb/include/lldb/Symbol/CompilerType.h | 2 ++ lldb/include/lldb/Symbol/TypeSystem.h | 2 ++ lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp | 9 +++++++++ lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h | 2 ++ lldb/source/Symbol/CompilerType.cpp | 7 +++++++ lldb/source/ValueObject/ValueObject.cpp | 7 ++++--- 6 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lldb/include/lldb/Symbol/CompilerType.h b/lldb/include/lldb/Symbol/CompilerType.h index 41a1676dabd76..de222a6c5ce8e 100644 --- a/lldb/include/lldb/Symbol/CompilerType.h +++ b/lldb/include/lldb/Symbol/CompilerType.h @@ -186,6 +186,8 @@ class CompilerType { bool IsReferenceType(CompilerType *pointee_type = nullptr, bool *is_rvalue = nullptr) const; + bool IsValidDereferenceType() const; + bool ShouldTreatScalarValueAsAddress() const; bool IsScalarType() const; diff --git a/lldb/include/lldb/Symbol/TypeSystem.h b/lldb/include/lldb/Symbol/TypeSystem.h index 59fb066e087d3..d881136b22699 100644 --- a/lldb/include/lldb/Symbol/TypeSystem.h +++ b/lldb/include/lldb/Symbol/TypeSystem.h @@ -494,6 +494,8 @@ class TypeSystem : public PluginInterface, virtual bool IsReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type, bool *is_rvalue) = 0; + virtual bool IsValidDereferenceType(lldb::opaque_compiler_type_t type) = 0; + virtual bool ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) { return IsPointerOrReferenceType(type, nullptr); diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp index ed6297cc6f3e0..ed56ad92e23f2 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp @@ -3443,6 +3443,13 @@ bool TypeSystemClang::IsReferenceType(lldb::opaque_compiler_type_t type, return false; } +bool TypeSystemClang::IsValidDereferenceType( + lldb::opaque_compiler_type_t type) { + CompilerType compiler_type = GetType(clang::QualType::getFromOpaquePtr(type)); + return compiler_type.IsPointerOrReferenceType() || + compiler_type.IsArrayType(); +} + bool TypeSystemClang::IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count, bool &is_complex) { if (type) { @@ -6565,6 +6572,8 @@ llvm::Expected<CompilerType> TypeSystemClang::GetChildCompilerTypeAtIndex( return size_or_err.takeError(); child_byte_size = *size_or_err; child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; + if (idx == 0) + child_is_deref_of_parent = true; return element_type; } } diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h index 442f88a5b79ae..5026e26041afd 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h @@ -716,6 +716,8 @@ class TypeSystemClang : public TypeSystem { bool IsReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type, bool *is_rvalue) override; + bool IsValidDereferenceType(lldb::opaque_compiler_type_t type) override; + bool IsScalarType(lldb::opaque_compiler_type_t type) override; bool IsTypedefType(lldb::opaque_compiler_type_t type) override; diff --git a/lldb/source/Symbol/CompilerType.cpp b/lldb/source/Symbol/CompilerType.cpp index 22fdd24fc7cd5..c73046f137470 100644 --- a/lldb/source/Symbol/CompilerType.cpp +++ b/lldb/source/Symbol/CompilerType.cpp @@ -233,6 +233,13 @@ bool CompilerType::IsReferenceType(CompilerType *pointee_type, return false; } +bool CompilerType::IsValidDereferenceType() const { + if (IsValid()) + if (auto type_system_sp = GetTypeSystem()) + return type_system_sp->IsValidDereferenceType(m_type); + return false; +} + bool CompilerType::ShouldTreatScalarValueAsAddress() const { if (IsValid()) if (auto type_system_sp = GetTypeSystem()) diff --git a/lldb/source/ValueObject/ValueObject.cpp b/lldb/source/ValueObject/ValueObject.cpp index eac24353de90b..caa859376c698 100644 --- a/lldb/source/ValueObject/ValueObject.cpp +++ b/lldb/source/ValueObject/ValueObject.cpp @@ -2844,8 +2844,9 @@ ValueObjectSP ValueObject::Dereference(Status &error) { if (m_deref_valobj) return m_deref_valobj->GetSP(); - const bool is_pointer_or_reference_type = IsPointerOrReferenceType(); - if (is_pointer_or_reference_type) { + const bool is_valid_dereference_type = + GetCompilerType().IsValidDereferenceType(); + if (is_valid_dereference_type) { bool omit_empty_base_classes = true; bool ignore_array_bounds = false; @@ -2924,7 +2925,7 @@ ValueObjectSP ValueObject::Dereference(Status &error) { StreamString strm; GetExpressionPath(strm); - if (is_pointer_or_reference_type) + if (is_valid_dereference_type) error = Status::FromErrorStringWithFormat( "dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetData()); _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits