[Sema] Suppress -Wchar-subscripts if the index is a literal char

Assume that the user knows what they're doing if they provide a char
literal as an array index. This more closely matches the behavior of
GCC.

Differential Revision: https://reviews.llvm.org/D58896
This commit is contained in:
Edward Jones 2019-11-07 15:44:38 +00:00
parent 343597789e
commit 7adab7719e
2 changed files with 15 additions and 1 deletions

View File

@ -4741,7 +4741,8 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc,
if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) ||
IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U))
&& !IndexExpr->isTypeDependent())
&& !IndexExpr->isTypeDependent()
&& !isa<CharacterLiteral>(IndexExpr))
Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
// C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,

View File

@ -14,6 +14,19 @@ void t2() {
int val = subscript[array]; // expected-warning{{array subscript is of type 'char'}}
}
void t3() {
int array[50] = { 0 };
int val = array[' ']; // no warning, subscript is a literal
}
void t4() {
int array[50] = { 0 };
int val = '('[array]; // no warning, subscript is a literal
}
void t5() {
int array[50] = { 0 };
int val = array['\x11']; // no warning, subscript is a literal
}
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}}