Search in sources :

Example 1 with IntegerLiteralNode

use of org.checkerframework.dataflow.cfg.node.IntegerLiteralNode in project checker-framework by typetools.

the class RegexTransfer method handleRegexUtil.

private TransferResult<CFValue, CFStore> handleRegexUtil(MethodInvocationNode n, ExecutableElement method, TransferResult<CFValue, CFStore> result) {
    RegexAnnotatedTypeFactory factory = (RegexAnnotatedTypeFactory) analysis.getTypeFactory();
    if (ElementUtils.matchesElement(method, IS_REGEX_METHOD_NAME, String.class, int.class)) {
        // RegexUtil.isRegex(s, groups) method
        // (No special case is needed for isRegex(String) because of
        // the annotation on that method's definition.)
        CFStore thenStore = result.getRegularStore();
        CFStore elseStore = thenStore.copy();
        ConditionalTransferResult<CFValue, CFStore> newResult = new ConditionalTransferResult<>(result.getResultValue(), thenStore, elseStore);
        Receiver firstParam = FlowExpressions.internalReprOf(factory.getContext().getAnnotationProvider(), n.getArgument(0));
        // add annotation with correct group count (if possible,
        // regex annotation without count otherwise)
        Node count = n.getArgument(1);
        int groupCount;
        if (count instanceof IntegerLiteralNode) {
            IntegerLiteralNode iln = (IntegerLiteralNode) count;
            groupCount = iln.getValue();
        } else {
            groupCount = 0;
        }
        AnnotationMirror regexAnnotation = factory.createRegexAnnotation(groupCount);
        thenStore.insertValue(firstParam, regexAnnotation);
        return newResult;
    } else if (ElementUtils.matchesElement(method, AS_REGEX_METHOD_NAME, String.class, int.class)) {
        // RegexUtil.asRegex(s, groups) method
        // (No special case is needed for asRegex(String) because of
        // the annotation on that method's definition.)
        // add annotation with correct group count (if possible,
        // regex annotation without count otherwise)
        AnnotationMirror regexAnnotation;
        Node count = n.getArgument(1);
        int groupCount;
        if (count instanceof IntegerLiteralNode) {
            IntegerLiteralNode iln = (IntegerLiteralNode) count;
            groupCount = iln.getValue();
        } else {
            groupCount = 0;
        }
        regexAnnotation = factory.createRegexAnnotation(groupCount);
        CFValue newResultValue = analysis.createSingleAnnotationValue(regexAnnotation, result.getResultValue().getUnderlyingType());
        return new RegularTransferResult<>(newResultValue, result.getRegularStore());
    }
    return result;
}
Also used : CFValue(org.checkerframework.framework.flow.CFValue) AnnotationMirror(javax.lang.model.element.AnnotationMirror) CFStore(org.checkerframework.framework.flow.CFStore) ConditionalTransferResult(org.checkerframework.dataflow.analysis.ConditionalTransferResult) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) LessThanNode(org.checkerframework.dataflow.cfg.node.LessThanNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) GreaterThanNode(org.checkerframework.dataflow.cfg.node.GreaterThanNode) LessThanOrEqualNode(org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode) GreaterThanOrEqualNode(org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode) Node(org.checkerframework.dataflow.cfg.node.Node) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode)

Example 2 with IntegerLiteralNode

use of org.checkerframework.dataflow.cfg.node.IntegerLiteralNode 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;
}
Also used : CFStore(org.checkerframework.framework.flow.CFStore) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) ConditionalTransferResult(org.checkerframework.dataflow.analysis.ConditionalTransferResult) ExecutableElement(javax.lang.model.element.ExecutableElement) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) LessThanNode(org.checkerframework.dataflow.cfg.node.LessThanNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) GreaterThanNode(org.checkerframework.dataflow.cfg.node.GreaterThanNode) LessThanOrEqualNode(org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode) GreaterThanOrEqualNode(org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode) Node(org.checkerframework.dataflow.cfg.node.Node) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) CFValue(org.checkerframework.framework.flow.CFValue) AnnotationMirror(javax.lang.model.element.AnnotationMirror) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode)

Aggregations

AnnotationMirror (javax.lang.model.element.AnnotationMirror)2 ConditionalTransferResult (org.checkerframework.dataflow.analysis.ConditionalTransferResult)2 Receiver (org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)2 ClassNameNode (org.checkerframework.dataflow.cfg.node.ClassNameNode)2 GreaterThanNode (org.checkerframework.dataflow.cfg.node.GreaterThanNode)2 GreaterThanOrEqualNode (org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode)2 IntegerLiteralNode (org.checkerframework.dataflow.cfg.node.IntegerLiteralNode)2 LessThanNode (org.checkerframework.dataflow.cfg.node.LessThanNode)2 LessThanOrEqualNode (org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode)2 MethodAccessNode (org.checkerframework.dataflow.cfg.node.MethodAccessNode)2 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)2 Node (org.checkerframework.dataflow.cfg.node.Node)2 CFStore (org.checkerframework.framework.flow.CFStore)2 CFValue (org.checkerframework.framework.flow.CFValue)2 ExecutableElement (javax.lang.model.element.ExecutableElement)1