Skip to content

Commit a5f6806

Browse files
authored
Merge pull request #21105 from jketema/load-this
C++: Use the loaded `this` instead of the `this` initialization
2 parents 6a3c74c + c567197 commit a5f6806

File tree

8 files changed

+151
-155
lines changed

8 files changed

+151
-155
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaImplCommon.qll

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -688,15 +688,9 @@ private module Cached {
688688
conversionFlow(mid, instr, false, _)
689689
)
690690
or
691-
exists(int ind0 |
692-
exists(Operand address |
693-
isDereference(operand.getDef(), address, _) and
694-
isUseImpl(address, base, ind0)
695-
)
696-
or
697-
isUseImpl(operand.getDef().(InitializeParameterInstruction).getAnOperand(), base, ind0)
698-
|
699-
ind0 = ind - 1
691+
exists(Operand address |
692+
isDereference(operand.getDef(), address, _) and
693+
isUseImpl(address, base, ind - 1)
700694
)
701695
}
702696

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedExpr.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2679,7 +2679,7 @@ class TranslatedDestructorFieldDestruction extends TranslatedNonConstantExpr, St
26792679
final override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {
26802680
tag = OnlyInstructionTag() and
26812681
operandTag instanceof UnaryOperandTag and
2682-
result = getTranslatedFunction(getEnclosingFunction(expr)).getInitializeThisInstruction()
2682+
result = getTranslatedFunction(getEnclosingFunction(expr)).getLoadThisInstruction()
26832683
}
26842684

