[clang] [clang] Catch missing format attributes (PR #70024)
Budimir Aranđelović via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 9 04:37:47 PDT 2024
================
@@ -0,0 +1,277 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+
+typedef unsigned short char16_t;
+typedef unsigned int char32_t;
+typedef __WCHAR_TYPE__ wchar_t;
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *ch, size_t, const char *, va_list); // #vsnprintf
+
+__attribute__((__format__(__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */) // #f1
+{
+ va_list args;
+ vsnprintf(out, len, format, args); // expected-warning@#f1 {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f1'}}
+ // CHECK-FIXES: __attribute__((format(printf, 3, 4)))
+}
+
+__attribute__((__format__(__printf__, 1, 4)))
+void f2(char *out, const size_t len, const char *format, ... /* args */) // #f2
+{
+ va_list args;
+ vsnprintf(out, len, format, args); // expected-warning@#f2 {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f2'}}
+ // CHECK-FIXES: __attribute__((format(printf, 3, 4)))
+}
+
+void f3(char *out, va_list args) // #f3
+{
+ vprintf(out, args); // expected-warning@#f3 {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f3'}}
+ // CHECK-FIXES: __attribute__((format(printf, 1, 0)))
+ vscanf(out, args); // expected-warning@#f3 {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f3'}}
+ // CHECK-FIXES: __attribute__((format(scanf, 1, 0)))
----------------
budimirarandjelovicsyrmia wrote:
I edited code so now it does not emit diagnostic when there are different format functions in the same function body. However, I wonder if diagnostic could be emitted in this situation:
`void f41(char *out, ... /* args */) // #f41
{
va_list args;
char *ch;
vscanf("%s", ch); // expected-no-warning@#f41
vprintf(out, args); // expected-no-warning@#f41
}`
Currently, there is no diagnostic due to different format functions. If there were only one format function (ie. either vscanf or vprintf), diagnostic would be emitted only for latter one (vprintf). As there are more than one format function and va_list only appears as argument in one of them, does it make sense to emit diagnostic for one where format argument appears?
https://github.com/llvm/llvm-project/pull/70024
More information about the cfe-commits
mailing list