[clang] [Sema] Suggest missing format attributes (PR #166738)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 19 18:50:28 PST 2025
https://github.com/apple-fcloutier requested changes to this pull request.
Thanks for the format_matches support (and reviewing again, I now see that ReleaseNotes.rst was updated very early on). The last bits that I'm still tracking:
1. -Wmissing-format-attribute should be a subgroup of -Wformat-nonliteral (should have a test that with both enabled, you only get -Wmissing-format-attribute). We should have a test for it as well.
2. We should not add the implicit attribute if diag::warn_missing_format_attribute is ignored, since it would create downstream diagnostics that cannot be disabled in any reasonable way.
I expect these are the last changes I'll request, everything else looks good.
----
Example for point 1:
```c
// clang -fsyntax-only -Wformat-nonliteral -Wmissing-format-attribute test.c
#include <stdarg.h>
__attribute__((format(printf, 1, 0)))
int vprintf(const char *, va_list);
void foo(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
// warning: diagnostic behavior may be improved by adding the 'format(printf, 1, 2)' attribute to the declaration of 'foo' [-Wmissing-format-attribute]
// warning: format string is not a string literal [-Wformat-nonliteral]
}
```
The expected result is that we have just the -Wmissing-format-attribute since they are both complaining about the same thing but -Wmissing-format-attribute knows how to fix it. Enabling -Wformat-nonliteral should enable -Wmissing-format-attribute as well since it is now the better diagnostic for that situation.
---
Example for point 2:
```c
// clang -fsyntax-only -Wformat
#include <stdarg.h>
__attribute__((format(printf, 1, 0)))
int vprintf(const char *, va_list);
void foo(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
}
void bar(void) {
foo("%g\n", 123);
// warning: format specifies type 'double' but the argument has type 'int' [-Wformat]
}
```
`foo` is not attributed in source and there is no clear explanation for what's going on; if there's ever a false positive, there is no way to prevent clang from adding the format attribute implicitly to `foo`. The expected result is that if -Wmissing-format-attribute is disabled, then we should not implicitly add the format attribute.
https://github.com/llvm/llvm-project/pull/166738
More information about the cfe-commits
mailing list