From 12d21d8ecf834c1fbd4f4f2e78b6beb3ca61744c Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Thu, 17 May 2012 17:57:44 +0400 Subject: [PATCH] Fixed out-of-order for-loop body instruction in Python CFG --- .../codeInsight/controlflow/PyControlFlowBuilder.java | 10 +++++++--- python/testData/codeInsight/controlflow/for.txt | 2 +- python/testData/codeInsight/controlflow/forif.txt | 2 +- python/testData/codeInsight/controlflow/forreturn.txt | 2 +- .../codeInsight/controlflow/fortrycontinue.txt | 6 +++--- python/testData/codeInsight/controlflow/trybreak.txt | 2 +- python/testData/codeInsight/controlflow/trytry.txt | 2 +- 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java index 03fe3763b231..8bc80feaa473 100644 --- a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java +++ b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java @@ -361,20 +361,24 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { } final PyStatementList list = forPart.getStatementList(); if (list != null) { - final Instruction bodyInstruction = myBuilder.startNode(list); + final Instruction body; final PyExpression target = forPart.getTarget(); if (target != null) { + body = myBuilder.startNode(target); target.accept(this); } + else { + body = myBuilder.startNode(list); + } list.accept(this); if (myBuilder.prevInstruction != null) { - myBuilder.addEdge(myBuilder.prevInstruction, bodyInstruction); //loop + myBuilder.addEdge(myBuilder.prevInstruction, body); //loop myBuilder.addPendingEdge(list, myBuilder.prevInstruction); // exit } myBuilder.processPending(new ControlFlowBuilder.PendingProcessor() { public void process(final PsiElement pendingScope, final Instruction instruction) { if (pendingScope != null && PsiTreeUtil.isAncestor(list, pendingScope, false)) { - myBuilder.addEdge(instruction, bodyInstruction); //loop + myBuilder.addEdge(instruction, body); //loop myBuilder.addPendingEdge(list, instruction); // exit } else { diff --git a/python/testData/codeInsight/controlflow/for.txt b/python/testData/codeInsight/controlflow/for.txt index ec5ca7aade0f..207860580baf 100644 --- a/python/testData/codeInsight/controlflow/for.txt +++ b/python/testData/codeInsight/controlflow/for.txt @@ -1,7 +1,7 @@ 0(1) element: null 1(2) element: PyForStatement 2(3,7) READ ACCESS: range -3(4) element: PyStatementList +3(4) element: PyTargetExpression: i 4(5) WRITE ACCESS: i 5(6) element: PyPrintStatement 6(3,7) READ ACCESS: i diff --git a/python/testData/codeInsight/controlflow/forif.txt b/python/testData/codeInsight/controlflow/forif.txt index 9ac94d4c7451..20ddbb701077 100644 --- a/python/testData/codeInsight/controlflow/forif.txt +++ b/python/testData/codeInsight/controlflow/forif.txt @@ -1,7 +1,7 @@ 0(1) element: null 1(2) element: PyForStatement 2(3,10) READ ACCESS: lines -3(4) element: PyStatementList +3(4) element: PyTargetExpression: line 4(5) WRITE ACCESS: line 5(6) element: PyIfStatement 6(7,3,10) READ ACCESS: line diff --git a/python/testData/codeInsight/controlflow/forreturn.txt b/python/testData/codeInsight/controlflow/forreturn.txt index 42218407ada7..c189bc10beb5 100644 --- a/python/testData/codeInsight/controlflow/forreturn.txt +++ b/python/testData/codeInsight/controlflow/forreturn.txt @@ -1,7 +1,7 @@ 0(1) element: null 1(2) element: PyForStatement 2(3,15) READ ACCESS: self -3(4) element: PyStatementList +3(4) element: PyTupleExpression 4(5) WRITE ACCESS: start 5(6) WRITE ACCESS: end 6(7) WRITE ACCESS: name diff --git a/python/testData/codeInsight/controlflow/fortrycontinue.txt b/python/testData/codeInsight/controlflow/fortrycontinue.txt index c34c53b5bdf0..bc5ad352e7f1 100644 --- a/python/testData/codeInsight/controlflow/fortrycontinue.txt +++ b/python/testData/codeInsight/controlflow/fortrycontinue.txt @@ -3,16 +3,16 @@ 2(3) WRITE ACCESS: tests 3(4) element: PyForStatement 4(5,17) READ ACCESS: tests -5(6) element: PyStatementList +5(6) element: PyTargetExpression: t 6(7) WRITE ACCESS: t 7(8) element: PyTryExceptStatement 8(9,14) element: PyTryPart 9(10,14,5,17) element: PyForStatement -10(11) element: PyStatementList +10(11,14) element: PyTargetExpression: t 11(12,14) WRITE ACCESS: t 12(13,14) element: PyPrintStatement 13(10,14,5,17) READ ACCESS: t 14(15) element: PyExceptPart 15(16) READ ACCESS: Exception 16(3) element: PyContinueStatement -17() element: null +17() element: null \ No newline at end of file diff --git a/python/testData/codeInsight/controlflow/trybreak.txt b/python/testData/codeInsight/controlflow/trybreak.txt index fe1b9a538aea..ba02f2ced03a 100644 --- a/python/testData/codeInsight/controlflow/trybreak.txt +++ b/python/testData/codeInsight/controlflow/trybreak.txt @@ -3,7 +3,7 @@ 2(3,8) element: PyTryPart 3(4,8) element: PyForStatement 4(5,8,11) READ ACCESS: bar -5(6) element: PyStatementList +5(6,8) element: PyTargetExpression: i 6(7,8) WRITE ACCESS: i 7(8,11) element: PyBreakStatement 8(9) element: PyExceptPart diff --git a/python/testData/codeInsight/controlflow/trytry.txt b/python/testData/codeInsight/controlflow/trytry.txt index e4888f7bb84d..b6078f8500d5 100644 --- a/python/testData/codeInsight/controlflow/trytry.txt +++ b/python/testData/codeInsight/controlflow/trytry.txt @@ -6,7 +6,7 @@ 5(6,55) element: PyAssignmentStatement 6(7,55) WRITE ACCESS: b 7(8,53,55) element: PyForStatement -8(9) element: PyStatementList +8(9,55) element: PyTargetExpression: x 9(10,55) WRITE ACCESS: x 10(11,55) element: PyTryExceptStatement 11(12,45) element: PyTryPart