Skip to content

Commit 4e62163

Browse files
authored
Merge pull request #1935 from AndreiDiaconu1/ircsharp-forinitfix
C# IR: Fix for init
2 parents e6b748a + fcb3d99 commit 4e62163

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,10 @@ class TranslatedForStmt extends TranslatedLoop {
660660
id = initializersNo() + updatesNo() + 1 and result = this.getBody()
661661
}
662662

663-
private TranslatedLocalDeclaration getDeclAndInit(int index) {
664-
result = getTranslatedLocalDeclaration(stmt.getInitializer(index))
663+
private TranslatedElement getDeclAndInit(int index) {
664+
if stmt.getInitializer(index) instanceof LocalVariableDeclExpr
665+
then result = getTranslatedLocalDeclaration(stmt.getInitializer(index))
666+
else result = getTranslatedExpr(stmt.getInitializer(index))
665667
}
666668

667669
private predicate hasInitialization() { exists(stmt.getAnInitializer()) }

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,11 +1263,13 @@ stmts.cs:
12631263

12641264
# 76| Block 4
12651265
# 76| r4_0(glval<Int32>) = VariableAddress[a] :
1266-
# 76| r4_1(Int32) = Constant[0] :
1267-
# 76| mu4_2(Int32) = Store : &:r4_0, r4_1
1268-
# 76| r4_3(glval<Int32>) = VariableAddress[b] :
1269-
# 76| r4_4(Int32) = Constant[10] :
1270-
# 76| mu4_5(Int32) = Store : &:r4_3, r4_4
1266+
# 76| mu4_1(Int32) = Uninitialized[a] : &:r4_0
1267+
# 76| r4_2(glval<Int32>) = VariableAddress[b] :
1268+
# 76| r4_3(Int32) = Constant[10] :
1269+
# 76| mu4_4(Int32) = Store : &:r4_2, r4_3
1270+
# 77| r4_5(Int32) = Constant[0] :
1271+
# 77| r4_6(glval<Int32>) = VariableAddress[a] :
1272+
# 77| mu4_7(Int32) = Store : &:r4_6, r4_5
12711273
#-----| Goto -> Block 5
12721274

12731275
# 77| Block 5

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public static void forStmt()
7373
x = x - 1;
7474
}
7575

76-
int a = 0, b = 10;
77-
for (; a < b; )
76+
int a, b = 10;
77+
for (a = 0; a < b; )
7878
{
7979
a++;
8080
}

0 commit comments

Comments
 (0)