[PATCH] Make Clang error on enum increment/decrement in C++

Richard Trieu rtrieu at google.com
Mon Aug 5 18:24:38 PDT 2013


  Add more test cases for enum increment/decrement.

http://llvm-reviews.chandlerc.com/D1205

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1205?vs=2979&id=3210#toc

Files:
  lib/Sema/SemaExpr.cpp
  test/Sema/enum-increment.c
  test/SemaCXX/enum-increment.cpp
  include/clang/Basic/DiagnosticSemaKinds.td

Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -8351,6 +8351,10 @@
     }
     // 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()) {
Index: test/Sema/enum-increment.c
===================================================================
--- test/Sema/enum-increment.c
+++ 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;
+}
Index: test/SemaCXX/enum-increment.cpp
===================================================================
--- test/SemaCXX/enum-increment.cpp
+++ 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 type 'A'}}
+  a--;  // expected-error{{cannot decrement expression of type 'A'}}
+  ++a;  // expected-error{{cannot increment expression of type 'A'}}
+  --a;  // expected-error{{cannot decrement expression of 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++; }
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -4843,6 +4843,8 @@
 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 type %1">;
 def err_catch_incomplete_ptr : Error<
   "cannot catch pointer to incomplete type %0">;
 def err_catch_incomplete_ref : Error<
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1205.2.patch
Type: text/x-patch
Size: 2435 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130805/f474949e/attachment.bin>


More information about the cfe-commits mailing list