[clang][bytecode] Diagnose negative array sizes differently (#114380)

We have a special diagnostic ID for this.
This commit is contained in:
Timm Baeder 2024-10-31 10:59:53 +01:00 committed by GitHub
parent 28d0718033
commit ccb7cc319f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 2 deletions

View File

@ -273,8 +273,14 @@ bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT *NumElements,
*NumElements > MaxElements) {
if (!IsNoThrow) {
const SourceInfo &Loc = S.Current->getSource(OpPC);
S.FFDiag(Loc, diag::note_constexpr_new_too_large)
<< NumElements->toDiagnosticString(S.getASTContext());
if (NumElements->isSigned() && NumElements->isNegative()) {
S.FFDiag(Loc, diag::note_constexpr_new_negative)
<< NumElements->toDiagnosticString(S.getASTContext());
} else {
S.FFDiag(Loc, diag::note_constexpr_new_too_large)
<< NumElements->toDiagnosticString(S.getASTContext());
}
}
return false;
}

View File

@ -274,6 +274,15 @@ namespace NowThrowNew {
static_assert(erroneous_array_bound_nothrow2(-1) == 0);// expected-error {{not an integral constant expression}}
static_assert(!erroneous_array_bound_nothrow2(1LL << 62));// expected-error {{not an integral constant expression}}
constexpr bool erroneous_array_bound(long long n) {
delete[] new int[n]; // both-note {{array bound -1 is negative}} both-note {{array bound 4611686018427387904 is too large}}
return true;
}
static_assert(erroneous_array_bound(3));
static_assert(erroneous_array_bound(0));
static_assert(erroneous_array_bound(-1)); // both-error {{constant expression}} both-note {{in call}}
static_assert(erroneous_array_bound(1LL << 62)); // both-error {{constant expression}} both-note {{in call}}
constexpr bool evaluate_nothrow_arg() {
bool ok = false;
delete new ((ok = true, std::nothrow)) int;