Skip to content

Commit 5a91aa2

Browse files
committed
C++: Expose SSA definitions from dataflow.
1 parent 1dae787 commit 5a91aa2

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,3 +2576,13 @@ Function getARuntimeTarget(Call call) {
25762576
result = DataFlowImplCommon::viableCallableLambda(dfCall, _).asSourceCallable()
25772577
)
25782578
}
2579+
2580+
class Definition = Ssa::Definition;
2581+
2582+
class ExplicitDefinition = Ssa::ExplicitDefinition;
2583+
2584+
class DirectExplicitDefinition = Ssa::DirectExplicitDefinition;
2585+
2586+
class IndirectExplicitDefinition = Ssa::IndirectExplicitDefinition;
2587+
2588+
class PhiNode = Ssa::PhiNode;

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,15 @@ class Definition extends SsaImpl::Definition {
11401140
not result instanceof PhiNode
11411141
}
11421142

1143+
/** Gets a `Node` that represents a use of this definition. */
1144+
Node getAUse() {
1145+
exists(SourceVariable sv, IRBlock bb, int i, UseImpl use |
1146+
ssaDefReachesRead(sv, this, bb, i) and
1147+
use.hasIndexInBlock(bb, i, sv) and
1148+
result = use.getNode()
1149+
)
1150+
}
1151+
11431152
/**
11441153
* INTERNAL: Do not use.
11451154
*/
@@ -1172,4 +1181,54 @@ class Definition extends SsaImpl::Definition {
11721181
Type getUnspecifiedType() { result = this.getUnderlyingType().getUnspecifiedType() }
11731182
}
11741183

1184+
/**
1185+
* An SSA definition that corresponds to an explicit definition.
1186+
*/
1187+
class ExplicitDefinition extends Definition, SsaImpl::WriteDefinition {
1188+
DefImpl def;
1189+
1190+
ExplicitDefinition() {
1191+
exists(IRBlock bb, int i, SourceVariable sv |
1192+
this.definesAt(sv, bb, i) and
1193+
def.hasIndexInBlock(sv, bb, i)
1194+
)
1195+
}
1196+
1197+
/**
1198+
* Gets the `Node` computing the value that is written by this SSA definition.
1199+
*/
1200+
Node getAssignedValue() { result.asInstruction() = def.getValue().asInstruction() }
1201+
}
1202+
1203+
/**
1204+
* An explicit SSA definition that writes an indirect value to a pointer.
1205+
*
1206+
* For example in:
1207+
* ```cpp
1208+
* int x = 42; // (1)
1209+
* int* p = &x; // (2)
1210+
* ```
1211+
* There are three `ExplicitDefinition`:
1212+
* 1. A `DirectExplicitDefinition` at (1) which writes `42` to the SSA variable
1213+
* corresponding to `x`.
1214+
* 2. A `DirectExplicitDefinition` at (2) which writes `&x` to the SSA variable
1215+
* corresponding to `p`.
1216+
* 3. A `IndirectExplicitDefinition` at (2) which writes `*&x` (i.e., `x`) to
1217+
* the SSA vairable corresponding to `*p`.
1218+
*/
1219+
class IndirectExplicitDefinition extends ExplicitDefinition {
1220+
IndirectExplicitDefinition() { this.getIndirectionIndex() > 0 }
1221+
}
1222+
1223+
/**
1224+
* An SSA definition that corresponds to an explicit definition.
1225+
*
1226+
* Unlike `ExplicitDefinition` this class does not include indirect
1227+
* explicit definition. See `IndirectExplicitDefinition` if you want to include
1228+
* those.
1229+
*/
1230+
class DirectExplicitDefinition extends ExplicitDefinition {
1231+
DirectExplicitDefinition() { this.getIndirectionIndex() = 0 }
1232+
}
1233+
11751234
import SsaCached

0 commit comments

Comments
 (0)