On 6/16/21 5:48 AM, Jakub Jelinek wrote:
On Tue, Jun 15, 2021 at 06:11:27PM +0200, Richard Biener wrote:
--- a/gcc/tree-sra.c
+++ b/gcc/tree-sra.c
@@ -915,6 +915,12 @@ create_access (tree expr, gimple *stmt, bool
write)
if (!DECL_P (base) || !bitmap_bit_p (candidate_bitmap, DECL_UID
(base)))
     return NULL;

+  if (write && TREE_READONLY (base))
+    {
+      disqualify_candidate (base, "Encountered a store to a read-only
decl.");

Wouldn't this be a useful point to also emit some warning (with
some TREE_NO_WARNING prevention) that some particular statement modifies
a const decl?
I guess it can be warned elsewhere though.
As testcases one could use 
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100994#c4
and #c5.  Though would be nice if we diagnose that even without those -fno-*
options.

I didn't finish my patch to diagnose these bugs in time for GCC 11
but I'm hoping to finish and submit it for GCC 12.  (It's being
tracked in PR 90404).  My approach is the same as for similar
warnings such as -Wfree-nonheap-object or -Wstringop-overflow.
It depends on no particular optimization options (with all
the expected consequences at -O0).

Martin



        Jakub


Reply via email to