From 7adab7719e55e1b29bfd521dcc73f202139e8f41 Mon Sep 17 00:00:00 2001 From: Edward Jones Date: Thu, 7 Nov 2019 15:44:38 +0000 Subject: [PATCH] [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 --- clang/lib/Sema/SemaExpr.cpp | 3 ++- clang/test/SemaCXX/warn-char-subscripts.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index e41cd5b..2842e71 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -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(IndexExpr)) 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/clang/test/SemaCXX/warn-char-subscripts.cpp b/clang/test/SemaCXX/warn-char-subscripts.cpp index 84ea536..7760ed0 100644 --- a/clang/test/SemaCXX/warn-char-subscripts.cpp +++ b/clang/test/SemaCXX/warn-char-subscripts.cpp @@ -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(); // expected-note {{in instantiation of function template specialization 't1' requested here}} t2(); // expected-note {{in instantiation of function template specialization 't2' requested here}} -- 2.7.4