Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameters declared restrict and compiler warnings

Neither gcc 5 nor clang 3.6 give warnings where the constraints of the restrict qualifier are violated, even when called with -Wall. Consider the following code fragment:

extern void f(char *restrict p, char *restrict q);

void g(char *p)
{
    f(p, p);
}

Naively, I'd expect that the violation can be determined statically, and I was expecting that -Wall would give a warning. Have I missed a flag somewhere, or is there some problem with giving warnings that I'm not seeing?

like image 216
jch Avatar asked Jun 05 '15 00:06

jch


People also ask

What are compiler warnings?

Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors. Unlike compilation errors, warnings don't interrupt the compilation process.

Which option can be used to display compiler warnings?

You can request many specific warnings with options beginning with ' -W ', for example -Wimplicit to request warnings on implicit declarations. Each of these specific warning options also has a negative form beginning ' -Wno- ' to turn off warnings; for example, -Wno-implicit .

How do I stop a GCC warning?

If the value of y is always 1, 2 or 3, then x is always initialized, but GCC doesn't know this. To suppress the warning, you need to provide a default case with assert(0) or similar code. This option also warns when a non-volatile automatic variable might be changed by a call to longjmp.

How do you stop warnings in Cmake?

Use -Wno-dev to suppress it. You can disable the warning like this when you are configuring your build.


2 Answers

Starting with version 8, gcc gives a helpful warning for the above code:

a.c: In function ‘g’:
a.c:5:5: warning: passing argument 1 to restrict-qualified parameter aliases with argument 2 [-Wrestrict]
     f(p, p);
     ^
like image 150
jch Avatar answered Sep 29 '22 10:09

jch


The restrict keyword is an explicit assurance from the programmer that the pointers in question don't alias. In essence, it allows the compiler to omit alias analysis for those pointers, because the programmer has already provided the presumed answer. In addition to enabling better optimization, this can also save compilation time. In large programs, analysis can be quite expensive, so that's potentially a big deal in its own right.

So, the answer to your question is, I believe, "the compilers aren't looking, because the code tells them not to bother"

like image 22
Phil Miller Avatar answered Sep 29 '22 10:09

Phil Miller