Skip to content

Commit

Permalink
Improve -Werror: handling
Browse files Browse the repository at this point in the history
* update the formatted diagnostic text so that warnings that cause the build to
fail because of `-Werror:` are formatted with the `error:` text, to make it
obvious which warnings caused the build to fail and which are just warnings

* also override the diagnostic kind, to ensure that errors are sorted after
warnings

PiperOrigin-RevId: 607014038
Change-Id: I589b42ecf423781dfe031cdadd3710de4208990f
  • Loading branch information
cushon authored and copybara-github committed Feb 14, 2024
1 parent 1ef93be commit 8aedcc4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public static BlazeJavacResult compile(BlazeJavacArguments arguments) {
return BlazeJavacResult.error(e.getMessage());
}

Optional<WerrorCustomOption> maybeWerrorCustom =
arguments.blazeJavacOptions().stream()
.filter(arg -> arg.startsWith("-Werror:"))
.collect(toOptional())
.map(WerrorCustomOption::create);

Context context = new Context();
BlazeJavacStatistics.preRegister(context);
CacheFSInfo.preRegister(context);
Expand All @@ -113,7 +119,7 @@ public static BlazeJavacResult compile(BlazeJavacArguments arguments) {
// TODO(cushon): where is this used when a diagnostic listener is registered? Consider removing
// it and handling exceptions directly in callers.
PrintWriter errWriter = new PrintWriter(errOutput);
Listener diagnosticsBuilder = new Listener(arguments.failFast(), context);
Listener diagnosticsBuilder = new Listener(arguments.failFast(), maybeWerrorCustom, context);
BlazeJavaCompiler compiler;

// Initialize parts of context that the filemanager depends on
Expand Down Expand Up @@ -189,20 +195,10 @@ public static BlazeJavacResult compile(BlazeJavacArguments arguments) {

boolean werror =
diagnostics.stream().anyMatch(d -> d.getCode().equals("compiler.err.warnings.and.werror"));
if (status.equals(Status.OK)) {
Optional<WerrorCustomOption> maybeWerrorCustom =
arguments.blazeJavacOptions().stream()
.filter(arg -> arg.startsWith("-Werror:"))
.collect(toOptional())
.map(WerrorCustomOption::create);
if (maybeWerrorCustom.isPresent()) {
WerrorCustomOption werrorCustom = maybeWerrorCustom.get();
if (diagnostics.stream().anyMatch(d -> isWerror(werrorCustom, d))) {
errOutput.append("error: warnings found and -Werror specified\n");
status = Status.ERROR;
werror = true;
}
}
if (status.equals(Status.OK) && diagnosticsBuilder.werror()) {
errOutput.append("error: warnings found and -Werror specified\n");
status = Status.ERROR;
werror = true;
}

return BlazeJavacResult.createFullResult(
Expand All @@ -227,16 +223,6 @@ private static Status fromResult(Result result) {
throw new AssertionError(result);
}

private static boolean isWerror(WerrorCustomOption werrorCustom, FormattedDiagnostic diagnostic) {
switch (diagnostic.getKind()) {
case WARNING:
case MANDATORY_WARNING:
return werrorCustom.isEnabled(diagnostic.getLintCategory());
default:
return false;
}
}

private static final ImmutableSet<String> IGNORED_DIAGNOSTIC_CODES =
ImmutableSet.of(
"compiler.note.deprecated.filename",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.sun.tools.javac.util.JavacMessages;
import com.sun.tools.javac.util.Log;
import java.util.Locale;
import java.util.Optional;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileObject;
Expand All @@ -38,12 +39,17 @@ public class FormattedDiagnostic implements Diagnostic<JavaFileObject> {
public final Diagnostic<? extends JavaFileObject> diagnostic;
public final String formatted;
public final String lintCategory;
public final boolean werror;

public FormattedDiagnostic(
Diagnostic<? extends JavaFileObject> diagnostic, String formatted, String lintCategory) {
Diagnostic<? extends JavaFileObject> diagnostic,
String formatted,
String lintCategory,
boolean werror) {
this.diagnostic = diagnostic;
this.formatted = formatted;
this.lintCategory = lintCategory;
this.werror = werror;
}

/** The formatted diagnostic message produced by javac's diagnostic formatter. */
Expand All @@ -62,7 +68,7 @@ public String toString() {

@Override
public Kind getKind() {
return diagnostic.getKind();
return werror ? Kind.ERROR : diagnostic.getKind();
}

@Override
Expand Down Expand Up @@ -111,32 +117,69 @@ static class Listener implements DiagnosticListener<JavaFileObject> {

private final ImmutableList.Builder<FormattedDiagnostic> diagnostics = ImmutableList.builder();
private final boolean failFast;
private final Optional<WerrorCustomOption> werrorCustomOption;
private final Context context;

Listener(boolean failFast, Context context) {
private boolean werror = false;

Listener(boolean failFast, Optional<WerrorCustomOption> werrorCustomOption, Context context) {
this.failFast = failFast;
this.werrorCustomOption = werrorCustomOption;
// retrieve context values later, in case it isn't initialized yet
this.context = context;
}

@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
JCDiagnostic jcDiagnostic = (JCDiagnostic) diagnostic;
boolean werror = isWerror(jcDiagnostic);
if (werror) {
this.werror = true;
}
DiagnosticFormatter<JCDiagnostic> formatter = Log.instance(context).getDiagnosticFormatter();
Locale locale = JavacMessages.instance(context).getCurrentLocale();
String formatted = formatter.format((JCDiagnostic) diagnostic, locale);
LintCategory lintCategory = ((JCDiagnostic) diagnostic).getLintCategory();
JavacMessages messages = JavacMessages.instance(context);
Locale locale = messages.getCurrentLocale();
String formatted = formatter.format(jcDiagnostic, locale);
if (werror) {
formatted =
formatted.replaceFirst(
formatter.formatKind(jcDiagnostic, locale),
messages.getLocalizedString(locale, "compiler.err.error"));
}
LintCategory lintCategory = jcDiagnostic.getLintCategory();
FormattedDiagnostic formattedDiagnostic =
new FormattedDiagnostic(
diagnostic, formatted, lintCategory != null ? lintCategory.option : null);
diagnostic, formatted, lintCategory != null ? lintCategory.option : null, werror);
diagnostics.add(formattedDiagnostic);
if (failFast && diagnostic.getKind().equals(Diagnostic.Kind.ERROR)) {
throw new FailFastException(formatted);
}
}

private boolean isWerror(JCDiagnostic diagnostic) {
if (werrorCustomOption.isEmpty()) {
return false;
}
LintCategory lintCategory = diagnostic.getLintCategory();
if (lintCategory == null) {
return false;
}
switch (diagnostic.getKind()) {
case WARNING:
case MANDATORY_WARNING:
return werrorCustomOption.get().isEnabled(lintCategory.option);
default:
return false;
}
}

ImmutableList<FormattedDiagnostic> build() {
return diagnostics.build();
}

boolean werror() {
return werror;
}
}

static class FailFastException extends RuntimeException {
Expand Down

0 comments on commit 8aedcc4

Please sign in to comment.