|
| 1 | +// Copyright 2024 The JSpecify Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.jspecify.nullness; |
| 16 | + |
| 17 | +import com.sun.source.tree.AssignmentTree; |
| 18 | +import com.sun.source.tree.ClassTree; |
| 19 | +import com.sun.source.tree.ExpressionTree; |
| 20 | +import com.sun.source.tree.MethodInvocationTree; |
| 21 | +import com.sun.source.tree.Tree; |
| 22 | +import java.util.List; |
| 23 | +import javax.lang.model.element.ExecutableElement; |
| 24 | +import org.checkerframework.framework.type.AnnotatedTypeFactory; |
| 25 | +import org.checkerframework.framework.type.AnnotatedTypeFormatter; |
| 26 | +import org.checkerframework.framework.type.AnnotatedTypeMirror; |
| 27 | +import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType; |
| 28 | +import org.checkerframework.framework.util.visualize.AbstractTypeInformationPresenter; |
| 29 | +import org.checkerframework.framework.util.visualize.TypeOccurrenceKind; |
| 30 | +import org.checkerframework.javacutil.TreeUtils; |
| 31 | + |
| 32 | +/** |
| 33 | + * Output "sinkType" and "sourceType" diagnostic warning messages so the conformance tests can look |
| 34 | + * for (a subset of) them. |
| 35 | + */ |
| 36 | +public final class ConformanceTypeInformationPresenter extends AbstractTypeInformationPresenter { |
| 37 | + |
| 38 | + /** |
| 39 | + * Constructs a presenter for the given factory. |
| 40 | + * |
| 41 | + * @param atypeFactory the AnnotatedTypeFactory for the current analysis |
| 42 | + */ |
| 43 | + public ConformanceTypeInformationPresenter(AnnotatedTypeFactory atypeFactory) { |
| 44 | + super(atypeFactory); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + protected AnnotatedTypeFormatter createTypeFormatter() { |
| 49 | + // Use the same type formatter as normal error messages. Look into whether a different format |
| 50 | + // would be better here. |
| 51 | + return atypeFactory.getAnnotatedTypeFormatter(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected TypeInformationReporter createTypeInformationReporter(ClassTree tree) { |
| 56 | + return new ConformanceTypeInformationReporter(tree); |
| 57 | + } |
| 58 | + |
| 59 | + class ConformanceTypeInformationReporter extends TypeInformationReporter { |
| 60 | + ConformanceTypeInformationReporter(ClassTree tree) { |
| 61 | + super(tree); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void reportTreeType( |
| 66 | + Tree tree, AnnotatedTypeMirror type, TypeOccurrenceKind occurrenceKind) { |
| 67 | + switch (tree.getKind()) { |
| 68 | + case ASSIGNMENT: |
| 69 | + AssignmentTree asgn = (AssignmentTree) tree; |
| 70 | + AnnotatedTypeMirror varType = |
| 71 | + genFactory != null |
| 72 | + ? genFactory.getAnnotatedTypeLhs(asgn.getVariable()) |
| 73 | + : atypeFactory.getAnnotatedType(asgn.getVariable()); |
| 74 | + checker.reportWarning( |
| 75 | + asgn.getVariable(), |
| 76 | + "sinkType", |
| 77 | + typeFormatter.format(varType), |
| 78 | + asgn.getVariable().toString()); |
| 79 | + break; |
| 80 | + case RETURN: |
| 81 | + checker.reportWarning(tree, "sinkType", typeFormatter.format(type), "return"); |
| 82 | + break; |
| 83 | + case METHOD_INVOCATION: |
| 84 | + ExecutableElement calledElem = TreeUtils.elementFromUse((MethodInvocationTree) tree); |
| 85 | + String methodName = calledElem.getSimpleName().toString(); |
| 86 | + AnnotatedExecutableType calledType = (AnnotatedExecutableType) type; |
| 87 | + List<? extends AnnotatedTypeMirror> params = calledType.getParameterTypes(); |
| 88 | + MethodInvocationTree mit = (MethodInvocationTree) tree; |
| 89 | + List<? extends ExpressionTree> args = mit.getArguments(); |
| 90 | + assert params.size() == args.size(); |
| 91 | + |
| 92 | + for (int i = 0; i < params.size(); ++i) { |
| 93 | + String paramName = calledElem.getParameters().get(i).getSimpleName().toString(); |
| 94 | + String paramLocation = String.format("%s#%s", methodName, paramName); |
| 95 | + checker.reportWarning( |
| 96 | + tree, "sinkType", typeFormatter.format(params.get(i)), paramLocation); |
| 97 | + } |
| 98 | + break; |
| 99 | + default: |
| 100 | + // Nothing special for other trees. |
| 101 | + } |
| 102 | + |
| 103 | + if (TreeUtils.isExpressionTree(tree)) { |
| 104 | + checker.reportWarning(tree, "sourceType", typeFormatter.format(type), tree.toString()); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments