mirror of
https://github.com/intel/llvm.git
synced 2026-01-14 11:57:39 +08:00
[lldb] Add SB API to access static constexpr member values (#89730)
The main change is the addition of a new SBTypeStaticField class, representing a static member of a class. It can be retrieved created through SBType::GetStaticFieldWithName. It contains several methods (GetName, GetMangledName, etc.) whose meaning is hopefully obvious. The most interesting method is lldb::SBValue GetConstantValue(lldb::SBTarget) which returns a the value of the field -- if it is a compile time constant. The reason for that is that only constants have their values represented in the clang AST. For non-constants, we need to go back to the module containing that constant, and ask retrieve the associated ValueObjectVariable. That's easy enough if the we are still in the type system of the module (because then the type system will contain the pointer to the module symbol file), but it's hard when the type has been copied into another AST (e.g. during expression evaluation). To do that we would need to walk the ast import chain backwards to find the source TypeSystem, and I haven't found a nice way to do that. Another possibility would be to use the mangled name of the variable to perform a lookup (in all modules). That is sort of what happens when evaluating the variable in an expression (which does work), but I did not want to commit to that implementation as it's not necessary for my use case (and if anyone wants to, he can use the GetMangledName function and perform the lookup manually). The patch adds a couple of new TypeSystem functions to surface the information needed to implement this functionality.
This commit is contained in:
@@ -954,6 +954,7 @@ protected:
|
||||
friend class SBSection;
|
||||
friend class SBSourceManager;
|
||||
friend class SBSymbol;
|
||||
friend class SBTypeStaticField;
|
||||
friend class SBValue;
|
||||
friend class SBVariablesOptions;
|
||||
|
||||
|
||||
@@ -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<lldb_private::CompilerDecl> 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;
|
||||
|
||||
@@ -426,6 +426,7 @@ protected:
|
||||
friend class SBModule;
|
||||
friend class SBTarget;
|
||||
friend class SBThread;
|
||||
friend class SBTypeStaticField;
|
||||
friend class SBTypeSummary;
|
||||
friend class SBValueList;
|
||||
|
||||
|
||||
@@ -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<lldb_private::CompilerContext> 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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<lldb_private::CompilerContext>
|
||||
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,
|
||||
|
||||
@@ -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<CompilerDecl>(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);
|
||||
|
||||
|
||||
@@ -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<clang::TagDecl>(decl))
|
||||
return GetTypeForDecl(tag_decl);
|
||||
if (clang::ValueDecl *value_decl = llvm::dyn_cast<clang::ValueDecl>(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<clang::RecordType>(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<clang::VarDecl>(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<clang::Decl *>(opaque_decl);
|
||||
clang::VarDecl *var_decl = llvm::dyn_cast<clang::VarDecl>(decl);
|
||||
if (!var_decl)
|
||||
return Scalar();
|
||||
clang::Expr *init_expr = var_decl->getInit();
|
||||
if (!init_expr)
|
||||
return Scalar();
|
||||
std::optional<llvm::APSInt> value =
|
||||
init_expr->getIntegerConstantExpr(getASTContext());
|
||||
if (!value)
|
||||
return Scalar();
|
||||
return Scalar(*value);
|
||||
}
|
||||
|
||||
// CompilerDeclContext functions
|
||||
|
||||
std::vector<CompilerDecl> TypeSystemClang::DeclContextFindDeclByName(
|
||||
|
||||
@@ -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 <typename RecordDeclType>
|
||||
CompilerType
|
||||
GetTypeForIdentifier(llvm::StringRef type_name,
|
||||
@@ -559,6 +562,8 @@ public:
|
||||
std::vector<lldb_private::CompilerContext>
|
||||
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(
|
||||
|
||||
@@ -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<lldb_private::CompilerContext>
|
||||
CompilerDecl::GetCompilerContext() const {
|
||||
return m_type_system->DeclGetCompilerContext(m_opaque_decl);
|
||||
}
|
||||
|
||||
Scalar CompilerDecl::GetConstantValue() const {
|
||||
return m_type_system->DeclGetConstantValue(m_opaque_decl);
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 <unsigned Value> struct PointerInfo {
|
||||
enum Masks1 { pointer_mask };
|
||||
|
||||
Reference in New Issue
Block a user