From af199f32b008191341f248d7d50616a6a71ba354 Mon Sep 17 00:00:00 2001 From: Mike Stump Date: Thu, 7 May 2009 18:43:07 +0000 Subject: [PATCH] Tighten up relationals with blocks and ints. Radar 6441502 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@71171 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaExpr.cpp | 32 +++++++++++++------------------- test/Sema/block-misc.c | 8 ++++++++ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 87bc037c58..79c471a2d4 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -2795,8 +2795,10 @@ QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, // pointer constants in case both operands are null pointer constants. if ((LHSPT || LHSBPT) && (RHSPT || RHSBPT)) { // C99 6.5.15p3,6 // get the "pointed to" types - QualType lhptee = LHSPT ? LHSPT->getPointeeType() : LHSBPT->getPointeeType(); - QualType rhptee = RHSPT ? RHSPT->getPointeeType() : RHSBPT->getPointeeType(); + QualType lhptee = (LHSPT ? LHSPT->getPointeeType() + : LHSBPT->getPointeeType()); + QualType rhptee = (RHSPT ? RHSPT->getPointeeType() + : RHSBPT->getPointeeType()); // ignore qualifiers on void (C99 6.5.15p3, clause 6) if (lhptee->isVoidType() @@ -3518,7 +3520,8 @@ QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, // Enforce type constraints: C99 6.5.6p3. // Handle the common case first (both operands are arithmetic). - if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) { + if (lex->getType()->isArithmeticType() + && rex->getType()->isArithmeticType()) { if (CompLHSTy) *CompLHSTy = compType; return compType; } @@ -3680,7 +3683,8 @@ QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, QualType lType = lex->getType(); QualType rType = rex->getType(); - if (!lType->isFloatingType() && !(lType->isBlockPointerType() && isRelational)) { + if (!lType->isFloatingType() + && !(lType->isBlockPointerType() && isRelational)) { // For non-floating point types, check for self-comparisons of the form // x == x, x != x, x < x, etc. These always evaluate to a constant, and // often indicate logic errors in the program. @@ -3880,23 +3884,13 @@ QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, return ResultTy; } // Handle block pointers. - if (lType->isBlockPointerType() && rType->isIntegerType()) { - if (isRelational) - Diag(Loc, diag::err_typecheck_invalid_operands) - << lType << rType << lex->getSourceRange() << rex->getSourceRange(); - else if (!RHSIsNull) - Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) - << lType << rType << lex->getSourceRange() << rex->getSourceRange(); + if (!isRelational && RHSIsNull + && lType->isBlockPointerType() && rType->isIntegerType()) { ImpCastExprToType(rex, lType); // promote the integer to pointer return ResultTy; } - if (lType->isIntegerType() && rType->isBlockPointerType()) { - if (isRelational) - Diag(Loc, diag::err_typecheck_invalid_operands) - << lType << rType << lex->getSourceRange() << rex->getSourceRange(); - else if (!LHSIsNull) - Diag(Loc, diag::ext_typecheck_comparison_of_pointer_integer) - << lType << rType << lex->getSourceRange() << rex->getSourceRange(); + if (!isRelational && LHSIsNull + && lType->isIntegerType() && rType->isBlockPointerType()) { ImpCastExprToType(lex, rType); // promote the integer to pointer return ResultTy; } @@ -4931,7 +4925,7 @@ void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { } void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { - assert(ParamInfo.getIdentifier() == 0 && "block-id should have no identifier!"); + assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); ProcessDeclAttributes(CurBlock->TheDecl, ParamInfo); diff --git a/test/Sema/block-misc.c b/test/Sema/block-misc.c index 9eda46420a..6c62376dc4 100644 --- a/test/Sema/block-misc.c +++ b/test/Sema/block-misc.c @@ -171,4 +171,12 @@ void test17() { (void)(bp > bp); // expected-error {{invalid operands}} (void)(bp > vp); // expected-error {{invalid operands}} f(1 ? bp : rp); // expected-error {{incompatible operand types ('void (^)(int)' and 'void (*)(int)')}} + (void)(bp == 1); // expected-error {{invalid operands to binary expression}} + (void)(bp == 0); + (void)(1 == bp); // expected-error {{invalid operands to binary expression}} + (void)(0 == bp); + (void)(bp < 1); // expected-error {{invalid operands to binary expression}} + (void)(bp < 0); // expected-error {{invalid operands to binary expression}} + (void)(1 < bp); // expected-error {{invalid operands to binary expression}} + (void)(0 < bp); // expected-error {{invalid operands to binary expression}} } -- 2.40.0