[cfe-commits] r61275 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def lib/Sema/Sema.h lib/Sema/SemaExpr.cpp test/SemaCXX/bool.cpp www/cxx_status.html
Sebastian Redl
sebastian.redl at getdesigned.at
Sat Dec 20 01:35:38 PST 2008
Author: cornedbee
Date: Sat Dec 20 03:35:34 2008
New Revision: 61275
URL: http://llvm.org/viewvc/llvm-project?rev=61275&view=rev
Log:
Implement checks for bool in increment and decrement.
Modified:
cfe/trunk/include/clang/Basic/DiagnosticKinds.def
cfe/trunk/lib/Sema/Sema.h
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/test/SemaCXX/bool.cpp
cfe/trunk/www/cxx_status.html
Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=61275&r1=61274&r2=61275&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Sat Dec 20 03:35:34 2008
@@ -1298,6 +1298,10 @@
"cannot delete expression of type %0")
DIAG(warn_delete_incomplete, WARNING,
"deleting pointer to incomplete type %0 may cause undefined behaviour")
+DIAG(err_decrement_bool, ERROR,
+ "cannot decrement expression of type bool")
+DIAG(warn_increment_bool, WARNING,
+ "incrementing expression of type bool is deprecated")
DIAG(err_invalid_use_of_function_type, ERROR,
"a function type is not allowed here")
Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=61275&r1=61274&r2=61275&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Sat Dec 20 03:35:34 2008
@@ -1306,7 +1306,8 @@
/// 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);
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=61275&r1=61274&r2=61275&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sat Dec 20 03:35:34 2008
@@ -952,7 +952,8 @@
// 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 @@
/// 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 @@
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);
Modified: cfe/trunk/test/SemaCXX/bool.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/bool.cpp?rev=61275&r1=61274&r2=61275&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/bool.cpp (original)
+++ cfe/trunk/test/SemaCXX/bool.cpp Sat Dec 20 03:35:34 2008
@@ -5,3 +5,12 @@
ReadWrite = false,
ReadOnly = true
};
+
+// bool cannot be decremented, and gives a warning on increment
+void test(bool b)
+{
+ ++b; // expected-warning {{incrementing expression of type bool is deprecated}}
+ b++; // expected-warning {{incrementing expression of type bool is deprecated}}
+ --b; // expected-error {{cannot decrement expression of type bool}}
+ b--; // expected-error {{cannot decrement expression of type bool}}
+}
Modified: cfe/trunk/www/cxx_status.html
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/www/cxx_status.html?rev=61275&r1=61274&r2=61275&view=diff
==============================================================================
--- cfe/trunk/www/cxx_status.html (original)
+++ cfe/trunk/www/cxx_status.html Sat Dec 20 03:35:34 2008
@@ -548,9 +548,9 @@
<td> 5.2.6 [expr.post.incr]</td>
<td class="complete" align="center">✓</td>
<td class="complete" align="center">✓</td>
- <td class="advanced"></td>
+ <td class="complete" align="center">✓</td>
+ <td></td>
<td></td>
- <td>Decrement of bool is accepted, increment not warned about</td>
</tr>
<tr>
<td> 5.2.7 [expr.dynamic.cast]</td>
@@ -646,9 +646,9 @@
<td> 5.3.2 [expr.pre.incr]</td>
<td class="complete" align="center">✓</td>
<td class="complete" align="center">✓</td>
- <td class="advanced"></td>
+ <td class="complete" align="center">✓</td>
+ <td></td>
<td></td>
- <td>Decrement of bool is accepted, increment not warned about</td>
</tr>
<tr>
<td> 5.3.3 [expr.sizeof]</td>
More information about the cfe-commits
mailing list