From 6623584c0ec508110d75572eef092bf98fedf3f4 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Tue, 29 Jun 2010 17:12:35 +0000 Subject: [PATCH] With packed enums, an enumerator's value may be stored in more bits than the enumeration type itself takes. Fixes PR7477. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@107163 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaStmt.cpp | 2 ++ test/Sema/enum-packed.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 test/Sema/enum-packed.c diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 738fc55823..8e163ef245 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -836,6 +836,8 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtArg Switch, llvm::APSInt Val = (*EDI)->getInitVal(); if(Val.getBitWidth() < CondWidth) Val.extend(CondWidth); + else if (Val.getBitWidth() > CondWidth) + Val.trunc(CondWidth); Val.setIsSigned(CondIsSigned); EnumVals.push_back(std::make_pair(Val, (*EDI))); } diff --git a/test/Sema/enum-packed.c b/test/Sema/enum-packed.c new file mode 100644 index 0000000000..0eb6c14dbe --- /dev/null +++ b/test/Sema/enum-packed.c @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +// PR7477 +enum __attribute__((packed)) E { + Ea, Eb, Ec, Ed +}; + +void test_E(enum E e) { + switch (e) { + case Ea: + case Eb: + case Ec: + case Ed: + break; + } +} -- 2.40.0