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;
}
}
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;
}
}
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;
}
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));
}
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);
}
}
Aggregations