[PATCH] warning for restrict-qualified pointer assignments with undefined behavior

hfinkel at anl.gov hfinkel at anl.gov
Tue Oct 21 12:15:56 PDT 2014


Hi rsmith, dberlin, aaron.ballman,

This patch introduces a warning for undefined behavior introduced by assignments to a restrict-qualified pointer an expression based on other restrict qualified pointers. Roughly speaking, this happens when the LHS is designated by an identifier with a scope contained by the scope of the RHS designator (you can't assign from an "inner" restrict pointer to an "outer" one -- or between two at the same level).

For example, if we have:

  struct S {
    int * restrict x;
    int * restrict y;
  };

  void f6(int * restrict q1, int * restrict q2, struct S s, struct S *t)
  {
    s.x = q1;
    t->y = s.x;
  }

you'll get these warnings (with some hopefully-useful notes):

  warning: assignment to a restrict-qualified pointer designated by 's' using an expression based on a restrict-qualified
        pointer designated by 'q1' has undefined behavior [-Wundefined-restrict-assignment]
    s.x = q1;
        ^
  note: the block associated with 'q1', declared here
  void f6(int * restrict q1, int * restrict q2, struct S s, struct S *t)
                         ^
  note: does not begin before the block associated with 's', declared here
  void f6(int * restrict q1, int * restrict q2, struct S s, struct S *t)
                                                         ^
  warning: assignment to a restrict-qualified pointer designated by 't' using an expression based on a restrict-qualified
        pointer designated by 's' has undefined behavior [-Wundefined-restrict-assignment]
    t->y = s.x;
         ^
  note: the block associated with 's', declared here
  void f6(int * restrict q1, int * restrict q2, struct S s, struct S *t)
                                                          ^
  note: does not begin before the block associated with 't', declared here
  void f6(int * restrict q1, int * restrict q2, struct S s, struct S *t)
                                                                      ^
Currently, this only determines the "based on" relationship via a syntactic analysis of the expression. In the future, I'd like to make this an analysis-based warning so that some data-flow information can inform the "based on" determination in case it flows through other non-restrict-qualified pointers.

Thanks again!

http://reviews.llvm.org/D5889

Files:
  include/clang/Basic/DiagnosticSemaKinds.td
  include/clang/Sema/Sema.h
  lib/Sema/SemaExpr.cpp
  test/Sema/restrict-assignment.c
  test/SemaCXX/restrict-assignment.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D5889.15198.patch
Type: text/x-patch
Size: 17050 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20141021/7e4337e4/attachment.bin>


More information about the cfe-commits mailing list