This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: PR35503 - warn for restrict pointer


On 30/08/16 16:54, Tom de Vries wrote:
On 26/08/16 13:39, Prathamesh Kulkarni wrote:
Hi,
The following patch adds option -Wrestrict that warns when an argument
is passed to a restrict qualified parameter and it aliases with
another argument.

eg:
int foo (const char *__restrict buf, const char *__restrict fmt, ...);

void f(void)
{
  char buf[100] = "hello";
  foo (buf, "%s-%s", buf, "world");
}

Does -Wrestrict generate a warning for this example?

...
void h(int n, int * restrict p, int * restrict q, int * restrict r)
{
  int i;
  for (i = 0; i < n; i++)
    p[i] = q[i] + r[i];
}

h (100, a, b, b)
...

[ Note that this is valid C, and does not violate the restrict
definition. ]

If -Wrestrict indeed generates a warning, then we should be explicit in
the documentation that while the warning triggers on this type of
example, the code is correct.

So, what I had in mind is something like this:
...
Warn when an argument passed to a restrict-qualified parameter pointing to a non-const type aliases with another argument.

The warning will trigger for the following illegal use of restrict:

  void foo (int *restrict a, int *b) { *a = 1; *b = 2; }
  void foo2 (void) { int o; foo (&o, &o); }

The use of restrict is illegal because in the lifetime of the scope of the restrict pointer 'a' both:
- the object 'o' that the restrict pointer 'a' points to is modified,
  and
- the object 'o' is accessed through pointer 'b', which is not based on
  restrict pointer 'a'.

However, the warning will also trigger for the following legal use of restrict:

  int foo (int *restrict a, int *b) { return *a + *b; }
  int foo2 (void) { int o = 1; return foo (&o, &o); }

The use of restrict is legal, because the object 'o' that the restrict pointer 'a' points to is not modified in the lifetime of the scope of the restrict pointer 'a'. The warning can be silenced by changing the type of 'a' to 'const int *restrict'.
...

Thanks,
- Tom


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]