From 783c421a55709c962b9b0fe0246ac5ad1702e783 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Mon, 14 Nov 2011 18:15:29 +0400 Subject: [PATCH] Fixed loop-exit CFG edges in for-else loops (PY-4239) --- .../controlflow/PyControlFlowBuilder.java | 16 ++++------------ python/testData/codeInsight/controlflow/for.py | 6 +++--- python/testData/codeInsight/controlflow/for.txt | 2 +- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java index 0a2d76082b37..234f1086d66f 100644 --- a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java +++ b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java @@ -30,10 +30,6 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { private final ControlFlowBuilder myBuilder = new ControlFlowBuilder(); private List> myPendindBackup = null; -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -//// Control flow builder staff -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - public ControlFlow buildControlFlow(@NotNull final ScopeOwner owner) { return myBuilder.build(this, owner); } @@ -322,27 +318,23 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { if (elsePart == null) { myBuilder.addPendingEdge(node, myBuilder.prevInstruction); } - final PyStatementList list = forPart.getStatementList(); if (list != null) { - Instruction bodyInstruction = myBuilder.startNode(list); + final Instruction bodyInstruction = myBuilder.startNode(list); final PyExpression target = forPart.getTarget(); if (target != null) { target.accept(this); } - list.accept(this); - if (myBuilder.prevInstruction != null) { myBuilder.addEdge(myBuilder.prevInstruction, bodyInstruction); //loop - myBuilder.addPendingEdge(node, myBuilder.prevInstruction); // exit + myBuilder.addPendingEdge(list, myBuilder.prevInstruction); // exit } - final Ref bodyInstRef = new Ref(bodyInstruction); 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, bodyInstRef.get()); //loop - myBuilder.addPendingEdge(node, instruction); // exit + myBuilder.addEdge(instruction, bodyInstruction); //loop + myBuilder.addPendingEdge(list, instruction); // exit } else { myBuilder.addPendingEdge(pendingScope, instruction); diff --git a/python/testData/codeInsight/controlflow/for.py b/python/testData/codeInsight/controlflow/for.py index 4dd60ddc2fe0..2a3a27c47995 100644 --- a/python/testData/codeInsight/controlflow/for.py +++ b/python/testData/codeInsight/controlflow/for.py @@ -1,4 +1,4 @@ -for i in range(10) +for i in range(10): print(i) -else - print "Something went wrong" \ No newline at end of file +else: + print('for-loop executed 0 or more times, no break') \ No newline at end of file diff --git a/python/testData/codeInsight/controlflow/for.txt b/python/testData/codeInsight/controlflow/for.txt index bb3183a2ad67..ec5ca7aade0f 100644 --- a/python/testData/codeInsight/controlflow/for.txt +++ b/python/testData/codeInsight/controlflow/for.txt @@ -4,6 +4,6 @@ 3(4) element: PyStatementList 4(5) WRITE ACCESS: i 5(6) element: PyPrintStatement -6(3,8) READ ACCESS: i +6(3,7) READ ACCESS: i 7(8) element: PyPrintStatement 8() element: null \ No newline at end of file