use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class ForwardAnalysisImpl method performAnalysis.
@Override
public void performAnalysis(ControlFlowGraph cfg) {
if (isRunning) {
throw new BugInCF("ForwardAnalysisImpl::performAnalysis() shouldn't be called when the analysis is" + " running.");
}
isRunning = true;
try {
init(cfg);
while (!worklist.isEmpty()) {
Block b = worklist.poll();
performAnalysisBlock(b);
}
} finally {
assert isRunning;
// In case performAnalysisBlock crashed, reset isRunning to false.
isRunning = false;
}
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class AnnotatedTypeFactory method getCheckerNames.
/**
* Returns the names of the annotation processors that are being run.
*
* @return the names of the annotation processors that are being run
*/
// ClassLoader.getResources returns an Enumeration
@SuppressWarnings("JdkObsolete")
public String[] getCheckerNames() {
com.sun.tools.javac.util.Context context = ((JavacProcessingEnvironment) processingEnv).getContext();
String processorArg = Options.instance(context).get("-processor");
if (processorArg != null) {
return processorArg.split(",");
}
try {
String filename = "META-INF/services/javax.annotation.processing.Processor";
List<String> lines = new ArrayList<>();
Enumeration<URL> urls = getClass().getClassLoader().getResources(filename);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
lines.addAll(in.lines().collect(Collectors.toList()));
}
String[] result = lines.toArray(new String[0]);
return result;
} catch (IOException e) {
throw new BugInCF(e);
}
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class AnnotatedTypeFactory method methodFromUse.
/**
* Determines the type of the invoked method based on the passed expression tree, executable
* element, and receiver type.
*
* @param tree either a MethodInvocationTree or a MemberReferenceTree
* @param methodElt the element of the referenced method
* @param receiverType the type of the receiver
* @return the method type being invoked with tree and the (inferred) type arguments
* @see #methodFromUse(MethodInvocationTree)
*/
public ParameterizedExecutableType methodFromUse(ExpressionTree tree, ExecutableElement methodElt, AnnotatedTypeMirror receiverType) {
AnnotatedExecutableType memberTypeWithoutOverrides = // get unsubstituted type
getAnnotatedType(methodElt);
AnnotatedExecutableType memberTypeWithOverrides = applyFakeOverrides(receiverType, methodElt, memberTypeWithoutOverrides);
memberTypeWithOverrides = applyRecordTypesToAccessors(methodElt, memberTypeWithOverrides);
methodFromUsePreSubstitution(tree, memberTypeWithOverrides);
AnnotatedExecutableType methodType = AnnotatedTypes.asMemberOf(types, this, receiverType, methodElt, memberTypeWithOverrides);
List<AnnotatedTypeMirror> typeargs = new ArrayList<>(methodType.getTypeVariables().size());
Map<TypeVariable, AnnotatedTypeMirror> typeParamToTypeArg = AnnotatedTypes.findTypeArguments(processingEnv, this, tree, methodElt, methodType);
if (!typeParamToTypeArg.isEmpty()) {
typeParamToTypeArg = captureMethodTypeArgs(typeParamToTypeArg, memberTypeWithOverrides.getTypeVariables());
for (AnnotatedTypeVariable tv : methodType.getTypeVariables()) {
if (typeParamToTypeArg.get(tv.getUnderlyingType()) == null) {
throw new BugInCF("AnnotatedTypeFactory.methodFromUse:mismatch between declared method type variables" + " and the inferred method type arguments. Method type variables: " + methodType.getTypeVariables() + "; " + "Inferred method type arguments: " + typeParamToTypeArg);
}
typeargs.add(typeParamToTypeArg.get(tv.getUnderlyingType()));
}
methodType = (AnnotatedExecutableType) typeVarSubstitutor.substitute(typeParamToTypeArg, methodType);
}
if (tree.getKind() == Tree.Kind.METHOD_INVOCATION && TreeUtils.isMethodInvocation(tree, objectGetClass, processingEnv)) {
adaptGetClassReturnTypeToReceiver(methodType, receiverType, tree);
}
return new ParameterizedExecutableType(methodType, typeargs);
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class AnnotationFileParser method sameType.
/**
* Returns true if the two types are the same.
*
* @param javacType type in javac form
* @param javaParserType type in JavaParser form
* @return true if the two types are the same
*/
private boolean sameType(TypeMirror javacType, Type javaParserType) {
switch(javacType.getKind()) {
case BOOLEAN:
return javaParserType.equals(PrimitiveType.booleanType());
case BYTE:
return javaParserType.equals(PrimitiveType.byteType());
case CHAR:
return javaParserType.equals(PrimitiveType.charType());
case DOUBLE:
return javaParserType.equals(PrimitiveType.doubleType());
case FLOAT:
return javaParserType.equals(PrimitiveType.floatType());
case INT:
return javaParserType.equals(PrimitiveType.intType());
case LONG:
return javaParserType.equals(PrimitiveType.longType());
case SHORT:
return javaParserType.equals(PrimitiveType.shortType());
case DECLARED:
case TYPEVAR:
if (!(javaParserType instanceof ClassOrInterfaceType)) {
return false;
}
com.sun.tools.javac.code.Type javacTypeInternal = (com.sun.tools.javac.code.Type) javacType;
ClassOrInterfaceType javaParserClassType = (ClassOrInterfaceType) javaParserType;
// Use asString() because toString() includes annotations.
String javaParserString = javaParserClassType.asString();
Element javacElement = javacTypeInternal.asElement();
// Check both fully-qualified name and simple name.
return javacElement.toString().equals(javaParserString) || javacElement.getSimpleName().contentEquals(javaParserString);
case ARRAY:
return javaParserType.isArrayType() && sameType(((ArrayType) javacType).getComponentType(), javaParserType.asArrayType().getComponentType());
default:
throw new BugInCF("Unhandled type %s of kind %s", javacType, javacType.getKind());
}
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class SourceChecker method shouldSuppressWarnings.
/**
* Determines whether all the warnings pertaining to a given tree should be suppressed. Returns
* true if the tree is within the scope of a @SuppressWarnings annotation, one of whose values
* suppresses the checker's warnings. Also, returns true if the {@code errKey} matches a string in
* {@code -AsuppressWarnings}.
*
* @param tree the tree that might be a source of a warning
* @param errKey the error key the checker is emitting
* @return true if no warning should be emitted for the given tree because it is contained by a
* declaration with an appropriately-valued {@literal @}SuppressWarnings annotation; false
* otherwise
*/
public boolean shouldSuppressWarnings(Tree tree, String errKey) {
Collection<String> prefixes = getSuppressWarningsPrefixes();
if (prefixes.isEmpty() || (prefixes.contains(SUPPRESS_ALL_PREFIX) && prefixes.size() == 1)) {
throw new BugInCF("Checker must provide a SuppressWarnings prefix." + " SourceChecker#getSuppressWarningsPrefixes was not overridden correctly.");
}
if (shouldSuppress(getSuppressWarningsStringsFromOption(), errKey)) {
return true;
}
if (shouldSuppress(getSuppressWarningsStringsFromOption(), errKey)) {
// the warning.
return true;
}
// trees.getPath might be slow, but this is only used in error reporting
@Nullable TreePath path = trees.getPath(this.currentRoot, tree);
@Nullable VariableTree var = TreePathUtil.enclosingVariable(path);
if (var != null && shouldSuppressWarnings(TreeUtils.elementFromTree(var), errKey)) {
return true;
}
@Nullable MethodTree method = TreePathUtil.enclosingMethod(path);
if (method != null) {
@Nullable Element elt = TreeUtils.elementFromTree(method);
if (shouldSuppressWarnings(elt, errKey)) {
return true;
}
if (isAnnotatedForThisCheckerOrUpstreamChecker(elt)) {
// because they may not have an @AnnotatedFor.
return false;
}
}
@Nullable ClassTree cls = TreePathUtil.enclosingClass(path);
if (cls != null) {
@Nullable Element elt = TreeUtils.elementFromTree(cls);
if (shouldSuppressWarnings(elt, errKey)) {
return true;
}
if (isAnnotatedForThisCheckerOrUpstreamChecker(elt)) {
// because they may not have an @AnnotatedFor.
return false;
}
}
if (useConservativeDefault("source")) {
// false, we DO suppress the warning.
return true;
}
return false;
}
Aggregations