1 // RUN: %check_clang_tidy %s cppcoreguidelines-narrowing-conversions %t \
2 // RUN: -- -- -target x86_64-unknown-linux -funsigned-char
3 
narrow_integer_to_unsigned_integer_is_ok()4 void narrow_integer_to_unsigned_integer_is_ok() {
5   signed char sc;
6   short s;
7   int i;
8   long l;
9   long long ll;
10 
11   char c;
12   unsigned short us;
13   unsigned int ui;
14   unsigned long ul;
15   unsigned long long ull;
16 
17   ui = sc;
18   c = s;
19   c = i;
20   c = l;
21   c = ll;
22 
23   c = c;
24   c = us;
25   c = ui;
26   c = ul;
27   c = ull;
28 }
29 
narrow_integer_to_signed_integer_is_not_ok()30 void narrow_integer_to_signed_integer_is_not_ok() {
31   signed char sc;
32   short s;
33   int i;
34   long l;
35   long long ll;
36 
37   char c;
38   unsigned short us;
39   unsigned int ui;
40   unsigned long ul;
41   unsigned long long ull;
42 
43   sc = sc;
44   sc = s;
45   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'short' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
46   sc = i;
47   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'int' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
48   sc = l;
49   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'long' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
50   sc = ll;
51   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'long long' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
52 
53   sc = c;
54   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'char' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
55   sc = us;
56   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'unsigned short' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
57   sc = ui;
58   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'unsigned int' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
59   sc = ul;
60   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'unsigned long' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
61   sc = ull;
62   // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'unsigned long long' to signed type 'signed char' is implementation-defined [cppcoreguidelines-narrowing-conversions]
63 }
64 
narrow_constant_to_unsigned_integer_is_ok()65 void narrow_constant_to_unsigned_integer_is_ok() {
66   char c1 = -128; // unsigned dst type is well defined.
67   char c2 = 127;  // unsigned dst type is well defined.
68   char c3 = -129; // unsigned dst type is well defined.
69   char c4 = 128;  // unsigned dst type is well defined.
70   unsigned char uc1 = 0;
71   unsigned char uc2 = 255;
72   unsigned char uc3 = -1;  // unsigned dst type is well defined.
73   unsigned char uc4 = 256; // unsigned dst type is well defined.
74   signed char sc = 128;
75   // 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]
76 }
77 
narrow_conditional_operator_contant_to_unsigned_is_ok(bool b)78 void narrow_conditional_operator_contant_to_unsigned_is_ok(bool b) {
79   // conversion to unsigned char type is well defined.
80   char c1 = b ? 1 : 0;
81   char c2 = b ? 1 : 256;
82   char c3 = b ? -1 : 0;
83 }
84