Skip to content

Commit f43336a

Browse files
committed
VariableWidthIntegerTypesUsed - support function return types
Expand support for A3-9-1 and MISRA C++ 2023 6.9.2 to include the use of integers in function return types.
1 parent 5b37d13 commit f43336a

File tree

5 files changed

+108
-19
lines changed

5 files changed

+108
-19
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A3-9-1` - `VariableWidthIntegerTypesUsed.ql`:
2+
- This query now reports the use of non-fixed width integer types in function return types, with the exception of `char` types and for `main` functions.

cpp/common/src/codingstandards/cpp/BuiltInNumericTypes.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ class BuiltInIntegerType extends BuiltInType {
2020
class ExcludedVariable extends Parameter {
2121
ExcludedVariable() { getFunction() instanceof MainFunction }
2222
}
23+
24+
/**
25+
* Any main function.
26+
*/
27+
class ExcludedFunction extends Function {
28+
ExcludedFunction() { this instanceof MainFunction }
29+
}

cpp/common/src/codingstandards/cpp/rules/variablewidthintegertypesused/VariableWidthIntegerTypesUsed.qll

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,37 @@ abstract class VariableWidthIntegerTypesUsedSharedQuery extends Query { }
1616

1717
Query getQuery() { result instanceof VariableWidthIntegerTypesUsedSharedQuery }
1818

19-
query predicate problems(Variable v, string message) {
20-
not isExcluded(v, getQuery()) and
21-
exists(Type typeStrippedOfSpecifiers |
22-
typeStrippedOfSpecifiers = stripSpecifiers(v.getType()) and
19+
query predicate problems(Element e, string message) {
20+
not isExcluded(e, getQuery()) and
21+
exists(Type typeStrippedOfSpecifiers, Type rawType |
22+
typeStrippedOfSpecifiers = stripSpecifiers(rawType) and
2323
(
2424
typeStrippedOfSpecifiers instanceof BuiltInIntegerType or
2525
typeStrippedOfSpecifiers instanceof UnsignedCharType or
2626
typeStrippedOfSpecifiers instanceof SignedCharType
27-
) and
28-
not v instanceof ExcludedVariable and
29-
// Dont consider template instantiations because instantiations with
30-
// Fixed Width Types are recorded after stripping their typedef'd type,
31-
// thereby, causing false positives (#540).
32-
not v.isFromTemplateInstantiation(_) and
33-
//post-increment/post-decrement operators are required by the standard to have a dummy int parameter
34-
not v.(Parameter).getFunction() instanceof PostIncrementOperator and
35-
not v.(Parameter).getFunction() instanceof PostDecrementOperator
36-
) and
37-
message = "Variable '" + v.getName() + "' has variable-width type."
38-
}
27+
)
28+
|
29+
exists(Variable v | v = e |
30+
v.getType() = rawType and
31+
not v instanceof ExcludedVariable and
32+
// Dont consider template instantiations because instantiations with
33+
// Fixed Width Types are recorded after stripping their typedef'd type,
34+
// thereby, causing false positives (#540).
35+
not v.isFromTemplateInstantiation(_) and
36+
//post-increment/post-decrement operators are required by the standard to have a dummy int parameter
37+
not v.(Parameter).getFunction() instanceof PostIncrementOperator and
38+
not v.(Parameter).getFunction() instanceof PostDecrementOperator and
39+
message = "Variable '" + v.getName() + "' has variable-width type."
40+
)
41+
or
42+
exists(Function f | f = e |
43+
f.getType() = rawType and
44+
not f instanceof ExcludedFunction and
45+
// Dont consider template instantiations because instantiations with
46+
// Fixed Width Types are recorded after stripping their typedef'd type,
47+
// thereby, causing false positives (#540).
48+
not f.isFromTemplateInstantiation(_) and
49+
message = "Function '" + f.getName() + "' has variable-width return type."
50+
)
51+
)
52+
}

cpp/common/test/rules/variablewidthintegertypesused/test.cpp

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,74 @@ template <typename MyType> constexpr void test_fix_fp_540(MyType value) {
8282
value++;
8383
}
8484

85-
int call_test_fix_fp_540() {
85+
void call_test_fix_fp_540() {
8686
test_fix_fp_540<std::uint8_t>(19);
8787
test_fix_fp_540<std::int16_t>(20);
88-
return 0;
88+
}
89+
90+
char test_char_return() { // COMPLIANT
91+
return 'a';
92+
}
93+
unsigned char test_unsigned_char_return() { // NON_COMPLIANT
94+
return 'b';
95+
}
96+
signed char test_signed_char_return() { // NON_COMPLIANT
97+
return 'c';
98+
}
99+
int test_int_return() { // NON_COMPLIANT
100+
return 42;
101+
}
102+
unsigned int test_unsigned_int_return() { // NON_COMPLIANT
103+
return 43;
104+
}
105+
unsigned test_unsigned_return() { // NON_COMPLIANT
106+
return 44;
107+
}
108+
signed int test_signed_int_return() { // NON_COMPLIANT
109+
return 45;
110+
}
111+
signed test_signed_return() { // NON_COMPLIANT
112+
return 46;
113+
}
114+
short test_short_return() { // NON_COMPLIANT
115+
return 47;
116+
}
117+
unsigned short test_unsigned_short_return() { // NON_COMPLIANT
118+
return 48;
119+
}
120+
signed short test_signed_short_return() { // NON_COMPLIANT
121+
return 49;
122+
}
123+
long test_long_return() { // NON_COMPLIANT
124+
return 50;
125+
}
126+
unsigned long test_unsigned_long_return() { // NON_COMPLIANT
127+
return 51;
128+
}
129+
signed long test_signed_long_return() { // NON_COMPLIANT
130+
return 52;
131+
}
132+
std::int8_t test_int8_t_return() { // COMPLIANT
133+
return 53;
134+
}
135+
std::int16_t test_int16_t_return() { // COMPLIANT
136+
return 54;
137+
}
138+
std::int32_t test_int32_t_return() { // COMPLIANT
139+
return 55;
140+
}
141+
std::int64_t test_int64_t_return() { // COMPLIANT
142+
return 56;
143+
}
144+
std::uint8_t test_uint8_t_return() { // COMPLIANT
145+
return 57;
146+
}
147+
std::uint16_t test_uint16_t_return() { // COMPLIANT
148+
return 58;
149+
}
150+
std::uint32_t test_uint32_t_return() { // COMPLIANT
151+
return 59;
152+
}
153+
std::uint64_t test_uint64_t_return() { // COMPLIANT
154+
return 60;
89155
}

rule_packages/cpp/Declarations.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"maintainability"
9393
],
9494
"implementation_scope": {
95-
"description": "This implementation excludes the plain char type from consideration."
95+
"description": "This implementation excludes the plain char type. It also excludes the use of standard integer types in the definition of main functions, and the use of an integer parameter in the declaration of postfix operators."
9696
},
9797
"shared_implementation_short_name": "VariableWidthIntegerTypesUsed"
9898
},

0 commit comments

Comments
 (0)