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

via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 30 05:50:47 PDT 2022


Author: serge-sans-paille
Date: 2022-08-30T14:49:37+02:00
New Revision: 31dd0255aeaab7f2270abd0454e835842cfdcbb0

URL: https://github.com/llvm/llvm-project/commit/31dd0255aeaab7f2270abd0454e835842cfdcbb0
DIFF: https://github.com/llvm/llvm-project/commit/31dd0255aeaab7f2270abd0454e835842cfdcbb0.diff

LOG: [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

Added: 
    

Modified: 
    clang/lib/Sema/SemaChecking.cpp
    clang/test/SemaCXX/array-bounds-strict-flex-arrays.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index bb4ca8c1e4209..e2e3d64fcade7 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -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

diff  --git a/clang/test/SemaCXX/array-bounds-strict-flex-arrays.cpp b/clang/test/SemaCXX/array-bounds-strict-flex-arrays.cpp
index 225a804b3171c..7136973a8ab8e 100644
--- a/clang/test/SemaCXX/array-bounds-strict-flex-arrays.cpp
+++ b/clang/test/SemaCXX/array-bounds-strict-flex-arrays.cpp
@@ -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.
+// Under -fstrict-flex-arrays={1,2} `a` is not a flexible array
+struct t0 {
+  int f;
+  int a[10]; // relaxed-note {{array 'a' declared here}}
+};
+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=2 `a` is not a flexible array, but it is under -fstrict-flex-arrays=1
 struct t1 {
   int f;
-  int a[1]; // expected-note {{array 'a' declared here}}
+  int a[1]; // strict-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)}}
+  s2->a[2] = 0; // strict-warning {{array index 2 is past the end of the array (which contains 1 element)}}
 }
 
-// Under -fstrict-flex-arrays `a` is a flexible array.
+// Under -fstrict-flex-arrays={1,2} `a` is a flexible array.
 struct t2 {
   int f;
   int a[0];


        


More information about the cfe-commits mailing list