Get rid of “type qualifier” warnings on functions

2019-08-09 04:14发布

问题:

I'm trying to clean up warnings that I'm getting when compiling Blitz++ of the form:

/opt/local/include/blitz/tinyvec2.h:261:35: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
/opt/local/include/blitz/tinyvec2.h:264:43: warning: type qualifiers ignored on function return type [-Wignored-qualifiers] 
/opt/local/include/blitz/tinyvec2.h:267:40: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
<<etc.>>

From these kinds of member functions (of the TinyVector class)

T_numtype * restrict data()    // line 261                      
    { return data_; }          // data is a member that is an array of data values

const T_numtype * restrict data() const       // line 264       
    { return data_; }       

As far as I can tell I am getting the warnings due to the restrict keyword. Note: there are macros that should replace the restrict with __restrict__ (which g++ understands).

I can get rid of the warnings by deleting the keywords; however, since this is supposed to be a high-performance numerical library, I don't want to lose any compiler optimizations that the restrict keywords are allowing.

What can I do to suppress these warnings without just dropping the restrict's altogether, and while keeping -Wall on?

回答1:

README

__restrict__ has no usage on return-types, which is exactly why gcc is giving you the diagnostic. The keyword is used to signal that two pointers passed to a function doesn't point to the same object, ie. that they are unique; changing one will not affect the other. This can greatly improve optimizations, but it's worth noting that it won't affect the returned value of a function.

There's no reason for you to apply __restrict__ on the return type.

The above README is written by me, I used quotations to draw some extra attention to it since the information contained is important to the matter at hand..


Ignore warning using gcc specific pragma

You could ask gcc to disregard diagnostics matching -Wignored-qualifiers by using diagnostic pragmas.

  • Diagnostic Pragmas - Using the GNU Compiler Collection (gcc)

Ignore it everywhere

This will ignore every warning that -Wignored-qualifiers would enable;

#pragma GCC diagnostic ignored "-Wignored-qualifiers"

Ignore it for a seleted area

This will ignore -Wignored-qualifiers related warnings for the code replaced by ...

... // warnings enabled

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wignored-qualifiers"

... // warning disabled here

#pragma GCC diagnostic pop

... // warnings enabled again

Note: -Wignored-qualifiers is enabled through -Wextra, not -Wall.