diff options
| author | Sebastian Redl <sebastian.redl@getdesigned.at> | 2008-12-20 09:35:34 +0000 |
|---|---|---|
| committer | Sebastian Redl <sebastian.redl@getdesigned.at> | 2008-12-20 09:35:34 +0000 |
| commit | e10c2c32af55dc7a2a52d4af9b17f8d26eef1ecb (patch) | |
| tree | 96d14d0f8a2ada752dfa5c83598c3013373a5841 /clang/lib | |
| parent | af7415ffb1b01b91f9abf190f32766514156db5a (diff) | |
| download | bcm5719-llvm-e10c2c32af55dc7a2a52d4af9b17f8d26eef1ecb.tar.gz bcm5719-llvm-e10c2c32af55dc7a2a52d4af9b17f8d26eef1ecb.zip | |
Implement checks for bool in increment and decrement.
llvm-svn: 61275
Diffstat (limited to 'clang/lib')
| -rw-r--r-- | clang/lib/Sema/Sema.h | 3 | ||||
| -rw-r--r-- | clang/lib/Sema/SemaExpr.cpp | 20 |
2 files changed, 17 insertions, 6 deletions
diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h index 56b681c0705..9cab3308c57 100644 --- a/clang/lib/Sema/Sema.h +++ b/clang/lib/Sema/Sema.h @@ -1306,7 +1306,8 @@ public: /// type checking unary operators (subroutines of ActOnUnaryOp). /// C99 6.5.3.1, 6.5.3.2, 6.5.3.4 - QualType CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc); + QualType CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc, + bool isInc); QualType CheckAddressOfOperand(Expr *op, SourceLocation OpLoc); QualType CheckIndirectionOperand(Expr *op, SourceLocation OpLoc); QualType CheckRealImagOperand(Expr *&Op, SourceLocation OpLoc); diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index da0354886c1..51e4e05eec8 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -952,7 +952,8 @@ Action::ExprResult Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, // build a built-in operation. } - QualType result = CheckIncrementDecrementOperand(Arg, OpLoc); + QualType result = CheckIncrementDecrementOperand(Arg, OpLoc, + Opc == UnaryOperator::PostInc); if (result.isNull()) return true; return new UnaryOperator(Arg, Opc, result, OpLoc); @@ -2762,12 +2763,20 @@ QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. -QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc) { +QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, + bool isInc) { QualType ResType = Op->getType(); assert(!ResType.isNull() && "no type for increment/decrement expression"); - // C99 6.5.2.4p1: We allow complex as a GCC extension. - if (ResType->isRealType()) { + if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { + // Decrement of bool is not allowed. + if (!isInc) { + Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); + return QualType(); + } + // Increment of bool sets it to true, but is deprecated. + Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); + } else if (ResType->isRealType()) { // OK! } else if (const PointerType *PT = ResType->getAsPointerType()) { // C99 6.5.2.4p2, 6.5.6p2 @@ -3350,7 +3359,8 @@ Action::ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, assert(0 && "Unimplemented unary expr!"); case UnaryOperator::PreInc: case UnaryOperator::PreDec: - resultType = CheckIncrementDecrementOperand(Input, OpLoc); + resultType = CheckIncrementDecrementOperand(Input, OpLoc, + Opc == UnaryOperator::PreInc); break; case UnaryOperator::AddrOf: resultType = CheckAddressOfOperand(Input, OpLoc); |

