Protect scripts compilation from passing -Xuse-ir via configuration

as well as other options that require changes in the compilation setup
before compiler options from the configuration could be processed
This commit is contained in:
Ilya Chernikov
2021-01-07 18:58:23 +01:00
parent bac6a7346e
commit 43d7536a28
2 changed files with 37 additions and 24 deletions
@@ -170,19 +170,12 @@ private fun CompilerConfiguration.updateWithCompilerOptions(
false false
} ?: run { } ?: run {
messageCollector.reportArgumentParseProblems(it) messageCollector.reportArgumentParseProblems(it)
reportArgumentsIgnoredGenerally( val error = reportArgumentsNotAllowed(it, messageCollector, ignoredOptionsReportingState)
it, reportArgumentsIgnoredGenerally(it, messageCollector, ignoredOptionsReportingState)
messageCollector,
ignoredOptionsReportingState
)
if (isRefinement) { if (isRefinement) {
reportArgumentsIgnoredFromRefinement( reportArgumentsIgnoredFromRefinement(it, messageCollector, ignoredOptionsReportingState)
it,
messageCollector,
ignoredOptionsReportingState
)
} }
true !error
} }
} }
} }
@@ -92,15 +92,32 @@ class IgnoredOptionsReportingState {
var currentArguments = K2JVMCompilerArguments() var currentArguments = K2JVMCompilerArguments()
} }
internal fun reportArgumentsNotAllowed(
arguments: K2JVMCompilerArguments,
messageCollector: MessageCollector,
reportingState: IgnoredOptionsReportingState
) =
reportInvalidArguments(
arguments,
"The following compiler arguments are not allowed in the script compilation configuration: ",
CompilerMessageSeverity.ERROR,
messageCollector,
reportingState,
K2JVMCompilerArguments::useJavac,
K2JVMCompilerArguments::useIR,
K2JVMCompilerArguments::useOldBackend,
K2JVMCompilerArguments::useFir
)
internal fun reportArgumentsIgnoredGenerally( internal fun reportArgumentsIgnoredGenerally(
arguments: K2JVMCompilerArguments, arguments: K2JVMCompilerArguments,
messageCollector: MessageCollector, messageCollector: MessageCollector,
reportingState: IgnoredOptionsReportingState reportingState: IgnoredOptionsReportingState
) { ) =
reportInvalidArguments(
reportIgnoredArguments(
arguments, arguments,
"The following compiler arguments are ignored on script compilation: ", "The following compiler arguments are ignored on script compilation: ",
CompilerMessageSeverity.STRONG_WARNING,
messageCollector, messageCollector,
reportingState, reportingState,
K2JVMCompilerArguments::version, K2JVMCompilerArguments::version,
@@ -121,14 +138,14 @@ internal fun reportArgumentsIgnoredGenerally(
K2JVMCompilerArguments::reportPerf, K2JVMCompilerArguments::reportPerf,
K2JVMCompilerArguments::dumpPerf K2JVMCompilerArguments::dumpPerf
) )
}
internal fun reportArgumentsIgnoredFromRefinement( internal fun reportArgumentsIgnoredFromRefinement(
arguments: K2JVMCompilerArguments, messageCollector: MessageCollector, reportingState: IgnoredOptionsReportingState arguments: K2JVMCompilerArguments, messageCollector: MessageCollector, reportingState: IgnoredOptionsReportingState
) { ) =
reportIgnoredArguments( reportInvalidArguments(
arguments, arguments,
"The following compiler arguments are ignored when configured from refinement callbacks: ", "The following compiler arguments are ignored when configured from refinement callbacks: ",
CompilerMessageSeverity.STRONG_WARNING,
messageCollector, messageCollector,
reportingState, reportingState,
K2JVMCompilerArguments::noJdk, K2JVMCompilerArguments::noJdk,
@@ -138,23 +155,26 @@ internal fun reportArgumentsIgnoredFromRefinement(
K2JVMCompilerArguments::noStdlib, K2JVMCompilerArguments::noStdlib,
K2JVMCompilerArguments::noReflect K2JVMCompilerArguments::noReflect
) )
}
private fun reportIgnoredArguments(
arguments: K2JVMCompilerArguments, message: String, private fun reportInvalidArguments(
arguments: K2JVMCompilerArguments,
message: String, severity: CompilerMessageSeverity,
messageCollector: MessageCollector, reportingState: IgnoredOptionsReportingState, messageCollector: MessageCollector, reportingState: IgnoredOptionsReportingState,
vararg toIgnore: KMutableProperty1<K2JVMCompilerArguments, *> vararg toIgnore: KMutableProperty1<K2JVMCompilerArguments, *>
) { ): Boolean {
val ignoredArgKeys = toIgnore.mapNotNull { argProperty -> val invalidArgKeys = toIgnore.mapNotNull { argProperty ->
if (argProperty.get(arguments) != argProperty.get(reportingState.currentArguments)) { if (argProperty.get(arguments) != argProperty.get(reportingState.currentArguments)) {
argProperty.annotations.firstIsInstanceOrNull<Argument>()?.value argProperty.annotations.firstIsInstanceOrNull<Argument>()?.value
?: throw IllegalStateException("unknown compiler argument property: $argProperty: no Argument annotation found") ?: throw IllegalStateException("unknown compiler argument property: $argProperty: no Argument annotation found")
} else null } else null
} }
if (ignoredArgKeys.isNotEmpty()) { if (invalidArgKeys.isNotEmpty()) {
messageCollector.report(CompilerMessageSeverity.STRONG_WARNING, "$message${ignoredArgKeys.joinToString(", ")}") messageCollector.report(severity, "$message${invalidArgKeys.joinToString(", ")}")
return true
} }
return false
} }
val MessageCollector.reporter: MessageReporter val MessageCollector.reporter: MessageReporter