Skip to content

Commit 291f1a1

Browse files
committed
C++: Unify the two branches.
1 parent 6e6e118 commit 291f1a1

File tree

4 files changed

+37
-74
lines changed

4 files changed

+37
-74
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: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,87 +12,52 @@ import SsaInternalsCommon
1212
private module SourceVariables {
1313
cached
1414
private newtype TSourceVariable =
15-
TSourceIRVariable(BaseIRVariable baseVar, int ind) {
16-
ind = [0 .. countIndirectionsForCppType(baseVar.getIRVariable().getLanguageType()) + 1]
17-
} or
18-
TCallVariable(AllocationInstruction call, int ind) {
19-
ind = [0 .. countIndirectionsForCppType(getResultLanguageType(call))]
15+
TMkSourceVariable(SsaInternals0::SourceVariable base, int ind) {
16+
ind = [0 .. countIndirectionsForCppType(base.getLanguageType()) + 1]
2017
}
2118

22-
abstract class SourceVariable extends TSourceVariable {
19+
class SourceVariable extends TSourceVariable {
20+
SsaInternals0::SourceVariable base;
2321
int ind;
2422

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

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

3142
/**
3243
* Gets the number of loads performed on the base source variable
3344
* to reach the value of this source variable.
3445
*/
3546
int getIndirection() { result = ind }
3647

37-
/**
38-
* Gets the base source variable (i.e., the variable without any
39-
* indirections) of this source variable.
40-
*/
41-
abstract BaseSourceVariable getBaseVariable();
42-
4348
/** Holds if this variable is a glvalue. */
44-
predicate isGLValue() { none() }
49+
predicate isGLValue() { ind = 0 }
4550

4651
/**
4752
* Gets the type of this source variable. If `isGLValue()` holds, then
4853
* the type of this source variable should be thought of as "pointer
4954
* to `getType()`".
5055
*/
51-
abstract DataFlowType getType();
52-
}
53-
54-
class SourceIRVariable extends SourceVariable, TSourceIRVariable {
55-
BaseIRVariable var;
56-
57-
SourceIRVariable() { this = TSourceIRVariable(var, ind) }
58-
59-
IRVariable getIRVariable() { result = var.getIRVariable() }
60-
61-
override BaseIRVariable getBaseVariable() { result.getIRVariable() = this.getIRVariable() }
62-
63-
override string toString() {
64-
ind = 0 and
65-
result = this.getIRVariable().toString()
66-
or
67-
ind > 0 and
68-
result = this.getIRVariable().toString() + " indirection"
56+
DataFlowType getType() {
57+
if this.isGLValue()
58+
then result = base.getType()
59+
else result = getTypeImpl(base.getType(), ind - 1)
6960
}
70-
71-
override predicate isGLValue() { ind = 0 }
72-
73-
override DataFlowType getType() {
74-
if ind = 0 then result = var.getType() else result = getTypeImpl(var.getType(), ind - 1)
75-
}
76-
}
77-
78-
class CallVariable extends SourceVariable, TCallVariable {
79-
AllocationInstruction call;
80-
81-
CallVariable() { this = TCallVariable(call, ind) }
82-
83-
AllocationInstruction getCall() { result = call }
84-
85-
override BaseCallVariable getBaseVariable() { result.getCallInstruction() = call }
86-
87-
override string toString() {
88-
ind = 0 and
89-
result = "Call"
90-
or
91-
ind > 0 and
92-
result = "Call indirection"
93-
}
94-
95-
override DataFlowType getType() { result = getTypeImpl(call.getResultType(), ind) }
9661
}
9762
}
9863

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,19 @@ 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+
abstract CppType getLanguageType();
379381
}
380382

381-
class BaseIRVariable extends BaseSourceVariable, TBaseIRVariable {
383+
final class BaseSourceVariable = AbstractBaseSourceVariable;
384+
385+
class BaseIRVariable extends AbstractBaseSourceVariable, TBaseIRVariable {
382386
IRVariable var;
383387

384388
IRVariable getIRVariable() { result = var }
@@ -387,10 +391,10 @@ class BaseIRVariable extends BaseSourceVariable, TBaseIRVariable {
387391

388392
override string toString() { result = var.toString() }
389393

390-
override DataFlowType getType() { result = var.getType() }
394+
override CppType getLanguageType() { result = var.getLanguageType() }
391395
}
392396

393-
class BaseCallVariable extends BaseSourceVariable, TBaseCallVariable {
397+
class BaseCallVariable extends AbstractBaseSourceVariable, TBaseCallVariable {
394398
AllocationInstruction call;
395399

396400
BaseCallVariable() { this = TBaseCallVariable(call) }
@@ -399,7 +403,7 @@ class BaseCallVariable extends BaseSourceVariable, TBaseCallVariable {
399403

400404
override string toString() { result = call.toString() }
401405

402-
override DataFlowType getType() { result = call.getResultType() }
406+
override CppType getLanguageType() { result = getResultLanguageType(call) }
403407
}
404408

405409
/**

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,9 @@ 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 {
2119
BaseSourceVariable getBaseVariable() { result = this }
2220
}
23-
24-
class SourceIRVariable = BaseIRVariable;
25-
26-
class CallVariable = BaseCallVariable;
2721
}
2822

2923
import SourceVariables

0 commit comments

Comments
 (0)