[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-12-03 Thread via cfe-commits

cor3ntin wrote:

Thanks for your first contribution. Maybe the first of many :D

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-12-03 Thread via cfe-commits

https://github.com/cor3ntin closed 
https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-12-03 Thread via cfe-commits

wheatman wrote:

@cor3ntin done, thanks for taking a look 

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-12-03 Thread via cfe-commits

https://github.com/wheatman updated 
https://github.com/llvm/llvm-project/pull/69061

>From df15b6239557edaae8b3d7f63915bad494ad674f Mon Sep 17 00:00:00 2001
From: Brian Wheatman 
Date: Sat, 14 Oct 2023 12:02:19 -0400
Subject: [PATCH] Remove warnings from -Wchar-subscripts for known positive
 constants

---
 clang/docs/ReleaseNotes.rst  |   2 +
 clang/lib/Sema/SemaExpr.cpp  |  11 ++-
 clang/test/Sema/warn-char-subscripts.c   |  25 ++
 clang/test/Sema/warn-char-subscripts.cpp | 103 +++
 4 files changed, 138 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/warn-char-subscripts.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c7a948fd3fae5..683d0026bb345 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -651,6 +651,8 @@ Bug Fixes in This Version
 - Fixed false positive error emitted by clang when performing qualified name
   lookup and the current class instantiation has dependent bases.
   Fixes (`#13826 `_)
+- Clang's ``-Wchar-subscripts`` no longer warns on chars whose values are 
known non-negative constants.
+  Fixes (`#18763 `_)
 
 Bug Fixes to Compiler Builtins
 ^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index d1b2b8084b8ff..d629be083d8c3 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6053,9 +6053,14 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, 
SourceLocation LLoc,
  << IndexExpr->getSourceRange());
 
   if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) ||
-   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U))
- && !IndexExpr->isTypeDependent())
-Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) &&
+  !IndexExpr->isTypeDependent()) {
+std::optional IntegerContantExpr =
+IndexExpr->getIntegerConstantExpr(getASTContext());
+if (!IntegerContantExpr.has_value() ||
+IntegerContantExpr.value().isNegative())
+  Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+  }
 
   // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
   // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
diff --git a/clang/test/Sema/warn-char-subscripts.c 
b/clang/test/Sema/warn-char-subscripts.c
index 2e72d90fa612a..0a012f68feae0 100644
--- a/clang/test/Sema/warn-char-subscripts.c
+++ b/clang/test/Sema/warn-char-subscripts.c
@@ -62,3 +62,28 @@ void t10(void) {
   UnsignedCharTy subscript = 0;
   int val = array[subscript]; // no warning for unsigned char
 }
+
+void t11(void) {
+  int array[256] = { 0 };
+  int val = array['a']; // no warning for char with known positive value
+}
+
+void t12(void) {
+  int array[256] = { 0 };
+  char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t13(void) {
+  int array[256] = { 0 };
+  const char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t14(void) {
+  int array[256] = { 0 }; // expected-note {{array 'array' declared here}}
+  const char b = -1;
+  // expected-warning@+2 {{array subscript is of type 'char'}}
+  // expected-warning@+1 {{array index -1 is before the beginning of the 
array}}
+  int val = array[b];
+}
diff --git a/clang/test/Sema/warn-char-subscripts.cpp 
b/clang/test/Sema/warn-char-subscripts.cpp
new file mode 100644
index 0..929fb372c5173
--- /dev/null
+++ b/clang/test/Sema/warn-char-subscripts.cpp
@@ -0,0 +1,103 @@
+// RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s
+
+void t1(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t2(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t3(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t4(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+char returnsChar(void);
+void t5(void) {
+  int *array = 0;
+  int val = array[returnsChar()]; // expected-warning{{array subscript is of 
type 'char'}}
+}
+
+void t6(void) {
+  int array[1] = { 0 };
+  signed char subscript = 0;
+  int val = array[subscript]; // no warning for explicit signed char
+}
+
+void t7(void) {
+  int array[1] = { 0 };
+  unsigned char subscript = 0;
+  int val = array[subscript]; // no warning for unsigned char
+}
+
+typedef char CharTy;
+void t8(void) {
+  int array[1] 

[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-12-03 Thread via cfe-commits

cor3ntin wrote:

@wheatman can you rebase again? I want to make sure CI passes before merging 
(and the bots were broken earlier today). Thanks.
LGTM otherwise

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-12-02 Thread via cfe-commits

https://github.com/wheatman updated 
https://github.com/llvm/llvm-project/pull/69061

>From 894e8623689abe615600d768ef4fbedea78ab799 Mon Sep 17 00:00:00 2001
From: Brian Wheatman 
Date: Sat, 14 Oct 2023 12:02:19 -0400
Subject: [PATCH] Remove warnings from -Wchar-subscripts for known positive
 constants

---
 clang/docs/ReleaseNotes.rst  |   2 +
 clang/lib/Sema/SemaExpr.cpp  |  11 ++-
 clang/test/Sema/warn-char-subscripts.c   |  25 ++
 clang/test/Sema/warn-char-subscripts.cpp | 103 +++
 4 files changed, 138 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/warn-char-subscripts.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c7a948fd3fae5..683d0026bb345 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -651,6 +651,8 @@ Bug Fixes in This Version
 - Fixed false positive error emitted by clang when performing qualified name
   lookup and the current class instantiation has dependent bases.
   Fixes (`#13826 `_)
+- Clang's ``-Wchar-subscripts`` no longer warns on chars whose values are 
known non-negative constants.
+  Fixes (`#18763 `_)
 
 Bug Fixes to Compiler Builtins
 ^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index d1b2b8084b8ff..d629be083d8c3 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6053,9 +6053,14 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, 
SourceLocation LLoc,
  << IndexExpr->getSourceRange());
 
   if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) ||
-   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U))
- && !IndexExpr->isTypeDependent())
-Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) &&
+  !IndexExpr->isTypeDependent()) {
+std::optional IntegerContantExpr =
+IndexExpr->getIntegerConstantExpr(getASTContext());
+if (!IntegerContantExpr.has_value() ||
+IntegerContantExpr.value().isNegative())
+  Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+  }
 
   // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
   // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
