xref: /llvm-project/clang/test/SemaCXX/attr-format.cpp (revision 442f67c8702a792a135d61765909b732827d6bf2)
1 // RUN: %clang_cc1 -fsyntax-only -Wformat-nonliteral -verify %s
2 #include <stdarg.h>
3 
4 int printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
5 
6 struct S {
7   static void f(const char *, ...) __attribute__((format(printf, 1, 2)));
8   static const char *f2(const char *) __attribute__((format_arg(1)));
9 
10   // GCC has a hidden 'this' argument in member functions which is why
11   // the format argument is argument 2 here.
12   void g(const char*, ...) __attribute__((format(printf, 2, 3)));
13   const char* g2(const char*) __attribute__((format_arg(2)));
14 
15   void h(const char*, ...) __attribute__((format(printf, 1, 4))); // \
16       expected-error{{implicit this argument as the format string}}
17   void h2(const char*, ...) __attribute__((format(printf, 2, 1))); // \
18       expected-error{{out of bounds}}
19   const char* h3(const char*) __attribute__((format_arg(1))); // \
20       expected-error{{invalid for the implicit this argument}}
21 
22   void operator() (const char*, ...) __attribute__((format(printf, 2, 3)));
23 };
24 
25 // PR5521
26 struct A { void a(const char*,...) __attribute((format(printf,2,3))); };
b(A x)27 void b(A x) {
28   x.a("%d", 3);
29 }
30 
31 // PR8625: correctly interpret static member calls as not having an implicit
32 // 'this' argument.
33 namespace PR8625 {
34   struct S {
35     static void f(const char*, const char*, ...)
36       __attribute__((format(printf, 2, 3)));
37   };
test(S s,const char * str)38   void test(S s, const char* str) {
39     s.f(str, "%s", str);
40   }
41 }
42 
43 // Make sure we interpret member operator calls as having an implicit
44 // this argument.
test_operator_call(S s,const char * str)45 void test_operator_call(S s, const char *str) {
46   s("%s", str);
47 }
48 
49 template <typename... Args>
50 void format(const char *fmt, Args &&...args) // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}}
51     __attribute__((format(printf, 1, 2)));
52 
53 template <typename Arg>
expand(Arg & a)54 Arg &expand(Arg &a) { return a; }
55 
56 struct foo {
57   int big[10];
58   foo();
59   ~foo();
60 
61   template <typename... Args>
formatfoo62   void format(const char *const fmt, Args &&...args) // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}}
63       __attribute__((format(printf, 2, 3))) {
64     printf(fmt, expand(args)...);
65   }
66 };
67 
68 void format_invalid_nonpod(const char *fmt, struct foo f) // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}}
69     __attribute__((format(printf, 1, 2)));
70 
do_format()71 void do_format() {
72   int x = 123;
73   int &y = x;
74   const char *s = "world";
75   bool b = false;
76   format("bare string");
77   format("%s", 123); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
78   format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, &do_format);
79   format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, do_format);
80   format("bad format %s"); // expected-warning{{more '%' conversions than data arguments}}
81 
82   format("%c %c %hhd %hd %d\n", (char)'a', 'a', 'a', (short)123, (int)123);
83   format("%f %f %f\n", (__fp16)123.f, 123.f, 123.);
84   format("%Lf", (__fp16)123.f); // expected-warning{{format specifies type 'long double' but the argument has type '__fp16'}}
85   format("%Lf", 123.f); // expected-warning{{format specifies type 'long double' but the argument has type 'float'}}
86   format("%hhi %hhu %hi %hu %i %u", b, b, b, b, b, b);
87   format("%li", b); // expected-warning{{format specifies type 'long' but the argument has type 'bool'}}
88 
89   struct foo f;
90   format_invalid_nonpod("hello %i", f); // expected-warning{{format specifies type 'int' but the argument has type 'struct foo'}}
91 
92   f.format("%s", 123); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}
93   f.format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, &do_format);
94   f.format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, do_format);
95   f.format("bad format %s"); // expected-warning{{more '%' conversions than data arguments}}
96 }
97