diff --git a/python/src/com/jetbrains/python/codeInsight/controlflow/InstructionBuilder.java b/python/src/com/jetbrains/python/codeInsight/controlflow/InstructionBuilder.java index 71bf944aa7e6..e180111005c2 100644 --- a/python/src/com/jetbrains/python/codeInsight/controlflow/InstructionBuilder.java +++ b/python/src/com/jetbrains/python/codeInsight/controlflow/InstructionBuilder.java @@ -10,15 +10,22 @@ import java.util.List; * @author traff */ public class InstructionBuilder { - public static List buildInstructions(ControlFlowBuilder builder, List definitions) { + public static List buildInstructions(ControlFlowBuilder builder, List assertions) { List result = CollectionFactory.arrayList(); - for (PyConditionEvaluator.Definition def: definitions) { + for (PyAssertionEvaluator.Assertion def: assertions) { processDef(builder, def, result); } return result; } - private static void processDef(ControlFlowBuilder builder, PyConditionEvaluator.Definition def, List result) { + private static void processDef(ControlFlowBuilder builder, PyAssertionEvaluator.Assertion def, List result) { result.add(ReadWriteInstruction.writeType(builder, def.getElement(), def.getName())); } + + public static void addAssertInstructions(ControlFlowBuilder builder, PyAssertionEvaluator assertionEvaluator) { + for (Instruction instr : buildInstructions(builder, assertionEvaluator.getDefinitions())) { + builder.addNode(instr); + } + + } } diff --git a/python/src/com/jetbrains/python/codeInsight/controlflow/PyConditionEvaluator.java b/python/src/com/jetbrains/python/codeInsight/controlflow/PyAssertionEvaluator.java similarity index 77% rename from python/src/com/jetbrains/python/codeInsight/controlflow/PyConditionEvaluator.java rename to python/src/com/jetbrains/python/codeInsight/controlflow/PyAssertionEvaluator.java index 2e3a0996fc08..07f6f068feee 100644 --- a/python/src/com/jetbrains/python/codeInsight/controlflow/PyConditionEvaluator.java +++ b/python/src/com/jetbrains/python/codeInsight/controlflow/PyAssertionEvaluator.java @@ -10,11 +10,11 @@ import java.util.Stack; /** * @author traff */ -public class PyConditionEvaluator extends PyRecursiveElementVisitor { +public class PyAssertionEvaluator extends PyRecursiveElementVisitor { - private Stack myStack = CollectionFactory.stack(); + private Stack myStack = CollectionFactory.stack(); - public PyConditionEvaluator() { + public PyAssertionEvaluator() { } @@ -35,17 +35,17 @@ public class PyConditionEvaluator extends PyRecursiveElementVisitor { PyExpression[] args = node.getArguments(); if (args.length == 2 && args[0] instanceof PyReferenceExpression) { PyReferenceExpression target = (PyReferenceExpression)args[0]; - Definition o = new Definition(target.getName(), args[1]); + Assertion o = new Assertion(target.getName(), args[1]); myStack.push(o); } } } - class Definition { + class Assertion { private final String name; private final PyElement element; - Definition(String name, PyElement element) { + Assertion(String name, PyElement element) { this.name = name; this.element = element; } @@ -59,7 +59,7 @@ public class PyConditionEvaluator extends PyRecursiveElementVisitor { } } - public List getDefinitions() { + public List getDefinitions() { return myStack; } } diff --git a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java index abfb32f6ea6d..963f66a5b2d0 100644 --- a/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java +++ b/python/src/com/jetbrains/python/codeInsight/controlflow/PyControlFlowBuilder.java @@ -198,10 +198,10 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { myBuilder.startNode(node); final PyIfPart ifPart = node.getIfPart(); PyExpression condition = ifPart.getCondition(); - PyConditionEvaluator conditionEvaluator = new PyConditionEvaluator(); + PyAssertionEvaluator assertionEvaluator = new PyAssertionEvaluator(); if (condition != null) { condition.accept(this); - condition.accept(conditionEvaluator); + condition.accept(assertionEvaluator); } // Set the head as the last instruction of condition Instruction head = getPrevInstruction(condition); @@ -209,9 +209,7 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { final PyStatementList thenStatements = ifPart.getStatementList(); if (thenStatements != null) { myBuilder.startConditionalNode(thenStatements, condition, true); - for (Instruction instr : InstructionBuilder.buildInstructions(myBuilder, conditionEvaluator.getDefinitions())) { - myBuilder.addNode(instr); - } + InstructionBuilder.addAssertInstructions(myBuilder, assertionEvaluator); thenStatements.accept(this); myBuilder.processPending(new ControlFlowBuilder.PendingProcessor() { public void process(final PsiElement pendingScope, final Instruction instruction) { @@ -553,4 +551,12 @@ public class PyControlFlowBuilder extends PyRecursiveElementVisitor { result.accept(this); } } + + public void visitPyAssertStatement(final PyAssertStatement node) { + PyAssertionEvaluator evaluator = new PyAssertionEvaluator(); + node.acceptChildren(evaluator); + InstructionBuilder.addAssertInstructions(myBuilder, evaluator); + } + + } \ No newline at end of file diff --git a/python/src/com/jetbrains/python/psi/PyElementVisitor.java b/python/src/com/jetbrains/python/psi/PyElementVisitor.java index 61205145a11a..3ef777a1c1ec 100644 --- a/python/src/com/jetbrains/python/psi/PyElementVisitor.java +++ b/python/src/com/jetbrains/python/psi/PyElementVisitor.java @@ -226,4 +226,8 @@ public class PyElementVisitor extends PsiElementVisitor { public void visitPyConditionalStatementPart(PyConditionalStatementPart node) { visitPyElement(node); } + + public void visitPyAssertStatement(final PyAssertStatement node) { + visitPyElement(node); + } } diff --git a/python/src/com/jetbrains/python/psi/impl/PyAssertStatementImpl.java b/python/src/com/jetbrains/python/psi/impl/PyAssertStatementImpl.java index c4c8f9bdc6b6..94d9c064a364 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyAssertStatementImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyAssertStatementImpl.java @@ -2,6 +2,7 @@ package com.jetbrains.python.psi.impl; import com.intellij.lang.ASTNode; import com.jetbrains.python.psi.PyAssertStatement; +import com.jetbrains.python.psi.PyElementVisitor; /** * @author yole @@ -10,4 +11,8 @@ public class PyAssertStatementImpl extends PyElementImpl implements PyAssertStat public PyAssertStatementImpl(ASTNode astNode) { super(astNode); } + + protected void acceptPyVisitor(PyElementVisitor pyVisitor) { + pyVisitor.visitPyAssertStatement(this); + } } diff --git a/python/testData/codeInsight/controlflow/assert.py b/python/testData/codeInsight/controlflow/assert.py new file mode 100644 index 000000000000..cf16c36e8d44 --- /dev/null +++ b/python/testData/codeInsight/controlflow/assert.py @@ -0,0 +1 @@ +assert isinstance(s, basestring), isinstance(x, int) \ No newline at end of file diff --git a/python/testData/codeInsight/controlflow/assert.txt b/python/testData/codeInsight/controlflow/assert.txt new file mode 100644 index 000000000000..b953b2b3782c --- /dev/null +++ b/python/testData/codeInsight/controlflow/assert.txt @@ -0,0 +1,4 @@ +0(1) element: null +1(2) WRITETYPE ACCESS: s +2(3) WRITETYPE ACCESS: x +3() element: null \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PyControlFlowBuilderTest.java b/python/testSrc/com/jetbrains/python/PyControlFlowBuilderTest.java index e513b5141fec..4893d6583d78 100644 --- a/python/testSrc/com/jetbrains/python/PyControlFlowBuilderTest.java +++ b/python/testSrc/com/jetbrains/python/PyControlFlowBuilderTest.java @@ -30,7 +30,11 @@ public class PyControlFlowBuilderTest extends LightMarkedTestCase { final String fullPath = getTestDataPath() + testName + ".txt"; check(fullPath, flow); } - + + public void testAssert() throws Exception { + doTest(); + } + public void testFile() throws Exception { doTest(); }