|
| 1 | +/** Provides the numeric-type detector shared by legacy and current directive wrappers. */ |
| 2 | + |
| 3 | +import cpp |
| 4 | +import codingstandards.cpp.BuiltInNumericTypes |
| 5 | + |
| 6 | +/** A numeric type covered by the legacy directive. */ |
| 7 | +private class NumericType extends BuiltInType { |
| 8 | + NumericType() { |
| 9 | + this.(CharType).isExplicitlySigned() |
| 10 | + or |
| 11 | + this.(CharType).isExplicitlyUnsigned() |
| 12 | + or |
| 13 | + this instanceof BuiltInIntegerType |
| 14 | + or |
| 15 | + this instanceof FloatType |
| 16 | + or |
| 17 | + this instanceof DoubleType |
| 18 | + or |
| 19 | + this instanceof LongDoubleType |
| 20 | + or |
| 21 | + this instanceof ComplexNumberType |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** A typedef name that encodes numeric width and signedness. */ |
| 26 | +private class SizedTypeName extends string { |
| 27 | + string pattern; |
| 28 | + int width; |
| 29 | + |
| 30 | + bindingset[this] |
| 31 | + pragma[inline] |
| 32 | + SizedTypeName() { |
| 33 | + pattern = "(u?int|c?float)(4|8|16|32|64|128)_t" and |
| 34 | + this.regexpMatch(pattern) and |
| 35 | + width = this.regexpCapture(pattern, 2).toInt() |
| 36 | + } |
| 37 | + |
| 38 | + /** Gets the width encoded by this name. */ |
| 39 | + bindingset[this] |
| 40 | + pragma[inline] |
| 41 | + int getWidth() { result = width } |
| 42 | + |
| 43 | + /** Holds if this name denotes a complex type. */ |
| 44 | + bindingset[this] |
| 45 | + pragma[inline] |
| 46 | + predicate isComplex() { this.charAt(0) = "c" } |
| 47 | +} |
| 48 | + |
| 49 | +/** Holds if `type` has an invalid complex name, with `message` explaining why. */ |
| 50 | +private predicate forbiddenComplexType(CTypedefType type, string message) { |
| 51 | + type.getName().(SizedTypeName).isComplex() and |
| 52 | + ( |
| 53 | + if type.getBaseType().stripTopLevelSpecifiers() instanceof ComplexNumberType |
| 54 | + then |
| 55 | + type.getSize() * 8 != type.getName().(SizedTypeName).getWidth() * 2 and |
| 56 | + message = "The typedef type " + type.getName() + " does not have its indicated real size." |
| 57 | + else message = "The typedef type " + type.getName() + " is not a complex type." |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +/** Holds if `type` has an invalid real name, with `message` explaining why. */ |
| 62 | +private predicate forbiddenRealType(CTypedefType type, string message) { |
| 63 | + not type.getName().(SizedTypeName).isComplex() and |
| 64 | + ( |
| 65 | + if type.getBaseType().stripTopLevelSpecifiers() instanceof ComplexNumberType |
| 66 | + then message = "The typedef name " + type.getName() + " does not indicate a complex type." |
| 67 | + else ( |
| 68 | + type.getSize() * 8 != type.getName().(SizedTypeName).getWidth() and |
| 69 | + message = "The typedef type " + type.getName() + " does not have its indicated size." |
| 70 | + ) |
| 71 | + ) |
| 72 | +} |
| 73 | + |
| 74 | +/** Holds if `type` is a forbidden alias, with `message` explaining why. */ |
| 75 | +private predicate forbiddenAlias(CTypedefType type, string message) { |
| 76 | + if type.getName() instanceof SizedTypeName |
| 77 | + then forbiddenRealType(type, message) or forbiddenComplexType(type, message) |
| 78 | + else ( |
| 79 | + ( |
| 80 | + type.getBaseType() instanceof NumericType |
| 81 | + or |
| 82 | + forbiddenAlias(type.getBaseType(), _) |
| 83 | + ) and |
| 84 | + message = "The type " + type.getName() + " is not an alias to a fixed-width numeric type." |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +/** Holds if `element` violates the legacy directive, with `message` explaining why. */ |
| 89 | +predicate problems(Element element, string message) { |
| 90 | + exists(Variable variable | variable = element | |
| 91 | + variable.getType() instanceof NumericType and |
| 92 | + not variable instanceof ExcludedVariable and |
| 93 | + message = "The type " + variable.getType() + " is not a fixed-width numeric type." |
| 94 | + ) |
| 95 | + or |
| 96 | + forbiddenAlias(element, message) |
| 97 | +} |
| 98 | + |
| 99 | +/** Holds if `element` declares or aliases a real or complex floating-point type. */ |
| 100 | +predicate hasFloatingType(Element element) { |
| 101 | + exists(Type type | |
| 102 | + type = element.(Variable).getType() or type = element.(CTypedefType).getBaseType() |
| 103 | + | |
| 104 | + type.stripType() instanceof FloatingPointType |
| 105 | + or |
| 106 | + type.stripType() instanceof ComplexNumberType |
| 107 | + ) |
| 108 | +} |
0 commit comments