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

Edward Jones via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 7 07:49:00 PST 2019


Author: Edward Jones
Date: 2019-11-07T15:45:44Z
New Revision: 7adab7719e55e1b29bfd521dcc73f202139e8f41

URL: https://github.com/llvm/llvm-project/commit/7adab7719e55e1b29bfd521dcc73f202139e8f41
DIFF: https://github.com/llvm/llvm-project/commit/7adab7719e55e1b29bfd521dcc73f202139e8f41.diff

LOG: [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

Added: 
    

Modified: 
    clang/lib/Sema/SemaExpr.cpp
    clang/test/SemaCXX/warn-char-subscripts.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index e41cd5b6653a..2842e7199b1d 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<CharacterLiteral>(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 84ea536b979e..7760ed0e5eba 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<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}}


        


More information about the cfe-commits mailing list