Skip to content

Commit 2a923d7

Browse files
committed
[lldb][SBType] GetBasicType to unwrap canonical type (llvm#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 (cherry picked from commit b7889a6)
1 parent 367909f commit 2a923d7

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
@@ -5701,7 +5701,7 @@ CompilerType TypeSystemClang::GetBuiltinTypeByName(ConstString name) {
57015701
lldb::BasicType
57025702
TypeSystemClang::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
57035703
if (type) {
5704-
clang::QualType qual_type(GetQualType(type));
5704+
clang::QualType qual_type(GetCanonicalQualType(type));
57055705
const clang::Type::TypeClass type_class = qual_type->getTypeClass();
57065706
if (type_class == clang::Type::Builtin) {
57075707
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
@@ -370,12 +370,11 @@ bool CompilerType::IsSigned() const {
370370
}
371371

372372
bool CompilerType::IsNullPtrType() const {
373-
return GetCanonicalType().GetBasicTypeEnumeration() ==
374-
lldb::eBasicTypeNullPtr;
373+
return GetBasicTypeEnumeration() == lldb::eBasicTypeNullPtr;
375374
}
376375

377376
bool CompilerType::IsBoolean() const {
378-
return GetCanonicalType().GetBasicTypeEnumeration() == lldb::eBasicTypeBool;
377+
return GetBasicTypeEnumeration() == lldb::eBasicTypeBool;
379378
}
380379

381380
bool CompilerType::IsEnumerationIntegerTypeSigned() const {
@@ -395,7 +394,7 @@ bool CompilerType::IsPromotableIntegerType() const {
395394
if (IsUnscopedEnumerationType())
396395
return true;
397396

398-
switch (GetCanonicalType().GetBasicTypeEnumeration()) {
397+
switch (GetBasicTypeEnumeration()) {
399398
case lldb::eBasicTypeBool:
400399
case lldb::eBasicTypeChar:
401400
case lldb::eBasicTypeSignedChar:
@@ -471,8 +470,7 @@ bool CompilerType::IsContextuallyConvertibleToBool() const {
471470
}
472471

473472
bool CompilerType::IsBasicType() const {
474-
return GetCanonicalType().GetBasicTypeEnumeration() !=
475-
lldb::eBasicTypeInvalid;
473+
return GetBasicTypeEnumeration() != lldb::eBasicTypeInvalid;
476474
}
477475

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

lldb/source/ValueObject/ValueObject.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3389,8 +3389,8 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) {
33893389
llvm::APSInt ext =
33903390
int_value_or_err->extOrTrunc(type_byte_size * CHAR_BIT);
33913391
Scalar scalar_int(ext);
3392-
llvm::APFloat f = scalar_int.CreateAPFloatFromAPSInt(
3393-
type.GetCanonicalType().GetBasicTypeEnumeration());
3392+
llvm::APFloat f =
3393+
scalar_int.CreateAPFloatFromAPSInt(type.GetBasicTypeEnumeration());
33943394
return ValueObject::CreateValueObjectFromAPFloat(target, f, type,
33953395
"result");
33963396
} else {
@@ -3405,7 +3405,7 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) {
34053405
if (int_value_or_err) {
34063406
Scalar scalar_int(*int_value_or_err);
34073407
llvm::APFloat f = scalar_int.CreateAPFloatFromAPSInt(
3408-
type.GetCanonicalType().GetBasicTypeEnumeration());
3408+
type.GetBasicTypeEnumeration());
34093409
return ValueObject::CreateValueObjectFromAPFloat(target, f, type,
34103410
"result");
34113411
} else {
@@ -3420,7 +3420,7 @@ lldb::ValueObjectSP ValueObject::CastToBasicType(CompilerType type) {
34203420
if (float_value_or_err) {
34213421
Scalar scalar_float(*float_value_or_err);
34223422
llvm::APFloat f = scalar_float.CreateAPFloatFromAPFloat(
3423-
type.GetCanonicalType().GetBasicTypeEnumeration());
3423+
type.GetBasicTypeEnumeration());
34243424
return ValueObject::CreateValueObjectFromAPFloat(target, f, type,
34253425
"result");
34263426
} 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)