[clang] [clang] Catch missing format attributes (PR #70024)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 6 06:34:59 PST 2023


================
@@ -0,0 +1,58 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c99 -Wmissing-format-attribute %s
+
+#include <stdarg.h>
+#include <stdio.h>
+
+__attribute__((__format__ (__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */)
+{
+    va_list args;
+    vsnprintf(out, len, format, args); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f1'}}
+                                       // CHECK-FIXES: __attribute__((format(printf, 1, 4)))
+}
+
+void f2(char *out, va_list args)
+{
+    vprintf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f2'}}
+                        // CHECK-FIXES: __attribute__((format(printf, 1, 0)))
+    vscanf(out, args); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f2'}}
+                       // CHECK-FIXES: __attribute__((format(scanf, 1, 0)))
+}
+
+void f3(char* out, ... /* args */)
+{
+    va_list args;
+    vprintf("test", args); // no warning
+}
+
+void f4(char *out, ... /* args */)
+{
+    const char *ch;
+    va_list args;
+    vscanf(ch, args); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f4'}}
+                      // CHECK-FIXES: __attribute__((format(scanf, 1, 2)))
+}
+
+void f5(va_list args)
+{
+    char *ch;
+    vscanf(ch, args); // no warning
+}
+
+void f6(char *out, va_list args)
+{
+    char *ch;
+    vscanf(ch, args); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f6'}}
+                      // CHECK-FIXES: __attribute__((format(scanf, 1, 0)))
+}
+
+void f7(const char *out, ... /* args */)
+{
+    va_list args;
+
+    vscanf(out, &args[0]); // expected-warning {{diagnostic behavior may be improved by adding the 'scanf' format attribute to the declaration of 'f7'}}
+                           // CHECK-FIXES: __attribute__((format(scanf, 1, 2)))
+    vprintf(out, &args[0]); // expected-warning {{diagnostic behavior may be improved by adding the 'printf' format attribute to the declaration of 'f7'}}
+                            // CHECK-FIXES: __attribute__((format(printf, 1, 2)))
+}
----------------
AaronBallman wrote:

Please add a newline to the end of the file.

https://github.com/llvm/llvm-project/pull/70024


More information about the cfe-commits mailing list