Skip to content

Commit 01a074c

Browse files
authored
Merge pull request #14749 from MathiasVP/less-code-duplication
2 parents 3a62628 + 9062fb6 commit 01a074c

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ class Node0Impl extends TIRDataFlowNode0 {
8181
/** Gets the operands corresponding to this node, if any. */
8282
Operand asOperand() { result = this.(OperandNode0).getOperand() }
8383

84+
/** Gets the location of this node. */
85+
final Location getLocation() { result = this.getLocationImpl() }
86+
87+
/** INTERNAL: Do not use. */
88+
Location getLocationImpl() {
89+
none() // overridden by subclasses
90+
}
91+
8492
/** INTERNAL: Do not use. */
8593
string toStringImpl() {
8694
none() // overridden by subclasses
@@ -131,9 +139,15 @@ abstract class InstructionNode0 extends Node0Impl {
131139
override DataFlowType getType() { result = getInstructionType(instr, _) }
132140

133141
override string toStringImpl() {
134-
// This predicate is overridden in subclasses. This default implementation
135-
// does not use `Instruction.toString` because that's expensive to compute.
136-
result = instr.getOpcode().toString()
142+
if instr.(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable
143+
then result = "this"
144+
else result = instr.getAst().toString()
145+
}
146+
147+
override Location getLocationImpl() {
148+
if exists(instr.getAst().getLocation())
149+
then result = instr.getAst().getLocation()
150+
else result instanceof UnknownDefaultLocation
137151
}
138152

139153
final override predicate isGLValue() { exists(getInstructionType(instr, true)) }
@@ -173,7 +187,17 @@ abstract class OperandNode0 extends Node0Impl {
173187

174188
override DataFlowType getType() { result = getOperandType(op, _) }
175189

176-
override string toStringImpl() { result = op.toString() }
190+
override string toStringImpl() {
191+
if op.getDef().(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable
192+
then result = "this"
193+
else result = op.getDef().getAst().toString()
194+
}
195+
196+
override Location getLocationImpl() {
197+
if exists(op.getDef().getAst().getLocation())
198+
then result = op.getDef().getAst().getLocation()
199+
else result instanceof UnknownDefaultLocation
200+
}
177201

178202
final override predicate isGLValue() { exists(getOperandType(op, true)) }
179203
}

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ private class Node0 extends Node, TNode0 {
432432

433433
override Declaration getFunction() { result = node.getFunction() }
434434

435+
override Location getLocationImpl() { result = node.getLocation() }
436+
437+
override string toStringImpl() { result = node.toString() }
438+
435439
override DataFlowType getType() { result = node.getType() }
436440

437441
override predicate isGLValue() { node.isGLValue() }
@@ -448,18 +452,6 @@ class InstructionNode extends Node0 {
448452

449453
/** Gets the instruction corresponding to this node. */
450454
Instruction getInstruction() { result = instr }
451-
452-
override Location getLocationImpl() {
453-
if exists(instr.getAst().getLocation())
454-
then result = instr.getAst().getLocation()
455-
else result instanceof UnknownDefaultLocation
456-
}
457-
458-
override string toStringImpl() {
459-
if instr.(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable
460-
then result = "this"
461-
else result = instr.getAst().toString()
462-
}
463455
}
464456

465457
/**
@@ -473,18 +465,6 @@ class OperandNode extends Node, Node0 {
473465

474466
/** Gets the operand corresponding to this node. */
475467
Operand getOperand() { result = op }
476-
477-
override Location getLocationImpl() {
478-
if exists(op.getDef().getAst().getLocation())
479-
then result = op.getDef().getAst().getLocation()
480-
else result instanceof UnknownDefaultLocation
481-
}
482-
483-
override string toStringImpl() {
484-
if op.getDef().(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable
485-
then result = "this"
486-
else result = op.getDef().getAst().toString()
487-
}
488468
}
489469

490470
/**

0 commit comments

Comments
 (0)