Skip to content

Commit a80c6fb

Browse files
author
Dave Bartolomeo
committed
C++: Print target variable name for Load and Store, if known
Now that we've started printing the targets of `Call` instructions in the IR dumps, I figured I might as well print the names of the variable being loaded or stored as well. We could potentially extend this to match fields, array elements, etc., but that's quite a bit more work.
1 parent 7f2aa81 commit a80c6fb

File tree

12 files changed

+2775
-2685
lines changed

12 files changed

+2775
-2685
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,12 +804,26 @@ class CopyValueInstruction extends CopyInstruction, UnaryInstruction {
804804
final override UnaryOperand getSourceValueOperand() { result = getAnOperand() }
805805
}
806806

807+
/**
808+
* Gets a string describing the location pointed to by the specified address operand.
809+
*/
810+
private string getAddressOperandDescription(AddressOperand operand) {
811+
result = operand.getDef().(VariableAddressInstruction).getIRVariable().toString()
812+
or
813+
not operand.getDef() instanceof VariableAddressInstruction and
814+
result = "?"
815+
}
816+
807817
/**
808818
* An instruction that returns a register result containing a copy of its memory operand.
809819
*/
810820
class LoadInstruction extends CopyInstruction {
811821
LoadInstruction() { getOpcode() instanceof Opcode::Load }
812822

823+
final override string getImmediateString() {
824+
result = getAddressOperandDescription(getSourceAddressOperand())
825+
}
826+
813827
/**
814828
* Gets the operand that provides the address of the value being loaded.
815829
*/
@@ -829,6 +843,10 @@ class LoadInstruction extends CopyInstruction {
829843
class StoreInstruction extends CopyInstruction {
830844
StoreInstruction() { getOpcode() instanceof Opcode::Store }
831845

846+
final override string getImmediateString() {
847+
result = getAddressOperandDescription(getDestinationAddressOperand())
848+
}
849+
832850
/**
833851
* Gets the operand that provides the address of the location to which the value will be stored.
834852
*/

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,12 +804,26 @@ class CopyValueInstruction extends CopyInstruction, UnaryInstruction {
804804
final override UnaryOperand getSourceValueOperand() { result = getAnOperand() }
805805
}
806806

807+
/**
808+
* Gets a string describing the location pointed to by the specified address operand.
809+
*/
810+
private string getAddressOperandDescription(AddressOperand operand) {
811+
result = operand.getDef().(VariableAddressInstruction).getIRVariable().toString()
812+
or
813+
not operand.getDef() instanceof VariableAddressInstruction and
814+
result = "?"
815+
}
816+
807817
/**
808818
* An instruction that returns a register result containing a copy of its memory operand.
809819
*/
810820
class LoadInstruction extends CopyInstruction {
811821
LoadInstruction() { getOpcode() instanceof Opcode::Load }
812822

823+
final override string getImmediateString() {
824+
result = getAddressOperandDescription(getSourceAddressOperand())
825+
}
826+
813827
/**
814828
* Gets the operand that provides the address of the value being loaded.
815829
*/
@@ -829,6 +843,10 @@ class LoadInstruction extends CopyInstruction {
829843
class StoreInstruction extends CopyInstruction {
830844
StoreInstruction() { getOpcode() instanceof Opcode::Store }
831845

846+
final override string getImmediateString() {
847+
result = getAddressOperandDescription(getDestinationAddressOperand())
848+
}
849+
832850
/**
833851
* Gets the operand that provides the address of the location to which the value will be stored.
834852
*/

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,12 +804,26 @@ class CopyValueInstruction extends CopyInstruction, UnaryInstruction {
804804
final override UnaryOperand getSourceValueOperand() { result = getAnOperand() }
805805
}
806806

807+
/**
808+
* Gets a string describing the location pointed to by the specified address operand.
809+
*/
810+
private string getAddressOperandDescription(AddressOperand operand) {
811+
result = operand.getDef().(VariableAddressInstruction).getIRVariable().toString()
812+
or
813+
not operand.getDef() instanceof VariableAddressInstruction and
814+
result = "?"
815+
}
816+
807817
/**
808818
* An instruction that returns a register result containing a copy of its memory operand.
809819
*/
810820
class LoadInstruction extends CopyInstruction {
811821
LoadInstruction() { getOpcode() instanceof Opcode::Load }
812822

823+
final override string getImmediateString() {
824+
result = getAddressOperandDescription(getSourceAddressOperand())
825+
}
826+
813827
/**
814828
* Gets the operand that provides the address of the value being loaded.
815829
*/
@@ -829,6 +843,10 @@ class LoadInstruction extends CopyInstruction {
829843
class StoreInstruction extends CopyInstruction {
830844
StoreInstruction() { getOpcode() instanceof Opcode::Store }
831845

846+
final override string getImmediateString() {
847+
result = getAddressOperandDescription(getDestinationAddressOperand())
848+
}
849+
832850
/**
833851
* Gets the operand that provides the address of the location to which the value will be stored.
834852
*/

cpp/ql/test/library-tests/ir/ir/raw_ir.expected

Lines changed: 1307 additions & 1307 deletions
Large diffs are not rendered by default.

cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected

Lines changed: 229 additions & 229 deletions
Large diffs are not rendered by default.

cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir_unsound.expected

Lines changed: 229 additions & 229 deletions
Large diffs are not rendered by default.

cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir.expected

Lines changed: 231 additions & 231 deletions
Large diffs are not rendered by default.

cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir_unsound.expected

Lines changed: 231 additions & 231 deletions
Large diffs are not rendered by default.

cpp/ql/test/library-tests/valuenumbering/GlobalValueNumbering/ir_gvn.expected

Lines changed: 143 additions & 143 deletions
Large diffs are not rendered by default.

csharp/ql/src/experimental/ir/implementation/raw/Instruction.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,12 +804,26 @@ class CopyValueInstruction extends CopyInstruction, UnaryInstruction {
804804
final override UnaryOperand getSourceValueOperand() { result = getAnOperand() }
805805
}
806806

807+
/**
808+
* Gets a string describing the location pointed to by the specified address operand.
809+
*/
810+
private string getAddressOperandDescription(AddressOperand operand) {
811+
result = operand.getDef().(VariableAddressInstruction).getIRVariable().toString()
812+
or
813+
not operand.getDef() instanceof VariableAddressInstruction and
814+
result = "?"
815+
}
816+
807817
/**
808818
* An instruction that returns a register result containing a copy of its memory operand.
809819
*/
810820
class LoadInstruction extends CopyInstruction {
811821
LoadInstruction() { getOpcode() instanceof Opcode::Load }
812822

823+
final override string getImmediateString() {
824+
result = getAddressOperandDescription(getSourceAddressOperand())
825+
}
826+
813827
/**
814828
* Gets the operand that provides the address of the value being loaded.
815829
*/
@@ -829,6 +843,10 @@ class LoadInstruction extends CopyInstruction {
829843
class StoreInstruction extends CopyInstruction {
830844
StoreInstruction() { getOpcode() instanceof Opcode::Store }
831845

846+
final override string getImmediateString() {
847+
result = getAddressOperandDescription(getDestinationAddressOperand())
848+
}
849+
832850
/**
833851
* Gets the operand that provides the address of the location to which the value will be stored.
834852
*/

0 commit comments

Comments
 (0)