aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/clang/Basic/DiagnosticGroups.td4
-rw-r--r--include/clang/Basic/DiagnosticSemaKinds.td2
-rw-r--r--lib/Sema/SemaExpr.cpp3
-rw-r--r--test/Sema/warn-char-subscripts.c31
-rw-r--r--test/SemaCXX/warn-char-subscripts.cpp21
5 files changed, 60 insertions, 1 deletions
diff --git a/include/clang/Basic/DiagnosticGroups.td b/include/clang/Basic/DiagnosticGroups.td
index d00ad6915f..0901636335 100644
--- a/include/clang/Basic/DiagnosticGroups.td
+++ b/include/clang/Basic/DiagnosticGroups.td
@@ -106,6 +106,7 @@ def : DiagGroup<"variadic-macros">;
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 @@ def Most : DiagGroup<"most", [
UnusedVariable,
VectorConversions,
VolatileRegisterVar,
- Reorder
+ Reorder,
+ CharSubscript
]>;
// -Wall is -Wmost -Wparentheses
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index ab12463cc0..db60455873 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1319,6 +1319,8 @@ def err_typecheck_member_reference_unknown : Error<
"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">;
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 11bd323011..32d3e06e3e 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -1809,6 +1809,9 @@ Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc,
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)
diff --git a/test/Sema/warn-char-subscripts.c b/test/Sema/warn-char-subscripts.c
new file mode 100644
index 0000000000..65a34e8fc2
--- /dev/null
+++ b/test/Sema/warn-char-subscripts.c
@@ -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'}}
+}
diff --git a/test/SemaCXX/warn-char-subscripts.cpp b/test/SemaCXX/warn-char-subscripts.cpp
new file mode 100644
index 0000000000..1c06db91c3
--- /dev/null
+++ b/test/SemaCXX/warn-char-subscripts.cpp
@@ -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}}
+}
+