Search in sources :

Example 1 with BaseTypeChecker

use of org.checkerframework.common.basetype.BaseTypeChecker in project checker-framework by typetools.

the class AnnotationFileElementTypes method parseStubFiles.

/**
 * Parses the stub files in the following order:
 *
 * <ol>
 *   <li>jdk.astub in the same directory as the checker, if it exists and ignorejdkastub option is
 *       not supplied
 *   <li>If parsing annotated JDK as stub files, all package-info.java files under the jdk/
 *       directory
 *   <li>Stub files listed in @StubFiles annotation on the checker; must be in same directory as
 *       the checker
 *   <li>Stub files returned by {@link BaseTypeChecker#getExtraStubFiles} (treated like those
 *       listed in @StubFiles annotation)
 *   <li>Stub files provided via {@code -Astubs} compiler option
 * </ol>
 *
 * <p>If a type is annotated with a qualifier from the same hierarchy in more than one stub file,
 * the qualifier in the last stub file is applied.
 *
 * <p>If using JDK 11, then the JDK stub files are only parsed if a type or declaration annotation
 * is requested from a class in that file.
 */
public void parseStubFiles() {
    parsing = true;
    BaseTypeChecker checker = factory.getChecker();
    ProcessingEnvironment processingEnv = factory.getProcessingEnv();
    // Only look in .jar files, and parse it right away.
    if (!checker.hasOption("ignorejdkastub")) {
        InputStream jdkStubIn = checker.getClass().getResourceAsStream("jdk.astub");
        if (jdkStubIn != null) {
            AnnotationFileParser.parseStubFile(checker.getClass().getResource("jdk.astub").toString(), jdkStubIn, factory, processingEnv, annotationFileAnnos, AnnotationFileType.BUILTIN_STUB);
        }
        String jdkVersionStub = "jdk" + annotatedJdkVersion + ".astub";
        InputStream jdkVersionStubIn = checker.getClass().getResourceAsStream(jdkVersionStub);
        if (jdkVersionStubIn != null) {
            AnnotationFileParser.parseStubFile(checker.getClass().getResource(jdkVersionStub).toString(), jdkVersionStubIn, factory, processingEnv, annotationFileAnnos, AnnotationFileType.BUILTIN_STUB);
        }
        // 2. Annotated JDK
        // This preps but does not parse the JDK files (except package-info.java files).
        // The JDK source code files will be parsed later, on demand.
        prepJdkStubs();
        // prepping the JDK parses all package-info.java files, which sets the `parsing` field to
        // false, so re-set it to true.
        parsing = true;
    }
    // 3. Stub files listed in @StubFiles annotation on the checker
    StubFiles stubFilesAnnotation = checker.getClass().getAnnotation(StubFiles.class);
    if (stubFilesAnnotation != null) {
        parseAnnotationFiles(Arrays.asList(stubFilesAnnotation.value()), AnnotationFileType.BUILTIN_STUB);
    }
    // 4. Stub files returned by the `getExtraStubFiles()` method
    parseAnnotationFiles(checker.getExtraStubFiles(), AnnotationFileType.BUILTIN_STUB);
    // 5. Stub files provided via -Astubs command-line option
    String stubsOption = checker.getOption("stubs");
    if (stubsOption != null) {
        parseAnnotationFiles(Arrays.asList(stubsOption.split(File.pathSeparator)), AnnotationFileType.COMMAND_LINE_STUB);
    }
    parsing = false;
}
Also used : StubFiles(org.checkerframework.framework.qual.StubFiles) BaseTypeChecker(org.checkerframework.common.basetype.BaseTypeChecker) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment)

Example 2 with BaseTypeChecker

use of org.checkerframework.common.basetype.BaseTypeChecker in project checker-framework by typetools.

the class GenericAnnotatedTypeFactory method addSharedCFGForTree.

/**
 * Add a new entry to the shared CFG. If this is a subchecker, this method delegates to the
 * superchecker's GenericAnnotatedTypeFactory, if it exists. Duplicate keys must map to the same
 * CFG.
 *
 * <p>Calls to this method should be guarded by checking {@link #hasOrIsSubchecker}; it is
 * nonsensical to have a shared CFG when a checker is running alone.
 *
 * @param tree the source code corresponding to cfg
 * @param cfg the control flow graph to use for tree
 * @return whether a shared CFG was found to actually add to (duplicate keys also return true)
 */
public boolean addSharedCFGForTree(Tree tree, ControlFlowGraph cfg) {
    if (!shouldCache) {
        return false;
    }
    BaseTypeChecker parentChecker = this.checker.getUltimateParentChecker();
    // Checking reference equality.
    @SuppressWarnings("interning") boolean parentIsThisChecker = parentChecker == this.checker;
    if (parentIsThisChecker) {
        // This is the ultimate parent.
        if (this.subcheckerSharedCFG == null) {
            this.subcheckerSharedCFG = new HashMap<>(getCacheSize());
        }
        if (!this.subcheckerSharedCFG.containsKey(tree)) {
            this.subcheckerSharedCFG.put(tree, cfg);
        } else {
            assert this.subcheckerSharedCFG.get(tree).equals(cfg);
        }
        return true;
    }
    // This is a subchecker.
    if (parentChecker != null) {
        GenericAnnotatedTypeFactory<?, ?, ?, ?> parentAtf = parentChecker.getTypeFactory();
        return parentAtf.addSharedCFGForTree(tree, cfg);
    } else {
        return false;
    }
}
Also used : BaseTypeChecker(org.checkerframework.common.basetype.BaseTypeChecker)

