use of org.checkerframework.dataflow.cfg.node.MethodInvocationNode in project checker-framework by typetools.
the class InitializationTransfer method initializedFieldsAfterCall.
/**
* Returns the fields that can safely be considered initialized after the method call {@code
* node}.
*/
protected List<VariableElement> initializedFieldsAfterCall(MethodInvocationNode node, ConditionalTransferResult<V, S> transferResult) {
List<VariableElement> result = new ArrayList<>();
MethodInvocationTree tree = node.getTree();
ExecutableElement method = TreeUtils.elementFromUse(tree);
boolean isConstructor = method.getSimpleName().contentEquals("<init>");
Node receiver = node.getTarget().getReceiver();
String methodString = tree.getMethodSelect().toString();
// invariant fields are guaranteed to be initialized.
if (isConstructor && receiver instanceof ThisLiteralNode && methodString.equals("this")) {
ClassTree clazz = TreeUtils.enclosingClass(analysis.getTypeFactory().getPath(tree));
TypeElement clazzElem = TreeUtils.elementFromDeclaration(clazz);
markInvariantFieldsAsInitialized(result, clazzElem);
}
// invariant fields of any super class are guaranteed to be initialized.
if (isConstructor && receiver instanceof ThisLiteralNode && methodString.equals("super")) {
ClassTree clazz = TreeUtils.enclosingClass(analysis.getTypeFactory().getPath(tree));
TypeElement clazzElem = TreeUtils.elementFromDeclaration(clazz);
TypeMirror superClass = clazzElem.getSuperclass();
while (superClass != null && superClass.getKind() != TypeKind.NONE) {
clazzElem = (TypeElement) analysis.getTypes().asElement(superClass);
superClass = clazzElem.getSuperclass();
markInvariantFieldsAsInitialized(result, clazzElem);
}
}
return result;
}
use of org.checkerframework.dataflow.cfg.node.MethodInvocationNode in project checker-framework by typetools.
the class RegexTransfer method handleMatcherGroupCount.
/**
* See whether possibleMatcher is a call of groupCount on a Matcher and possibleConstant is a
* constant. If so, annotate the matcher as constant + 1 if !isAlsoEqual constant if isAlsoEqual
*
* @param possibleMatcher the Node that might be a call of Matcher.groupCount()
* @param possibleConstant the Node that might be a constant
* @param isAlsoEqual whether the comparison operation is strict or reflexive
* @param in the TransferInput
* @param resultIn TransferResult
* @return the possibly refined output TransferResult
*/
private TransferResult<CFValue, CFStore> handleMatcherGroupCount(Node possibleMatcher, Node possibleConstant, boolean isAlsoEqual, TransferInput<CFValue, CFStore> in, TransferResult<CFValue, CFStore> resultIn) {
if (!(possibleMatcher instanceof MethodInvocationNode)) {
return resultIn;
}
if (!(possibleConstant instanceof IntegerLiteralNode)) {
return resultIn;
}
MethodAccessNode methodAccessNode = ((MethodInvocationNode) possibleMatcher).getTarget();
ExecutableElement method = methodAccessNode.getMethod();
Node receiver = methodAccessNode.getReceiver();
if (!isMatcherGroupCountMethod(method, receiver)) {
return resultIn;
}
Receiver matcherReceiver = FlowExpressions.internalReprOf(analysis.getTypeFactory(), receiver);
IntegerLiteralNode iln = (IntegerLiteralNode) possibleConstant;
int groupCount;
if (isAlsoEqual) {
groupCount = iln.getValue();
} else {
groupCount = iln.getValue() + 1;
}
CFStore thenStore = resultIn.getRegularStore();
CFStore elseStore = thenStore.copy();
ConditionalTransferResult<CFValue, CFStore> newResult = new ConditionalTransferResult<>(resultIn.getResultValue(), thenStore, elseStore);
RegexAnnotatedTypeFactory factory = (RegexAnnotatedTypeFactory) analysis.getTypeFactory();
AnnotationMirror regexAnnotation = factory.createRegexAnnotation(groupCount);
thenStore.insertValue(matcherReceiver, regexAnnotation);
return newResult;
}
use of org.checkerframework.dataflow.cfg.node.MethodInvocationNode in project checker-framework by typetools.
the class CFAbstractTransfer method getValueFromFactory.
/**
* Returns the abstract value of a non-leaf tree {@code tree}, as computed by the {@link
* AnnotatedTypeFactory}.
*
* @return the abstract value of a non-leaf tree {@code tree}, as computed by the {@link
* AnnotatedTypeFactory}
*/
protected V getValueFromFactory(Tree tree, Node node) {
GenericAnnotatedTypeFactory<V, S, T, ? extends CFAbstractAnalysis<V, S, T>> factory = analysis.atypeFactory;
Tree preTree = analysis.getCurrentTree();
analysis.setCurrentTree(tree);
AnnotatedTypeMirror at;
if (node instanceof MethodInvocationNode && ((MethodInvocationNode) node).getIterableExpression() != null) {
ExpressionTree iter = ((MethodInvocationNode) node).getIterableExpression();
at = factory.getIterableElementType(iter);
} else if (node instanceof ArrayAccessNode && ((ArrayAccessNode) node).getArrayExpression() != null) {
ExpressionTree array = ((ArrayAccessNode) node).getArrayExpression();
at = factory.getIterableElementType(array);
} else {
at = factory.getAnnotatedType(tree);
}
analysis.setCurrentTree(preTree);
return analysis.createAbstractValue(at);
}
use of org.checkerframework.dataflow.cfg.node.MethodInvocationNode in project checker-framework by typetools.
the class CFAbstractTransfer method processPostconditionsAndConditionalPostconditions.
/**
* Add information from the postconditions and conditional postconditions of a method to the
* stores after an invocation.
*
* @param invocationNode a method call
* @param invocationTree the tree for the method call
* @param thenStore the "then" store; is side-effected by this method
* @param elseStore the "else" store; is side-effected by this method
* @param postconditions the postconditions
*/
private void processPostconditionsAndConditionalPostconditions(MethodInvocationNode invocationNode, Tree invocationTree, S thenStore, S elseStore, Set<? extends Contract> postconditions) {
StringToJavaExpression stringToJavaExpr = stringExpr -> StringToJavaExpression.atMethodInvocation(stringExpr, invocationNode, analysis.checker);
for (Contract p : postconditions) {
// Viewpoint-adapt to the method use (the call site).
AnnotationMirror anno = p.viewpointAdaptDependentTypeAnnotation(analysis.atypeFactory, stringToJavaExpr, /*errorTree=*/
null);
String expressionString = p.expressionString;
try {
JavaExpression je = stringToJavaExpr.toJavaExpression(expressionString);
// are removed from the store before this method is called.
if (p.kind == Contract.Kind.CONDITIONALPOSTCONDITION) {
if (((ConditionalPostcondition) p).resultValue) {
thenStore.insertOrRefinePermitNondeterministic(je, anno);
} else {
elseStore.insertOrRefinePermitNondeterministic(je, anno);
}
} else {
thenStore.insertOrRefinePermitNondeterministic(je, anno);
}
} catch (JavaExpressionParseException e) {
// report errors here
if (e.isFlowParseError()) {
Object[] args = new Object[e.args.length + 1];
args[0] = ElementUtils.getSimpleSignature(TreeUtils.elementFromUse(invocationNode.getTree()));
System.arraycopy(e.args, 0, args, 1, e.args.length);
analysis.checker.reportError(invocationTree, "flowexpr.parse.error.postcondition", args);
} else {
analysis.checker.report(invocationTree, e.getDiagMessage());
}
}
}
}
use of org.checkerframework.dataflow.cfg.node.MethodInvocationNode in project checker-framework by typetools.
the class CalledMethodsTransfer method visitMethodInvocation.
@Override
public TransferResult<CFValue, CFStore> visitMethodInvocation(final MethodInvocationNode node, final TransferInput<CFValue, CFStore> input) {
exceptionalStores = makeExceptionalStores(node, input);
TransferResult<CFValue, CFStore> superResult = super.visitMethodInvocation(node, input);
handleEnsuresCalledMethodsVarArgs(node, superResult);
Node receiver = node.getTarget().getReceiver();
if (receiver != null) {
String methodName = node.getTarget().getMethod().getSimpleName().toString();
methodName = ((CalledMethodsAnnotatedTypeFactory) atypeFactory).adjustMethodNameUsingValueChecker(methodName, node.getTree());
accumulate(receiver, superResult, methodName);
}
TransferResult<CFValue, CFStore> finalResult = new ConditionalTransferResult<>(superResult.getResultValue(), superResult.getThenStore(), superResult.getElseStore(), exceptionalStores);
exceptionalStores = null;
return finalResult;
}
Aggregations