From 9fef0c86504be154b013991da3e123072ccf6ccd Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Sun, 22 Sep 2013 18:49:29 +0400 Subject: [PATCH] Support '-suppress warnings' #KT-3319 In Progress --- .../jet/cli/CommonCompilerArguments.java | 7 ++ .../jetbrains/jet/cli/common/CLICompiler.java | 13 ++-- .../jet/cli/common/CompilerArguments.java | 2 + compiler/testData/cli/help.out | 5 +- .../testData/cli/suppressAllWarningsJS.out | 1 + .../cli/suppressAllWarningsLowercase.out | 1 + .../cli/suppressAllWarningsMixedCase.out | 1 + compiler/testData/cli/warnings.kt | 10 +++ compiler/testData/cli/wrongArgument.out | 5 +- .../org/jetbrains/jet/cli/jvm/CliTest.java | 66 ++++++++++++++----- 10 files changed, 86 insertions(+), 25 deletions(-) create mode 100644 compiler/testData/cli/suppressAllWarningsJS.out create mode 100644 compiler/testData/cli/suppressAllWarningsLowercase.out create mode 100644 compiler/testData/cli/suppressAllWarningsMixedCase.out create mode 100644 compiler/testData/cli/warnings.kt diff --git a/compiler/cli/src/org/jetbrains/jet/cli/CommonCompilerArguments.java b/compiler/cli/src/org/jetbrains/jet/cli/CommonCompilerArguments.java index f0d878ffdc2..6446928551d 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/CommonCompilerArguments.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/CommonCompilerArguments.java @@ -12,6 +12,8 @@ public abstract class CommonCompilerArguments extends CompilerArguments { public boolean version; @Argument(value = "help", alias = "h", description = "Show help") public boolean help; + @Argument(value = "suppress", description = "Suppress compiler messages by severity (warnings)") + public String suppress; @Override public boolean isHelp() { @@ -40,4 +42,9 @@ public abstract class CommonCompilerArguments extends CompilerArguments { public void setTags(boolean tags) { this.tags = tags; } + + @Override + public boolean suppressAllWarnings() { + return "warnings".equalsIgnoreCase(suppress); + } } diff --git a/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java b/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java index 04911146c27..81e6c3c85a8 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/common/CLICompiler.java @@ -16,6 +16,7 @@ package org.jetbrains.jet.cli.common; +import com.google.common.base.Predicates; import com.intellij.openapi.Disposable; import com.intellij.openapi.util.Disposer; import com.sampullara.cli.Args; @@ -27,9 +28,7 @@ import org.jetbrains.jet.config.CompilerConfiguration; import java.io.PrintStream; -import static org.jetbrains.jet.cli.common.ExitCode.COMPILATION_ERROR; -import static org.jetbrains.jet.cli.common.ExitCode.INTERNAL_ERROR; -import static org.jetbrains.jet.cli.common.ExitCode.OK; +import static org.jetbrains.jet.cli.common.ExitCode.*; public abstract class CLICompiler { @@ -108,10 +107,14 @@ public abstract class CLICompiler { printVersionIfNeeded(errStream, arguments, messageRenderer); - PrintingMessageCollector printingCollector = new PrintingMessageCollector(errStream, messageRenderer, arguments.isVerbose()); + MessageCollector collector = new PrintingMessageCollector(errStream, messageRenderer, arguments.isVerbose()); + + if (arguments.suppressAllWarnings()) { + collector = new FilteringMessageCollector(collector, Predicates.equalTo(CompilerMessageSeverity.WARNING)); + } try { - return exec(printingCollector, arguments); + return exec(collector, arguments); } finally { errStream.print(messageRenderer.renderConclusion()); diff --git a/compiler/cli/src/org/jetbrains/jet/cli/common/CompilerArguments.java b/compiler/cli/src/org/jetbrains/jet/cli/common/CompilerArguments.java index be652080918..2319025bffc 100644 --- a/compiler/cli/src/org/jetbrains/jet/cli/common/CompilerArguments.java +++ b/compiler/cli/src/org/jetbrains/jet/cli/common/CompilerArguments.java @@ -45,4 +45,6 @@ public abstract class CompilerArguments { public abstract boolean isVerbose(); public abstract String getSrc(); + + public abstract boolean suppressAllWarnings(); } diff --git a/compiler/testData/cli/help.out b/compiler/testData/cli/help.out index 68e88a4e834..7b71ad45156 100644 --- a/compiler/testData/cli/help.out +++ b/compiler/testData/cli/help.out @@ -13,9 +13,10 @@ Usage: org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments -output [String] output directory -module [String] module to compile -script [flag] evaluate script + -kotlinHome [String] Path to Kotlin compiler home directory, used for annotations and runtime libraries discovery -tags [flag] Demarcate each compilation message (error, warning, etc) with an open and close tag -verbose [flag] Enable verbose logging output -version [flag] Display compiler version - -help (-h) [flag] show help - -kotlinHome [String] Path to Kotlin compiler home directory, used for annotations and runtime libraries discovery + -help (-h) [flag] Show help + -suppress [String] Suppress compiler messages by severity (warnings) OK diff --git a/compiler/testData/cli/suppressAllWarningsJS.out b/compiler/testData/cli/suppressAllWarningsJS.out new file mode 100644 index 00000000000..d86bac9de59 --- /dev/null +++ b/compiler/testData/cli/suppressAllWarningsJS.out @@ -0,0 +1 @@ +OK diff --git a/compiler/testData/cli/suppressAllWarningsLowercase.out b/compiler/testData/cli/suppressAllWarningsLowercase.out new file mode 100644 index 00000000000..d86bac9de59 --- /dev/null +++ b/compiler/testData/cli/suppressAllWarningsLowercase.out @@ -0,0 +1 @@ +OK diff --git a/compiler/testData/cli/suppressAllWarningsMixedCase.out b/compiler/testData/cli/suppressAllWarningsMixedCase.out new file mode 100644 index 00000000000..d86bac9de59 --- /dev/null +++ b/compiler/testData/cli/suppressAllWarningsMixedCase.out @@ -0,0 +1 @@ +OK diff --git a/compiler/testData/cli/warnings.kt b/compiler/testData/cli/warnings.kt new file mode 100644 index 00000000000..0c84ed3b416 --- /dev/null +++ b/compiler/testData/cli/warnings.kt @@ -0,0 +1,10 @@ +import jet.Any +import jet.Any + +fun foo(p: Int??) { + +} + +trait T { + abstract fun foo() +} \ No newline at end of file diff --git a/compiler/testData/cli/wrongArgument.out b/compiler/testData/cli/wrongArgument.out index 370e7161fcf..687b7245532 100644 --- a/compiler/testData/cli/wrongArgument.out +++ b/compiler/testData/cli/wrongArgument.out @@ -14,9 +14,10 @@ Usage: org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments -output [String] output directory -module [String] module to compile -script [flag] evaluate script + -kotlinHome [String] Path to Kotlin compiler home directory, used for annotations and runtime libraries discovery -tags [flag] Demarcate each compilation message (error, warning, etc) with an open and close tag -verbose [flag] Enable verbose logging output -version [flag] Display compiler version - -help (-h) [flag] show help - -kotlinHome [String] Path to Kotlin compiler home directory, used for annotations and runtime libraries discovery + -help (-h) [flag] Show help + -suppress [String] Suppress compiler messages by severity (warnings) INTERNAL_ERROR diff --git a/compiler/tests/org/jetbrains/jet/cli/jvm/CliTest.java b/compiler/tests/org/jetbrains/jet/cli/jvm/CliTest.java index 98e6b836fe6..3c44a2dea94 100644 --- a/compiler/tests/org/jetbrains/jet/cli/jvm/CliTest.java +++ b/compiler/tests/org/jetbrains/jet/cli/jvm/CliTest.java @@ -21,6 +21,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.cli.common.CLICompiler; import org.jetbrains.jet.cli.common.ExitCode; +import org.jetbrains.jet.cli.js.K2JSCompiler; import org.jetbrains.jet.cli.jvm.compiler.KotlinToJVMBytecodeCompiler; import org.jetbrains.jet.lang.parsing.JetScriptDefinition; import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter; @@ -48,12 +49,12 @@ public class CliTest { public final TestName testName = new TestName(); @NotNull - private String executeCompilerGrabOutput(@NotNull String[] args) { + private static String executeCompilerGrabOutput(@NotNull CLICompiler compiler, @NotNull String[] args) { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); PrintStream origOut = System.out; try { System.setOut(new PrintStream(bytes)); - ExitCode exitCode = CLICompiler.doMainNoExit(new K2JVMCompiler(), args); + ExitCode exitCode = CLICompiler.doMainNoExit(compiler, args); return bytes.toString("utf-8") + exitCode + "\n"; } catch (Exception e) { @@ -64,21 +65,28 @@ public class CliTest { } } - private void executeCompilerCompareOutput(@NotNull String[] args) { - String actual = executeCompilerGrabOutput(args) + private void executeCompilerCompareOutput(@NotNull CLICompiler compiler, @NotNull String[] args) { + String actual = executeCompilerGrabOutput(compiler, args) .replace(new File("compiler/testData/cli/").getAbsolutePath(), "$TESTDATA_DIR$") .replace("\\", "/"); JetTestUtils.assertEqualsToFile(new File("compiler/testData/cli/" + testName.getMethodName() + ".out"), actual); } + private void executeCompilerCompareOutputJVM(@NotNull String[] args) { + executeCompilerCompareOutput(new K2JVMCompiler(), args); + } + + private void executeCompilerCompareOutputJS(@NotNull String[] args) { + executeCompilerCompareOutput(new K2JSCompiler(), args); + } @Test public void simple() throws Exception { String[] args = { "-src", "compiler/testData/cli/simple.kt", "-output", tmpdir.getTmpDir().getPath()}; - executeCompilerCompareOutput(args); + executeCompilerCompareOutputJVM(args); Assert.assertTrue(new File(tmpdir.getTmpDir(), PackageClassUtils.getPackageClassName(FqName.ROOT) + ".class").isFile()); } @@ -90,7 +98,7 @@ public class CliTest { + File.pathSeparator + "compiler/testData/cli/diagnosticsOrder2.kt", "-output", tmpdir.getTmpDir().getPath()}; - executeCompilerCompareOutput(args); + executeCompilerCompareOutputJVM(args); } @Test @@ -98,7 +106,7 @@ public class CliTest { String[] args = { "-src", "compiler/testData/cli/multipleTextRangesInDiagnosticsOrder.kt", "-output", tmpdir.getTmpDir().getPath()}; - executeCompilerCompareOutput(args); + executeCompilerCompareOutputJVM(args); } @Test @@ -107,7 +115,7 @@ public class CliTest { "-src", "compiler/testData/cli/wrongKotlinSignature.kt", "-classpath", "compiler/testData/cli/wrongKotlinSignatureLib", "-output", tmpdir.getTmpDir().getPath()}; - executeCompilerCompareOutput(args); + executeCompilerCompareOutputJVM(args); } @Test @@ -116,29 +124,29 @@ public class CliTest { "-src", "compiler/testData/cli/wrongAbiVersion.kt", "-classpath", "compiler/testData/cli/wrongAbiVersionLib", "-output", tmpdir.getTmpDir().getPath()}; - executeCompilerCompareOutput(args); + executeCompilerCompareOutputJVM(args); } @Test public void help() throws Exception { - executeCompilerCompareOutput(new String[] {"-help"}); + executeCompilerCompareOutputJVM(new String[] {"-help"}); } @Test public void script() throws Exception { - executeCompilerCompareOutput(new String[]{ "-script", "compiler/testData/cli/script.ktscript", "hi", "there" }); + executeCompilerCompareOutputJVM(new String[] {"-script", "compiler/testData/cli/script.ktscript", "hi", "there"}); } @Test public void wrongArgument() { - executeCompilerCompareOutput(new String[] { "-wrongArgument" }); + executeCompilerCompareOutputJVM(new String[] {"-wrongArgument"}); } @Test public void printArguments() { try { System.setProperty("kotlin.print.cmd.args", "true"); - executeCompilerCompareOutput(new String[] {"-script", "compiler/testData/cli/hello.ktscript"}); + executeCompilerCompareOutputJVM(new String[] {"-script", "compiler/testData/cli/hello.ktscript"}); } finally { System.clearProperty("kotlin.print.cmd.args"); @@ -152,7 +160,7 @@ public class CliTest { "-classpath", "not/existing/path", "-annotations", "yet/another/not/existing/path", "-output", tmpdir.getTmpDir().getPath()}; - executeCompilerCompareOutput(args); + executeCompilerCompareOutputJVM(args); Assert.assertTrue(new File(tmpdir.getTmpDir(), PackageClassUtils.getPackageClassName(FqName.ROOT) + ".class").isFile()); } @@ -162,7 +170,7 @@ public class CliTest { String[] args = { "-src", "not/existing/path", "-output", tmpdir.getTmpDir().getPath()}; - executeCompilerCompareOutput(args); + executeCompilerCompareOutputJVM(args); } @Test @@ -199,7 +207,6 @@ public class CliTest { } } - @Test public void testScriptWithScriptDefinition() { LinkedList scriptParameters = new LinkedList(); @@ -216,4 +223,31 @@ public class CliTest { throw new RuntimeException(e); } } + + @Test + public void suppressAllWarningsLowercase() { + String[] args = { + "-src", "compiler/testData/cli/warnings.kt", + "-suppress", "warnings", + "-output", tmpdir.getTmpDir().getPath()}; + executeCompilerCompareOutputJVM(args); + } + + @Test + public void suppressAllWarningsMixedCase() { + String[] args = { + "-src", "compiler/testData/cli/warnings.kt", + "-suppress", "WaRnInGs", + "-output", tmpdir.getTmpDir().getPath()}; + executeCompilerCompareOutputJVM(args); + } + + @Test + public void suppressAllWarningsJS() { + String[] args = { + "-sourceFiles", "compiler/testData/cli/warnings.kt", + "-suppress", "WaRnInGs", + "-output", new File(tmpdir.getTmpDir(), "out.js").getPath()}; + executeCompilerCompareOutputJS(args); + } }