diff --git a/clang/test/Sema/warn-char-subscripts.c 
b/clang/test/Sema/warn-char-subscripts.c
index 2e72d90fa612a..0a012f68feae0 100644
--- a/clang/test/Sema/warn-char-subscripts.c
+++ b/clang/test/Sema/warn-char-subscripts.c
@@ -62,3 +62,28 @@ void t10(void) {
   UnsignedCharTy subscript = 0;
   int val = array[subscript]; // no warning for unsigned char
 }
+
+void t11(void) {
+  int array[256] = { 0 };
+  int val = array['a']; // no warning for char with known positive value
+}
+
+void t12(void) {
+  int array[256] = { 0 };
+  char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t13(void) {
+  int array[256] = { 0 };
+  const char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t14(void) {
+  int array[256] = { 0 }; // expected-note {{array 'array' declared here}}
+  const char b = -1;
+  // expected-warning@+2 {{array subscript is of type 'char'}}
+  // expected-warning@+1 {{array index -1 is before the beginning of the 
array}}
+  int val = array[b];
+}
diff --git a/clang/test/Sema/warn-char-subscripts.cpp 
b/clang/test/Sema/warn-char-subscripts.cpp
new file mode 100644
index 0..929fb372c5173
--- /dev/null
+++ b/clang/test/Sema/warn-char-subscripts.cpp
@@ -0,0 +1,103 @@
+// RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s
+
+void t1(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t2(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t3(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t4(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+char returnsChar(void);
+void t5(void) {
+  int *array = 0;
+  int val = array[returnsChar()]; // expected-warning{{array subscript is of 
type 'char'}}
+}
+
+void t6(void) {
+  int array[1] = { 0 };
+  signed char subscript = 0;
+  int val = array[subscript]; // no warning for explicit signed char
+}
+
+void t7(void) {
+  int array[1] = { 0 };
+  unsigned char subscript = 0;
+  int val = array[subscript]; // no warning for unsigned char
+}
+
+typedef char CharTy;
+void t8(void) {
+  int array[1] 

[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-16 Thread via cfe-commits

wheatman wrote:

@AaronBallman if there are no more comments, could you please merge it in

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-16 Thread via cfe-commits

https://github.com/wheatman updated 
https://github.com/llvm/llvm-project/pull/69061

>From d19da42a2ccf65a6d9b6696ab0bdb3bb42cd3832 Mon Sep 17 00:00:00 2001
From: Brian Wheatman 
Date: Sat, 14 Oct 2023 12:02:19 -0400
Subject: [PATCH] Remove warnings from -Wchar-subscripts for known positive
 constants

---
 clang/docs/ReleaseNotes.rst  |   2 +
 clang/lib/Sema/SemaExpr.cpp  |  11 ++-
 clang/test/Sema/warn-char-subscripts.c   |  25 ++
 clang/test/Sema/warn-char-subscripts.cpp | 103 +++
 4 files changed, 138 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/warn-char-subscripts.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ed1a978b5382d71..1f0bbd99f3da40c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -557,6 +557,8 @@ Bug Fixes in This Version
   Fixes (`#67317 `_)
 - Fixed an issue that a benign assertion might hit when instantiating a pack 
expansion
   inside a lambda. (`#61460 
`_)
+- Clang's ``-Wchar-subscripts`` no longer warns on chars whose values are 
known non-negative constants.
+  Fixes (`#18763 `_)
 
 Bug Fixes to Compiler Builtins
 ^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index fc39d6149c1cc65..b22782820f323ab 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6047,9 +6047,14 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, 
SourceLocation LLoc,
  << IndexExpr->getSourceRange());
 
   if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) ||
-   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U))
- && !IndexExpr->isTypeDependent())
-Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) &&
+  !IndexExpr->isTypeDependent()) {
+std::optional IntegerContantExpr =
+IndexExpr->getIntegerConstantExpr(getASTContext());
+if (!IntegerContantExpr.has_value() ||
+IntegerContantExpr.value().isNegative())
+  Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+  }
 
   // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
   // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
diff --git a/clang/test/Sema/warn-char-subscripts.c 
b/clang/test/Sema/warn-char-subscripts.c
index 2e72d90fa612aed..0a012f68feae07f 100644
--- a/clang/test/Sema/warn-char-subscripts.c
+++ b/clang/test/Sema/warn-char-subscripts.c
@@ -62,3 +62,28 @@ void t10(void) {
   UnsignedCharTy subscript = 0;
   int val = array[subscript]; // no warning for unsigned char
 }
