]> granicus.if.org Git - clang/commitdiff
Sema::CheckConditionalOperands(): Soften pointer/integer mismatch from error->warning.
authorSteve Naroff <snaroff@apple.com>
Wed, 8 Apr 2009 17:05:15 +0000 (17:05 +0000)
committerSteve Naroff <snaroff@apple.com>
Wed, 8 Apr 2009 17:05:15 +0000 (17:05 +0000)
Fixes <rdar://problem/6762239> [sema] gcc incompatibility; error on incompatible operand types in ?:.

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExpr.cpp
test/Sema/conditional-expr.c

index 4dbd12dd9fd0e88e8f8d09a04d9115b5b575d384..3245323a74ba8e2791b94526e83a26e59d9ff789 100644 (file)
@@ -1207,6 +1207,8 @@ def err_typecheck_cond_incompatible_operands : Error<
   "incompatible operand types (%0 and %1)">;
 def warn_typecheck_cond_incompatible_pointers : Warning<
   "pointer type mismatch (%0 and %1)">;
+def warn_typecheck_cond_pointer_integer_mismatch : Warning<
+  "pointer/integer type mismatch in conditional expression (%0 and %1)">;
 def err_typecheck_choose_expr_requires_constant : Error<
   "'__builtin_choose_expr' requires a constant expression">;
 def ext_typecheck_expression_not_constant_but_accepted : Extension<
index d442b1fc583793a4b3a1547271d43e300e74e0f4..4f8ad5b4a5e8866ce5d9e37b60bfc3ce28707177 100644 (file)
@@ -2714,6 +2714,20 @@ QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS,
     }
   }
 
+  // GCC compatibility: soften pointer/integer mismatch.
+  if (RHSTy->isPointerType() && LHSTy->isIntegerType()) {
+    Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch)
+      << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange();
+    ImpCastExprToType(LHS, RHSTy); // promote the integer to a pointer.
+    return RHSTy;
+  }
+  if (LHSTy->isPointerType() && RHSTy->isIntegerType()) {
+    Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch)
+      << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange();
+    ImpCastExprToType(RHS, LHSTy); // promote the integer to a pointer.
+    return LHSTy;
+  }
+
   // Selection between block pointer types is ok as long as they are the same.
   if (LHSTy->isBlockPointerType() && RHSTy->isBlockPointerType() &&
       Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy))
index c068113e9b8ab2249c72b7055188d217774ff21c..1f0a9deb5e47d84cbef80a01bde85a252a006eb5 100644 (file)
@@ -40,3 +40,12 @@ int Postgresql() {
   char x;
   return ((((&x) != ((void *) 0)) ? (*(&x) = ((char) 1)) : (void) ((void *) 0)), (unsigned long) ((void *) 0)); // expected-warning {{C99 forbids conditional expressions with only one void side}}
 }
+
+#define nil ((void*) 0)
+
+extern int f1(void);
+
+int f0(int a) {
+  // GCC considers this a warning.
+  return a ? f1() : nil; // expected-warning {{pointer/integer type mismatch in conditional expression ('int' and 'void *')}} expected-warning {{incompatible pointer to integer conversion returning 'void *', expected 'int'}}
+}