[Lldb-commits] [lldb] a715b1b - [lldb] Don't crash when printing static enum members with bool as underlying type

Arthur Eubanks via lldb-commits lldb-commits at lists.llvm.org
Tue Nov 1 09:29:32 PDT 2022


Author: Arthur Eubanks
Date: 2022-11-01T09:28:54-07:00
New Revision: a715b1bde91077b313b9c36d9ecbc0c83a0edac2

URL: https://github.com/llvm/llvm-project/commit/a715b1bde91077b313b9c36d9ecbc0c83a0edac2
DIFF: https://github.com/llvm/llvm-project/commit/a715b1bde91077b313b9c36d9ecbc0c83a0edac2.diff

LOG: [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

Added: 
    

Modified: 
    lldb/include/lldb/Symbol/CompilerType.h
    lldb/include/lldb/Symbol/TypeSystem.h
    lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
    lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
    lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
    lldb/source/Symbol/CompilerType.cpp
    lldb/test/API/lang/cpp/const_static_integral_member/TestConstStaticIntegralMember.py
    lldb/test/API/lang/cpp/const_static_integral_member/main.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Symbol/CompilerType.h b/lldb/include/lldb/Symbol/CompilerType.h
index ef9f283cefec..1ad9d6ae4c40 100644
--- a/lldb/include/lldb/Symbol/CompilerType.h
+++ b/lldb/include/lldb/Symbol/CompilerType.h
@@ -121,8 +121,6 @@ class CompilerType {
 
   bool IsIntegerOrEnumerationType(bool &is_signed) const;
 
-  bool IsBooleanType() const;
-
   bool IsPolymorphicClass() const;
 
   /// \param target_type    Can pass nullptr.

diff  --git a/lldb/include/lldb/Symbol/TypeSystem.h b/lldb/include/lldb/Symbol/TypeSystem.h
index 93959f4596d6..fd31b130c4ff 100644
--- a/lldb/include/lldb/Symbol/TypeSystem.h
+++ b/lldb/include/lldb/Symbol/TypeSystem.h
@@ -178,8 +178,6 @@ class TypeSystem : public PluginInterface {
     return false;
   }
 
-  virtual bool IsBooleanType(lldb::opaque_compiler_type_t type) = 0;
-
   virtual bool IsScopedEnumerationType(lldb::opaque_compiler_type_t type) = 0;
 
   virtual bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,

diff  --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index 6e76203dcf94..037e11895b10 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -2820,12 +2820,7 @@ void DWARFASTParserClang::ParseSingleMember(
         return;
       }
 
-      if (ct.IsBooleanType())
-        TypeSystemClang::SetBoolInitializerForVariable(
-            v, !const_value_or_err->isZero());
-      else
-        TypeSystemClang::SetIntegerInitializerForVariable(v,
-                                                          *const_value_or_err);
+      TypeSystemClang::SetIntegerInitializerForVariable(v, *const_value_or_err);
     }
     return;
   }

diff  --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index 4fa3c65be739..5175ad81606d 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -3226,20 +3226,6 @@ bool TypeSystemClang::IsIntegerType(lldb::opaque_compiler_type_t type,
   return false;
 }
 
