Skip to content

Commit d2e8b88

Browse files
committed
C++: Add an 'EdgeKind' column to 'getChildSuccessor'.
1 parent a391f78 commit d2e8b88

File tree

9 files changed

+413
-352
lines changed

9 files changed

+413
-352
lines changed

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,32 @@ abstract class TranslatedCall extends TranslatedExpr {
5252
resultType = getTypeForPRValue(this.getCallResultType())
5353
}
5454

55-
override Instruction getChildSuccessor(TranslatedElement child) {
56-
child = this.getQualifier() and
57-
result = this.getFirstCallTargetInstruction()
58-
or
59-
child = this.getCallTarget() and
60-
result = this.getFirstArgumentOrCallInstruction()
61-
or
62-
exists(int argIndex |
63-
child = this.getArgument(argIndex) and
64-
if exists(this.getArgument(argIndex + 1))
65-
then result = this.getArgument(argIndex + 1).getFirstInstruction()
66-
else result = this.getInstruction(CallTag())
55+
override Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) {
56+
kind instanceof GotoEdge and
57+
(
58+
child = this.getQualifier() and
59+
result = this.getFirstCallTargetInstruction()
60+
or
61+
child = this.getCallTarget() and
62+
result = this.getFirstArgumentOrCallInstruction()
63+
or
64+
exists(int argIndex |
65+
child = this.getArgument(argIndex) and
66+
if exists(this.getArgument(argIndex + 1))
67+
then result = this.getArgument(argIndex + 1).getFirstInstruction()
68+
else result = this.getInstruction(CallTag())
69+
)
6770
)
6871
or
6972
child = this.getSideEffects() and
7073
if this.isNoReturn()
7174
then
75+
kind instanceof GotoEdge and
7276
result =
7377
any(UnreachedInstruction instr |
7478
this.getEnclosingFunction().getFunction() = instr.getEnclosingFunction()
7579
)
76-
else result = this.getParent().getChildSuccessor(this)
80+
else result = this.getParent().getChildSuccessor(this, kind)
7781
}
7882

7983
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
@@ -203,12 +207,12 @@ abstract class TranslatedSideEffects extends TranslatedElement {
203207
)
204208
}
205209

206-
final override Instruction getChildSuccessor(TranslatedElement te) {
210+
final override Instruction getChildSuccessor(TranslatedElement te, EdgeKind kind) {
207211
exists(int i |
208212
this.getChild(i) = te and
209213
if exists(this.getChild(i + 1))
210-
then result = this.getChild(i + 1).getFirstInstruction()
211-
else result = this.getParent().getChildSuccessor(this)
214+
then kind instanceof GotoEdge and result = this.getChild(i + 1).getFirstInstruction()
215+
else result = this.getParent().getChildSuccessor(this, kind)
212216
)
213217
}
214218

@@ -220,7 +224,8 @@ abstract class TranslatedSideEffects extends TranslatedElement {
220224
result = this.getChild(0).getFirstInstruction()
221225
or
222226
// Some functions, like `std::move()`, have no side effects whatsoever.
223-
not exists(this.getChild(0)) and result = this.getParent().getChildSuccessor(this)
227+
not exists(this.getChild(0)) and
228+
result = this.getParent().getChildSuccessor(this, any(GotoEdge edge))
224229
}
225230

226231
final override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) { none() }
@@ -376,7 +381,7 @@ private int initializeAllocationGroup() { result = 3 }
376381
abstract class TranslatedSideEffect extends TranslatedElement {
377382
final override TranslatedElement getChild(int n) { none() }
378383

379-
final override Instruction getChildSuccessor(TranslatedElement child) { none() }
384+
final override Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) { none() }
380385

381386
final override Instruction getFirstInstruction() {
382387
result = this.getInstruction(OnlyInstructionTag())
@@ -388,9 +393,8 @@ abstract class TranslatedSideEffect extends TranslatedElement {
388393
}
389394

390395
final override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
391-
result = this.getParent().getChildSuccessor(this) and
392-
tag = OnlyInstructionTag() and
393-
kind instanceof GotoEdge
396+
result = this.getParent().getChildSuccessor(this, kind) and
397+
tag = OnlyInstructionTag()
394398
}
395399

