public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Helmut Zeisel <HZ2012@gmx.at>
To: Jonathan Wakely <jwakely.gcc@gmail.com>, gcc@gcc.gnu.org
Subject: Aw: Re: Warning: shared mutable data
Date: Fri, 17 Feb 2023 16:57:08 +0100	[thread overview]
Message-ID: <trinity-3fcba8f2-a1a7-495f-9999-a098e90fd890-1676649428481@3c-app-gmx-bap61> (raw)
In-Reply-To: <CAH6eHdQP4CHPcsfLT1=szPMvrdNrUqiDzVskXyHi0RCo-bJtJw@mail.gmail.com>

Von: "Jonathan Wakely" <jwakely.gcc@gmail.com>


 > What exactly are you suggesting for the semantics of the warning? 

Good question. It is difficult to detect all suspiscious cases, but at least some of the can be defined:

If we have a function prototype 

f(...,Ti xi,...Tj xj,...)

and call the function f(... xi, ... xj,...) with some xi, xj with aliasing / data sharing,

and both Ti and Tj are references/pointers and at least one is a non-const pointer / reference,
then the warning should be given.

E.g. for both int:

Ti int: no warning
Ti int& or int*: warning if Tj is int*, const int*, int&, or const int&; no warning for Tj int.
Ti const int& or const int*: warning if Tj is int*, or int&; no warning if Tj is const int*, const int&, or int. 

Or maybe some code example:

void increase_x_by_y_and_z(int& x, const int& y, const int& z)
{
  x+=y;
  x+=z;
}

This should be OK (no sharing, no warning):

x=1;
y=1;
z=1;

increase_x_by_y_and_z(x,y,z);

This should give a warning (sharing of int& and const int&):

x=1;
y=1;

increase_x_by_y_and_z(x,y,x);

On the other hand, this is OK (y is shared but not mutable - two times const int&):

x=1;
y=1;

increase_x_by_y_and_z(x,y,y);


Helmut




      reply	other threads:[~2023-02-17 15:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-17 13:14 Helmut Zeisel
2023-02-17 14:06 ` Jonathan Wakely
2023-02-17 15:57   ` Helmut Zeisel [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=trinity-3fcba8f2-a1a7-495f-9999-a098e90fd890-1676649428481@3c-app-gmx-bap61 \
    --to=hz2012@gmx.at \
    --cc=gcc@gcc.gnu.org \
    --cc=jwakely.gcc@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).