All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.resolve.diagnostics.PrecomputedSuppressCache.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.resolve.diagnostics

import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.psi.KtAnnotated
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtTreeVisitorVoid
import org.jetbrains.kotlin.resolve.BindingContext

/**
 * A [KotlinSuppressCache] implementation that computes all suppressions at the moment of instantiation.
 * This is useful in the IR backend, where we clear the main binding context after psi2ir to avoid taking extra memory.
 * To make suppression of errors reported from backend possible though, we need to precompute all resolved `@Suppress` annotations,
 * and store this information outside of the binding context, which is going to be cleared.
 *
 * @param context the binding context where the data should be loaded from. Note that it shouldn't be stored as a property because the
 *   primary use case of this class is when that binding context is cleared and thus is useless after a certain point.
 */
class PrecomputedSuppressCache(context: BindingContext, files: List) : KotlinSuppressCache() {
    val storage: Map> =
        mutableMapOf>().also { storage ->
            val visitor = PrecomputingVisitor(storage, BindingContextSuppressCache(context))
            for (file in files) {
                file.accept(visitor, null)
            }
        }

    private class PrecomputingVisitor(
        val storage: MutableMap>,
        val computer: KotlinSuppressCache,
    ) : KtTreeVisitorVoid() {
        override fun visitKtElement(element: KtElement) {
            super.visitKtElement(element)
            if (element is KtAnnotated) {
                computeAnnotations(element)
            }
        }

        override fun visitKtFile(file: KtFile) {
            super.visitKtFile(file)
            computeAnnotations(file)
        }

        private fun computeAnnotations(element: KtAnnotated) {
            val suppressions = computer.getSuppressionAnnotations(element).filter { it.fqName == StandardNames.FqNames.suppress }
            if (suppressions.isNotEmpty()) {
                storage[element] = suppressions
            }
        }
    }

    override fun getSuppressionAnnotations(annotated: KtAnnotated): List =
        storage[annotated].orEmpty()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy