Skip to content

Commit 53b1068

Browse files
committed
C++: Unshare code between assignment types
This commit undoes the code sharing between `TranslatedAssignExpr` (`=`) and `TranslatedAssignOperation` (`+=`, `<<=`, ...). In the next commit, when we change how the `Load` works on the LHS of `TranslatedAssignOperation`, these classes will become so different that sharing is no longer helpful.
1 parent c5950d2 commit 53b1068

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

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

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,8 +1224,8 @@ class TranslatedBinaryOperation extends TranslatedSingleInstructionExpr {
12241224
}
12251225
}
12261226

1227-
abstract class TranslatedAssignment extends TranslatedNonConstantExpr {
1228-
override Assignment expr;
1227+
class TranslatedAssignExpr extends TranslatedNonConstantExpr {
1228+
override AssignExpr expr;
12291229

12301230
final override TranslatedElement getChild(int id) {
12311231
id = 0 and result = getLeftOperand()
@@ -1245,7 +1245,7 @@ abstract class TranslatedAssignment extends TranslatedNonConstantExpr {
12451245
// value assigned to the left operand. If this is C++, then the result is
12461246
// an lvalue, but that lvalue is being loaded as part of this expression.
12471247
// EDG doesn't mark this as a load.
1248-
result = getStoredValue()
1248+
result = getRightOperand().getResult()
12491249
else
12501250
// This is C++, where the result is an lvalue for the left operand,
12511251
// and that lvalue is not being loaded as part of this expression.
@@ -1261,10 +1261,6 @@ abstract class TranslatedAssignment extends TranslatedNonConstantExpr {
12611261
final TranslatedExpr getRightOperand() {
12621262
result = getTranslatedExpr(expr.getRValue().getFullyConverted())
12631263
}
1264-
}
1265-
1266-
class TranslatedAssignExpr extends TranslatedAssignment {
1267-
TranslatedAssignExpr() { expr instanceof AssignExpr }
12681264

12691265
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
12701266
tag = AssignmentStoreTag() and
@@ -1297,13 +1293,44 @@ class TranslatedAssignExpr extends TranslatedAssignment {
12971293
result = getRightOperand().getResult()
12981294
)
12991295
}
1300-
1301-
override Instruction getStoredValue() { result = getRightOperand().getResult() }
13021296
}
13031297

1304-
class TranslatedAssignOperation extends TranslatedAssignment {
1298+
class TranslatedAssignOperation extends TranslatedNonConstantExpr {
13051299
override AssignOperation expr;
13061300

1301+
final override TranslatedElement getChild(int id) {
1302+
id = 0 and result = getLeftOperand()
1303+
or
1304+
id = 1 and result = getRightOperand()
1305+
}
1306+
1307+
final override Instruction getFirstInstruction() {
1308+
// Evaluation is right-to-left
1309+
result = getRightOperand().getFirstInstruction()
1310+
}
1311+
1312+
final override Instruction getResult() {
1313+
if expr.isPRValueCategory()
1314+
then
1315+
// If this is C, then the result of an assignment is a prvalue for the new
1316+
// value assigned to the left operand. If this is C++, then the result is
1317+
// an lvalue, but that lvalue is being loaded as part of this expression.
1318+
// EDG doesn't mark this as a load.
1319+
result = getStoredValue()
1320+
else
1321+
// This is C++, where the result is an lvalue for the left operand,
1322+
// and that lvalue is not being loaded as part of this expression.
1323+
result = getLeftOperand().getResult()
1324+
}
1325+
1326+
final TranslatedExpr getLeftOperand() {
1327+
result = getTranslatedExpr(expr.getLValue().getFullyConverted())
1328+
}
1329+
1330+
final TranslatedExpr getRightOperand() {
1331+
result = getTranslatedExpr(expr.getRValue().getFullyConverted())
1332+
}
1333+
13071334
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
13081335
kind instanceof GotoEdge and
13091336
(
@@ -1341,7 +1368,7 @@ class TranslatedAssignOperation extends TranslatedAssignment {
13411368
result = getInstruction(AssignOperationLoadTag())
13421369
}
13431370

1344-
override Instruction getStoredValue() {
1371+
private Instruction getStoredValue() {
13451372
if leftOperandNeedsConversion()
13461373
then result = getInstruction(AssignOperationConvertResultTag())
13471374
else result = getInstruction(AssignOperationOpTag())

0 commit comments

Comments
 (0)