use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction in project intellij-community by JetBrains.
the class GroovyConstructorUsagesSearcher method processGroovyConstructorUsages.
private static boolean processGroovyConstructorUsages(GrCodeReferenceElement element, final Processor<GrNewExpression> newExpressionProcessor, final LiteralConstructorSearcher literalProcessor) {
PsiElement parent = element.getParent();
if (parent instanceof GrAnonymousClassDefinition) {
parent = parent.getParent();
}
if (parent instanceof GrNewExpression) {
return newExpressionProcessor.process((GrNewExpression) parent);
}
if (parent instanceof GrTypeElement) {
final GrTypeElement typeElement = (GrTypeElement) parent;
final PsiElement grandpa = typeElement.getParent();
if (grandpa instanceof GrVariableDeclaration) {
final GrVariable[] vars = ((GrVariableDeclaration) grandpa).getVariables();
if (vars.length == 1) {
final GrVariable variable = vars[0];
if (!checkLiteralInstantiation(variable.getInitializerGroovy(), literalProcessor)) {
return false;
}
}
} else if (grandpa instanceof GrMethod) {
final GrMethod method = (GrMethod) grandpa;
if (typeElement == method.getReturnTypeElementGroovy()) {
ControlFlowUtils.visitAllExitPoints(method.getBlock(), new ControlFlowUtils.ExitPointVisitor() {
@Override
public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
if (!checkLiteralInstantiation(returnValue, literalProcessor)) {
return false;
}
return true;
}
});
}
} else if (grandpa instanceof GrTypeCastExpression) {
final GrTypeCastExpression cast = (GrTypeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
} else if (grandpa instanceof GrSafeCastExpression) {
final GrSafeCastExpression cast = (GrSafeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction in project intellij-community by JetBrains.
the class UnassignedVariableAccessInspection method check.
@Override
protected void check(@NotNull GrControlFlowOwner owner, @NotNull ProblemsHolder problemsHolder) {
Instruction[] flow = owner.getControlFlow();
ReadWriteVariableInstruction[] reads = ControlFlowBuilderUtil.getReadsWithoutPriorWrites(flow, true);
for (ReadWriteVariableInstruction read : reads) {
PsiElement element = read.getElement();
if (element instanceof GroovyPsiElement && !(element instanceof GrClosableBlock)) {
String name = read.getVariableName();
GroovyPsiElement property = ResolveUtil.resolveProperty((GroovyPsiElement) element, name);
if (property != null && !(property instanceof PsiParameter) && !(property instanceof PsiField) && PsiTreeUtil.isAncestor(owner, property, false) && !(myIgnoreBooleanExpressions && isBooleanCheck(element))) {
problemsHolder.registerProblem(element, GroovyInspectionBundle.message("unassigned.access.tooltip", name));
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction in project intellij-community by JetBrains.
the class ControlFlowUtils method findAccess.
/**
* searches for next or previous write access to local variable
* @param local variable to analyze
* @param place place to start searching
* @param ahead if true search for next write. if false searches for previous write
* @return all write instructions leading to (or preceding) the place
*/
public static List<ReadWriteVariableInstruction> findAccess(GrVariable local, final PsiElement place, boolean ahead, boolean writeAccessOnly) {
LOG.assertTrue(!(local instanceof GrField), local.getClass());
final GrControlFlowOwner owner = findControlFlowOwner(place);
assert owner != null;
final Instruction cur = findInstruction(place, owner.getControlFlow());
if (cur == null) {
throw new IllegalArgumentException("place is not in the flow");
}
return findAccess(local, ahead, writeAccessOnly, cur);
}
use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction in project intellij-community by JetBrains.
the class ControlFlowUtils method inferWriteAccessMap.
@NotNull
public static List<BitSet> inferWriteAccessMap(final Instruction[] flow, final GrVariable var) {
final Semilattice<BitSet> sem = new Semilattice<BitSet>() {
@NotNull
@Override
public BitSet join(@NotNull List<BitSet> ins) {
BitSet result = new BitSet(flow.length);
for (BitSet set : ins) {
result.or(set);
}
return result;
}
@Override
public boolean eq(@NotNull BitSet e1, @NotNull BitSet e2) {
return e1.equals(e2);
}
};
DfaInstance<BitSet> dfa = new DfaInstance<BitSet>() {
@Override
public void fun(@NotNull BitSet bitSet, @NotNull Instruction instruction) {
if (!(instruction instanceof ReadWriteVariableInstruction))
return;
if (!((ReadWriteVariableInstruction) instruction).isWrite())
return;
final PsiElement element = instruction.getElement();
if (element instanceof GrVariable && element != var)
return;
if (element instanceof GrReferenceExpression) {
final GrReferenceExpression ref = (GrReferenceExpression) element;
if (ref.isQualified() || ref.resolve() != var) {
return;
}
}
if (!((ReadWriteVariableInstruction) instruction).getVariableName().equals(var.getName())) {
return;
}
bitSet.clear();
bitSet.set(instruction.num());
}
@NotNull
@Override
public BitSet initial() {
return new BitSet(flow.length);
}
};
return new DFAEngine<>(flow, dfa, sem).performForceDFA();
}
use of org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction in project intellij-community by JetBrains.
the class ControlFlowUtils method visitAllExitPointsInner.
private static boolean visitAllExitPointsInner(Instruction last, Instruction first, boolean[] visited, ExitPointVisitor visitor) {
if (first == last)
return true;
if (last instanceof AfterCallInstruction) {
visited[last.num()] = true;
return visitAllExitPointsInner(((AfterCallInstruction) last).myCall, first, visited, visitor);
}
if (last instanceof MaybeReturnInstruction) {
return visitor.visitExitPoint(last, (GrExpression) last.getElement());
} else if (last instanceof IfEndInstruction) {
visited[last.num()] = true;
for (Instruction instruction : last.allPredecessors()) {
if (!visitAllExitPointsInner(instruction, first, visited, visitor))
return false;
}
return true;
} else if (last instanceof ThrowingInstruction) {
PsiElement element = last.getElement();
if (!(element instanceof GrThrowStatement || element instanceof GrAssertStatement))
return true;
}
PsiElement element = last.getElement();
if (element != null) {
final GrExpression returnValue;
if (element instanceof GrReturnStatement) {
returnValue = ((GrReturnStatement) element).getReturnValue();
} else if (element instanceof GrExpression && PsiUtil.isExpressionStatement(element)) {
returnValue = (GrExpression) element;
} else {
returnValue = null;
}
return visitor.visitExitPoint(last, returnValue);
}
visited[last.num()] = true;
for (Instruction pred : last.allPredecessors()) {
if (!visited[pred.num()]) {
if (!visitAllExitPointsInner(pred, first, visited, visitor))
return false;
}
}
return true;
}
Aggregations