Skip to content

Commit 19e1d82

Browse files
authored
Merge pull request #2686 from jbj/ir-crement-load
C++: Move the LoadInstruction from `++` to `e` in `e++`.
2 parents 90f94e2 + 91927c9 commit 19e1d82

File tree

12 files changed

+288
-211
lines changed

12 files changed

+288
-211
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/InstructionTag.qll

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ newtype TInstructionTag =
1212
ZeroPadStringElementIndexTag() or
1313
ZeroPadStringElementAddressTag() or
1414
ZeroPadStringStoreTag() or
15-
AssignOperationLoadTag() or
1615
AssignOperationConvertLeftTag() or
1716
AssignOperationOpTag() or
1817
AssignOperationConvertResultTag() or
1918
AssignmentStoreTag() or
20-
CrementLoadTag() or
2119
CrementConstantTag() or
2220
CrementOpTag() or
2321
CrementStoreTag() or
@@ -95,8 +93,6 @@ string getInstructionTagId(TInstructionTag tag) {
9593
or
9694
tag = ZeroPadStringStoreTag() and result = "ZeroPadStore"
9795
or
98-
tag = AssignOperationLoadTag() and result = "AssignOpLoad"
99-
or
10096
tag = AssignOperationConvertLeftTag() and result = "AssignOpConvLeft"
10197
or
10298
tag = AssignOperationOpTag() and result = "AssignOpOp"
@@ -105,8 +101,6 @@ string getInstructionTagId(TInstructionTag tag) {
105101
or
106102
tag = AssignmentStoreTag() and result = "AssignStore"
107103
or
108-
tag = CrementLoadTag() and result = "CrementLoad"
109-
or
110104
tag = CrementConstantTag() and result = "CrementConst"
111105
or
112106
tag = CrementOpTag() and result = "CrementOp"

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private predicate usedAsCondition(Expr expr) {
208208
* AST as an lvalue-to-rvalue conversion, but the IR represents both a function
209209
* lvalue and a function pointer prvalue the same.
210210
*/
211-
predicate ignoreLoad(Expr expr) {
211+
private predicate ignoreLoad(Expr expr) {
212212
expr.hasLValueToRValueConversion() and
213213
(
214214
expr instanceof ThisExpr or
@@ -220,6 +220,34 @@ predicate ignoreLoad(Expr expr) {
220220
)
221221
}
222222

223+
/**
224+
* Holds if `expr` should have a load on it because it will be loaded as part
225+
* of the translation of its parent. We want to associate this load with `expr`
226+
* itself rather than its parent since in practical applications like data flow
227+
* we maintain that the value of the `x` in `x++` should be what's loaded from
228+
* `x`.
229+
*/
230+
private predicate needsLoadForParentExpr(Expr expr) {
231+
exists(CrementOperation crement | expr = crement.getOperand().getFullyConverted())
232+
or
233+
exists(AssignOperation ao | expr = ao.getLValue().getFullyConverted())
234+
}
235+
236+
/**
237+
* Holds if `expr` should have a `TranslatedLoad` on it.
238+
*/
239+
predicate hasTranslatedLoad(Expr expr) {
240+
(
241+
expr.hasLValueToRValueConversion()
242+
or
243+
needsLoadForParentExpr(expr)
244+
) and
245+
not ignoreExpr(expr) and
246+
not isNativeCondition(expr) and
247+
not isFlexibleCondition(expr) and
248+
not ignoreLoad(expr)
249+
}
250+
223251
newtype TTranslatedElement =
224252
// An expression that is not being consumed as a condition
225253
TTranslatedValueExpr(Expr expr) {
@@ -229,21 +257,12 @@ newtype TTranslatedElement =
229257
} or
230258
// A separate element to handle the lvalue-to-rvalue conversion step of an
231259
// expression.
232-
TTranslatedLoad(Expr expr) {
233-
not ignoreExpr(expr) and
234-
not isNativeCondition(expr) and
235-
not isFlexibleCondition(expr) and
236-
expr.hasLValueToRValueConversion() and
237-
not ignoreLoad(expr)
238-
} or
260+
TTranslatedLoad(Expr expr) { hasTranslatedLoad(expr) } or
261+
// For expressions that would not otherwise generate an instruction.
239262
TTranslatedResultCopy(Expr expr) {
240263
not ignoreExpr(expr) and
241264
exprNeedsCopyIfNotLoaded(expr) and
242-
// Doesn't have a TTranslatedLoad
243-
not (
244-
expr.hasLValueToRValueConversion() and
245-
not ignoreLoad(expr)
246-
)
265+
not hasTranslatedLoad(expr)
247266
} or
248267
// An expression most naturally translated as control flow.
249268
TTranslatedNativeCondition(Expr expr) {

0 commit comments

Comments
 (0)