Skip to content

Commit 3767794

Browse files
author
Dave Bartolomeo
authored
Merge pull request github#2975 from rdmarsh2/printir-generate-all
C++/C#: generate IR for funcs excluded in PrintIR
2 parents cf5b1f0 + 2b69cc9 commit 3767794

File tree

23 files changed

+140
-49
lines changed

23 files changed

+140
-49
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/IRConfiguration.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class IRConfiguration extends TIRConfiguration {
1616
* Holds if IR should be created for function `func`. By default, holds for all functions.
1717
*/
1818
predicate shouldCreateIRForFunction(Language::Function func) { any() }
19+
20+
predicate shouldEvaluateDebugStringsForFunction(Language::Function func) { any() }
1921
}
2022

2123
private newtype TIREscapeAnalysisConfiguration = MkIREscapeAnalysisConfiguration()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class IRBlockBase extends TIRBlock {
2727
* by debugging and printing code only.
2828
*/
2929
int getDisplayIndex() {
30+
exists(IRConfiguration::IRConfiguration config |
31+
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
32+
) and
3033
this =
3134
rank[result + 1](IRBlock funcBlock |
3235
funcBlock.getEnclosingFunction() = getEnclosingFunction()

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ private import Imports::OperandTag
1515
* `File` and line number. Used for assigning register names when printing IR.
1616
*/
1717
private Instruction getAnInstructionAtLine(IRFunction irFunc, Language::File file, int line) {
18+
exists(IRConfiguration::IRConfiguration config |
19+
config.shouldEvaluateDebugStringsForFunction(irFunc.getFunction())
20+
) and
1821
exists(Language::Location ___location |
1922
irFunc = result.getEnclosingIRFunction() and
2023
___location = result.getLocation() and
@@ -39,13 +42,20 @@ class Instruction extends Construction::TInstruction {
3942
result = getResultString() + " = " + getOperationString() + " " + getOperandsString()
4043
}
4144

45+
predicate shouldGenerateDumpStrings() {
46+
exists(IRConfiguration::IRConfiguration config |
47+
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
48+
)
49+
}
50+
4251
/**
4352
* Gets a string describing the operation of this instruction. This includes
4453
* the opcode and the immediate value, if any. For example:
4554
*
4655
* VariableAddress[x]
4756
*/
4857
final string getOperationString() {
58+
shouldGenerateDumpStrings() and
4959
if exists(getImmediateString())
5060
then result = getOperationPrefix() + getOpcode().toString() + "[" + getImmediateString() + "]"
5161
else result = getOperationPrefix() + getOpcode().toString()
@@ -57,10 +67,12 @@ class Instruction extends Construction::TInstruction {
5767
string getImmediateString() { none() }
5868

5969
private string getOperationPrefix() {
70+
shouldGenerateDumpStrings() and
6071
if this instanceof SideEffectInstruction then result = "^" else result = ""
6172
}
6273

6374
private string getResultPrefix() {
75+
shouldGenerateDumpStrings() and
6476
if getResultIRType() instanceof IRVoidType
6577
then result = "v"
6678
else
@@ -74,6 +86,7 @@ class Instruction extends Construction::TInstruction {
7486
* used by debugging and printing code only.
7587
*/
7688
int getDisplayIndexInBlock() {
89+
shouldGenerateDumpStrings() and
7790
exists(IRBlock block |
7891
this = block.getInstruction(result)
7992
or
@@ -87,6 +100,7 @@ class Instruction extends Construction::TInstruction {
87100
}
88101

89102
private int getLineRank() {
103+
shouldGenerateDumpStrings() and
90104
this =
91105
rank[result](Instruction instr |
92106
instr =
@@ -105,6 +119,7 @@ class Instruction extends Construction::TInstruction {
105119
* Example: `r1_1`
106120
*/
107121
string getResultId() {
122+
shouldGenerateDumpStrings() and
108123
result = getResultPrefix() + getAST().getLocation().getStartLine() + "_" + getLineRank()
109124
}
110125

@@ -116,6 +131,7 @@ class Instruction extends Construction::TInstruction {
116131
* Example: `r1_1(int*)`
117132
*/
118133
final string getResultString() {
134+
shouldGenerateDumpStrings() and
119135
result = getResultId() + "(" + getResultLanguageType().getDumpString() + ")"
120136
}
121137

@@ -126,6 +142,7 @@ class Instruction extends Construction::TInstruction {
126142
* Example: `func:r3_4, this:r3_5`
127143
*/
128144
string getOperandsString() {
145+
shouldGenerateDumpStrings() and
129146
result =
130147
concat(Operand operand |
131148
operand = getAnOperand()

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ class PrintIRConfiguration extends TPrintIRConfiguration {
1818
predicate shouldPrintFunction(Language::Function func) { any() }
1919
}
2020

21-
private predicate shouldPrintFunction(Language::Function func) {
22-
exists(PrintIRConfiguration config | config.shouldPrintFunction(func))
23-
}
24-
2521
/**
26-
* Override of `IRConfiguration` to only create IR for the functions that are to be dumped.
22+
* Override of `IRConfiguration` to only evaluate debug strings for the functions that are to be dumped.
2723
*/
2824
private class FilteredIRConfiguration extends IRConfiguration {
29-
override predicate shouldCreateIRForFunction(Language::Function func) {
25+
override predicate shouldEvaluateDebugStringsForFunction(Language::Function func) {
3026
shouldPrintFunction(func)
3127
}
3228
}
3329

30+
private predicate shouldPrintFunction(Language::Function func) {
31+
exists(PrintIRConfiguration config | config.shouldPrintFunction(func))
32+
}
33+
3434
private string getAdditionalInstructionProperty(Instruction instr, string key) {
3535
exists(IRPropertyProvider provider | result = provider.getInstructionProperty(instr, key))
3636
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import semmle.code.cpp.ir.internal.IRCppLanguage as Language
22
import SSAConstruction as Construction
3+
import semmle.code.cpp.ir.implementation.IRConfiguration as IRConfiguration

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class IRBlockBase extends TIRBlock {
2727
* by debugging and printing code only.
2828
*/
2929
int getDisplayIndex() {
30+
exists(IRConfiguration::IRConfiguration config |
31+
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
32+
) and
3033
this =
3134
rank[result + 1](IRBlock funcBlock |
3235
funcBlock.getEnclosingFunction() = getEnclosingFunction()

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ private import Imports::OperandTag
1515
* `File` and line number. Used for assigning register names when printing IR.
1616
*/
1717
private Instruction getAnInstructionAtLine(IRFunction irFunc, Language::File file, int line) {
18+
exists(IRConfiguration::IRConfiguration config |
19+
config.shouldEvaluateDebugStringsForFunction(irFunc.getFunction())
20+
) and
1821
exists(Language::Location ___location |
1922
irFunc = result.getEnclosingIRFunction() and
2023
___location = result.getLocation() and
@@ -39,13 +42,20 @@ class Instruction extends Construction::TInstruction {
3942
result = getResultString() + " = " + getOperationString() + " " + getOperandsString()
4043
}
4144

45+
predicate shouldGenerateDumpStrings() {
46+
exists(IRConfiguration::IRConfiguration config |
47+
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
48+
)
49+
}
50+
4251
/**
4352
* Gets a string describing the operation of this instruction. This includes
4453
* the opcode and the immediate value, if any. For example:
4554
*
4655
* VariableAddress[x]
4756
*/
4857
final string getOperationString() {
58+
shouldGenerateDumpStrings() and
4959
if exists(getImmediateString())
5060
then result = getOperationPrefix() + getOpcode().toString() + "[" + getImmediateString() + "]"
5161
else result = getOperationPrefix() + getOpcode().toString()
@@ -57,10 +67,12 @@ class Instruction extends Construction::TInstruction {
5767
string getImmediateString() { none() }
5868

5969
private string getOperationPrefix() {
70+
shouldGenerateDumpStrings() and
6071
if this instanceof SideEffectInstruction then result = "^" else result = ""
6172
}
6273

6374
private string getResultPrefix() {
75+
shouldGenerateDumpStrings() and
6476
if getResultIRType() instanceof IRVoidType
6577
then result = "v"
6678
else
@@ -74,6 +86,7 @@ class Instruction extends Construction::TInstruction {
7486
* used by debugging and printing code only.
7587
*/
7688
int getDisplayIndexInBlock() {
89+
shouldGenerateDumpStrings() and
7790
exists(IRBlock block |
7891
this = block.getInstruction(result)
7992
or
@@ -87,6 +100,7 @@ class Instruction extends Construction::TInstruction {
87100
}
88101

89102
private int getLineRank() {
103+
shouldGenerateDumpStrings() and
90104
this =
91105
rank[result](Instruction instr |
92106
instr =
@@ -105,6 +119,7 @@ class Instruction extends Construction::TInstruction {
105119
* Example: `r1_1`
106120
*/
107121
string getResultId() {
122+
shouldGenerateDumpStrings() and
108123
result = getResultPrefix() + getAST().getLocation().getStartLine() + "_" + getLineRank()
109124
}
110125

@@ -116,6 +131,7 @@ class Instruction extends Construction::TInstruction {
116131
* Example: `r1_1(int*)`
117132
*/
118133
final string getResultString() {
134+
shouldGenerateDumpStrings() and
119135
result = getResultId() + "(" + getResultLanguageType().getDumpString() + ")"
120136
}
121137

@@ -126,6 +142,7 @@ class Instruction extends Construction::TInstruction {
126142
* Example: `func:r3_4, this:r3_5`
127143
*/
128144
string getOperandsString() {
145+
shouldGenerateDumpStrings() and
129146
result =
130147
concat(Operand operand |
131148
operand = getAnOperand()

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ class PrintIRConfiguration extends TPrintIRConfiguration {
1818
predicate shouldPrintFunction(Language::Function func) { any() }
1919
}
2020

21-
private predicate shouldPrintFunction(Language::Function func) {
22-
exists(PrintIRConfiguration config | config.shouldPrintFunction(func))
23-
}
24-
2521
/**
26-
* Override of `IRConfiguration` to only create IR for the functions that are to be dumped.
22+
* Override of `IRConfiguration` to only evaluate debug strings for the functions that are to be dumped.
2723
*/
2824
private class FilteredIRConfiguration extends IRConfiguration {
29-
override predicate shouldCreateIRForFunction(Language::Function func) {
25+
override predicate shouldEvaluateDebugStringsForFunction(Language::Function func) {
3026
shouldPrintFunction(func)
3127
}
3228
}
3329

30+
private predicate shouldPrintFunction(Language::Function func) {
31+
exists(PrintIRConfiguration config | config.shouldPrintFunction(func))
32+
}
33+
3434
private string getAdditionalInstructionProperty(Instruction instr, string key) {
3535
exists(IRPropertyProvider provider | result = provider.getInstructionProperty(instr, key))
3636
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
import semmle.code.cpp.ir.internal.IRCppLanguage as Language
22
import IRConstruction as Construction
3+
import semmle.code.cpp.ir.implementation.IRConfiguration as IRConfiguration

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class IRBlockBase extends TIRBlock {
2727
* by debugging and printing code only.
2828
*/
2929
int getDisplayIndex() {
30+
exists(IRConfiguration::IRConfiguration config |
31+
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
32+
) and
3033
this =
3134
rank[result + 1](IRBlock funcBlock |
3235
funcBlock.getEnclosingFunction() = getEnclosingFunction()

0 commit comments

Comments
 (0)