From: Richard Trieu Date: Thu, 8 Aug 2013 01:50:23 +0000 (+0000) Subject: Emit an error for enum increments and decrements in C++ mode. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fbbdc5daee4dc772d4d137080890fd79492592d6;p=clang 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 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 12ddc6b8a1..20b2a40cb0 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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; +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< diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 8038a4502a..8515cb2f53 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -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()) { diff --git a/test/Sema/enum-increment.c b/test/Sema/enum-increment.c new file mode 100644 index 0000000000..baaa3489b9 --- /dev/null +++ b/test/Sema/enum-increment.c @@ -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; +} diff --git a/test/SemaCXX/enum-increment.cpp b/test/SemaCXX/enum-increment.cpp new file mode 100644 index 0000000000..dc1a921852 --- /dev/null +++ b/test/SemaCXX/enum-increment.cpp @@ -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++; }