+
+void t11(void) {
+  int array[256] = { 0 };
+  int val = array['a']; // no warning for char with known positive value
+}
+
+void t12(void) {
+  int array[256] = { 0 };
+  char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t13(void) {
+  int array[256] = { 0 };
+  const char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t14(void) {
+  int array[256] = { 0 }; // expected-note {{array 'array' declared here}}
+  const char b = -1;
+  // expected-warning@+2 {{array subscript is of type 'char'}}
+  // expected-warning@+1 {{array index -1 is before the beginning of the 
array}}
+  int val = array[b];
+}
diff --git a/clang/test/Sema/warn-char-subscripts.cpp 
b/clang/test/Sema/warn-char-subscripts.cpp
new file mode 100644
index 000..929fb372c5173fd
--- /dev/null
+++ b/clang/test/Sema/warn-char-subscripts.cpp
@@ -0,0 +1,103 @@
+// RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s
+
+void t1(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t2(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t3(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t4(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+char returnsChar(void);
+void t5(void) {
+  int *array = 0;
+  int val = array[returnsChar()]; // expected-warning{{array subscript is of 
type 'char'}}
+}
+
+void t6(void) {
+  int array[1] = { 0 };
+  signed char subscript = 0;
+  int val = array[subscript]; // no warning for explicit signed char
+}
+
+void t7(void) {
+  int array[1] = { 0 };
+  unsigned char subscript = 0;
+  int val = array[subscript]; // no warning for unsigned char
+}
+
+typedef 

[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-03 Thread via cfe-commits

wheatman wrote:

Thank you for the review.
I don't have write access, so unless @shafik has more comments, could one of 
you merge it in

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-03 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman approved this pull request.

LGTM, thank you for the fix!

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-02 Thread via cfe-commits

https://github.com/wheatman updated 
https://github.com/llvm/llvm-project/pull/69061

>From 20e471721bbb17701954c3c6bf7f4342dd9bc3e2 Mon Sep 17 00:00:00 2001
From: Brian Wheatman 
Date: Sat, 14 Oct 2023 12:02:19 -0400
Subject: [PATCH] Remove warnings from -Wchar-subscripts for known positive
 constants

---
 clang/docs/ReleaseNotes.rst  |   2 +
 clang/lib/Sema/SemaExpr.cpp  |  11 ++-
 clang/test/Sema/warn-char-subscripts.c   |  25 ++
 clang/test/Sema/warn-char-subscripts.cpp | 103 +++
 4 files changed, 138 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/warn-char-subscripts.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4696836b3a00caa..e43b43acf0f05bf 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -541,6 +541,8 @@ Bug Fixes in This Version
   Fixes (`#67687 `_)
 - Fix crash from constexpr evaluator evaluating uninitialized arrays as rvalue.
   Fixes (`#67317 `_)
+- Clang's ``-Wchar-subscripts`` no longer warns on chars whose values are 
known non-negative constants.
+  Fixes (`#18763 `_)
 
 Bug Fixes to Compiler Builtins
 ^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 4a4cb02b0e4a6d7..68ab63552a5e94f 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6041,9 +6041,14 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, 
SourceLocation LLoc,
  << IndexExpr->getSourceRange());
 
   if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) ||
-   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U))
- && !IndexExpr->isTypeDependent())
-Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) &&
+  !IndexExpr->isTypeDependent()) {
+std::optional IntegerContantExpr =
+IndexExpr->getIntegerConstantExpr(getASTContext());
+if (!IntegerContantExpr.has_value() ||
+IntegerContantExpr.value().isNegative())
+  Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+  }
 
   // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
   // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
diff --git a/clang/test/Sema/warn-char-subscripts.c 
b/clang/test/Sema/warn-char-subscripts.c
index 2e72d90fa612aed..0a012f68feae07f 100644
--- a/clang/test/Sema/warn-char-subscripts.c
+++ b/clang/test/Sema/warn-char-subscripts.c
@@ -62,3 +62,28 @@ void t10(void) {
   UnsignedCharTy subscript = 0;
   int val = array[subscript]; // no warning for unsigned char
 }
