]> granicus.if.org Git - clang/commitdiff
Fix an extremely subtle bug with pointer comparisons: they have to be
authorEli Friedman <eli.friedman@gmail.com>
Thu, 29 May 2008 15:09:15 +0000 (15:09 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Thu, 29 May 2008 15:09:15 +0000 (15:09 +0000)
unsigned because it's possible (at least in theory) to have
have both positive and negative pointers pointing to the same object.

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

lib/CodeGen/CGExprScalar.cpp
test/CodeGen/pointer-cmp-type.c [new file with mode: 0644]

index 9daa21eb340a150f769a4df78840ad9f938fa5ac..fc8639dbf24bf59281f12c26ea5ae346c866e578 100644 (file)
@@ -917,12 +917,12 @@ Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
     if (LHS->getType()->isFloatingPoint()) {
       Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
                                   LHS, RHS, "cmp");
-    } else if (LHSTy->isUnsignedIntegerType()) {
-      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
+    } else if (LHSTy->isSignedIntegerType()) {
+      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
                                   LHS, RHS, "cmp");
     } else {
-      // Signed integers and pointers.
-      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
+      // Unsigned integers and pointers.
+      Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
                                   LHS, RHS, "cmp");
     }
   } else {
diff --git a/test/CodeGen/pointer-cmp-type.c b/test/CodeGen/pointer-cmp-type.c
new file mode 100644 (file)
index 0000000..02fc4c7
--- /dev/null
@@ -0,0 +1,3 @@
+// RUN: clang -emit-llvm %s -o - | grep "icmp ult"
+
+int a(char* a, char* b) {return a<b;}