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;
});
}
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;
}
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;
}
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;
}
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;
}
Aggregations