Skip to content

Commit 54fe5ea

Browse files
committed
A3-9-1: Convert to shared query
Convert AUTOSAR rule A3-9-1 to a shared query to reuse the implementation for MISRA C++ 2023 Rule 6.9.2.
1 parent 55cebdb commit 54fe5ea

File tree

10 files changed

+149
-105
lines changed

10 files changed

+149
-105
lines changed

cpp/autosar/src/rules/A3-9-1/VariableWidthIntegerTypesUsed.ql

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,10 @@
1717

1818
import cpp
1919
import codingstandards.cpp.autosar
20-
import codingstandards.cpp.EncapsulatingFunctions
21-
import codingstandards.cpp.BuiltInNumericTypes
22-
import codingstandards.cpp.Type
23-
import codingstandards.cpp.Operator
20+
import codingstandards.cpp.rules.variablewidthintegertypesused.VariableWidthIntegerTypesUsed
2421

25-
from Variable v, Type typeStrippedOfSpecifiers
26-
where
27-
not isExcluded(v, DeclarationsPackage::variableWidthIntegerTypesUsedQuery()) and
28-
typeStrippedOfSpecifiers = stripSpecifiers(v.getType()) and
29-
(
30-
typeStrippedOfSpecifiers instanceof BuiltInIntegerType or
31-
typeStrippedOfSpecifiers instanceof UnsignedCharType or
32-
typeStrippedOfSpecifiers instanceof SignedCharType
33-
) and
34-
not v instanceof ExcludedVariable and
35-
// Dont consider template instantiations because instantiations with
36-
// Fixed Width Types are recorded after stripping their typedef'd type,
37-
// thereby, causing false positives (#540).
38-
not v.isFromTemplateInstantiation(_) and
39-
//post-increment/post-decrement operators are required by the standard to have a dummy int parameter
40-
not v.(Parameter).getFunction() instanceof PostIncrementOperator and
41-
not v.(Parameter).getFunction() instanceof PostDecrementOperator
42-
select v, "Variable '" + v.getName() + "' has variable-width type."
22+
class VariableWidthIntegerTypesUsedQuery extends VariableWidthIntegerTypesUsedSharedQuery {
23+
VariableWidthIntegerTypesUsedQuery() {
24+
this = DeclarationsPackage::variableWidthIntegerTypesUsedQuery()
25+
}
26+
}

cpp/autosar/test/rules/A3-9-1/VariableWidthIntegerTypesUsed.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/variablewidthintegertypesused/VariableWidthIntegerTypesUsed.ql
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
| test.cpp:4:8:4:8 | c | Variable 'c' has variable-width char type. |
2-
| test.cpp:38:14:38:15 | c1 | Variable 'c1' has variable-width char type. |
3-
| test.cpp:56:17:56:18 | c2 | Variable 'c2' has variable-width char type. |
2+
| test.cpp:10:14:10:15 | c1 | Variable 'c1' has variable-width char type. |
3+
| test.cpp:14:17:14:18 | c2 | Variable 'c2' has variable-width char type. |

cpp/autosar/test/rules/A3-9-1/test.cpp

Lines changed: 7 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,16 @@
22

33
void test_variable_width_type_variables() {
44
char c; // NON_COMPLIANT
5-
unsigned char uc; // NON_COMPLIANT
6-
signed char sc; // NON_COMPLIANT
7-
8-
int i; // NON_COMPLIANT
9-
unsigned int ui; // NON_COMPLIANT
10-
unsigned u; // NON_COMPLIANT
11-
signed int si; // NON_COMPLIANT
12-
signed s; // NON_COMPLIANT
13-
14-
short sh; // NON_COMPLIANT
15-
unsigned short ush; // NON_COMPLIANT
16-
signed short ssh; // NON_COMPLIANT
17-
18-
long l; // NON_COMPLIANT
19-
unsigned long ul; // NON_COMPLIANT
20-
signed long sl; // NON_COMPLIANT
21-
22-
std::int8_t i8; // COMPLIANT
23-
std::int16_t i16; // COMPLIANT
24-
std::int32_t i32; // COMPLIANT
25-
std::int64_t i64; // COMPLIANT
26-
27-
std::uint8_t u8; // COMPLIANT
28-
std::uint16_t u16; // COMPLIANT
29-
std::uint32_t u32; // COMPLIANT
30-
std::uint64_t u64; // COMPLIANT
31-
}
32-
33-
int main(int argc, char *argv[]) { // COMPLIANT
34-
// main as an exception
5+
unsigned char uc; // COMPLIANT - covered by VariableWidthIntegerTypesUsed
6+
signed char sc; // COMPLIANT - covered by VariableWidthIntegerTypesUsed
357
}
368

