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.
UnusedVariable,
VectorConversions,
VolatileRegisterVar,
- Reorder
+ Reorder,
+ CharSubscript
]>;
// -Wall is -Wmost -Wparentheses
"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">;
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)
--- /dev/null
+// 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'}}
+}
--- /dev/null
+// 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}}
+}
+