[clang] Fix -Warray-bound interaction with -fstrict-flex-arrays=1

The test to check if an array was a FAM in the context of array bound checking
and strict-flex-arrays=1 was inverted.

As a by product, improve test coverage.

Differential Revision: https://reviews.llvm.org/D132853
This commit is contained in:
serge-sans-paille 2022-08-29 17:02:50 +02:00
parent 52d7c0b760
commit 31dd0255ae
2 changed files with 19 additions and 8 deletions

View File

@ -15852,7 +15852,7 @@ static bool IsTailPaddedMemberArray(Sema &S, const llvm::APInt &Size,
if (StrictFlexArraysLevel >= 2 && Size != 0)
return false;
if (StrictFlexArraysLevel == 1 && Size.ule(1))
if (StrictFlexArraysLevel == 1 && Size.uge(2))
return false;
// FIXME: While the default -fstrict-flex-arrays=0 permits Size>1 trailing

View File

@ -1,4 +1,5 @@
// RUN: %clang_cc1 -verify -fstrict-flex-arrays=2 %s
// RUN: %clang_cc1 -verify=relaxed -fstrict-flex-arrays=1 %s
// RUN: %clang_cc1 -verify=relaxed,strict -fstrict-flex-arrays=2 %s
// We cannot know for sure the size of a flexible array.
struct t {
@ -9,16 +10,26 @@ void test(t *s2) {
s2->a[2] = 0; // no-warning
}
// Under -fstrict-flex-arrays `a` is not a flexible array.
struct t1 {
// Under -fstrict-flex-arrays={1,2} `a` is not a flexible array
struct t0 {
int f;
int a[1]; // expected-note {{array 'a' declared here}}
int a[10]; // relaxed-note {{array 'a' declared here}}
};
void test1(t1 *s2) {
s2->a[2] = 0; // expected-warning {{array index 2 is past the end of the array (which contains 1 element)}}
void test0(t0 *s2) {
s2->a[12] = 0; // relaxed-warning {{array index 12 is past the end of the array (which contains 10 elements)}}
}
// Under -fstrict-flex-arrays `a` is a flexible array.
// Under -fstrict-flex-arrays=2 `a` is not a flexible array, but it is under -fstrict-flex-arrays=1
struct t1 {
int f;
int a[1]; // strict-note {{array 'a' declared here}}
};
void test1(t1 *s2) {
s2->a[2] = 0; // strict-warning {{array index 2 is past the end of the array (which contains 1 element)}}
}
// Under -fstrict-flex-arrays={1,2} `a` is a flexible array.
struct t2 {
int f;
int a[0];