108 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -std=c++23 -Wformat-nonliteral -verify %s2#include <stdarg.h>3 4int printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));5 6struct 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 why11 // 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 // From C++23 'this' can also be specified explicitly.15 void g3(this S&, const char *, ...) __attribute__((format(printf, 2, 3)));16 void g4(this const char* s, ...) __attribute__((format(printf, 1, 2)));17 consteval operator const char*() const { return "%f"; } // #g4_fmt_string18 19 void h(const char*, ...) __attribute__((format(printf, 1, 4))); // \20 expected-error{{implicit this argument as the format string}}21 void h2(const char*, ...) __attribute__((format(printf, 2, 1))); // \22 expected-error{{out of bounds}}23 const char* h3(const char*) __attribute__((format_arg(1))); // \24 expected-error{{invalid for the implicit this argument}}25 void h4(this S&, const char *, ...) __attribute__((format(printf, 1, 3))); // \26 expected-error {{format argument not a string type}}27 28 void operator() (const char*, ...) __attribute__((format(printf, 2, 3)));29};30 31void s() {32 S().g4(4); // expected-warning {{format specifies type 'double' but the argument has type 'int'}}33 // expected-note@#g4_fmt_string {{format string is defined here}}34}35 36// PR552137struct A { void a(const char*,...) __attribute((format(printf,2,3))); };38void b(A x) {39 x.a("%d", 3);40}41 42// PR8625: correctly interpret static member calls as not having an implicit43// 'this' argument.44namespace PR8625 {45 struct S {46 static void f(const char*, const char*, ...)47 __attribute__((format(printf, 2, 3)));48 };49 void test(S s, const char* str) {50 s.f(str, "%s", str);51 }52}53 54// Make sure we interpret member operator calls as having an implicit55// this argument.56void test_operator_call(S s, const char *str) {57 s("%s", str);58}59 60template <typename... Args>61void format(const char *fmt, Args &&...args) // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}}62 __attribute__((format(printf, 1, 2)));63 64template <typename Arg>65Arg &expand(Arg &a) { return a; }66 67struct foo {68 int big[10];69 foo();70 ~foo();71 72 template <typename... Args>73 void format(const char *const fmt, Args &&...args) // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}}74 __attribute__((format(printf, 2, 3))) {75 printf(fmt, expand(args)...);76 }77};78 79void format_invalid_nonpod(const char *fmt, struct foo f) // expected-warning{{GCC requires a function with the 'format' attribute to be variadic}}80 __attribute__((format(printf, 1, 2)));81 82void do_format() {83 int x = 123;84 int &y = x;85 const char *s = "world";86 bool b = false;87 format("bare string");88 format("%s", 123); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}89 format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, &do_format);90 format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, do_format);91 format("bad format %s"); // expected-warning{{more '%' conversions than data arguments}}92 93 format("%c %c %hhd %hd %d\n", (char)'a', 'a', 'a', (short)123, (int)123);94 format("%f %f %f\n", (__fp16)123.f, 123.f, 123.);95 format("%Lf", (__fp16)123.f); // expected-warning{{format specifies type 'long double' but the argument has type '__fp16'}}96 format("%Lf", 123.f); // expected-warning{{format specifies type 'long double' but the argument has type 'float'}}97 format("%hhi %hhu %hi %hu %i %u", b, b, b, b, b, b);98 format("%li", b); // expected-warning{{format specifies type 'long' but the argument has type 'bool'}}99 100 struct foo f;101 format_invalid_nonpod("hello %i", f); // expected-warning{{format specifies type 'int' but the argument has type 'struct foo'}}102 103 f.format("%s", 123); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}}104 f.format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, &do_format);105 f.format("%s %s %u %d %i %p\n", "hello", s, 10u, x, y, do_format);106 f.format("bad format %s"); // expected-warning{{more '%' conversions than data arguments}}107}108