Skip to content

Commit 47120bc

Browse files
author
AndreiDiaconu1
committed
PR fixes
1 parent 420abbf commit 47120bc

4 files changed

Lines changed: 117 additions & 81 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private import semmle.code.csharp.ir.internal.IRCSharpLanguage as Language
1010

1111
/**
1212
* The IR translation of a call to a function. The function can be a normal function
13-
* (eg. `MethodCall`) or a constructor call (eg. `ObjectCreation`). Notice that the
13+
* (e.g. `MethodCall`) or a constructor call (e.g. `ObjectCreation`). Notice that the
1414
* AST generated translated calls are tied to an expression (unlike compiler generated ones,
1515
* which can be attached to either a statement or an expression).
1616
*/

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,14 +768,20 @@ class TranslatedContinueStmt extends TranslatedSpecificJump {
768768
}
769769

770770
private Instruction getEnclosingLoopTargetInstruction(Stmt crtStmt) {
771-
if crtStmt instanceof LoopStmt
772-
then
773-
if crtStmt instanceof ForStmt
774-
then
775-
result = getTranslatedStmt(crtStmt).(TranslatedForStmt).getUpdate(0).getFirstInstruction()
776-
else result = getTranslatedStmt(crtStmt).getFirstInstruction()
771+
if crtStmt instanceof ForStmt
772+
then result = getNextForInstruction(crtStmt)
773+
else if crtStmt instanceof LoopStmt
774+
then result = getTranslatedStmt(crtStmt).getFirstInstruction()
777775
else result = this.getEnclosingLoopTargetInstruction(crtStmt.getParent())
778776
}
777+
778+
private Instruction getNextForInstruction(ForStmt for) {
779+
if exists(for.getUpdate(0))
780+
then result = getTranslatedStmt(for).(TranslatedForStmt).getUpdate(0).getFirstInstruction()
781+
else if exists(for.getCondition())
782+
then result = getTranslatedStmt(for).(TranslatedForStmt).getCondition().getFirstInstruction()
783+
else result = getTranslatedStmt(for).(TranslatedForStmt).getBody().getFirstInstruction()
784+
}
779785
}
780786

781787
class TranslatedGotoLabelStmt extends TranslatedSpecificJump {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public static void Main()
1313
Console.WriteLine("BreakAndContinue");
1414
}
1515

