[PATCH] D58896: Suppress -Wchar-subscripts if the index is a literal char

Edward Jones via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 4 02:35:49 PST 2019


edward-jones created this revision.
Herald added subscribers: cfe-commits, arphaman.
Herald added a project: clang.

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.


Repository:
  rC Clang

https://reviews.llvm.org/D58896

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


Index: test/SemaCXX/warn-char-subscripts.cpp
===================================================================
--- test/SemaCXX/warn-char-subscripts.cpp
+++ test/SemaCXX/warn-char-subscripts.cpp
@@ -14,6 +14,19 @@
   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}}
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -4718,7 +4718,8 @@
 
   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,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58896.189127.patch
Type: text/x-patch
Size: 1489 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190304/6c7bcb49/attachment.bin>


More information about the cfe-commits mailing list