+
+void t11(void) {
+  int array[256] = { 0 };
+  int val = array['a']; // no warning for char with known positive value
+}
+
+void t12(void) {
+  int array[256] = { 0 };
+  char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t13(void) {
+  int array[256] = { 0 };
+  const char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t14(void) {
+  int array[256] = { 0 }; // expected-note {{array 'array' declared here}}
+  const char b = -1;
+  // expected-warning@+2 {{array subscript is of type 'char'}}
+  // expected-warning@+1 {{array index -1 is before the beginning of the 
array}}
+  int val = array[b];
+}
diff --git a/clang/test/Sema/warn-char-subscripts.cpp 
b/clang/test/Sema/warn-char-subscripts.cpp
new file mode 100644
index 000..929fb372c5173fd
--- /dev/null
+++ b/clang/test/Sema/warn-char-subscripts.cpp
@@ -0,0 +1,103 @@
+// RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s
+
+void t1(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t2(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t3(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t4(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+char returnsChar(void);
+void t5(void) {
+  int *array = 0;
+  int val = array[returnsChar()]; // expected-warning{{array subscript is of 
type 'char'}}
+}
+
+void t6(void) {
+  int array[1] = { 0 };
+  signed char subscript = 0;
+  int val = array[subscript]; // no warning for explicit signed char
+}
+
+void t7(void) {
+  int array[1] = { 0 };
+  unsigned char subscript = 0;
+  int val = array[subscript]; // no warning for unsigned char
+}
+
+typedef char CharTy;
+void 

[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-02 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff e6c0e8565cb05d5884de294b546dac85bc11b497 
b46d376e803bae29231e313a623f508e22e6c9d6 -- 
clang/test/Sema/warn-char-subscripts.cpp clang/lib/Sema/SemaExpr.cpp 
clang/test/Sema/warn-char-subscripts.c
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 5c84c8ff146a..aa6f6664d1da 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -8612,15 +8612,14 @@ ExprResult Sema::BuildVectorLiteral(SourceLocation 
LParenLoc,
 // For OpenCL, when the number of initializers is a single value,
 // it will be replicated to all components of the vector.
 if (getLangOpts().OpenCL &&
-VTy->getVectorKind() == VectorType::GenericVector &&
-numExprs == 1) {
-QualType ElemTy = VTy->getElementType();
-ExprResult Literal = DefaultLvalueConversion(exprs[0]);
-if (Literal.isInvalid())
-  return ExprError();
-Literal = ImpCastExprToType(Literal.get(), ElemTy,
-PrepareScalarCast(Literal, ElemTy));
-return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.get());
+VTy->getVectorKind() == VectorType::GenericVector && numExprs == 1) {
+  QualType ElemTy = VTy->getElementType();
+  ExprResult Literal = DefaultLvalueConversion(exprs[0]);
+  if (Literal.isInvalid())
+return ExprError();
+  Literal = ImpCastExprToType(Literal.get(), ElemTy,
+  PrepareScalarCast(Literal, ElemTy));
+  return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.get());
 }
 
 initExprs.append(exprs, exprs + numExprs);
@@ -11078,9 +11077,9 @@ QualType Sema::CheckVectorOperands(ExprResult , 
ExprResult ,
 
   // AltiVec-style "vector bool op vector bool" combinations are allowed
   // for some operators but not others.
-  if (!AllowBothBool &&
-  LHSVecType && LHSVecType->getVectorKind() == VectorType::AltiVecBool &&
-  RHSVecType && RHSVecType->getVectorKind() == VectorType::AltiVecBool)
+  if (!AllowBothBool && LHSVecType &&
+  LHSVecType->getVectorKind() == VectorType::AltiVecBool && RHSVecType &&
+  RHSVecType->getVectorKind() == VectorType::AltiVecBool)
 return ReportInvalid ? InvalidOperands(Loc, LHS, RHS) : QualType();
 
   // This operation may not be performed on boolean vectors.
@@ -14864,8 +14863,8 @@ static QualType CheckIncrementDecrementOperand(Sema , 
Expr *Op,
  (ResType->castAs()->getVectorKind() !=
   VectorType::AltiVecBool)) {
 // The z vector extensions allow ++ and -- for non-bool vectors.
-  } else if(S.getLangOpts().OpenCL && ResType->isVectorType() &&
-ResType->castAs()->getElementType()->isIntegerType()) {
+  } else if (S.getLangOpts().OpenCL && ResType->isVectorType() &&
+ ResType->castAs()->getElementType()->isIntegerType()) 
{
 // OpenCL V1.2 6.3 says dec/inc ops operate on integer vector types.
   } else {
 S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement)
@@ -16354,7 +16353,7 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation 
OpLoc,
  // The z vector extensions don't allow + or - with bool vectors.
  (!Context.getLangOpts().ZVector ||
   resultType->castAs()->getVectorKind() !=
-  VectorType::AltiVecBool))
+  VectorType::AltiVecBool))
   break;
 else if (resultType->isSveVLSBuiltinType()) // SVE vectors allow + and -
   break;

``




https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-02 Thread via cfe-commits

wheatman wrote:

Thanks you for the comments. I made the updates and added the note in 
`clang/docs/ReleaseNotes.rst`

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-02 Thread via cfe-commits


@@ -62,3 +62,28 @@ void t10(void) {
   UnsignedCharTy subscript = 0;
   int val = array[subscript]; // no warning for unsigned char
 }
+
+void t11(void) {
+  int array[256] = { 0 };
+  int val = array['a']; // no warning for char with known positive value
+}
+
+void t12(void) {
+  int array[256] = { 0 };
+  char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t13(void) {
+  int array[256] = { 0 };
+  const char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t14(void) {
+  int array[256] = { 0 }; // expected-note {{array 'array' declared here}}
+  const char b = -1;
+  // expected-warning@+2 {{array subscript is of type 'char'}}
+  // expected-warning@+1 {{array index -1 is before the beginning of the 
array}}
+  int val = array[b];
+}

wheatman wrote:

This behavior was not changed https://godbolt.org/z/4drrx6h9M since in `c`  
mode `b` is not considered a constant expression.  These tests were added to 
since they were added in the `c++` file and I wanted to show the expected 
behavior in both languages. 

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-02 Thread via cfe-commits

https://github.com/wheatman updated 
https://github.com/llvm/llvm-project/pull/69061

>From b46d376e803bae29231e313a623f508e22e6c9d6 Mon Sep 17 00:00:00 2001
From: Brian Wheatman 
Date: Sat, 14 Oct 2023 12:02:19 -0400
Subject: [PATCH] Remove warnings from -Wchar-subscripts for known positive
 constants

---
 clang/docs/ReleaseNotes.rst  |   2 +
 clang/lib/Sema/SemaExpr.cpp  |  11 ++-
 clang/test/Sema/warn-char-subscripts.c   |  25 ++
 clang/test/Sema/warn-char-subscripts.cpp | 103 +++
 4 files changed, 138 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/warn-char-subscripts.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index be7c8bf247f7af5..4e3d7108f0408cc 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -393,6 +393,8 @@ Bug Fixes in This Version
   operator in C. No longer issuing a confusing diagnostic along the lines of
   "incompatible operand types ('foo' and 'foo')" with extensions such as matrix
   types. Fixes (`#69008 `_)
+- Clang's ``-Wchar-subscripts`` no longer warns on chars whose values are 
known non-negative constants.
+  Fixes (`#18763 `_)
 
 Bug Fixes to Compiler Builtins
 ^^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index aa30a3a03887558..5c84c8ff146a3f6 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6018,9 +6018,14 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, 
