[PATCH] Enhanced ignored-qualifiers checks for restrict

hfinkel at anl.gov hfinkel at anl.gov
Mon Oct 20 13:05:55 PDT 2014

Hi rsmith, dberlin, aaron.ballman,

We already include the restrict qualifier in the -Wignored-qualifiers checks, and so we'll produce a warning when -Wignored-qualifiers is in effect for a function declared with a restrict-qualified return type:

  warning: 'restrict' type qualifier on return type has no effect [-Wignored-qualifiers]
     int * restrict f8a(void);

but we don't warn about restrict-qualified types being used a return types generally. We also don't warn about restrict-qualified types in case expressions (which also don't convey any pointer-aliasing information because of how restrict's semantics are defined).

restrict's semantics are subtle, and there is a lot of misunderstanding about exactly how restrict can be used. Users often believe they can put restrict on function return types and in cast expressions to express pointer-aliasing information, and they can't (although the resulting code is legal).

This patch does two things. First, it adds a warning about indirectly-restrict-qualified function return types. For example, the code:

  typedef float * restrict floatp;
  typedef floatp realp;
  realp f8c(void);

will yield (when compiling with -Wignored-qualifiers):

  warning: restrict-qualified function return type has no effect [-Wignored-qualifiers]
  realp f8c(void);
  note: 'realp' declared here
  typedef floatp realp;
  note: 'floatp' declared here
  typedef float * restrict floatp;

(the patch adds code that tries to look back through a chain of typedefs to find where the restrict was introduced, and then produces a corresponding series of notes, hopefully making it easy for the user to see where the restrict is coming from).

Second, we add a corresponding warning for restrict in case expressions, like this:

  extern int *r, *q;
  r = (int * restrict)q;

  for(i=0; i<100; i++)
    *(int * restrict)p++ =   r[i];

will yield:

  warning: restrict-qualified type in a cast expression has no effect [-Wignored-qualifiers]
         r = (int * restrict)q;

  warning: restrict-qualified type in a cast expression has no effect [-Wignored-qualifiers]
             *(int * restrict)p++ =   r[i];

One remaining problem (and I'm not sure how to best solve this), the cast warning should not fire when the cast is the top-level expression on the RHS of an assignment, and the cast is making the assignment legal. Suggestions here would be greatly appreciated.

Thanks again!


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D5872.15151.patch
Type: text/x-patch
Size: 6076 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20141020/388f7a53/attachment.bin>

More information about the cfe-commits mailing list