Search in sources :

Example 1 with ReadWriteInstruction

use of com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction in project intellij-community by JetBrains.

the class PyUnusedLocalInspectionVisitor method analyzeReadsInScope.

private void analyzeReadsInScope(@NotNull String name, @NotNull ScopeOwner owner, @NotNull Instruction[] instructions, int startInstruction, @Nullable PsiElement scopeAnchor) {
    // Check if the element is declared out of scope, mark all out of scope write accesses as used
    if (scopeAnchor != null) {
        final ScopeOwner declOwner = ScopeUtil.getDeclarationScopeOwner(scopeAnchor, name);
        if (declOwner != null && declOwner != owner) {
            final Collection<PsiElement> writeElements = ScopeUtil.getReadWriteElements(name, declOwner, false, true);
            for (PsiElement e : writeElements) {
                myUsedElements.add(e);
                myUnusedElements.remove(e);
            }
        }
    }
    ControlFlowUtil.iteratePrev(startInstruction, instructions, inst -> {
        final PsiElement instElement = inst.getElement();
        if (instElement instanceof PyFunction) {
            if (name.equals(((PyFunction) instElement).getName())) {
                myUsedElements.add(instElement);
                myUnusedElements.remove(instElement);
                return ControlFlowUtil.Operation.CONTINUE;
            }
        } else if (inst instanceof ReadWriteInstruction) {
            final ReadWriteInstruction rwInstruction = (ReadWriteInstruction) inst;
            if (rwInstruction.getAccess().isWriteAccess() && name.equals(rwInstruction.getName())) {
                if (instElement != null && PsiTreeUtil.isAncestor(owner, instElement, false)) {
                    myUsedElements.add(instElement);
                    myUnusedElements.remove(instElement);
                }
                return ControlFlowUtil.Operation.CONTINUE;
            }
        }
        return ControlFlowUtil.Operation.NEXT;
    });
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) PsiElement(com.intellij.psi.PsiElement)

Example 2 with ReadWriteInstruction

use of com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction in project intellij-community by JetBrains.

the class PyReachingDefsDfaInstance method processReducedMap.

private DFAMap<ScopeVariable> processReducedMap(DFAMap<ScopeVariable> map, final Instruction instruction, final PsiElement element) {
    String name = null;
    // Process readwrite instruction
    if (instruction instanceof ReadWriteInstruction && ((ReadWriteInstruction) instruction).getAccess().isWriteAccess()) {
        name = ((ReadWriteInstruction) instruction).getName();
    } else // Processing PyFunction
    if (element instanceof PyFunction) {
        name = ((PyFunction) element).getName();
    }
    if (name == null) {
        return map;
    }
    final ScopeVariable variable = map.get(name);
    // Parameter case
    final PsiElement parameterScope = ScopeUtil.getParameterScope(element);
    if (parameterScope != null) {
        final ScopeVariable scopeVariable = new ScopeVariableImpl(name, true, element);
        map = map.asWritable();
        map.put(name, scopeVariable);
    } else // Local variable case
    {
        final ScopeVariableImpl scopeVariable;
        final boolean isParameter = variable != null && variable.isParameter();
        if (variable == null) {
            scopeVariable = new ScopeVariableImpl(name, isParameter, element);
        } else {
            scopeVariable = new ScopeVariableImpl(name, isParameter, variable.getDeclarations());
        }
        map = map.asWritable();
        map.put(name, scopeVariable);
    }
    return map;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) PyFunction(com.jetbrains.python.psi.PyFunction) ScopeVariableImpl(com.jetbrains.python.codeInsight.dataflow.scope.impl.ScopeVariableImpl) ScopeVariable(com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable) PsiElement(com.intellij.psi.PsiElement)

Example 3 with ReadWriteInstruction

use of com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction in project intellij-community by JetBrains.

the class PyCodeFragmentUtil method getGlobalWrites.

