Skip to content

Commit

Permalink
Merge pull request #14749 from MathiasVP/less-code-duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasVP authored Nov 10, 2023
2 parents 3a62628 + 9062fb6 commit 01a074c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ class Node0Impl extends TIRDataFlowNode0 {
/** Gets the operands corresponding to this node, if any. */
Operand asOperand() { result = this.(OperandNode0).getOperand() }

/** Gets the location of this node. */
final Location getLocation() { result = this.getLocationImpl() }

/** INTERNAL: Do not use. */
Location getLocationImpl() {
none() // overridden by subclasses
}

/** INTERNAL: Do not use. */
string toStringImpl() {
none() // overridden by subclasses
Expand Down Expand Up @@ -131,9 +139,15 @@ abstract class InstructionNode0 extends Node0Impl {
override DataFlowType getType() { result = getInstructionType(instr, _) }

override string toStringImpl() {
// This predicate is overridden in subclasses. This default implementation
// does not use `Instruction.toString` because that's expensive to compute.
result = instr.getOpcode().toString()
if instr.(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable
then result = "this"
else result = instr.getAst().toString()
}

override Location getLocationImpl() {
if exists(instr.getAst().getLocation())
then result = instr.getAst().getLocation()
else result instanceof UnknownDefaultLocation
}

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

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

override string toStringImpl() { result = op.toString() }
override string toStringImpl() {
if op.getDef().(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable
then result = "this"
else result = op.getDef().getAst().toString()
}

override Location getLocationImpl() {
if exists(op.getDef().getAst().getLocation())
then result = op.getDef().getAst().getLocation()
else result instanceof UnknownDefaultLocation
}

final override predicate isGLValue() { exists(getOperandType(op, true)) }
}
Expand Down
28 changes: 4 additions & 24 deletions cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ private class Node0 extends Node, TNode0 {

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

override Location getLocationImpl() { result = node.getLocation() }

override string toStringImpl() { result = node.toString() }

override DataFlowType getType() { result = node.getType() }

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

/** Gets the instruction corresponding to this node. */
Instruction getInstruction() { result = instr }

override Location getLocationImpl() {
if exists(instr.getAst().getLocation())
then result = instr.getAst().getLocation()
else result instanceof UnknownDefaultLocation
}

override string toStringImpl() {
if instr.(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable
then result = "this"
else result = instr.getAst().toString()
}
}

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

/** Gets the operand corresponding to this node. */
Operand getOperand() { result = op }

override Location getLocationImpl() {
if exists(op.getDef().getAst().getLocation())
then result = op.getDef().getAst().getLocation()
else result instanceof UnknownDefaultLocation
}

override string toStringImpl() {
if op.getDef().(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable
then result = "this"
else result = op.getDef().getAst().toString()
}
}

/**
Expand Down

0 comments on commit 01a074c

Please sign in to comment.