Search in sources :

Example 16 with Log

use of com.sun.tools.javac.util.Log 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 17 with Log

use of com.sun.tools.javac.util.Log 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 18 with Log

use of com.sun.tools.javac.util.Log in project checker-framework by typetools.

the class Resolver method findPackage.

/**
 * Finds the package with name {@code name}.
 *
 * @param name the name of the package
 * @param path the tree path to the local scope
 * @return the {@code PackageSymbol} for the package if it is found, {@code null} otherwise
 */
@Nullable
public PackageSymbol findPackage(String name, TreePath path) {
    Log.DiagnosticHandler discardDiagnosticHandler = new Log.DiscardDiagnosticHandler(log);
    try {
        Env<AttrContext> env = getEnvForPath(path);
        Element res = wrapInvocationOnResolveInstance(FIND_IDENT, env, names.fromString(name), Kinds.KindSelector.PCK);
        // a.b.c.MyClass.myStaticField. "exists()" must be called on it to ensure that it exists.
        if (res.getKind() == ElementKind.PACKAGE) {
            PackageSymbol ps = (PackageSymbol) res;
            return ps.exists() ? ps : null;
        } else {
            return null;
        }
    } finally {
        log.popDiagnosticHandler(discardDiagnosticHandler);
    }
}
Also used : PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) Log(com.sun.tools.javac.util.Log) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) AttrContext(com.sun.tools.javac.comp.AttrContext) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 19 with Log

use of com.sun.tools.javac.util.Log in project checker-framework by typetools.

the class CFGProcessor method typeProcessingOver.

@Override
public void typeProcessingOver() {
    if (rootTree == null) {
        result = new CFGProcessResult("Root tree is null.");
    } else if (classTree == null) {
        result = new CFGProcessResult("Method tree is null.");
    } else if (methodTree == null) {
        result = new CFGProcessResult("Class tree is null.");
    } else {
        Log log = getCompilerLog();
        if (log.nerrors > 0) {
            result = new CFGProcessResult("Compilation issued an error.");
        } else {
            ControlFlowGraph cfg = CFGBuilder.build(rootTree, methodTree, classTree, processingEnv);
            result = new CFGProcessResult(cfg);
        }
    }
    super.typeProcessingOver();
}
Also used : Log(com.sun.tools.javac.util.Log)

Example 20 with Log

use of com.sun.tools.javac.util.Log in project checker-framework by typetools.

the class Resolver method findLocalVariableOrParameter.

/**
 * Finds the local variable (including formal parameters) with name {@code name} in the given
 * scope.
 *
 * @param name the name of the local variable
 * @param path the tree path to the local scope
 * @return the element for the local variable, {@code null} otherwise
 */
@Nullable
public VariableElement findLocalVariableOrParameter(String name, TreePath path) {
    Log.DiagnosticHandler discardDiagnosticHandler = new Log.DiscardDiagnosticHandler(log);
    try {
        Env<AttrContext> env = getEnvForPath(path);
        Element res = wrapInvocationOnResolveInstance(FIND_VAR, env, names.fromString(name));
        if (res.getKind() == ElementKind.LOCAL_VARIABLE || res.getKind() == ElementKind.PARAMETER) {
            return (VariableElement) res;
        } else {
            // The Element might be FIELD or a SymbolNotFoundError.
            return null;
        }
    } finally {
        log.popDiagnosticHandler(discardDiagnosticHandler);
    }
}
Also used : Log(com.sun.tools.javac.util.Log) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) AttrContext(com.sun.tools.javac.comp.AttrContext) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

Log (com.sun.tools.javac.util.Log)23 AttrContext (com.sun.tools.javac.comp.AttrContext)9 Element (javax.lang.model.element.Element)8 VariableElement (javax.lang.model.element.VariableElement)8 Context (com.sun.tools.javac.util.Context)7 ExecutableElement (javax.lang.model.element.ExecutableElement)5 JavaFileObject (javax.tools.JavaFileObject)5 Nullable (org.checkerframework.checker.nullness.qual.Nullable)5 JavacScope (com.sun.tools.javac.api.JavacScope)3 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)3 JCTree (com.sun.tools.javac.tree.JCTree)3 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)3 Type (com.sun.tools.javac.code.Type)2 Parser (com.sun.tools.javac.parser.Parser)2 ParserFactory (com.sun.tools.javac.parser.ParserFactory)2 Name (com.sun.tools.javac.util.Name)2 TypeMirror (javax.lang.model.type.TypeMirror)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Supplier (com.google.common.base.Supplier)1 Suppliers (com.google.common.base.Suppliers)1