--- /dev/null
+/* Test for format attributes: test use of __attribute__. */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99 -Wformat" } */
+
+#include "format.h"
+
+extern void tformatprintf (const char *, ...) __attribute__((format(printf, 1, 2)));
+extern void tformat__printf__ (const char *, ...) __attribute__((format(__printf__, 1, 2)));
+extern void tformatscanf (const char *, ...) __attribute__((format(scanf, 1, 2)));
+extern void tformat__scanf__ (const char *, ...) __attribute__((format(__scanf__, 1, 2)));
+extern void tformatstrftime (const char *) __attribute__((format(strftime, 1, 0)));
+extern void tformat__strftime__ (const char *) __attribute__((format(__strftime__, 1, 0)));
+extern void tformatstrfmon (const char *, ...) __attribute__((format(strfmon, 1, 2)));
+extern void tformat__strfmon__ (const char *, ...) __attribute__((format(__strfmon__, 1, 2)));
+extern void t__format__printf (const char *, ...) __attribute__((__format__(printf, 1, 2)));
+extern void t__format____printf__ (const char *, ...) __attribute__((__format__(__printf__, 1, 2)));
+extern void t__format__scanf (const char *, ...) __attribute__((__format__(scanf, 1, 2)));
+extern void t__format____scanf__ (const char *, ...) __attribute__((__format__(__scanf__, 1, 2)));
+extern void t__format__strftime (const char *) __attribute__((__format__(strftime, 1, 0)));
+extern void t__format____strftime__ (const char *) __attribute__((__format__(__strftime__, 1, 0)));
+extern void t__format__strfmon (const char *, ...) __attribute__((__format__(strfmon, 1, 2)));
+extern void t__format____strfmon__ (const char *, ...) __attribute__((__format__(__strfmon__, 1, 2)));
+
+extern char *tformat_arg (const char *) __attribute__((format_arg(1)));
+extern char *t__format_arg__ (const char *) __attribute__((__format_arg__(1)));
+
+void
+foo (int i, int *ip, double d)
+{
+ tformatprintf ("%d", i);
+ tformatprintf ("%"); /* { dg-warning "format" "attribute format printf" } */
+ tformat__printf__ ("%d", i);
+ tformat__printf__ ("%"); /* { dg-warning "format" "attribute format __printf__" } */
+ tformatscanf ("%d", ip);
+ tformatscanf ("%"); /* { dg-warning "format" "attribute format scanf" } */
+ tformat__scanf__ ("%d", ip);
+ tformat__scanf__ ("%"); /* { dg-warning "format" "attribute format __scanf__" } */
+ tformatstrftime ("%a");
+ tformatstrftime ("%"); /* { dg-warning "format" "attribute format strftime" } */
+ tformat__strftime__ ("%a");
+ tformat__strftime__ ("%"); /* { dg-warning "format" "attribute format __strftime__" } */
+ tformatstrfmon ("%n", d);
+ tformatstrfmon ("%"); /* { dg-warning "format" "attribute format strfmon" } */
+ tformat__strfmon__ ("%n", d);
+ tformat__strfmon__ ("%"); /* { dg-warning "format" "attribute format __strfmon__" } */
+ t__format__printf ("%d", i);
+ t__format__printf ("%"); /* { dg-warning "format" "attribute __format__ printf" } */
+ t__format____printf__ ("%d", i);
+ t__format____printf__ ("%"); /* { dg-warning "format" "attribute __format__ __printf__" } */
+ t__format__scanf ("%d", ip);
+ t__format__scanf ("%"); /* { dg-warning "format" "attribute __format__ scanf" } */
+ t__format____scanf__ ("%d", ip);
+ t__format____scanf__ ("%"); /* { dg-warning "format" "attribute __format__ __scanf__" } */
+ t__format__strftime ("%a");
+ t__format__strftime ("%"); /* { dg-warning "format" "attribute __format__ strftime" } */
+ t__format____strftime__ ("%a");
+ t__format____strftime__ ("%"); /* { dg-warning "format" "attribute __format__ __strftime__" } */
+ t__format__strfmon ("%n", d);
+ t__format__strfmon ("%"); /* { dg-warning "format" "attribute __format__ strfmon" } */
+ t__format____strfmon__ ("%n", d);
+ t__format____strfmon__ ("%"); /* { dg-warning "format" "attribute __format__ __strfmon__" } */
+ tformatprintf (tformat_arg ("%d"), i);
+ tformatprintf (tformat_arg ("%")); /* { dg-warning "format" "attribute format_arg" } */
+ tformatprintf (t__format_arg__ ("%d"), i);
+ tformatprintf (t__format_arg__ ("%")); /* { dg-warning "format" "attribute __format_arg__" } */
+}
--- /dev/null
+/* Test for format attributes: test bad uses of __attribute__. */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+/* { dg-do compile } */
+/* { dg-options "-std=gnu99 -Wformat" } */
+
+#include "format.h"
+
+/* Proper uses of the attributes. */
+extern void fa0 (const char *, ...) __attribute__((format(printf, 1, 2)));
+extern void fa1 (char *, ...) __attribute__((format(printf, 1, 2)));
+extern char *fa2 (const char *) __attribute__((format_arg(1)));
+extern char *fa3 (char *) __attribute__((format_arg(1)));
+
+/* Uses with too few or too many arguments. */
+extern void fb0 (const char *, ...) __attribute__((format)); /* { dg-error "wrong number of arguments" "bad format" } */
+extern void fb1 (const char *, ...) __attribute__((format())); /* { dg-error "wrong number of arguments" "bad format" } */
+extern void fb2 (const char *, ...) __attribute__((format(printf))); /* { dg-error "wrong number of arguments" "bad format" } */
+extern void fb3 (const char *, ...) __attribute__((format(printf, 1))); /* { dg-error "wrong number of arguments" "bad format" } */
+extern void fb4 (const char *, ...) __attribute__((format(printf, 1, 2, 3))); /* { dg-error "wrong number of arguments" "bad format" } */
+
+extern void fc1 (const char *) __attribute__((format_arg)); /* { dg-error "wrong number of arguments" "bad format_arg" } */
+extern void fc2 (const char *) __attribute__((format_arg())); /* { dg-error "wrong number of arguments" "bad format_arg" } */
+extern void fc3 (const char *) __attribute__((format_arg(1, 2))); /* { dg-error "wrong number of arguments" "bad format_arg" } */
+
+/* These attributes presently only apply to declarations, not to types.
+ Eventually, they should be usable with declarators for function types
+ anywhere, but still not with structure/union/enum types. */
+struct s0 { int i; } __attribute__((format(printf, 1, 2))); /* { dg-error "does not apply" "format on struct" } */
+union u0 { int i; } __attribute__((format(printf, 1, 2))); /* { dg-error "does not apply" "format on union" } */
+enum e0 { E0V0 } __attribute__((format(printf, 1, 2))); /* { dg-error "does not apply" "format on enum" } */
+
+struct s1 { int i; } __attribute__((format_arg(1))); /* { dg-error "does not apply" "format_arg on struct" } */
+union u1 { int i; } __attribute__((format_arg(1))); /* { dg-error "does not apply" "format_arg on union" } */
+enum e1 { E1V0 } __attribute__((format_arg(1))); /* { dg-error "does not apply" "format_arg on enum" } */
+
+/* At present, only functions can be declared with these attributes.
+ Once they can be applied to function types in function pointers, etc.,
+ these tests should be removed, and tests should be added (say in a new
+ testcase attr-<num>.c) that such attributes work and calls through such
+ function pointers (etc.) get checked. */
+extern void (*fd0) (const char *, ...) __attribute__((format(printf, 1, 2))); /* { dg-error "non-function" "format on non-function" } */
+extern char *(*fd1) (const char *) __attribute__((format_arg(1))); /* { dg-error "non-function" "format on non-function" } */
+
+/* The format type must be an identifier, one of those recognised. */
+extern void fe0 (const char *, ...) __attribute__((format(12345, 1, 2))); /* { dg-error "format specifier" "non-id format" } */
+extern void fe1 (const char *, ...) __attribute__((format(nosuch, 1, 2))); /* { dg-warning "format function type" "unknown format" } */
+
+/* Both the numbers must be integer constant expressions. */
+extern void ff0 (const char *, ...) __attribute__((format(printf, 3-2, (long long)(10/5))));
+int foo;
+extern void ff1 (const char *, ...) __attribute__((format(printf, foo, 10/5))); /* { dg-error "invalid operand" "bad number" } */
+extern void ff2 (const char *, ...) __attribute__((format(printf, 3-2, foo))); /* { dg-error "invalid operand" "bad number" } */
+extern char *ff3 (const char *) __attribute__((format_arg(3-2)));
+extern char *ff4 (const char *) __attribute__((format_arg(foo))); /* { dg-error "invalid operand" "bad format_arg number" } */
+
+/* The format string argument must precede the arguments to be formatted.
+ This includes if no parameter types are specified (which is not valid ISO
+ C for variadic functions). */
+extern void fg0 () __attribute__((format(printf, 1, 2)));
+extern void fg1 () __attribute__((format(printf, 1, 0)));
+extern void fg2 () __attribute__((format(printf, 1, 1))); /* { dg-error "follows" "bad number order" } */
+extern void fg3 () __attribute__((format(printf, 2, 1))); /* { dg-error "follows" "bad number order" } */
+
+/* The format string argument must be a string type, and the arguments to
+ be formatted must be the "...". */
+extern void fh0 (int, ...) __attribute__((format(printf, 1, 2))); /* { dg-error "not a string" "format int string" } */
+extern void fh1 (signed char *, ...) __attribute__((format(printf, 1, 2))); /* { dg-error "not a string" "signed char string" } */
+extern void fh2 (unsigned char *, ...) __attribute__((format(printf, 1, 2))); /* { dg-error "not a string" "unsigned char string" } */
+extern void fh3 (const char *, ...) __attribute__((format(printf, 1, 3))); /* { dg-error "is not" "not ..." } */
+extern void fh4 (const char *, int, ...) __attribute__((format(printf, 1, 2))); /* { dg-error "is not" "not ..." } */
+
+/* format_arg formats must take and return a string. */
+extern char *fi0 (int) __attribute__((format_arg(1))); /* { dg-error "not a string" "format_arg int string" } */
+extern char *fi1 (signed char *) __attribute__((format_arg(1))); /* { dg-error "not a string" "format_arg signed char string" } */
+extern char *fi2 (unsigned char *) __attribute__((format_arg(1))); /* { dg-error "not a string" "format_arg unsigned char string" } */
+extern int fi3 (const char *) __attribute__((format_arg(1))); /* { dg-error "not return string" "format_arg ret int string" } */
+extern signed char *fi4 (const char *) __attribute__((format_arg(1))); /* { dg-error "not return string" "format_arg ret signed char string" } */
+extern unsigned char *fi5 (const char *) __attribute__((format_arg(1))); /* { dg-error "not return string" "format_arg ret unsigned char string" } */