SourceLocation LLoc,
  << IndexExpr->getSourceRange());
 
   if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) ||
-   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U))
- && !IndexExpr->isTypeDependent())
-Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) &&
+  !IndexExpr->isTypeDependent()) {
+std::optional IntegerContantExpr =
+IndexExpr->getIntegerConstantExpr(getASTContext());
+if (!IntegerContantExpr.has_value() ||
+IntegerContantExpr.value().isNegative())
+  Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+  }
 
   // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
   // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
diff --git a/clang/test/Sema/warn-char-subscripts.c 
b/clang/test/Sema/warn-char-subscripts.c
index 2e72d90fa612aed..0a012f68feae07f 100644
--- a/clang/test/Sema/warn-char-subscripts.c
+++ b/clang/test/Sema/warn-char-subscripts.c
@@ -62,3 +62,28 @@ void t10(void) {
   UnsignedCharTy subscript = 0;
   int val = array[subscript]; // no warning for unsigned char
 }
+
+void t11(void) {
+  int array[256] = { 0 };
+  int val = array['a']; // no warning for char with known positive value
+}
+
+void t12(void) {
+  int array[256] = { 0 };
+  char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t13(void) {
+  int array[256] = { 0 };
+  const char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t14(void) {
+  int array[256] = { 0 }; // expected-note {{array 'array' declared here}}
+  const char b = -1;
+  // expected-warning@+2 {{array subscript is of type 'char'}}
+  // expected-warning@+1 {{array index -1 is before the beginning of the 
array}}
+  int val = array[b];
+}
diff --git a/clang/test/Sema/warn-char-subscripts.cpp 
b/clang/test/Sema/warn-char-subscripts.cpp
new file mode 100644
index 000..929fb372c5173fd
--- /dev/null
+++ b/clang/test/Sema/warn-char-subscripts.cpp
@@ -0,0 +1,103 @@
+// RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s
+
+void t1(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t2(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t3(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t4(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+char returnsChar(void);
+void t5(void) {
+  int *array = 0;
+  int val = array[returnsChar()]; // expected-warning{{array subscript is of 
type 'char'}}
+}
+
+void t6(void) {
+  int array[1] = { 0 };
+  signed char subscript = 0;
+  int val = array[subscript]; // no warning for explicit signed char
+}
+
+void t7(void) {
+  int array[1] = { 0 };
+  unsigned char subscript = 0;
+  int val = array[subscript]; // no warning for unsigned char
+}
+
+typedef char 

[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-02 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman commented:

Thank you for this! The changes should come with a release note in 
`clang/docs/ReleaseNotes.rst` so users know about the change in behavior.

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-02 Thread Aaron Ballman via cfe-commits


@@ -6018,9 +6018,15 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, 
SourceLocation LLoc,
  << IndexExpr->getSourceRange());
 
   if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) ||
-   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U))
- && !IndexExpr->isTypeDependent())
-Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) &&
+  !IndexExpr->isTypeDependent()) {
+std::optional IntegerContantExpr =
+IndexExpr->getIntegerConstantExpr(getASTContext());
+if (!(IntegerContantExpr.has_value() &&
+  IntegerContantExpr.value().isNonNegative())) {
+  Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+}

AaronBallman wrote:

```suggestion
std::optional IntegerContantExpr =
IndexExpr->getIntegerConstantExpr(getASTContext());
if (!IntegerContantExpr.has_value() ||
IntegerContantExpr.value().isNegative())
  Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
```
This performs extra work for each array subscript in the TU, but because it's 
limited to only array subscripts with a character type, I don't think the 
compile-time performance hit should be too bad, so this seems reasonable to me.

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-02 Thread Aaron Ballman via cfe-commits


@@ -62,3 +62,28 @@ void t10(void) {
   UnsignedCharTy subscript = 0;
   int val = array[subscript]; // no warning for unsigned char
 }
+
+void t11(void) {
+  int array[256] = { 0 };
+  int val = array['a']; // no warning for char with known positive value
+}
+
+void t12(void) {
+  int array[256] = { 0 };
+  char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t13(void) {
+  int array[256] = { 0 };
+  const char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t14(void) {
+  int array[256] = { 0 }; // expected-note {{array 'array' declared here}}
+  const char b = -1;
+  // expected-warning@+2 {{array subscript is of type 'char'}}
+  // expected-warning@+1 {{array index -1 is before the beginning of the 
array}}
+  int val = array[b];
+}

AaronBallman wrote:

These two both surprise me in a C file; `b` is not an integer constant 
expression in either case, so I would have expected no diagnostic here. The 
diagnostic is defensible, just a surprise.

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-02 Thread Aaron Ballman via cfe-commits


@@ -0,0 +1,103 @@
+// RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s
+
+void t1(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t2(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t3(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t4(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+char returnsChar(void);
+void t5(void) {
+  int *array = 0;
+  int val = array[returnsChar()]; // expected-warning{{array subscript is of 
type 'char'}}
+}
+
+void t6(void) {
+  int array[1] = { 0 };
+  signed char subscript = 0;
+  int val = array[subscript]; // no warning for explicit signed char

AaronBallman wrote:

We rule out explicitly signed or explicitly unsigned `char` type because of 
`int8_t` and `uint8_t` because those are pretty reasonable to use as array 
indexes.

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-11-02 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman edited 
https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-10-25 Thread via cfe-commits


@@ -0,0 +1,103 @@
+// RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s
+
+void t1(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t2(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t3(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t4(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+char returnsChar(void);
+void t5(void) {
+  int *array = 0;
+  int val = array[returnsChar()]; // expected-warning{{array subscript is of 
type 'char'}}
+}
+
+void t6(void) {
+  int array[1] = { 0 };
+  signed char subscript = 0;
+  int val = array[subscript]; // no warning for explicit signed char

wheatman wrote:

That is an interesting questions, the behavior for that was not changed.  
The current behavior for explicitly signed or unsigned chars is to have no 
warning, only unspecified chars have warnings.

https://godbolt.org/z/7oc3ET4h4


https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-10-25 Thread via cfe-commits

https://github.com/wheatman updated 
https://github.com/llvm/llvm-project/pull/69061

>From af3b7f9a94d7862acf5bd56dfb0ef652a684244d Mon Sep 17 00:00:00 2001
From: Brian Wheatman 
Date: Sat, 14 Oct 2023 12:02:19 -0400
Subject: [PATCH] Remove warnings from -Wchar-subscripts for known positive
 constants

---
 clang/lib/Sema/SemaExpr.cpp  |  12 ++-
 clang/test/Sema/warn-char-subscripts.c   |  25 ++
 clang/test/Sema/warn-char-subscripts.cpp | 103 +++
 3 files changed, 137 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/warn-char-subscripts.cpp

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index aa30a3a03887558..dd9ba5cecaf2404 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6018,9 +6018,15 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, 
SourceLocation LLoc,
  << IndexExpr->getSourceRange());
 
   if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) ||
-   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U))
- && !IndexExpr->isTypeDependent())
-Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) &&
+  !IndexExpr->isTypeDependent()) {
+std::optional IntegerContantExpr =
+IndexExpr->getIntegerConstantExpr(getASTContext());
+if (!(IntegerContantExpr.has_value() &&
+  IntegerContantExpr.value().isNonNegative())) {
+  Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+}
+  }
 
   // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
   // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
