gcc has warning "variable ‘c’ set but not used". However this warning is not printed when local variable is cleared using memset, and then not used. Attempt to compile following code using gcc 5.4.0 prints warning regarding c var, but not regarding buf var: #include <string.h> void test() { char buf[10]; memset(buf, 0, sizeof(buf)); int c; c = 1; } $ gcc -c -O3 -Wall -Wextra test.c -o test.o test.c: In function ‘test’: test.c:8:9: warning: variable ‘c’ set but not used [-Wunused-but-set-variable] int c; ^
Confirmed.
The challenge is that the DECL_READ_P flag is set indiscriminately by the front end (in convert_lvalue_to_rvalue called from c_parser_expr_list while parsing the argument list in the function call expression) in response to seeing the array being passed to a function as an argument. There's no logic that would take into account the properties of the function is being called. In general, GCC also has no notion of "out" only arguments (such as memset's first), or "in" only (such as memcpy's second). It may be a useful concept to introduce in the form of an attribute. With such an attribute, this issue could then be detected in for any function whose arguments were decorated with it.
"in" attribute is similar to "const", I am not sure if we need another one for this. "out" attribute would be handy. I recall than in the past I was looking for it. gcc can print warning that uninitialized variable is passed to function. Now one has to initialize that variable before passing it to function. With this attribute initialization would not be necessary. Beside silenced warning this also would produce a bit faster executable. BTW, C# provides "out" parameter modifier, but no "in".
Right, const could be used in lieu of an "in" attribute for warnings. It can't be used for the same thing for optimization because constness can be cast away. Bug 10138 has a good discussion on the subject. Let me see if I can prototype something.
Patch posted for review: https://gcc.gnu.org/ml/gcc-patches/2017-05/msg01493.html
I have checked list of my issues reported here and found Bug 68034 which is closely related to this one. This patch probably will fix that one too, user will know that his memset was removed by compiler.
My patch for this warning causes regressions in Wunused-var-8.c added for bug 44108. As it turns out, GCC considers it a use of a variable even when it's just mentioned in an unevaluated context (such as the sizeof expression in the call to memset in comment #0), without being evaluated.
Patch: https://gcc.gnu.org/ml/gcc-patches/2019-09/msg01690.html
GCC 10.1 has been released.
GCC 10.2 is released, adjusting target milestone.
GCC 10.3 is being released, retargeting bugs to GCC 10.4.
I'm not working on this aspect anymore (though all the pieces are in place to enable the warning).
GCC 10.4 is being released, retargeting bugs to GCC 10.5.