|
16 | 16 | import cpp
|
17 | 17 | import codingstandards.cpp.autosar
|
18 | 18 |
|
19 |
| -from Variable v, Expr aexp |
| 19 | +newtype TTemplatedElement = |
| 20 | + TClassTemplate(TemplateClass c) or |
| 21 | + TFunctionTemplate(TemplateFunction f) or |
| 22 | + TVariableTemplate(TemplateVariable v) |
| 23 | + |
| 24 | +class TemplatedElement extends TTemplatedElement { |
| 25 | + TemplateClass asTemplateClass() { this = TClassTemplate(result) } |
| 26 | + |
| 27 | + TemplateFunction asTemplateFunction() { this = TFunctionTemplate(result) } |
| 28 | + |
| 29 | + TemplateVariable asTemplateVariable() { this = TVariableTemplate(result) } |
| 30 | + |
| 31 | + string toString() { |
| 32 | + result = this.asTemplateClass().toString() or |
| 33 | + result = this.asTemplateFunction().toString() or |
| 34 | + result = this.asTemplateVariable().toString() |
| 35 | + } |
| 36 | + |
| 37 | + Location getLocation() { |
| 38 | + result = this.asTemplateClass().getLocation() or |
| 39 | + result = this.asTemplateFunction().getLocation() or |
| 40 | + result = this.asTemplateVariable().getLocation() |
| 41 | + } |
| 42 | + |
| 43 | + string getName() { |
| 44 | + result = this.asTemplateClass().getName() or |
| 45 | + result = this.asTemplateFunction().getName() or |
| 46 | + result = this.asTemplateVariable().getName() |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +newtype TTemplateInstantiation = |
| 51 | + TClassTemplateInstantiation(ClassTemplateInstantiation c) or |
| 52 | + TFunctionTemplateInstantiation(FunctionTemplateInstantiation f) or |
| 53 | + TVariableTemplateInstantiation(VariableTemplateInstantiation v) |
| 54 | + |
| 55 | +class TemplateInstantiation extends TTemplateInstantiation { |
| 56 | + ClassTemplateInstantiation asClassTemplateInstantiation() { |
| 57 | + this = TClassTemplateInstantiation(result) |
| 58 | + } |
| 59 | + |
| 60 | + FunctionTemplateInstantiation asFunctionTemplateInstantiation() { |
| 61 | + this = TFunctionTemplateInstantiation(result) |
| 62 | + } |
| 63 | + |
| 64 | + VariableTemplateInstantiation asVariableTemplateInstantiation() { |
| 65 | + this = TVariableTemplateInstantiation(result) |
| 66 | + } |
| 67 | + |
| 68 | + string toString() { |
| 69 | + result = this.asClassTemplateInstantiation().toString() or |
| 70 | + result = this.asFunctionTemplateInstantiation().toString() or |
| 71 | + result = this.asVariableTemplateInstantiation().toString() |
| 72 | + } |
| 73 | + |
| 74 | + Location getLocation() { |
| 75 | + result = this.asClassTemplateInstantiation().getLocation() or |
| 76 | + result = this.asFunctionTemplateInstantiation().getLocation() or |
| 77 | + result = this.asVariableTemplateInstantiation().getLocation() |
| 78 | + } |
| 79 | + |
| 80 | + Element asElement() { |
| 81 | + result = this.asClassTemplateInstantiation() or |
| 82 | + result = this.asFunctionTemplateInstantiation() or |
| 83 | + result = this.asVariableTemplateInstantiation() |
| 84 | + } |
| 85 | + |
| 86 | + TemplatedElement getTemplate() { |
| 87 | + result.asTemplateClass() = this.asClassTemplateInstantiation().getTemplate() or |
| 88 | + result.asTemplateFunction() = this.asFunctionTemplateInstantiation().getTemplate() or |
| 89 | + result.asTemplateVariable() = this.asVariableTemplateInstantiation().getTemplate() |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Gets a use of an instantiation of this template. i.e. |
| 94 | + * 1. For a class template, it's where the instantiated type is used by the name. |
| 95 | + * 2. For a function template, it's where the instantiated function is called. |
| 96 | + * 3. For a variable template, it's where the instantiated variable is initialized. |
| 97 | + */ |
| 98 | + Element getAUse() { |
| 99 | + result = this.asClassTemplateInstantiation().getATypeNameUse() or |
| 100 | + result = this.asFunctionTemplateInstantiation().getACallToThisFunction() or |
| 101 | + result = this.asVariableTemplateInstantiation() |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +class ImplicitConversionFromPlainCharType extends Conversion { |
| 106 | + ImplicitConversionFromPlainCharType() { |
| 107 | + this.isImplicit() and |
| 108 | + this.getExpr().getUnspecifiedType() instanceof PlainCharType and |
| 109 | + ( |
| 110 | + this.getUnspecifiedType() instanceof SignedCharType or |
| 111 | + this.getUnspecifiedType() instanceof UnsignedCharType |
| 112 | + ) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +newtype TImplicitConversionElement = |
| 117 | + TImplicitConversionOutsideTemplate(ImplicitConversionFromPlainCharType implicitConversion) { |
| 118 | + not exists(TemplateInstantiation instantiation | |
| 119 | + implicitConversion.isFromTemplateInstantiation(instantiation.asElement()) |
| 120 | + ) |
| 121 | + } or |
| 122 | + TInstantiationOfImplicitConversionTemplate( |
| 123 | + TemplateInstantiation templateInstantiation, |
| 124 | + ImplicitConversionFromPlainCharType implicitConversion |
| 125 | + ) { |
| 126 | + implicitConversion.getEnclosingElement+() = templateInstantiation.asElement() |
| 127 | + } |
| 128 | + |
| 129 | +class ImplicitConversionLocation extends TImplicitConversionElement { |
| 130 | + ImplicitConversionFromPlainCharType asImplicitConversionOutsideTemplate() { |
| 131 | + this = TImplicitConversionOutsideTemplate(result) |
| 132 | + } |
| 133 | + |
| 134 | + TemplateInstantiation asInstantiationOfImplicitConversionTemplate( |
| 135 | + ImplicitConversionFromPlainCharType implicitConversion |
| 136 | + ) { |
| 137 | + this = TInstantiationOfImplicitConversionTemplate(result, implicitConversion) |
| 138 | + } |
| 139 | + |
| 140 | + predicate isImplicitConversionOutsideTemplate() { |
| 141 | + exists(this.asImplicitConversionOutsideTemplate()) |
| 142 | + } |
| 143 | + |
| 144 | + predicate isInstantiationOfImplicitConversionTemplate() { |
| 145 | + exists( |
| 146 | + TemplateInstantiation templateInstantiation, |
| 147 | + ImplicitConversionFromPlainCharType implicitConversion |
| 148 | + | |
| 149 | + templateInstantiation = this.asInstantiationOfImplicitConversionTemplate(implicitConversion) |
| 150 | + ) |
| 151 | + } |
| 152 | + |
| 153 | + ImplicitConversionFromPlainCharType getImplicitConversion() { |
| 154 | + result = this.asImplicitConversionOutsideTemplate() or |
| 155 | + exists(TemplateInstantiation templateInstantiation | |
| 156 | + this = TInstantiationOfImplicitConversionTemplate(templateInstantiation, result) |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + string toString() { |
| 161 | + result = this.asImplicitConversionOutsideTemplate().toString() or |
| 162 | + exists(ImplicitConversionFromPlainCharType implicitConversion | |
| 163 | + result = this.asInstantiationOfImplicitConversionTemplate(implicitConversion).toString() |
| 164 | + ) |
| 165 | + } |
| 166 | + |
| 167 | + Location getLocation() { |
| 168 | + result = this.asImplicitConversionOutsideTemplate().getLocation() or |
| 169 | + exists(ImplicitConversionFromPlainCharType implicitConversion | |
| 170 | + result = this.asInstantiationOfImplicitConversionTemplate(implicitConversion).getLocation() |
| 171 | + ) |
| 172 | + } |
| 173 | + |
| 174 | + Element asElement() { |
| 175 | + result = this.asImplicitConversionOutsideTemplate() or |
| 176 | + exists(ImplicitConversionFromPlainCharType implicitConversion | |
| 177 | + result = this.asInstantiationOfImplicitConversionTemplate(implicitConversion).getAUse() |
| 178 | + ) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +string getMessageTemplate(ImplicitConversionLocation implicitConversionLocation) { |
| 183 | + exists(ImplicitConversionFromPlainCharType implicitConversion | |
| 184 | + implicitConversion = implicitConversionLocation.getImplicitConversion() |
| 185 | + | |
| 186 | + implicitConversionLocation.isImplicitConversionOutsideTemplate() and |
| 187 | + result = |
| 188 | + "Implicit conversion of plain char $@ to " + implicitConversion.getType().getName() + "." |
| 189 | + or |
| 190 | + implicitConversionLocation.isInstantiationOfImplicitConversionTemplate() and |
| 191 | + result = |
| 192 | + "Implicit conversion of plain char $@ to " + implicitConversion.getType().getName() + |
| 193 | + " from instantiating template '" + |
| 194 | + implicitConversionLocation |
| 195 | + .asInstantiationOfImplicitConversionTemplate(implicitConversion) |
| 196 | + .getTemplate() |
| 197 | + .getName() + "'." |
| 198 | + ) |
| 199 | +} |
| 200 | + |
| 201 | +from |
| 202 | + ImplicitConversionLocation implicitConversionLocation, |
| 203 | + ImplicitConversionFromPlainCharType implicitConversion |
20 | 204 | where
|
21 |
| - not isExcluded(v, |
| 205 | + not isExcluded(implicitConversionLocation.asElement(), |
22 | 206 | StringsPackage::signedCharAndUnsignedCharTypeShallOnlyBeUsedForTheStorageAndUseOfNumericValuesQuery()) and
|
23 |
| - // We find cases where it is an explicitly signed char type with an assignment |
24 |
| - // to a non-numeric type. NOTE: This rule addresses cases where the char type |
25 |
| - // is used character data only, the rule does not explicitly cover this. |
26 |
| - // Please see M5-0-11 for explicit handling of this case. Get types that are |
27 |
| - // char, except for ones that are 'plain', meaning the sign is explicit. |
28 |
| - ( |
29 |
| - v.getUnspecifiedType() instanceof SignedCharType or |
30 |
| - v.getUnspecifiedType() instanceof UnsignedCharType |
31 |
| - ) and |
32 |
| - // Identify places where these explicitly signed types are being assigned to a |
33 |
| - // non-numeric type. |
34 |
| - aexp = v.getAnAssignedValue() and |
35 |
| - aexp.getUnspecifiedType() instanceof CharType |
36 |
| -select aexp, |
37 |
| - "Assignment of an non-integer type to variable $@ which is a variable with an explicitly signed char type", |
38 |
| - v, v.getName() |
| 207 | + implicitConversion = implicitConversionLocation.getImplicitConversion() |
| 208 | +select implicitConversionLocation.asElement(), getMessageTemplate(implicitConversionLocation), |
| 209 | + implicitConversion.getExpr(), "expression" |
0 commit comments