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

Richard Trieu rtrieu at google.com
Wed Aug 7 18:50:24 PDT 2013


Author: rtrieu
Date: Wed Aug  7 20:50:23 2013
New Revision: 187955

URL: http://llvm.org/viewvc/llvm-project?rev=187955&view=rev
Log:
Emit an error for enum increments and decrements in C++ mode.
Fixes PR16394.

Added:
    cfe/trunk/test/Sema/enum-increment.c
    cfe/trunk/test/SemaCXX/enum-increment.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=187955&r1=187954&r2=187955&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Aug  7 20:50:23 2013
@@ -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<

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=187955&r1=187954&r2=187955&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Aug  7 20:50:23 2013
@@ -8377,6 +8377,10 @@ static QualType CheckIncrementDecrementO
     }
     // 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()) {

Added: cfe/trunk/test/Sema/enum-increment.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/enum-increment.c?rev=187955&view=auto
==============================================================================
--- cfe/trunk/test/Sema/enum-increment.c (added)
+++ cfe/trunk/test/Sema/enum-increment.c Wed Aug  7 20:50:23 2013
@@ -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;
+}

Added: cfe/trunk/test/SemaCXX/enum-increment.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/enum-increment.cpp?rev=187955&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/enum-increment.cpp (added)
+++ cfe/trunk/test/SemaCXX/enum-increment.cpp Wed Aug  7 20:50:23 2013
@@ -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++; }





More information about the cfe-commits mailing list