[cfe-commits] r74513 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/Sema/compare.c

Chris Lattner sabre at nondot.org
Mon Jun 29 23:24:06 PDT 2009


Author: lattner
Date: Tue Jun 30 01:24:05 2009
New Revision: 74513

URL: http://llvm.org/viewvc/llvm-project?rev=74513&view=rev
Log:
Implement PR4175, catching some questionable comparisons.  Patch by
David Majnemer!

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/compare.c

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

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Jun 30 01:24:05 2009
@@ -1213,6 +1213,10 @@
   "subtraction of pointer %0 requires pointee to be a complete object type">;
 def err_typecheck_sub_ptr_compatible : Error<
   "%0 and %1 are not pointers to compatible types">;
+def ext_typecheck_ordered_comparison_of_pointer_integer : ExtWarn<
+  "ordered comparison between pointer and integer (%0 and %1)">;
+def ext_typecheck_ordered_comparison_of_function_pointers : ExtWarn<
+  "ordered comparison of function pointers (%0 and %1)">;
 def ext_typecheck_comparison_of_pointer_integer : ExtWarn<
   "comparison between pointer and integer (%0 and %1)">;
 def ext_typecheck_comparison_of_distinct_pointers : ExtWarn<

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Jun 30 01:24:05 2009
@@ -4028,6 +4028,17 @@
     QualType RCanPointeeTy =
       Context.getCanonicalType(rType->getAsPointerType()->getPointeeType());
 
+    if (rType->isFunctionPointerType() || lType->isFunctionPointerType()) {
+      if (isRelational) {
+        Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers)
+          << lType << rType << lex->getSourceRange() << rex->getSourceRange();
+      }
+    }
+    if (((!LHSIsNull || isRelational) && LCanPointeeTy->isVoidType()) !=
+        ((!RHSIsNull || isRelational) && RCanPointeeTy->isVoidType())) {
+      Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers)
+        << lType << rType << lex->getSourceRange() << rex->getSourceRange();
+    }
     // Simple check: if the pointee types are identical, we're done.
     if (LCanPointeeTy == RCanPointeeTy)
       return ResultTy;
@@ -4140,7 +4151,10 @@
   }
   if ((lType->isPointerType() || lType->isObjCQualifiedIdType()) &&
        rType->isIntegerType()) {
-    if (!RHSIsNull)
+    if (isRelational)
+      Diag(Loc, diag::ext_typecheck_ordered_comparison_of_pointer_integer)
+        << lType << rType << lex->getSourceRange() << rex->getSourceRange();
+    else if (!RHSIsNull)
       Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer)
         << lType << rType << lex->getSourceRange() << rex->getSourceRange();
     ImpCastExprToType(rex, lType); // promote the integer to pointer
@@ -4148,7 +4162,10 @@
   }
   if (lType->isIntegerType() &&
       (rType->isPointerType() || rType->isObjCQualifiedIdType())) {
-    if (!LHSIsNull)
+    if (isRelational)
+      Diag(Loc, diag::ext_typecheck_ordered_comparison_of_pointer_integer)
+        << lType << rType << lex->getSourceRange() << rex->getSourceRange();
+    else if (!LHSIsNull)
       Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer)
         << lType << rType << lex->getSourceRange() << rex->getSourceRange();
     ImpCastExprToType(lex, rType); // promote the integer to pointer

Modified: cfe/trunk/test/Sema/compare.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/compare.c?rev=74513&r1=74512&r2=74513&view=diff

==============================================================================
--- cfe/trunk/test/Sema/compare.c (original)
+++ cfe/trunk/test/Sema/compare.c Tue Jun 30 01:24:05 2009
@@ -15,3 +15,17 @@
   int d = (a == c);
   return a == b; // expected-warning {{comparison of distinct pointer types}}
 }
+
+int pointers(int *a)
+{
+  return a > 0; // expected-warning {{ordered comparison between pointer and integer}}
+  return a > (void *)0; // expected-warning {{comparison of distinct pointer types}}
+}
+
+int function_pointers(int (*a)(int), int (*b)(int))
+{
+  return a > b; // expected-warning {{ordered comparison of function pointers}}
+  return function_pointers > function_pointers; // expected-warning {{ordered comparison of function pointers}}
+  return a == (void *) 0;
+  return a == (void *) 1; // expected-warning {{comparison of distinct pointer types}}
+}





More information about the cfe-commits mailing list