[cfe-commits] r41530 - in /cfe/trunk: Sema/SemaDecl.cpp include/clang/Basic/DiagnosticKinds.def

Chris Lattner sabre at nondot.org
Mon Aug 27 22:27:00 PDT 2007


Author: lattner
Date: Tue Aug 28 00:27:00 2007
New Revision: 41530

URL: http://llvm.org/viewvc/llvm-project?rev=41530&view=rev
Log:
add a diagnostic:

t.c:1:12: warning: ISO C restricts enumerator values to range of 'int' (180388626432 is too large)
enum e {A, B = 42LL << 32, C = -4, D = 12456 };
           ^


Modified:
    cfe/trunk/Sema/SemaDecl.cpp
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def

Modified: cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?rev=41530&r1=41529&r2=41530&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Tue Aug 28 00:27:00 2007
@@ -987,6 +987,7 @@
   // TODO: If the result value doesn't fit in an int, it must be a long or long
   // long value.  ISO C does not support this, but GCC does as an extension,
   // emit a warning.
+  unsigned IntWidth = Context.getTypeSize(Context.IntTy, Enum->getLocation());
   
   
   // Verify that all the values are okay, and reverse the list.
@@ -995,6 +996,19 @@
     EnumConstantDecl *ECD =
       cast_or_null<EnumConstantDecl>(static_cast<Decl*>(Elements[i]));
     if (!ECD) continue;  // Already issued a diagnostic.
+    
+    // If the enum value doesn't fit in an int, emit an extension warning.
+    assert(ECD->getInitVal().getBitWidth() >= IntWidth &&
+           "Should have promoted value to int");
+    const llvm::APSInt &InitVal = ECD->getInitVal();
+    if (InitVal.getBitWidth() > IntWidth) {
+      llvm::APSInt V(InitVal);
+      V.trunc(IntWidth);
+      V.extend(InitVal.getBitWidth());
+      if (V != InitVal)
+        Diag(ECD->getLocation(), diag::ext_enum_value_not_int,
+             InitVal.toString());
+    }
 
     ECD->setNextDeclarator(EltList);
     EltList = ECD;

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=41530&r1=41529&r2=41530&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Tue Aug 28 00:27:00 2007
@@ -520,6 +520,8 @@
      "duplicate member '%0'")
 DIAG(err_enum_value_not_integer_constant_expr, ERROR,
      "enumerator value for '%0' is not an integer constant")
+DIAG(ext_enum_value_not_int, EXTENSION,
+     "ISO C restricts enumerator values to range of 'int' (%0 is too large)")
 DIAG(err_case_label_not_integer_constant_expr, ERROR,
      "case label does not reduce to an integer constant")
 DIAG(err_typecheck_illegal_vla, ERROR,





More information about the cfe-commits mailing list