396400
final override Declaration getFunction() { result = this.getParent().getFunction() }

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ abstract class TranslatedFlexibleCondition extends TranslatedCondition, Conditio
5454

5555
final override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) { none() }
5656

57-
final override Instruction getChildSuccessor(TranslatedElement child) { none() }
57+
final override Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) { none() }
5858

5959
abstract TranslatedCondition getOperand();
6060
}
@@ -80,7 +80,7 @@ class TranslatedParenthesisCondition extends TranslatedFlexibleCondition {
8080
abstract class TranslatedNativeCondition extends TranslatedCondition, TTranslatedNativeCondition {
8181
TranslatedNativeCondition() { this = TTranslatedNativeCondition(expr) }
8282

83-
final override Instruction getChildSuccessor(TranslatedElement child) { none() }
83+
final override Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) { none() }
8484
}
8585

8686
abstract class TranslatedBinaryLogicalOperation extends TranslatedNativeCondition, ConditionContext {
@@ -158,9 +158,10 @@ class TranslatedValueCondition extends TranslatedCondition, TTranslatedValueCond
158158
resultType = getVoidType()
159159
}
160160

161-
override Instruction getChildSuccessor(TranslatedElement child) {
161+
override Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) {
162162
child = this.getValueExpr() and
163-
result = this.getInstruction(ValueConditionConditionalBranchTag())
163+
result = this.getInstruction(ValueConditionConditionalBranchTag()) and
164+
kind instanceof GotoEdge
164165
}
165166

166167
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ abstract class TranslatedLocalVariableDeclaration extends TranslatedVariableInit
6868
}
6969

7070
final override Instruction getInitializationSuccessor() {
71-
result = this.getParent().getChildSuccessor(this)
71+
result = this.getParent().getChildSuccessor(this, any(GotoEdge edge))
7272
}
7373

7474
final override IRVariable getIRVariable() {
@@ -163,7 +163,7 @@ class TranslatedStaticLocalVariableDeclarationEntry extends TranslatedDeclaratio
163163
tag = DynamicInitializationConditionalBranchTag() and
164164
(
165165
kind instanceof TrueEdge and
166-
result = this.getParent().getChildSuccessor(this)
166+
result = this.getParent().getChildSuccessor(this, any(GotoEdge edge))
167167
or
168168
kind instanceof FalseEdge and
169169
result = this.getInitialization().getFirstInstruction()
@@ -174,13 +174,13 @@ class TranslatedStaticLocalVariableDeclarationEntry extends TranslatedDeclaratio
174174
result = this.getInstruction(DynamicInitializationFlagStoreTag())
175175
or
176176
tag = DynamicInitializationFlagStoreTag() and
177-
kind instanceof GotoEdge and
178-
result = this.getParent().getChildSuccessor(this)
177+
result = this.getParent().getChildSuccessor(this, kind)
179178
}
180179

181-
final override Instruction getChildSuccessor(TranslatedElement child) {
180+
final override Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind) {
182181
child = this.getInitialization() and
183-
result = this.getInstruction(DynamicInitializationFlagConstantTag())
182+
result = this.getInstruction(DynamicInitializationFlagConstantTag()) and
183+
kind instanceof GotoEdge
184184
}
185185

186186
final override IRDynamicInitializationFlag getInstructionVariable(InstructionTag tag) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,10 @@ abstract class TranslatedElement extends TTranslatedElement {
904904

905905
/**
906906
* Gets the successor instruction to which control should flow after the
907-
* child element specified by `child` has finished execution.
907+
* child element specified by `child` has finished execution. The successor
908+
* edge kind is specified by `kind`.
908909
*/
909-
abstract Instruction getChildSuccessor(TranslatedElement child);
910+
abstract Instruction getChildSuccessor(TranslatedElement child, EdgeKind kind);
910911

911912
/**
912913
* Gets the instruction to which control should flow if an exception is thrown

0 commit comments

Comments
 (0)