Skip to content

Commit eeaa361

Browse files
authored
Merge pull request github#13907 from MathiasVP/cleanup-ssa-internals
C++: Small cleanup of `SsaInternals`
2 parents e9750af + f2f4e1f commit eeaa361

File tree

4 files changed

+42
-83
lines changed

4 files changed

+42
-83
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ private IRVariable getIRVariableForParameterNode(ParameterNode p) {
10781078

10791079
/** Holds if `v` is the source variable corresponding to the parameter represented by `p`. */
10801080
pragma[nomagic]
1081-
private predicate parameterNodeHasSourceVariable(ParameterNode p, Ssa::SourceIRVariable v) {
1081+
private predicate parameterNodeHasSourceVariable(ParameterNode p, Ssa::SourceVariable v) {
10821082
v.getIRVariable() = getIRVariableForParameterNode(p) and
10831083
exists(Position pos | p.isParameterOf(_, pos) |
10841084
pos instanceof DirectPosition and

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 26 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -10,98 +10,55 @@ private import ssa0.SsaInternals as SsaInternals0
1010
import SsaInternalsCommon
1111

1212
private module SourceVariables {
13-
int getMaxIndirectionForIRVariable(IRVariable var) {
14-
exists(Type type, boolean isGLValue |
15-
var.getLanguageType().hasType(type, isGLValue) and
16-
if isGLValue = true
17-
then result = 1 + getMaxIndirectionsForType(type)
18-
else result = getMaxIndirectionsForType(type)
19-
)
20-
}
21-
2213
cached
2314
private newtype TSourceVariable =
24-
TSourceIRVariable(BaseIRVariable baseVar, int ind) {
25-
ind = [0 .. getMaxIndirectionForIRVariable(baseVar.getIRVariable())]
26-
} or
27-
TCallVariable(AllocationInstruction call, int ind) {
28-
ind = [0 .. countIndirectionsForCppType(getResultLanguageType(call))]
15+
TMkSourceVariable(SsaInternals0::SourceVariable base, int ind) {
16+
ind = [0 .. countIndirectionsForCppType(base.getLanguageType()) + 1]
2917
}
3018

31-
abstract class SourceVariable extends TSourceVariable {
19+
class SourceVariable extends TSourceVariable {
20+
SsaInternals0::SourceVariable base;
3221
int ind;
3322

34-
bindingset[ind]
35-
SourceVariable() { any() }
23+
SourceVariable() { this = TMkSourceVariable(base, ind) }
24+
25+
/** Gets the IR variable associated with this `SourceVariable`, if any. */
26+
IRVariable getIRVariable() { result = base.(BaseIRVariable).getIRVariable() }
27+
28+
/**
29+
* Gets the base source variable (i.e., the variable without any
30+
* indirections) of this source variable.
31+
*/
32+
SsaInternals0::SourceVariable getBaseVariable() { result = base }
3633

3734
/** Gets a textual representation of this element. */
38-
abstract string toString();
35+
string toString() {
36+
ind = 0 and
37+
result = this.getBaseVariable().toString()
38+
or
39+
ind > 0 and
40+
result = this.getBaseVariable().toString() + " indirection"
41+
}
3942

4043
/**
4144
* Gets the number of loads performed on the base source variable
4245
* to reach the value of this source variable.
4346
*/
4447
int getIndirection() { result = ind }
4548

46-
/**
47-
* Gets the base source variable (i.e., the variable without any
48-
* indirections) of this source variable.
49-
*/
50-
abstract BaseSourceVariable getBaseVariable();
51-
5249
/** Holds if this variable is a glvalue. */
53-
predicate isGLValue() { none() }
50+
predicate isGLValue() { ind = 0 }
5451

5552
/**
5653
* Gets the type of this source variable. If `isGLValue()` holds, then
5754
* the type of this source variable should be thought of as "pointer
5855
* to `getType()`".
5956
*/
60-
abstract DataFlowType getType();
61-
}
62-
63-
class SourceIRVariable extends SourceVariable, TSourceIRVariable {
64-
BaseIRVariable var;
65-
66-
SourceIRVariable() { this = TSourceIRVariable(var, ind) }
67-
68-
IRVariable getIRVariable() { result = var.getIRVariable() }
69-
70-
override BaseIRVariable getBaseVariable() { result.getIRVariable() = this.getIRVariable() }
71-
72-
override string toString() {
73-
ind = 0 and
74-
result = this.getIRVariable().toString()
75-
or
76-
ind > 0 and
77-
result = this.getIRVariable().toString() + " indirection"
78-
}
79-
80-
override predicate isGLValue() { ind = 0 }
81-
82-
override DataFlowType getType() {
83-
if ind = 0 then result = var.getType() else result = getTypeImpl(var.getType(), ind - 1)
84-
}
85-
}
86-
87-
class CallVariable extends SourceVariable, TCallVariable {
88-
AllocationInstruction call;
89-
90-
CallVariable() { this = TCallVariable(call, ind) }
91-
92-
AllocationInstruction getCall() { result = call }
93-
94-
override BaseCallVariable getBaseVariable() { result.getCallInstruction() = call }
95-
96-
override string toString() {
97-
ind = 0 and
98-
result = "Call"
99-
or
100-
ind > 0 and
101-
result = "Call indirection"
57+
DataFlowType getType() {
58+
if this.isGLValue()
59+
then result = base.getType()
60+
else result = getTypeImpl(base.getType(), ind - 1)
10261
}
103-
104-
override DataFlowType getType() { result = getTypeImpl(call.getResultType(), ind) }
10562
}
10663
}
10764

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternalsCommon.qll

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,20 @@ newtype TBaseSourceVariable =
370370
// Each allocation gets its own source variable
371371
TBaseCallVariable(AllocationInstruction call)
372372

373-
abstract class BaseSourceVariable extends TBaseSourceVariable {
373+
abstract private class AbstractBaseSourceVariable extends TBaseSourceVariable {
374374
/** Gets a textual representation of this element. */
375375
abstract string toString();
376376

377377
/** Gets the type of this base source variable. */
378-
abstract DataFlowType getType();
378+
final DataFlowType getType() { this.getLanguageType().hasUnspecifiedType(result, _) }
379+
380+
/** Gets the `CppType` of this base source variable. */
381+
abstract CppType getLanguageType();
379382
}
380383

381-
class BaseIRVariable extends BaseSourceVariable, TBaseIRVariable {
384+
final class BaseSourceVariable = AbstractBaseSourceVariable;
385+
386+
class BaseIRVariable extends AbstractBaseSourceVariable, TBaseIRVariable {
382387
IRVariable var;
383388

384389
IRVariable getIRVariable() { result = var }
@@ -387,10 +392,10 @@ class BaseIRVariable extends BaseSourceVariable, TBaseIRVariable {
387392

388393
override string toString() { result = var.toString() }
389394

390-
override DataFlowType getType() { result = var.getType() }
395+
override CppType getLanguageType() { result = var.getLanguageType() }
391396
}
392397

393-
class BaseCallVariable extends BaseSourceVariable, TBaseCallVariable {
398+
class BaseCallVariable extends AbstractBaseSourceVariable, TBaseCallVariable {
394399
AllocationInstruction call;
395400

396401
BaseCallVariable() { this = TBaseCallVariable(call) }
@@ -399,7 +404,7 @@ class BaseCallVariable extends BaseSourceVariable, TBaseCallVariable {
399404

400405
override string toString() { result = call.toString() }
401406

402-
override DataFlowType getType() { result = call.getResultType() }
407+
override CppType getLanguageType() { result = getResultLanguageType(call) }
403408
}
404409

405410
/**

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/ssa0/SsaInternals.qll

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ private import semmle.code.cpp.ir.dataflow.internal.DataFlowUtil
1515
private import semmle.code.cpp.ir.dataflow.internal.SsaInternalsCommon
1616

1717
private module SourceVariables {
18-
class SourceVariable instanceof BaseSourceVariable {
19-
string toString() { result = BaseSourceVariable.super.toString() }
20-
18+
class SourceVariable extends BaseSourceVariable {
19+
/**
20+
* Gets the base source variable of this `SourceVariable`.
21+
*/
2122
BaseSourceVariable getBaseVariable() { result = this }
2223
}
23-
24-
class SourceIRVariable = BaseIRVariable;
25-
26-
class CallVariable = BaseCallVariable;
2724
}
2825

2926
import SourceVariables

0 commit comments

Comments
 (0)