Search in sources :

Example 11 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class SignednessAnnotatedTypeFactory method maskIgnoresMSB.

/**
 * Given a masking operation of the form {@code expr & maskLit} or {@code expr | maskLit}, return
 * true iff the masking operation results in the same output regardless of the value of the
 * shiftAmount most significant bits of expr. This is if the shiftAmount most significant bits of
 * mask are 0 for AND, and 1 for OR. For example, assuming that shiftAmount is 4, the following is
 * true about AND and OR masks:
 *
 * <p>{@code expr & 0x0[anything] == 0x0[something] ;}
 *
 * <p>{@code expr | 0xF[anything] == 0xF[something] ;}
 *
 * @param maskKind the kind of mask (AND or OR)
 * @param shiftAmountLit the LiteralTree whose value is shiftAmount
 * @param maskLit the LiteralTree whose value is mask
 * @param shiftedTypeKind the type of shift operation; int or long
 * @return true iff the shiftAmount most significant bits of mask are 0 for AND, and 1 for OR
 */
private boolean maskIgnoresMSB(Tree.Kind maskKind, LiteralTree shiftAmountLit, LiteralTree maskLit, TypeKind shiftedTypeKind) {
    long shiftAmount = getLong(shiftAmountLit.getValue());
    // Shift of zero is a nop
    if (shiftAmount == 0) {
        return true;
    }
    long mask = getLong(maskLit.getValue());
    // bits, zeroing out the rest.
    if (shiftedTypeKind == TypeKind.INT) {
        mask <<= 32;
    }
    mask >>>= (64 - shiftAmount);
    if (maskKind == Tree.Kind.AND) {
        // Check that the shiftAmount most significant bits of the mask were 0.
        return mask == 0;
    } else if (maskKind == Tree.Kind.OR) {
        // Check that the shiftAmount most significant bits of the mask were 1.
        return mask == (1 << shiftAmount) - 1;
    } else {
        throw new TypeSystemError("Invalid Masking Operation");
    }
}
Also used : TypeSystemError(org.checkerframework.javacutil.TypeSystemError)

Example 12 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class GuiEffectTypeFactory method getComputedEffectAtCallsite.

/**
 * Get the effect of a method call at its callsite, acknowledging polymorphic instantiation using
 * type use annotations.
 *
 * @param node the method invocation as an AST node
 * @param callerReceiver the type of the receiver object if available. Used to resolve direct
 *     calls like "super()"
 * @param methodElt the element of the callee method
 * @return the computed effect (SafeEffect or UIEffect) for the method call
 */
public Effect getComputedEffectAtCallsite(MethodInvocationTree node, AnnotatedTypeMirror.AnnotatedDeclaredType callerReceiver, ExecutableElement methodElt) {
    Effect targetEffect = getDeclaredEffect(methodElt);
    if (targetEffect.isPoly()) {
        AnnotatedTypeMirror srcType = null;
        if (node.getMethodSelect().getKind() == Tree.Kind.MEMBER_SELECT) {
            ExpressionTree src = ((MemberSelectTree) node.getMethodSelect()).getExpression();
            srcType = getAnnotatedType(src);
        } else if (node.getMethodSelect().getKind() == Tree.Kind.IDENTIFIER) {
            // Tree.Kind.IDENTIFIER, e.g. a direct call like "super()"
            if (callerReceiver == null) {
                // Not enought information provided to instantiate this type-polymorphic effects
                return targetEffect;
            }
            srcType = callerReceiver;
        } else {
            throw new TypeSystemError("Unexpected getMethodSelect() kind at callsite " + node);
        }
        // Instantiate type-polymorphic effects
        if (srcType.hasAnnotation(AlwaysSafe.class)) {
            targetEffect = new Effect(SafeEffect.class);
        } else if (srcType.hasAnnotation(UI.class)) {
            targetEffect = new Effect(UIEffect.class);
        }
    // Poly substitution would be a noop.
    }
    return targetEffect;
}
Also used : PolyUI(org.checkerframework.checker.guieffect.qual.PolyUI) UI(org.checkerframework.checker.guieffect.qual.UI) MemberSelectTree(com.sun.source.tree.MemberSelectTree) SafeEffect(org.checkerframework.checker.guieffect.qual.SafeEffect) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) TypeSystemError(org.checkerframework.javacutil.TypeSystemError) SafeEffect(org.checkerframework.checker.guieffect.qual.SafeEffect) PolyUIEffect(org.checkerframework.checker.guieffect.qual.PolyUIEffect) UIEffect(org.checkerframework.checker.guieffect.qual.UIEffect) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 13 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class SourceChecker method typeProcess.

/**
 * Type-check the code using this checker's visitor.
 *
 * @see Processor#process(Set, RoundEnvironment)
 */
