]> granicus.if.org Git - clang/commitdiff
With packed enums, an enumerator's value may be stored in more bits
authorDouglas Gregor <dgregor@apple.com>
Tue, 29 Jun 2010 17:12:35 +0000 (17:12 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 29 Jun 2010 17:12:35 +0000 (17:12 +0000)
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
test/Sema/enum-packed.c [new file with mode: 0644]

index 738fc55823b3d69ba884b2ecaed3d80705f4430f..8e163ef245273d59bf880f744b00131de72d3e79 100644 (file)
@@ -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 (file)
index 0000000..0eb6c14
--- /dev/null
@@ -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;
+  }
+}