Search in sources :

Example 31 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment 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 32 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project checker-framework by typetools.

the class BaseTypeChecker method typeProcess.

// AbstractTypeProcessor delegation
@Override
public void typeProcess(TypeElement element, TreePath tree) {
    if (!getSubcheckers().isEmpty()) {
        // TODO: I expected this to only be necessary if (parentChecker == null).
        // However, the NestedAggregateChecker fails otherwise.
        messageStore.clear();
    }
    // Errors (or other messages) issued via
    // SourceChecker#message(Diagnostic.Kind, Object, String, Object...)
    // are stored in messageStore until all checkers have processed this compilation unit.
    // All other messages are printed immediately.  This includes errors issued because the
    // checker threw an exception.
    // In order to run the next checker on this compilation unit even if the previous issued errors,
    // the next checker's errsOnLastExit needs to include all errors issued by previous checkers.
    Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
    Log log = Log.instance(context);
    int nerrorsOfAllPreviousCheckers = this.errsOnLastExit;
    for (BaseTypeChecker subchecker : getSubcheckers()) {
        subchecker.errsOnLastExit = nerrorsOfAllPreviousCheckers;
        subchecker.messageStore = messageStore;
        int errorsBeforeTypeChecking = log.nerrors;
        subchecker.typeProcess(element, tree);
        int errorsAfterTypeChecking = log.nerrors;
        nerrorsOfAllPreviousCheckers += errorsAfterTypeChecking - errorsBeforeTypeChecking;
    }
    this.errsOnLastExit = nerrorsOfAllPreviousCheckers;
    super.typeProcess(element, tree);
    if (!getSubcheckers().isEmpty()) {
        printStoredMessages(tree.getCompilationUnit());
        // Update errsOnLastExit to reflect the errors issued.
        this.errsOnLastExit = log.nerrors;
    }
}
Also used : Context(com.sun.tools.javac.util.Context) Log(com.sun.tools.javac.util.Log) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Example 33 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project checker-framework by typetools.

the class TypesUtils method freshTypeVariable.

/**
 * If {@code typeMirror} is a wildcard, returns a fresh type variable that will be used as a
 * captured type variable for it. If {@code typeMirror} is not a wildcard, returns {@code
 * typeMirror}.
 *
 * @param typeMirror a type
 * @param env processing environment
 * @return a fresh type variable if {@code typeMirror} is a wildcard, otherwise {@code typeMirror}
 */
public static TypeMirror freshTypeVariable(TypeMirror typeMirror, ProcessingEnvironment env) {
    JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) env;
    com.sun.tools.javac.code.Types types = com.sun.tools.javac.code.Types.instance(javacEnv.getContext());
    return types.freshTypeVariables(com.sun.tools.javac.util.List.of((Type) typeMirror)).head;
}
Also used : JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Example 34 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project checker-framework by typetools.

the class TypesUtils method substitute.

/**
 * Returns a new type mirror with the same type as {@code type} where all the type variables in
 * {@code typeVariables} have been substituted with the type arguments in {@code typeArgs}.
 *
 * <p>This is a wrapper around {@link com.sun.tools.javac.code.Types#subst(Type,
 * com.sun.tools.javac.util.List, com.sun.tools.javac.util.List)}.
 *
 * @param type type to do substitution in
 * @param typeVariables type variables that should be replaced with the type mirror at the same
 *     index of {@code typeArgs}
 * @param typeArgs type mirrors that should replace the type variable at the same index of {@code
 *     typeVariables}
 * @param env processing environment
 * @return a new type mirror with the same type as {@code type} where all the type variables in
 *     {@code typeVariables} have been substituted with the type arguments in {@code typeArgs}
 */
public static TypeMirror substitute(TypeMirror type, List<? extends TypeMirror> typeVariables, List<? extends TypeMirror> typeArgs, ProcessingEnvironment env) {
    List<Type> newP = CollectionsPlume.mapList(Type.class::cast, typeVariables);
    List<Type> newT = CollectionsPlume.mapList(Type.class::cast, typeArgs);
    JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) env;
    com.sun.tools.javac.code.Types types = com.sun.tools.javac.code.Types.instance(javacEnv.getContext());
    return types.subst((Type) type, com.sun.tools.javac.util.List.from(newP), com.sun.tools.javac.util.List.from(newT));
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) CapturedType(com.sun.tools.javac.code.Type.CapturedType) DeclaredType(javax.lang.model.type.DeclaredType) WildcardType(javax.lang.model.type.WildcardType) ArrayType(javax.lang.model.type.ArrayType) UnionType(javax.lang.model.type.UnionType) PrimitiveType(javax.lang.model.type.PrimitiveType) Type(com.sun.tools.javac.code.Type) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Example 35 with JavacProcessingEnvironment

use of com.sun.tools.javac.processing.JavacProcessingEnvironment in project checker-framework by typetools.

the class TypesUtils method wildLowerBound.

/**
 * Version of com.sun.tools.javac.code.Types.wildLowerBound(Type) that works with both jdk8
 * (called upperBound there) and jdk8u.
 */
public static Type wildLowerBound(TypeMirror tm, ProcessingEnvironment env) {
    Type t = (Type) tm;
    if (t.hasTag(TypeTag.WILDCARD)) {
        Context context = ((JavacProcessingEnvironment) env).getContext();
        Symtab syms = Symtab.instance(context);
        Type.WildcardType w = (Type.WildcardType) TypeAnnotationUtils.unannotatedType(t);
        return w.isExtendsBound() ? syms.botType : wildLowerBound(w.type, env);
    } else {
        return TypeAnnotationUtils.unannotatedType(t);
    }
}
Also used : Context(com.sun.tools.javac.util.Context) Symtab(com.sun.tools.javac.code.Symtab) ClassType(com.sun.tools.javac.code.Type.ClassType) CapturedType(com.sun.tools.javac.code.Type.CapturedType) DeclaredType(javax.lang.model.type.DeclaredType) WildcardType(javax.lang.model.type.WildcardType) ArrayType(javax.lang.model.type.ArrayType) UnionType(javax.lang.model.type.UnionType) PrimitiveType(javax.lang.model.type.PrimitiveType) Type(com.sun.tools.javac.code.Type) WildcardType(javax.lang.model.type.WildcardType) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment)

Aggregations

JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)49 Context (com.sun.tools.javac.util.Context)30 Type (com.sun.tools.javac.code.Type)13 WildcardType (javax.lang.model.type.WildcardType)11 DeclaredType (javax.lang.model.type.DeclaredType)9 ArrayList (java.util.ArrayList)7 ArrayType (javax.lang.model.type.ArrayType)7 TreePath (com.sun.source.util.TreePath)6 Symtab (com.sun.tools.javac.code.Symtab)6 CapturedType (com.sun.tools.javac.code.Type.CapturedType)6 ClassType (com.sun.tools.javac.code.Type.ClassType)6 PrimitiveType (javax.lang.model.type.PrimitiveType)6 UnionType (javax.lang.model.type.UnionType)6 Symbol (com.sun.tools.javac.code.Symbol)5 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)5 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)5 Types (com.sun.tools.javac.code.Types)5 VariableTree (com.sun.source.tree.VariableTree)4 AttrContext (com.sun.tools.javac.comp.AttrContext)4 Log (com.sun.tools.javac.util.Log)3