[cfe-commits] r81747 - in /cfe/trunk: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/Sema/warn-char-subscripts.c test/SemaCXX/warn-char-subscripts.cpp

Sam Weinig sam.weinig at gmail.com
Sun Sep 13 18:58:58 PDT 2009


Author: weinig
Date: Sun Sep 13 20:58:58 2009
New Revision: 81747

URL: http://llvm.org/viewvc/llvm-project?rev=81747&view=rev
Log:
Add support for -Wchar-subscripts. Fixes PR4801.

Added:
    cfe/trunk/test/Sema/warn-char-subscripts.c
    cfe/trunk/test/SemaCXX/warn-char-subscripts.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticGroups.td
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=81747&r1=81746&r2=81747&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Sun Sep 13 20:58:58 2009
@@ -106,6 +106,7 @@
 def VectorConversions : DiagGroup<"vector-conversions">;      // clang specific
 def VolatileRegisterVar : DiagGroup<"volatile-register-var">;
 def : DiagGroup<"write-strings">;
+def CharSubscript : DiagGroup<"char-subscripts">;
 
 // Aggregation warning settings.
 
@@ -142,7 +143,8 @@
     UnusedVariable,
     VectorConversions,
     VolatileRegisterVar,
-    Reorder
+    Reorder,
+    CharSubscript
  ]>;
 
 // -Wall is -Wmost -Wparentheses

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=81747&r1=81746&r2=81747&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Sun Sep 13 20:58:58 2009
@@ -1319,6 +1319,8 @@
   "cannot refer to member %0 with '%select{.|->}1'">;
 def note_member_reference_needs_call : Note<
   "perhaps you meant to call this function with '()'?">;
+def warn_subscript_is_char : Warning<"array subscript is of type 'char'">,
+  InGroup<CharSubscript>, DefaultIgnore;
 
 def err_typecheck_incomplete_tag : Error<"incomplete definition of type %0">;
 def err_typecheck_no_member_deprecated : Error<"no member named %0">;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=81747&r1=81746&r2=81747&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sun Sep 13 20:58:58 2009
@@ -1809,6 +1809,9 @@
     return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer)
                      << IndexExpr->getSourceRange());
 
+  if (IndexExpr->getType()->isCharType() && !IndexExpr->isTypeDependent())
+    Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+
   // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
   // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
   // type. Note that Functions are not objects, and that (in C99 parlance)

Added: cfe/trunk/test/Sema/warn-char-subscripts.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-char-subscripts.c?rev=81747&view=auto

==============================================================================
--- cfe/trunk/test/Sema/warn-char-subscripts.c (added)
+++ cfe/trunk/test/Sema/warn-char-subscripts.c Sun Sep 13 20:58:58 2009
@@ -0,0 +1,31 @@
+// RUN: clang-cc -Wchar-subscripts -fsyntax-only -verify %s
+
+void t1() {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t2() {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t3() {
+  int *array = 0;
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t4() {
+  int *array = 0;
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+char returnsChar();
+void t5() {
+  int *array = 0;
+  int val = array[returnsChar()]; // expected-warning{{array subscript is of type 'char'}}
+}

Added: cfe/trunk/test/SemaCXX/warn-char-subscripts.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-char-subscripts.cpp?rev=81747&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/warn-char-subscripts.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-char-subscripts.cpp Sun Sep 13 20:58:58 2009
@@ -0,0 +1,21 @@
+// RUN: clang-cc -Wchar-subscripts -fsyntax-only -verify %s
+
+template<typename T>
+void t1() {
+  int array[1] = { 0 };
+  T subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+template<typename T>
+void t2() {
+  int array[1] = { 0 };
+  T subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void test() {
+  t1<char>(); // expected-note {{in instantiation of function template specialization 't1<char>' requested here}}
+  t2<char>(); // expected-note {{in instantiation of function template specialization 't2<char>' requested here}}
+}
+





More information about the cfe-commits mailing list