-Wchar-subscripts should not warn for unsigned char subscripts. Fixes PR4978.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@81776 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 32d3e06..4967599 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -1809,7 +1809,9 @@
return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer)
<< IndexExpr->getSourceRange());
- if (IndexExpr->getType()->isCharType() && !IndexExpr->isTypeDependent())
+ QualType IndexTy = Context.getCanonicalType(IndexExpr->getType());
+ if ((IndexTy == Context.CharTy || IndexTy == Context.SignedCharTy)
+ && !IndexExpr->isTypeDependent())
Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
// C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
diff --git a/test/Sema/warn-char-subscripts.c b/test/Sema/warn-char-subscripts.c
index 65a34e8..972393c 100644
--- a/test/Sema/warn-char-subscripts.c
+++ b/test/Sema/warn-char-subscripts.c
@@ -29,3 +29,36 @@
int *array = 0;
int val = array[returnsChar()]; // expected-warning{{array subscript is of type 'char'}}
}
+
+void t6() {
+ int array[1] = { 0 };
+ signed char subscript = 0;
+ int val = array[subscript]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t7() {
+ int array[1] = { 0 };
+ unsigned char subscript = 0;
+ int val = array[subscript]; // no warning for unsigned char
+}
+
+typedef char CharTy;
+void t8() {
+ int array[1] = { 0 };
+ CharTy subscript = 0;
+ int val = array[subscript]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+typedef signed char SignedCharTy;
+void t9() {
+ int array[1] = { 0 };
+ SignedCharTy subscript = 0;
+ int val = array[subscript]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+typedef unsigned char UnsignedCharTy;
+void t10() {
+ int array[1] = { 0 };
+ UnsignedCharTy subscript = 0;
+ int val = array[subscript]; // no warning for unsigned char
+}