Before clang 16.0 I was able to compile all my projects using -Weveything without problems, fixing my code and paying attention to the (useful) warnings that this option gave. However, it seems that the -Wunsafe-buffer-usage was added in version 16 (my system updated to it recently) and then compiling any project is impossible without getting a huge list of useless warnings about any pointer being used, see minimal example below:
$ cat main.c
#include <stdio.h>
int main(int argc, char **argv) {
if (argc != 2) {
return 0;
}
printf("argv[1] = %s\n", argv[1]);
}
$ clang -Weverything main.c
main.c:3:27: warning: 'argv' is an unsafe pointer used for buffer access [-Wunsafe-buffer-usage]
int main(int argc, char **argv) {
~~~~~~~^~~~
main.c:7:30: note: used in buffer access here
printf("argv[1] = %s\n", argv[1]);
^~~~
1 warning generated.
One can't possibly reason about his code using this warning.
The clang documentation has the following to say:
Since -Weverything enables every diagnostic, we generally don’t recommend using it. -Wall -Wextra are a better choice for most projects. Using -Weverything means that updating your compiler is more difficult because you’re exposed to experimental diagnostics which might be of lower quality than the default ones. If you do use -Weverything then we advise that you address all new compiler diagnostics as they get added to Clang, either by fixing everything they find or explicitly disabling that diagnostic with its corresponding Wno- option.
In this case, how would I "fix everything they find"?
Setting -Wno-unsafe-buffer-usage
works, but it is an ugly solution.
It adds clutter to a Makefile and feels like cheating.
-Weverything
may not be a battle you want to start. You may be able to make-Weverything
happy in this case, but there will be many more warnings you'll need to satisfy, many of which may require making your code worse to "fix". For example, satisfying-Wdeclaration-after-statement
would require declaring all local variables at the top of functions instead of as needed. – Ligniform-Wdeclaration-after-statement
makes me organize my declarations in the proper scopes. Note that it does not complain aboutfor (int i = 0; i < n; i += 1)
. If it did, then I would agree with you with this particular example. – Dill-Weverything
, I also counter some options with-Wno-xyz
variants. One script I use has:-Weverything -Wno-padded -Wno-vla -Wno-reserved-id-macro -Wno-documentation-unknown-command -Wno-poison-system-directories -Wno-format-nonliteral
. I build format strings in a number of the programs, hence-Wno-format-nonliteral
. I use VLAs in matrix manipulation code (and carefully in other places too), so I need-Wno-vla
. I use feature test macros (for POSIX, and also systems like Solaris) to determine how to compile code, so-Wno-reserved-id-macro
was necessary. […continued…] – Impoverished-Wno-documentation-unknown-command
was because clang undertook to critique a comment containing "** Hex character constant - \xHH or \xH, where H is a hex digit.
". I needed-Wno-poison-system-directories
because clang designated/usr/local/include
as unsafe for cross-compilation. So,-Weverything
is a blunderbuss that, for me, does not work on real-world programs (or, at least, it does not work for me in my tiny little corner of the real world). I've not run afoul of that option yet, but I've not recently checked my code thoroughly on my Mac. – Impoverished-Wunsafe-buffer-usage
, maybe there lies the answer. – Dill-Weverything
. My Mac has Apple's Clang 14.0; the-Wunsafe-buffer-usage
option isn't valid. – Impoverished-Wno-unsafe-buffer-usage
feels like cheating, doesn't-Weverything
feel like an unreasonable handicap?-Wall -Wextra
is expected to be a useful set of warnings, as your quoted extract states. – Trophoplasm-Wunsafe-buffer-usage
warning does appear to be of the same ilk as Microsoft's "deprecation" of the majority of the C standard library functionality. In other words, completely useless in practice.argv
is an "unsafe buffer"? I have to wonder at the thinking that came up with that. Or rather, the lack of thinking. – Whitesmith-Wunsafe-buffer-usage
is enabled and the compilation doesn't trigger lots of warnings, I appreciate it, since I'm trying to learn about it. Otherwise, I agree with you that code written to high standards should have no warnings, which is what I did to my code before this diagnostic was added to clang. – Dillwarning: 'argv' is an unsafe pointer used for buffer access
, umm, unreasonable. If I used clang, I'd file a bug report for that "warning" so the devs would get real feedback. It's "unsafe" to access the command-line arguments of a process?!?!? That makes no sense. – Whitesmith-Wall -Wextra
that manually selecting them is tedious and error prone, besides adding clutter to a Makefile. – DillCFLAGS+=
lines in my Makefile. I already have similar blocks for includes, definitions and code-generation, as well as similar blocks for linker flags and libraries. It's not clutter when it's logically organised! – Trophoplasm