Skip to content

Commit 014d2b9

Browse files
authored
[lldb][TypeSystem] Call Type::isIntegralType from TypeSystemClang::IsIntegerType (#175669)
Instead of re-implementing `Type::isIntegralType`, call it explicitly. This means we get support for `BitIntType` out-of-the-box. We don't use `IsIntegerType` here because we want to abide by the language-specific notions of an integer type (which differ between C++ and C). The slight behaviour change here is that `IsIntegerType` will now treat complete enumerations as integers in C. This is correct according to the C standard.
1 parent 44df98e commit 014d2b9

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3290,17 +3290,18 @@ bool TypeSystemClang::IsIntegerType(lldb::opaque_compiler_type_t type,
32903290
return false;
32913291

32923292
clang::QualType qual_type(GetCanonicalQualType(type));
3293-
const clang::BuiltinType *builtin_type =
3294-
llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3293+
if (qual_type.isNull())
3294+
return false;
32953295

3296-
if (builtin_type) {
3297-
if (builtin_type->isInteger()) {
3298-
is_signed = builtin_type->isSignedInteger();
3299-
return true;
3300-
}
3301-
}
3296+
// Note, using 'isIntegralType' as opposed to 'isIntegerType' because
3297+
// the latter treats unscoped enums as integer types (which is not true
3298+
// in C++). The former accounts for this.
3299+
if (!qual_type->isIntegralType(getASTContext()))
3300+
return false;
33023301

3303-
return false;
3302+
is_signed = qual_type->isSignedIntegerType();
3303+
3304+
return true;
33043305
}
33053306

33063307
bool TypeSystemClang::IsEnumerationType(lldb::opaque_compiler_type_t type,

lldb/unittests/Symbol/TestTypeSystemClang.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ TEST_F(TestTypeSystemClang, TestIsEnumerationType) {
436436
bool is_signed;
437437
EXPECT_TRUE(enum_type.IsEnumerationType(is_signed));
438438
EXPECT_TRUE(is_signed);
439+
EXPECT_FALSE(enum_type.IsIntegerType(is_signed));
439440
}
440441

441442
// Scoped unsigned enum
@@ -449,6 +450,7 @@ TEST_F(TestTypeSystemClang, TestIsEnumerationType) {
449450
bool is_signed;
450451
EXPECT_TRUE(enum_type.IsEnumerationType(is_signed));
451452
EXPECT_FALSE(is_signed);
453+
EXPECT_FALSE(enum_type.IsIntegerType(is_signed));
452454
}
453455

454456
// Unscoped signed enum
@@ -462,6 +464,7 @@ TEST_F(TestTypeSystemClang, TestIsEnumerationType) {
462464
bool is_signed;
463465
EXPECT_TRUE(enum_type.IsEnumerationType(is_signed));
464466
EXPECT_TRUE(is_signed);
467+
EXPECT_FALSE(enum_type.IsIntegerType(is_signed));
465468
}
466469

467470
// Unscoped unsigned enum
@@ -475,6 +478,47 @@ TEST_F(TestTypeSystemClang, TestIsEnumerationType) {
475478
bool is_signed;
476479
EXPECT_TRUE(enum_type.IsEnumerationType(is_signed));
477480
EXPECT_FALSE(is_signed);
481+
EXPECT_FALSE(enum_type.IsIntegerType(is_signed));
482+
}
483+
}
484+
485+
TEST_F(TestTypeSystemClang, TestIsIntegerType_BitInt) {
486+
auto holder =
487+
std::make_unique<clang_utils::TypeSystemClangHolder>("bitint_ast");
488+
auto &ast = *holder->GetAST();
489+
490+
// Signed _BitInt
491+
{
492+
CompilerType bitint_type = ast.GetType(
493+
ast.getASTContext().getBitIntType(/*Unsigned=*/false, /*NumBits=*/37));
494+
ASSERT_TRUE(bitint_type);
495+
496+
EXPECT_TRUE(bitint_type.IsInteger());
497+
EXPECT_TRUE(bitint_type.IsSigned());
498+
499+
bool is_signed;
500+
EXPECT_TRUE(bitint_type.IsIntegerType(is_signed));
501+
EXPECT_TRUE(is_signed);
502+
503+
EXPECT_TRUE(bitint_type.IsIntegerOrEnumerationType(is_signed));
504+
EXPECT_TRUE(is_signed);
505+
}
506+
507+
// Unsigned _BitInt
508+
{
509+
CompilerType bitint_type = ast.GetType(
510+
ast.getASTContext().getBitIntType(/*Unsigned=*/true, /*NumBits=*/122));
511+
ASSERT_TRUE(bitint_type);
512+
513+
EXPECT_TRUE(bitint_type.IsInteger());
514+
EXPECT_FALSE(bitint_type.IsSigned());
515+
516+
bool is_signed;
517+
EXPECT_TRUE(bitint_type.IsIntegerType(is_signed));
518+
EXPECT_FALSE(is_signed);
519+
520+
EXPECT_TRUE(bitint_type.IsIntegerOrEnumerationType(is_signed));
521+
EXPECT_FALSE(is_signed);
478522
}
479523
}
480524

0 commit comments

Comments
 (0)