Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a404ce

Browse files
author
Robert Marsh
committed
C++: Add getLastInstruction to IR generation
1 parent 47720e0 commit 3a404ce

9 files changed

Lines changed: 238 additions & 0 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ abstract class TranslatedCall extends TranslatedExpr {
4747
else result = this.getFirstCallTargetInstruction(kind)
4848
}
4949

50+
override Instruction getLastInstruction() { result = this.getSideEffects().getLastInstruction() }
51+
5052
override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
5153
tag = CallTag() and
5254
opcode instanceof Opcode::Call and
@@ -246,6 +248,15 @@ abstract class TranslatedSideEffects extends TranslatedElement {
246248
result = this.getParent().getChildSuccessor(this, kind)
247249
}
248250

251+
override Instruction getLastInstruction() {
252+
if exists(this.getAChild())
253+
then result = this.getChild(max(int i | exists(this.getChild(i)))).getLastInstruction()
254+
else
255+
// If there are no side effects, the "last" instruction should be the parent call's last
256+
// instruction, so that implicit destructors can be inserted in the right place.
257+
result = this.getParent().getInstruction(CallTag())
258+
}
259+
249260
final override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) { none() }
250261

251262
/** Gets the primary instruction to be associated with each side effect instruction. */
@@ -423,6 +434,8 @@ abstract class TranslatedSideEffect extends TranslatedElement {
423434
kind instanceof GotoEdge
424435
}
425436

437+
override Instruction getLastInstruction() { result = this.getInstruction(OnlyInstructionTag()) }
438+
426439
final override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType type) {
427440
tag = OnlyInstructionTag() and
428441
this.sideEffectInstruction(opcode, type)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ abstract class TranslatedFlexibleCondition extends TranslatedCondition, Conditio
5656
result = this.getOperand().getFirstInstruction(kind)
5757
}
5858

59+
final override Instruction getLastInstruction() {
60+
result = this.getOperand().getLastInstruction()
61+
}
62+
5963
final override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
6064
none()
6165
}
@@ -104,6 +108,12 @@ abstract class TranslatedBinaryLogicalOperation extends TranslatedNativeConditio
104108
result = this.getLeftOperand().getFirstInstruction(kind)
105109
}
106110

111+
final override Instruction getLastInstruction() {
112+
result = this.getLeftOperand().getLastInstruction()
113+
or
114+
result = this.getRightOperand().getLastInstruction()
115+
}
116+
107117
final override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
108118
none()
109119
}
@@ -162,6 +172,10 @@ class TranslatedValueCondition extends TranslatedCondition, TTranslatedValueCond
162172
result = this.getValueExpr().getFirstInstruction(kind)
163173
}
164174

175+
override Instruction getLastInstruction() {
176+
result = this.getInstruction(ValueConditionConditionalBranchTag())
177+
}
178+
165179
override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
166180
tag = ValueConditionConditionalBranchTag() and
167181
opcode instanceof Opcode::ConditionalBranch and

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ class TranslatedStaticLocalVariableDeclarationEntry extends TranslatedDeclaratio
156156
kind instanceof GotoEdge
157157
}
158158

159+
final override Instruction getLastInstruction() {
160+
result = this.getInstruction(DynamicInitializationConditionalBranchTag())
161+
or
162+
result = this.getInstruction(DynamicInitializationFlagStoreTag())
163+
}
164+
159165
final override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
160166
tag = DynamicInitializationFlagAddressTag() and
161167
kind instanceof GotoEdge and

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,8 @@ abstract class TranslatedElement extends TTranslatedElement {
904904
*/
905905
abstract Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind);
906906

907+
abstract Instruction getLastInstruction();
908+
907909
/**
908910
* Gets the successor instruction to which control should flow after the
909911
* child element specified by `child` has finished execution. The successor

0 commit comments

Comments
 (0)