Skip to content

Commit 0e32639

Browse files
author
AndreiDiaconu1
committed
Address PR comments
1 parent f589033 commit 0e32639

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/internal/TranslatedStmt.qll

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,3 +1042,37 @@ class TranslatedUsingBlockStmt extends TranslatedStmt {
10421042

10431043
private TranslatedStmt getBody() { result = getTranslatedStmt(stmt.getBody()) }
10441044
}
1045+
1046+
// TODO: Should be modeled using the desugaring framework for a
1047+
// more exact translation.
1048+
class TranslatedUsingDeclStmt extends TranslatedStmt {
1049+
override UsingDeclStmt stmt;
1050+
1051+
override TranslatedElement getChild(int id) { result = getDecl(id) }
1052+
1053+
override Instruction getFirstInstruction() { result = this.getDecl(0).getFirstInstruction() }
1054+
1055+
override Instruction getChildSuccessor(TranslatedElement child) {
1056+
exists(int id |
1057+
child = this.getDecl(id) and
1058+
id < this.noDecls() - 1 and
1059+
result = this.getDecl(id + 1).getFirstInstruction()
1060+
)
1061+
or
1062+
child = this.getDecl(this.noDecls() - 1) and result = this.getParent().getChildSuccessor(this)
1063+
}
1064+
1065+
override predicate hasInstruction(
1066+
Opcode opcode, InstructionTag tag, Type resultType, boolean isLValue
1067+
) {
1068+
none()
1069+
}
1070+
1071+
override Instruction getInstructionSuccessor(InstructionTag tag, EdgeKind kind) { none() }
1072+
1073+
private TranslatedLocalDeclaration getDecl(int id) {
1074+
result = getTranslatedLocalDeclaration(stmt.getVariableDeclExpr(id))
1075+
}
1076+
1077+
private int noDecls() { result = count(stmt.getAVariableDeclExpr()) }
1078+
}

csharp/ql/test/library-tests/ir/ir/raw_ir.expected

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,15 +1327,15 @@ stmts.cs:
13271327
# 72| mu3_15(Int32) = Store : &:r3_11, r3_14
13281328
#-----| Goto (back edge) -> Block 2
13291329

1330-
# 76| Block 4
1331-
# 76| r4_0(glval<Int32>) = VariableAddress[a] :
1332-
# 76| mu4_1(Int32) = Uninitialized[a] : &:r4_0
1333-
# 76| r4_2(glval<Int32>) = VariableAddress[b] :
1334-
# 76| r4_3(Int32) = Constant[10] :
1335-
# 76| mu4_4(Int32) = Store : &:r4_2, r4_3
1336-
# 77| r4_5(Int32) = Constant[0] :
1337-
# 77| r4_6(glval<Int32>) = VariableAddress[a] :
1338-
# 77| mu4_7(Int32) = Store : &:r4_6, r4_5
1330+
# 77| Block 4
1331+
# 77| r4_0(glval<Int32>) = VariableAddress[a] :
1332+
# 77| mu4_1(Int32) = Uninitialized[a] : &:r4_0
1333+
# 77| r4_2(glval<Int32>) = VariableAddress[b] :
1334+
# 77| r4_3(Int32) = Constant[10] :
1335+
# 77| mu4_4(Int32) = Store : &:r4_2, r4_3
1336+
# 78| r4_5(Int32) = Constant[0] :
1337+
# 78| r4_6(glval<Int32>) = VariableAddress[a] :
1338+
# 78| mu4_7(Int32) = Store : &:r4_6, r4_5
13391339
#-----| Goto -> Block 5
13401340

13411341
# 78| Block 5
@@ -1476,9 +1476,20 @@ using.cs:
14761476
# 22| r0_22(glval<null>) = FunctionAddress[DoSomething] :
14771477
# 22| v0_23(Void) = Call : func:r0_22, this:r0_21
14781478
# 22| mu0_24(null) = ^CallSideEffect : ~mu0_2
1479-
# 12| v0_25(Void) = ReturnVoid :
1480-
# 12| v0_26(Void) = UnmodeledUse : mu*
1481-
# 12| v0_27(Void) = ExitFunction :
1479+
# 25| r0_25(glval<MyDisposable>) = VariableAddress[o3] :
1480+
# 25| r0_26(MyDisposable) = NewObj :
1481+
# 25| r0_27(glval<null>) = FunctionAddress[MyDisposable] :
1482+
# 25| v0_28(Void) = Call : func:r0_27, this:r0_26
1483+
# 25| mu0_29(null) = ^CallSideEffect : ~mu0_2
1484+
# 25| mu0_30(MyDisposable) = Store : &:r0_25, r0_26
1485+
# 26| r0_31(glval<MyDisposable>) = VariableAddress[o3] :
1486+
# 26| r0_32(MyDisposable) = Load : &:r0_31, ~mu0_2
1487+
# 26| r0_33(glval<null>) = FunctionAddress[DoSomething] :
1488+
# 26| v0_34(Void) = Call : func:r0_33, this:r0_32
1489+
# 26| mu0_35(null) = ^CallSideEffect : ~mu0_2
1490+
# 12| v0_36(Void) = ReturnVoid :
1491+
# 12| v0_37(Void) = UnmodeledUse : mu*
1492+
# 12| v0_38(Void) = ExitFunction :
14821493

14831494
variables.cs:
14841495
# 5| System.Void test_variables.f()

csharp/ql/test/library-tests/ir/ir/using.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,10 @@ static void Main()
2121
{
2222
o2.DoSomething();
2323
}
24+
25+
using var o3 = new MyDisposable();
26+
o3.DoSomething();
2427
}
2528
}
29+
30+
// semmle-extractor-options: /langversion:preview

0 commit comments

Comments
 (0)