diff --git a/lldb/include/lldb/API/SBTarget.h b/lldb/include/lldb/API/SBTarget.h index 3644ac056da3..823615e6a36d 100644 --- a/lldb/include/lldb/API/SBTarget.h +++ b/lldb/include/lldb/API/SBTarget.h @@ -954,6 +954,7 @@ protected: friend class SBSection; friend class SBSourceManager; friend class SBSymbol; + friend class SBTypeStaticField; friend class SBValue; friend class SBVariablesOptions; diff --git a/lldb/include/lldb/API/SBType.h b/lldb/include/lldb/API/SBType.h index 9980fe121830..5b9ff2170b2b 100644 --- a/lldb/include/lldb/API/SBType.h +++ b/lldb/include/lldb/API/SBType.h @@ -107,6 +107,35 @@ protected: lldb::TypeMemberFunctionImplSP m_opaque_sp; }; +class LLDB_API SBTypeStaticField { +public: + SBTypeStaticField(); + + SBTypeStaticField(const lldb::SBTypeStaticField &rhs); + lldb::SBTypeStaticField &operator=(const lldb::SBTypeStaticField &rhs); + + ~SBTypeStaticField(); + + explicit operator bool() const; + + bool IsValid() const; + + const char *GetName(); + + const char *GetMangledName(); + + lldb::SBType GetType(); + + lldb::SBValue GetConstantValue(lldb::SBTarget target); + +protected: + friend class SBType; + + explicit SBTypeStaticField(lldb_private::CompilerDecl decl); + + std::unique_ptr m_opaque_up; +}; + class SBType { public: SBType(); @@ -182,6 +211,8 @@ public: lldb::SBTypeMember GetVirtualBaseClassAtIndex(uint32_t idx); + lldb::SBTypeStaticField GetStaticFieldWithName(const char *name); + lldb::SBTypeEnumMemberList GetEnumMembers(); uint32_t GetNumberOfTemplateArguments(); @@ -242,6 +273,7 @@ protected: friend class SBTypeNameSpecifier; friend class SBTypeMember; friend class SBTypeMemberFunction; + friend class SBTypeStaticField; friend class SBTypeList; friend class SBValue; friend class SBWatchpoint; diff --git a/lldb/include/lldb/API/SBValue.h b/lldb/include/lldb/API/SBValue.h index bbcccaab51aa..67f55ce7da28 100644 --- a/lldb/include/lldb/API/SBValue.h +++ b/lldb/include/lldb/API/SBValue.h @@ -426,6 +426,7 @@ protected: friend class SBModule; friend class SBTarget; friend class SBThread; + friend class SBTypeStaticField; friend class SBTypeSummary; friend class SBValueList; diff --git a/lldb/include/lldb/Symbol/CompilerDecl.h b/lldb/include/lldb/Symbol/CompilerDecl.h index 825a4f15836f..5c99cae3781c 100644 --- a/lldb/include/lldb/Symbol/CompilerDecl.h +++ b/lldb/include/lldb/Symbol/CompilerDecl.h @@ -73,6 +73,9 @@ public: CompilerDeclContext GetDeclContext() const; + // If this decl has a type, return it. + CompilerType GetType() const; + // If this decl represents a function, return the return type CompilerType GetFunctionReturnType() const; @@ -91,6 +94,10 @@ public: /// the subsequent entry, so the topmost entry is the global namespace. std::vector GetCompilerContext() const; + // If decl represents a constant value, return it. Otherwise, return an + // invalid/empty Scalar. + Scalar GetConstantValue() const; + private: TypeSystem *m_type_system = nullptr; void *m_opaque_decl = nullptr; diff --git a/lldb/include/lldb/Symbol/CompilerType.h b/lldb/include/lldb/Symbol/CompilerType.h index b71c531f2163..9e889a53086b 100644 --- a/lldb/include/lldb/Symbol/CompilerType.h +++ b/lldb/include/lldb/Symbol/CompilerType.h @@ -416,6 +416,8 @@ public: CompilerType GetVirtualBaseClassAtIndex(size_t idx, uint32_t *bit_offset_ptr) const; + CompilerDecl GetStaticFieldWithName(llvm::StringRef name) const; + uint32_t GetIndexOfFieldWithName(const char *name, CompilerType *field_compiler_type = nullptr, uint64_t *bit_offset_ptr = nullptr, diff --git a/lldb/include/lldb/Symbol/TypeSystem.h b/lldb/include/lldb/Symbol/TypeSystem.h index 3a927d313b82..ad3b853227a9 100644 --- a/lldb/include/lldb/Symbol/TypeSystem.h +++ b/lldb/include/lldb/Symbol/TypeSystem.h @@ -27,6 +27,8 @@ #include "lldb/Symbol/CompilerDecl.h" #include "lldb/Symbol/CompilerDeclContext.h" #include "lldb/Symbol/Type.h" +#include "lldb/Utility/Scalar.h" +#include "lldb/lldb-forward.h" #include "lldb/lldb-private.h" #include "lldb/lldb-types.h" @@ -110,6 +112,8 @@ public: virtual std::vector DeclGetCompilerContext(void *opaque_decl); + virtual Scalar DeclGetConstantValue(void *opaque_decl) { return Scalar(); } + virtual CompilerType GetTypeForDecl(void *opaque_decl) = 0; // CompilerDeclContext functions @@ -339,6 +343,11 @@ public: GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) = 0; + virtual CompilerDecl GetStaticFieldWithName(lldb::opaque_compiler_type_t type, + llvm::StringRef name) { + return CompilerDecl(); + } + virtual CompilerType GetChildCompilerTypeAtIndex( lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers, bool omit_empty_base_classes, diff --git a/lldb/source/API/SBType.cpp b/lldb/source/API/SBType.cpp index ac0e56303fae..6cecb5c9ea81 100644 --- a/lldb/source/API/SBType.cpp +++ b/lldb/source/API/SBType.cpp @@ -7,16 +7,21 @@ //===----------------------------------------------------------------------===// #include "lldb/API/SBType.h" +#include "Utils.h" #include "lldb/API/SBDefines.h" #include "lldb/API/SBModule.h" #include "lldb/API/SBStream.h" #include "lldb/API/SBTypeEnumMember.h" #include "lldb/Core/Mangled.h" +#include "lldb/Core/ValueObjectConstResult.h" +#include "lldb/Symbol/CompilerDecl.h" #include "lldb/Symbol/CompilerType.h" #include "lldb/Symbol/Type.h" #include "lldb/Symbol/TypeSystem.h" #include "lldb/Utility/ConstString.h" +#include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Instrumentation.h" +#include "lldb/Utility/Scalar.h" #include "lldb/Utility/Stream.h" #include "llvm/ADT/APSInt.h" @@ -325,6 +330,79 @@ lldb::SBTypeMemberFunction SBType::GetMemberFunctionAtIndex(uint32_t idx) { return sb_func_type; } +SBTypeStaticField::SBTypeStaticField() { LLDB_INSTRUMENT_VA(this); } + +SBTypeStaticField::SBTypeStaticField(lldb_private::CompilerDecl decl) + : m_opaque_up(decl ? std::make_unique(decl) : nullptr) {} + +SBTypeStaticField::SBTypeStaticField(const SBTypeStaticField &rhs) { + LLDB_INSTRUMENT_VA(this, rhs); + + m_opaque_up = clone(rhs.m_opaque_up); +} + +SBTypeStaticField &SBTypeStaticField::operator=(const SBTypeStaticField &rhs) { + LLDB_INSTRUMENT_VA(this, rhs); + + m_opaque_up = clone(rhs.m_opaque_up); + return *this; +} + +SBTypeStaticField::~SBTypeStaticField() { LLDB_INSTRUMENT_VA(this); } + +SBTypeStaticField::operator bool() const { + LLDB_INSTRUMENT_VA(this); + + return IsValid(); +} + +bool SBTypeStaticField::IsValid() const { + LLDB_INSTRUMENT_VA(this); + + return m_opaque_up != nullptr; +} + +const char *SBTypeStaticField::GetName() { + LLDB_INSTRUMENT_VA(this); + + if (!IsValid()) + return ""; + return m_opaque_up->GetName().GetCString(); +} + +const char *SBTypeStaticField::GetMangledName() { + LLDB_INSTRUMENT_VA(this); + + if (!IsValid()) + return ""; + return m_opaque_up->GetMangledName().GetCString(); +} + +SBType SBTypeStaticField::GetType() { + LLDB_INSTRUMENT_VA(this); + + if (!IsValid()) + return SBType(); + return SBType(m_opaque_up->GetType()); +} + +SBValue SBTypeStaticField::GetConstantValue(lldb::SBTarget target) { + LLDB_INSTRUMENT_VA(this, target); + + if (!IsValid()) + return SBValue(); + + Scalar value = m_opaque_up->GetConstantValue(); + if (!value.IsValid()) + return SBValue(); + DataExtractor data; + value.GetData(data); + auto value_obj_sp = ValueObjectConstResult::Create( + target.GetSP().get(), m_opaque_up->GetType(), m_opaque_up->GetName(), + data); + return SBValue(std::move(value_obj_sp)); +} + lldb::SBType SBType::GetUnqualifiedType() { LLDB_INSTRUMENT_VA(this); @@ -438,6 +516,16 @@ SBTypeMember SBType::GetVirtualBaseClassAtIndex(uint32_t idx) { return sb_type_member; } +SBTypeStaticField SBType::GetStaticFieldWithName(const char *name) { + LLDB_INSTRUMENT_VA(this, name); + + if (!IsValid() || !name) + return SBTypeStaticField(); + + return SBTypeStaticField(m_opaque_sp->GetCompilerType(/*prefer_dynamic=*/true) + .GetStaticFieldWithName(name)); +} + SBTypeEnumMemberList SBType::GetEnumMembers() { LLDB_INSTRUMENT_VA(this); diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp index 96909425dedd..5da94adb771f 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp @@ -9,6 +9,7 @@ #include "TypeSystemClang.h" #include "clang/AST/DeclBase.h" +#include "clang/AST/ExprCXX.h" #include "llvm/Support/Casting.h" #include "llvm/Support/FormatAdapters.h" #include "llvm/Support/FormatVariadic.h" @@ -1148,6 +1149,8 @@ CompilerType TypeSystemClang::GetTypeForDecl(clang::NamedDecl *decl) { return GetTypeForDecl(interface_decl); if (clang::TagDecl *tag_decl = llvm::dyn_cast(decl)) return GetTypeForDecl(tag_decl); + if (clang::ValueDecl *value_decl = llvm::dyn_cast(decl)) + return GetTypeForDecl(value_decl); return CompilerType(); } @@ -1159,6 +1162,10 @@ CompilerType TypeSystemClang::GetTypeForDecl(ObjCInterfaceDecl *decl) { return GetType(getASTContext().getObjCInterfaceType(decl)); } +CompilerType TypeSystemClang::GetTypeForDecl(clang::ValueDecl *value_decl) { + return GetType(value_decl->getType()); +} + #pragma mark Structure, Unions, Classes void TypeSystemClang::SetOwningModule(clang::Decl *decl, @@ -5906,6 +5913,36 @@ CompilerType TypeSystemClang::GetVirtualBaseClassAtIndex( return CompilerType(); } +CompilerDecl +TypeSystemClang::GetStaticFieldWithName(lldb::opaque_compiler_type_t type, + llvm::StringRef name) { + clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type)); + switch (qual_type->getTypeClass()) { + case clang::Type::Record: { + if (!GetCompleteType(type)) + return CompilerDecl(); + + const clang::RecordType *record_type = + llvm::cast(qual_type.getTypePtr()); + const clang::RecordDecl *record_decl = record_type->getDecl(); + + clang::DeclarationName decl_name(&getASTContext().Idents.get(name)); + for (NamedDecl *decl : record_decl->lookup(decl_name)) { + auto *var_decl = dyn_cast(decl); + if (!var_decl || var_decl->getStorageClass() != clang::SC_Static) + continue; + + return CompilerDecl(this, var_decl); + } + break; + } + + default: + break; + } + return CompilerDecl(); +} + // If a pointer to a pointee type (the clang_type arg) says that it has no // children, then we either need to trust it, or override it and return a // different result. For example, an "int *" has one child that is an integer, @@ -9087,6 +9124,21 @@ CompilerType TypeSystemClang::DeclGetFunctionArgumentType(void *opaque_decl, return CompilerType(); } +Scalar TypeSystemClang::DeclGetConstantValue(void *opaque_decl) { + clang::Decl *decl = static_cast(opaque_decl); + clang::VarDecl *var_decl = llvm::dyn_cast(decl); + if (!var_decl) + return Scalar(); + clang::Expr *init_expr = var_decl->getInit(); + if (!init_expr) + return Scalar(); + std::optional value = + init_expr->getIntegerConstantExpr(getASTContext()); + if (!value) + return Scalar(); + return Scalar(*value); +} + // CompilerDeclContext functions std::vector TypeSystemClang::DeclContextFindDeclByName( diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h index 68b82e9688f1..62f14df7638d 100644 --- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h +++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h @@ -22,6 +22,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/ASTFwd.h" +#include "clang/AST/Decl.h" #include "clang/AST/TemplateBase.h" #include "clang/AST/Type.h" #include "clang/Basic/TargetInfo.h" @@ -251,6 +252,8 @@ public: CompilerType GetTypeForDecl(clang::ObjCInterfaceDecl *objc_decl); + CompilerType GetTypeForDecl(clang::ValueDecl *value_decl); + template CompilerType GetTypeForIdentifier(llvm::StringRef type_name, @@ -559,6 +562,8 @@ public: std::vector DeclGetCompilerContext(void *opaque_decl) override; + Scalar DeclGetConstantValue(void *opaque_decl) override; + CompilerType GetTypeForDecl(void *opaque_decl) override; // CompilerDeclContext override functions @@ -868,6 +873,9 @@ public: size_t idx, uint32_t *bit_offset_ptr) override; + CompilerDecl GetStaticFieldWithName(lldb::opaque_compiler_type_t type, + llvm::StringRef name) override; + static uint32_t GetNumPointeeChildren(clang::QualType type); CompilerType GetChildCompilerTypeAtIndex( diff --git a/lldb/source/Symbol/CompilerDecl.cpp b/lldb/source/Symbol/CompilerDecl.cpp index 0eb630e5b9e1..5fa0a32f041a 100644 --- a/lldb/source/Symbol/CompilerDecl.cpp +++ b/lldb/source/Symbol/CompilerDecl.cpp @@ -9,6 +9,7 @@ #include "lldb/Symbol/CompilerDecl.h" #include "lldb/Symbol/CompilerDeclContext.h" #include "lldb/Symbol/TypeSystem.h" +#include "lldb/Utility/Scalar.h" using namespace lldb_private; @@ -24,6 +25,10 @@ CompilerDeclContext CompilerDecl::GetDeclContext() const { return m_type_system->DeclGetDeclContext(m_opaque_decl); } +CompilerType CompilerDecl::GetType() const { + return m_type_system->GetTypeForDecl(m_opaque_decl); +} + CompilerType CompilerDecl::GetFunctionReturnType() const { return m_type_system->DeclGetFunctionReturnType(m_opaque_decl); } @@ -52,3 +57,7 @@ std::vector CompilerDecl::GetCompilerContext() const { return m_type_system->DeclGetCompilerContext(m_opaque_decl); } + +Scalar CompilerDecl::GetConstantValue() const { + return m_type_system->DeclGetConstantValue(m_opaque_decl); +} diff --git a/lldb/source/Symbol/CompilerType.cpp b/lldb/source/Symbol/CompilerType.cpp index 96e74b890d2d..9523fb6ea77c 100644 --- a/lldb/source/Symbol/CompilerType.cpp +++ b/lldb/source/Symbol/CompilerType.cpp @@ -849,6 +849,12 @@ CompilerType::GetVirtualBaseClassAtIndex(size_t idx, return CompilerType(); } +CompilerDecl CompilerType::GetStaticFieldWithName(llvm::StringRef name) const { + if (IsValid()) + return GetTypeSystem()->GetStaticFieldWithName(m_type, name); + return CompilerDecl(); +} + uint32_t CompilerType::GetIndexOfFieldWithName( const char *name, CompilerType *field_compiler_type_ptr, uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr, diff --git a/lldb/test/API/python_api/type/TestTypeList.py b/lldb/test/API/python_api/type/TestTypeList.py index c647c2bcdccb..81c44f7a39d6 100644 --- a/lldb/test/API/python_api/type/TestTypeList.py +++ b/lldb/test/API/python_api/type/TestTypeList.py @@ -33,6 +33,32 @@ class TypeAndTypeListTestCase(TestBase): self.assertTrue(pointer_masks2_type) self.DebugSBType(pointer_masks2_type) + def _find_static_field_in_Task_pointer(self, task_pointer): + self.assertTrue(task_pointer) + self.DebugSBType(task_pointer) + + task_type = task_pointer.GetPointeeType() + self.assertTrue(task_type) + self.DebugSBType(task_type) + + static_constexpr_field = task_type.GetStaticFieldWithName( + "static_constexpr_field" + ) + self.assertTrue(static_constexpr_field) + self.assertEqual(static_constexpr_field.GetName(), "static_constexpr_field") + self.assertEqual(static_constexpr_field.GetType().GetName(), "const long") + + value = static_constexpr_field.GetConstantValue(self.target()) + self.DebugSBValue(value) + self.assertEqual(value.GetValueAsSigned(), 47) + + static_mutable_field = task_type.GetStaticFieldWithName("static_mutable_field") + self.assertTrue(static_mutable_field) + self.assertEqual(static_mutable_field.GetName(), "static_mutable_field") + self.assertEqual(static_mutable_field.GetType().GetName(), "int") + + self.assertFalse(static_mutable_field.GetConstantValue(self.target())) + @skipIf(compiler="clang", compiler_version=["<", "17.0"]) def test(self): """Exercise SBType and SBTypeList API.""" @@ -175,6 +201,13 @@ class TypeAndTypeListTestCase(TestBase): frame0.EvaluateExpression("pointer").GetType() ) + self._find_static_field_in_Task_pointer( + frame0.FindVariable("task_head").GetType() + ) + self._find_static_field_in_Task_pointer( + frame0.EvaluateExpression("task_head").GetType() + ) + # We'll now get the child member 'id' from 'task_head'. id = task_head.GetChildMemberWithName("id") self.DebugSBValue(id) diff --git a/lldb/test/API/python_api/type/main.cpp b/lldb/test/API/python_api/type/main.cpp index 391f58e3e587..c86644d91827 100644 --- a/lldb/test/API/python_api/type/main.cpp +++ b/lldb/test/API/python_api/type/main.cpp @@ -27,12 +27,15 @@ public: enum E : unsigned char {} e; union U { } u; + static constexpr long static_constexpr_field = 47; + static int static_mutable_field; Task(int i, Task *n): id(i), next(n), type(TASK_TYPE_1) {} }; +int Task::static_mutable_field = 42; template struct PointerInfo { enum Masks1 { pointer_mask };