Index: test/Sema/bitfield-promote.c
===================================================================
--- test/Sema/bitfield-promote.c	(revision 79412)
+++ test/Sema/bitfield-promote.c	(working copy)
@@ -7,4 +7,29 @@
 
 struct { int x : 8; } x1;
 long long y1;
-__typeof__(((long long)x1.x + 1)) y1;
\ No newline at end of file
+__typeof__(((long long)x1.x + 1)) y1;
+
+
+// Check for extensions: variously sized unsigned bit-fields fitting
+// into a signed int promote to signed int.
+enum E { ec1, ec2, ec3 };
+struct S {
+  enum E          e : 2;
+  unsigned short us : 4;
+  unsigned long ul1 : 8;
+  unsigned long ul2 : 50;
+} s;
+
+__typeof(s.e + s.e) x_e;
+int x_e;
+
+__typeof(s.us + s.us) x_us;
+int x_us;
+
+__typeof(s.ul1 + s.ul1) x_ul1;
+int x_ul1;
+
+/* No promotion here. */
+__typeof(s.ul2 + s.ul2) x_ul2;
+unsigned long x_ul2;
+
Index: include/clang/AST/ASTContext.h
===================================================================
--- include/clang/AST/ASTContext.h	(revision 79412)
+++ include/clang/AST/ASTContext.h	(working copy)
@@ -818,6 +818,13 @@
   /// integer type.
   QualType getPromotedIntegerType(QualType PromotableType);
 
+  /// \brief Whether this is a promotable bitfield reference according
+  /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
+  ///
+  /// \returns the type this bit-field will promote to, or NULL if no
+  /// promotion occurs.
+  QualType isPromotableBitField(Expr *E);
+
   /// getIntegerTypeOrder - Returns the highest ranked integer type: 
   /// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
   /// LHS < RHS, return -1. 
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp	(revision 79412)
+++ lib/Sema/SemaExpr.cpp	(working copy)
@@ -215,41 +215,6 @@
   }
 }
 
-/// \brief Whether this is a promotable bitfield reference according
-/// to C99 6.3.1.1p2, bullet 2.
-///
-/// \returns the type this bit-field will promote to, or NULL if no
-/// promotion occurs.
-static QualType isPromotableBitField(Expr *E, ASTContext &Context) {
-  FieldDecl *Field = E->getBitField();
-  if (!Field)
-    return QualType();
-
-  const BuiltinType *BT = Field->getType()->getAsBuiltinType();
-  if (!BT)
-    return QualType();
-
-  if (BT->getKind() != BuiltinType::Bool &&
-      BT->getKind() != BuiltinType::Int &&
-      BT->getKind() != BuiltinType::UInt) 
-    return QualType();
-
-  llvm::APSInt BitWidthAP;
-  if (!Field->getBitWidth()->isIntegerConstantExpr(BitWidthAP, Context))
-    return QualType();
-
-  uint64_t BitWidth = BitWidthAP.getZExtValue();
-  uint64_t IntSize = Context.getTypeSize(Context.IntTy);
-  if (BitWidth < IntSize ||
-      (Field->getType()->isSignedIntegerType() && BitWidth == IntSize))
-    return Context.IntTy;
-
-  if (BitWidth == IntSize && Field->getType()->isUnsignedIntegerType())
-    return Context.UnsignedIntTy;
-
-  return QualType();
-}
-
 /// UsualUnaryConversions - Performs various conversions that are common to most
 /// operators (C99 6.3). The conversions of array and function types are 
 /// sometimes surpressed. For example, the array->pointer conversion doesn't
@@ -272,18 +237,17 @@
   //   value is converted to an int; otherwise, it is converted to an
   //   unsigned int. These are called the integer promotions. All
   //   other types are unchanged by the integer promotions.
+  QualType PTy = Context.isPromotableBitField(Expr);
+  if (!PTy.isNull()) {
+    ImpCastExprToType(Expr, PTy);
+    return Expr;
+  }
   if (Ty->isPromotableIntegerType()) {
     QualType PT = Context.getPromotedIntegerType(Ty);
     ImpCastExprToType(Expr, PT);
     return Expr;
-  } else {
-    QualType T = isPromotableBitField(Expr, Context);
-    if (!T.isNull()) {
-      ImpCastExprToType(Expr, T);
-      return Expr;
-    }
-  } 
-    
+  }
+
   DefaultFunctionArrayConversion(Expr);
   return Expr;
 }
