Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hurricup/exception fixes #2771

Merged
merged 5 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.intellij.psi.tree.TokenSet;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiCacheKey;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.Interner;
Expand Down Expand Up @@ -64,6 +65,9 @@ public final class PerlValuesManager {
private static final TokenSet LIST_VALUES = TokenSet.create(
STRING_LIST, COMMA_SEQUENCE_EXPR
);
private static final PsiCacheKey<PerlValue, PsiElement> VALUE_KEY = PsiCacheKey.create(
"perl5.value", it -> RecursionManager.doPreventingRecursion(it, true, () -> intern(computeValue(it)))
);
/**
* Values from this entities delegated to the kids
*/
Expand Down Expand Up @@ -150,15 +154,7 @@ else if (finalElement instanceof PerlDerefExpression) {
return from(children[0]);
}

return CachedValuesManager.getCachedValue(
finalElement, () -> {
PerlValue computedValue = RecursionManager.doPreventingRecursion(finalElement, true, () -> computeValue(finalElement));
if (computedValue == null) {
LOG.error("Recursion while computing value of " + finalElement + " from " + PsiUtilCore.getVirtualFile(finalElement));
computedValue = UNKNOWN_VALUE;
}
return CachedValueProvider.Result.create(intern(computedValue), finalElement.getContainingFile());
});
return VALUE_KEY.getValue(finalElement);
}

