summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/warn/Wsign-conversion.C
blob: 83fe2ed660b41c4a0a2fa09fa3da79508c3987a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* Test for diagnostics for implicit conversions between signed and
   unsigned integer types.
   C++ equivalent of gcc/testsuite/gcc.dg/Wsign-conversion.c  */

// { dg-do compile } 
// { dg-options "-fsigned-char -Wsign-conversion" } 
#include <limits.h>

void fsc (signed char sc);
void fuc (unsigned char uc);
unsigned fui (unsigned int  ui);
void fsi (signed int ui);

void h (int x)
{
  unsigned int ui = 3;
  int   si = 3;
  unsigned char uc = 3;
  signed char   sc = 3;

  uc = ui; 
  uc = si; 
  sc = ui; 
  sc = si; 
  fuc (ui);
  fuc (si);
  fsc (ui);
  fsc (si);

  fsi (si);
  fui (ui);
  fsi (uc);
  si = uc;
  fui (uc);
  ui = uc;
  fui ('A');
  ui = 'A';
  fsi ('A');
  si = 'A';
  fuc ('A');
  uc = 'A';

  uc = x ? 1U : -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  uc = x ? SCHAR_MIN : 1U;  /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  uc = x ? 1 : -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  uc = x ? SCHAR_MIN : 1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  ui = x ? 1U : -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  ui = x ? INT_MIN : 1U; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  ui = ui ? SCHAR_MIN : 1U; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  ui = 1U * -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  ui = ui + INT_MIN; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  ui = x ? 1 : -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  ui = ui ? SCHAR_MIN : 1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */

  fuc (-1); /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  uc = -1;  /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  fui (-1); /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  ui = -1; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  fuc ('\xa0'); /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  uc = '\xa0'; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  fui ('\xa0');/* { dg-warning "negative integer implicitly converted to unsigned type" } */
  ui = '\xa0'; /* { dg-warning "negative integer implicitly converted to unsigned type" } */
  fsi (0x80000000); /* { dg-warning "conversion" } */
  si = 0x80000000;  /* { dg-warning "conversion" } */


  fsi (UINT_MAX - 1);  /* { dg-warning "conversion" } */
  si = UINT_MAX - 1;   /* { dg-warning "conversion" } */
  fsi (UINT_MAX - 1U); /* { dg-warning "conversion" } */
  si = UINT_MAX - 1U;  /* { dg-warning "conversion" } */
  fsi (UINT_MAX/3U);
  si = UINT_MAX/3U;
  fsi (UINT_MAX/3);
  si = UINT_MAX/3;
  fui (UINT_MAX - 1);
  ui = UINT_MAX - 1;

  uc = (unsigned char) -1;
  ui = -1 * (1 * -1);
  ui = (unsigned) -1;

  fsc (uc); /* { dg-warning "conversion" } */
  sc = uc;  /* { dg-warning "conversion" } */
  fuc (sc); /* { dg-warning "conversion" } */
  uc = sc;  /* { dg-warning "conversion" } */
  fsi (ui); /* { dg-warning "conversion" } */
  si = ui;  /* { dg-warning "conversion" } */
  fui (si); /* { dg-warning "conversion" } */ 
  ui = si;  /* { dg-warning "conversion" } */ 
  fui (sc); /* { dg-warning "conversion" } */
  ui = sc;  /* { dg-warning "conversion" } */
}

unsigned fui (unsigned a) { return a + -1; } /* { dg-warning "negative integer implicitly converted to unsigned type" } */