]> granicus.if.org Git - clang/commitdiff
Fixup -ftrapv to be more gcc compatible. -ftrapu (for want of a
authorMike Stump <mrs@apple.com>
Thu, 2 Apr 2009 01:03:55 +0000 (01:03 +0000)
committerMike Stump <mrs@apple.com>
Thu, 2 Apr 2009 01:03:55 +0000 (01:03 +0000)
better name) is the option that SmallTalk can use to intercept all
overflows, including unsigned.  I added some testcases so we don't
break anything.

Also included is another patch from David for += and friends.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@68267 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/LangOptions.h
include/clang/Driver/Options.def
lib/CodeGen/CGExprScalar.cpp
tools/clang-cc/clang-cc.cpp

index 818ccda48bc26c9e44a227ae64a1f2c0ef60b69b..3587e8b39c13ec4552d1e1fbc8612f1114443f54 100644 (file)
@@ -59,8 +59,12 @@ public:
   unsigned MathErrno         : 1; // Math functions must respect errno
                                   // (modulo the platform support).
 
+  unsigned UnsignedOverflowChecking : 1;
+                                  // Extension to call a handler function when
+                                  // unsigned and signed integer arithmetic overflows.
+
   unsigned OverflowChecking  : 1; // Extension to call a handler function when
-                                  // integer arithmetic overflows.
+                                  // signed integer arithmetic overflows.
 
   unsigned HeinousExtensions : 1; // Extensions that we really don't like and
                                   // may be ripped out at any time.
@@ -90,6 +94,7 @@ public:
     EmitAllDecls = 0;
     MathErrno = 1;
 
+    UnsignedOverflowChecking = 0;
     OverflowChecking = 0;
 
     InstantiationDepth = 99;
index 33b24f63dd7c4b310ddca9557c40066d15016462..e49e611a54746b36643ccdc0bd7b5d6ac191d528 100644 (file)
@@ -448,6 +448,7 @@ OPTION("-ftemplate-depth-", ftemplate_depth_, Joined, f_Group, INVALID, "", 0, 0
 OPTION("-fterminated-vtables", fterminated_vtables, Flag, f_Group, INVALID, "", 0, 0, 0)
 OPTION("-ftime-report", ftime_report, Flag, clang_f_Group, INVALID, "", 0, 0, 0)
 OPTION("-ftraditional", ftraditional, Flag, f_Group, INVALID, "", 0, 0, 0)
+OPTION("-ftrapu", ftrapu, Flag, clang_f_Group, INVALID, "", 0, 0, 0)
 OPTION("-ftrapv", ftrapv, Flag, clang_f_Group, INVALID, "", 0, 0, 0)
 OPTION("-funwind-tables", funwind_tables, Flag, f_Group, INVALID, "", 0, 0, 0)
 OPTION("-fverbose-asm", fverbose_asm, Flag, f_Group, INVALID, "", 0, 0, 0)
index b47d5787346b0a41fdb98a454898e2261b772cbc..1d2da0ddf89d0cca8d5b345861c423c9a1d00064 100644 (file)
@@ -262,7 +262,9 @@ public:
     
   // Binary Operators.
   Value *EmitMul(const BinOpInfo &Ops) {
-    if (CGF.getContext().getLangOptions().OverflowChecking)
+    if (CGF.getContext().getLangOptions().UnsignedOverflowChecking
+        || (CGF.getContext().getLangOptions().OverflowChecking
+            && Ops.Ty->isSignedIntegerType()))
       return EmitOverflowCheckedBinOp(Ops);
     return Builder.CreateMul(Ops.LHS, Ops.RHS, "mul");
   }
@@ -834,21 +836,26 @@ Value *ScalarExprEmitter::EmitRem(const BinOpInfo &Ops) {
 Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
   unsigned IID;
   unsigned OpID = 0;
+
   if (Ops.Ty->isSignedIntegerType()) {
     switch (Ops.E->getOpcode()) {
       case BinaryOperator::Add:
+      case BinaryOperator::AddAssign:
         OpID = 1;
         IID = llvm::Intrinsic::sadd_with_overflow;
         break;
       case BinaryOperator::Sub:
+      case BinaryOperator::SubAssign:
         OpID = 2;
         IID = llvm::Intrinsic::ssub_with_overflow;
         break;
       case BinaryOperator::Mul:
+      case BinaryOperator::MulAssign:
         OpID = 3;
         IID = llvm::Intrinsic::smul_with_overflow;
         break;
       default:
+               fprintf(stderr, "Opcode: %d\n", Ops.E->getOpcode());
         assert(false && "Unsupported operation for overflow detection");
     }
     OpID <<= 1;
@@ -859,14 +866,17 @@ Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
         "Must be either a signed or unsigned integer op");
     switch (Ops.E->getOpcode()) {
       case BinaryOperator::Add:
+      case BinaryOperator::AddAssign:
         OpID = 1;
         IID = llvm::Intrinsic::uadd_with_overflow;
         break;
       case BinaryOperator::Sub:
+      case BinaryOperator::SubAssign:
         OpID = 2;
         IID = llvm::Intrinsic::usub_with_overflow;
         break;
       case BinaryOperator::Mul:
+      case BinaryOperator::MulAssign:
         OpID = 3;
         IID = llvm::Intrinsic::umul_with_overflow;
         break;
@@ -935,7 +945,9 @@ Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
 
 Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
   if (!Ops.Ty->isPointerType()) {
-    if (CGF.getContext().getLangOptions().OverflowChecking)
+    if (CGF.getContext().getLangOptions().UnsignedOverflowChecking
+        || (CGF.getContext().getLangOptions().OverflowChecking
+            && Ops.Ty->isSignedIntegerType()))
       return EmitOverflowCheckedBinOp(Ops);
     return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
   }
@@ -986,7 +998,9 @@ Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
 
 Value *ScalarExprEmitter::EmitSub(const BinOpInfo &Ops) {
   if (!isa<llvm::PointerType>(Ops.LHS->getType())) {
-    if (CGF.getContext().getLangOptions().OverflowChecking)
+    if (CGF.getContext().getLangOptions().UnsignedOverflowChecking
+        || (CGF.getContext().getLangOptions().OverflowChecking
+            && Ops.Ty->isSignedIntegerType()))
       return EmitOverflowCheckedBinOp(Ops);
     return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
   }
index b149a1da92b06005181d753789905c2634632dc0..1a7d2df708785b76057f5903debac1d0846d0ac4 100644 (file)
@@ -675,13 +675,19 @@ void InitializeGCMode(LangOptions &Options) {
     Options.setGCMode(LangOptions::HybridGC);
 }
 
+static llvm::cl::opt<bool>
+UnsignedOverflowChecking("ftrapu",
+                         llvm::cl::desc("Trap on unsigned and signed integer overflow"),
+                         llvm::cl::init(false));
+
 static llvm::cl::opt<bool>
 OverflowChecking("ftrapv",
-           llvm::cl::desc("Trap on integer overflow"),
+           llvm::cl::desc("Trap on signed integer overflow"),
            llvm::cl::init(false));
 
 void InitializeOverflowChecking(LangOptions &Options) {
-  Options.OverflowChecking = OverflowChecking;
+  Options.OverflowChecking = OverflowChecking | UnsignedOverflowChecking;
+  Options.UnsignedOverflowChecking = UnsignedOverflowChecking;
 }
 //===----------------------------------------------------------------------===//
 // Target Triple Processing.