https://github.com/kuilpd updated 
https://github.com/llvm/llvm-project/pull/115005

>From 4d797371598960baf7729d05590aa1a8c7077694 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuk...@accesssoftek.com>
Date: Mon, 4 Nov 2024 14:33:45 +0500
Subject: [PATCH 1/8] [lldb] Analyze enum promotion type during parsing

---
 clang/include/clang/AST/Decl.h                |  2 +-
 .../SymbolFile/DWARF/DWARFASTParserClang.cpp  | 95 ++++++++++++++++++-
 .../TypeSystem/Clang/TypeSystemClang.cpp      | 26 +----
 3 files changed, 98 insertions(+), 25 deletions(-)

diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 16403774e72b31..41cb47516f5803 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -3903,6 +3903,7 @@ class EnumDecl : public TagDecl {
   void setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
                                     TemplateSpecializationKind TSK);
 
+public:
   /// Sets the width in bits required to store all the
   /// non-negative enumerators of this enum.
   void setNumPositiveBits(unsigned Num) {
@@ -3914,7 +3915,6 @@ class EnumDecl : public TagDecl {
   /// negative enumerators of this enum. (see getNumNegativeBits)
   void setNumNegativeBits(unsigned Num) { EnumDeclBits.NumNegativeBits = Num; }
 
-public:
   /// True if this tag declaration is a scoped enumeration. Only
   /// possible in C++11 mode.
   void setScoped(bool Scoped = true) { EnumDeclBits.IsScoped = Scoped; }
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp 
b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index e77188bfbd2e4a..bb9c35a235c1ff 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -2316,6 +2316,8 @@ size_t DWARFASTParserClang::ParseChildEnumerators(
     return 0;
 
   size_t enumerators_added = 0;
+  unsigned NumNegativeBits = 0;
+  unsigned NumPositiveBits = 0;
 
   for (DWARFDIE die : parent_die.children()) {
     const dw_tag_t tag = die.Tag();
@@ -2367,11 +2369,102 @@ size_t DWARFASTParserClang::ParseChildEnumerators(
     }
 
     if (name && name[0] && got_value) {
-      m_ast.AddEnumerationValueToEnumerationType(
+      auto ECD = m_ast.AddEnumerationValueToEnumerationType(
           clang_type, decl, name, enum_value, enumerator_byte_size * 8);
       ++enumerators_added;
+
+      llvm::APSInt InitVal = ECD->getInitVal();
+      // Keep track of the size of positive and negative values.
+      if (InitVal.isUnsigned() || InitVal.isNonNegative()) {
+        // If the enumerator is zero that should still be counted as a positive
+        // bit since we need a bit to store the value zero.
+        unsigned ActiveBits = InitVal.getActiveBits();
+        NumPositiveBits = std::max({NumPositiveBits, ActiveBits, 1u});
+      } else {
+        NumNegativeBits =
+            std::max(NumNegativeBits, (unsigned)InitVal.getSignificantBits());
+      }
     }
   }
+
+  /// The following code follows the same logic as in Sema::ActOnEnumBody
+  /// clang/lib/Sema/SemaDecl.cpp
+  // If we have an empty set of enumerators we still need one bit.
+  // From [dcl.enum]p8
+  // If the enumerator-list is empty, the values of the enumeration are as if
+  // the enumeration had a single enumerator with value 0
+  if (!NumPositiveBits && !NumNegativeBits)
+    NumPositiveBits = 1;
+
+  clang::QualType qual_type(ClangUtil::GetQualType(clang_type));
+  clang::EnumDecl *enum_decl = qual_type->getAs<clang::EnumType>()->getDecl();
+  enum_decl->setNumPositiveBits(NumPositiveBits);
+  enum_decl->setNumNegativeBits(NumNegativeBits);
+
+  // C++0x N3000 [conv.prom]p3:
+  //   An rvalue of an unscoped enumeration type whose underlying
+  //   type is not fixed can be converted to an rvalue of the first
+  //   of the following types that can represent all the values of
+  //   the enumeration: int, unsigned int, long int, unsigned long
+  //   int, long long int, or unsigned long long int.
+  // C99 6.4.4.3p2:
+  //   An identifier declared as an enumeration constant has type int.
+  // The C99 rule is modified by C23.
+  clang::QualType BestPromotionType;
+  unsigned BestWidth;
+
+  auto &Context = m_ast.getASTContext();
+  unsigned LongWidth = Context.getTargetInfo().getLongWidth();
+  unsigned IntWidth = Context.getTargetInfo().getIntWidth();
+  unsigned CharWidth = Context.getTargetInfo().getCharWidth();
+  unsigned ShortWidth = Context.getTargetInfo().getShortWidth();
+
+  bool is_cpp = Language::LanguageIsCPlusPlus(
+      SymbolFileDWARF::GetLanguage(*parent_die.GetCU()));
+
+  if (NumNegativeBits) {
+    // If there is a negative value, figure out the smallest integer type (of
+    // int/long/longlong) that fits.
+    if (NumNegativeBits <= CharWidth && NumPositiveBits < CharWidth) {
+      BestWidth = CharWidth;
+    } else if (NumNegativeBits <= ShortWidth && NumPositiveBits < ShortWidth) {
+      BestWidth = ShortWidth;
+    } else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
+      BestWidth = IntWidth;
+    } else if (NumNegativeBits <= LongWidth && NumPositiveBits < LongWidth) {
+      BestWidth = LongWidth;
+    } else {
+      BestWidth = Context.getTargetInfo().getLongLongWidth();
+    }
+    BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : qual_type);
+  } else {
+    // If there is no negative value, figure out the smallest type that fits
+    // all of the enumerator values.
+    if (NumPositiveBits <= CharWidth) {
+      BestPromotionType = Context.IntTy;
+      BestWidth = CharWidth;
+    } else if (NumPositiveBits <= ShortWidth) {
+      BestPromotionType = Context.IntTy;
+      BestWidth = ShortWidth;
+    } else if (NumPositiveBits <= IntWidth) {
+      BestWidth = IntWidth;
+      BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
+                              ? Context.UnsignedIntTy
+                              : Context.IntTy;
+    } else if (NumPositiveBits <= LongWidth) {
+      BestWidth = LongWidth;
+      BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
+                              ? Context.UnsignedLongTy
+                              : Context.LongTy;
+    } else {
+      BestWidth = Context.getTargetInfo().getLongLongWidth();
+      BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
+                              ? Context.UnsignedLongLongTy
+                              : Context.LongLongTy;
+    }
+  }
+  enum_decl->setPromotionType(BestPromotionType);
+
   return enumerators_added;
 }
 
diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp 
b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index 47051f2e68090f..94f99a8e360ee7 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -2321,7 +2321,6 @@ CompilerType TypeSystemClang::CreateEnumerationType(
   if (decl_ctx)
     decl_ctx->addDecl(enum_decl);
 
-  // TODO: check if we should be setting the promotion type too?
   enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
 
   enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
@@ -8453,30 +8452,11 @@ bool TypeSystemClang::CompleteTagDeclarationDefinition(
   if (enum_decl->isCompleteDefinition())
     return true;
 
-  clang::ASTContext &ast = lldb_ast->getASTContext();
-
-  /// TODO This really needs to be fixed.
-
   QualType integer_type(enum_decl->getIntegerType());
   if (!integer_type.isNull()) {
-    unsigned NumPositiveBits = 1;
-    unsigned NumNegativeBits = 0;
-
-    clang::QualType promotion_qual_type;
-    // If the enum integer type is less than an integer in bit width,
-    // then we must promote it to an integer size.
-    if (ast.getTypeSize(enum_decl->getIntegerType()) <
-        ast.getTypeSize(ast.IntTy)) {
-      if (enum_decl->getIntegerType()->isSignedIntegerType())
-        promotion_qual_type = ast.IntTy;
-      else
-        promotion_qual_type = ast.UnsignedIntTy;
-    } else
-      promotion_qual_type = enum_decl->getIntegerType();
-
-    enum_decl->completeDefinition(enum_decl->getIntegerType(),
-                                  promotion_qual_type, NumPositiveBits,
-                                  NumNegativeBits);
+    enum_decl->completeDefinition(
+        enum_decl->getIntegerType(), enum_decl->getPromotionType(),
+        enum_decl->getNumPositiveBits(), enum_decl->getNumNegativeBits());
   }
   return true;
 }

>From 1ba943dd5538b1d21552df151ac107451081aa17 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuk...@accesssoftek.com>
Date: Tue, 5 Nov 2024 19:18:41 +0500
Subject: [PATCH 2/8] Fix enum's underlying type retrieval

---
 .../Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp       | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp 
b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index bb9c35a235c1ff..676cc7d7603655 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -2396,8 +2396,8 @@ size_t DWARFASTParserClang::ParseChildEnumerators(
   if (!NumPositiveBits && !NumNegativeBits)
     NumPositiveBits = 1;
 
-  clang::QualType qual_type(ClangUtil::GetQualType(clang_type));
-  clang::EnumDecl *enum_decl = qual_type->getAs<clang::EnumType>()->getDecl();
+  clang::EnumDecl *enum_decl =
+      ClangUtil::GetQualType(clang_type)->getAs<clang::EnumType>()->getDecl();
   enum_decl->setNumPositiveBits(NumPositiveBits);
   enum_decl->setNumNegativeBits(NumNegativeBits);
 
@@ -2436,7 +2436,8 @@ size_t DWARFASTParserClang::ParseChildEnumerators(
     } else {
       BestWidth = Context.getTargetInfo().getLongLongWidth();
     }
-    BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : qual_type);
+    BestPromotionType =
+        BestWidth <= IntWidth ? Context.IntTy : enum_decl->getIntegerType();
   } else {
     // If there is no negative value, figure out the smallest type that fits
     // all of the enumerator values.

>From c528dd54489be9e7617d14696a6bd79da87c06e8 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuk...@accesssoftek.com>
Date: Tue, 5 Nov 2024 19:19:14 +0500
Subject: [PATCH 3/8] Add a test

---
 .../test/API/lang/cpp/enum_promotion/Makefile |  3 ++
 .../enum_promotion/TestCPPEnumPromotion.py    | 37 +++++++++++++++++++
 .../test/API/lang/cpp/enum_promotion/main.cpp | 22 +++++++++++
 3 files changed, 62 insertions(+)
 create mode 100644 lldb/test/API/lang/cpp/enum_promotion/Makefile
 create mode 100644 
lldb/test/API/lang/cpp/enum_promotion/TestCPPEnumPromotion.py
 create mode 100644 lldb/test/API/lang/cpp/enum_promotion/main.cpp

diff --git a/lldb/test/API/lang/cpp/enum_promotion/Makefile 
b/lldb/test/API/lang/cpp/enum_promotion/Makefile
new file mode 100644
index 00000000000000..99998b20bcb050
--- /dev/null
+++ b/lldb/test/API/lang/cpp/enum_promotion/Makefile
@@ -0,0 +1,3 @@
+CXX_SOURCES := main.cpp
+
+include Makefile.rules
diff --git a/lldb/test/API/lang/cpp/enum_promotion/TestCPPEnumPromotion.py 
b/lldb/test/API/lang/cpp/enum_promotion/TestCPPEnumPromotion.py
new file mode 100644
index 00000000000000..a4eed53c8d80f2
--- /dev/null
+++ b/lldb/test/API/lang/cpp/enum_promotion/TestCPPEnumPromotion.py
@@ -0,0 +1,37 @@
+"""
+Test LLDB type promotion of unscoped enums.
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+class TestCPPEnumPromotion(TestBase):
+
+    @skipIf(debug_info=no_match(["dwarf", "dwo"]))
+    def test(self):
+        self.build()
+        lldbutil.run_to_source_breakpoint(
+            self, "// break here", lldb.SBFileSpec("main.cpp")
+        )
+        UChar_promoted = self.frame().FindVariable("UChar_promoted")
+        UShort_promoted = self.frame().FindVariable("UShort_promoted")
+        UInt_promoted = self.frame().FindVariable("UInt_promoted")
+        SLong_promoted = self.frame().FindVariable("SLong_promoted")
+        ULong_promoted = self.frame().FindVariable("ULong_promoted")
+        NChar_promoted = self.frame().FindVariable("NChar_promoted")
+        NShort_promoted = self.frame().FindVariable("NShort_promoted")
+        NInt_promoted = self.frame().FindVariable("NInt_promoted")
+        NLong_promoted = self.frame().FindVariable("NLong_promoted")
+
+        # Check that LLDB's promoted type is the same as the compiler's
+        self.expect_expr("+EnumUChar::UChar", 
result_type=UChar_promoted.type.name)
+        self.expect_expr("+EnumUShort::UShort", 
result_type=UShort_promoted.type.name)
+        self.expect_expr("+EnumUInt::UInt", 
result_type=UInt_promoted.type.name)
+        self.expect_expr("+EnumSLong::SLong", 
result_type=SLong_promoted.type.name)
+        self.expect_expr("+EnumULong::ULong", 
result_type=ULong_promoted.type.name)
+        self.expect_expr("+EnumNChar::NChar", 
result_type=NChar_promoted.type.name)
+        self.expect_expr("+EnumNShort::NShort", 
result_type=NShort_promoted.type.name)
+        self.expect_expr("+EnumNInt::NInt", 
result_type=NInt_promoted.type.name)
+        self.expect_expr("+EnumNLong::NLong", 
result_type=NLong_promoted.type.name)
diff --git a/lldb/test/API/lang/cpp/enum_promotion/main.cpp 
b/lldb/test/API/lang/cpp/enum_promotion/main.cpp
new file mode 100644
index 00000000000000..fafb96f6c7b075
--- /dev/null
+++ b/lldb/test/API/lang/cpp/enum_promotion/main.cpp
@@ -0,0 +1,22 @@
+enum EnumUChar { UChar = 1 };
+enum EnumUShort { UShort = 0x101 };
+enum EnumUInt { UInt = 0x10001 };
+enum EnumSLong { SLong = 0x100000001 };
+enum EnumULong { ULong = 0xFFFFFFFFFFFFFFF0 };
+enum EnumNChar { NChar = -1 };
+enum EnumNShort { NShort= -0x101 };
+enum EnumNInt { NInt = -0x10001 };
+enum EnumNLong { NLong = -0x100000001 };
+
+int main() {
+  auto UChar_promoted = +EnumUChar::UChar;
+  auto UShort_promoted = +EnumUShort::UShort;
+  auto UInt_promoted = +EnumUInt::UInt;
+  auto SLong_promoted = +EnumSLong::SLong;
+  auto ULong_promoted = +EnumULong::ULong;
+  auto NChar_promoted = +EnumNChar::NChar;
+  auto NShort_promoted = +EnumNShort::NShort;
+  auto NInt_promoted = +EnumNInt::NInt;
+  auto NLong_promoted = +EnumNLong::NLong;
+  return 0; // break here
+}

>From 8bec472e48834cf8dcd38bd8526c7446a0f89363 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuk...@accesssoftek.com>
Date: Tue, 5 Nov 2024 20:10:58 +0500
Subject: [PATCH 4/8] Fix formatting

---
 lldb/test/API/lang/cpp/enum_promotion/TestCPPEnumPromotion.py | 2 +-
 lldb/test/API/lang/cpp/enum_promotion/main.cpp                | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lldb/test/API/lang/cpp/enum_promotion/TestCPPEnumPromotion.py 
b/lldb/test/API/lang/cpp/enum_promotion/TestCPPEnumPromotion.py
index a4eed53c8d80f2..3cf110441a0ae7 100644
--- a/lldb/test/API/lang/cpp/enum_promotion/TestCPPEnumPromotion.py
+++ b/lldb/test/API/lang/cpp/enum_promotion/TestCPPEnumPromotion.py
@@ -7,8 +7,8 @@
 from lldbsuite.test.lldbtest import *
 from lldbsuite.test import lldbutil
 
-class TestCPPEnumPromotion(TestBase):
 
+class TestCPPEnumPromotion(TestBase):
     @skipIf(debug_info=no_match(["dwarf", "dwo"]))
     def test(self):
         self.build()
diff --git a/lldb/test/API/lang/cpp/enum_promotion/main.cpp 
b/lldb/test/API/lang/cpp/enum_promotion/main.cpp
index fafb96f6c7b075..bcdb0adff5d401 100644
--- a/lldb/test/API/lang/cpp/enum_promotion/main.cpp
+++ b/lldb/test/API/lang/cpp/enum_promotion/main.cpp
@@ -4,7 +4,7 @@ enum EnumUInt { UInt = 0x10001 };
 enum EnumSLong { SLong = 0x100000001 };
 enum EnumULong { ULong = 0xFFFFFFFFFFFFFFF0 };
 enum EnumNChar { NChar = -1 };
-enum EnumNShort { NShort= -0x101 };
+enum EnumNShort { NShort = -0x101 };
 enum EnumNInt { NInt = -0x10001 };
 enum EnumNLong { NLong = -0x100000001 };
 

>From 77c262dcb59913ebd591e860d7db59603e732811 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuk...@accesssoftek.com>
Date: Fri, 20 Dec 2024 23:52:16 +0500
Subject: [PATCH 5/8] Move enum type and width computations to a separate
 function

---
 clang/include/clang/Sema/Sema.h               |  7 ++
 clang/lib/Sema/SemaDecl.cpp                   | 81 +++++++++++++++++++
 .../SymbolFile/DWARF/DWARFASTParserClang.cpp  | 65 ++-------------
 3 files changed, 96 insertions(+), 57 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 4d6e02fe2956e0..39c97f0bc8fea6 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3994,6 +3994,13 @@ class Sema final : public SemaBase {
                           SourceLocation IdLoc, IdentifierInfo *Id,
                           const ParsedAttributesView &Attrs,
                           SourceLocation EqualLoc, Expr *Val);
+
+  bool ComputeBestEnumProperties(ASTContext &Context, EnumDecl *Enum,
+                                 bool isCpp, bool isPacked,
+                                 unsigned NumNegativeBits,
+                                 unsigned NumPositiveBits, unsigned &BestWidth,
+                                 QualType &BestType,
+                                 QualType &BestPromotionType);
   void ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
                      Decl *EnumDecl, ArrayRef<Decl *> Elements, Scope *S,
                      const ParsedAttributesView &Attr);
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c3ff247a6316d3..6bb576eb735a19 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -20036,6 +20036,87 @@ bool Sema::IsValueInFlagEnum(const EnumDecl *ED, const 
llvm::APInt &Val,
   return !(FlagMask & Val) || (AllowMask && !(FlagMask & ~Val));
 }
 
+bool Sema::ComputeBestEnumProperties(ASTContext &Context, EnumDecl *Enum,
+                                     bool is_cpp, bool isPacked,
+                                     unsigned NumNegativeBits,
+                                     unsigned NumPositiveBits,
+                                     unsigned &BestWidth, QualType &BestType,
+                                     QualType &BestPromotionType) {
+  unsigned IntWidth = Context.getTargetInfo().getIntWidth();
+  unsigned CharWidth = Context.getTargetInfo().getCharWidth();
+  unsigned ShortWidth = Context.getTargetInfo().getShortWidth();
+  bool enum_too_large = false;
+  if (NumNegativeBits) {
+    // If there is a negative value, figure out the smallest integer type (of
+    // int/long/longlong) that fits.
+    // If it's packed, check also if it fits a char or a short.
+    if (isPacked && NumNegativeBits <= CharWidth &&
+        NumPositiveBits < CharWidth) {
+      BestType = Context.SignedCharTy;
+      BestWidth = CharWidth;
+    } else if (isPacked && NumNegativeBits <= ShortWidth &&
+               NumPositiveBits < ShortWidth) {
+      BestType = Context.ShortTy;
+      BestWidth = ShortWidth;
+    } else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
+      BestType = Context.IntTy;
+      BestWidth = IntWidth;
+    } else {
+      BestWidth = Context.getTargetInfo().getLongWidth();
+
+      if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) {
+        BestType = Context.LongTy;
+      } else {
+        BestWidth = Context.getTargetInfo().getLongLongWidth();
+
+        if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth)
+          enum_too_large = true;
+        BestType = Context.LongLongTy;
+      }
+    }
+    BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : BestType);
+  } else {
+    // If there is no negative value, figure out the smallest type that fits
+    // all of the enumerator values.
+    // If it's packed, check also if it fits a char or a short.
+    if (isPacked && NumPositiveBits <= CharWidth) {
+      BestType = Context.UnsignedCharTy;
+      BestPromotionType = Context.IntTy;
+      BestWidth = CharWidth;
+    } else if (isPacked && NumPositiveBits <= ShortWidth) {
+      BestType = Context.UnsignedShortTy;
+      BestPromotionType = Context.IntTy;
+      BestWidth = ShortWidth;
+    } else if (NumPositiveBits <= IntWidth) {
+      BestType = Context.UnsignedIntTy;
+      BestWidth = IntWidth;
+      BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
+                              ? Context.UnsignedIntTy
+                              : Context.IntTy;
+    } else if (NumPositiveBits <=
+               (BestWidth = Context.getTargetInfo().getLongWidth())) {
+      BestType = Context.UnsignedLongTy;
+      BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
+                              ? Context.UnsignedLongTy
+                              : Context.LongTy;
+    } else {
+      BestWidth = Context.getTargetInfo().getLongLongWidth();
+      if (NumPositiveBits > BestWidth) {
+        // This can happen with bit-precise integer types, but those are not
+        // allowed as the type for an enumerator per C23 6.7.2.2p4 and p12.
+        // FIXME: GCC uses __int128_t and __uint128_t for cases that fit within
+        // a 128-bit integer, we should consider doing the same.
+        enum_too_large = true;
+      }
+      BestType = Context.UnsignedLongLongTy;
+      BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
+                              ? Context.UnsignedLongLongTy
+                              : Context.LongLongTy;
+    }
+  }
+  return enum_too_large;
+}
+
 void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
                          Decl *EnumDeclX, ArrayRef<Decl *> Elements, Scope *S,
                          const ParsedAttributesView &Attrs) {
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp 
b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index 676cc7d7603655..2bc899e5b9edec 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -2401,69 +2401,20 @@ size_t DWARFASTParserClang::ParseChildEnumerators(
   enum_decl->setNumPositiveBits(NumPositiveBits);
   enum_decl->setNumNegativeBits(NumNegativeBits);
 
-  // C++0x N3000 [conv.prom]p3:
-  //   An rvalue of an unscoped enumeration type whose underlying
-  //   type is not fixed can be converted to an rvalue of the first
-  //   of the following types that can represent all the values of
-  //   the enumeration: int, unsigned int, long int, unsigned long
-  //   int, long long int, or unsigned long long int.
-  // C99 6.4.4.3p2:
-  //   An identifier declared as an enumeration constant has type int.
-  // The C99 rule is modified by C23.
+  auto ts_ptr = clang_type.GetTypeSystem().dyn_cast_or_null<TypeSystemClang>();
+  if (!ts_ptr)
+    return enumerators_added;
+
   clang::QualType BestPromotionType;
+  clang::QualType BestType;
   unsigned BestWidth;
 
   auto &Context = m_ast.getASTContext();
-  unsigned LongWidth = Context.getTargetInfo().getLongWidth();
-  unsigned IntWidth = Context.getTargetInfo().getIntWidth();
-  unsigned CharWidth = Context.getTargetInfo().getCharWidth();
-  unsigned ShortWidth = Context.getTargetInfo().getShortWidth();
-
   bool is_cpp = Language::LanguageIsCPlusPlus(
       SymbolFileDWARF::GetLanguage(*parent_die.GetCU()));
-
-  if (NumNegativeBits) {
-    // If there is a negative value, figure out the smallest integer type (of
-    // int/long/longlong) that fits.
-    if (NumNegativeBits <= CharWidth && NumPositiveBits < CharWidth) {
-      BestWidth = CharWidth;
-    } else if (NumNegativeBits <= ShortWidth && NumPositiveBits < ShortWidth) {
-      BestWidth = ShortWidth;
-    } else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
-      BestWidth = IntWidth;
-    } else if (NumNegativeBits <= LongWidth && NumPositiveBits < LongWidth) {
-      BestWidth = LongWidth;
-    } else {
-      BestWidth = Context.getTargetInfo().getLongLongWidth();
-    }
-    BestPromotionType =
-        BestWidth <= IntWidth ? Context.IntTy : enum_decl->getIntegerType();
-  } else {
-    // If there is no negative value, figure out the smallest type that fits
-    // all of the enumerator values.
-    if (NumPositiveBits <= CharWidth) {
-      BestPromotionType = Context.IntTy;
-      BestWidth = CharWidth;
-    } else if (NumPositiveBits <= ShortWidth) {
-      BestPromotionType = Context.IntTy;
-      BestWidth = ShortWidth;
-    } else if (NumPositiveBits <= IntWidth) {
-      BestWidth = IntWidth;
-      BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
-                              ? Context.UnsignedIntTy
-                              : Context.IntTy;
-    } else if (NumPositiveBits <= LongWidth) {
-      BestWidth = LongWidth;
-      BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
-                              ? Context.UnsignedLongTy
-                              : Context.LongTy;
-    } else {
-      BestWidth = Context.getTargetInfo().getLongLongWidth();
-      BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
-                              ? Context.UnsignedLongLongTy
-                              : Context.LongLongTy;
-    }
-  }
+  ts_ptr->getSema()->ComputeBestEnumProperties(
+      Context, enum_decl, is_cpp, false, NumNegativeBits, NumPositiveBits,
+      BestWidth, BestType, BestPromotionType);
   enum_decl->setPromotionType(BestPromotionType);
 
   return enumerators_added;

>From 4abfc5ab45b785580c88dfa9c2d5238dbf89d958 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuk...@accesssoftek.com>
Date: Mon, 27 Jan 2025 20:29:52 +0500
Subject: [PATCH 6/8] Use computeBestEnumTypes() from ASTContext & cleanup code

---
 clang/include/clang/Sema/Sema.h               |  7 --
 clang/lib/Sema/SemaDecl.cpp                   | 81 -------------------
 .../SymbolFile/DWARF/DWARFASTParserClang.cpp  | 20 +----
 3 files changed, 2 insertions(+), 106 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 39c97f0bc8fea6..4d6e02fe2956e0 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3994,13 +3994,6 @@ class Sema final : public SemaBase {
                           SourceLocation IdLoc, IdentifierInfo *Id,
                           const ParsedAttributesView &Attrs,
                           SourceLocation EqualLoc, Expr *Val);
-
-  bool ComputeBestEnumProperties(ASTContext &Context, EnumDecl *Enum,
-                                 bool isCpp, bool isPacked,
-                                 unsigned NumNegativeBits,
-                                 unsigned NumPositiveBits, unsigned &BestWidth,
-                                 QualType &BestType,
-                                 QualType &BestPromotionType);
   void ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
                      Decl *EnumDecl, ArrayRef<Decl *> Elements, Scope *S,
                      const ParsedAttributesView &Attr);
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 6bb576eb735a19..c3ff247a6316d3 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -20036,87 +20036,6 @@ bool Sema::IsValueInFlagEnum(const EnumDecl *ED, const 
llvm::APInt &Val,
   return !(FlagMask & Val) || (AllowMask && !(FlagMask & ~Val));
 }
 
-bool Sema::ComputeBestEnumProperties(ASTContext &Context, EnumDecl *Enum,
-                                     bool is_cpp, bool isPacked,
-                                     unsigned NumNegativeBits,
-                                     unsigned NumPositiveBits,
-                                     unsigned &BestWidth, QualType &BestType,
-                                     QualType &BestPromotionType) {
-  unsigned IntWidth = Context.getTargetInfo().getIntWidth();
-  unsigned CharWidth = Context.getTargetInfo().getCharWidth();
-  unsigned ShortWidth = Context.getTargetInfo().getShortWidth();
-  bool enum_too_large = false;
-  if (NumNegativeBits) {
-    // If there is a negative value, figure out the smallest integer type (of
-    // int/long/longlong) that fits.
-    // If it's packed, check also if it fits a char or a short.
-    if (isPacked && NumNegativeBits <= CharWidth &&
-        NumPositiveBits < CharWidth) {
-      BestType = Context.SignedCharTy;
-      BestWidth = CharWidth;
-    } else if (isPacked && NumNegativeBits <= ShortWidth &&
-               NumPositiveBits < ShortWidth) {
-      BestType = Context.ShortTy;
-      BestWidth = ShortWidth;
-    } else if (NumNegativeBits <= IntWidth && NumPositiveBits < IntWidth) {
-      BestType = Context.IntTy;
-      BestWidth = IntWidth;
-    } else {
-      BestWidth = Context.getTargetInfo().getLongWidth();
-
-      if (NumNegativeBits <= BestWidth && NumPositiveBits < BestWidth) {
-        BestType = Context.LongTy;
-      } else {
-        BestWidth = Context.getTargetInfo().getLongLongWidth();
-
-        if (NumNegativeBits > BestWidth || NumPositiveBits >= BestWidth)
-          enum_too_large = true;
-        BestType = Context.LongLongTy;
-      }
-    }
-    BestPromotionType = (BestWidth <= IntWidth ? Context.IntTy : BestType);
-  } else {
-    // If there is no negative value, figure out the smallest type that fits
-    // all of the enumerator values.
-    // If it's packed, check also if it fits a char or a short.
-    if (isPacked && NumPositiveBits <= CharWidth) {
-      BestType = Context.UnsignedCharTy;
-      BestPromotionType = Context.IntTy;
-      BestWidth = CharWidth;
-    } else if (isPacked && NumPositiveBits <= ShortWidth) {
-      BestType = Context.UnsignedShortTy;
-      BestPromotionType = Context.IntTy;
-      BestWidth = ShortWidth;
-    } else if (NumPositiveBits <= IntWidth) {
-      BestType = Context.UnsignedIntTy;
-      BestWidth = IntWidth;
-      BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
-                              ? Context.UnsignedIntTy
-                              : Context.IntTy;
-    } else if (NumPositiveBits <=
-               (BestWidth = Context.getTargetInfo().getLongWidth())) {
-      BestType = Context.UnsignedLongTy;
-      BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
-                              ? Context.UnsignedLongTy
-                              : Context.LongTy;
-    } else {
-      BestWidth = Context.getTargetInfo().getLongLongWidth();
-      if (NumPositiveBits > BestWidth) {
-        // This can happen with bit-precise integer types, but those are not
-        // allowed as the type for an enumerator per C23 6.7.2.2p4 and p12.
-        // FIXME: GCC uses __int128_t and __uint128_t for cases that fit within
-        // a 128-bit integer, we should consider doing the same.
-        enum_too_large = true;
-      }
-      BestType = Context.UnsignedLongLongTy;
-      BestPromotionType = (NumPositiveBits == BestWidth || !is_cpp)
-                              ? Context.UnsignedLongLongTy
-                              : Context.LongLongTy;
-    }
-  }
-  return enum_too_large;
-}
-
 void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
                          Decl *EnumDeclX, ArrayRef<Decl *> Elements, Scope *S,
                          const ParsedAttributesView &Attrs) {
diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp 
b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index 2bc899e5b9edec..6da1f93a79a2c7 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -2387,12 +2387,6 @@ size_t DWARFASTParserClang::ParseChildEnumerators(
     }
   }
 
-  /// The following code follows the same logic as in Sema::ActOnEnumBody
-  /// clang/lib/Sema/SemaDecl.cpp
-  // If we have an empty set of enumerators we still need one bit.
-  // From [dcl.enum]p8
-  // If the enumerator-list is empty, the values of the enumeration are as if
-  // the enumeration had a single enumerator with value 0
   if (!NumPositiveBits && !NumNegativeBits)
     NumPositiveBits = 1;
 
@@ -2401,20 +2395,10 @@ size_t DWARFASTParserClang::ParseChildEnumerators(
   enum_decl->setNumPositiveBits(NumPositiveBits);
   enum_decl->setNumNegativeBits(NumNegativeBits);
 
-  auto ts_ptr = clang_type.GetTypeSystem().dyn_cast_or_null<TypeSystemClang>();
-  if (!ts_ptr)
-    return enumerators_added;
-
   clang::QualType BestPromotionType;
   clang::QualType BestType;
-  unsigned BestWidth;
-
-  auto &Context = m_ast.getASTContext();
-  bool is_cpp = Language::LanguageIsCPlusPlus(
-      SymbolFileDWARF::GetLanguage(*parent_die.GetCU()));
-  ts_ptr->getSema()->ComputeBestEnumProperties(
-      Context, enum_decl, is_cpp, false, NumNegativeBits, NumPositiveBits,
-      BestWidth, BestType, BestPromotionType);
+  m_ast.getASTContext().computeBestEnumTypes(
+      false, NumNegativeBits, NumPositiveBits, BestType, BestPromotionType);
   enum_decl->setPromotionType(BestPromotionType);
 
   return enumerators_added;

>From e88c7e8f9198b2016d36b165c915ce05daed8629 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuk...@accesssoftek.com>
Date: Mon, 27 Jan 2025 23:16:49 +0500
Subject: [PATCH 7/8] Add comments

---
 .../source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp 
b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index 6da1f93a79a2c7..01c70a30b2f231 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -2387,6 +2387,10 @@ size_t DWARFASTParserClang::ParseChildEnumerators(
     }
   }
 
+  // If we have an empty set of enumerators we still need one bit.
+  // From [dcl.enum]p8
+  // If the enumerator-list is empty, the values of the enumeration are as if
+  // the enumeration had a single enumerator with value 0
   if (!NumPositiveBits && !NumNegativeBits)
     NumPositiveBits = 1;
 
@@ -2398,7 +2402,7 @@ size_t DWARFASTParserClang::ParseChildEnumerators(
   clang::QualType BestPromotionType;
   clang::QualType BestType;
   m_ast.getASTContext().computeBestEnumTypes(
-      false, NumNegativeBits, NumPositiveBits, BestType, BestPromotionType);
+      /*IsPacked=*/false, NumNegativeBits, NumPositiveBits, BestType, 
BestPromotionType);
   enum_decl->setPromotionType(BestPromotionType);
 
   return enumerators_added;

>From 8625ab6b455f1c1ac62a0ef820bd21bb1a9e22f2 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuk...@accesssoftek.com>
Date: Mon, 27 Jan 2025 23:49:02 +0500
Subject: [PATCH 8/8] Apply clang-format

---
 lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp 
b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
index 01c70a30b2f231..4b15d174717411 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -2402,7 +2402,8 @@ size_t DWARFASTParserClang::ParseChildEnumerators(
   clang::QualType BestPromotionType;
   clang::QualType BestType;
   m_ast.getASTContext().computeBestEnumTypes(
-      /*IsPacked=*/false, NumNegativeBits, NumPositiveBits, BestType, 
BestPromotionType);
+      /*IsPacked=*/false, NumNegativeBits, NumPositiveBits, BestType,
+      BestPromotionType);
   enum_decl->setPromotionType(BestPromotionType);
 
   return enumerators_added;

_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to