|
18 | 18 |
|
19 | 19 | import cpp |
20 | 20 | import codingstandards.cpp.cert |
21 | | -import codingstandards.cpp.Enums |
22 | | -import semmle.code.cpp.rangeanalysis.SimpleRangeAnalysis |
23 | | -import codingstandards.cpp.SimpleRangeAnalysisCustomizations |
| 21 | +import codingstandards.cpp.rules.donotcasttoanoutofrangeenumerationvalueshared.DoNotCastToAnOutOfRangeEnumerationValueShared |
24 | 22 |
|
25 | | -from Cast c, Enum e, string description |
26 | | -where |
27 | | - not isExcluded(c, TypeRangesPackage::doNotCastToAnOutOfRangeEnumerationValueQuery()) and |
28 | | - // Conversion from an integral type to an enum type |
29 | | - c.getExpr().getType().getUnspecifiedType() instanceof IntegralType and |
30 | | - c.getType().getUnspecifiedType() = e and |
31 | | - not ( |
32 | | - // The deduced bound for the expression is within the type range for the explicit type |
33 | | - upperBound(c.getExpr()) <= Enums::getValueRangeUpperBound(e) and |
34 | | - lowerBound(c.getExpr()) >= Enums::getValueRangeLowerBound(e) |
35 | | - ) and |
36 | | - // Not a compile time constant with the same value as an existing enum constant |
37 | | - not exists(float enumConstantValue | |
38 | | - enumConstantValue = Enums::getEnumConstantValue(e.getAnEnumConstant()) |
39 | | - | |
40 | | - // Expression is a constant |
41 | | - c.getExpr().getValue().toFloat() = enumConstantValue |
42 | | - or |
43 | | - // Range analysis has precise bounds |
44 | | - enumConstantValue = upperBound(c.getExpr()) and |
45 | | - enumConstantValue = lowerBound(c.getExpr()) |
46 | | - ) and |
47 | | - ( |
48 | | - if exists(upperBound(c.getExpr())) |
49 | | - then |
50 | | - description = |
51 | | - "Cast to enum $@ with value range " + Enums::getValueRangeLowerBound(e) + "..." + |
52 | | - Enums::getValueRangeUpperBound(e) + " from expression with wider value range " + |
53 | | - lowerBound(c.getExpr()) + "..." + upperBound(c.getExpr()) + " in function " + |
54 | | - c.getEnclosingFunction().getName() + "." |
55 | | - else |
56 | | - description = |
57 | | - "Cast to enum $@ with value range " + Enums::getValueRangeLowerBound(e) + "..." + |
58 | | - Enums::getValueRangeUpperBound(e) + |
59 | | - " from expression with a potentially wider value range." |
60 | | - ) |
61 | | -select c, description, e, e.getName() |
| 23 | +module DoNotCastToAnOutOfRangeEnumerationValueConfig implements |
| 24 | + DoNotCastToAnOutOfRangeEnumerationValueSharedConfigSig |
| 25 | +{ |
| 26 | + Query getQuery() { result = TypeRangesPackage::doNotCastToAnOutOfRangeEnumerationValueQuery() } |
| 27 | +} |
| 28 | + |
| 29 | +import DoNotCastToAnOutOfRangeEnumerationValueShared<DoNotCastToAnOutOfRangeEnumerationValueConfig> |
0 commit comments