[clang] Remove warnings from -Wchar-subscripts for known positive constants (PR #69061)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 2 09:46:47 PDT 2023
================
@@ -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<llvm::APSInt> 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<llvm::APSInt> 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
More information about the cfe-commits
mailing list