From 9158804749356f88be8c5a3bade75b761846273c Mon Sep 17 00:00:00 2001 From: Steve Naroff <snaroff@apple.com> Date: Wed, 8 Apr 2009 17:05:15 +0000 Subject: [PATCH] Sema::CheckConditionalOperands(): Soften pointer/integer mismatch from error->warning. 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 | 2 ++ lib/Sema/SemaExpr.cpp | 14 ++++++++++++++ test/Sema/conditional-expr.c | 9 +++++++++ 3 files changed, 25 insertions(+) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 4dbd12dd9f..3245323a74 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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< diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index d442b1fc58..4f8ad5b4a5 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -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)) diff --git a/test/Sema/conditional-expr.c b/test/Sema/conditional-expr.c index c068113e9b..1f0a9deb5e 100644 --- a/test/Sema/conditional-expr.c +++ b/test/Sema/conditional-expr.c @@ -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'}} +} -- 2.40.0