16+
for (int i = 0 ; i < 10 ; )
17+
{
18+
i++;
19+
continue;
20+
}
21+
1622
int a = 0;
1723
while (true)
1824
{

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

Lines changed: 98 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -657,93 +657,94 @@ jumps.cs:
657657
#-----| Goto -> Block 19
658658

659659
# 16| Block 7
660-
# 16| r7_0(glval<Int32>) = VariableAddress[a] :
660+
# 16| r7_0(glval<Int32>) = VariableAddress[i] :
661661
# 16| r7_1(Int32) = Constant[0] :
662662
# 16| mu7_2(Int32) = Store : &:r7_0, r7_1
663663
#-----| Goto -> Block 8
664664

665-
# 17| Block 8
666-
# 17| r8_0(Boolean) = Constant[true] :
667-
# 17| v8_1(Void) = ConditionalBranch : r8_0
668-
#-----| False -> Block 13
665+
# 16| Block 8
666+
# 16| r8_0(glval<Int32>) = VariableAddress[i] :
667+
# 16| r8_1(Int32) = Load : &:r8_0, ~mu0_2
668+
# 16| r8_2(Int32) = Constant[10] :
669+
# 16| r8_3(Boolean) = CompareLT : r8_1, r8_2
670+
# 16| v8_4(Void) = ConditionalBranch : r8_3
671+
#-----| False -> Block 10
669672
#-----| True -> Block 9
670673

671-
# 19| Block 9
672-
# 19| r9_0(glval<Int32>) = VariableAddress[a] :
673-
# 19| r9_1(Int32) = Load : &:r9_0, ~mu0_2
674-
# 19| r9_2(Int32) = Constant[1] :
675-
# 19| r9_3(Int32) = Add : r9_1, r9_2
676-
# 19| mu9_4(Int32) = Store : &:r9_0, r9_3
677-
# 20| r9_5(glval<Int32>) = VariableAddress[a] :
678-
# 20| r9_6(Int32) = Load : &:r9_5, ~mu0_2
679-
# 20| r9_7(Int32) = Constant[5] :
680-
# 20| r9_8(Boolean) = CompareEQ : r9_6, r9_7
681-
# 20| v9_9(Void) = ConditionalBranch : r9_8
682-
#-----| False -> Block 11
683-
#-----| True -> Block 10
684-
685-
# 21| Block 10
686-
# 21| v10_0(Void) = NoOp :
674+
# 18| Block 9
675+
# 18| r9_0(glval<Int32>) = VariableAddress[i] :
676+
# 18| r9_1(Int32) = Load : &:r9_0, ~mu0_2
677+
# 18| r9_2(Int32) = Constant[1] :
678+
# 18| r9_3(Int32) = Add : r9_1, r9_2
679+
# 18| mu9_4(Int32) = Store : &:r9_0, r9_3
680+
# 19| v9_5(Void) = NoOp :
687681
#-----| Goto (back edge) -> Block 8
688682

689-
# 22| Block 11
690-
# 22| r11_0(glval<Int32>) = VariableAddress[a] :
691-
# 22| r11_1(Int32) = Load : &:r11_0, ~mu0_2
692-
# 22| r11_2(Int32) = Constant[10] :
693-
# 22| r11_3(Boolean) = CompareEQ : r11_1, r11_2
694-
# 22| v11_4(Void) = ConditionalBranch : r11_3
695-
#-----| False (back edge) -> Block 8
683+
# 22| Block 10
684+
# 22| r10_0(glval<Int32>) = VariableAddress[a] :
685+
# 22| r10_1(Int32) = Constant[0] :
686+
# 22| mu10_2(Int32) = Store : &:r10_0, r10_1
687+
#-----| Goto -> Block 11
688+
689+
# 23| Block 11
690+
# 23| r11_0(Boolean) = Constant[true] :
691+
# 23| v11_1(Void) = ConditionalBranch : r11_0
692+
#-----| False -> Block 16
696693
#-----| True -> Block 12
697694

698-
# 23| Block 12
699-
# 23| v12_0(Void) = NoOp :
700-
#-----| Goto -> Block 13
701-
702-
# 26| Block 13
703-
# 26| r13_0(glval<Int32>) = VariableAddress[i] :
704-
# 26| r13_1(Int32) = Constant[1] :
705-
# 26| mu13_2(Int32) = Store : &:r13_0, r13_1
706-
#-----| Goto -> Block 15
707-
708-
# 26| Block 14
709-
# 26| r14_0(glval<Int32>) = VariableAddress[i] :
710-
# 26| r14_1(Int32) = Load : &:r14_0, ~mu0_2
711-
# 26| r14_2(Int32) = Constant[1] :
712-
# 26| r14_3(Int32) = Add : r14_1, r14_2
713-
# 26| mu14_4(Int32) = Store : &:r14_0, r14_3
714-
#-----| Goto (back edge) -> Block 15
715-
716-
# 26| Block 15
717-
# 26| r15_0(glval<Int32>) = VariableAddress[i] :
718-
# 26| r15_1(Int32) = Load : &:r15_0, ~mu0_2
719-
# 26| r15_2(Int32) = Constant[10] :
720-
# 26| r15_3(Boolean) = CompareLE : r15_1, r15_2
721-
# 26| v15_4(Void) = ConditionalBranch : r15_3
722-
#-----| False -> Block 18
723-
#-----| True -> Block 16
724-
725-
# 28| Block 16
726-
# 28| r16_0(glval<Int32>) = VariableAddress[i] :
727-
# 28| r16_1(Int32) = Load : &:r16_0, ~mu0_2
728-
# 28| r16_2(Int32) = Constant[5] :
729-
# 28| r16_3(Boolean) = CompareEQ : r16_1, r16_2
730-
# 28| v16_4(Void) = ConditionalBranch : r16_3
695+
# 25| Block 12
696+
# 25| r12_0(glval<Int32>) = VariableAddress[a] :
697+
# 25| r12_1(Int32) = Load : &:r12_0, ~mu0_2
698+
# 25| r12_2(Int32) = Constant[1] :
699+
# 25| r12_3(Int32) = Add : r12_1, r12_2
700+
# 25| mu12_4(Int32) = Store : &:r12_0, r12_3
701+
# 26| r12_5(glval<Int32>) = VariableAddress[a] :
702+
# 26| r12_6(Int32) = Load : &:r12_5, ~mu0_2
703+
# 26| r12_7(Int32) = Constant[5] :
704+
# 26| r12_8(Boolean) = CompareEQ : r12_6, r12_7
705+
# 26| v12_9(Void) = ConditionalBranch : r12_8
731706
#-----| False -> Block 14
732-
#-----| True -> Block 17
733-
734-
# 29| Block 17
735-
# 29| v17_0(Void) = NoOp :
707+
#-----| True -> Block 13
708+
709+
# 27| Block 13
710+
# 27| v13_0(Void) = NoOp :
711+
#-----| Goto (back edge) -> Block 11
712+
713+
# 28| Block 14
714+
# 28| r14_0(glval<Int32>) = VariableAddress[a] :
715+
# 28| r14_1(Int32) = Load : &:r14_0, ~mu0_2
716+
# 28| r14_2(Int32) = Constant[10] :
717+
# 28| r14_3(Boolean) = CompareEQ : r14_1, r14_2
718+
# 28| v14_4(Void) = ConditionalBranch : r14_3
719+
#-----| False (back edge) -> Block 11
720+
#-----| True -> Block 15
721+
722+
# 29| Block 15
723+
# 29| v15_0(Void) = NoOp :
724+
#-----| Goto -> Block 16
725+
726+
# 32| Block 16
727+
# 32| r16_0(glval<Int32>) = VariableAddress[i] :
728+
# 32| r16_1(Int32) = Constant[1] :
729+
# 32| mu16_2(Int32) = Store : &:r16_0, r16_1
736730
#-----| Goto -> Block 18
737731

738-
# 31| Block 18
739-
# 31| v18_0(Void) = NoOp :
740-
# 32| r18_1(glval<null>) = FunctionAddress[WriteLine] :
741-
# 32| r18_2(String) = StringConstant["Done"] :
742-
# 32| v18_3(Void) = Call : func:r18_1, 0:r18_2
743-
# 32| mu18_4(null) = ^CallSideEffect : ~mu0_2
744-
# 5| v18_5(Void) = ReturnVoid :
745-
# 5| v18_6(Void) = UnmodeledUse : mu*
746-
# 5| v18_7(Void) = ExitFunction :
732+
# 32| Block 17
733+
# 32| r17_0(glval<Int32>) = VariableAddress[i] :
734+
# 32| r17_1(Int32) = Load : &:r17_0, ~mu0_2
735+
# 32| r17_2(Int32) = Constant[1] :
736+
# 32| r17_3(Int32) = Add : r17_1, r17_2
737+
# 32| mu17_4(Int32) = Store : &:r17_0, r17_3
738+
#-----| Goto (back edge) -> Block 18
739+
740+
# 32| Block 18
741+
# 32| r18_0(glval<Int32>) = VariableAddress[i] :
742+
# 32| r18_1(Int32) = Load : &:r18_0, ~mu0_2
743+
# 32| r18_2(Int32) = Constant[10] :
744+
# 32| r18_3(Boolean) = CompareLE : r18_1, r18_2
745+
# 32| v18_4(Void) = ConditionalBranch : r18_3
746+
#-----| False -> Block 22
747+
#-----| True -> Block 20
747748

748749
# 7| Block 19
749750
# 7| r19_0(glval<Int32>) = VariableAddress[i] :
@@ -753,6 +754,29 @@ jumps.cs:
753754
# 7| mu19_4(Int32) = Store : &:r19_0, r19_3
754755
#-----| Goto (back edge) -> Block 1
755756

757+
# 34| Block 20
758+
# 34| r20_0(glval<Int32>) = VariableAddress[i] :
759+
# 34| r20_1(Int32) = Load : &:r20_0, ~mu0_2
760+
# 34| r20_2(Int32) = Constant[5] :
761+
# 34| r20_3(Boolean) = CompareEQ : r20_1, r20_2
762+
# 34| v20_4(Void) = ConditionalBranch : r20_3
763+
#-----| False -> Block 17
764+
#-----| True -> Block 21
765+
766+
# 35| Block 21
767+
# 35| v21_0(Void) = NoOp :
768+
#-----| Goto -> Block 22
769+
770+
# 37| Block 22
771+
# 37| v22_0(Void) = NoOp :
772+
# 38| r22_1(glval<null>) = FunctionAddress[WriteLine] :
773+
# 38| r22_2(String) = StringConstant["Done"] :
774+
# 38| v22_3(Void) = Call : func:r22_1, 0:r22_2
775+
# 38| mu22_4(null) = ^CallSideEffect : ~mu0_2
776+
# 5| v22_5(Void) = ReturnVoid :
777+
# 5| v22_6(Void) = UnmodeledUse : mu*
778+
# 5| v22_7(Void) = ExitFunction :
779+
756780
lock.cs:
757781
# 5| System.Void LockTest.A()
758782
# 5| Block 0

0 commit comments

Comments
 (0)