[cfe-commits] r41408 - in /cfe/trunk: Sema/SemaExpr.cpp test/Sema/compare.c

Chris Lattner sabre at nondot.org
Sat Aug 25 18:10:15 PDT 2007


Author: lattner
Date: Sat Aug 25 20:10:14 2007
New Revision: 41408

URL: http://llvm.org/viewvc/llvm-project?rev=41408&view=rev
Log:
Cases like this:
  char *C;
  C != ((void*)0);

Should not warn about incompatible pointer types.  Also, make sure to
insert an implicit conversion even if the operand is null.


Added:
    cfe/trunk/test/Sema/compare.c
Modified:
    cfe/trunk/Sema/SemaExpr.cpp

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

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Sat Aug 25 20:10:14 2007
@@ -1068,33 +1068,35 @@
   if (lType->isRealType() && rType->isRealType())
     return Context.IntTy;
   
+  bool LHSIsNull = lex->isNullPointerConstant(Context);
+  bool RHSIsNull = rex->isNullPointerConstant(Context);
+  
   // All of the following pointer related warnings are GCC extensions. One
   // day, we can consider making them errors (when -pedantic-errors is enabled).
   if (lType->isPointerType() && rType->isPointerType()) {
-    if (!Type::pointerTypesAreCompatible(lType, rType)) {
+    if (!LHSIsNull && !RHSIsNull &&
+        !Type::pointerTypesAreCompatible(lType, rType)) {
       Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
            lType.getAsString(), rType.getAsString(),
            lex->getSourceRange(), rex->getSourceRange());
-      promoteExprToType(rex, lType); // promote the pointer to pointer
     }
+    promoteExprToType(rex, lType); // promote the pointer to pointer
     return Context.IntTy;
   }
   if (lType->isPointerType() && rType->isIntegerType()) {
-    if (!rex->isNullPointerConstant(Context)) {
+    if (!RHSIsNull)
       Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
            lType.getAsString(), rType.getAsString(),
            lex->getSourceRange(), rex->getSourceRange());
-      promoteExprToType(rex, lType); // promote the integer to pointer
-    }
+    promoteExprToType(rex, lType); // promote the integer to pointer
     return Context.IntTy;
   }
   if (lType->isIntegerType() && rType->isPointerType()) {
-    if (!lex->isNullPointerConstant(Context)) {
+    if (!LHSIsNull)
       Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
            lType.getAsString(), rType.getAsString(),
            lex->getSourceRange(), rex->getSourceRange());
-      promoteExprToType(lex, rType); // promote the integer to pointer
-    }
+    promoteExprToType(lex, rType); // promote the integer to pointer
     return Context.IntTy;
   }
   InvalidOperands(loc, lex, rex);
@@ -1117,33 +1119,37 @@
   if (lType->isArithmeticType() && rType->isArithmeticType())
     return Context.IntTy;
     
+  bool LHSIsNull = lex->isNullPointerConstant(Context);
+  bool RHSIsNull = rex->isNullPointerConstant(Context);
+
   // All of the following pointer related warnings are GCC extensions. One
   // day, we can consider making them errors (when -pedantic-errors is enabled).
   if (lType->isPointerType() && rType->isPointerType()) {
-    if (!Type::pointerTypesAreCompatible(lType, rType)) {
+    if (!LHSIsNull && !RHSIsNull &&
+        !Type::pointerTypesAreCompatible(lType, rType)) {
       Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
            lType.getAsString(), rType.getAsString(),
            lex->getSourceRange(), rex->getSourceRange());
-      promoteExprToType(rex, lType); // promote the pointer to pointer
     }
+    promoteExprToType(rex, lType); // promote the pointer to pointer
     return Context.IntTy;
   }
   if (lType->isPointerType() && rType->isIntegerType()) {
-    if (!rex->isNullPointerConstant(Context)) {
+    if (!RHSIsNull) {
       Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
            lType.getAsString(), rType.getAsString(),
            lex->getSourceRange(), rex->getSourceRange());
-      promoteExprToType(rex, lType); // promote the integer to pointer
     }
+    promoteExprToType(rex, lType); // promote the integer to pointer
     return Context.IntTy;
   }
   if (lType->isIntegerType() && rType->isPointerType()) {
-    if (!lex->isNullPointerConstant(Context)) {
+    if (!LHSIsNull) {
       Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
            lType.getAsString(), rType.getAsString(),
            lex->getSourceRange(), rex->getSourceRange());
-      promoteExprToType(lex, rType); // promote the integer to pointer
     }
+    promoteExprToType(lex, rType); // promote the integer to pointer
     return Context.IntTy;
   }
   InvalidOperands(loc, lex, rex);

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

==============================================================================
--- cfe/trunk/test/Sema/compare.c (added)
+++ cfe/trunk/test/Sema/compare.c Sat Aug 25 20:10:14 2007
@@ -0,0 +1,8 @@
+// RUN: clang -parse-ast-check %s
+
+int test(char *C) { // nothing here should warn.
+  return C != ((void*)0);
+  return C != (void*)0;
+  return C != 0;
+}
+





More information about the cfe-commits mailing list