Skip to content

Commit 8013c2a

Browse files
committed
C++: QLDoc and naming updates for implicit destructors in IR
1 parent bbabf1d commit 8013c2a

File tree

9 files changed

+173
-140
lines changed

9 files changed

+173
-140
lines changed

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

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

50-
override Instruction getLastInstructionInternal() {
51-
result = this.getSideEffects().getLastInstruction()
50+
override Instruction getALastInstructionInternal() {
51+
result = this.getSideEffects().getALastInstruction()
5252
}
5353

5454
override TranslatedElement getLastChild() { result = this.getSideEffects() }
@@ -94,7 +94,6 @@ abstract class TranslatedCall extends TranslatedExpr {
9494
}
9595

9696
override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) {
97-
kind instanceof GotoEdge and
9897
tag = CallTag() and
9998
result = this.getSideEffects().getFirstInstruction(kind)
10099
}
@@ -257,9 +256,9 @@ abstract class TranslatedSideEffects extends TranslatedElement {
257256
result = this.getParent().getChildSuccessor(this, kind)
258257
}
259258

260-
override Instruction getLastInstructionInternal() {
259+
override Instruction getALastInstructionInternal() {
261260
if exists(this.getAChild())
262-
then result = this.getChild(max(int i | exists(this.getChild(i)))).getLastInstruction()
261+
then result = this.getChild(max(int i | exists(this.getChild(i)))).getALastInstruction()
263262
else
264263
// If there are no side effects, the "last" instruction should be the parent call's last
265264
// instruction, so that implicit destructors can be inserted in the right place.
@@ -455,7 +454,7 @@ abstract class TranslatedSideEffect extends TranslatedElement {
455454
kind instanceof GotoEdge
456455
}
457456

458-
override Instruction getLastInstructionInternal() {
457+
override Instruction getALastInstructionInternal() {
459458
result = this.getInstruction(OnlyInstructionTag())
460459
}
461460

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ abstract class TranslatedFlexibleCondition extends TranslatedCondition, Conditio
5858
result = this.getOperand().getFirstInstruction(kind)
5959
}
6060

61-
final override Instruction getLastInstructionInternal() {
62-
result = this.getOperand().getLastInstruction()
61+
final override Instruction getALastInstructionInternal() {
62+
result = this.getOperand().getALastInstruction()
6363
}
6464

6565
final override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
@@ -70,7 +70,7 @@ abstract class TranslatedFlexibleCondition extends TranslatedCondition, Conditio
7070
none()
7171
}
7272

73-
final override Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) { none() }
73+
final override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) { none() }
7474

7575
abstract TranslatedCondition getOperand();
7676
}
@@ -96,7 +96,7 @@ class TranslatedParenthesisCondition extends TranslatedFlexibleCondition {
9696
abstract class TranslatedNativeCondition extends TranslatedCondition, TTranslatedNativeCondition {
9797
TranslatedNativeCondition() { this = TTranslatedNativeCondition(expr) }
9898

99-
final override Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) { none() }
99+
final override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) { none() }
100100
}
101101

102102
abstract class TranslatedBinaryLogicalOperation extends TranslatedNativeCondition, ConditionContext {
@@ -114,10 +114,10 @@ abstract class TranslatedBinaryLogicalOperation extends TranslatedNativeConditio
114114
result = this.getLeftOperand().getFirstInstruction(kind)
115115
}
116116

117-
final override Instruction getLastInstructionInternal() {
118-
result = this.getLeftOperand().getLastInstruction()
117+
final override Instruction getALastInstructionInternal() {
118+
result = this.getLeftOperand().getALastInstruction()
119119
or
120-
result = this.getRightOperand().getLastInstruction()
120+
result = this.getRightOperand().getALastInstruction()
121121
}
122122

123123
final override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
@@ -180,7 +180,7 @@ class TranslatedValueCondition extends TranslatedCondition, TTranslatedValueCond
180180
result = this.getValueExpr().getFirstInstruction(kind)
181181
}
182182

183-
override Instruction getLastInstructionInternal() {
183+
override Instruction getALastInstructionInternal() {
184184
result = this.getInstruction(ValueConditionConditionalBranchTag())
185185
}
186186

@@ -192,7 +192,7 @@ class TranslatedValueCondition extends TranslatedCondition, TTranslatedValueCond
192192
resultType = getVoidType()
193193
}
194194