Example 3 with BaseTypeChecker

use of org.checkerframework.common.basetype.BaseTypeChecker in project checker-framework by typetools.

the class GenericAnnotatedTypeFactory method getSharedCFGForTree.

/**
 * Get the shared control flow graph used for {@code tree} by this checker's topmost superchecker.
 * Returns null if no information is available about the given tree, or if this checker has a
 * parent checker that does not have a GenericAnnotatedTypeFactory.
 *
 * <p>Calls to this method should be guarded by checking {@link #hasOrIsSubchecker}; it is
 * nonsensical to have a shared CFG when a checker is running alone.
 *
 * @param tree the tree whose CFG should be looked up
 * @return the CFG stored by this checker's uppermost superchecker for tree, or null if it is not
 *     available
 */
@Nullable
public ControlFlowGraph getSharedCFGForTree(Tree tree) {
    if (!shouldCache) {
        return null;
    }
    BaseTypeChecker parentChecker = this.checker.getUltimateParentChecker();
    // Checking reference equality.
    @SuppressWarnings("interning") boolean parentIsThisChecker = parentChecker == this.checker;
    if (parentIsThisChecker) {
        // This is the ultimate parent;
        return this.subcheckerSharedCFG == null ? null : this.subcheckerSharedCFG.getOrDefault(tree, null);
    }
    // This is a subchecker.
    if (parentChecker != null) {
        GenericAnnotatedTypeFactory<?, ?, ?, ?> parentAtf = parentChecker.getTypeFactory();
        return parentAtf.getSharedCFGForTree(tree);
    } else {
        return null;
    }
}
Also used : BaseTypeChecker(org.checkerframework.common.basetype.BaseTypeChecker) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 4 with BaseTypeChecker

use of org.checkerframework.common.basetype.BaseTypeChecker in project checker-framework by typetools.

the class CFCFGBuilder method assumeAssertionsActivatedForAssertTree.

/**
 * Given a SourceChecker and an AssertTree, returns whether the AssertTree uses
 * an @AssumeAssertion string that is relevant to the SourceChecker.
 *
 * @param checker the checker
 * @param tree an assert tree
 * @return true if the assert tree contains an @AssumeAssertion(checker) message string for any
 *     subchecker of the given checker's ultimate parent checker
 */
public static boolean assumeAssertionsActivatedForAssertTree(BaseTypeChecker checker, AssertTree tree) {
    ExpressionTree detail = tree.getDetail();
    if (detail != null) {
        String msg = detail.toString();
        BaseTypeChecker ultimateParent = checker.getUltimateParentChecker();
        Collection<String> prefixes = ultimateParent.getSuppressWarningsPrefixesOfSubcheckers();
        for (String prefix : prefixes) {
            String assumeAssert = "@AssumeAssertion(" + prefix + ")";
            if (msg.contains(assumeAssert)) {
                return true;
            }
        }
    }
    return false;
}
Also used : BaseTypeChecker(org.checkerframework.common.basetype.BaseTypeChecker) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 5 with BaseTypeChecker

use of org.checkerframework.common.basetype.BaseTypeChecker in project checker-framework by typetools.

the class InitializedFieldsAnnotatedTypeFactory method createTypeFactoryForProcessor.

/**
 * Creates a new type factory for the given annotation processor, if it is a type-checker. This
 * does NOT return an existing type factory.
 *
 * @param processorName the fully-qualified class name of an annotation processor
 * @return the type factory for the given annotation processor, or null if it's not a checker
 */
GenericAnnotatedTypeFactory<?, ?, ?, ?> createTypeFactoryForProcessor(@BinaryName String processorName) {
    try {
        Class<?> checkerClass = Class.forName(processorName);
        if (!BaseTypeChecker.class.isAssignableFrom(checkerClass)) {
            return null;
        }
        @SuppressWarnings("unchecked") BaseTypeChecker c = ((Class<? extends BaseTypeChecker>) checkerClass).getDeclaredConstructor().newInstance();
        c.init(processingEnv);
        c.initChecker();
        BaseTypeVisitor<?> v = c.createSourceVisitorPublic();
        GenericAnnotatedTypeFactory<?, ?, ?, ?> atf = v.createTypeFactoryPublic();
        if (atf == null) {
            throw new UserError("Cannot find %s; check the classpath or processorpath", processorName);
        }
        return atf;
    } catch (ClassNotFoundException | InstantiationException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
        throw new UserError("Problem instantiating " + processorName, e);
    }
}
Also used : UserError(org.checkerframework.javacutil.UserError) BaseTypeChecker(org.checkerframework.common.basetype.BaseTypeChecker) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

BaseTypeChecker (org.checkerframework.common.basetype.BaseTypeChecker)6 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 ExpressionTree (com.sun.source.tree.ExpressionTree)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ProcessingEnvironment (javax.annotation.processing.ProcessingEnvironment)1 UnderlyingAST (org.checkerframework.dataflow.cfg.UnderlyingAST)1 StubFiles (org.checkerframework.framework.qual.StubFiles)1 UserError (org.checkerframework.javacutil.UserError)1