Skip to content

Commit 416431a

Browse files
committed
C++: Add convenience predicates for working with qualifiers as parameters.
1 parent 056b0c2 commit 416431a

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,16 @@ class InitializeParameterInstruction extends VariableInstruction {
588588
* Gets the parameter initialized by this instruction.
589589
*/
590590
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
591+
592+
/**
593+
* Holds if this instruction initializes the parameter with index `index`, or
594+
* if `index` is `-1` and this instruction initializes `this`.
595+
*/
596+
final predicate isParameterOrQualifierIndex(int index) {
597+
index >= 0 and index = this.getParameter().getIndex()
598+
or
599+
index = -1 and this.getIRVariable() instanceof IRThisVariable
600+
}
591601
}
592602

593603
/**
@@ -601,6 +611,17 @@ class InitializeIndirectionInstruction extends VariableInstruction {
601611
* Gets the parameter initialized by this instruction.
602612
*/
603613
final Language::Parameter getParameter() { result = var.(IRUserVariable).getVariable() }
614+
615+
/**
616+
* Holds if this instruction initializes the memory pointed to by the parameter with
617+
* index `index`, or if `index` is `-1` and this instruction initializes the memory
618+
* pointed to by `this`.
619+
*/
620+
final predicate isParameterOrQualifierIndex(int index) {
621+
index >= 0 and index = this.getParameter().getIndex()
622+
or
623+
index = -1 and this.getIRVariable() instanceof IRThisVariable
624+
}
604625
}
605626

606627
/**
@@ -775,6 +796,16 @@ class ReturnIndirectionInstruction extends VariableInstruction {
775796
* Holds if this instruction is the return indirection for `this`.
776797
*/
777798
final predicate isThisIndirection() { var instanceof IRThisVariable }
799+
800+
/**
801+
* Holds if this instruction is the return indirection for the parameter with index `index`, or
802+
* if this instruction is the return indirection for `this` and `index` is `-1`.
803+
*/
804+
final predicate isParameterOrThisIndirection(int index) {
805+
index >= 0 and index = this.getParameter().getIndex()
806+
or
807+
index = -1 and this.isThisIndirection()
808+
}
778809
}
779810

780811
/**
@@ -1587,6 +1618,24 @@ class CallInstruction extends Instruction {
15871618
result = getPositionalArgumentOperand(index).getDef()
15881619
}
15891620

1621+
/**
1622+
* Gets the argument operand at the specified index, or `this` if `index` is `-1`.
1623+
*/
1624+
pragma[noinline]
1625+
final ArgumentOperand getPositionalOrThisArgumentOperand(int index) {
1626+
index >= 0 and result = getPositionalArgumentOperand(index)
1627+
or
1628+
index = -1 and result = getThisArgumentOperand()
1629+
}
1630+
1631+
/**
1632+
* Gets the argument at the specified index, or `this` if `index` is `-1`.
1633+
*/
1634+
pragma[noinline]
1635+
final Instruction getPositionalOrThisArgument(int index) {
1636+
result = getPositionalOrThisArgumentOperand(index).getDef()
1637+
}
1638+
15901639
/**
15911640
* Gets the number of arguments of the call, including the `this` pointer, if any.
15921641
*/

0 commit comments

Comments
 (0)