GCC ignoring the "access" function-attribute? - c

I am currently getting into using attributes using c. However when trying to use the "access" attribute for funtions. However when compiling I get a warning saying the attribute is ignored.
This is a minimal example that has this issue arise:
#include <stdio.h>
__attribute__ ((access (read_only,1))) void printAll(double * arr,size_t size){
for(int i=0;i<size;i++) printf("%lf\n", arr[i]);
}
int main(){
double testArr[]={1,2,3,4,5,6};
printAll(testArr,6);
}
The compiler gives the following warning:
warning: 'access' attribute directive ignored [-Wattributes] __attribute__ ((access (read_only,1))) void printAll(double * arr,size_t size){
I read about this issue arising with other attributes.
And one solution was that the gcc version may be relevant.
I still have gcc version 8.3.0 from an older installation.

Related

Clang parameter -Wno-conditional-type-mismatch and GCC

The following simple C example program correctly emits a compile warning:
#include <stdio.h>
int main(int argc, char *argv[])
{
int *p = NULL;
int q = 1;
if(1 == 2 ? p : q) { printf("Info\n"); }
return(0);
}
The warning emitted is "warning: pointer/integer type mismatch in conditional expression".
In clang, the parameter "-Wno-conditional-type-mismatch" works in such a way that no warnings appear for the example code.
In GCC, I was looking for a similar option. The best I could find was the parameter "-fcond-mismatch", which would allow the example code. From the man page:
Allow conditional expressions with mismatched types in the second and third arguments. The
value of such an expression is void. This option is not supported for C++.
But when using this option, GCC keeps complaining:
# gcc -fcond-mismatch -c example.c
example.c: In function 'main':
example.c:8:17: warning: pointer/integer type mismatch in conditional expression
8 | if(1 == 2 ? p : q) { printf("Info\n"); }
Using GCC 10.3.0, would there be a GCC option or compiler flag to suppress the warning, in a similar way as clang does?
I don't think there is any such flag. GCC's manual says, under -Werror=, "The warning message for each controllable warning includes the option that controls the warning."
Since this message does not mention such an option, this suggests it is not controllable.

Telling gcc to fail to compile when in pre-C99 mode

I am giving a lecture on the history of C, and would like to show some idioms that used to be impossible but now are (in particular, defining variables in the middle of the block). I would like to show that older C compilers wouldn't compile it.
gcc has the -std= option for setting the language standard. Unfortunately, setting it for -std=c89 does not produce compilation errors on defining variables in the middle of a block.
I was hoping for a more accurate std version (i.e. - -std=knr), but I could not find any such option.
Am I missing something? Is this a bug in GCC?
gcc (Ubuntu 8.2.0-7ubuntu1) 8.2.0
The code that erroneously compiles:
#include <stdio.h>
int main(argc, argv)
int argc;
char *argv[];
{
printf("Hello, world\n");
int a;
return 0;
}
If you want truly strict conformance to a -std flag, it should be accompanied by the -pedantic-errors flag.
As a demonstration, your code on wandbox with those flags produces:
prog.c: In function 'main':
prog.c:9:9: error: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
int a;
^~~

Clang: Do not optimize a specific function

For a long time i used gcc to compile C code. Sometimes i had to use the optimize("O0") attribute to disable optimizations for a specific function. Now i like to do this with clang.
Assume the following code:
#include <stdio.h>
void __attribute__((optimize("O0"))) blabla(void) {
}
int main(void) {
blabla();
return 0;
}
If i compile it with clang i get this error:
test2.c:3:21: warning: unknown attribute 'optimize' ignored [-Wattributes]
void __attribute__((optimize("O0"))) blabla(void) {
^
1 warning generated.
Then i used google (and also) stackoverflow to find out what attribute is required for clang, because many of them are not in the standard (as soon as i know).
I found this thread:
In clang, how do you use per-function optimization attributes?
If i try the attribute optimize("0") i get this error:
test2.c:3:21: warning: unknown attribute 'optimize' ignored [-Wattributes]
void __attribute__((optimize("0"))) blabla(void) {
^
1 warning generated.
And if i try the attribute optnone i get this error:
test2.c:3:21: warning: unknown attribute 'optnone' ignored [-Wattributes]
void __attribute__((optnone)) blabla(void) {
^
1 warning generated.
I also tried to move the attribute after the function name, but it doesn't work (for some reason there is a warning about GCC?!):
test2.c:3:34: warning: GCC does not allow optnone attribute in this position on a function definition [-Wgcc-compat]
void blabla(void) __attribute__((optnone)) {
^
test2.c:3:34: warning: unknown attribute 'optnone' ignored [-Wattributes]
2 warnings generated.
Another test with the following code:
#include <stdio.h>
[[clang::optnone]]
void blabla(void) {
}
int main(void) {
blabla();
return 0;
}
It produces:
user#ubuntu:/tmp/optxx$ clang test2.c
test2.c:3:1: error: expected identifier or '('
[[clang::optnone]]
^
test2.c:3:2: error: expected expression
[[clang::optnone]]
^
test2.c:8:5: warning: implicit declaration of function 'blabla' is invalid in C99 [-Wimplicit-function-declaration]
blabla();
^
1 warning and 2 errors generated.
Probably i do something wrong, but i cannot see what.
-edit-
clang version:
user#ubuntu:/tmp/optxx$ clang -v
Ubuntu clang version 3.3-16ubuntu1 (branches/release_33) (based on LLVM 3.3)
Target: x86_64-pc-linux-gnu
Thread model: posix
Try the following, clang-style attribute specification:
[[clang::optnone]]
void blabla(void);
EDIT: Clang 3.3 is pretty outdated. Use a more recent version, and your original ((optnone)) code will work.

Compiling mathgl C-Example

I'm trying to compile the first example of the MathGL library in C:
http://mathgl.sourceforge.net/doc_en/Examples.html
#include <mgl2/mgl_cf.h>
int main()
{
HMGL gr = mgl_create_graph(600,400);
mgl_fplot(gr,"sin(pi*x)","","");
mgl_write_frame(gr,"test.png","");
mgl_delete_graph(gr);
}
I installed libmgl-dev using aptitude and rejected the first option offered, because aptitude wanted to remove a number of different programs and accepted the second option, which only upgraded a few.
If I try to compile:
gcc test.c -o test -lmgl
In file included from /usr/include/mgl2/mgl_cf.h:29:0,
from test.c:1:
/usr/include/mgl2/data_cf.h:318:78: error: unknown type name ‘bool’
EXPORT mgl_fft(double *x, long s, long n, const void *wt, void *ws, bool inv);
So I tried to add #include <stdbool.h> and tried flags like -std=gnu11 or -std=c11 and -std=c99. None of which worked. I tried adding the flag -lmgl (I even read that I should put it at the very end).
How do I compile this example?
You appear to have an old version of the library, in which this is a bug.
See this bug report
from May 2013. The Alexey Balakin who confirms the bug is the lead developer of MathGL.
See that the bug is fixed in MathGL 2.3.3 version of data_ch.h,
where the declaration is:
void MGL_EXPORT mgl_fft(double *x, long s, long n, const void *wt, void *ws, int inv);
with bool replaced by int.

How to enforce the usage of return values in C

I'm searching for a compiler flag for gcc and if possible for clang and the Microsoft compilers as well, that triggers a warning (error with -Werror) if a non-void function is called without using the return value like this:
int test() {
return 4;
}
int main(void) {
test(); //should trigger a warning
int number = test(); //shouldn't trigger the warning
return 0;
}
If there is no such compiler flag, maybe some way to tell the clang static analyzer to complain about it.
EDIT: To clarify my original question: I actually meant using the return value, not only assigning it.
I never used it myself (do you really need it?), you can try
defining the function with warn_unused_result attribute
enabling -Wunused-result flag with gcc.
This will tell you about any unused value from the function return.
In case, any doubts, SEE IT LIVE or SEE IT LIVE AGAIN Thanks to M.M for the link in the comment
Or:
#include <stdio.h>
extern int func1(void) __attribute__((warn_unused_result));
extern int func2(void);
int main(void)
{
func1();
int rc1 = func1();
int rc2 = func1();
func2();
printf("%d\n", rc1);
return 0;
}
Compilation (GCC 5.1.0 on Mac OS X 10.10.5):
$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -c warn.c
warn.c: In function ‘main’:
warn.c:10:9: error: unused variable ‘rc2’ [-Werror=unused-variable]
int rc2 = func1();
^
warn.c:8:5: error: ignoring return value of ‘func1’, declared with attribute warn_unused_result [-Werror=unused-result]
func1();
^
cc1: all warnings being treated as errors
$
Some static code analyzers like splint can check for these kind of things:
$ splint check.c
Splint 3.1.2 --- 03 May 2009
check.c: (in function main)
check.c:6:5: Return value (type int) ignored: test()
Result returned by function call is not used. If this is intended, can cast
result to (void) to eliminate message. (Use -retvalint to inhibit warning)
Unlike #Sourav's answer, this does not require a specific __attribute__ annotation on the target function, but on the other hand possibly emits many warnings. Its usually possible to suppress the warnings for specific functions or function calls by using annotations (like /*#alt void#*/).

Resources