| 1 |
/* Test for format attributes: test bad uses of __attribute__. */ |
| 2 |
/* Origin: Joseph Myers <[email protected]> */ |
| 3 |
/* { dg-do compile } */ |
| 4 |
/* { dg-options "-std=gnu99 -Wformat" } */ |
| 5 |
|
| 6 |
#define DONT_GNU_PROTOTYPE |
| 7 |
#include "format.h" |
| 8 |
|
| 9 |
/* Proper uses of the attributes. */ |
| 10 |
extern void fa0 (const char *, ...) __attribute__((format(gnu_attr_printf, 1, 2))); |
| 11 |
extern void fa1 (char *, ...) __attribute__((format(gnu_attr_printf, 1, 2))); |
| 12 |
extern char *fa2 (const char *) __attribute__((format_arg(1))); |
| 13 |
extern char *fa3 (char *) __attribute__((format_arg(1))); |
| 14 |
|
| 15 |
/* Uses with too few or too many arguments. */ |
| 16 |
extern void fb0 (const char *, ...) __attribute__((format)); /* { dg-error "wrong number of arguments" "bad format" } */ |
| 17 |
extern void fb1 (const char *, ...) __attribute__((format())); /* { dg-error "wrong number of arguments" "bad format" } */ |
| 18 |
extern void fb2 (const char *, ...) __attribute__((format(gnu_attr_printf))); /* { dg-error "wrong number of arguments" "bad format" } */ |
| 19 |
extern void fb3 (const char *, ...) __attribute__((format(gnu_attr_printf, 1))); /* { dg-error "wrong number of arguments" "bad format" } */ |
| 20 |
extern void fb4 (const char *, ...) __attribute__((format(gnu_attr_printf, 1, 2, 3))); /* { dg-error "wrong number of arguments" "bad format" } */ |
| 21 |
|
| 22 |
extern void fc1 (const char *) __attribute__((format_arg)); /* { dg-error "wrong number of arguments" "bad format_arg" } */ |
| 23 |
extern void fc2 (const char *) __attribute__((format_arg())); /* { dg-error "wrong number of arguments" "bad format_arg" } */ |
| 24 |
extern void fc3 (const char *) __attribute__((format_arg(1, 2))); /* { dg-error "wrong number of arguments" "bad format_arg" } */ |
| 25 |
|
| 26 |
/* These attributes presently only apply to declarations, not to types. |
| 27 |
Eventually, they should be usable with declarators for function types |
| 28 |
anywhere, but still not with structure/union/enum types. */ |
| 29 |
struct s0 { int i; } __attribute__((format(gnu_attr_printf, 1, 2))); /* { dg-error "does not apply|only applies" "format on struct" } */ |
| 30 |
union u0 { int i; } __attribute__((format(gnu_attr_printf, 1, 2))); /* { dg-error "does not apply|only applies" "format on union" } */ |
| 31 |
enum e0 { E0V0 } __attribute__((format(gnu_attr_printf, 1, 2))); /* { dg-error "does not apply|only applies" "format on enum" } */ |
| 32 |
|
| 33 |
struct s1 { int i; } __attribute__((format_arg(1))); /* { dg-error "does not apply|only applies" "format_arg on struct" } */ |
| 34 |
union u1 { int i; } __attribute__((format_arg(1))); /* { dg-error "does not apply|only applies" "format_arg on union" } */ |
| 35 |
enum e1 { E1V0 } __attribute__((format_arg(1))); /* { dg-error "does not apply|only applies" "format_arg on enum" } */ |
| 36 |
|
| 37 |
/* The format type must be an identifier, one of those recognized. */ |
| 38 |
extern void fe0 (const char *, ...) __attribute__((format(12345, 1, 2))); /* { dg-error "format specifier" "non-id format" } */ |
| 39 |
extern void fe1 (const char *, ...) __attribute__((format(nosuch, 1, 2))); /* { dg-warning "format function type" "unknown format" } */ |
| 40 |
|
| 41 |
/* Both the numbers must be integer constant expressions. */ |
| 42 |
extern void ff0 (const char *, ...) __attribute__((format(gnu_attr_printf, 3-2, (long long)(10/5)))); |
| 43 |
int foo; |
| 44 |
extern void ff1 (const char *, ...) __attribute__((format(gnu_attr_printf, foo, 10/5))); /* { dg-warning ".format. attribute argument 2 value .foo. is not an integer constant" "bad number" } */ |
| 45 |
extern void ff2 (const char *, ...) __attribute__((format(gnu_attr_printf, 3-2, foo))); /* { dg-warning ".format. attribute argument 3 value .foo. is not an integer constant" "bad number" } */ |
| 46 |
extern char *ff3 (const char *) __attribute__((format_arg(3-2))); |
| 47 |
extern char *ff4 (const char *) __attribute__((format_arg(foo))); /* { dg-warning ".format_arg. attribute argument value .foo. is not an integer constant" "bad format_arg number" } */ |
| 48 |
|
| 49 |
/* The format string argument must precede the arguments to be formatted. |
| 50 |
This includes if no parameter types are specified (which is not valid ISO |
| 51 |
C for variadic functions). */ |
| 52 |
extern void fg0 () __attribute__((format(gnu_attr_printf, 1, 2))); |
| 53 |
extern void fg1 () __attribute__((format(gnu_attr_printf, 1, 0))); |
| 54 |
extern void fg2 () __attribute__((format(gnu_attr_printf, 1, 1))); /* { dg-error "follows" "bad number order" } */ |
| 55 |
extern void fg3 () __attribute__((format(gnu_attr_printf, 2, 1))); /* { dg-error "follows" "bad number order" } */ |
| 56 |
|
| 57 |
/* The format string argument must be a string type, and the arguments to |
| 58 |
be formatted must be the "...". */ |
| 59 |
extern void fh0 (int, ...) __attribute__((format(gnu_attr_printf, 1, 2))); /* { dg-error ".format. attribute argument 2 value .1. refers to parameter type .int." "format int string" } */ |
| 60 |
extern void fh1 (signed char *, ...) __attribute__((format(gnu_attr_printf, 1, 2))); /* { dg-error ".format. attribute argument 2 value .1. refers to parameter type .signed char \\\*." "signed char string" } */ |
| 61 |
extern void fh2 (unsigned char *, ...) __attribute__((format(gnu_attr_printf, 1, 2))); /* { dg-error ".format. attribute argument 2 value .1. refers to parameter type .unsigned char \\\*." "unsigned char string" } */ |
| 62 |
extern void fh3 (const char *, ...) __attribute__((format(gnu_attr_printf, 1, 3))); /* { dg-error "is not" "not ..." } */ |
| 63 |
extern void fh4 (const char *, int, ...) __attribute__((format(gnu_attr_printf, 1, 2))); /* { dg-error ".format. attribute argument 3 value .2. does not refer to a variable argument list" "not ..." } */ |
| 64 |
|
| 65 |
/* format_arg formats must take and return a string. */ |
| 66 |
extern char *fi0 (int) __attribute__((format_arg(1))); /* { dg-error ".format_arg. attribute argument value .1. refers to parameter type .int." } */ |
| 67 |
extern char *fi1 (signed char *) __attribute__((format_arg(1))); /* { dg-error ".format_arg. attribute argument value .1. refers to parameter type .signed char \\\*." "format_arg signed char string" } */ |
| 68 |
extern char *fi2 (unsigned char *) __attribute__((format_arg(1))); /* { dg-error ".format_arg. attribute argument value .1. refers to parameter type .unsigned char \\\*." "format_arg unsigned char string" } */ |
| 69 |
extern int fi3 (const char *) __attribute__((format_arg(1))); /* { dg-error "not return string" "format_arg ret int string" } */ |
| 70 |
extern signed char *fi4 (const char *) __attribute__((format_arg(1))); /* { dg-error "not return string" "format_arg ret signed char string" } */ |
| 71 |
extern unsigned char *fi5 (const char *) __attribute__((format_arg(1))); /* { dg-error "not return string" "format_arg ret unsigned char string" } */ |