[lldb] Add SBType::GetEnumerationIntegerType method
authorAndy Yankovsky <weratt@gmail.com>
Tue, 22 Dec 2020 18:07:44 +0000 (10:07 -0800)
committerJonas Devlieghere <jonas@devlieghere.com>
Tue, 22 Dec 2020 18:08:22 +0000 (10:08 -0800)
Add a method for getting the enumeration underlying type.

Differential revision: https://reviews.llvm.org/D93696

lldb/bindings/interface/SBType.i
lldb/include/lldb/API/SBType.h
lldb/include/lldb/Symbol/CompilerType.h
lldb/include/lldb/Symbol/TypeSystem.h
lldb/source/API/SBType.cpp
lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
lldb/source/Symbol/CompilerType.cpp
lldb/test/API/python_api/type/TestTypeList.py
lldb/test/API/python_api/type/main.cpp

index b65eddb..2d9a4a4 100644 (file)
@@ -245,6 +245,9 @@ public:
     GetCanonicalType();
 
     lldb::SBType
+    GetEnumerationIntegerType();
+
+    lldb::SBType
     GetArrayElementType ();
 
     lldb::SBType
index 9ac385c..529b4d0 100644 (file)
@@ -152,6 +152,9 @@ public:
   lldb::SBType GetVectorElementType();
 
   lldb::SBType GetCanonicalType();
+
+  lldb::SBType GetEnumerationIntegerType();
+
   // Get the "lldb::BasicType" enumeration for a type. If a type is not a basic
   // type eBasicTypeInvalid will be returned
   lldb::BasicType GetBasicType();
index 1e0f520..5a0e8e5 100644 (file)
@@ -187,6 +187,8 @@ public:
 
   CompilerType GetFullyUnqualifiedType() const;
 
+  CompilerType GetEnumerationIntegerType() const;
+
   /// Returns -1 if this isn't a function of if the function doesn't
   /// have a prototype Returns a value >= 0 if there is a prototype.
   int GetFunctionArgumentCount() const;
index b8393b9..1fad8f6 100644 (file)
@@ -227,6 +227,9 @@ public:
 
   virtual CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) = 0;
 
+  virtual CompilerType
+  GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) = 0;
+
   // Returns -1 if this isn't a function of if the function doesn't have a
   // prototype Returns a value >= 0 if there is a prototype.
   virtual int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) = 0;
index 7d8d4cf..550c4b0 100644 (file)
@@ -344,6 +344,16 @@ lldb::SBType SBType::GetCanonicalType() {
   return LLDB_RECORD_RESULT(SBType());
 }
 
+SBType SBType::GetEnumerationIntegerType() {
+  LLDB_RECORD_METHOD_NO_ARGS(lldb::SBType, SBType, GetEnumerationIntegerType);
+
+  if (IsValid()) {
+    return LLDB_RECORD_RESULT(
+        SBType(m_opaque_sp->GetCompilerType(true).GetEnumerationIntegerType()));
+  }
+  return LLDB_RECORD_RESULT(SBType());
+}
+
 lldb::BasicType SBType::GetBasicType() {
   LLDB_RECORD_METHOD_NO_ARGS(lldb::BasicType, SBType, GetBasicType);
 
@@ -952,6 +962,7 @@ void RegisterMethods<SBType>(Registry &R) {
                        GetMemberFunctionAtIndex, (uint32_t));
   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetUnqualifiedType, ());
   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetCanonicalType, ());
+  LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetEnumerationIntegerType, ());
   LLDB_REGISTER_METHOD(lldb::BasicType, SBType, GetBasicType, ());
   LLDB_REGISTER_METHOD(lldb::SBType, SBType, GetBasicType, (lldb::BasicType));
   LLDB_REGISTER_METHOD(uint32_t, SBType, GetNumberOfDirectBaseClasses, ());
index d1a9e93..4f55cf7 100644 (file)
@@ -4195,6 +4195,13 @@ TypeSystemClang::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
   return CompilerType();
 }
 
+CompilerType
+TypeSystemClang::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
+  if (type)
+    return GetEnumerationIntegerType(GetType(GetCanonicalQualType(type)));
+  return CompilerType();
+}
+
 int TypeSystemClang::GetFunctionArgumentCount(
     lldb::opaque_compiler_type_t type) {
   if (type) {
index 7b16579..d24c595 100644 (file)
@@ -678,6 +678,9 @@ public:
   CompilerType
   GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) override;
 
+  CompilerType
+  GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) override;
+
   // Returns -1 if this isn't a function of if the function doesn't have a
   // prototype Returns a value >= 0 if there is a prototype.
   int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) override;
index 2c5910a..4f0c3b3 100644 (file)
@@ -350,6 +350,12 @@ CompilerType CompilerType::GetFullyUnqualifiedType() const {
   return CompilerType();
 }
 
+CompilerType CompilerType::GetEnumerationIntegerType() const {
+  if (IsValid())
+    return m_type_system->GetEnumerationIntegerType(m_type);
+  return CompilerType();
+}
+
 int CompilerType::GetFunctionArgumentCount() const {
   if (IsValid()) {
     return m_type_system->GetFunctionArgumentCount(m_type);
index 6ed6ca4..ff560de 100644 (file)
@@ -150,7 +150,20 @@ class TypeAndTypeListTestCase(TestBase):
         self.assertTrue(enum_type)
         self.DebugSBType(enum_type)
         self.assertFalse(enum_type.IsScopedEnumerationType())
+
         scoped_enum_type = target.FindFirstType('ScopedEnumType')
         self.assertTrue(scoped_enum_type)
         self.DebugSBType(scoped_enum_type)
         self.assertTrue(scoped_enum_type.IsScopedEnumerationType())
+        int_scoped_enum_type = scoped_enum_type.GetEnumerationIntegerType()
+        self.assertTrue(int_scoped_enum_type)
+        self.DebugSBType(int_scoped_enum_type)
+        self.assertEquals(int_scoped_enum_type.GetName(), 'int')
+
+        enum_uchar = target.FindFirstType('EnumUChar')
+        self.assertTrue(enum_uchar)
+        self.DebugSBType(enum_uchar)
+        int_enum_uchar = enum_uchar.GetEnumerationIntegerType()
+        self.assertTrue(int_enum_uchar)
+        self.DebugSBType(int_enum_uchar)
+        self.assertEquals(int_enum_uchar.GetName(), 'unsigned char')
index 5b96f47..b1ef625 100644 (file)
@@ -31,6 +31,7 @@ public:
 
 enum EnumType {};
 enum class ScopedEnumType {};
+enum class EnumUChar : unsigned char {};
 
 int main (int argc, char const *argv[])
 {
@@ -63,6 +64,7 @@ int main (int argc, char const *argv[])
 
     EnumType enum_type;
     ScopedEnumType scoped_enum_type;
+    EnumUChar scoped_enum_type_uchar;
 
     return 0; // Break at this line
 }