diff --git a/clang/test/Sema/warn-char-subscripts.c 
b/clang/test/Sema/warn-char-subscripts.c
index 2e72d90fa612aed..0a012f68feae07f 100644
--- a/clang/test/Sema/warn-char-subscripts.c
+++ b/clang/test/Sema/warn-char-subscripts.c
@@ -62,3 +62,28 @@ void t10(void) {
   UnsignedCharTy subscript = 0;
   int val = array[subscript]; // no warning for unsigned char
 }
+
+void t11(void) {
+  int array[256] = { 0 };
+  int val = array['a']; // no warning for char with known positive value
+}
+
+void t12(void) {
+  int array[256] = { 0 };
+  char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t13(void) {
+  int array[256] = { 0 };
+  const char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t14(void) {
+  int array[256] = { 0 }; // expected-note {{array 'array' declared here}}
+  const char b = -1;
+  // expected-warning@+2 {{array subscript is of type 'char'}}
+  // expected-warning@+1 {{array index -1 is before the beginning of the 
array}}
+  int val = array[b];
+}
diff --git a/clang/test/Sema/warn-char-subscripts.cpp 
b/clang/test/Sema/warn-char-subscripts.cpp
new file mode 100644
index 000..929fb372c5173fd
--- /dev/null
+++ b/clang/test/Sema/warn-char-subscripts.cpp
@@ -0,0 +1,103 @@
+// RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s
+
+void t1(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t2(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t3(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t4(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+char returnsChar(void);
+void t5(void) {
+  int *array = 0;
+  int val = array[returnsChar()]; // expected-warning{{array subscript is of 
type 'char'}}
+}
+
+void t6(void) {
+  int array[1] = { 0 };
+  signed char subscript = 0;
+  int val = array[subscript]; // no warning for explicit signed char
+}
+
+void t7(void) {
+  int array[1] = { 0 };
+  unsigned char subscript = 0;
+  int val = array[subscript]; // no warning for unsigned char
+}
+
+typedef char CharTy;
+void t8(void) {
+  int array[1] = { 0 };
+  CharTy subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+typedef signed char SignedCharTy;
+void t9(void) {
+  int array[1] = { 0 };
+  SignedCharTy subscript = 0;
+  int val = array[subscript]; // no warning for explicit signed char
+}
+
+typedef unsigned char UnsignedCharTy;
+void t10(void) {
+  int array[1] = { 0 };
+  UnsignedCharTy subscript = 0;
+  int val = array[subscript]; // no warning for unsigned char
+}
+
+void t11(void) {
+  int array[256] = { 0 };
+  int val = array['a']; // no warning for char with known positive value
+}
+
+void t12(void) {
+  int array[256] = { 0 };
+  char b = 'a';
+  int val = array[b]; // 

[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-10-25 Thread Shafik Yaghmour via cfe-commits


@@ -0,0 +1,103 @@
+// RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s
+
+void t1(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t2(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t3(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t4(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+char returnsChar(void);
+void t5(void) {
+  int *array = 0;
+  int val = array[returnsChar()]; // expected-warning{{array subscript is of 
type 'char'}}
+}
+
+void t6(void) {
+  int array[1] = { 0 };
+  signed char subscript = 0;
+  int val = array[subscript]; // no warning for explicit signed char

shafik wrote:

Why do we exclude the `signed char` case?

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-10-25 Thread Shafik Yaghmour via cfe-commits

https://github.com/shafik commented:

You need to make sure you tests have newlines at the end

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-10-25 Thread Shafik Yaghmour via cfe-commits

https://github.com/shafik edited https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-10-14 Thread via cfe-commits

https://github.com/wheatman edited 
https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-10-14 Thread via cfe-commits

https://github.com/wheatman updated 
https://github.com/llvm/llvm-project/pull/69061

>From 44e66636bd886b263a15fe9e49d575d8ea53592d Mon Sep 17 00:00:00 2001
From: Brian Wheatman 
Date: Sat, 14 Oct 2023 12:02:19 -0400
Subject: [PATCH] Remove warnings from -Wchar-subscripts for known positive
 constants

---
 clang/lib/Sema/SemaExpr.cpp  |  12 ++-
 clang/test/Sema/warn-char-subscripts.c   |  25 ++
 clang/test/Sema/warn-char-subscripts.cpp | 103 +++
 3 files changed, 137 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/warn-char-subscripts.cpp

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index aa30a3a03887558..dd9ba5cecaf2404 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6018,9 +6018,15 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, 
SourceLocation LLoc,
  << IndexExpr->getSourceRange());
 
   if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) ||
-   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U))
- && !IndexExpr->isTypeDependent())
-Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) &&
+  !IndexExpr->isTypeDependent()) {
+std::optional IntegerContantExpr =
+IndexExpr->getIntegerConstantExpr(getASTContext());
+if (!(IntegerContantExpr.has_value() &&
+  IntegerContantExpr.value().isNonNegative())) {
+  Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+}
+  }
 
   // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
   // C++ [expr.sub]p1: The type "T" shall be a completely-defined object
diff --git a/clang/test/Sema/warn-char-subscripts.c 
b/clang/test/Sema/warn-char-subscripts.c
index 2e72d90fa612aed..61409c0a50daf47 100644
--- a/clang/test/Sema/warn-char-subscripts.c
+++ b/clang/test/Sema/warn-char-subscripts.c
@@ -62,3 +62,28 @@ void t10(void) {
   UnsignedCharTy subscript = 0;
   int val = array[subscript]; // no warning for unsigned char
 }
+
+void t11(void) {
+  int array[256] = { 0 };
+  int val = array['a']; // no warning for char with known positive value
+}
+
+void t12(void) {
+  int array[256] = { 0 };
+  char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t13(void) {
+  int array[256] = { 0 };
+  const char b = 'a';
+  int val = array[b]; // expected-warning{{array subscript is of type 'char'}}
+}
+
+void t14(void) {
+  int array[256] = { 0 }; // expected-note {{array 'array' declared here}}
+  const char b = -1;
+  // expected-warning@+2 {{array subscript is of type 'char'}}
+  // expected-warning@+1 {{array index -1 is before the beginning of the 
array}}
+  int val = array[b];
+}
\ No newline at end of file
diff --git a/clang/test/Sema/warn-char-subscripts.cpp 
b/clang/test/Sema/warn-char-subscripts.cpp
new file mode 100644
index 000..21b991d83590937
--- /dev/null
+++ b/clang/test/Sema/warn-char-subscripts.cpp
@@ -0,0 +1,103 @@
+// RUN: %clang_cc1 -Wchar-subscripts -fsyntax-only -verify %s
+
+void t1(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t2(void) {
+  int array[1] = { 0 };
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t3(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+void t4(void) {
+  int *array = 0;
+  char subscript = 0;
+  int val = subscript[array]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+char returnsChar(void);
+void t5(void) {
+  int *array = 0;
+  int val = array[returnsChar()]; // expected-warning{{array subscript is of 
type 'char'}}
+}
+
+void t6(void) {
+  int array[1] = { 0 };
+  signed char subscript = 0;
+  int val = array[subscript]; // no warning for explicit signed char
+}
+
+void t7(void) {
+  int array[1] = { 0 };
+  unsigned char subscript = 0;
+  int val = array[subscript]; // no warning for unsigned char
+}
+
+typedef char CharTy;
+void t8(void) {
+  int array[1] = { 0 };
+  CharTy subscript = 0;
+  int val = array[subscript]; // expected-warning{{array subscript is of type 
'char'}}
+}
+
+typedef signed char SignedCharTy;
+void t9(void) {
+  int array[1] = { 0 };
+  SignedCharTy subscript = 0;
+  int val = array[subscript]; // no warning for explicit signed char
+}
+
+typedef unsigned char UnsignedCharTy;
+void t10(void) {
+  int array[1] = { 0 };
+  UnsignedCharTy subscript = 0;
+  int val = array[subscript]; // no warning for unsigned char
+}
+
+void t11(void) {
+  int array[256] = { 0 };
+  int val = array['a']; // no warning for char with known positive value
+}
+
+void t12(void) {
+  int array[256] = { 0 };
+  char b = 'a';
+  

[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-10-14 Thread via cfe-commits

wheatman wrote:

I found the test I would want to modify here 
(llvm-project/clang/test/Sema/warn-char-subscripts.c)
I will work on updating it

https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-10-14 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: None (wheatman)


Changes

This is to address https://github.com/llvm/llvm-project/issues/18763

it removes warnings from using a signed char as an array bound if the char is a 
known positives constant.

This goes one step farther than gcc does.

For example given the following code
```c++
char upper[300];

int main() {
  upper['a'] = 'A';
  char b = 'a';
  upper[b] = 'A';
  const char c = 'a';
  upper[c] = 'A';
  constexpr char d = 'a';
  upper[d] = 'A';
  char e = -1;
  upper[e] = 'A';
  const char f = -1;
  upper[f] = 'A';
  constexpr char g = -1;
  upper[g] = 'A';
  return 1;
}
```

clang currently gives warnings for all cases, while gcc gives warnings for all 
cases except for 'a' (https://godbolt.org/z/5ahjETTv3)

with the change there is no longer any warning for 'a', 'c', or 'd'.

```
../test.cpp:7:8: warning: array subscript is of type 'char' [-Wchar-subscripts]
7 |   upper[b] = 'A';
  |^~
../test.cpp:13:8: warning: array subscript is of type 'char' [-Wchar-subscripts]
   13 |   upper[e] = 'A';
  |^~
../test.cpp:15:8: warning: array subscript is of type 'char' [-Wchar-subscripts]
   15 |   upper[f] = 'A';
  |^~
../test.cpp:17:8: warning: array subscript is of type 'char' [-Wchar-subscripts]
   17 |   upper[g] = 'A';
  |^~
../test.cpp:15:3: warning: array index -1 is before the beginning of the array 
[-Warray-bounds]
   15 |   upper[f] = 'A';
  |   ^ ~
../test.cpp:1:1: note: array 'upper' declared here
1 | char upper[300];
  | ^
../test.cpp:17:3: warning: array index -1 is before the beginning of the array 
[-Warray-bounds]
   17 |   upper[g] = 'A';
  |   ^ ~
../test.cpp:1:1: note: array 'upper' declared here
1 | char upper[300];
  | ^
6 warnings generated.
```

This pull request in incomplete in that this is my first change submitted and I 
don't know how to add tests, any guidance on what sort of tests to add, and 
where to find documentation on the testing infrastructure  

---
Full diff: https://github.com/llvm/llvm-project/pull/69061.diff


1 Files Affected:

- (modified) clang/lib/Sema/SemaExpr.cpp (+9-3) 


``diff
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index aa30a3a03887558..dd9ba5cecaf2404 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6018,9 +6018,15 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, 
SourceLocation LLoc,
  << IndexExpr->getSourceRange());
 
   if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) ||
-   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U))
- && !IndexExpr->isTypeDependent())
-Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) &&
+  !IndexExpr->isTypeDependent()) {
+std::optional IntegerContantExpr =
+IndexExpr->getIntegerConstantExpr(getASTContext());
+if (!(IntegerContantExpr.has_value() &&
+  IntegerContantExpr.value().isNonNegative())) {
+  Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+}
+  }
 
   // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
   // C++ [expr.sub]p1: The type "T" shall be a completely-defined object

