mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-13 19:25:38 +02:00
Bug 491834 - StackOverflowError in HeuristicResolver
Change-Id: I8aaf80e66d6d7bd999482450c506f7160bd276fd
This commit is contained in:
parent
fc07efa909
commit
6be5a7fbe0
1 changed files with 86 additions and 64 deletions
|
@ -96,6 +96,15 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownType;
|
||||||
* implementing more advanced heuristics that could deal with this.
|
* implementing more advanced heuristics that could deal with this.
|
||||||
*/
|
*/
|
||||||
public class HeuristicResolver {
|
public class HeuristicResolver {
|
||||||
|
// Infrastructure to protect against infinite recursion in heuristic resolution.
|
||||||
|
private static final int RESOLUTION_DEPTH_LIMIT = 32;
|
||||||
|
private static final ThreadLocal<Integer> fResolutionDepth = new ThreadLocal<Integer>() {
|
||||||
|
@Override
|
||||||
|
protected Integer initialValue() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a dependent type, heuristically tries to find a concrete scope (i.e. not an unknown scope)
|
* Given a dependent type, heuristically tries to find a concrete scope (i.e. not an unknown scope)
|
||||||
* for it.
|
* for it.
|
||||||
|
@ -342,78 +351,91 @@ public class HeuristicResolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function for resolveUnknownType() which does one round of resolution.
|
* Helper function for {@link #resolveUnknownType} which does one round of resolution.
|
||||||
*/
|
*/
|
||||||
private static IType resolveUnknownTypeOnce(ICPPUnknownType type, IASTNode point) {
|
private static IType resolveUnknownTypeOnce(ICPPUnknownType type, IASTNode point) {
|
||||||
if (type instanceof ICPPDeferredClassInstance) {
|
// Guard against infinite recursion.
|
||||||
ICPPDeferredClassInstance deferredInstance = (ICPPDeferredClassInstance) type;
|
int resolutionDepth = fResolutionDepth.get();
|
||||||
return deferredInstance.getClassTemplate();
|
if (resolutionDepth > RESOLUTION_DEPTH_LIMIT) {
|
||||||
} else if (type instanceof TypeOfDependentExpression) {
|
return type;
|
||||||
ICPPEvaluation evaluation = ((TypeOfDependentExpression) type).getEvaluation();
|
}
|
||||||
if (evaluation instanceof EvalUnary) {
|
// Increment the resolution depth for the duration of this call.
|
||||||
EvalUnary unary = (EvalUnary) evaluation;
|
fResolutionDepth.set(resolutionDepth + 1);
|
||||||
// Handle the common case of a dependent type representing the result of
|
|
||||||
// dereferencing another dependent type.
|
try {
|
||||||
if (unary.getOperator() == IASTUnaryExpression.op_star) {
|
if (type instanceof ICPPDeferredClassInstance) {
|
||||||
IType argument = unary.getArgument().getType(point);
|
ICPPDeferredClassInstance deferredInstance = (ICPPDeferredClassInstance) type;
|
||||||
if (argument instanceof ICPPUnknownType) {
|
return deferredInstance.getClassTemplate();
|
||||||
IType resolved = resolveUnknownType((ICPPUnknownType) argument, point);
|
} else if (type instanceof TypeOfDependentExpression) {
|
||||||
if (resolved instanceof IPointerType) {
|
ICPPEvaluation evaluation = ((TypeOfDependentExpression) type).getEvaluation();
|
||||||
return ((IPointerType) resolved).getType();
|
if (evaluation instanceof EvalUnary) {
|
||||||
|
EvalUnary unary = (EvalUnary) evaluation;
|
||||||
|
// Handle the common case of a dependent type representing the result of
|
||||||
|
// dereferencing another dependent type.
|
||||||
|
if (unary.getOperator() == IASTUnaryExpression.op_star) {
|
||||||
|
IType argument = unary.getArgument().getType(point);
|
||||||
|
if (argument instanceof ICPPUnknownType) {
|
||||||
|
IType resolved = resolveUnknownType((ICPPUnknownType) argument, point);
|
||||||
|
if (resolved instanceof IPointerType) {
|
||||||
|
return ((IPointerType) resolved).getType();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if (evaluation instanceof EvalID) {
|
||||||
} else if (evaluation instanceof EvalID) {
|
EvalID id = (EvalID) evaluation;
|
||||||
EvalID id = (EvalID) evaluation;
|
ICPPEvaluation fieldOwner = id.getFieldOwner();
|
||||||
ICPPEvaluation fieldOwner = id.getFieldOwner();
|
if (fieldOwner != null) {
|
||||||
if (fieldOwner != null) {
|
IType fieldOwnerType = fieldOwner.getType(point);
|
||||||
IType fieldOwnerType = fieldOwner.getType(point);
|
IBinding[] candidates = lookInside(fieldOwnerType, id.isPointerDeref(), id.getName(),
|
||||||
IBinding[] candidates = lookInside(fieldOwnerType, id.isPointerDeref(), id.getName(),
|
id.getTemplateArgs(), point);
|
||||||
id.getTemplateArgs(), point);
|
if (candidates.length == 1) {
|
||||||
if (candidates.length == 1) {
|
return typeForBinding(candidates[0]);
|
||||||
return typeForBinding(candidates[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (evaluation instanceof EvalFunctionCall) {
|
|
||||||
EvalFunctionCall evalFunctionCall = (EvalFunctionCall) evaluation;
|
|
||||||
ICPPEvaluation function = evalFunctionCall.getArguments()[0];
|
|
||||||
IType functionType = function.getType(point);
|
|
||||||
if (functionType instanceof ICPPUnknownType) {
|
|
||||||
functionType = resolveUnknownType((ICPPUnknownType) functionType, point);
|
|
||||||
}
|
|
||||||
return ExpressionTypes.typeFromFunctionCall(functionType);
|
|
||||||
} else if (evaluation instanceof EvalMemberAccess) {
|
|
||||||
IBinding member = ((EvalMemberAccess) evaluation).getMember();
|
|
||||||
// Presumably the type will be unknown. That's fine, it will be
|
|
||||||
// resolved during subsequent resolution rounds.
|
|
||||||
return typeForBinding(member);
|
|
||||||
}
|
|
||||||
// TODO(nathanridge): Handle more cases.
|
|
||||||
} else if (type instanceof ICPPUnknownMemberClass) {
|
|
||||||
ICPPUnknownMemberClass member = (ICPPUnknownMemberClass) type;
|
|
||||||
IType ownerType = member.getOwnerType();
|
|
||||||
IBinding[] candidates = lookInside(ownerType, false, member.getNameCharArray(), null, point);
|
|
||||||
if (candidates.length == 1) {
|
|
||||||
if (candidates[0] instanceof IType) {
|
|
||||||
IType result = (IType) candidates[0];
|
|
||||||
if (type instanceof ICPPUnknownMemberClassInstance) {
|
|
||||||
ICPPTemplateArgument[] args = ((ICPPUnknownMemberClassInstance) type).getArguments();
|
|
||||||
if (result instanceof ICPPClassTemplate) {
|
|
||||||
result = (IType) CPPTemplates.instantiate((ICPPClassTemplate) result, args, point);
|
|
||||||
} else if (result instanceof ICPPAliasTemplate) {
|
|
||||||
result = (IType) CPPTemplates.instantiateAliasTemplate((ICPPAliasTemplate) result,
|
|
||||||
args, point);
|
|
||||||
} else if (result instanceof ICPPAliasTemplateInstance) {
|
|
||||||
// TODO(nathanridge): Remove this branch once we properly represent
|
|
||||||
// specializations of alias templates (which will then implement
|
|
||||||
// ICPPAliasTemplate and be caught by the previous branch).
|
|
||||||
result = (IType) CPPTemplates.instantiateAliasTemplateInstance(
|
|
||||||
(ICPPAliasTemplateInstance) result, args, point);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
} else if (evaluation instanceof EvalFunctionCall) {
|
||||||
|
EvalFunctionCall evalFunctionCall = (EvalFunctionCall) evaluation;
|
||||||
|
ICPPEvaluation function = evalFunctionCall.getArguments()[0];
|
||||||
|
IType functionType = function.getType(point);
|
||||||
|
if (functionType instanceof ICPPUnknownType) {
|
||||||
|
functionType = resolveUnknownType((ICPPUnknownType) functionType, point);
|
||||||
|
}
|
||||||
|
return ExpressionTypes.typeFromFunctionCall(functionType);
|
||||||
|
} else if (evaluation instanceof EvalMemberAccess) {
|
||||||
|
IBinding member = ((EvalMemberAccess) evaluation).getMember();
|
||||||
|
// Presumably the type will be unknown. That's fine, it will be
|
||||||
|
// resolved during subsequent resolution rounds.
|
||||||
|
return typeForBinding(member);
|
||||||
|
}
|
||||||
|
// TODO(nathanridge): Handle more cases.
|
||||||
|
} else if (type instanceof ICPPUnknownMemberClass) {
|
||||||
|
ICPPUnknownMemberClass member = (ICPPUnknownMemberClass) type;
|
||||||
|
IType ownerType = member.getOwnerType();
|
||||||
|
IBinding[] candidates = lookInside(ownerType, false, member.getNameCharArray(), null, point);
|
||||||
|
if (candidates.length == 1) {
|
||||||
|
if (candidates[0] instanceof IType) {
|
||||||
|
IType result = (IType) candidates[0];
|
||||||
|
if (type instanceof ICPPUnknownMemberClassInstance) {
|
||||||
|
ICPPTemplateArgument[] args = ((ICPPUnknownMemberClassInstance) type).getArguments();
|
||||||
|
if (result instanceof ICPPClassTemplate) {
|
||||||
|
result = (IType) CPPTemplates.instantiate((ICPPClassTemplate) result, args, point);
|
||||||
|
} else if (result instanceof ICPPAliasTemplate) {
|
||||||
|
result = (IType) CPPTemplates.instantiateAliasTemplate((ICPPAliasTemplate) result,
|
||||||
|
args, point);
|
||||||
|
} else if (result instanceof ICPPAliasTemplateInstance) {
|
||||||
|
// TODO(nathanridge): Remove this branch once we properly represent
|
||||||
|
// specializations of alias templates (which will then implement
|
||||||
|
// ICPPAliasTemplate and be caught by the previous branch).
|
||||||
|
result = (IType) CPPTemplates.instantiateAliasTemplateInstance(
|
||||||
|
(ICPPAliasTemplateInstance) result, args, point);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
// Restore original resolution depth.
|
||||||
|
fResolutionDepth.set(resolutionDepth);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue