Skip to content

Commit aeb9b65

Browse files
committed
Factor out shared clauses to common
1 parent f6488aa commit aeb9b65

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

c/misra/src/rules/DIR-4-6/PlainNumericalTypeUsedOverExplicitTypedef.ql

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,16 @@
1212

1313
import cpp
1414
import codingstandards.c.misra
15+
import codingstandards.cpp.BuiltInNumericTypes
1516

16-
class BuiltinNumericType extends BuiltInType {
17-
BuiltinNumericType() {
17+
class BuiltInNumericType extends BuiltInType {
18+
BuiltInNumericType() {
1819
/* Exclude the plain char because it does not count as a numeric type */
1920
this.(CharType).isExplicitlySigned()
2021
or
2122
this.(CharType).isExplicitlyUnsigned()
2223
or
23-
this instanceof ShortType
24-
or
25-
this instanceof IntType
26-
or
27-
this instanceof LongType
28-
or
29-
this instanceof LongLongType
24+
this instanceof BuiltInIntegerType
3025
or
3126
this instanceof FloatType
3227
or
@@ -37,12 +32,13 @@ class BuiltinNumericType extends BuiltInType {
3732
}
3833

3934
predicate forbiddenBuiltinNumericUsedInDecl(Variable var, string message) {
40-
var.getType() instanceof BuiltinNumericType and
35+
var.getType() instanceof BuiltInNumericType and
36+
not var instanceof ExcludedVariable and
4137
message = "The type " + var.getType() + " is not a fixed-width numeric type."
4238
}
4339

4440
predicate forbiddenTypedef(TypedefType typedef, string message) {
45-
typedef.getBaseType() instanceof BuiltinNumericType and
41+
typedef.getBaseType() instanceof BuiltInNumericType and
4642
not typedef.getName().regexpMatch("u?(int|float)(4|8|16|32|64|128)_t") and
4743
message = "The type " + typedef.getName() + " is not an alias to a fixed-width numeric type."
4844
}

c/misra/test/rules/DIR-4-6/PlainNumericalTypeUsedOverExplicitTypedef.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
| test.c:27:5:27:26 | _astronomical_number_t | The type _astronomical_number_t is not an alias to a fixed-width numeric type. |
2-
| test.c:30:10:30:13 | argc | The type int is not a fixed-width numeric type. |
32
| test.c:34:15:34:16 | c2 | The type signed char is not a fixed-width numeric type. |
43
| test.c:35:17:35:18 | c3 | The type unsigned char is not a fixed-width numeric type. |
54
| test.c:38:9:38:10 | s1 | The type short is not a fixed-width numeric type. |

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,16 @@
1818
import cpp
1919
import codingstandards.cpp.autosar
2020
import codingstandards.cpp.EncapsulatingFunctions
21-
22-
/**
23-
* any `Parameter` in a main function like:
24-
* int main(int argc, char *argv[])
25-
*/
26-
class ExcludedVariable extends Parameter {
27-
ExcludedVariable() { getFunction() instanceof MainFunction }
28-
}
21+
import codingstandards.cpp.BuiltInNumericTypes
2922

3023
from Variable v
3124
where
3225
not isExcluded(v, DeclarationsPackage::variableWidthIntegerTypesUsedQuery()) and
3326
(
34-
v.getType() instanceof PlainCharType
35-
or
36-
v.getType() instanceof UnsignedCharType
37-
or
27+
v.getType() instanceof BuiltInIntegerType or
28+
v.getType() instanceof PlainCharType or
29+
v.getType() instanceof UnsignedCharType or
3830
v.getType() instanceof SignedCharType
39-
or
40-
v.getType() instanceof ShortType
41-
or
42-
v.getType() instanceof IntType
43-
or
44-
v.getType() instanceof LongType
45-
or
46-
v.getType() instanceof LongLongType
4731
) and
4832
not v instanceof ExcludedVariable
4933
select v, "Variable '" + v.getName() + "' has variable-width type."
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import cpp
2+
import codingstandards.cpp.EncapsulatingFunctions
3+
4+
class BuiltInIntegerType extends BuiltInType {
5+
BuiltInIntegerType() {
6+
this instanceof ShortType
7+
or
8+
this instanceof IntType
9+
or
10+
this instanceof LongType
11+
or
12+
this instanceof LongLongType
13+
}
14+
}
15+
16+
/**
17+
* any `Parameter` in a main function like:
18+
* int main(int argc, char *argv[])
19+
*/
20+
class ExcludedVariable extends Parameter {
21+
ExcludedVariable() { getFunction() instanceof MainFunction }
22+
}

0 commit comments

Comments
 (0)