Partial implementation of PR3342: break out pointer sign 
incompatibilities in assignments from other pointer incompatibilities.  
Based off of the patch in PR3342.  (This doesn't implement -Wno-pointer-sign,
but I don't know the driver code very well.)



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67494 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 3220a9e..85d28a8 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -2792,9 +2792,33 @@
   }
   // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
   // unqualified versions of compatible types, ...
-  if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
-                                  rhptee.getUnqualifiedType()))
-    return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
+  lhptee = lhptee.getUnqualifiedType();
+  rhptee = rhptee.getUnqualifiedType();
+  if (!Context.typesAreCompatible(lhptee, rhptee)) {
+    // Check if the pointee types are compatible ignoring the sign.
+    // We explicitly check for char so that we catch "char" vs
+    // "unsigned char" on systems where "char" is unsigned.
+    if (lhptee->isCharType()) {
+      lhptee = Context.UnsignedCharTy;
+    } else if (lhptee->isSignedIntegerType()) {
+      lhptee = Context.getCorrespondingUnsignedType(lhptee);
+    }
+    if (rhptee->isCharType()) {
+      rhptee = Context.UnsignedCharTy;
+    } else if (rhptee->isSignedIntegerType()) {
+      rhptee = Context.getCorrespondingUnsignedType(rhptee);
+    }
+    if (lhptee == rhptee) {
+      // Types are compatible ignoring the sign. Qualifier incompatibility
+      // takes priority over sign incompatibility because the sign
+      // warning can be disabled.
+      if (ConvTy != Compatible)
+        return ConvTy;
+      return IncompatiblePointerSign;
+    }
+    // General pointer incompatibility takes priority over qualifiers.
+    return IncompatiblePointer; 
+  }
   return ConvTy;
 }
 
@@ -4629,6 +4653,9 @@
   case IncompatiblePointer:
     DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
     break;
+  case IncompatiblePointerSign:
+    DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign;
+    break;
   case FunctionVoidPointer:
     DiagKind = diag::ext_typecheck_convert_pointer_void_func;
     break;