private static @NotNull PerlValue computeValue(@NotNull PsiElement element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


Expand Down Expand Up @@ -93,7 +94,7 @@ else if (canonicalPath.contains(tokenText)) {
/**
* Adds a variable declaration that current one shadows
*/
private void addShadowedVariable(@NotNull PsiElement sourceElement, ArrayList<PsiElement> result) {
private void addShadowedVariable(@NotNull PsiElement sourceElement, @NotNull List<? super PsiElement> result) {
if (!(sourceElement instanceof PerlVariableNameElement)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiCacheKey;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.intellij.util.PairProcessor;
Expand Down Expand Up @@ -59,11 +58,22 @@


public final class PerlResolveUtil {
private PerlResolveUtil() {
}

private static final Logger LOG = Logger.getInstance(PerlResolveUtil.class);
private static boolean SUPPRESS_ERRORS = false;
private static final ThreadLocal<int[]> INFERENCE_DEPTH = ThreadLocal.withInitial(()-> new int[]{0});
private static final int MAX_INFERENCE_DEPTH = 300;
private static final PsiCacheKey<PerlVariableDeclarationElement, PerlVariable> DECLARATION_KEY = PsiCacheKey.create(
"perl5.variable.declaration", variable -> {
PerlVariableDeclarationSearcher variableProcessor = new PerlVariableDeclarationSearcher(variable);
if (PerlResolveUtil.treeWalkUp(variable, variableProcessor)) {
variableProcessor.searchBuiltIn();
}
return variableProcessor.getResult();
}
);

private PerlResolveUtil() {
}

public static boolean treeWalkUp(@Nullable PsiElement place, @NotNull PsiScopeProcessor processor) {
PsiElement lastParent = null;
Expand Down Expand Up @@ -117,20 +127,15 @@ public static boolean processChildren(@NotNull PsiElement element,
* @param variable variable to search declaration for
* @return variable in declaration term or null if there is no such one
*/
public static @Nullable PerlVariableDeclarationElement getLexicalDeclaration(PerlVariable variable) {
public static @Nullable PerlVariableDeclarationElement getLexicalDeclaration(@NotNull PerlVariable variable) {
if (variable instanceof PerlVariableDeclarationElement) {
return (PerlVariableDeclarationElement)variable;
}
if (variable.getExplicitNamespaceName() != null) {
return null;
}
return CachedValuesManager.getCachedValue(variable, () -> {
PerlVariableDeclarationSearcher variableProcessor = new PerlVariableDeclarationSearcher(variable);
if (PerlResolveUtil.treeWalkUp(variable, variableProcessor)) {
variableProcessor.searchBuiltIn();
}
return CachedValueProvider.Result.create(variableProcessor.getResult(), variable.getContainingFile());
});

return DECLARATION_KEY.getValue(variable);
}

/**
Expand Down Expand Up @@ -189,7 +194,22 @@ else if (reference != null) {
* @see PerlValue
*/
public static @NotNull PerlValue inferVariableValue(@NotNull PerlVariable variable) {
return getValueFromControlFlow(new TypeInferringContext(variable)).buildValue();
var currentDepth = INFERENCE_DEPTH.get();
if(currentDepth[0] >= MAX_INFERENCE_DEPTH){
LOG.warn("Too complex to analyze: " + variable + "; file: " + PsiUtilCore.getVirtualFile(variable));
return UNKNOWN_VALUE;
}
try{
currentDepth[0]++;
return getValueFromControlFlow(new TypeInferringContext(variable)).buildValue();
}
catch (StackOverflowError e){
LOG.error("Stack overflow while inferring variable value: " + variable + "; file: " + PsiUtilCore.getVirtualFile(variable));
return UNKNOWN_VALUE;
}
finally {
currentDepth[0]--;
}
}

public static @NotNull PerlValue inferVariableValue(@NotNull PerlBuiltInVariable variable, @NotNull PsiElement contextElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
}

public static @Nullable PsiElement getPerlInjectionContext(@NotNull PsiLanguageInjectionHost host) {
return RecursionManager.doPreventingRecursion(host, false, CachedValuesManager.getCachedValue(
host, () -> CachedValueProvider.Result.create(() -> computeInjectionContext(host), host)));
return CachedValuesManager.getCachedValue(
host, () -> {
var result = RecursionManager.doPreventingRecursion(host, false, () -> computeInjectionContext(host));
return CachedValueProvider.Result.create(result, host.getContainingFile());
});
}

private static @Nullable PsiElement computeInjectionContext(@NotNull PsiLanguageInjectionHost host) {
Expand All @@ -98,24 +101,24 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
return parent;
}

if (!(parent instanceof PsiPerlAssignExpr)) {
if (!(parent instanceof PsiPerlAssignExpr assignExpr)) {
return null;
}

if (PsiUtilCore.getElementType(((PsiPerlAssignExpr)parent).getRightOperatorElement(host)) != OPERATOR_ASSIGN) {
if (PsiUtilCore.getElementType(assignExpr.getRightOperatorElement(host)) != OPERATOR_ASSIGN) {
return null;
}

PsiElement variable = ((PsiPerlAssignExpr)parent).getLeftPartOfAssignment(host);
if (variable instanceof PerlVariableDeclarationExpr) {
List<PsiPerlVariableDeclarationElement> variables = ((PerlVariableDeclarationExpr)variable).getVariableDeclarationElementList();
PsiElement variable = assignExpr.getLeftPartOfAssignment(host);
if (variable instanceof PerlVariableDeclarationExpr variableDeclarationExpr) {
List<PsiPerlVariableDeclarationElement> variables = variableDeclarationExpr.getVariableDeclarationElementList();
if (variables.size() != 1) {
return null;
}
variable = variables.get(0);
}
else if (variable instanceof PsiPerlScalarVariable) {
PerlVariableDeclarationElement variableDeclarationElement = ((PsiPerlScalarVariable)variable).getLexicalDeclaration();
else if (variable instanceof PsiPerlScalarVariable scalarVariable) {
PerlVariableDeclarationElement variableDeclarationElement = scalarVariable.getLexicalDeclaration();
if (variableDeclarationElement == null) {
return null;
}
Expand Down
8 changes: 7 additions & 1 deletion plugin/src/test/java/unit/perl/PerlStubsTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2020 Alexandr Evstigneev
* Copyright 2015-2023 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,6 +40,12 @@ protected String getBaseDataPath() {
return "unit/perl/stubs";
}

@Test
public void testAmavis_pl() { doTest(); }

@Test
public void testHugeList_pl() { doTest(); }

@Test
public void testFunctionParametersFun_pl() {doTest();}

Expand Down
Loading
Loading