|
15 | 15 | import cpp |
16 | 16 | import codingstandards.cpp.misra |
17 | 17 |
|
18 | | -from Cast cast, Type sourceType, Type targetType |
| 18 | +from Cast cast, Type sourceType, PointerType targetType, string typeKind |
19 | 19 | where |
20 | 20 | not isExcluded(cast, Conversions2Package::intToPointerCastProhibitedQuery()) and |
21 | | - sourceType = cast.getExpr().getType().stripTopLevelSpecifiers() and |
22 | | - targetType = cast.getType().stripTopLevelSpecifiers() and |
23 | | - targetType instanceof PointerType and |
24 | | - not targetType instanceof FunctionPointerType and |
25 | | - not ( |
26 | | - // Exception: casts between void pointers are allowed |
27 | | - targetType.(PointerType).getBaseType().stripTopLevelSpecifiers() instanceof VoidType and |
28 | | - sourceType instanceof PointerType and |
29 | | - sourceType.(PointerType).getBaseType().stripTopLevelSpecifiers() instanceof VoidType |
30 | | - ) and |
| 21 | + sourceType = cast.getExpr().getType().getUnspecifiedType() and |
| 22 | + targetType = cast.getType().getUnspecifiedType() and |
31 | 23 | ( |
32 | 24 | // Integral types |
33 | | - sourceType instanceof IntegralType |
| 25 | + sourceType instanceof IntegralType and |
| 26 | + typeKind = "integral" |
34 | 27 | or |
35 | 28 | // Enumerated types |
36 | | - sourceType instanceof Enum |
| 29 | + sourceType instanceof Enum and |
| 30 | + typeKind = "enumerated" |
37 | 31 | or |
38 | 32 | // Pointer to void type |
39 | | - sourceType instanceof PointerType and |
40 | | - sourceType.(PointerType).getBaseType().stripTopLevelSpecifiers() instanceof VoidType |
| 33 | + sourceType.(PointerType).getBaseType() instanceof VoidType and |
| 34 | + typeKind = "pointer to void" and |
| 35 | + // Exception: casts between void pointers are allowed |
| 36 | + not targetType.getBaseType() instanceof VoidType |
41 | 37 | ) |
42 | 38 | select cast, |
43 | | - "Cast from '" + sourceType.toString() + "' to '" + targetType.toString() + "' is prohibited." |
| 39 | + "Cast from " + typeKind + " type '" + cast.getExpr().getType() + "' to pointer type '" + |
| 40 | + cast.getType() + "'." |
0 commit comments