Skip to content

Commit bcf8ae9

Browse files
committed
BuiltInTypeRules: Rename getRealSize to getBuiltInSize
1 parent 8be7b3c commit bcf8ae9

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

cpp/misra/src/codingstandards/cpp/misra/BuiltInTypeRules.qll

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,36 @@ newtype Signedness =
8585
Signed() or
8686
Unsigned()
8787

88-
class CharacterType extends Type {
89-
// The actual character type, which is either a plain char or a wide char
88+
/**
89+
* A `Type` which is considered to be a built-in type by MISRA.
90+
*
91+
* It differs from `BuiltInType` in that includes:
92+
* - Built in types with specifiers (e.g., `const`, `volatile`, `restrict`).
93+
* - Typedefs to built in types
94+
* - References to built in types
95+
* - Enum types with an explicit underlying type that is a built-in type.
96+
*/
97+
class MisraBuiltInType extends Type {
98+
// The built in type underlying this MISRA built in type
9099
BuiltInType builtInType;
91100

92-
CharacterType() {
93-
// A type whose type category is character
94-
getBuiltInTypeCategory(builtInType) = Character() and
95-
builtInType = getBuiltInType(this)
96-
}
101+
MisraBuiltInType() { builtInType = getBuiltInType(this) }
97102

98103
private BuiltInType getBuiltInType() { result = builtInType }
99104

100-
predicate isSameType(CharacterType other) { this.getBuiltInType() = other.getBuiltInType() }
105+
/** Gets the size of the underlying built in type. */
106+
int getBuiltInSize() { result = builtInType.getSize() }
107+
108+
TypeCategory getTypeCategory() { result = getBuiltInTypeCategory(builtInType) }
109+
110+
predicate isSameType(MisraBuiltInType other) { this.getBuiltInType() = other.getBuiltInType() }
111+
}
112+
113+
class CharacterType extends MisraBuiltInType {
114+
CharacterType() {
115+
// A type whose type category is character
116+
getBuiltInTypeCategory(builtInType) = Character()
117+
}
101118
}
102119

103120
/**
@@ -108,25 +125,16 @@ class CharacterType extends Type {
108125
* - Typedef'd types that are numeric types.
109126
* - Numeric types with specifiers (e.g., `const`, `volatile`, `restrict`).
110127
*/
111-
class NumericType extends Type {
112-
// The actual numeric type, which is either an integral or a floating-point type.
113-
BuiltInType builtInType;
114-
128+
class NumericType extends MisraBuiltInType {
115129
NumericType() {
116130
// A type whose type category is either integral or a floating-point
117-
getBuiltInTypeCategory(builtInType) = [Integral().(TypeCategory), FloatingPoint()] and
118-
builtInType = getBuiltInType(this)
131+
getBuiltInTypeCategory(builtInType) = [Integral().(TypeCategory), FloatingPoint()]
119132
}
120133

121134
Signedness getSignedness() {
122135
if builtInType.(IntegralType).isUnsigned() then result = Unsigned() else result = Signed()
123136
}
124137

125-
/** Gets the size of the actual numeric type. */
126-
int getRealSize() { result = builtInType.getSize() }
127-
128-
TypeCategory getTypeCategory() { result = getBuiltInTypeCategory(builtInType) }
129-
130138
/**
131139
* Gets the integeral upper bound of the numeric type, if it represents an integer type.
132140
*/
@@ -136,10 +144,6 @@ class NumericType extends Type {
136144
* Gets the integeral lower bound of the numeric type, if it represents an integer type.
137145
*/
138146
QlBuiltins::BigInt getIntegralLowerBound() { integralTypeBounds(builtInType, result, _) }
139-
140-
private BuiltInType getBuiltInType() { result = builtInType }
141-
142-
predicate isSameType(NumericType other) { this.getBuiltInType() = other.getBuiltInType() }
143147
}
144148

145149
predicate isSignedType(NumericType t) { t.getSignedness() = Signed() }
@@ -300,7 +304,7 @@ CanonicalIntegerNumericType getBitFieldType(BitField bf) {
300304
other.getSize() * 8 >= bf.getNumBits() and
301305
other.getSignedness() = result.getSignedness()
302306
|
303-
other.getSize() < result.getRealSize()
307+
other.getSize() < result.getBuiltInSize()
304308
)
305309
)
306310
}

cpp/misra/src/rules/RULE-7-0-5/NoSignednessChangeFromPromotion.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ class IntegerPromotion extends IntegerPromotionOrUsualArithmeticConversionAsCast
8989
// Only consider cases where the integer promotion is the last conversion applied
9090
exists(Expr e | e.getFullyConverted() = this) and
9191
// Integer promotion occurs where the from type is smaller than int
92-
fromType.getRealSize() < sizeOfInt() and
92+
fromType.getBuiltInSize() < sizeOfInt() and
9393
// To type is bigger than or equal to int
94-
toType.getRealSize() >= sizeOfInt() and
94+
toType.getBuiltInSize() >= sizeOfInt() and
9595
// An integer promotion is a conversion from an integral type to an integral type
9696
//
9797
// This deliberately excludes integer promotions from `bool` and unscoped enums which do not
@@ -152,7 +152,7 @@ class ImpliedIntegerPromotion extends IntegerPromotionOrUsualArithmeticConversio
152152
fromType = this.getType() and
153153
fromType.getTypeCategory() = Integral() and
154154
// If the size is less than int, then it is an implied integer promotion
155-
fromType.getRealSize() < sizeOfInt()
155+
fromType.getBuiltInSize() < sizeOfInt()
156156
}
157157

158158
override NumericType getFromType() { result = fromType }

cpp/misra/src/rules/RULE-7-0-6/NumericAssignmentTypeMismatch.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ predicate isValidTypeMatch(NumericType sourceType, NumericType targetType) {
5757
// Same type category, signedness and size
5858
sourceType.getTypeCategory() = targetType.getTypeCategory() and
5959
sourceType.getSignedness() = targetType.getSignedness() and
60-
sourceType.getRealSize() = targetType.getRealSize()
60+
sourceType.getBuiltInSize() = targetType.getBuiltInSize()
6161
}
6262

6363
/**
@@ -118,7 +118,7 @@ predicate isValidWidening(Expr source, NumericType sourceType, NumericType targe
118118
) and
119119
sourceType.getTypeCategory() = targetType.getTypeCategory() and
120120
sourceType.getSignedness() = targetType.getSignedness() and
121-
sourceType.getRealSize() < targetType.getRealSize()
121+
sourceType.getBuiltInSize() < targetType.getBuiltInSize()
122122
}
123123

124124
/**

0 commit comments

Comments
 (0)