From de048fb13cceb70aec73b1f7c22b843ce3768d48 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 15 Oct 2024 21:11:43 -0700 Subject: [PATCH] Update `--should-stop=ifError=FLOW` flags This is equivalent to `-XDshould-stop.ifError=FLOW`, but the `-XD` flag is a internal javac debug flag interface that writes directly to the options table. The `--should-stop` flags are slightly more standard. https://github.com/google/error-prone/issues/4595 PiperOrigin-RevId: 686353769 --- .../errorprone/BaseErrorProneJavaCompiler.java | 14 ++++++-------- .../ErrorProneCompilerIntegrationTest.java | 14 ++++++++++++-- .../errorprone/ErrorProneJavacPluginTest.java | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/BaseErrorProneJavaCompiler.java b/check_api/src/main/java/com/google/errorprone/BaseErrorProneJavaCompiler.java index 6778d2d41909..0d81955c2fe5 100644 --- a/check_api/src/main/java/com/google/errorprone/BaseErrorProneJavaCompiler.java +++ b/check_api/src/main/java/com/google/errorprone/BaseErrorProneJavaCompiler.java @@ -196,27 +196,25 @@ private static ImmutableList setCompilePolicyToByFile(ImmutableListbuilder().addAll(args).add("-XDcompilePolicy=simple").build(); } - private static void checkShouldStopIfErrorPolicy(String value) { + private static void checkShouldStopIfErrorPolicy(String arg) { + String value = arg.substring(arg.lastIndexOf('=') + 1); CompileState state = CompileState.valueOf(value); if (CompileState.FLOW.isAfter(state)) { throw new InvalidCommandLineOptionException( String.format( - "-XDshould-stop.ifError=%s is not supported by Error Prone, pass" - + " -XDshould-stop.ifError=FLOW instead", - state)); + "%s is not supported by Error Prone, pass --should-stop=ifError=FLOW instead", arg)); } } private static ImmutableList setShouldStopIfErrorPolicyToFlow( ImmutableList args) { for (String arg : args) { - if (arg.startsWith("-XDshould-stop.ifError")) { - String value = arg.substring(arg.indexOf('=') + 1); - checkShouldStopIfErrorPolicy(value); + if (arg.startsWith("--should-stop=ifError") || arg.startsWith("-XDshould-stop.ifError")) { + checkShouldStopIfErrorPolicy(arg); return args; // don't do anything if a valid policy is already set } } - return ImmutableList.builder().addAll(args).add("-XDshould-stop.ifError=FLOW").build(); + return ImmutableList.builder().addAll(args).add("--should-stop=ifError=FLOW").build(); } /** Registers our message bundle. */ diff --git a/core/src/test/java/com/google/errorprone/ErrorProneCompilerIntegrationTest.java b/core/src/test/java/com/google/errorprone/ErrorProneCompilerIntegrationTest.java index ef52380e3be2..4f5dc18ec60c 100644 --- a/core/src/test/java/com/google/errorprone/ErrorProneCompilerIntegrationTest.java +++ b/core/src/test/java/com/google/errorprone/ErrorProneCompilerIntegrationTest.java @@ -689,7 +689,7 @@ public void stopPolicy_effectivelyFinal() { compilerBuilder.report( ScannerSupplier.fromBugCheckerClasses(EffectivelyFinalChecker.class, CPSChecker.class)); compiler = compilerBuilder.build(); - // Without -XDshould-stop.ifError=FLOW, the errors reported by CPSChecker will cause javac to + // Without --should-stop=ifError=FLOW, the errors reported by CPSChecker will cause javac to // stop processing B after an error is reported in A. Error Prone will still analyze B without // it having gone through 'flow', and the EFFECTIVELY_FINAL analysis will not have happened. // see https://github.com/google/error-prone/issues/4595 @@ -729,7 +729,7 @@ int f(int x) { public void stopPolicy_flow() { Result exitCode = compiler.compile( - new String[] {"-XDshould-stop.ifError=FLOW"}, + new String[] {"--should-stop=ifError=FLOW"}, ImmutableList.of( forSourceLines( "Test.java", @@ -743,6 +743,16 @@ class Test {} @Test public void stopPolicy_init() { + InvalidCommandLineOptionException e = + assertThrows( + InvalidCommandLineOptionException.class, + () -> + compiler.compile(new String[] {"--should-stop=ifError=INIT"}, ImmutableList.of())); + assertThat(e).hasMessageThat().contains("--should-stop=ifError=INIT is not supported"); + } + + @Test + public void stopPolicy_init_xD() { InvalidCommandLineOptionException e = assertThrows( InvalidCommandLineOptionException.class, diff --git a/core/src/test/java/com/google/errorprone/ErrorProneJavacPluginTest.java b/core/src/test/java/com/google/errorprone/ErrorProneJavacPluginTest.java index 21d38ee64bfb..b50eef04927d 100644 --- a/core/src/test/java/com/google/errorprone/ErrorProneJavacPluginTest.java +++ b/core/src/test/java/com/google/errorprone/ErrorProneJavacPluginTest.java @@ -304,7 +304,7 @@ public void stopOnErrorPolicy() throws IOException { ImmutableList.of( "-Xplugin:ErrorProne", "-XDcompilePolicy=byfile", - "-XDshould-stop.ifError=LOWER"), + "--should-stop=ifError=LOWER"), ImmutableList.of(), fileManager.getJavaFileObjects(one, two)); assertThat(task.call()).isFalse();