1 /* PR tree-optimization/80397 - missing -Wformat-overflow with arguments
2    of enum types
3    { dg-do compile }
4    { dg-options "-O2 -Wall -Wformat-overflow=1 -ftrack-macro-expansion=0" }
5    { dg-require-effective-target int32plus } */
6 
7 void sink (char*);
8 
integer_range(long long min,long long max)9 static long long integer_range (long long min, long long max)
10 {
11   extern long long integer_value (void);
12   long long n = integer_value ();
13   return n < min || max < n ? min : n;
14 }
15 
16 typedef enum { i0, imax = __INT_MAX__ } Int;
17 typedef enum { ll0, llmax = __LONG_LONG_MAX__ } LLong;
18 
19 #define R(T, min, max) (T)integer_range (min, max)
20 
21 char buffer[1];
22 #define T(fmt, ...)						\
23   __builtin_sprintf (buffer + 1, fmt, __VA_ARGS__), sink (buffer)
24 
test_bool(_Bool b)25 void test_bool (_Bool b)
26 {
27   T ("%hhi", b);   // { dg-warning "writing 1 byte" }
28   T ( "%hi", b);   // { dg-warning "writing 1 byte" }
29   T (  "%i", b);   // { dg-warning "writing 1 byte" }
30 }
31 
test_enum(void)32 void test_enum (void)
33 {
34   T ("%hhi", R (Int,    1,     1));   // { dg-warning "writing 1 byte" }
35   T ("%hhi", R (Int,    1,    22));   // { dg-warning "between 1 and 2 bytes" }
36 
37   T ( "%hi", R (Int,    1,     2));   // { dg-warning "writing 1 " }
38   T ( "%hi", R (Int,    1,    22));   // { dg-warning "between 1 and 2 " }
39   T ( "%hi", R (Int,   22,   333));   // { dg-warning "between 2 and 3 " }
40   T ( "%hi", R (Int,  333,  4444));   // { dg-warning "between 3 and 4 " }
41 
42   T (  "%i", R (Int,    1,     1));   // { dg-warning "writing 1 " }
43   T (  "%i", R (Int,    1,    22));   // { dg-warning "between 1 and 2 " }
44   T (  "%i", R (Int,   22,   333));   // { dg-warning "between 2 and 3 " }
45   T (  "%i", R (Int,  333,  4444));   // { dg-warning "between 3 and 4 " }
46   T (  "%i", R (Int, 4444, 55555));   // { dg-warning "between 4 and 5 " }
47 
48 #if __LONG_MAX__ == __LONG_LONG_MAX__
49 #  define LLI "%li"
50 #else
51 #  define LLI "%lli"
52 #endif
53 
54   T (LLI, R (LLong,    1,     1));       // { dg-warning "writing 1 " }
55   T (LLI, R (LLong,    1,    22));       // { dg-warning "between 1 and 2 " }
56   T (LLI, R (LLong,   22,   333));       // { dg-warning "between 2 and 3 " }
57   T (LLI, R (LLong,  333,  4444));       // { dg-warning "between 3 and 4 " }
58   T (LLI, R (LLong, 4444, 55555));       // { dg-warning "between 4 and 5 " }
59 
60   T (LLI, R (LLong, 4444, 1234567890));  // { dg-warning "between 4 and 10 " }
61   T (LLI, R (LLong, 4444, 12345678901)); // { dg-warning "between 4 and 11 " }
62 }
63