-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unnecessary warning: "array subscript is of type 'char' [-Wchar-subscripts]" with constant char indexes #18763
Comments
Also, I encountered this warning when I use the toupper function (which was implemented using a lookup table, without first casting the subscript to int). I think pre-known positive constant values should be clear of this warning, and toupper and tolower macros should add casts. |
This behavior is still the same on post 17 trunk(7d495d6) code
warning
|
@wheatman I think you have a copy-paste mistake in the previous comment |
I think this is a reasonable diagnostic. I think it is highly likely to be a mistake to use a char to index into an array. |
I think the argument is does this reasoning still hold true when the char is a fixed positive constant, for which I have no opinion. I did not comment specifically on this post in support of the changed diagnostic, I am simply going through all old issues to see what still has the same behavior years down the road and what has long been fixed and can be closed |
I think this is a valid request for QoI. Worth noting that GCC implemented this between 4.1 and 4.4: https://godbolt.org/z/q9aec3Gh1 I think this is a reasonable good first issue. I would tackle this by modifying: llvm-project/clang/lib/Sema/SemaExpr.cpp Line 6018 in 64d78d8
to look test whether the subscript expression is a constant expression; if it has a known positive value, then we can suppress the warning. We can either tie this to being a character literal explicitly, or we could tie it to any constant value (e.g., constexpr char c = 'a'; array[c]; )
|
@llvm/issue-subscribers-good-first-issue Author: None (llvmbot)
| | |
| --- | --- |
| Bugzilla Link | [18389](https://llvm.org/bz18389) |
| Version | unspecified |
| OS | Linux |
| Reporter | LLVM Bugzilla Contributor |
| CC | @DougGregor |
Extended DescriptionThe following code for example generates the warning: BackupQueries.cpp:2092:29: warning: array subscript is of type 'char' [-Wchar-subscripts] Because 'm' is in ASCII, and therefore a positive char value, on all platforms that GCC and LLVM support and almost certainly on all platforms implementing C++11, this warning seems unnecessary, and clearing it requires obfuscating the code thus:
We could probably choose not to show this warning when the value of the index is known by the compiler to be positive. |
In implementing a fix for this I noticed something interesting in the different behaviors between c and c++.
If this is c then In this context I am just using llvm-project/clang/include/clang/AST/Expr.h Line 544 in 80737d2
|
Correct; C has different ideas about constant expressions from C++ (though C23 did get
That is the tool I'd reach for as well. |
Extended Description
The following code for example generates the warning:
BackupQueries.cpp:2092:29: warning: array subscript is of type 'char' [-Wchar-subscripts]
bool MachineReadable = opts['m'];
Because 'm' is in ASCII, and therefore a positive char value, on all platforms that GCC and LLVM support and almost certainly on all platforms implementing C++11, this warning seems unnecessary, and clearing it requires obfuscating the code thus:
We could probably choose not to show this warning when the value of the index is known by the compiler to be positive.
The text was updated successfully, but these errors were encountered: