[cfe-commits] r67494 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/Sema.h lib/Sema/SemaExpr.cpp test/Sema/incompatible-sign.c

Eli Friedman eli.friedman at gmail.com
Sun Mar 22 16:59:44 PDT 2009


Author: efriedma
Date: Sun Mar 22 18:59:44 2009
New Revision: 67494

URL: http://llvm.org/viewvc/llvm-project?rev=67494&view=rev
Log:
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.)


Added:
    cfe/trunk/test/Sema/incompatible-sign.c
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=67494&r1=67493&r2=67494&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Sun Mar 22 18:59:44 2009
@@ -1092,6 +1092,8 @@
   "incompatible integer to pointer conversion %2 %1, expected %0">;
 def ext_typecheck_convert_pointer_void_func : Extension<
   "%2 %1 converts between void* and function pointer, expected %0">;
+def ext_typecheck_convert_incompatible_pointer_sign : ExtWarn<
+  "pointer types point to integer types with different sign %2 %1, expected %0">;
 def ext_typecheck_convert_incompatible_pointer : ExtWarn<
   "incompatible pointer types %2 %1, expected %0">;
 def ext_typecheck_convert_discards_qualifiers : ExtWarn<

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=67494&r1=67493&r2=67494&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Sun Mar 22 18:59:44 2009
@@ -2146,7 +2146,13 @@
     /// IncompatiblePointer - The assignment is between two pointers types that
     /// are not compatible, but we accept them as an extension.
     IncompatiblePointer,
-    
+
+    /// IncompatiblePointer - The assignment is between two pointers types which
+    /// point to integers which have a different sign, but are otherwise identical.
+    /// This is a subset of the above, but broken out because it's by far the most
+    /// common case of incompatible pointers.
+    IncompatiblePointerSign,
+
     /// CompatiblePointerDiscardsQualifiers - The assignment discards
     /// c/v/r qualifiers, which we accept as an extension.
     CompatiblePointerDiscardsQualifiers,

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=67494&r1=67493&r2=67494&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sun Mar 22 18:59:44 2009
@@ -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;

Added: cfe/trunk/test/Sema/incompatible-sign.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/incompatible-sign.c?rev=67494&view=auto

==============================================================================
--- cfe/trunk/test/Sema/incompatible-sign.c (added)
+++ cfe/trunk/test/Sema/incompatible-sign.c Sun Mar 22 18:59:44 2009
@@ -0,0 +1,5 @@
+// RUN: clang %s -verify -fsyntax-only
+
+int a(int* x);
+int b(unsigned* y) { return a(y); } // expected-warning {{pointer types point to integer types with different sign}}
+





More information about the cfe-commits mailing list