379
void test_variable_width_type_qualified_variables() {
3810
const char c1 = 0; // NON_COMPLIANT
39-
const unsigned char uc1 = 0; // NON_COMPLIANT
40-
const signed char sc1 = 0; // NON_COMPLIANt
41-
42-
const int i1 = 0; // NON_COMPLIANT
43-
const unsigned int ui1 = 0; // NON_COMPLIANT
44-
const unsigned u1 = 0; // NON_COMPLIANT
45-
const signed int si1 = 0; // NON_COMPLIANT
46-
const signed s1 = 0; // NON_COMPLIANT
47-
48-
const short sh1 = 0; // NON_COMPLIANT
49-
const unsigned short ush1 = 0; // NON_COMPLIANT
50-
const signed short ssh1 = 0; // NON_COMPLIANT
51-
52-
const long l1 = 0; // NON_COMPLIANT
53-
const unsigned long ul1 = 0; // NON_COMPLIANT
54-
const signed long sl1 = 0; // NON_COMPLIANT
11+
const unsigned char uc1 = 0; // COMPLIANT - (VariableWidthIntegerTypesUsed)
12+
const signed char sc1 = 0; // COMPLIANT - (VariableWidthIntegerTypesUsed)
5513

5614
volatile char c2; // NON_COMPLIANT
57-
volatile unsigned char uc2; // NON_COMPLIANT
58-
volatile signed char sc2; // NON_COMPLIANt
59-
60-
volatile int i2; // NON_COMPLIANT
61-
volatile unsigned int ui2; // NON_COMPLIANT
62-
volatile unsigned u2; // NON_COMPLIANT
63-
volatile signed int si2; // NON_COMPLIANT
64-
volatile signed s2; // NON_COMPLIANT
65-
66-
volatile short sh2; // NON_COMPLIANT
67-
volatile unsigned short ush2; // NON_COMPLIANT
68-
volatile signed short ssh2; // NON_COMPLIANT
69-
70-
volatile long l2; // NON_COMPLIANT
71-
volatile unsigned long ul2; // NON_COMPLIANT
72-
volatile signed long sl2; // NON_COMPLIANT
73-
}
74-
75-
struct test_fix_fp_614 {
76-
test_fix_fp_614 operator++(int); // COMPLIANT
77-
test_fix_fp_614 operator--(int); // COMPLIANT
78-
};
79-
80-
// COMPLIANT - instantiated with Fixed Width Types.
81-
template <typename MyType> constexpr void test_fix_fp_540(MyType value) {
82-
value++;
83-
}
84-
85-
int call_test_fix_fp_540() {
86-
test_fix_fp_540<std::uint8_t>(19);
87-
test_fix_fp_540<std::int16_t>(20);
88-
return 0;
89-
}
15+
volatile unsigned char uc2; // COMPLIANT - (VariableWidthIntegerTypesUsed)
16+
volatile signed char sc2; // COMPLIANT - (VariableWidthIntegerTypesUsed)
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Provides a library with a `problems` predicate for the following issue:
3+
* The basic numerical types of signed/unsigned char, int, short, long are not supposed
4+
* to be used. The specific-length types from <cstdint> header need be used instead.
5+
*/
6+
7+
import cpp
8+
import codingstandards.cpp.Customizations
9+
import codingstandards.cpp.Exclusions
10+
import codingstandards.cpp.EncapsulatingFunctions
11+
import codingstandards.cpp.BuiltInNumericTypes
12+
import codingstandards.cpp.Type
13+
import codingstandards.cpp.Operator
14+
15+
abstract class VariableWidthIntegerTypesUsedSharedQuery extends Query { }
16+
17+
Query getQuery() { result instanceof VariableWidthIntegerTypesUsedSharedQuery }
18+
19+
query predicate problems(Variable v, string message) {
20+
not isExcluded(v, getQuery()) and
21+
exists(Type typeStrippedOfSpecifiers |
22+
typeStrippedOfSpecifiers = stripSpecifiers(v.getType()) and
23+
(
24+
typeStrippedOfSpecifiers instanceof BuiltInIntegerType or
25+
typeStrippedOfSpecifiers instanceof UnsignedCharType or
26+
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+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.variablewidthintegertypesused.VariableWidthIntegerTypesUsed
3+
4+
class TestFileQuery extends VariableWidthIntegerTypesUsedSharedQuery, TestQuery { }
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <cstdint>
2+
3+
void test_variable_width_type_variables() {
4+
char c; // COMPLIANT
5+
unsigned char uc; // NON_COMPLIANT
6+
signed char sc; // NON_COMPLIANT
7+
8+
int i; // NON_COMPLIANT
9+
unsigned int ui; // NON_COMPLIANT
10+
unsigned u; // NON_COMPLIANT
11+
signed int si; // NON_COMPLIANT
12+
signed s; // NON_COMPLIANT
13+
14+
short sh; // NON_COMPLIANT
15+
unsigned short ush; // NON_COMPLIANT
16+
signed short ssh; // NON_COMPLIANT
17+
18+
long l; // NON_COMPLIANT
19+
unsigned long ul; // NON_COMPLIANT
20+
signed long sl; // NON_COMPLIANT
21+
22+
std::int8_t i8; // COMPLIANT
23+
std::int16_t i16; // COMPLIANT
24+
std::int32_t i32; // COMPLIANT
25+
std::int64_t i64; // COMPLIANT
26+
27+
std::uint8_t u8; // COMPLIANT
28+
std::uint16_t u16; // COMPLIANT
29+
std::uint32_t u32; // COMPLIANT
30+
std::uint64_t u64; // COMPLIANT
31+
}
32+
33+
int main(int argc, char *argv[]) { // COMPLIANT
34+
// main as an exception
35+
}
36+
37+
void test_variable_width_type_qualified_variables() {
38+
const char c1 = 0; // COMPLIANT
39+
const unsigned char uc1 = 0; // NON_COMPLIANT
40+
const signed char sc1 = 0; // NON_COMPLIANt
41+
42+
const int i1 = 0; // NON_COMPLIANT
43+
const unsigned int ui1 = 0; // NON_COMPLIANT
44+
const unsigned u1 = 0; // NON_COMPLIANT
45+
const signed int si1 = 0; // NON_COMPLIANT
46+
const signed s1 = 0; // NON_COMPLIANT
47+
48+
const short sh1 = 0; // NON_COMPLIANT
49+
const unsigned short ush1 = 0; // NON_COMPLIANT
50+
const signed short ssh1 = 0; // NON_COMPLIANT
51+
52+
const long l1 = 0; // NON_COMPLIANT
53+
const unsigned long ul1 = 0; // NON_COMPLIANT
54+
const signed long sl1 = 0; // NON_COMPLIANT
55+
56+
volatile char c2; // COMPLIANT
57+
volatile unsigned char uc2; // NON_COMPLIANT
58+
volatile signed char sc2; // NON_COMPLIANt
59+
60+
volatile int i2; // NON_COMPLIANT
61+
volatile unsigned int ui2; // NON_COMPLIANT
62+
volatile unsigned u2; // NON_COMPLIANT
63+
volatile signed int si2; // NON_COMPLIANT
64+
volatile signed s2; // NON_COMPLIANT
65+
66+
volatile short sh2; // NON_COMPLIANT
67+
volatile unsigned short ush2; // NON_COMPLIANT
68+
volatile signed short ssh2; // NON_COMPLIANT
69+
70+
volatile long l2; // NON_COMPLIANT
71+
volatile unsigned long ul2; // NON_COMPLIANT
72+
volatile signed long sl2; // NON_COMPLIANT
73+
}
74+
75+
struct test_fix_fp_614 {
76+
test_fix_fp_614 operator++(int); // COMPLIANT
77+
test_fix_fp_614 operator--(int); // COMPLIANT
78+
};
79+
80+
// COMPLIANT - instantiated with Fixed Width Types.
81+
template <typename MyType> constexpr void test_fix_fp_540(MyType value) {
82+
value++;
83+
}
84+
85+
int call_test_fix_fp_540() {
86+
test_fix_fp_540<std::uint8_t>(19);
87+
test_fix_fp_540<std::int16_t>(20);
88+
return 0;
89+
}

rule_packages/cpp/Declarations.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@
9393
],
9494
"implementation_scope": {
9595
"description": "This implementation excludes the plain char type from consideration."
96-
}
96+
},
97+
"shared_implementation_short_name": "VariableWidthIntegerTypesUsed"
9798
},
9899
{
99100
"description": "The basic numerical type char is not supposed to be used. The specific-length types from <cstdint> header need be used instead.",

0 commit comments

Comments
 (0)