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,


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,
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to