195-
override Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) {
195+
override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) {
196196
child = this.getValueExpr() and
197197
result = this.getInstruction(ValueConditionConditionalBranchTag()) and
198198
kind instanceof GotoEdge

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

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

159-
final override Instruction getLastInstructionInternal() {
159+
final override Instruction getALastInstructionInternal() {
160160
result = this.getInstruction(DynamicInitializationConditionalBranchTag())
161161
or
162162
result = this.getInstruction(DynamicInitializationFlagStoreTag())
@@ -188,7 +188,7 @@ class TranslatedStaticLocalVariableDeclarationEntry extends TranslatedDeclaratio
188188
result = this.getParent().getChildSuccessor(this, kind)
189189
}
190190

191-
final override Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) {
191+
final override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) {
192192
child = this.getInitialization() and
193193
result = this.getInstruction(DynamicInitializationFlagConstantTag()) and
194194
kind instanceof GotoEdge

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

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ private import SideEffects
2323
Element getRealParent(Expr expr) {
2424
result = expr.getParentWithConversions()
2525
or
26-
/*
27-
* exists(Stmt destructorParent, DestructorCall dc |
28-
* destructorParent.getAnImplicitDestructorCall() = dc and
29-
* dc.getQualifier() = expr and
30-
* result = dc
31-
* )
32-
* or
33-
*/
34-
3526
result.(Destructor).getADestruction() = expr
3627
or
3728
result.(Expr).getAnImplicitDestructorCall() = expr
@@ -873,12 +864,18 @@ abstract class TranslatedElement extends TTranslatedElement {
873864
/**
874865
* Holds if this element has implicit destructor calls that should follow it.
875866
*/
876-
predicate hasImplicitDestructorCalls() { none() }
867+
predicate hasAnImplicitDestructorCall() { none() }
877868

878869
/**
870+
* Gets the child index of the first destructor call that should be executed after this `TranslatedElement`
879871
*/
880872
int getFirstDestructorCallIndex() { none() }
881873

874+
/**
875+
* Holds if this `TranslatedElement` includes any destructor calls that must be performed after
876+
* it in its `getChildSuccessorInternal`, `getInstructionSuccessorInternal`, and
877+
* `getALastInstructionInternal` relations, rather than needing them inserted.
878+
*/
882879
predicate handlesDestructorsExplicitly() { none() }
883880

884881
private int getUniqueId() {
@@ -916,42 +913,65 @@ abstract class TranslatedElement extends TTranslatedElement {
916913
/**
917914
* Gets the successor instruction of the instruction that was generated by
918915
* this element for tag `tag`. The successor edge kind is specified by `kind`.
916+
* This predicate does not usually include destructors, which are inserted as
917+
* part of `getInstructionSuccessor` unless `handlesDestructorsExplicitly`
918+
* holds.
919919
*/
920920
abstract Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind);
921-
922-
Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
921+
/**
922+
* Gets the successor instruction of the instruction that was generated by
923+
* this element for tag `tag`. The successor edge kind is specified by `kind`.
924+
*/
925+
final Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
923926
if
924-
this.hasImplicitDestructorCalls() and
925-
this.getInstruction(tag) = this.getLastInstructionInternal() and
927+
this.hasAnImplicitDestructorCall() and
928+
this.getInstruction(tag) = this.getALastInstructionInternal() and
926929
not this.handlesDestructorsExplicitly()
927930
then
928931
result = this.getChild(this.getFirstDestructorCallIndex()).getFirstInstruction(kind) and
929932
kind instanceof GotoEdge
930933
else result = this.getInstructionSuccessorInternal(tag, kind)
931934
}
932935

933-
final Instruction getLastInstruction() {
934-
if this.hasImplicitDestructorCalls() and not this.handlesDestructorsExplicitly()
935-
then result = this.getChild(max(int n | exists(this.getChild(n)))).getLastInstruction() // last destructor
936-
else result = this.getLastInstructionInternal()
936+
/**
937+
* Gets an instruction within this `TranslatedElement` (including its transitive children) which
938+
* will be followed by an instruction outside the `TranslatedElement`.
939+
*/
940+
final Instruction getALastInstruction() {
941+
if this.hasAnImplicitDestructorCall() and not this.handlesDestructorsExplicitly()
942+
then result = this.getChild(max(int n | exists(this.getChild(n)))).getALastInstruction() // last destructor
943+
else result = this.getALastInstructionInternal()
937944
}
938945

939-
abstract Instruction getLastInstructionInternal();
946+
/**
947+
* Gets an instruction within this `TranslatedElement` (including its transitive children) which
948+
* will be followed by an instruction outside the `TranslatedElement`.
949+
* This predicate does not usually include destructors, which are inserted as
950+
* part of `getALastInstruction` unless `handlesDestructorsExplicitly` holds.
951+
*/
952+
abstract Instruction getALastInstructionInternal();
940953

941954
TranslatedElement getLastChild() { none() }
942955

943956
/**
944957
* Gets the successor instruction to which control should flow after the
945958
* child element specified by `child` has finished execution. The successor
946959
* edge kind is specified by `kind`.
960+
* This predicate does not usually include destructors, which are inserted as
961+
* part of `getChildSuccessor` unless `handlesDestructorsExplicitly` holds.
947962
*/
948963
Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) { none() }
949964

950-
Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) {
965+
/**
966+
* Gets the successor instruction to which control should flow after the
967+
* child element specified by `child` has finished execution. The successor
968+
* edge kind is specified by `kind`.
969+
*/
970+
final Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) {
951971
(
952972
if
953973
// this is the last child and we need to handle destructors for it
954-
this.hasImplicitDestructorCalls() and
974+
this.hasAnImplicitDestructorCall() and
955975
not this.handlesDestructorsExplicitly() and
956976
child = this.getLastChild()
957977
then result = this.getChild(this.getFirstDestructorCallIndex()).getFirstInstruction(kind)

0 commit comments

Comments
 (0)