Skip to content

Commit 4c23ad1

Browse files
dave-bartolomeoRobert Marsh
authored andcommitted
C++: Rename a few IR APIs
There are a few IR APIs that we've found to be confusingly named. This PR renames them to be more consistent within the IR and with the AST API: `Instruction.getFunction` -> `Instruction.getEnclosingFunction`: This was especially confusing when you'd call `FunctionAddressInstruction.getFunction` to get the function whose address was taken, and wound up with the enclosing function instead. `Instruction.getXXXOperand` -> `Instruction.getXXX`. Now that `Operand` is an exposed type, we want a way to get a specific `Operand` of an `Instruction`, but more often we want to get the definition instruction of that operand. Now, the pattern is that `getXXXOperand` returns the `Operand`, and `getXXX` is equivalent to `getXXXOperand().getDefinitionInstruction()`. `Operand.getInstruction` -> `Operand.getUseInstruction`: More consistent with the existing `Operand.getDefinitionInstruction` predicate.
1 parent 97c5b8e commit 4c23ad1

37 files changed

+329
-326
lines changed

cpp/ql/src/semmle/code/cpp/controlflow/IRGuards.qll

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ class IRGuardCondition extends Instruction {
276276
this.controlsBlock(controlled, testIsTrue)
277277
or
278278
exists (IRGuardCondition ne
279-
| this = ne.(LogicalNotInstruction).getOperand() and
279+
| this = ne.(LogicalNotInstruction).getUnary() and
280280
ne.controls(controlled, testIsTrue.booleanNot()))
281281
}
282282

@@ -359,7 +359,7 @@ private predicate is_condition(Instruction guard) {
359359
branch.getCondition() = guard
360360
)
361361
or
362-
exists(LogicalNotInstruction cond | is_condition(cond) and cond.getOperand() = guard)
362+
exists(LogicalNotInstruction cond | is_condition(cond) and cond.getUnary() = guard)
363363
}
364364

365365
/**
@@ -383,7 +383,7 @@ private predicate compares_eq(Instruction test, Operand left, Operand right, int
383383
or
384384
/* (x is true => (left == right + k)) => (!x is false => (left == right + k)) */
385385
exists(boolean isFalse | testIsTrue = isFalse.booleanNot() |
386-
compares_eq(test.(LogicalNotInstruction).getOperand(), left, right, k, areEqual, isFalse)
386+
compares_eq(test.(LogicalNotInstruction).getUnary(), left, right, k, areEqual, isFalse)
387387
)
388388
}
389389

@@ -421,7 +421,7 @@ private predicate compares_lt(Instruction test, Operand left, Operand right, int
421421
or
422422
/* (x is true => (left < right + k)) => (!x is false => (left < right + k)) */
423423
exists(boolean isFalse | testIsTrue = isFalse.booleanNot() |
424-
compares_lt(test.(LogicalNotInstruction).getOperand(), left, right, k, isLt, isFalse)
424+
compares_lt(test.(LogicalNotInstruction).getUnary(), left, right, k, isLt, isFalse)
425425
)
426426
}
427427

