Update -Warray-bounds with -fstrict-flex-arrays.
authorQing Zhao <qing.zhao@oracle.com>
Tue, 6 Dec 2022 18:50:04 +0000 (18:50 +0000)
committerQing Zhao <qing.zhao@oracle.com>
Tue, 6 Dec 2022 18:50:04 +0000 (18:50 +0000)
commit710c9676520dfd38b4bfdcc937ce026ed89921d6
tree81a6bc70c710cba93ab79a61180ce822fe31ae13
parentdcfc7ac94dbcf6c86c0c58ce6dc1d8bd853e4093
Update -Warray-bounds with -fstrict-flex-arrays.

     A. add the following to clarify the relationship between -Warray-bounds
        and the LEVEL of -fstrict-flex-array:

     By default, the trailing array of a structure will be treated as a
     flexible array member by '-Warray-bounds' or '-Warray-bounds=N' if
     it is declared as either a flexible array member per C99 standard
     onwards ('[]'), a GCC zero-length array extension ('[0]'), or an
     one-element array ('[1]').  As a result, out of bounds subscripts
     or offsets into zero-length arrays or one-element arrays are not
     warned by default.

     You can add the option '-fstrict-flex-arrays' or
     '-fstrict-flex-arrays=LEVEL' to control how this option treat
     trailing array of a structure as a flexible array member.

     when LEVEL<=1, no change to the default behavior.

     when LEVEL=2, additional warnings will be issued for out of bounds
     subscripts or offsets into one-element arrays;

     when LEVEL=3, in addition to LEVEL=2, additional warnings will be
     issued for out of bounds subscripts or offsets into zero-length
     arrays.

     B. change -Warray-bounds=2 to exclude its control on how to treat
        trailing arrays as flexible array members:

     '-Warray-bounds=2'
          This warning level also warns about the intermediate results
          of pointer arithmetic that may yield out of bounds values.
          This warning level may give a larger number of false positives
          and is deactivated by default.

gcc/ChangeLog:

* attribs.cc (strict_flex_array_level_of): New function.
* attribs.h (strict_flex_array_level_of): Prototype for new function.
* doc/invoke.texi: Update -Warray-bounds by specifying the impact from
-fstrict-flex-arrays. Also update -Warray-bounds=2 by eliminating its
impact on treating trailing arrays as flexible array members.
* gimple-array-bounds.cc (get_up_bounds_for_array_ref): New function.
(check_out_of_bounds_and_warn): New function.
(array_bounds_checker::check_array_ref): Update with call to the above
new functions.
* tree.cc (array_ref_flexible_size_p): Add one new argument.
(component_ref_sam_type): New function.
(component_ref_size): Control with level of strict-flex-array.
* tree.h (array_ref_flexible_size_p): Update prototype.
(enum struct special_array_member): Add two new enum values.
(component_ref_sam_type): New prototype.

gcc/c/ChangeLog:

* c-decl.cc (is_flexible_array_member_p): Call new function
strict_flex_array_level_of.

gcc/testsuite/ChangeLog:

* gcc.dg/Warray-bounds-11.c: Update warnings for -Warray-bounds=2.
* gcc.dg/Warray-bounds-flex-arrays-1.c: New test.
* gcc.dg/Warray-bounds-flex-arrays-2.c: New test.
* gcc.dg/Warray-bounds-flex-arrays-3.c: New test.
* gcc.dg/Warray-bounds-flex-arrays-4.c: New test.
* gcc.dg/Warray-bounds-flex-arrays-5.c: New test.
* gcc.dg/Warray-bounds-flex-arrays-6.c: New test.
14 files changed:
gcc/attribs.cc
gcc/attribs.h
gcc/c/c-decl.cc
gcc/doc/invoke.texi
gcc/gimple-array-bounds.cc
gcc/testsuite/gcc.dg/Warray-bounds-11.c
gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-5.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/Warray-bounds-flex-arrays-6.c [new file with mode: 0644]
gcc/tree.cc
gcc/tree.h