11#include < cstdint>
22
3- void f1 () {
4- unsigned char a1 = ' c' ; // NON_COMPLIANT
5- unsigned char a2 = 10 ;
6- signed char a3 = ' c' ; // NON_COMPLIANT
7- signed char a4 = 10 ;
3+ template <typename T, T y> class C1 {
4+ public:
5+ C1 () : x(y) {}
86
9- std::int8_t a5 = ' c' ; // NON_COMPLIANT
10- std::int8_t a6 = 10 ;
7+ private:
8+ unsigned char x;
9+ };
1110
12- std::uint8_t a7 = ' c' ; // NON_COMPLIANT
13- std::uint8_t a8 = 10 ;
11+ template <typename T, T y> class C2 {
12+ public:
13+ C2 () : x(y) {}
1414
15- char a9 = ' c' ;
15+ private:
16+ signed char x;
17+ };
18+
19+ template <typename T, T y> class C3 {
20+ public:
21+ C3 () : x(y) {}
22+
23+ private:
24+ unsigned char x;
25+ };
26+
27+ template <typename T, T y> class C4 {
28+ public:
29+ C4 () : x(y) {}
30+
31+ private:
32+ signed char x;
33+ };
34+
35+ /* Twin templates for std::uint8_t and std::int8_t */
36+ template <typename T, T y> class C9 {
37+ public:
38+ C9 () : x(y) {}
39+
40+ private:
41+ std::uint8_t x;
42+ };
43+
44+ template <typename T, T y> class C10 {
45+ public:
46+ C10 () : x(y) {}
47+
48+ private:
49+ std::int8_t x;
50+ };
51+
52+ template <typename T, T y> class C11 {
53+ public:
54+ C11 () : x(y) {}
55+
56+ private:
57+ std::uint8_t x;
58+ };
59+
60+ template <typename T, T y> class C12 {
61+ public:
62+ C12 () : x(y) {}
63+
64+ private:
65+ std::int8_t x;
66+ };
67+
68+ void f1 (unsigned char x) {}
69+ void f2 (signed char x) {}
70+ void f3 (unsigned char x) {}
71+ void f4 (signed char x) {}
72+
73+ /* Twin functions for std::uint8_t and std::int8_t */
74+ void f9 (std::uint8_t x) {}
75+ void f10 (std::int8_t x) {}
76+ void f11 (std::uint8_t x) {}
77+ void f12 (std::int8_t x) {}
78+
79+ template <typename T> void f5 (T x) { unsigned char y = x; }
80+ template <typename T> void f6 (T x) { signed char y = x; }
81+ template <typename T> void f7 (T x) { signed char y = x; }
82+ template <typename T> void f8 (T x) { signed char y = x; }
83+
84+ /* Twin template functions for std::uint8_t and std::int8_t */
85+ template <typename T> void f13 (T x) { std::uint8_t y = x; }
86+ template <typename T> void f14 (T x) { std::int8_t y = x; }
87+ template <typename T> void f15 (T x) { std::int8_t y = x; }
88+ template <typename T> void f16 (T x) { std::int8_t y = x; }
89+
90+ template <typename T> class C5 {
91+ public:
92+ C5 (T y) : x(y) {}
93+
94+ private:
95+ unsigned char x;
96+ };
97+
98+ template <typename T> class C6 {
99+ public:
100+ C6 (T y) : x(y) {}
101+
102+ private:
103+ signed char x;
104+ };
105+
106+ template <typename T> class C7 {
107+ public:
108+ C7 (T y) : x(y) {}
109+
110+ private:
111+ signed char x;
112+ };
113+
114+ template <typename T> class C8 {
115+ public:
116+ C8 (T y) : x(y) {}
117+
118+ private:
119+ signed char x;
120+ };
121+
122+ /* Twin template classes for std::uint8_t and std::int8_t */
123+ template <typename T> class C13 {
124+ public:
125+ C13 (T y) : x(y) {}
126+
127+ private:
128+ std::uint8_t x;
129+ };
130+
131+ template <typename T> class C14 {
132+ public:
133+ C14 (T y) : x(y) {}
134+
135+ private:
136+ std::int8_t x;
137+ };
138+
139+ template <typename T> class C15 {
140+ public:
141+ C15 (T y) : x(y) {}
142+
143+ private:
144+ std::int8_t x;
145+ };
146+
147+ template <typename T> class C16 {
148+ public:
149+ C16 (T y) : x(y) {}
150+
151+ private:
152+ std::int8_t x;
153+ };
154+
155+ int main () {
156+
157+ /* ========== 1. Assigning a char to another char ========== */
158+
159+ /* ===== 1-1. Assigning a char to a char variable ===== */
160+
161+ unsigned char x1 = 1 ;
162+ unsigned char y1 =
163+ x1; // COMPLIANT: unsigned char assigned to an unsigned char
164+
165+ signed char x2 = 1 ;
166+ signed char y2 = x2; // COMPLIANT: signed char assigned to a signed char
167+
168+ char x3 = ' x' ;
169+ unsigned char y3 = x3; // NON-COMPLIANT: plain char assigned to a unsigned char
170+
171+ char x4 = ' x' ;
172+ signed char y4 = x4; // NON-COMPLIANT: plain char assigned to a signed char
173+
174+ /* Twin cases with std::uint8_t and std::int8_t */
175+ std::uint8_t x5 = 1 ;
176+ std::uint8_t y5 =
177+ x5; // COMPLIANT: std::uint8_t assigned to a std::uint8_t
178+
179+ std::int8_t x6 = 1 ;
180+ std::int8_t y6 = x6; // COMPLIANT: std::int8_t assigned to a std::int8_t
181+
182+ char x7 = ' x' ;
183+ std::uint8_t y7 = x7; // NON-COMPLIANT: plain char assigned to a std::uint8_t
184+
185+ char x8 = ' x' ;
186+ std::int8_t y8 = x8; // NON-COMPLIANT: plain char assigned to a std::int8_t
187+
188+ /* ===== 1-2. Assigning a char to a char member ===== */
189+
190+ C1<unsigned char , 1 > c1; // COMPLIANT: unsigned char arg passed to an unsigned
191+ // char member through a template
192+
193+ C2<signed char , 1 > c2; // COMPLIANT: signed char arg passed to a signed char
194+ // member through a template
195+
196+ C3<char , ' x' > c3; // NON-COMPLIANT: plain char arg passed to a unsigned char
197+ // member through a template
198+
199+ C4<char , ' x' > c4; // NON-COMPLIANT: plain char arg passed to a signed char
200+ // member through a template
201+
202+ /* Twin cases with std::uint8_t and std::int8_t */
203+ C9<std::uint8_t , 1 > c9; // COMPLIANT: std::uint8_t arg passed to a std::uint8_t
204+ // member through a template
205+
206+ C10<std::int8_t , 1 > c10; // COMPLIANT: std::int8_t arg passed to a std::int8_t
207+ // member through a template
208+
209+ C11<char , ' x' > c11; // NON-COMPLIANT: plain char arg passed to a std::uint8_t
210+ // member through a template
211+
212+ C12<char , ' x' > c12; // NON-COMPLIANT: plain char arg passed to a std::int8_t
213+ // member through a template
214+
215+ /* ========== 1-3. Assigning a char to a char through a pointer ========== */
216+
217+ unsigned char x9 = 1 ;
218+ unsigned char *y9 = &x9;
219+ signed char z1 =
220+ *y9; // COMPLIANT: unsigned char assigned to a *&unsigned char
221+
222+ unsigned char x10 = 1 ;
223+ unsigned char *y10 = &x10;
224+ signed char z2 = *y10; // COMPLIANT: signed char assigned to an *&signed char
225+
226+ char x11 = 1 ;
227+ char *y11 = &x11;
228+ unsigned char z3 =
229+ *y11; // NON-COMPLIANT: plain char assigned to an *&unsigned char
230+
231+ char x12 = 1 ;
232+ char *y12 = &x12;
233+ signed char z4 =
234+ *y12; // NON-COMPLIANT: plain char assigned to an *&signed char
235+
236+ /* Twin cases with std::uint8_t and std::int8_t */
237+ std::uint8_t x13 = 1 ;
238+ std::uint8_t *y13 = &x13;
239+ std::int8_t z5 =
240+ *y13; // COMPLIANT: std::uint8_t assigned to a *&std::uint8_t
241+
242+ std::uint8_t x14 = 1 ;
243+ std::uint8_t *y14 = &x14;
244+ std::int8_t z6 = *y14; // COMPLIANT: std::int8_t assigned to an *&std::int8_t
245+
246+ char x15 = 1 ;
247+ char *y15 = &x15;
248+ std::uint8_t z7 =
249+ *y15; // NON-COMPLIANT: plain char assigned to an *&std::uint8_t
250+
251+ char x16 = 1 ;
252+ char *y16 = &x16;
253+ std::int8_t z8 =
254+ *y16; // NON-COMPLIANT: plain char assigned to an *&std::int8_t
255+
256+ /* ========== 2. Passing a char argument to a char parameter ========== */
257+
258+ /* ===== 2-1. Passing char argument to a char parameter of a regular function
259+ * ===== */
260+
261+ unsigned char a1 = 1 ;
262+ f1 (a1); // COMPLIANT: unsigned char arg passed to an unsigned char parameter
263+
264+ signed char a2 = 1 ;
265+ f2 (a2); // COMPLIANT: signed char arg passed to a signed char parameter
266+
267+ char a3 = ' a' ;
268+ f3 (a3); // NON-COMPLIANT: plain char arg passed to a unsigned char parameter
269+
270+ char a4 = ' a' ;
271+ f4 (a4); // NON-COMPLIANT: plain char arg passed to a signed char parameter
272+
273+ /* Twin cases with std::uint8_t and std::int8_t */
274+ std::uint8_t a5 = 1 ;
275+ f9 (a5); // COMPLIANT: std::uint8_t arg passed to a std::uint8_t parameter
276+
277+ std::int8_t a6 = 1 ;
278+ f10 (a6); // COMPLIANT: std::int8_t arg passed to a std::int8_t parameter
279+
280+ char a7 = ' a' ;
281+ f11 (a7); // NON-COMPLIANT: plain char arg passed to a std::uint8_t parameter
282+
283+ char a8 = ' a' ;
284+ f12 (a8); // NON-COMPLIANT: plain char arg passed to a std::int8_t parameter
285+
286+ /* ===== 2-2. Passing char argument to a char parameter through a template
287+ * ===== */
288+
289+ unsigned char a9 = ' a' ;
290+ f5 (a9); // COMPLIANT: unsigned char arg passed to an unsigned char parameter
291+ // through a template
292+
293+ signed char a10 = ' a' ;
294+ f6 (a10); // COMPLIANT: signed char arg passed to a signed char parameter
295+ // through a template
296+
297+ char a11 = ' a' ;
298+ f7 (a11); // NON-COMPLIANT: plain char arg passed to a unsigned char parameter
299+ // through a template
300+
301+ char a12 = ' a' ;
302+ f8 (a12); // COMPLIANT: plain char arg passed to a signed char parameter through
303+ // a template
304+
305+ /* Twin cases with std::uint8_t and std::int8_t */
306+ std::uint8_t a13 = ' a' ;
307+ f13 (a13); // COMPLIANT: std::uint8_t arg passed to a std::uint8_t parameter
308+ // through a template
309+
310+ std::int8_t a14 = ' a' ;
311+ f14 (a14); // COMPLIANT: std::int8_t arg passed to a std::int8_t parameter
312+ // through a template
313+
314+ char a15 = ' a' ;
315+ f15 (a15); // NON-COMPLIANT: plain char arg passed to a std::uint8_t parameter
316+ // through a template
317+
318+ char a16 = ' a' ;
319+ f16 (a16); // COMPLIANT: plain char arg passed to a std::int8_t parameter through
320+ // a template
321+
322+ /* ========== 2-3. Passing a char argument to a char parameter through a
323+ * template ========== */
324+
325+ unsigned char a17 = 1 ;
326+ C5<unsigned char > c5 (
327+ a17); // COMPLIANT: unsigned char arg passed to an unsigned char parameter
328+ // of a constructor through a template
329+
330+ signed char a18 = 1 ;
331+ C6<signed char > c6 (a18); // COMPLIANT: signed char arg passed to an signed
332+ // char parameter of a constructor through a template
333+
334+ char a19 = ' a' ;
335+ C7<char > c7 (a19); // NON-COMPLIANT: plain char arg passed to an unsigned char
336+ // parameter of a constructor through a template
337+
338+ char a20 = ' a' ;
339+ C8<char > c8 (a20); // NON-COMPLIANT: plain char arg passed to an signed char
340+ // parameter of a constructor through a template
341+
342+ /* Twin cases with std::uint8_t and std::int8_t */
343+ std::uint8_t a21 = 1 ;
344+ C13<std::uint8_t > c13 (
345+ a21); // COMPLIANT: std::uint8_t arg passed to a std::uint8_t parameter
346+ // of a constructor through a template
347+
348+ std::int8_t a22 = 1 ;
349+ C14<std::int8_t > c14 (a22); // COMPLIANT: std::int8_t arg passed to a std::int8_t
350+ // parameter of a constructor through a template
351+
352+ char a23 = ' a' ;
353+ C15<char > c15 (a23); // NON-COMPLIANT: plain char arg passed to a std::uint8_t
354+ // parameter of a constructor through a template
355+
356+ char a24 = ' a' ;
357+ C16<char > c16 (a24); // NON-COMPLIANT: plain char arg passed to a std::int8_t
358+ // parameter of a constructor through a template
16359}
0 commit comments