1 /* Test for diagnostics for implicit conversions between integer types
2    These tests come from gcc/testsuite/gcc.dg/overflow-warn-2.c  */
3 
4 /* { dg-do compile } */
5 /* { dg-options "-std=c99 -fsigned-char -Wconversion" } */
6 
7 #include <limits.h>
8 
9 void fsc (signed char sc);
10 void fuc (unsigned char uc);
11 unsigned fui (unsigned int  ui);
12 void fsi (signed int ui);
13 
h(int x)14 void h (int x)
15 {
16   unsigned int ui = 3;
17   int   si = 3;
18   unsigned char uc = 3;
19   signed char   sc = 3;
20 
21   uc = ui; /* { dg-warning "conversion" } */
22   uc = si; /* { dg-warning "conversion" } */
23   sc = ui; /* { dg-warning "conversion" } */
24   sc = si; /* { dg-warning "conversion" } */
25   fuc (ui); /* { dg-warning "conversion" } */
26   fuc (si); /* { dg-warning "conversion" } */
27   fsc (ui); /* { dg-warning "conversion" } */
28   fsc (si); /* { dg-warning "conversion" } */
29 
30   fsi (si);
31   fui (ui);
32   fsi (uc);
33   si = uc;
34   fui (uc);
35   ui = uc;
36   fui ('A');
37   ui = 'A';
38   fsi ('A');
39   si = 'A';
40   fuc ('A');
41   uc = 'A';
42 
43   uc = x ? 1U : -1; /* { dg-warning "conversion from .unsigned int. to .unsigned char." } */
44   /* { dg-warning "unsigned conversion" "implicit" { target *-*-* } .-1 } */
45   uc = x ? SCHAR_MIN : 1U; /* { dg-warning "unsigned conversion" } */
46   /* { dg-warning "conversion from .unsigned int. to .unsigned char." "implicit" { target *-*-* } .-1 } */
47   uc = x ? 1 : -1; /* { dg-warning "signed conversion" } */
48   uc = x ? SCHAR_MIN : 1; /* { dg-warning "signed conversion" } */
49   ui = x ? 1U : -1; /* { dg-warning "signed conversion" } */
50   ui = x ? INT_MIN : 1U; /* { dg-warning "signed conversion" } */
51   ui = ui ? SCHAR_MIN : 1U; /* { dg-warning "signed conversion" } */
52   ui = 1U * -1; /* { dg-warning "signed conversion" } */
53   ui = ui + INT_MIN; /* { dg-warning "signed conversion" } */
54   ui = x ? 1 : -1; /* { dg-warning "signed conversion" } */
55   ui = ui ? SCHAR_MIN : 1; /* { dg-warning "signed conversion" } */
56 
57   fuc (-1); /* { dg-warning "signed conversion" } */
58   uc = -1;  /* { dg-warning "signed conversion" } */
59   fui (-1); /* { dg-warning "signed conversion" } */
60   ui = -1; /* { dg-warning "signed conversion" } */
61   fuc ('\xa0'); /* { dg-warning "signed conversion" } */
62   uc = '\xa0'; /* { dg-warning "signed conversion" } */
63   fui ('\xa0');/* { dg-warning "signed conversion" } */
64   ui = '\xa0'; /* { dg-warning "signed conversion" } */
65   fsi (0x80000000); /* { dg-warning "conversion" } */
66   si = 0x80000000;  /* { dg-warning "conversion" } */
67 
68 
69   fsi (UINT_MAX - 1);  /* { dg-warning "conversion" } */
70   si = UINT_MAX - 1;   /* { dg-warning "conversion" } */
71   fsi (UINT_MAX - 1U); /* { dg-warning "conversion" } */
72   si = UINT_MAX - 1U;  /* { dg-warning "conversion" } */
73   fsi (UINT_MAX/3U);
74   si = UINT_MAX/3U;
75   fsi (UINT_MAX/3);
76   si = UINT_MAX/3;
77   fui (UINT_MAX - 1);
78   ui = UINT_MAX - 1;
79 
80   uc = (unsigned char) -1;
81   ui = -1 * (1 * -1);
82   ui = (unsigned) -1;
83 
84   fsc (uc); /* { dg-warning "conversion" } */
85   sc = uc;  /* { dg-warning "conversion" } */
86   fuc (sc); /* { dg-warning "conversion" } */
87   uc = sc;  /* { dg-warning "conversion" } */
88   fsi (ui); /* { dg-warning "conversion" } */
89   si = ui;  /* { dg-warning "conversion" } */
90   fui (si); /* { dg-warning "conversion" } */
91   ui = si;  /* { dg-warning "conversion" } */
92   fui (sc); /* { dg-warning "conversion" } */
93   ui = sc;  /* { dg-warning "conversion" } */
94 }
95 
fui(unsigned a)96 unsigned fui (unsigned a) { return a + -1; } /* { dg-warning "signed conversion" } */
97 
98 
99