[clang] [clang][Sema] Fix for enums overflowing (#24667) (PR #78742)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 5 09:19:58 PST 2024


================
@@ -19797,6 +19797,19 @@ EnumConstantDecl *Sema::CheckEnumConstant(EnumDecl *Enum,
           //     - If an initializer is specified for an enumerator, the
           //       initializing value has the same type as the expression.
           EltTy = Val->getType();
+        } else if (getLangOpts().C23) {
+          // C23 6.7.2.2p11 b4
+          // int, if given explicitly with = and the value of the
+          // integer constant expression is representable by an int
+          //
+          // C23 6.7.2.2p11 b5
+          // the type of the integer constant expression, if given
+          // explicitly with = and if the value of the integer
+          // constant expression is not representable by int;
+          if (isRepresentableIntegerValue(Context, EnumVal, Context.IntTy)) {
+            Val = ImpCastExprToType(Val, Context.IntTy, CK_IntegralCast).get();
+          }
----------------
AaronBallman wrote:

```suggestion
          if (isRepresentableIntegerValue(Context, EnumVal, Context.IntTy))
            Val = ImpCastExprToType(Val, Context.IntTy, CK_IntegralCast).get();
```
Per our odd coding standard.

https://github.com/llvm/llvm-project/pull/78742


More information about the cfe-commits mailing list