[PATCH] D130210: [SemaCXX] Set promotion type for enum bool to integer type.

Zequan Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 20 16:11:55 PDT 2022


zequanwu created this revision.
zequanwu added reviewers: hans, aaron.ballman.
Herald added a project: All.
zequanwu requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

EnumDecl's promotion type is set either to the parsed type or calculated type
after completing its definition. When it's bool type and has no definition,
its promotion type is bool which is not allowed by clang.

This fixes https://github.com/llvm/llvm-project/issues/56560.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D130210

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/test/SemaCXX/enum-bool.cpp


Index: clang/test/SemaCXX/enum-bool.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/enum-bool.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1  -fsyntax-only %s
+
+// This shouldn't crash.
+
+enum GH56560_1 : bool;
+bool GH56560_2(GH56560_1 a, GH56560_1 b) {
+    return a == b;
+}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -16194,7 +16194,11 @@
           ED->setIntegerTypeSourceInfo(TI);
         else
           ED->setIntegerType(QualType(EnumUnderlying.get<const Type *>(), 0));
-        ED->setPromotionType(ED->getIntegerType());
+        QualType EnumTy = ED->getIntegerType();
+        if (EnumTy->isPromotableIntegerType())
+          ED->setPromotionType(Context.getPromotedIntegerType(EnumTy));
+        else
+          ED->setPromotionType(EnumTy);
       }
     } else { // struct/union
       New = RecordDecl::Create(Context, Kind, SearchDC, KWLoc, Loc, Name,
@@ -16816,8 +16820,12 @@
       if (TypeSourceInfo *TI = EnumUnderlying.dyn_cast<TypeSourceInfo*>())
         ED->setIntegerTypeSourceInfo(TI);
       else
-        ED->setIntegerType(QualType(EnumUnderlying.get<const Type*>(), 0));
-      ED->setPromotionType(ED->getIntegerType());
+        ED->setIntegerType(QualType(EnumUnderlying.get<const Type *>(), 0));
+      QualType EnumTy = ED->getIntegerType();
+      if (EnumTy->isPromotableIntegerType())
+        ED->setPromotionType(Context.getPromotedIntegerType(EnumTy));
+      else
+        ED->setPromotionType(EnumTy);
       assert(ED->isComplete() && "enum with type should be complete");
     }
   } else {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130210.446293.patch
Type: text/x-patch
Size: 1725 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220720/f319fb4a/attachment.bin>


More information about the cfe-commits mailing list