-bool TypeSystemClang::IsBooleanType(lldb::opaque_compiler_type_t type) {
-  if (!type)
-    return false;
-
-  clang::QualType qual_type(GetCanonicalQualType(type));
-  const clang::BuiltinType *builtin_type =
-      llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
-
-  if (!builtin_type)
-    return false;
-
-  return builtin_type->isBooleanType();
-}
-
 bool TypeSystemClang::IsEnumerationType(lldb::opaque_compiler_type_t type,
                                         bool &is_signed) {
   if (type) {
@@ -7593,18 +7579,6 @@ clang::VarDecl *TypeSystemClang::AddVariableToRecordType(
   return var_decl;
 }
 
-void TypeSystemClang::SetBoolInitializerForVariable(VarDecl *var, bool value) {
-  assert(!var->hasInit() && "variable already initialized");
-
-  QualType qt = var->getType();
-  assert(qt->isSpecificBuiltinType(BuiltinType::Bool) &&
-         "only boolean supported");
-
-  clang::ASTContext &ast = var->getASTContext();
-  var->setInit(CXXBoolLiteralExpr::Create(ast, value, qt.getUnqualifiedType(),
-                                          SourceLocation()));
-}
-
 void TypeSystemClang::SetIntegerInitializerForVariable(
     VarDecl *var, const llvm::APInt &init_value) {
   assert(!var->hasInit() && "variable already initialized");
@@ -7619,8 +7593,15 @@ void TypeSystemClang::SetIntegerInitializerForVariable(
     const EnumDecl *enum_decl = enum_type->getDecl();
     qt = enum_decl->getIntegerType();
   }
-  var->setInit(IntegerLiteral::Create(ast, init_value, qt.getUnqualifiedType(),
-                                      SourceLocation()));
+  // Bools are handled separately because the clang AST printer handles bools
+  // separately from other integral types.
+  if (qt->isSpecificBuiltinType(BuiltinType::Bool)) {
+    var->setInit(CXXBoolLiteralExpr::Create(
+        ast, !init_value.isZero(), qt.getUnqualifiedType(), SourceLocation()));
+  } else {
+    var->setInit(IntegerLiteral::Create(
+        ast, init_value, qt.getUnqualifiedType(), SourceLocation()));
+  }
 }
 
 void TypeSystemClang::SetFloatingInitializerForVariable(

diff  --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
index 291bb5d58143..a56c5f27850c 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
@@ -592,8 +592,6 @@ class TypeSystemClang : public TypeSystem {
   bool IsEnumerationType(lldb::opaque_compiler_type_t type,
                          bool &is_signed) override;
 
-  bool IsBooleanType(lldb::opaque_compiler_type_t type) override;
-
   bool IsScopedEnumerationType(lldb::opaque_compiler_type_t type) override;
 
   static bool IsObjCClassType(const CompilerType &type);
@@ -863,8 +861,6 @@ class TypeSystemClang : public TypeSystem {
   static void SetIntegerInitializerForVariable(clang::VarDecl *var,
                                                const llvm::APInt &init_value);
 
-  static void SetBoolInitializerForVariable(clang::VarDecl *var, bool value);
-
   /// Initializes a variable with a floating point value.
   /// \param var The variable to initialize. Must not already have an
   ///            initializer and must have a floating point type.

diff  --git a/lldb/source/Symbol/CompilerType.cpp b/lldb/source/Symbol/CompilerType.cpp
index 1b02895fd390..1bf2e7d189bb 100644
--- a/lldb/source/Symbol/CompilerType.cpp
+++ b/lldb/source/Symbol/CompilerType.cpp
@@ -154,12 +154,6 @@ bool CompilerType::IsIntegerOrEnumerationType(bool &is_signed) const {
   return IsIntegerType(is_signed) || IsEnumerationType(is_signed);
 }
 
-bool CompilerType::IsBooleanType() const {
-  if (IsValid())
-    return m_type_system->IsBooleanType(m_type);
-  return false;
-}
-
 bool CompilerType::IsPointerType(CompilerType *pointee_type) const {
   if (IsValid()) {
     return m_type_system->IsPointerType(m_type, pointee_type);

diff  --git a/lldb/test/API/lang/cpp/const_static_integral_member/TestConstStaticIntegralMember.py b/lldb/test/API/lang/cpp/const_static_integral_member/TestConstStaticIntegralMember.py
index ed7cd2514ae8..7fccbf8c43f5 100644
--- a/lldb/test/API/lang/cpp/const_static_integral_member/TestConstStaticIntegralMember.py
+++ b/lldb/test/API/lang/cpp/const_static_integral_member/TestConstStaticIntegralMember.py
@@ -57,6 +57,8 @@ def test(self):
 
         # Test an unscoped enum.
         self.expect_expr("A::enum_val", result_value="enum_case2")
+        # Test an unscoped enum with bool as the underlying type.
+        self.expect_expr("A::enum_bool_val", result_value="enum_bool_case2")
 
         # Test a scoped enum.
         self.expect_expr("A::scoped_enum_val", result_value="scoped_enum_case2")

diff  --git a/lldb/test/API/lang/cpp/const_static_integral_member/main.cpp b/lldb/test/API/lang/cpp/const_static_integral_member/main.cpp
index 4cd4933275ae..4128a4d05023 100644
--- a/lldb/test/API/lang/cpp/const_static_integral_member/main.cpp
+++ b/lldb/test/API/lang/cpp/const_static_integral_member/main.cpp
@@ -5,6 +5,11 @@ enum Enum {
   enum_case2 = 2,
 };
 
+enum EnumBool : bool {
+  enum_bool_case1 = false,
+  enum_bool_case2 = true,
+};
+
 enum class ScopedEnum {
   scoped_enum_case1 = 1,
   scoped_enum_case2 = 2,
@@ -51,6 +56,7 @@ struct A {
   const static auto wchar_min = std::numeric_limits<wchar_t>::min();
 
   const static Enum enum_val = enum_case2;
+  const static EnumBool enum_bool_val = enum_bool_case2;
   const static ScopedEnum scoped_enum_val = ScopedEnum::scoped_enum_case2;
   const static ScopedEnum not_enumerator_scoped_enum_val = static_cast<ScopedEnum>(5);
   const static ScopedEnum not_enumerator_scoped_enum_val_2 =


        


More information about the lldb-commits mailing list