Validate -Xjsr305 value in CLI
This commit is contained in:
+34
-7
@@ -198,7 +198,7 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
|
||||
|
||||
fun parseJsr305UnderMigration(collector: MessageCollector, item: String): ReportLevel? {
|
||||
val rawState = item.split(":").takeIf { it.size == 2 }?.get(1)
|
||||
return ReportLevel.findByDescription(rawState)
|
||||
return ReportLevel.findByDescription(rawState) ?: reportUnrecognizedJsr305(collector, item).let { null }
|
||||
}
|
||||
|
||||
jsr305?.forEach { item ->
|
||||
@@ -206,11 +206,18 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
|
||||
item.startsWith("@") -> {
|
||||
val (name, state) = parseJsr305UserDefined(collector, item) ?: return@forEach
|
||||
val current = userDefined[name]
|
||||
current?.let { return@forEach }
|
||||
if (current != null) {
|
||||
reportDuplicateJsr305(collector, "@$name:${current.description}", item)
|
||||
return@forEach
|
||||
}
|
||||
userDefined[name] = state
|
||||
}
|
||||
item.startsWith("under-migration") -> {
|
||||
migration?.let { return@forEach }
|
||||
if (migration != null) {
|
||||
reportDuplicateJsr305(collector, "under-migration:${migration?.description}", item)
|
||||
return@forEach
|
||||
}
|
||||
|
||||
migration = parseJsr305UnderMigration(collector, item)
|
||||
}
|
||||
item == "enable" -> {
|
||||
@@ -218,11 +225,15 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
|
||||
CompilerMessageSeverity.STRONG_WARNING,
|
||||
"Option 'enable' for -Xjsr305 flag is deprecated. Please use 'strict' instead"
|
||||
)
|
||||
global?.let { return@forEach }
|
||||
if (global != null) return@forEach
|
||||
|
||||
global = ReportLevel.STRICT
|
||||
}
|
||||
else -> {
|
||||
global?.let { return@forEach }
|
||||
if (global != null) {
|
||||
reportDuplicateJsr305(collector, global!!.description, item)
|
||||
return@forEach
|
||||
}
|
||||
global = ReportLevel.findByDescription(item)
|
||||
}
|
||||
}
|
||||
@@ -232,9 +243,25 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
|
||||
return if (state == Jsr305State.DISABLED) Jsr305State.DISABLED else state
|
||||
}
|
||||
|
||||
private fun reportUnrecognizedJsr305(collector: MessageCollector, item: String) {
|
||||
collector.report(CompilerMessageSeverity.ERROR, "Unrecognized -Xjsr305 value: $item")
|
||||
}
|
||||
|
||||
private fun reportDuplicateJsr305(collector: MessageCollector, first: String, second: String) {
|
||||
collector.report(CompilerMessageSeverity.ERROR, "Conflict duplicating -Xjsr305 value: $first, $second")
|
||||
}
|
||||
|
||||
private fun parseJsr305UserDefined(collector: MessageCollector, item: String): Pair<String, ReportLevel>? {
|
||||
val (name, rawState) = item.substring(1).split(":").takeIf { it.size == 2 } ?: return null
|
||||
val state = ReportLevel.findByDescription(rawState) ?: return null
|
||||
val (name, rawState) = item.substring(1).split(":").takeIf { it.size == 2 } ?: run {
|
||||
reportUnrecognizedJsr305(collector, item)
|
||||
return null
|
||||
}
|
||||
|
||||
val state = ReportLevel.findByDescription(rawState) ?: run {
|
||||
reportUnrecognizedJsr305(collector, item)
|
||||
return null
|
||||
}
|
||||
|
||||
return name to state
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,8 +175,9 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> extends CLI
|
||||
configuration.put(CLIConfigurationKeys.IS_API_VERSION_EXPLICIT, true);
|
||||
}
|
||||
|
||||
MessageCollector collector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY);
|
||||
if (apiVersion.compareTo(languageVersion) > 0) {
|
||||
configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY).report(
|
||||
collector.report(
|
||||
ERROR,
|
||||
"-api-version (" + apiVersion.getVersionString() + ") cannot be greater than " +
|
||||
"-language-version (" + languageVersion.getVersionString() + ")",
|
||||
@@ -185,7 +186,7 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> extends CLI
|
||||
}
|
||||
|
||||
if (!languageVersion.isStable()) {
|
||||
configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY).report(
|
||||
collector.report(
|
||||
STRONG_WARNING,
|
||||
"Language version " + languageVersion.getVersionString() + " is experimental, there are " +
|
||||
"no backwards compatibility guarantees for new language and library features",
|
||||
@@ -206,7 +207,7 @@ public abstract class CLICompiler<A extends CommonCompilerArguments> extends CLI
|
||||
CommonConfigurationKeysKt.setLanguageVersionSettings(configuration, new LanguageVersionSettingsImpl(
|
||||
languageVersion,
|
||||
ApiVersion.createByLanguageVersion(apiVersion),
|
||||
arguments.configureAnalysisFlags(configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)),
|
||||
arguments.configureAnalysisFlags(collector),
|
||||
extraLanguageFeatures
|
||||
));
|
||||
}
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
$TESTDATA_DIR$/simple.kt
|
||||
-d
|
||||
$TEMP_DIR$
|
||||
-Xjsr305=enable
|
||||
-Xjsr305=under-migration\:stct
|
||||
-Xjsr305=@hello\:warning
|
||||
-Xjsr305=@hello\:warn
|
||||
-Xjsr305=@hello\:ignore
|
||||
-Xjsr305=strict
|
||||
-Xjsr305=ignore
|
||||
-Xjsr305=under-migration\:ignore
|
||||
-Xjsr305=under-migration\:strict
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
warning: option 'enable' for -Xjsr305 flag is deprecated. Please use 'strict' instead
|
||||
error: unrecognized -Xjsr305 value: under-migration:stct
|
||||
error: unrecognized -Xjsr305 value: @hello:warning
|
||||
error: conflict duplicating -Xjsr305 value: @hello:warn, @hello:ignore
|
||||
error: conflict duplicating -Xjsr305 value: strict, strict
|
||||
error: conflict duplicating -Xjsr305 value: strict, ignore
|
||||
error: conflict duplicating -Xjsr305 value: under-migration:ignore, under-migration:strict
|
||||
COMPILATION_ERROR
|
||||
@@ -475,6 +475,12 @@ public class CliTestGenerated extends AbstractCliTest {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/wrongScriptWithNoSource.args");
|
||||
doJvmTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("wrongXjsr305.args")
|
||||
public void testWrongXjsr305() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/wrongXjsr305.args");
|
||||
doJvmTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/cli/js")
|
||||
|
||||
Reference in New Issue
Block a user