Skip to content

Commit c9fbbfe

Browse files
authored
Merge pull request #984 from rdmarsh2/rdmarsh/cpp/ir-stmtexpr
C++: add support for GNU StmtExpr in IR
2 parents 97a9954 + fd7512c commit c9fbbfe

File tree

7 files changed

+1232
-1086
lines changed

7 files changed

+1232
-1086
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ private import TranslatedDeclarationEntry
88
private import TranslatedElement
99
private import TranslatedFunction
1010
private import TranslatedInitialization
11+
private import TranslatedFunction
12+
private import TranslatedStmt
1113
import TranslatedCall
1214

1315
/**
@@ -2727,3 +2729,53 @@ class TranslatedLambdaExpr extends TranslatedNonConstantExpr, InitializationCont
27272729
result = getTranslatedInitialization(expr.getChild(0).getFullyConverted())
27282730
}
27292731
}
2732+
2733+
/**
2734+
* The IR translation of `StmtExpr` (the GNU statement expression extension to C/C++), such as
2735+
* ``` ({ doSomething(); a + b; })```
2736+
*/
2737+
class TranslatedStmtExpr extends TranslatedNonConstantExpr {
2738+
override StmtExpr expr;
2739+
2740+
override final Instruction getFirstInstruction() {
2741+
result = getStmt().getFirstInstruction()
2742+
}
2743+
2744+
override final TranslatedElement getChild(int id) {
2745+
id = 0 and result = getStmt()
2746+
}
2747+
2748+
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) {
2749+
tag instanceof OnlyInstructionTag and
2750+
kind instanceof GotoEdge and
2751+
result = getParent().getChildSuccessor(this)
2752+
}
2753+
2754+
override Instruction getChildSuccessor(TranslatedElement child) {
2755+
child = getStmt() and
2756+
result = getInstruction(OnlyInstructionTag())
2757+
}
2758+
2759+
override predicate hasInstruction(Opcode opcode, InstructionTag tag, Type resultType,
2760+
boolean isGLValue) {
2761+
opcode instanceof Opcode::CopyValue and
2762+
tag instanceof OnlyInstructionTag and
2763+
resultType = expr.getType() and
2764+
isGLValue = false
2765+
}
2766+
2767+
override Instruction getResult() {
2768+
result = getInstruction(OnlyInstructionTag())
2769+
}
2770+
2771+
override Instruction getInstructionOperand(InstructionTag tag,
2772+
OperandTag operandTag) {
2773+
tag instanceof OnlyInstructionTag and
2774+
operandTag instanceof UnaryOperandTag and
2775+
result = getTranslatedExpr(expr.getResultExpr().getFullyConverted()).getResult()
2776+
}
2777+
2778+
TranslatedStmt getStmt() {
2779+
result = getTranslatedStmt(expr.getStmt())
2780+
}
2781+
}

cpp/ql/test/library-tests/dataflow/dataflow-tests/test_diff.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
| test.cpp:484:18:484:23 | test.cpp:485:8:485:12 | AST only |
2424
| test.cpp:489:7:489:11 | test.cpp:491:8:491:12 | AST only |
2525
| test.cpp:490:18:490:22 | test.cpp:491:8:491:12 | AST only |
26-
| test.cpp:497:26:497:32 | test.cpp:498:9:498:22 | AST only |
27-
| test.cpp:497:26:497:32 | test.cpp:509:8:509:12 | AST only |
2826
| true_upon_entry.cpp:9:11:9:16 | true_upon_entry.cpp:13:8:13:8 | IR only |
2927
| true_upon_entry.cpp:62:11:62:16 | true_upon_entry.cpp:66:8:66:8 | IR only |
3028
| true_upon_entry.cpp:98:11:98:16 | true_upon_entry.cpp:105:8:105:8 | IR only |

cpp/ql/test/library-tests/dataflow/dataflow-tests/test_ir.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
| test.cpp:314:12:314:12 | Load: x | test.cpp:313:22:313:27 | Call: call to source |
2828
| test.cpp:337:14:337:14 | Load: x | test.cpp:353:17:353:22 | Call: call to source |
2929
| test.cpp:366:7:366:7 | Load: x | test.cpp:362:4:362:9 | Call: call to source |
30+
| test.cpp:498:9:498:22 | CopyValue: (statement expression) | test.cpp:497:26:497:32 | InitializeParameter: source1 |
31+
| test.cpp:509:8:509:12 | Load: local | test.cpp:497:26:497:32 | InitializeParameter: source1 |
3032
| true_upon_entry.cpp:13:8:13:8 | Load: x | true_upon_entry.cpp:9:11:9:16 | Call: call to source |
3133
| true_upon_entry.cpp:21:8:21:8 | Load: x | true_upon_entry.cpp:17:11:17:16 | Call: call to source |
3234
| true_upon_entry.cpp:29:8:29:8 | Load: x | true_upon_entry.cpp:27:9:27:14 | Call: call to source |

0 commit comments

Comments
 (0)