@NotNull
private static Set<String> getGlobalWrites(@NotNull List<Instruction> instructions, @NotNull ScopeOwner owner) {
    final Scope scope = ControlFlowCache.getScope(owner);
    final Set<String> globalWrites = new LinkedHashSet<>();
    for (Instruction instruction : getWriteInstructions(instructions)) {
        if (instruction instanceof ReadWriteInstruction) {
            final String name = ((ReadWriteInstruction) instruction).getName();
            final PsiElement element = instruction.getElement();
            if (scope.isGlobal(name) || (owner instanceof PsiFile && element instanceof PsiNamedElement && isUsedOutside((PsiNamedElement) element, instructions))) {
                globalWrites.add(name);
            }
        }
    }
    return globalWrites;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with ReadWriteInstruction

use of com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction in project intellij-community by JetBrains.

the class PyCodeFragmentUtil method getNonlocalWrites.

@NotNull
private static Set<String> getNonlocalWrites(@NotNull List<Instruction> instructions, @NotNull ScopeOwner owner) {
    final Scope scope = ControlFlowCache.getScope(owner);
    final Set<String> nonlocalWrites = new LinkedHashSet<>();
    for (Instruction instruction : getWriteInstructions(instructions)) {
        if (instruction instanceof ReadWriteInstruction) {
            final String name = ((ReadWriteInstruction) instruction).getName();
            if (scope.isNonlocal(name)) {
                nonlocalWrites.add(name);
            }
        }
    }
    return nonlocalWrites;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with ReadWriteInstruction

use of com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction in project intellij-community by JetBrains.

the class PyBaseMakeFunctionTopLevelProcessor method analyseScope.

@NotNull
protected AnalysisResult analyseScope(@NotNull ScopeOwner owner) {
    final ControlFlow controlFlow = ControlFlowCache.getControlFlow(owner);
    final AnalysisResult result = new AnalysisResult();
    for (Instruction instruction : controlFlow.getInstructions()) {
        if (instruction instanceof ReadWriteInstruction) {
            final ReadWriteInstruction readWriteInstruction = (ReadWriteInstruction) instruction;
            final PsiElement element = readWriteInstruction.getElement();
            if (element == null) {
                continue;
            }
            if (readWriteInstruction.getAccess().isReadAccess()) {
                for (PsiElement resolved : PyUtil.multiResolveTopPriority(element, myResolveContext)) {
                    if (resolved != null) {
                        if (isInitOrNewMethod(resolved)) {
                            resolved = ((PyFunction) resolved).getContainingClass();
                        }
                        if (isFromEnclosingScope(resolved)) {
                            result.readsFromEnclosingScope.add(element);
                        } else if (!belongsToFunction(resolved)) {
                            myExternalReads.add(resolved);
                        }
                        if (resolved instanceof PyParameter && ((PyParameter) resolved).isSelf()) {
                            if (PsiTreeUtil.getParentOfType(resolved, PyFunction.class) == myFunction) {
                                result.readsOfSelfParameter.add(element);
                            } else if (!PsiTreeUtil.isAncestor(myFunction, resolved, true)) {
                                result.readsOfSelfParametersFromEnclosingScope.add(element);
                            }
                        }
                    }
                }
            }
            if (readWriteInstruction.getAccess().isWriteAccess() && element instanceof PyTargetExpression) {
                for (PsiElement resolved : PyUtil.multiResolveTopPriority(element, myResolveContext)) {
                    if (resolved != null) {
                        if (element.getParent() instanceof PyNonlocalStatement && isFromEnclosingScope(resolved)) {
                            result.nonlocalWritesToEnclosingScope.add((PyTargetExpression) element);
                        }
                        if (resolved instanceof PyParameter && ((PyParameter) resolved).isSelf() && PsiTreeUtil.getParentOfType(resolved, PyFunction.class) == myFunction) {
                            result.writesToSelfParameter.add((PyTargetExpression) element);
                        }
                    }
                }
            }
        }
    }
    return result;
}
Also used : ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) ReadWriteInstruction(com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction) Instruction(com.intellij.codeInsight.controlflow.Instruction) ControlFlow(com.intellij.codeInsight.controlflow.ControlFlow) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ReadWriteInstruction (com.jetbrains.python.codeInsight.controlflow.ReadWriteInstruction)10 Instruction (com.intellij.codeInsight.controlflow.Instruction)8 PsiElement (com.intellij.psi.PsiElement)7 NotNull (org.jetbrains.annotations.NotNull)4 ControlFlow (com.intellij.codeInsight.controlflow.ControlFlow)2 Scope (com.jetbrains.python.codeInsight.dataflow.scope.Scope)2 StubBasedPsiElement (com.intellij.psi.StubBasedPsiElement)1 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)1 ScopeVariable (com.jetbrains.python.codeInsight.dataflow.scope.ScopeVariable)1 ScopeVariableImpl (com.jetbrains.python.codeInsight.dataflow.scope.impl.ScopeVariableImpl)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 PyDefUseUtil (com.jetbrains.python.refactoring.PyDefUseUtil)1 ArrayList (java.util.ArrayList)1