Emit an error for enum increments and decrements in C++ mode.

Fixes PR16394.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@187955 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Richard Trieu 2013-08-08 01:50:23 +00:00
parent 9793fe99de
commit fbbdc5daee
4 changed files with 35 additions and 0 deletions

View File

@ -4882,6 +4882,8 @@ def note_member_declared_here : Note<
def err_decrement_bool : Error<"cannot decrement expression of type bool">;
def warn_increment_bool : Warning<
"incrementing expression of type bool is deprecated">, InGroup<Deprecated>;
def err_increment_decrement_enum : Error<
"cannot %select{decrement|increment}0 expression of enum type %1">;
def err_catch_incomplete_ptr : Error<
"cannot catch pointer to incomplete type %0">;
def err_catch_incomplete_ref : Error<

View File

@ -8377,6 +8377,10 @@ static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op,
}
// Increment of bool sets it to true, but is deprecated.
S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange();
} else if (S.getLangOpts().CPlusPlus && ResType->isEnumeralType()) {
// Error on enum increments and decrements in C++ mode
S.Diag(OpLoc, diag::err_increment_decrement_enum) << IsInc << ResType;
return QualType();
} else if (ResType->isRealType()) {
// OK!
} else if (ResType->isPointerType()) {

View File

@ -0,0 +1,13 @@
// RUN: %clang_cc1 -fsyntax-only %s -verify
// expected-no-diagnostics
enum A { A1, A2, A3 };
typedef enum A A;
void test() {
A a;
a++;
a--;
++a;
--a;
a = a + 1;
a = a - 1;
}

View File

@ -0,0 +1,16 @@
// RUN: %clang_cc1 -fsyntax-only %s -verify
enum A { A1, A2, A3 };
void test() {
A a;
a++; // expected-error{{cannot increment expression of enum type 'A'}}
a--; // expected-error{{cannot decrement expression of enum type 'A'}}
++a; // expected-error{{cannot increment expression of enum type 'A'}}
--a; // expected-error{{cannot decrement expression of enum type 'A'}}
}
enum B {B1, B2};
inline B &operator++ (B &b) { b = B((int)b+1); return b; }
inline B operator++ (B &b, int) { B ret = b; ++b; return b; }
void foo(enum B b) { ++b; b++; }