``




https://github.com/llvm/llvm-project/pull/69061
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)

2023-10-14 Thread via cfe-commits

https://github.com/wheatman created 
https://github.com/llvm/llvm-project/pull/69061

This is to address https://github.com/llvm/llvm-project/issues/18763

it removes warnings from using a signed char as an array bound if the char is a 
known positives constant.

This goes one step farther than gcc does.

For example given the following code
```c++
char upper[300];

int main() {
  upper['a'] = 'A';
  char b = 'a';
  upper[b] = 'A';
  const char c = 'a';
  upper[c] = 'A';
  constexpr char d = 'a';
  upper[d] = 'A';
  char e = -1;
  upper[e] = 'A';
  const char f = -1;
  upper[f] = 'A';
  constexpr char g = -1;
  upper[g] = 'A';
  return 1;
}
```

clang currently gives warnings for all cases, while gcc gives warnings for all 
cases except for 'a' (https://godbolt.org/z/5ahjETTv3)

with the change there is no longer any warning for 'a', 'c', or 'd'.

```
../test.cpp:7:8: warning: array subscript is of type 'char' [-Wchar-subscripts]
7 |   upper[b] = 'A';
  |^~
../test.cpp:13:8: warning: array subscript is of type 'char' [-Wchar-subscripts]
   13 |   upper[e] = 'A';
  |^~
../test.cpp:15:8: warning: array subscript is of type 'char' [-Wchar-subscripts]
   15 |   upper[f] = 'A';
  |^~
../test.cpp:17:8: warning: array subscript is of type 'char' [-Wchar-subscripts]
   17 |   upper[g] = 'A';
  |^~
../test.cpp:15:3: warning: array index -1 is before the beginning of the array 
[-Warray-bounds]
   15 |   upper[f] = 'A';
  |   ^ ~
../test.cpp:1:1: note: array 'upper' declared here
1 | char upper[300];
  | ^
../test.cpp:17:3: warning: array index -1 is before the beginning of the array 
[-Warray-bounds]
   17 |   upper[g] = 'A';
  |   ^ ~
../test.cpp:1:1: note: array 'upper' declared here
1 | char upper[300];
  | ^
6 warnings generated.
```

This pull request in incomplete in that this is my first change submitted and I 
don't know how to add tests, any guidance on what sort of tests to add, and 
where to find documentation on the testing infrastructure  

>From b3111e99b8caf875bca6c59c5ac78cfc0a9f0d0f Mon Sep 17 00:00:00 2001
From: Brian Wheatman 
Date: Sat, 14 Oct 2023 12:02:19 -0400
Subject: [PATCH] Remove warnings from -Wchar-subscripts for known positive
 constants

---
 clang/lib/Sema/SemaExpr.cpp | 12 +---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index aa30a3a03887558..dd9ba5cecaf2404 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -6018,9 +6018,15 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, 
SourceLocation LLoc,
  << IndexExpr->getSourceRange());
 
   if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) ||
-   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U))
- && !IndexExpr->isTypeDependent())
-Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+   IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) &&
+  !IndexExpr->isTypeDependent()) {
+std::optional IntegerContantExpr =
+IndexExpr->getIntegerConstantExpr(getASTContext());
+if (!(IntegerContantExpr.has_value() &&
+  IntegerContantExpr.value().isNonNegative())) {
+  Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange();
+}
+  }
 
   // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly,
   // C++ [expr.sub]p1: The type "T" shall be a completely-defined object

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits