Skip to content

Commit c708ed1

Browse files
author
Dave Bartolomeo
committed
C++: Remove some usage of Instruction.getResultType()
There were a few places in the IR itself where we use `Instruction.getResultType()`, which returns the C++ `Type` of the result, instead of `Instruction.getResultIRType()`, which returns the language-neutral `IRType` of the result. By removing this usage, we can avoid evaluating `getResultType()` at all. There are still other uses of `Instruction.getResultType()` in other libraries. We should switch those as well.
1 parent 1181848 commit c708ed1

8 files changed

Lines changed: 70 additions & 31 deletions

File tree

cpp/ql/src/Likely Bugs/RedundantNullCheckSimple.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import semmle.code.cpp.ir.ValueNumbering
2323
class NullInstruction extends ConstantValueInstruction {
2424
NullInstruction() {
2525
this.getValue() = "0" and
26-
this.getResultType().getUnspecifiedType() instanceof PointerType
26+
this.getResultIRType() instanceof IRAddressType
2727
}
2828
}
2929

@@ -44,8 +44,8 @@ predicate explicitNullTestOfInstruction(Instruction checked, Instruction bool) {
4444
bool =
4545
any(ConvertInstruction convert |
4646
checked = convert.getUnary() and
47-
convert.getResultType() instanceof BoolType and
48-
checked.getResultType() instanceof PointerType
47+
convert.getResultIRType() instanceof IRBooleanType and
48+
checked.getResultIRType() instanceof IRAddressType
4949
)
5050
}
5151

cpp/ql/src/semmle/code/cpp/ir/implementation/IRType.qll

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class IRBooleanType extends IRSizedType, TIRBooleanType {
128128
}
129129

130130
/**
131-
* A numberic type. This includes `IRSignedIntegerType`, `IRUnsignedIntegerType`, and
131+
* A numeric type. This includes `IRSignedIntegerType`, `IRUnsignedIntegerType`, and
132132
* `IRFloatingPointType`.
133133
*/
134134
class IRNumericType extends IRSizedType {
@@ -139,34 +139,41 @@ class IRNumericType extends IRSizedType {
139139
}
140140
}
141141

