Skip to content

Commit a715b1b

Browse files
committed
[lldb] Don't crash when printing static enum members with bool as underlying type
Undoes a lot of the code added in D135169 to piggyback off of the enum logic in `TypeSystemClang::SetIntegerInitializerForVariable()`. Fixes #58383. Reviewed By: DavidSpickett Differential Revision: https://reviews.llvm.org/D137045
1 parent 0991da3 commit a715b1b

File tree

8 files changed

+18
-48
lines changed

8 files changed

+18
-48
lines changed

lldb/include/lldb/Symbol/CompilerType.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ class CompilerType {
121121

122122
bool IsIntegerOrEnumerationType(bool &is_signed) const;
123123

124-
bool IsBooleanType() const;
125-
126124
bool IsPolymorphicClass() const;
127125

128126
/// \param target_type Can pass nullptr.

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ class TypeSystem : public PluginInterface {
178178
return false;
179179
}
180180

181-
virtual bool IsBooleanType(lldb::opaque_compiler_type_t type) = 0;
182-
183181
virtual bool IsScopedEnumerationType(lldb::opaque_compiler_type_t type) = 0;
184182

185183
virtual bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,

lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,12 +2820,7 @@ void DWARFASTParserClang::ParseSingleMember(
28202820
return;
28212821
}
28222822

2823-
if (ct.IsBooleanType())
2824-
TypeSystemClang::SetBoolInitializerForVariable(
2825-
v, !const_value_or_err->isZero());
2826-
else
2827-
TypeSystemClang::SetIntegerInitializerForVariable(v,
2828-
*const_value_or_err);
2823+
TypeSystemClang::SetIntegerInitializerForVariable(v, *const_value_or_err);
28292824
}
28302825
return;
28312826
}

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

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,20 +3226,6 @@ bool TypeSystemClang::IsIntegerType(lldb::opaque_compiler_type_t type,
32263226
return false;
32273227
}
32283228

3229-
bool TypeSystemClang::IsBooleanType(lldb::opaque_compiler_type_t type) {
3230-
if (!type)
3231-
return false;
3232-
3233-
clang::QualType qual_type(GetCanonicalQualType(type));
3234-
const clang::BuiltinType *builtin_type =
3235-
llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3236-
3237-
if (!builtin_type)
3238-
return false;
3239-
3240-
return builtin_type->isBooleanType();
3241-
}
3242-
32433229
bool TypeSystemClang::IsEnumerationType(lldb::opaque_compiler_type_t type,
32443230
bool &is_signed) {
32453231
if (type) {
@@ -7593,18 +7579,6 @@ clang::VarDecl *TypeSystemClang::AddVariableToRecordType(
75937579
return var_decl;
75947580
}
75957581

7596-
void TypeSystemClang::SetBoolInitializerForVariable(VarDecl *var, bool value) {
7597-
assert(!var->hasInit() && "variable already initialized");
7598-
7599-
QualType qt = var->getType();
7600-
assert(qt->isSpecificBuiltinType(BuiltinType::Bool) &&
7601-
"only boolean supported");
7602-
7603-
clang::ASTContext &ast = var->getASTContext();
7604-
var->setInit(CXXBoolLiteralExpr::Create(ast, value, qt.getUnqualifiedType(),
7605-
SourceLocation()));
7606-
}
7607-
76087582
void TypeSystemClang::SetIntegerInitializerForVariable(
76097583
VarDecl *var, const llvm::APInt &init_value) {
76107584
assert(!var->hasInit() && "variable already initialized");
@@ -7619,8 +7593,15 @@ void TypeSystemClang::SetIntegerInitializerForVariable(
76197593
const EnumDecl *enum_decl = enum_type->getDecl();
76207594
qt = enum_decl->getIntegerType();
76217595
}
7622-
var->setInit(IntegerLiteral::Create(ast, init_value, qt.getUnqualifiedType(),
7623-
SourceLocation()));
7596+
// Bools are handled separately because the clang AST printer handles bools
7597+
// separately from other integral types.
7598+
if (qt->isSpecificBuiltinType(BuiltinType::Bool)) {
7599+
var->setInit(CXXBoolLiteralExpr::Create(
7600+
ast, !init_value.isZero(), qt.getUnqualifiedType(), SourceLocation()));
7601+
} else {
7602+
var->setInit(IntegerLiteral::Create(
7603+
ast, init_value, qt.getUnqualifiedType(), SourceLocation()));
7604+
}
76247605
}
76257606

76267607
void TypeSystemClang::SetFloatingInitializerForVariable(

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,6 @@ class TypeSystemClang : public TypeSystem {
592592
bool IsEnumerationType(lldb::opaque_compiler_type_t type,
593593
bool &is_signed) override;
594594

595-
bool IsBooleanType(lldb::opaque_compiler_type_t type) override;
596-
597595
bool IsScopedEnumerationType(lldb::opaque_compiler_type_t type) override;
598596

599597
static bool IsObjCClassType(const CompilerType &type);
@@ -863,8 +861,6 @@ class TypeSystemClang : public TypeSystem {
863861
static void SetIntegerInitializerForVariable(clang::VarDecl *var,
864862
const llvm::APInt &init_value);
865863

866-
static void SetBoolInitializerForVariable(clang::VarDecl *var, bool value);
867-
868864
/// Initializes a variable with a floating point value.
869865
/// \param var The variable to initialize. Must not already have an
870866
/// initializer and must have a floating point type.

lldb/source/Symbol/CompilerType.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,6 @@ bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed) const {
154154
return IsIntegerType(is_signed) || IsEnumerationType(is_signed);
155155
}
156156

157-
bool CompilerType::IsBooleanType() const {
158-
if (IsValid())
159-
return m_type_system->IsBooleanType(m_type);
160-
return false;
161-
}
162-
163157
bool CompilerType::IsPointerType(CompilerType *pointee_type) const {
164158
if (IsValid()) {
165159
return m_type_system->IsPointerType(m_type, pointee_type);

lldb/test/API/lang/cpp/const_static_integral_member/TestConstStaticIntegralMember.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def test(self):
5757

5858
# Test an unscoped enum.
5959
self.expect_expr("A::enum_val", result_value="enum_case2")
60+
# Test an unscoped enum with bool as the underlying type.
61+
self.expect_expr("A::enum_bool_val", result_value="enum_bool_case2")
6062

6163
# Test a scoped enum.
6264
self.expect_expr("A::scoped_enum_val", result_value="scoped_enum_case2")

lldb/test/API/lang/cpp/const_static_integral_member/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ enum Enum {
55
enum_case2 = 2,
66
};
77

8+
enum EnumBool : bool {
9+
enum_bool_case1 = false,
10+
enum_bool_case2 = true,
11+
};
12+
813
enum class ScopedEnum {
914
scoped_enum_case1 = 1,
1015
scoped_enum_case2 = 2,
@@ -51,6 +56,7 @@ struct A {
5156
const static auto wchar_min = std::numeric_limits<wchar_t>::min();
5257

5358
const static Enum enum_val = enum_case2;
59+
const static EnumBool enum_bool_val = enum_bool_case2;
5460
const static ScopedEnum scoped_enum_val = ScopedEnum::scoped_enum_case2;
5561
const static ScopedEnum not_enumerator_scoped_enum_val = static_cast<ScopedEnum>(5);
5662
const static ScopedEnum not_enumerator_scoped_enum_val_2 =

0 commit comments

Comments
 (0)