Skip to content

Commit b7889a6

Browse files
authored
[lldb][SBType] GetBasicType to unwrap canonical type (#149112)
`SBType::GetBasicType` fails on typedefs to primitive types. The docs for `GetBasicType` state: ``` Returns the BasicType value that is most appropriate to this type ``` But, e.g., for `uint64_t` this would currently return `eBasicTypeInvalid`. `TypeSystemClang::GetBasicTypeEnumeration` (which is what `SBType::GetBasicType` uses) doesn't see through typedefs. Inside LLDB we almost always call `GetBasicTypeEnumeration` on the canonical type. In the cases we don't I suspect those were just subtle bugs. This patch gets the canonical type inside of `GetBasicTypeEnumeration` instead. rdar://155829208
1 parent 0cfea5b commit b7889a6

File tree

6 files changed

+53
-11
lines changed

6 files changed

+53
-11
lines changed

lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5460,7 +5460,7 @@ CompilerType TypeSystemClang::GetBuiltinTypeByName(ConstString name) {
54605460
lldb::BasicType
54615461
TypeSystemClang::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
54625462
if (type) {
5463-
clang::QualType qual_type(GetQualType(type));
5463+
clang::QualType qual_type(GetCanonicalQualType(type));
54645464
const clang::Type::TypeClass type_class = qual_type->getTypeClass();
54655465
if (type_class == clang::Type::Builtin) {
54665466
switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {

lldb/source/Symbol/CompilerType.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,11 @@ bool CompilerType::IsSigned() const {
354354
}
355355

356356
bool CompilerType::IsNullPtrType() const {
357-
return GetCanonicalType().GetBasicTypeEnumeration() ==
358-
lldb::eBasicTypeNullPtr;
357+
return GetBasicTypeEnumeration() == lldb::eBasicTypeNullPtr;
359358
}
360359

361360
bool CompilerType::IsBoolean() const {
362-
return GetCanonicalType().GetBasicTypeEnumeration() == lldb::eBasicTypeBool;
361+
return GetBasicTypeEnumeration() == lldb::eBasicTypeBool;
363362
}
364363

365364
bool CompilerType::IsEnumerationIntegerTypeSigned() const {
@@ -379,7 +378,7 @@ bool CompilerType::IsPromotableIntegerType() const {
379378
if (IsUnscopedEnumerationType())
380379
return true;
381380

382-
switch (GetCanonicalType().GetBasicTypeEnumeration()) {
381+
switch (GetBasicTypeEnumeration()) {
383382
case lldb::eBasicTypeBool:
384383
case lldb::eBasicTypeChar:
385384
case lldb::eBasicTypeSignedChar:
@@ -455,8 +454,7 @@ bool CompilerType::IsContextuallyConvertibleToBool() const {
455454
}
456455

457456
bool CompilerType::IsBasicType() const {
458-
return GetCanonicalType().GetBasicTypeEnumeration() !=
459-
lldb::eBasicTypeInvalid;
457+
return GetBasicTypeEnumeration() != lldb::eBasicTypeInvalid;
460458
}
461459

462460
std::string CompilerType::TypeDescription() {

lldb/source/ValueObject/ValueObject.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3228,8 +3228,8 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) {
32283228
llvm::APSInt ext =
32293229
int_value_or_err->extOrTrunc(type_byte_size * CHAR_BIT);
32303230
Scalar scalar_int(ext);
3231-
llvm::APFloat f = scalar_int.CreateAPFloatFromAPSInt(
3232-
type.GetCanonicalType().GetBasicTypeEnumeration());
3231+
llvm::APFloat f =
3232+
scalar_int.CreateAPFloatFromAPSInt(type.GetBasicTypeEnumeration());
32333233
return ValueObject::CreateValueObjectFromAPFloat(target, f, type,
32343234
"result");
32353235
} else {
@@ -3245,7 +3245,7 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) {
32453245
if (int_value_or_err) {
32463246
Scalar scalar_int(*int_value_or_err);
32473247
llvm::APFloat f = scalar_int.CreateAPFloatFromAPSInt(
3248-
type.GetCanonicalType().GetBasicTypeEnumeration());
3248+
type.GetBasicTypeEnumeration());
32493249
return ValueObject::CreateValueObjectFromAPFloat(target, f, type,
32503250
"result");
32513251
} else {
@@ -3261,7 +3261,7 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) {
32613261
if (float_value_or_err) {
32623262
Scalar scalar_float(*float_value_or_err);
32633263
llvm::APFloat f = scalar_float.CreateAPFloatFromAPFloat(
3264-
type.GetCanonicalType().GetBasicTypeEnumeration());
3264+
type.GetBasicTypeEnumeration());
32653265
return ValueObject::CreateValueObjectFromAPFloat(target, f, type,
32663266
"result");
32673267
} else {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
from lldbsuite.test.lldbtest import *
4+
from lldbsuite.test import lldbutil
5+
6+
7+
class TestCase(TestBase):
8+
def test(self):
9+
"""Test that SBType.GetBasicType unwraps typedefs."""
10+
self.build()
11+
lldbutil.run_to_source_breakpoint(self, "return", lldb.SBFileSpec("main.cpp"))
12+
13+
a = self.frame().FindVariable("a")
14+
self.assertTrue(a)
15+
16+
int_basic_type = a.GetType().GetBasicType()
17+
self.assertEqual(int_basic_type, 13)
18+
19+
b = self.frame().FindVariable("b")
20+
self.assertTrue(b)
21+
22+
c = self.frame().FindVariable("c")
23+
self.assertTrue(c)
24+
25+
d = self.frame().FindVariable("d")
26+
self.assertTrue(d)
27+
28+
self.assertEqual(b.GetType().GetBasicType(), int_basic_type)
29+
self.assertEqual(c.GetType().GetBasicType(), int_basic_type)
30+
self.assertEqual(d.GetType().GetBasicType(), int_basic_type)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using T1 = int;
2+
using T2 = T1;
3+
using T3 = T2;
4+
5+
int main() {
6+
int a;
7+
T1 b;
8+
T2 c;
9+
T3 d;
10+
return 0;
11+
}

0 commit comments

Comments
 (0)