Skip to content

Commit 60b384a

Browse files
author
Robert Marsh
committed
C++/C#: use line numbers for instruction IDs
This should reduce the number of merge conflicts in the IR tests resulting from instruction ID changes due to inserting or removing instructions
1 parent 113df4e commit 60b384a

File tree

9 files changed

+7137
-7107
lines changed

9 files changed

+7137
-7107
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Imports::IRType
99
import Imports::MemoryAccessKind
1010
import Imports::Opcode
1111
private import Imports::OperandTag
12+
import semmle.code.cpp.ir.implementation.internal.PrintSupport
1213

1314
module InstructionSanity {
1415
/**
@@ -368,6 +369,12 @@ class Instruction extends Construction::TInstruction {
368369
)
369370
}
370371

372+
int getLineRank() {
373+
this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr
374+
order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
375+
)
376+
}
377+
371378
/**
372379
* Gets a human-readable string that uniquely identifies this instruction
373380
* within the function. This string is used to refer to this instruction when
@@ -376,8 +383,7 @@ class Instruction extends Construction::TInstruction {
376383
* Example: `r1_1`
377384
*/
378385
string getResultId() {
379-
result = getResultPrefix() + getBlock().getDisplayIndex().toString() + "_" +
380-
getDisplayIndexInBlock().toString()
386+
result = getResultPrefix() + getAST().getLocation().getStartLine() + "_" + getLineRank()
381387
}
382388

383389
/**

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Imports::IRType
99
import Imports::MemoryAccessKind
1010
import Imports::Opcode
1111
private import Imports::OperandTag
12+
import semmle.code.cpp.ir.implementation.internal.PrintSupport
1213

1314
module InstructionSanity {
1415
/**
@@ -368,6 +369,12 @@ class Instruction extends Construction::TInstruction {
368369
)
369370
}
370371

372+
int getLineRank() {
373+
this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr
374+
order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
375+
)
376+
}
377+
371378
/**
372379
* Gets a human-readable string that uniquely identifies this instruction
373380
* within the function. This string is used to refer to this instruction when
@@ -376,8 +383,7 @@ class Instruction extends Construction::TInstruction {
376383
* Example: `r1_1`
377384
*/
378385
string getResultId() {
379-
result = getResultPrefix() + getBlock().getDisplayIndex().toString() + "_" +
380-
getDisplayIndexInBlock().toString()
386+
result = getResultPrefix() + getAST().getLocation().getStartLine() + "_" + getLineRank()
381387
}
382388

383389
/**

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Imports::IRType
99
import Imports::MemoryAccessKind
1010
import Imports::Opcode
1111
private import Imports::OperandTag
12+
import semmle.code.cpp.ir.implementation.internal.PrintSupport
1213

1314
module InstructionSanity {
1415
/**
@@ -368,6 +369,12 @@ class Instruction extends Construction::TInstruction {
368369
)
369370
}
370371

372+
int getLineRank() {
373+
this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr
374+
order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
375+
)
376+
}
377+
371378
/**
372379
* Gets a human-readable string that uniquely identifies this instruction
373380
* within the function. This string is used to refer to this instruction when
@@ -376,8 +383,7 @@ class Instruction extends Construction::TInstruction {
376383
* Example: `r1_1`
377384
*/
378385
string getResultId() {
379-
result = getResultPrefix() + getBlock().getDisplayIndex().toString() + "_" +
380-
getDisplayIndexInBlock().toString()
386+
result = getResultPrefix() + getAST().getLocation().getStartLine() + "_" + getLineRank()
381387
}
382388

383389
/**

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

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

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

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

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

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

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

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

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Imports::IRType
99
import Imports::MemoryAccessKind
1010
import Imports::Opcode
1111
private import Imports::OperandTag
12+
import semmle.code.cpp.ir.implementation.internal.PrintSupport
1213

1314
module InstructionSanity {
1415
/**
@@ -368,6 +369,12 @@ class Instruction extends Construction::TInstruction {
368369
)
369370
}
370371

372+
int getLineRank() {
373+
this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr
374+
order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
375+
)
376+
}
377+
371378
/**
372379
* Gets a human-readable string that uniquely identifies this instruction
373380
* within the function. This string is used to refer to this instruction when
@@ -376,8 +383,7 @@ class Instruction extends Construction::TInstruction {
376383
* Example: `r1_1`
377384
*/
378385
string getResultId() {
379-
result = getResultPrefix() + getBlock().getDisplayIndex().toString() + "_" +
380-
getDisplayIndexInBlock().toString()
386+
result = getResultPrefix() + getAST().getLocation().getStartLine() + "_" + getLineRank()
381387
}
382388

383389
/**

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Imports::IRType
99
import Imports::MemoryAccessKind
1010
import Imports::Opcode
1111
private import Imports::OperandTag
12+
import semmle.code.cpp.ir.implementation.internal.PrintSupport
1213

1314
module InstructionSanity {
1415
/**
@@ -368,6 +369,12 @@ class Instruction extends Construction::TInstruction {
368369
)
369370
}
370371

372+
int getLineRank() {
373+
this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr
374+
order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
375+
)
376+
}
377+
371378
/**
372379
* Gets a human-readable string that uniquely identifies this instruction
373380
* within the function. This string is used to refer to this instruction when
@@ -376,8 +383,7 @@ class Instruction extends Construction::TInstruction {
376383
* Example: `r1_1`
377384
*/
378385
string getResultId() {
379-
result = getResultPrefix() + getBlock().getDisplayIndex().toString() + "_" +
380-
getDisplayIndexInBlock().toString()
386+
result = getResultPrefix() + getAST().getLocation().getStartLine() + "_" + getLineRank()
381387
}
382388

383389
/**

0 commit comments

Comments
 (0)