@Override
public void typeProcess(TypeElement e, TreePath p) {
    if (javacErrored) {
        reportJavacError(p);
        return;
    }
    // Cannot use BugInCF here because it is outside of the try/catch for BugInCF.
    if (e == null) {
        messager.printMessage(Kind.ERROR, "Refusing to process empty TypeElement");
        return;
    }
    if (p == null) {
        messager.printMessage(Kind.ERROR, "Refusing to process empty TreePath in TypeElement: " + e);
        return;
    }
    if (!warnedAboutGarbageCollection) {
        String gcUsageMessage = SystemPlume.gcUsageMessage(.25, 60);
        if (gcUsageMessage != null) {
            messager.printMessage(Kind.WARNING, gcUsageMessage);
            warnedAboutGarbageCollection = true;
        }
    }
    Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    Source source = Source.instance(context);
    // Also the enum constant Source.JDK1_8 was renamed at some point...
    if (!warnedAboutSourceLevel && source.compareTo(Source.lookup("8")) < 0) {
        messager.printMessage(Kind.WARNING, "-source " + source.name + " does not support type annotations");
        warnedAboutSourceLevel = true;
    }
    Log log = Log.instance(context);
    if (log.nerrors > this.errsOnLastExit) {
        this.errsOnLastExit = log.nerrors;
        javacErrored = true;
        reportJavacError(p);
        return;
    }
    if (visitor == null) {
        // there. Don't also cause a NPE here.
        return;
    }
    if (p.getCompilationUnit() != currentRoot) {
        setRoot(p.getCompilationUnit());
        if (hasOption("filenames")) {
            // TODO: Have a command-line option to turn the timestamps on/off too, because
            // they are nondeterministic across runs.
            // Add timestamp to indicate how long operations are taking.
            // Duplicate messages are suppressed, so this might not appear in front of every "
            // is type-checking " message (when a file takes less than a second to type-check).
            message(Kind.NOTE, Instant.now().toString());
            message(Kind.NOTE, "%s is type-checking %s", (Object) this.getClass().getSimpleName(), currentRoot.getSourceFile().getName());
        }
    }
    // Visit the attributed tree.
    try {
        visitor.visit(p);
        warnUnneededSuppressions();
    } catch (UserError ce) {
        logUserError(ce);
    } catch (TypeSystemError ce) {
        logTypeSystemError(ce);
    } catch (BugInCF ce) {
        logBugInCF(ce);
    } catch (Throwable t) {
        logBugInCF(wrapThrowableAsBugInCF("SourceChecker.typeProcess", t, p));
    } finally {
        // Also add possibly deferred diagnostics, which will get published back in
        // AbstractTypeProcessor.
        this.errsOnLastExit = log.nerrors;
    }
}
Also used : Context(com.sun.tools.javac.util.Context) UserError(org.checkerframework.javacutil.UserError) Log(com.sun.tools.javac.util.Log) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) TypeSystemError(org.checkerframework.javacutil.TypeSystemError) BugInCF(org.checkerframework.javacutil.BugInCF) Source(com.sun.tools.javac.code.Source) DiagnosticSource(com.sun.tools.javac.util.DiagnosticSource)

Example 14 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class NullnessTransfer method hasNullableValueType.

/**
 * Returns true if mapType's value type (the V type argument to Map) is @Nullable.
 *
 * @param mapOrSubtype the Map type, or a subtype
 * @return true if mapType's value type is @Nullable
 */
private boolean hasNullableValueType(AnnotatedTypeMirror mapOrSubtype) {
    AnnotatedDeclaredType mapType = AnnotatedTypes.asSuper(nullnessTypeFactory, mapOrSubtype, MAP_TYPE);
    int numTypeArguments = mapType.getTypeArguments().size();
    if (numTypeArguments != 2) {
        throw new TypeSystemError("Wrong number %d of type arguments: %s", numTypeArguments, mapType);
    }
    AnnotatedTypeMirror valueType = mapType.getTypeArguments().get(1);
    return valueType.hasAnnotation(NULLABLE);
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) TypeSystemError(org.checkerframework.javacutil.TypeSystemError) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 15 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class MustCallConsistencyAnalyzer method getParametersOfInvocation.

/**
 * Get the elements representing the formal parameters of a method or constructor, from an
 * invocation of that method or constructor.
 *
 * @param node a method invocation or object creation node
 * @return a list of the declarations of the formal parameters of the method or constructor being
 *     invoked
 */
private List<? extends VariableElement> getParametersOfInvocation(Node node) {
    ExecutableElement executableElement;
    if (node instanceof MethodInvocationNode) {
        MethodInvocationNode invocationNode = (MethodInvocationNode) node;
        executableElement = TreeUtils.elementFromUse(invocationNode.getTree());
    } else if (node instanceof ObjectCreationNode) {
        executableElement = TreeUtils.elementFromUse(((ObjectCreationNode) node).getTree());
    } else {
        throw new TypeSystemError("unexpected node type " + node.getClass());
    }
    return executableElement.getParameters();
}
Also used : MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) ExecutableElement(javax.lang.model.element.ExecutableElement) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) TypeSystemError(org.checkerframework.javacutil.TypeSystemError)

Aggregations

TypeSystemError (org.checkerframework.javacutil.TypeSystemError)23 AnnotationMirror (javax.lang.model.element.AnnotationMirror)6 ExecutableElement (javax.lang.model.element.ExecutableElement)4 Range (org.checkerframework.common.value.util.Range)4 Element (javax.lang.model.element.Element)3 VariableElement (javax.lang.model.element.VariableElement)3 RequiresNonNull (org.checkerframework.checker.nullness.qual.RequiresNonNull)3 ArrayLenRange (org.checkerframework.common.value.qual.ArrayLenRange)3 MethodTree (com.sun.source.tree.MethodTree)2 ArrayList (java.util.ArrayList)2 TypeElement (javax.lang.model.element.TypeElement)2 MustCallAnnotatedTypeFactory (org.checkerframework.checker.mustcall.MustCallAnnotatedTypeFactory)2 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)2 ObjectCreationNode (org.checkerframework.dataflow.cfg.node.ObjectCreationNode)2 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)2 AnnotationBuilder (org.checkerframework.javacutil.AnnotationBuilder)2 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)1 MemberReferenceTree (com.sun.source.tree.MemberReferenceTree)1