Skip to content

Commit 9b25834

Browse files
committed
C++: Use parameterized modules instead of abstract classes and predicates to handle debug printing.
1 parent 5016459 commit 9b25834

File tree

4 files changed

+78
-75
lines changed

4 files changed

+78
-75
lines changed
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
/**
2-
* This file activates debugging mode for dataflow node printing.
2+
* This file contains module that implements the _debug_ version of
3+
* `toString` for `Instruction` and `Operand` dataflow nodes.
34
*/
45

5-
private import Node0ToString
6+
private import semmle.code.cpp.ir.IR
7+
private import codeql.util.Unit
8+
private import Node0ToStringSig
9+
private import DataFlowUtil
610

7-
private class DebugNode0ToString extends Node0ToString {
8-
final override predicate isDebugMode() { any() }
11+
private module DebugNode0ToString implements Node0ToStringSig {
12+
string instructionToString(Instruction i) { result = i.getDumpString() }
13+
14+
string operandToString(Operand op) {
15+
result = op.getDumpString() + " @ " + op.getUse().getResultId()
16+
}
17+
18+
string toExprString(Node n) { none() }
919
}
20+
21+
import DebugNode0ToString
Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,8 @@
11
/**
2-
* This file contains the abstract class that serves as the base class for
3-
* dataflow node printing.
2+
* This file imports the module that is used to construct the strings used by `Node.ToString`.
43
*
5-
* By default, a non-debug string is produced. However, a debug-friendly
6-
* string can be produced by importing `DebugPrinting.qll`.
4+
* Normally, this file should just import `NormalNode0ToString` to compute the efficient `toString`, but for debugging purposes
5+
* one can import `DebugPrinting.qll` to better correlate the dataflow nodes with their underlying instructions and operands.
76
*/
87

9-
private import semmle.code.cpp.ir.IR
10-
private import codeql.util.Unit
11-
12-
/**
13-
* A class to control whether a debugging version of instructions and operands
14-
* should be printed as part of the `toString` output of dataflow nodes.
15-
*
16-
* To enable debug printing import the `DebugPrinting.ql` file. By default,
17-
* non-debug output will be used.
18-
*/
19-
class Node0ToString extends Unit {
20-
abstract predicate isDebugMode();
21-
22-
private string normalInstructionToString(Instruction i) {
23-
not this.isDebugMode() and
24-
if i.(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable
25-
then result = "this"
26-
else result = i.getAst().toString()
27-
}
28-
29-
private string normalOperandToString(Operand op) {
30-
not this.isDebugMode() and
31-
if op.getDef().(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable
32-
then result = "this"
33-
else result = op.getDef().getAst().toString()
34-
}
35-
36-
/**
37-
* Gets the string that should be used by `InstructionNode.toString`
38-
*/
39-
string instructionToString(Instruction i) {
40-
if this.isDebugMode()
41-
then result = i.getDumpString()
42-
else result = this.normalInstructionToString(i)
43-
}
44-
45-
/**
46-
* Gets the string that should be used by `OperandNode.toString`.
47-
*/
48-
string operandToString(Operand op) {
49-
if this.isDebugMode()
50-
then result = op.getDumpString() + " @ " + op.getUse().getResultId()
51-
else result = this.normalOperandToString(op)
52-
}
53-
}
54-
55-
private class NoDebugNode0ToString extends Node0ToString {
56-
final override predicate isDebugMode() { none() }
57-
}
58-
59-
/**
60-
* Gets the string that should be used by `OperandNode.toString`.
61-
*/
62-
string operandToString(Operand op) { result = any(Node0ToString nts).operandToString(op) }
63-
64-
/**
65-
* Gets the string that should be used by `InstructionNode.toString`
66-
*/
67-
string instructionToString(Instruction i) { result = any(Node0ToString nts).instructionToString(i) }
68-
69-
/**
70-
* Holds if debugging mode is enabled.
71-
*
72-
* In debug mode the `toString` on dataflow nodes is more expensive to compute,
73-
* but gives more precise information about the different dataflow nodes.
74-
*/
75-
predicate isDebugMode() { any(Node0ToString nts).isDebugMode() }
8+
import NormalNode0ToString
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* This file contains the signature module for controlling the behavior of `Node.toString`.
3+
*/
4+
5+
private import semmle.code.cpp.ir.IR
6+
private import codeql.util.Unit
7+
private import DataFlowUtil
8+
9+
/** A signature for a module to control the behavior of `Node.toString`. */
10+
signature module Node0ToStringSig {
11+
/**
12+
* Gets the string that should be used by `OperandNode.toString`.
13+
*/
14+
string operandToString(Operand op);
15+
16+
/**
17+
* Gets the string that should be used by `InstructionNode.toString`.
18+
*/
19+
string instructionToString(Instruction i);
20+
21+
/**
22+
* Gets the string representation of the `Expr` associated with `n`, if any.
23+
*/
24+
string toExprString(Node n);
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* This file contains module that implements the non-debug version of
3+
* `toString` for `Instruction` and `Operand` dataflow nodes.
4+
*/
5+
6+
private import semmle.code.cpp.ir.IR
7+
private import codeql.util.Unit
8+
private import Node0ToStringSig
9+
private import DataFlowUtil
10+
private import DataFlowPrivate
11+
12+
private module NormalNode0ToStringImpl implements Node0ToStringSig {
13+
string instructionToString(Instruction i) {
14+
if i.(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable
15+
then result = "this"
16+
else result = i.getAst().toString()
17+
}
18+
19+
string operandToString(Operand op) {
20+
if op.getDef().(InitializeParameterInstruction).getIRVariable() instanceof IRThisVariable
21+
then result = "this"
22+
else result = op.getDef().getAst().toString()
23+
}
24+
25+
string toExprString(Node n) {
26+
result = n.asExpr(0).toString()
27+
or
28+
not exists(n.asExpr()) and
29+
result = stars(n) + n.asIndirectExpr(0, 1).toString()
30+
}
31+
}
32+
33+
import NormalNode0ToStringImpl

0 commit comments

Comments
 (0)