From c512db8e1ccb2fd4cd5bda3067fd6b779412d48d Mon Sep 17 00:00:00 2001 From: e5l Date: Mon, 11 Sep 2017 13:31:02 +0600 Subject: [PATCH] Validate -Xjsr305 value in CLI --- .../arguments/K2JVMCompilerArguments.kt | 41 +++++++++++++++---- .../kotlin/cli/common/CLICompiler.java | 7 ++-- compiler/testData/cli/jvm/wrongXjsr305.args | 12 ++++++ compiler/testData/cli/jvm/wrongXjsr305.out | 8 ++++ .../kotlin/cli/CliTestGenerated.java | 6 +++ 5 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 compiler/testData/cli/jvm/wrongXjsr305.args create mode 100644 compiler/testData/cli/jvm/wrongXjsr305.out diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt index 2eedb6dc2d5..4a291c984ce 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.kt @@ -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? { - 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 } } diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java b/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java index 1d2f6786a7d..f6afab2fbc8 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java @@ -175,8 +175,9 @@ public abstract class CLICompiler 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 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 extends CLI CommonConfigurationKeysKt.setLanguageVersionSettings(configuration, new LanguageVersionSettingsImpl( languageVersion, ApiVersion.createByLanguageVersion(apiVersion), - arguments.configureAnalysisFlags(configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)), + arguments.configureAnalysisFlags(collector), extraLanguageFeatures )); } diff --git a/compiler/testData/cli/jvm/wrongXjsr305.args b/compiler/testData/cli/jvm/wrongXjsr305.args new file mode 100644 index 00000000000..1ba791849e4 --- /dev/null +++ b/compiler/testData/cli/jvm/wrongXjsr305.args @@ -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 diff --git a/compiler/testData/cli/jvm/wrongXjsr305.out b/compiler/testData/cli/jvm/wrongXjsr305.out new file mode 100644 index 00000000000..78ec5fb7b0f --- /dev/null +++ b/compiler/testData/cli/jvm/wrongXjsr305.out @@ -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 diff --git a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java index 07a2786ecd3..baaaa972215 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -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")