[JS IR] Add CLI argument to setup error tolerance policy

-  Make sure dev mode is enabled if code with errors is allowed
This commit is contained in:
Roman Artemev
2020-07-08 14:16:52 +03:00
parent 8a810e2a5e
commit a60782f3df
8 changed files with 67 additions and 19 deletions
@@ -175,6 +175,9 @@ class K2JSCompilerArguments : CommonCompilerArguments() {
@Argument(value = "-Xdisable-fake-override-validator", description = "Disable IR fake override validator")
var disableFakeOverrideValidator: Boolean by FreezableVar(false)
@Argument(value = "-Xerror-tolerance-policy", description = "Set up error tolerance policy (NONE, SEMANTIC, SYNTAX, ALL)")
var errorTolerancePolicy: String? by NullableStringFreezableVar(null)
override fun checkIrSupport(languageVersionSettings: LanguageVersionSettings, collector: MessageCollector) {
if (!isIrBackendEnabled()) return
@@ -35,10 +35,7 @@ import org.jetbrains.kotlin.incremental.js.IncrementalNextRoundChecker
import org.jetbrains.kotlin.incremental.js.IncrementalResultsConsumer
import org.jetbrains.kotlin.ir.backend.js.*
import org.jetbrains.kotlin.ir.declarations.persistent.PersistentIrFactory
import org.jetbrains.kotlin.js.config.EcmaVersion
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.js.config.JsConfig
import org.jetbrains.kotlin.js.config.SourceMapSourceEmbedding
import org.jetbrains.kotlin.js.config.*
import org.jetbrains.kotlin.metadata.deserialization.BinaryVersion
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.serialization.js.ModuleKind
@@ -314,6 +311,13 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
configuration.putIfNotNull(CommonConfigurationKeys.LOOKUP_TRACKER, services[LookupTracker::class.java])
configuration.putIfNotNull(CommonConfigurationKeys.EXPECT_ACTUAL_TRACKER, services[ExpectActualTracker::class.java])
val errorTolerancePolicy = arguments.errorTolerancePolicy?.let { ErrorTolerancePolicy.resolvePolicy(it) }
configuration.putIfNotNull(JSConfigurationKeys.ERROR_TOLERANCE_POLICY, errorTolerancePolicy)
if (errorTolerancePolicy?.allowErrors == true) {
configuration.put(JSConfigurationKeys.DEVELOPER_MODE, true)
}
val sourceMapEmbedContentString = arguments.sourceMapEmbedSources
var sourceMapContentEmbedding: SourceMapSourceEmbedding? = if (sourceMapEmbedContentString != null)
sourceMapContentEmbeddingMap[sourceMapEmbedContentString]
@@ -42,6 +42,7 @@ import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.js.analyzer.JsAnalysisResult
import org.jetbrains.kotlin.js.config.ErrorTolerancePolicy
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
import org.jetbrains.kotlin.konan.properties.propertyList
import org.jetbrains.kotlin.konan.util.KlibMetadataFactories
@@ -106,6 +107,7 @@ fun generateKLib(
nopack: Boolean
) {
val incrementalDataProvider = configuration.get(JSConfigurationKeys.INCREMENTAL_DATA_PROVIDER)
val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT
val icData: List<KotlinFileSerializedData>
val serializedIrFiles: List<SerializedIrFile>?
@@ -140,7 +142,7 @@ fun generateKLib(
val depsDescriptors =
ModulesStructure(project, MainModule.SourceFiles(files), analyzer, configuration, allDependencies, friendDependencies)
val psi2IrContext = runAnalysisAndPreparePsi2Ir(depsDescriptors, irFactory)
val psi2IrContext = runAnalysisAndPreparePsi2Ir(depsDescriptors, irFactory, errorPolicy)
val irBuiltIns = psi2IrContext.irBuiltIns
val functionFactory = IrFunctionFactory(irBuiltIns, psi2IrContext.symbolTable)
irBuiltIns.functionFactory = functionFactory
@@ -222,10 +224,11 @@ fun loadIr(
): IrModuleInfo {
val depsDescriptors = ModulesStructure(project, mainModule, analyzer, configuration, allDependencies, friendDependencies)
val deserializeFakeOverrides = configuration.getBoolean(CommonConfigurationKeys.DESERIALIZE_FAKE_OVERRIDES)
val errorPolicy = configuration.get(JSConfigurationKeys.ERROR_TOLERANCE_POLICY) ?: ErrorTolerancePolicy.DEFAULT
when (mainModule) {
is MainModule.SourceFiles -> {
val psi2IrContext: GeneratorContext = runAnalysisAndPreparePsi2Ir(depsDescriptors, irFactory)
val psi2IrContext: GeneratorContext = runAnalysisAndPreparePsi2Ir(depsDescriptors, irFactory, errorPolicy)
val irBuiltIns = psi2IrContext.irBuiltIns
val symbolTable = psi2IrContext.symbolTable
val functionFactory = IrFunctionFactory(irBuiltIns, symbolTable)
@@ -294,9 +297,9 @@ fun loadIr(
}
}
private fun runAnalysisAndPreparePsi2Ir(depsDescriptors: ModulesStructure, irFactory: IrFactory): GeneratorContext {
val (bindingContext, moduleDescriptor) = depsDescriptors.runAnalysis()
val psi2Ir = Psi2IrTranslator(depsDescriptors.compilerConfiguration.languageVersionSettings, Psi2IrConfiguration())
private fun runAnalysisAndPreparePsi2Ir(depsDescriptors: ModulesStructure, irFactory: IrFactory, errorIgnorancePolicy: ErrorTolerancePolicy): GeneratorContext {
val (bindingContext, moduleDescriptor) = depsDescriptors.runAnalysis(errorIgnorancePolicy)
val psi2Ir = Psi2IrTranslator(depsDescriptors.compilerConfiguration.languageVersionSettings, Psi2IrConfiguration(errorIgnorancePolicy.allowErrors))
val symbolTable = SymbolTable(IdSignatureDescriptor(JsManglerDesc), irFactory)
return psi2Ir.createGeneratorContext(moduleDescriptor, bindingContext, symbolTable)
}
@@ -380,7 +383,7 @@ private class ModulesStructure(
val builtInsDep = allDependencies.getFullList().find { it.isBuiltIns }
fun runAnalysis(): JsAnalysisResult {
fun runAnalysis(errorPolicy: ErrorTolerancePolicy): JsAnalysisResult {
require(mainModule is MainModule.SourceFiles)
val files = mainModule.files
@@ -403,10 +406,11 @@ private class ModulesStructure(
/** can throw [IncrementalNextRoundException] */
compareMetadataAndGoToNextICRoundIfNeeded(analysisResult, compilerConfiguration, files)
}
if (analyzer.hasErrors() || analysisResult !is JsAnalysisResult)
if (!errorPolicy.allowErrors && analyzer.hasErrors() || analysisResult !is JsAnalysisResult)
throw JsIrCompilationError
TopDownAnalyzerFacadeForJSIR.checkForErrors(files, analysisResult.bindingContext)
TopDownAnalyzerFacadeForJSIR.checkForErrors(files, analysisResult.bindingContext, errorPolicy)
return analysisResult
}