diff --git a/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll b/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll index e3f30b07dad0..9548ff680744 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll @@ -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 @@ -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)) } @@ -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)) } } diff --git a/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll b/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll index 20bdf7afe45e..50b374c5b044 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowUtil.qll @@ -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() } @@ -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() - } } /** @@ -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() - } } /**