From: Douglas Gregor Date: Tue, 29 Jun 2010 17:12:35 +0000 (+0000) Subject: With packed enums, an enumerator's value may be stored in more bits X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6623584c0ec508110d75572eef092bf98fedf3f4;p=clang 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 --- 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; + } +}