142+
/**
143+
* An integer type. This includes `IRSignedIntegerType` and `IRUnsignedIntegerType`.
144+
*/
145+
class IRIntegerType extends IRNumericType {
146+
IRIntegerType() {
147+
this = TIRSignedIntegerType(byteSize) or
148+
this = TIRUnsignedIntegerType(byteSize)
149+
}
150+
151+
pragma[noinline]
152+
final override int getByteSize() { result = byteSize }
153+
}
154+
142155
/**
143156
* A signed two's-complement integer. Also used to represent enums whose underlying type is a signed
144157
* integer, as well as character types whose representation is signed.
145158
*/
146-
class IRSignedIntegerType extends IRNumericType, TIRSignedIntegerType {
159+
class IRSignedIntegerType extends IRIntegerType, TIRSignedIntegerType {
147160
final override string toString() { result = "int" + byteSize.toString() }
148161

149162
final override Language::LanguageType getCanonicalLanguageType() {
150163
result = Language::getCanonicalSignedIntegerType(byteSize)
151164
}
152-
153-
pragma[noinline]
154-
final override int getByteSize() { result = byteSize }
155165
}
156166

157167
/**
158168
* An unsigned two's-complement integer. Also used to represent enums whose underlying type is an
159169
* unsigned integer, as well as character types whose representation is unsigned.
160170
*/
161-
class IRUnsignedIntegerType extends IRNumericType, TIRUnsignedIntegerType {
171+
class IRUnsignedIntegerType extends IRIntegerType, TIRUnsignedIntegerType {
162172
final override string toString() { result = "uint" + byteSize.toString() }
163173

164174
final override Language::LanguageType getCanonicalLanguageType() {
165175
result = Language::getCanonicalUnsignedIntegerType(byteSize)
166176
}
167-
168-
pragma[noinline]
169-
final override int getByteSize() { result = byteSize }
170177
}
171178

172179
/**

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,16 @@ class ConstantInstruction extends ConstantValueInstruction {
604604
}
605605

606606
class IntegerConstantInstruction extends ConstantInstruction {
607-
IntegerConstantInstruction() { getResultType() instanceof Language::IntegralType }
607+
IntegerConstantInstruction() {
608+
exists(IRType resultType |
609+
resultType = getResultIRType() and
610+
(resultType instanceof IRIntegerType or resultType instanceof IRBooleanType)
611+
)
612+
}
608613
}
609614

610615
class FloatConstantInstruction extends ConstantInstruction {
611-
FloatConstantInstruction() { getResultType() instanceof Language::FloatingPointType }
616+
FloatConstantInstruction() { getResultIRType() instanceof IRFloatingPointType }
612617
}
613618

614619
class StringConstantInstruction extends VariableInstruction {

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,16 @@ class ConstantInstruction extends ConstantValueInstruction {
604604
}
605605

606606
class IntegerConstantInstruction extends ConstantInstruction {
607-
IntegerConstantInstruction() { getResultType() instanceof Language::IntegralType }
607+
IntegerConstantInstruction() {
608+
exists(IRType resultType |
609+
resultType = getResultIRType() and
610+
(resultType instanceof IRIntegerType or resultType instanceof IRBooleanType)
611+
)
612+
}
608613
}
609614

610615
class FloatConstantInstruction extends ConstantInstruction {
611-
FloatConstantInstruction() { getResultType() instanceof Language::FloatingPointType }
616+
FloatConstantInstruction() { getResultIRType() instanceof IRFloatingPointType }
612617
}
613618

614619
class StringConstantInstruction extends VariableInstruction {

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,16 @@ class ConstantInstruction extends ConstantValueInstruction {
604604
}
605605

606606
class IntegerConstantInstruction extends ConstantInstruction {
607-
IntegerConstantInstruction() { getResultType() instanceof Language::IntegralType }
607+
IntegerConstantInstruction() {
608+
exists(IRType resultType |
609+
resultType = getResultIRType() and
610+
(resultType instanceof IRIntegerType or resultType instanceof IRBooleanType)
611+
)
612+
}
608613
}
609614

610615
class FloatConstantInstruction extends ConstantInstruction {
611-
FloatConstantInstruction() { getResultType() instanceof Language::FloatingPointType }
616+
FloatConstantInstruction() { getResultIRType() instanceof IRFloatingPointType }
612617
}
613618

614619
class StringConstantInstruction extends VariableInstruction {

csharp/ql/src/semmle/code/csharp/ir/implementation/IRType.qll

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class IRBooleanType extends IRSizedType, TIRBooleanType {
128128
}
129129

130130
/**
131-
* A numberic type. This includes `IRSignedIntegerType`, `IRUnsignedIntegerType`, and
131+
* A numeric type. This includes `IRSignedIntegerType`, `IRUnsignedIntegerType`, and
132132
* `IRFloatingPointType`.
133133
*/
134134
class IRNumericType extends IRSizedType {
@@ -139,34 +139,41 @@ class IRNumericType extends IRSizedType {
139139
}
140140
}
141141

142+
/**
143+
* An integer type. This includes `IRSignedIntegerType` and `IRUnsignedIntegerType`.
144+
*/
145+
class IRIntegerType extends IRNumericType {
146+
IRIntegerType() {
147+
this = TIRSignedIntegerType(byteSize) or
148+
this = TIRUnsignedIntegerType(byteSize)
149+
}
150+
151+
pragma[noinline]
152+
final override int getByteSize() { result = byteSize }
153+
}
154+
142155
/**
143156
* A signed two's-complement integer. Also used to represent enums whose underlying type is a signed
144157
* integer, as well as character types whose representation is signed.
145158
*/
146-
class IRSignedIntegerType extends IRNumericType, TIRSignedIntegerType {
159+
class IRSignedIntegerType extends IRIntegerType, TIRSignedIntegerType {
147160
final override string toString() { result = "int" + byteSize.toString() }
148161

149162
final override Language::LanguageType getCanonicalLanguageType() {
150163
result = Language::getCanonicalSignedIntegerType(byteSize)
151164
}
152-
153-
pragma[noinline]
154-
final override int getByteSize() { result = byteSize }
155165
}
156166

157167
/**
158168
* An unsigned two's-complement integer. Also used to represent enums whose underlying type is an
159169
* unsigned integer, as well as character types whose representation is unsigned.
160170
*/
161-
class IRUnsignedIntegerType extends IRNumericType, TIRUnsignedIntegerType {
171+
class IRUnsignedIntegerType extends IRIntegerType, TIRUnsignedIntegerType {
162172
final override string toString() { result = "uint" + byteSize.toString() }
163173

164174
final override Language::LanguageType getCanonicalLanguageType() {
165175
result = Language::getCanonicalUnsignedIntegerType(byteSize)
166176
}
167-
168-
pragma[noinline]
169-
final override int getByteSize() { result = byteSize }
170177
}
171178

172179
/**

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/Instruction.qll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,16 @@ class ConstantInstruction extends ConstantValueInstruction {
604604
}
605605

606606
class IntegerConstantInstruction extends ConstantInstruction {
607-
IntegerConstantInstruction() { getResultType() instanceof Language::IntegralType }
607+
IntegerConstantInstruction() {
608+
exists(IRType resultType |
609+
resultType = getResultIRType() and
610+
(resultType instanceof IRIntegerType or resultType instanceof IRBooleanType)
611+
)
612+
}
608613
}
609614

610615
class FloatConstantInstruction extends ConstantInstruction {
611-
FloatConstantInstruction() { getResultType() instanceof Language::FloatingPointType }
616+
FloatConstantInstruction() { getResultIRType() instanceof IRFloatingPointType }
612617
}
613618

614619
class StringConstantInstruction extends VariableInstruction {

csharp/ql/src/semmle/code/csharp/ir/implementation/unaliased_ssa/Instruction.qll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,16 @@ class ConstantInstruction extends ConstantValueInstruction {
604604
}
605605

606606
class IntegerConstantInstruction extends ConstantInstruction {
607-
IntegerConstantInstruction() { getResultType() instanceof Language::IntegralType }
607+
IntegerConstantInstruction() {
608+
exists(IRType resultType |
609+
resultType = getResultIRType() and
610+
(resultType instanceof IRIntegerType or resultType instanceof IRBooleanType)
611+
)
612+
}
608613
}
609614

610615
class FloatConstantInstruction extends ConstantInstruction {
611-
FloatConstantInstruction() { getResultType() instanceof Language::FloatingPointType }
616+
FloatConstantInstruction() { getResultIRType() instanceof IRFloatingPointType }
612617
}
613618

614619
class StringConstantInstruction extends VariableInstruction {

0 commit comments

Comments
 (0)