[Bug c++/60835] New: Please support __attribute__((format_arg)) on class types as well as char*
b.r.longbons at gmail dot com
gcc-bugzilla@gcc.gnu.org
Sun Apr 13 22:21:00 GMT 2014
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60835
Bug ID: 60835
Summary: Please support __attribute__((format_arg)) on class
types as well as char*
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: enhancement
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: b.r.longbons at gmail dot com
It would be nice if GCC could remember if an "opaque" class contains a format
string.
The logical extension of this is that a function with __attribute__((format()))
could take a class type that was returned from the __attribute__((format_arg))
function.
Other than ordinary functions, the low-level cases are:
- constructor: std::string::string(const char *) (possibly complicated)
- member function: const char *std::string::c_str() (probably easy)
- UDL: std::string operator "" s (const char *, size_t) (already works for
non-class return types)
Some high level uses:
- Returning an owned string from a gettext()-like function.
- Completely forbidding char* and char[] for strings in a codebase (currently,
I'm using a nop UDL on the format strings and a real UDL on all others, which
isn't terrible but it would be nice to complete it).
Testcase:
// compile with -Wformat=2 -std=c++11
#include <cstdio>
struct String
{
const char *str;
// 'this' is arg 1; there is no return type for a ctor
__attribute__((format_arg(2)))
String(const char *s) : str(s) {}
__attribute__((format_arg(1)))
const char *c_str() const { return str; }
};
static __attribute__((format_arg(1)))
String operator "" _s(const char *s, size_t)
{
return String(s);
}
int main()
{
printf("Hello, world!\n"_s.c_str());
}
More information about the Gcc-bugs
mailing list