OSDN Git Service

* gcc.dg/testsuite/c++98.c,c++98-pedantic.c,c89.c,c89-pedantic.c,
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.dg / format-branch-1.c
1 /* Test for format checking of conditional expressions.  */
2 /* Origin: Joseph Myers <jsm28@cam.ac.uk> */
3 /* { dg-do compile } */
4 /* { dg-options "-std=gnu99 -Wformat" } */
5
6 #define NULL ((void *)0)
7
8 extern int printf (const char *, ...);
9
10 void
11 foo (long l, int nfoo)
12 {
13   printf ((nfoo > 1) ? "%d foos" : "%d foo", nfoo);
14   printf ((l > 1) ? "%d foos" : "%d foo", l); /* { dg-warning "int format" "wrong type in conditional expr" } */
15   printf ((l > 1) ? "%ld foos" : "%d foo", l); /* { dg-warning "int format" "wrong type in conditional expr" } */
16   printf ((l > 1) ? "%d foos" : "%ld foo", l); /* { dg-warning "int format" "wrong type in conditional expr" } */
17   /* Should allow one case to have extra arguments.  */
18   printf ((nfoo > 1) ? "%d foos" : "1 foo", nfoo);
19   printf ((nfoo > 1) ? "many foos" : "1 foo", nfoo); /* { dg-warning "too many" "too many args in all branches" } */
20   printf ((nfoo > 1) ? "%d foos" : "", nfoo);
21   printf ((nfoo > 1) ? "%d foos" : ((nfoo > 0) ? "1 foo" : "no foos"), nfoo);
22   printf ((nfoo > 1) ? "%d foos" : ((nfoo > 0) ? "%d foo" : "%d foos"), nfoo);
23   printf ((nfoo > 1) ? "%d foos" : ((nfoo > 0) ? "%d foo" : "%ld foos"), nfoo); /* { dg-warning "long int format" "wrong type" } */
24   printf ((nfoo > 1) ? "%ld foos" : ((nfoo > 0) ? "%d foo" : "%d foos"), nfoo); /* { dg-warning "long int format" "wrong type" } */
25   printf ((nfoo > 1) ? "%d foos" : ((nfoo > 0) ? "%ld foo" : "%d foos"), nfoo); /* { dg-warning "long int format" "wrong type" } */
26   /* Extra arguments to NULL should be complained about.  */
27   printf (NULL, "foo"); /* { dg-warning "too many" "NULL extra args" } */
28   /* { dg-warning "null" "null format arg" { target *-*-* } 27 } */
29 }