26852685
final override Field getInstructionField(InstructionTag tag) {

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedFunction.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,11 @@ class TranslatedFunction extends TranslatedRootElement, TTranslatedFunction {
306306
final predicate hasReturnValue() { hasReturnValue(func) }
307307

308308
/**
309-
* Gets the single `InitializeThis` instruction for this function. Holds only
310-
* if the function is an instance member function, constructor, or destructor.
309+
* Gets the first load of `this` for this function. Holds only if the function
310+
* is an instance member function, constructor, or destructor.
311311
*/
312-
final Instruction getInitializeThisInstruction() {
313-
result = getTranslatedThisParameter(func).getInstruction(InitializerStoreTag())
312+
final Instruction getLoadThisInstruction() {
313+
result = getTranslatedThisParameter(func).getInstruction(InitializerIndirectAddressTag())
314314
}
315315

316316
/**
@@ -639,7 +639,7 @@ class TranslatedConstructorInitList extends TranslatedElement, InitializationCon
639639
}
640640

641641
override Instruction getTargetAddress() {
642-
result = getTranslatedFunction(func).getInitializeThisInstruction()
642+
result = getTranslatedFunction(func).getLoadThisInstruction()
643643
}
644644

645645
override Type getTargetType() { result = getTranslatedFunction(func).getThisType() }

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedInitialization.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ abstract class TranslatedBaseStructorCall extends TranslatedStructorCallFromStru
950950
final override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {
951951
tag = OnlyInstructionTag() and
952952
operandTag instanceof UnaryOperandTag and
953-
result = getTranslatedFunction(this.getFunction()).getInitializeThisInstruction()
953+
result = getTranslatedFunction(this.getFunction()).getLoadThisInstruction()
954954
}
955955

956956
final override predicate getInstructionInheritance(
@@ -1000,7 +1000,7 @@ class TranslatedConstructorDelegationInit extends TranslatedConstructorCallFromC
10001000
}
10011001

10021002
final override Instruction getReceiver() {
1003-
result = getTranslatedFunction(this.getFunction()).getInitializeThisInstruction()
1003+
result = getTranslatedFunction(this.getFunction()).getLoadThisInstruction()
10041004
}
10051005
}
10061006

cpp/ql/test/library-tests/dataflow/fields/ir-path-flow.expected

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ edges
193193
| C.cpp:18:12:18:18 | call to C [s3] | C.cpp:18:12:18:18 | *new [s3] | provenance | |
194194
| C.cpp:19:5:19:5 | *c [s1] | C.cpp:27:8:27:11 | *this [s1] | provenance | |
195195
| C.cpp:19:5:19:5 | *c [s3] | C.cpp:27:8:27:11 | *this [s3] | provenance | |
196+
| C.cpp:22:3:22:3 | *C [post update] [s1] | C.cpp:22:3:22:3 | *this [Return] [s1] | provenance | |
196197
| C.cpp:22:3:22:3 | *this [Return] [s1] | C.cpp:18:12:18:18 | call to C [s1] | provenance | |
197198
| C.cpp:22:3:22:3 | *this [Return] [s3] | C.cpp:18:12:18:18 | call to C [s3] | provenance | |
198-
| C.cpp:22:3:22:3 | *this [post update] [s1] | C.cpp:22:3:22:3 | *this [Return] [s1] | provenance | |
199-
| C.cpp:22:12:22:21 | new | C.cpp:22:3:22:3 | *this [post update] [s1] | provenance | |
199+
| C.cpp:22:12:22:21 | new | C.cpp:22:3:22:3 | *C [post update] [s1] | provenance | |
200200
| C.cpp:22:12:22:21 | new | C.cpp:22:12:22:21 | new | provenance | |
201201
| C.cpp:24:5:24:8 | *this [post update] [s3] | C.cpp:22:3:22:3 | *this [Return] [s3] | provenance | |
202202
| C.cpp:24:5:24:25 | ... = ... | C.cpp:24:5:24:8 | *this [post update] [s3] | provenance | |
@@ -736,12 +736,12 @@ edges
736736
| constructors.cpp:19:22:19:23 | *this [b_] | constructors.cpp:19:22:19:23 | b_ | provenance | |
737737
| constructors.cpp:19:22:19:23 | b_ | constructors.cpp:19:9:19:9 | *b | provenance | |
738738
| constructors.cpp:19:22:19:23 | b_ | constructors.cpp:19:22:19:23 | b_ | provenance | |
739-
| constructors.cpp:23:5:23:7 | *this [post update] [a_] | constructors.cpp:23:5:23:7 | *this [Return] [a_] | provenance | |
740-
| constructors.cpp:23:5:23:7 | *this [post update] [b_] | constructors.cpp:23:5:23:7 | *this [Return] [b_] | provenance | |
739+
| constructors.cpp:23:5:23:7 | *Foo [post update] [a_] | constructors.cpp:23:5:23:7 | *this [Return] [a_] | provenance | |
740+
| constructors.cpp:23:5:23:7 | *Foo [post update] [b_] | constructors.cpp:23:5:23:7 | *this [Return] [b_] | provenance | |
741741
| constructors.cpp:23:13:23:13 | a | constructors.cpp:23:28:23:28 | a | provenance | |
742742
| constructors.cpp:23:20:23:20 | b | constructors.cpp:23:35:23:35 | b | provenance | |
743-
| constructors.cpp:23:28:23:28 | a | constructors.cpp:23:5:23:7 | *this [post update] [a_] | provenance | |
744-
| constructors.cpp:23:35:23:35 | b | constructors.cpp:23:5:23:7 | *this [post update] [b_] | provenance | |
743+
| constructors.cpp:23:28:23:28 | a | constructors.cpp:23:5:23:7 | *Foo [post update] [a_] | provenance | |
744+
| constructors.cpp:23:35:23:35 | b | constructors.cpp:23:5:23:7 | *Foo [post update] [b_] | provenance | |
745745
| constructors.cpp:26:15:26:15 | *f [a_] | constructors.cpp:28:10:28:10 | *f [a_] | provenance | |
746746
| constructors.cpp:26:15:26:15 | *f [b_] | constructors.cpp:29:10:29:10 | *f [b_] | provenance | |
747747
| constructors.cpp:28:10:28:10 | *f [a_] | constructors.cpp:18:9:18:9 | *this [a_] | provenance | |
@@ -1122,9 +1122,9 @@ nodes
11221122
| C.cpp:18:12:18:18 | call to C [s3] | semmle.label | call to C [s3] |
11231123
| C.cpp:19:5:19:5 | *c [s1] | semmle.label | *c [s1] |
11241124
| C.cpp:19:5:19:5 | *c [s3] | semmle.label | *c [s3] |
1125+
| C.cpp:22:3:22:3 | *C [post update] [s1] | semmle.label | *C [post update] [s1] |
11251126
| C.cpp:22:3:22:3 | *this [Return] [s1] | semmle.label | *this [Return] [s1] |
11261127
| C.cpp:22:3:22:3 | *this [Return] [s3] | semmle.label | *this [Return] [s3] |
1127-
| C.cpp:22:3:22:3 | *this [post update] [s1] | semmle.label | *this [post update] [s1] |
11281128
| C.cpp:22:12:22:21 | new | semmle.label | new |
11291129
| C.cpp:22:12:22:21 | new | semmle.label | new |
11301130
| C.cpp:24:5:24:8 | *this [post update] [s3] | semmle.label | *this [post update] [s3] |
@@ -1678,10 +1678,10 @@ nodes
16781678
| constructors.cpp:19:22:19:23 | *this [b_] | semmle.label | *this [b_] |
16791679
| constructors.cpp:19:22:19:23 | b_ | semmle.label | b_ |
16801680
| constructors.cpp:19:22:19:23 | b_ | semmle.label | b_ |
1681+
| constructors.cpp:23:5:23:7 | *Foo [post update] [a_] | semmle.label | *Foo [post update] [a_] |
1682+
| constructors.cpp:23:5:23:7 | *Foo [post update] [b_] | semmle.label | *Foo [post update] [b_] |
16811683
| constructors.cpp:23:5:23:7 | *this [Return] [a_] | semmle.label | *this [Return] [a_] |
16821684
| constructors.cpp:23:5:23:7 | *this [Return] [b_] | semmle.label | *this [Return] [b_] |
1683-
| constructors.cpp:23:5:23:7 | *this [post update] [a_] | semmle.label | *this [post update] [a_] |
1684-
| constructors.cpp:23:5:23:7 | *this [post update] [b_] | semmle.label | *this [post update] [b_] |
16851685
| constructors.cpp:23:13:23:13 | a | semmle.label | a |
16861686
| constructors.cpp:23:20:23:20 | b | semmle.label | b |
16871687
| constructors.cpp:23:28:23:28 | a | semmle.label | a |

0 commit comments

Comments
 (0)