@@ -452,12 +452,12 @@ private predicate complex_lt(CompareInstruction cmp, Operand left, Operand right
452452
left < (right - x) + c => left < right + (c-x) */
453453
private predicate sub_lt(CompareInstruction cmp, Operand left, Operand right, int k, boolean isLt, boolean testIsTrue) {
454454
exists(SubInstruction lhs, int c, int x | compares_lt(cmp, lhs.getAUse(), right, c, isLt, testIsTrue) and
455-
left = lhs.getAnOperand().(LeftOperand) and x = int_value(lhs.getRightOperand())
455+
left = lhs.getAnOperand().(LeftOperand) and x = int_value(lhs.getRight())
456456
and k = c + x
457457
)
458458
or
459459
exists(SubInstruction rhs, int c, int x | compares_lt(cmp, left, rhs.getAUse(), c, isLt, testIsTrue) and
460-
right = rhs.getAnOperand().(LeftOperand) and x = int_value(rhs.getRightOperand())
460+
right = rhs.getAnOperand().(LeftOperand) and x = int_value(rhs.getRight())
461461
and k = c - x
462462
)
463463
}
@@ -466,17 +466,17 @@ private predicate sub_lt(CompareInstruction cmp, Operand left, Operand right, in
466466
left < (right + x) + c => left < right + (c+x) */
467467
private predicate add_lt(CompareInstruction cmp, Operand left, Operand right, int k, boolean isLt, boolean testIsTrue) {
468468
exists(AddInstruction lhs, int c, int x | compares_lt(cmp, lhs.getAUse(), right, c, isLt, testIsTrue) and
469-
(left = lhs.getAnOperand().(LeftOperand) and x = int_value(lhs.getRightOperand())
469+
(left = lhs.getAnOperand().(LeftOperand) and x = int_value(lhs.getRight())
470470
or
471-
left = lhs.getAnOperand().(RightOperand) and x = int_value(lhs.getLeftOperand())
471+
left = lhs.getAnOperand().(RightOperand) and x = int_value(lhs.getLeft())
472472
)
473473
and k = c - x
474474
)
475475
or
476476
exists(AddInstruction rhs, int c, int x | compares_lt(cmp, left, rhs.getAUse(), c, isLt, testIsTrue) and
477-
(right = rhs.getAnOperand().(LeftOperand) and x = int_value(rhs.getRightOperand())
477+
(right = rhs.getAnOperand().(LeftOperand) and x = int_value(rhs.getRight())
478478
or
479-
right = rhs.getAnOperand().(RightOperand) and x = int_value(rhs.getLeftOperand())
479+
right = rhs.getAnOperand().(RightOperand) and x = int_value(rhs.getLeft())
480480
)
481481
and k = c + x
482482
)
@@ -487,12 +487,12 @@ private predicate add_lt(CompareInstruction cmp, Operand left, Operand right, in
487487
left == (right - x) + c => left == right + (c-x) */
488488
private predicate sub_eq(CompareInstruction cmp, Operand left, Operand right, int k, boolean areEqual, boolean testIsTrue) {
489489
exists(SubInstruction lhs, int c, int x | compares_eq(cmp, lhs.getAUse(), right, c, areEqual, testIsTrue) and
490-
left = lhs.getAnOperand().(LeftOperand) and x = int_value(lhs.getRightOperand())
490+
left = lhs.getAnOperand().(LeftOperand) and x = int_value(lhs.getRight())
491491
and k = c + x
492492
)
493493
or
494494
exists(SubInstruction rhs, int c, int x | compares_eq(cmp, left, rhs.getAUse(), c, areEqual, testIsTrue) and
495-
right = rhs.getAnOperand().(LeftOperand) and x = int_value(rhs.getRightOperand())
495+
right = rhs.getAnOperand().(LeftOperand) and x = int_value(rhs.getRight())
496496
and k = c - x
497497
)
498498
}
@@ -502,17 +502,17 @@ private predicate sub_eq(CompareInstruction cmp, Operand left, Operand right, in
502502
left == (right + x) + c => left == right + (c+x) */
503503
private predicate add_eq(CompareInstruction cmp, Operand left, Operand right, int k, boolean areEqual, boolean testIsTrue) {
504504
exists(AddInstruction lhs, int c, int x | compares_eq(cmp, lhs.getAUse(), right, c, areEqual, testIsTrue) and
505-
(left = lhs.getAnOperand().(LeftOperand) and x = int_value(lhs.getRightOperand())
505+
(left = lhs.getAnOperand().(LeftOperand) and x = int_value(lhs.getRight())
506506
or
507-
left = lhs.getAnOperand().(RightOperand) and x = int_value(lhs.getLeftOperand())
507+
left = lhs.getAnOperand().(RightOperand) and x = int_value(lhs.getLeft())
508508
)
509509
and k = c - x
510510
)
511511
or
512512
exists(AddInstruction rhs, int c, int x | compares_eq(cmp, left, rhs.getAUse(), c, areEqual, testIsTrue) and
513-
(right = rhs.getAnOperand().(LeftOperand) and x = int_value(rhs.getRightOperand())
513+
(right = rhs.getAnOperand().(LeftOperand) and x = int_value(rhs.getRight())
514514
or
515-
right = rhs.getAnOperand().(RightOperand) and x = int_value(rhs.getLeftOperand())
515+
right = rhs.getAnOperand().(RightOperand) and x = int_value(rhs.getLeft())
516516
)
517517
and k = c + x
518518
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Node extends Instruction {
1616
* INTERNAL: Do not use. Alternative name for `getFunction`.
1717
*/
1818
Function getEnclosingCallable() {
19-
result = this.getFunction()
19+
result = this.getEnclosingFunction()
2020
}
2121

2222
/** Gets the type of this node. */
@@ -155,7 +155,7 @@ predicate localFlowStep(Node nodeFrom, Node nodeTo) {
155155
nodeTo.(CopyInstruction).getSourceValue() = nodeFrom or
156156
nodeTo.(PhiInstruction).getAnOperand().getDefinitionInstruction() = nodeFrom or
157157
// Treat all conversions as flow, even conversions between different numeric types.
158-
nodeTo.(ConvertInstruction).getOperand() = nodeFrom
158+
nodeTo.(ConvertInstruction).getUnary() = nodeFrom
159159
}
160160

161161
/**

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,33 @@ class FunctionIR extends TFunctionIR {
4040
*/
4141
pragma[noinline]
4242
final EnterFunctionInstruction getEnterFunctionInstruction() {
43-
result.getFunctionIR() = this
43+
result.getEnclosingFunctionIR() = this
4444
}
4545

4646
/**
4747
* Gets the exit point for this function.
4848
*/
4949
pragma[noinline]
5050
final ExitFunctionInstruction getExitFunctionInstruction() {
51-
result.getFunctionIR() = this
51+
result.getEnclosingFunctionIR() = this
5252
}
5353

5454
pragma[noinline]
5555
final UnmodeledDefinitionInstruction getUnmodeledDefinitionInstruction() {
56-
result.getFunctionIR() = this
56+
result.getEnclosingFunctionIR() = this
5757
}
5858

5959
pragma[noinline]
6060
final UnmodeledUseInstruction getUnmodeledUseInstruction() {
61-
result.getFunctionIR() = this
61+
result.getEnclosingFunctionIR() = this
6262
}
6363

6464
/**
6565
* Gets the single return instruction for this function.
6666
*/
6767
pragma[noinline]
6868
final ReturnInstruction getReturnInstruction() {
69-
result.getFunctionIR() = this
69+
result.getEnclosingFunctionIR() = this
7070
}
7171

7272
/**
@@ -75,7 +75,7 @@ class FunctionIR extends TFunctionIR {
7575
*/
7676
pragma[noinline]
7777
final IRReturnVariable getReturnVariable() {
78-
result.getFunctionIR() = this
78+
result.getEnclosingFunctionIR() = this
7979
}
8080

8181
/**
@@ -90,13 +90,13 @@ class FunctionIR extends TFunctionIR {
9090
* Gets all instructions in this function.
9191
*/
9292
final Instruction getAnInstruction() {
93-
result.getFunctionIR() = this
93+
result.getEnclosingFunctionIR() = this
9494
}
9595

9696
/**
9797
* Gets all blocks in this function.
9898
*/
9999
final IRBlock getABlock() {
100-
result.getFunctionIR() = this
100+
result.getEnclosingFunctionIR() = this
101101
}
102102
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class IRBlockBase extends TIRBlock {
3333
*/
3434
int getDisplayIndex() {
3535
this = rank[result + 1](IRBlock funcBlock |
36-
funcBlock.getFunction() = getFunction() |
36+
funcBlock.getEnclosingFunction() = getEnclosingFunction() |
3737
funcBlock order by funcBlock.getUniqueId()
3838
)
3939
}
@@ -63,12 +63,12 @@ class IRBlockBase extends TIRBlock {
6363
result = strictcount(getInstruction(_))
6464
}
6565

66-
final FunctionIR getFunctionIR() {
67-
result = getFirstInstruction(this).getFunctionIR()
66+
final FunctionIR getEnclosingFunctionIR() {
67+
result = getFirstInstruction(this).getEnclosingFunctionIR()
6868
}
6969

70-
final Function getFunction() {
71-
result = getFirstInstruction(this).getFunction()
70+
final Function getEnclosingFunction() {
71+
result = getFirstInstruction(this).getEnclosingFunction()
7272
}
7373
}
7474

@@ -116,7 +116,7 @@ class IRBlock extends IRBlockBase {
116116
* Holds if this block is reachable from the entry point of its function
117117
*/
118118
final predicate isReachableFromFunctionEntry() {
119-
this = getFunctionIR().getEntryBlock() or
119+
this = getEnclosingFunctionIR().getEntryBlock() or
120120
getAPredecessor().isReachableFromFunctionEntry()
121121
}
122122
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ private import semmle.code.cpp.ir.internal.TIRVariable
77

88
IRUserVariable getIRUserVariable(Function func, Variable var) {
99
result.getVariable() = var and
10-
result.getFunction() = func
10+
result.getEnclosingFunction() = func
1111
}
1212

1313
/**
@@ -47,14 +47,14 @@ abstract class IRVariable extends TIRVariable {
4747
/**
4848
* Gets the IR for the function that references this variable.
4949
*/
50-
final FunctionIR getFunctionIR() {
50+
final FunctionIR getEnclosingFunctionIR() {
5151
result.getFunction() = func
5252
}
5353

5454
/**
5555
* Gets the function that references this variable.
5656
*/
57-
final Function getFunction() {
57+
final Function getEnclosingFunction() {
5858
result = func
5959
}
6060
}

0 commit comments

Comments
 (0)