@@ -355,10 +319,10 @@
     return lhs;
 
   // Perform bitfield promotions.
-  QualType LHSBitfieldPromoteTy = isPromotableBitField(lhsExpr, Context);
+  QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr);
   if (!LHSBitfieldPromoteTy.isNull())
     lhs = LHSBitfieldPromoteTy;
-  QualType RHSBitfieldPromoteTy = isPromotableBitField(rhsExpr, Context);
+  QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr);
   if (!RHSBitfieldPromoteTy.isNull())
     rhs = RHSBitfieldPromoteTy;
 
@@ -3948,15 +3912,12 @@
       }
       
       if (CompLHSTy) {
-        QualType LHSTy = lex->getType();
-        if (LHSTy->isPromotableIntegerType())
-          LHSTy = Context.getPromotedIntegerType(LHSTy);
-        else {
-          QualType T = isPromotableBitField(lex, Context);
-          if (!T.isNull())
-            LHSTy = T;
+        QualType LHSTy = Context.isPromotableBitField(lex);
+        if (LHSTy.isNull()) {
+          LHSTy = lex->getType();
+          if (LHSTy->isPromotableIntegerType())
+            LHSTy = Context.getPromotedIntegerType(LHSTy);
         }
-
         *CompLHSTy = LHSTy;
       }
       return PExp->getType();
@@ -4118,13 +4079,11 @@
 
   // Shifts don't perform usual arithmetic conversions, they just do integer
   // promotions on each operand. C99 6.5.7p3
-  QualType LHSTy = lex->getType();
-  if (LHSTy->isPromotableIntegerType())
-    LHSTy = Context.getPromotedIntegerType(LHSTy);
-  else {
-    QualType T = isPromotableBitField(lex, Context);
-    if (!T.isNull())
-      LHSTy = T;
+  QualType LHSTy = Context.isPromotableBitField(lex);
+  if (LHSTy.isNull()) {
+    LHSTy = lex->getType();
+    if (LHSTy->isPromotableIntegerType())
+      LHSTy = Context.getPromotedIntegerType(LHSTy);
   }
   if (!isCompAssign)
     ImpCastExprToType(lex, LHSTy);
Index: lib/AST/ASTContext.cpp
===================================================================
--- lib/AST/ASTContext.cpp	(revision 79412)
+++ lib/AST/ASTContext.cpp	(working copy)
@@ -2487,6 +2487,50 @@
   }
 }
 
+/// \brief Whether this is a promotable bitfield reference according
+/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
+///
+/// \returns the type this bit-field will promote to, or NULL if no
+/// promotion occurs.
+QualType ASTContext::isPromotableBitField(Expr *E) {
+  FieldDecl *Field = E->getBitField();
+  if (!Field)
+    return QualType();
+
+  QualType FT = Field->getType();
+  if (!FT->getAsBuiltinType()) {
+    const EnumType *ET = FT->getAsEnumType();
+    if (ET)
+      FT = ET->getDecl()->getIntegerType();
+    else
+      return QualType();
+  }
+  const BuiltinType *BT = FT->getAsBuiltinType();
+  assert(BT);
+
+  llvm::APSInt BitWidthAP;
+  if (!Field->getBitWidth()->isIntegerConstantExpr(BitWidthAP, *this))
+    return QualType();
+
+  uint64_t BitWidth = BitWidthAP.getZExtValue();
+  uint64_t IntSize = getTypeSize(IntTy);
+  // GCC extension compatibility: if the bit-field size is less than the
+  // size of int, it gets promoted no matter what its type is.
+  // For instance, unsigned long bf : 4 gets promoted to signed int.
+  if (BitWidth < IntSize)
+    return IntTy;
+
+  assert(BitWidth >= IntSize);
+  if (getIntegerTypeOrder(FT, UnsignedIntTy) <= 0) {
+    assert(BitWidth == IntSize);
+    // Here promotion occurs and it only depends on signedness.
+    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
+  }
+  else
+    // Types bigger than unsigned int are not subject to promotions.
+    return QualType();
+}
+
 /// getPromotedIntegerType - Returns the type that Promotable will
 /// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
 /// integer type.
