Skip to content

Commit 912c50a

Browse files
author
Dave Bartolomeo
authored
Merge pull request github#3937 from MathiasVP/replace-result-type-with-ir-result-type
C++: Replace getResultType() with getResultIRType()
2 parents 456a05e + 002f930 commit 912c50a

File tree

8 files changed

+55
-31
lines changed

8 files changed

+55
-31
lines changed

cpp/ql/src/experimental/semmle/code/cpp/rangeanalysis/Bound.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ private newtype TBound =
88
exists(Instruction i |
99
vn.getAnInstruction() = i and
1010
(
11-
i.getResultType() instanceof IntegralType or
12-
i.getResultType() instanceof PointerType
11+
i.getResultIRType() instanceof IRIntegerType or
12+
i.getResultIRType() instanceof IRAddressType
1313
) and
1414
not vn.getAnInstruction() instanceof ConstantInstruction
1515
|

cpp/ql/src/experimental/semmle/code/cpp/rangeanalysis/RangeAnalysis.qll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,14 @@ class CondReason extends Reason, TCondReason {
244244
/**
245245
* Holds if `typ` is a small integral type with the given lower and upper bounds.
246246
*/
247-
private predicate typeBound(IntegralType typ, int lowerbound, int upperbound) {
248-
typ.isSigned() and typ.getSize() = 1 and lowerbound = -128 and upperbound = 127
247+
private predicate typeBound(IRIntegerType typ, int lowerbound, int upperbound) {
248+
typ.isSigned() and typ.getByteSize() = 1 and lowerbound = -128 and upperbound = 127
249249
or
250-
typ.isUnsigned() and typ.getSize() = 1 and lowerbound = 0 and upperbound = 255
250+
typ.isUnsigned() and typ.getByteSize() = 1 and lowerbound = 0 and upperbound = 255
251251
or
252-
typ.isSigned() and typ.getSize() = 2 and lowerbound = -32768 and upperbound = 32767
252+
typ.isSigned() and typ.getByteSize() = 2 and lowerbound = -32768 and upperbound = 32767
253253
or
254-
typ.isUnsigned() and typ.getSize() = 2 and lowerbound = 0 and upperbound = 65535
254+
typ.isUnsigned() and typ.getByteSize() = 2 and lowerbound = 0 and upperbound = 65535
255255
}
256256

257257
/**
@@ -260,14 +260,14 @@ private predicate typeBound(IntegralType typ, int lowerbound, int upperbound) {
260260
private class NarrowingCastInstruction extends ConvertInstruction {
261261
NarrowingCastInstruction() {
262262
not this instanceof SafeCastInstruction and
263-
typeBound(getResultType(), _, _)
263+
typeBound(getResultIRType(), _, _)
264264
}
265265

266266
/** Gets the lower bound of the resulting type. */
267-
int getLowerBound() { typeBound(getResultType(), result, _) }
267+
int getLowerBound() { typeBound(getResultIRType(), result, _) }
268268

269269
/** Gets the upper bound of the resulting type. */
270-
int getUpperBound() { typeBound(getResultType(), _, result) }
270+
int getUpperBound() { typeBound(getResultIRType(), _, result) }
271271
}
272272

273273
/**

cpp/ql/src/experimental/semmle/code/cpp/rangeanalysis/RangeUtils.qll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ predicate backEdge(PhiInstruction phi, PhiInputOperand op) {
8686
* range analysis.
8787
*/
8888
pragma[inline]
89-
private predicate safeCast(IntegralType fromtyp, IntegralType totyp) {
90-
fromtyp.getSize() < totyp.getSize() and
89+
private predicate safeCast(IRIntegerType fromtyp, IRIntegerType totyp) {
90+
fromtyp.getByteSize() < totyp.getByteSize() and
9191
(
9292
fromtyp.isUnsigned()
9393
or
9494
totyp.isSigned()
9595
)
9696
or
97-
fromtyp.getSize() <= totyp.getSize() and
97+
fromtyp.getByteSize() <= totyp.getByteSize() and
9898
(
9999
fromtyp.isSigned() and
100100
totyp.isSigned()
@@ -109,8 +109,8 @@ private predicate safeCast(IntegralType fromtyp, IntegralType totyp) {
109109
*/
110110
class PtrToPtrCastInstruction extends ConvertInstruction {
111111
PtrToPtrCastInstruction() {
112-
getResultType() instanceof PointerType and
113-
getUnary().getResultType() instanceof PointerType
112+
getResultIRType() instanceof IRAddressType and
113+
getUnary().getResultIRType() instanceof IRAddressType
114114
}
115115
}
116116

@@ -119,7 +119,7 @@ class PtrToPtrCastInstruction extends ConvertInstruction {
119119
* that cannot overflow or underflow.
120120
*/
121121
class SafeIntCastInstruction extends ConvertInstruction {
122-
SafeIntCastInstruction() { safeCast(getUnary().getResultType(), getResultType()) }
122+
SafeIntCastInstruction() { safeCast(getUnary().getResultIRType(), getResultIRType()) }
123123
}
124124

125125
/**

cpp/ql/src/experimental/semmle/code/cpp/rangeanalysis/SignAnalysis.qll

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,17 +469,21 @@ module SignAnalysisCached {
469469
not exists(certainInstructionSign(i)) and
470470
not (
471471
result = TNeg() and
472-
i.getResultType().(IntegralType).isUnsigned()
472+
i.getResultIRType().(IRIntegerType).isUnsigned()
473473
) and
474474
(
475475
unknownSign(i)
476476
or
477477
exists(ConvertInstruction ci, Instruction prior, boolean fromSigned, boolean toSigned |
478478
i = ci and
479479
prior = ci.getUnary() and
480-
(if ci.getResultType().(IntegralType).isSigned() then toSigned = true else toSigned = false) and
481480
(
482-
if prior.getResultType().(IntegralType).isSigned()
481+
if ci.getResultIRType().(IRIntegerType).isSigned()
482+
then toSigned = true
483+
else toSigned = false
484+
) and
485+
(
486+
if prior.getResultIRType().(IRIntegerType).isSigned()
483487
then fromSigned = true
484488
else fromSigned = false
485489
) and
@@ -512,11 +516,11 @@ module SignAnalysisCached {
512516
i instanceof ShiftLeftInstruction and result = s1.lshift(s2)
513517
or
514518
i instanceof ShiftRightInstruction and
515-
i.getResultType().(IntegralType).isSigned() and
519+
i.getResultIRType().(IRIntegerType).isSigned() and
516520
result = s1.rshift(s2)
517521
or
518522
i instanceof ShiftRightInstruction and
519-
not i.getResultType().(IntegralType).isSigned() and
523+
not i.getResultIRType().(IRIntegerType).isSigned() and
520524
result = s1.urshift(s2)
521525
)
522526
or

cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,20 +234,20 @@ predicate clearsContent(Node n, Content c) {
234234
}
235235

236236
/** Gets the type of `n` used for type pruning. */
237-
Type getNodeType(Node n) {
237+
IRType getNodeType(Node n) {
238238
suppressUnusedNode(n) and
239-
result instanceof VoidType // stub implementation
239+
result instanceof IRVoidType // stub implementation
240240
}
241241

242242
/** Gets a string representation of a type returned by `getNodeType`. */
243-
string ppReprType(Type t) { none() } // stub implementation
243+
string ppReprType(IRType t) { none() } // stub implementation
244244

245245
/**
246246
* Holds if `t1` and `t2` are compatible, that is, whether data can flow from
247247
* a node of type `t1` to a node of type `t2`.
248248
*/
249249
pragma[inline]
250-
predicate compatibleTypes(Type t1, Type t2) {
250+
predicate compatibleTypes(IRType t1, IRType t2) {
251251
any() // stub implementation
252252
}
253253

@@ -271,7 +271,7 @@ class DataFlowCallable = Declaration;
271271

272272
class DataFlowExpr = Expr;
273273

274-
class DataFlowType = Type;
274+
class DataFlowType = IRType;
275275

276276
/** A function call relevant for data flow. */
277277
class DataFlowCall extends CallInstruction {

cpp/ql/src/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Node extends TIRDataFlowNode {
3333
Function getFunction() { none() } // overridden in subclasses
3434

3535
/** Gets the type of this node. */
36-
Type getType() { none() } // overridden in subclasses
36+
IRType getType() { none() } // overridden in subclasses
3737

3838
/** Gets the instruction corresponding to this node, if any. */
3939
Instruction asInstruction() { result = this.(InstructionNode).getInstruction() }
@@ -88,7 +88,7 @@ class Node extends TIRDataFlowNode {
8888
/**
8989
* Gets an upper bound on the type of this node.
9090
*/
91-
Type getTypeBound() { result = getType() }
91+
IRType getTypeBound() { result = getType() }
9292

9393
/** Gets the location of this element. */
9494
Location getLocation() { none() } // overridden by subclasses
@@ -125,7 +125,7 @@ class InstructionNode extends Node, TInstructionNode {
125125

126126
override Function getFunction() { result = instr.getEnclosingFunction() }
127127

128-
override Type getType() { result = instr.getResultType() }
128+
override IRType getType() { result = instr.getResultIRType() }
129129

130130
override Location getLocation() { result = instr.getLocation() }
131131

@@ -151,7 +151,7 @@ class OperandNode extends Node, TOperandNode {
151151

152152
override Function getFunction() { result = op.getUse().getEnclosingFunction() }
153153

154-
override Type getType() { result = op.getType() }
154+
override IRType getType() { result = op.getIRType() }
155155

156156
override Location getLocation() { result = op.getLocation() }
157157

@@ -449,7 +449,7 @@ class VariableNode extends Node, TVariableNode {
449449
result = v
450450
}
451451

452-
override Type getType() { result = v.getType() }
452+
override IRType getType() { result.getCanonicalLanguageType().hasUnspecifiedType(v.getType(), _) }
453453

454454
override Location getLocation() { result = v.getLocation() }
455455

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ class IRIntegerType extends IRNumericType {
152152
this = TIRSignedIntegerType(byteSize) or
153153
this = TIRUnsignedIntegerType(byteSize)
154154
}
155+
156+
/** Holds if this integer type is signed. */
157+
predicate isSigned() { none() }
158+
159+
/** Holds if this integer type is unsigned. */
160+
predicate isUnsigned() { none() }
155161
// Don't override `getByteSize()` here. The optimizer seems to generate better code when this is
156162
// overridden only in the leaf classes.
157163
}
@@ -169,6 +175,8 @@ class IRSignedIntegerType extends IRIntegerType, TIRSignedIntegerType {
169175

170176
pragma[noinline]
171177
final override int getByteSize() { result = byteSize }
178+
179+
override predicate isSigned() { any() }
172180
}
173181

174182
/**
@@ -184,6 +192,8 @@ class IRUnsignedIntegerType extends IRIntegerType, TIRUnsignedIntegerType {
184192

185193
pragma[noinline]
186194
final override int getByteSize() { result = byteSize }
195+
196+
override predicate isUnsigned() { any() }
187197
}
188198

189199
/**

csharp/ql/src/experimental/ir/implementation/IRType.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ class IRIntegerType extends IRNumericType {
152152
this = TIRSignedIntegerType(byteSize) or
153153
this = TIRUnsignedIntegerType(byteSize)
154154
}
155+
156+
/** Holds if this integer type is signed. */
157+
predicate isSigned() { none() }
158+
159+
/** Holds if this integer type is unsigned. */
160+
predicate isUnsigned() { none() }
155161
// Don't override `getByteSize()` here. The optimizer seems to generate better code when this is
156162
// overridden only in the leaf classes.
157163
}
@@ -169,6 +175,8 @@ class IRSignedIntegerType extends IRIntegerType, TIRSignedIntegerType {
169175

170176
pragma[noinline]
171177
final override int getByteSize() { result = byteSize }
178+
179+
override predicate isSigned() { any() }
172180
}
173181

174182
/**
@@ -184,6 +192,8 @@ class IRUnsignedIntegerType extends IRIntegerType, TIRUnsignedIntegerType {
184192

185193
pragma[noinline]
186194
final override int getByteSize() { result = byteSize }
195+
196+
override predicate isUnsigned() { any() }
187197
}
188198

189199
/**

0 commit comments

Comments
 (0)