2008-01-25 Douglas Gregor <doug.gregor@gmail.com>
[official-gcc.git] / gcc / testsuite / gcc.dg / Wsign-conversion.c
blob45edd3b436145d0aad6b17b905537ca2808655bd
1 /* Test for diagnostics for implicit conversions between signed and
2 unsigned integer types.
3 These tests come from gcc/testsuite/gcc.dg/overflow-warn-2.c */
5 /* { dg-do compile } */
6 /* { dg-options "-std=c99 -fsigned-char -Wsign-conversion" } */
7 #include <limits.h>
9 void fsc (signed char sc);
10 void fuc (unsigned char uc);
11 unsigned fui (unsigned int ui);
12 void fsi (signed int ui);
14 void h (int x)
16 unsigned int ui = 3;
17 int si = 3;
18 unsigned char uc = 3;
19 signed char sc = 3;
21 uc = ui;
22 uc = si;
23 sc = ui;
24 sc = si;
25 fuc (ui);
26 fuc (si);
27 fsc (ui);
28 fsc (si);
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';
43 uc = x ? 1U : -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
44 uc = x ? SCHAR_MIN : 1U; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
45 uc = x ? 1 : -1;
46 uc = x ? SCHAR_MIN : 1;
47 ui = x ? 1U : -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
48 ui = x ? INT_MIN : 1U; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
49 ui = ui ? SCHAR_MIN : 1U; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
50 ui = 1U * -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
51 ui = ui + INT_MIN; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
52 ui = x ? 1 : -1; /* { dg-warning "conversion" } */
53 ui = ui ? SCHAR_MIN : 1; /* { dg-warning "conversion" } */
55 fuc (-1); /* { dg-warning "negative integer implicitly converted to unsigned type" } */
56 uc = -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
57 fui (-1); /* { dg-warning "negative integer implicitly converted to unsigned type" } */
58 ui = -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
59 fuc ('\xa0'); /* { dg-warning "negative integer implicitly converted to unsigned type" } */
60 uc = '\xa0'; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
61 fui ('\xa0');/* { dg-warning "negative integer implicitly converted to unsigned type" } */
62 ui = '\xa0'; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
63 fsi (0x80000000); /* { dg-warning "conversion" } */
64 si = 0x80000000; /* { dg-warning "conversion" } */
67 fsi (UINT_MAX - 1); /* { dg-warning "conversion" } */
68 si = UINT_MAX - 1; /* { dg-warning "conversion" } */
69 fsi (UINT_MAX - 1U); /* { dg-warning "conversion" } */
70 si = UINT_MAX - 1U; /* { dg-warning "conversion" } */
71 fsi (UINT_MAX/3U);
72 si = UINT_MAX/3U;
73 fsi (UINT_MAX/3);
74 si = UINT_MAX/3;
75 fui (UINT_MAX - 1);
76 ui = UINT_MAX - 1;
78 uc = (unsigned char) -1;
79 ui = -1 * (1 * -1);
80 ui = (unsigned) -1;
82 fsc (uc); /* { dg-warning "conversion" } */
83 sc = uc; /* { dg-warning "conversion" } */
84 fuc (sc); /* { dg-warning "conversion" } */
85 uc = sc; /* { dg-warning "conversion" } */
86 fsi (ui); /* { dg-warning "conversion" } */
87 si = ui; /* { dg-warning "conversion" } */
88 fui (si); /* { dg-warning "conversion" } */
89 ui = si; /* { dg-warning "conversion" } */
90 fui (sc); /* { dg-warning "conversion" } */
91 ui = sc; /* { dg-warning "conversion" } */
94 unsigned fui (unsigned a) { return a + -1; } /* { dg-warning "negative integer implicitly converted to unsigned type" } */