@@ -13,15 +13,30 @@ std::int64_t s64;
13
13
float f;
14
14
double d;
15
15
16
+ namespace TestNamespace {
17
+ std::uint8_t g1;
18
+ std::uint16_t g2;
19
+ } // namespace TestNamespace
20
+
21
+ struct TestStruct {
22
+ std::uint8_t m1;
23
+ std::uint16_t m2;
24
+ static std::uint8_t s1;
25
+ static std::uint16_t s2;
26
+ };
27
+
28
+ std::uint8_t TestStruct::s1;
29
+ std::uint16_t TestStruct::s2;
30
+
16
31
// Test basic constant assignments
17
32
void test_constant_assignments () {
18
- u32 = 1 ; // COMPLIANT
19
- s32 = 4u * 2u ; // COMPLIANT
20
- u8 = 3u ; // COMPLIANT
21
- u8 = 300u ; // NON_COMPLIANT
33
+ u32 = 1 ; // COMPLIANT
34
+ s32 = 4u * 2u ; // COMPLIANT
35
+ u8 = 3u ; // COMPLIANT
36
+ u8 = 300u ; // NON_COMPLIANT
22
37
f = 1 ; // COMPLIANT
23
38
f = 9999999999 ; // COMPLIANT
24
- d = 0 .0f ; // NON_COMPLIANT
39
+ d = 0 .0f ; // NON_COMPLIANT
25
40
f = 0 .0f ; // COMPLIANT
26
41
}
27
42
@@ -33,7 +48,7 @@ void test_signedness_violations() {
33
48
34
49
// Test size violations
35
50
void test_size_violations () {
36
- u8 = u16 ; // NON_COMPLIANT
51
+ u8 = u16 ; // NON_COMPLIANT
37
52
u16 = u64 ; // NON_COMPLIANT
38
53
}
39
54
@@ -45,9 +60,49 @@ void test_type_category_violations() {
45
60
46
61
// Test widening of id-expressions
47
62
void test_widening_id_expressions () {
48
- u32 = u8 ; // COMPLIANT
49
- s64 = s8; // COMPLIANT
50
- u64 = u16 ; // COMPLIANT
63
+ u32 = u8 ; // COMPLIANT - widening of id-expression
64
+ s64 = s8; // COMPLIANT - widening of id-expression
65
+ u64 = u16 ; // COMPLIANT - widening of id-expression
66
+ }
67
+
68
+ // Test widening with namespace qualifiers (allowed)
69
+ void test_widening_namespace_qualified () {
70
+ u32 = TestNamespace::g1; // COMPLIANT - namespace qualified id-expression
71
+ u64 = TestNamespace::g2; // COMPLIANT - namespace qualified id-expression
72
+ }
73
+
74
+ // Test widening with type qualifiers (allowed)
75
+ void test_widening_type_qualified () {
76
+ u32 = TestStruct::s1; // COMPLIANT - type qualified id-expression
77
+ u64 = TestStruct::s2; // COMPLIANT - type qualified id-expression
78
+ }
79
+
80
+ // Test widening with decltype (allowed)
81
+ void test_widening_decltype_qualified () {
82
+ std::uint8_t l1 = 42 ;
83
+ std::uint16_t l2 = 42 ;
84
+ u32 = decltype (l1){}; // COMPLIANT - treated as a constant
85
+ u64 = decltype (l2){}; // COMPLIANT - treated as a constant
86
+ TestStruct l3;
87
+ u32 = decltype (l3)::s1; // COMPLIANT - decltype qualified
88
+ u64 = decltype (l3)::s2; // COMPLIANT - decltype qualified
89
+ }
90
+
91
+ // Test widening with object member access (not allowed)
92
+ void test_widening_object_member_access () {
93
+ TestStruct l1;
94
+ TestStruct *l2 = &l1;
95
+ u32 = l1.m1 ; // NON_COMPLIANT - object member access, not id-expression
96
+ u64 = l1.m2 ; // NON_COMPLIANT - object member access, not id-expression
97
+ u32 = l2->m1 ; // NON_COMPLIANT - object member access, not id-expression
98
+ u64 = l2->m2 ; // NON_COMPLIANT - object member access, not id-expression
99
+ }
100
+
101
+ // Test widening with expressions (not allowed)
102
+ void test_widening_expressions () {
103
+ u32 = u8 + 0 ; // NON_COMPLIANT - not id-expression
104
+ u32 = (u8 ); // NON_COMPLIANT - not id-expression (parenthesized)
105
+ u32 = static_cast <std::uint8_t >(u8 ); // NON_COMPLIANT - not id-expression
51
106
}
52
107
53
108
// Test expression results
@@ -66,7 +121,7 @@ void test_bitfields() {
66
121
l1.m1 = 2 ; // COMPLIANT
67
122
l1.m1 = 32u ; // NON_COMPLIANT
68
123
l1.m1 = u8 ; // COMPLIANT
69
- l1.m1 = u16 ; // NON_COMPLIANT
124
+ l1.m1 = u16 ; // NON_COMPLIANT
70
125
}
71
126
72
127
// Test enums
@@ -77,9 +132,9 @@ enum States { enabled, disabled };
77
132
void test_enums () {
78
133
Colour l1 = red;
79
134
u8 = red; // COMPLIANT
80
- u32 = red; // COMPLIANT
135
+ u32 = red; // COMPLIANT
81
136
u8 = l1; // NON_COMPLIANT
82
- u32 = l1; // COMPLIANT
137
+ u32 = l1; // COMPLIANT
83
138
u8 = enabled; // COMPLIANT - enabled is not numeric
84
139
}
85
140
@@ -106,7 +161,7 @@ void f3(std::int32_t l1) {}
106
161
107
162
void test_overloaded_functions () {
108
163
f3 (s32); // COMPLIANT
109
- f3 (s8); // NON_COMPLIANT
164
+ f3 (s8); // NON_COMPLIANT
110
165
f3 (s64); // COMPLIANT
111
166
}
112
167
@@ -123,7 +178,7 @@ void test_function_pointers() {
123
178
void f5 (const char *l1, ...) {}
124
179
125
180
void test_variadic_functions () {
126
- f5 (" test" , u8 ); // NON_COMPLIANT - will be promoted to `int`
181
+ f5 (" test" , u8 ); // NON_COMPLIANT - will be promoted to `int`
127
182
f5 (" test" , s32); // COMPLIANT - already `int`, no promotion needed
128
183
}
129
184
@@ -136,8 +191,8 @@ struct A {
136
191
137
192
void A::f7 () {
138
193
f6 (u32 , " answer" ); // NON_COMPLIANT - extensible, could call a global
139
- // function instead - e.g. `void f6(std::uint32_t l1,
140
- // std::string l2)`
194
+ // function instead - e.g. `void f6(std::uint32_t l1,
195
+ // std::string l2)`
141
196
this ->f6 (u32 , " answer" ); // COMPLIANT
142
197
this ->f6 (u32 , 42 ); // COMPLIANT
143
198
}
0 commit comments