summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/cppcoreguidelines-narrowing-conversions-unsigned-char.cpp
blob: 6bd437f98d44c59e49e817512d2f3f371d804988 (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
// RUN: %check_clang_tidy %s cppcoreguidelines-narrowing-conversions %t \
// RUN: -- -- -target x86_64-unknown-linux -funsigned-char

void narrow_integer_to_unsigned_integer_is_ok() {
  signed char sc;
  short s;
  int i;
  long l;
  long long ll;

  char c;
  unsigned short us;
  unsigned int ui;
  unsigned long ul;
  unsigned long long ull;

  ui = sc;
  c = s;
  c = i;
  c = l;
  c = ll;

  c = c;
  c = us;
  c = ui;
  c = ul;
  c = ull;
}

void narrow_integer_to_signed_integer_is_not_ok() {
  signed char sc;
  short s;
  int i;
  long l;
  long long ll;

  char c;
  unsigned short us;
  unsigned int ui;
  unsigned long ul;
  unsigned long long ull;

  sc = sc;
  sc = s;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'short' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
  sc = i;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'int' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
  sc = l;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'long' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
  sc = ll;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'long long' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]

  sc = c;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'char' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
  sc = us;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'unsigned short' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
  sc = ui;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'unsigned int' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
  sc = ul;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'unsigned long' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
  sc = ull;
  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'unsigned long long' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
}

void narrow_constant_to_unsigned_integer_is_ok() {
  char c1 = -128; // unsigned dst type is well defined.
  char c2 = 127;  // unsigned dst type is well defined.
  char c3 = -129; // unsigned dst type is well defined.
  char c4 = 128;  // unsigned dst type is well defined.
  unsigned char uc1 = 0;
  unsigned char uc2 = 255;
  unsigned char uc3 = -1;  // unsigned dst type is well defined.
  unsigned char uc4 = 256; // unsigned dst type is well defined.
  signed char sc = 128;
  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: narrowing conversion from constant value 128 (0x00000080) of type 'int' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
}

void narrow_conditional_operator_contant_to_unsigned_is_ok(bool b) {
  // conversion to unsigned char type is well defined.
  char c1 = b ? 1 : 0;
  char c2 = b ? 1 : 256;
  char c3 = b ? -1 : 0;
}
OpenPOWER on IntegriCloud