From e059e1612663c638423016addada76b9cd67fefe Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Thu, 13 Feb 2025 14:50:26 -0600 Subject: [PATCH 1/6] Add support for -Werror:key. --- .../sun/tools/javac/api/JavacTaskPool.java | 3 +- .../com/sun/tools/javac/code/Lint.java | 61 ++++-------- .../com/sun/tools/javac/comp/Modules.java | 2 +- .../com/sun/tools/javac/main/Arguments.java | 5 +- .../sun/tools/javac/main/JavaCompiler.java | 19 ++-- .../com/sun/tools/javac/main/Option.java | 31 +++++- .../JavacProcessingEnvironment.java | 2 +- .../tools/javac/resources/javac.properties | 14 ++- .../classes/com/sun/tools/javac/util/Log.java | 30 +++++- .../com/sun/tools/javac/util/Options.java | 94 +++++++++++++++++++ .../tools/javac/warnings/WerrorLint.e1.out | 4 + .../tools/javac/warnings/WerrorLint.e2.out | 5 + .../tools/javac/warnings/WerrorLint.java | 23 +++++ .../tools/javac/warnings/WerrorLint.w1.out | 2 + .../tools/javac/warnings/WerrorLint.w2.out | 3 + 15 files changed, 231 insertions(+), 67 deletions(-) create mode 100644 test/langtools/tools/javac/warnings/WerrorLint.e1.out create mode 100644 test/langtools/tools/javac/warnings/WerrorLint.e2.out create mode 100644 test/langtools/tools/javac/warnings/WerrorLint.java create mode 100644 test/langtools/tools/javac/warnings/WerrorLint.w1.out create mode 100644 test/langtools/tools/javac/warnings/WerrorLint.w2.out diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java index 1ce4cf50ada8a..396867eb6a92a 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -400,6 +400,7 @@ void clear() { sourceMap.clear(); nerrors = 0; nwarnings = 0; + lintWarnings.clear(); //Set a fake listener that will lazily lookup the context for the 'real' listener. Since //this field is never updated when a new task is created, we cannot simply reset the field //or keep old value. This is a hack to workaround the limitations in the current infrastructure. diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java index ca888676009ba..01405b773bedc 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java @@ -143,50 +143,29 @@ protected Lint(Lint other) { // Process command line options on demand to allow use of root Lint early during startup private void initializeRootIfNeeded() { - - // Already initialized? - if (values != null) - return; - - // Initialize enabled categories based on "-Xlint" flags - if (options.isSet(Option.XLINT) || options.isSet(Option.XLINT_CUSTOM, "all")) { - // If -Xlint or -Xlint:all is given, enable all categories by default - values = EnumSet.allOf(LintCategory.class); - } else if (options.isSet(Option.XLINT_CUSTOM, "none")) { - // if -Xlint:none is given, disable all categories by default - values = LintCategory.newEmptySet(); - } else { - // otherwise, enable on-by-default categories - values = LintCategory.newEmptySet(); - - Source source = Source.instance(context); - if (source.compareTo(Source.JDK9) >= 0) { - values.add(LintCategory.DEP_ANN); - } - if (Source.Feature.REDUNDANT_STRICTFP.allowedInSource(source)) { - values.add(LintCategory.STRICTFP); - } - values.add(LintCategory.REQUIRES_TRANSITIVE_AUTOMATIC); - values.add(LintCategory.OPENS); - values.add(LintCategory.MODULE); - values.add(LintCategory.REMOVAL); - if (!options.isSet(Option.PREVIEW)) { - values.add(LintCategory.PREVIEW); - } - values.add(LintCategory.SYNCHRONIZATION); - values.add(LintCategory.INCUBATING); + if (values == null) { + values = options.getLintCategories(Option.XLINT, this::populateDefaults); + suppressedValues = LintCategory.newEmptySet(); } + } - // Look for specific overrides - for (LintCategory lc : LintCategory.values()) { - if (options.isSet(Option.XLINT_CUSTOM, lc.option)) { - values.add(lc); - } else if (options.isSet(Option.XLINT_CUSTOM, "-" + lc.option)) { - values.remove(lc); - } + private void populateDefaults(EnumSet values) { + Source source = Source.instance(context); + if (source.compareTo(Source.JDK9) >= 0) { + values.add(LintCategory.DEP_ANN); } - - suppressedValues = LintCategory.newEmptySet(); + if (Source.Feature.REDUNDANT_STRICTFP.allowedInSource(source)) { + values.add(LintCategory.STRICTFP); + } + values.add(LintCategory.REQUIRES_TRANSITIVE_AUTOMATIC); + values.add(LintCategory.OPENS); + values.add(LintCategory.MODULE); + values.add(LintCategory.REMOVAL); + if (!options.isSet(Option.PREVIEW)) { + values.add(LintCategory.PREVIEW); + } + values.add(LintCategory.SYNCHRONIZATION); + values.add(LintCategory.INCUBATING); } @Override diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java index 596c5704cbef3..a159793fe327f 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java @@ -205,7 +205,7 @@ protected Modules(Context context) { allowAccessIntoSystem = options.isUnset(Option.RELEASE); - lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option); + lintOptions = !options.isExplicitlyDisabled(Option.XLINT, LintCategory.OPTIONS); multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH); ClassWriter classWriter = ClassWriter.instance(context); diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java index 14b42bc01c5d3..9e3a978c3bc9a 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java @@ -503,8 +503,7 @@ public boolean validate() { } } else { // single-module or legacy mode - boolean lintPaths = options.isUnset(Option.XLINT_CUSTOM, - "-" + LintCategory.PATH.option); + boolean lintPaths = !options.isExplicitlyDisabled(Option.XLINT, LintCategory.PATH); if (lintPaths) { Path outDirParent = outDir.getParent(); if (outDirParent != null && Files.exists(outDirParent.resolve("module-info.class"))) { @@ -577,7 +576,7 @@ public boolean validate() { reportDiag(Errors.SourcepathModulesourcepathConflict); } - boolean lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option); + boolean lintOptions = !options.isExplicitlyDisabled(Option.XLINT, LintCategory.OPTIONS); if (lintOptions && source.compareTo(Source.DEFAULT) < 0 && !options.isSet(Option.RELEASE)) { if (fm instanceof BaseFileManager baseFileManager) { if (source.compareTo(Source.JDK8) <= 0) { diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java index 05efcf7c04112..bc22f4f99210d 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java @@ -31,6 +31,7 @@ import java.nio.file.ReadOnlyFileSystemException; import java.util.Collection; import java.util.Comparator; +import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -442,9 +443,7 @@ public JavaCompiler(Context context) { // See if lint options checking was explicitly disabled by the // user; this is distinct from the options check being // enabled/disabled. - optionsCheckingInitiallyDisabled = - options.isSet(Option.XLINT_CUSTOM, "-options") || - options.isSet(Option.XLINT_CUSTOM, "none"); + optionsCheckingInitiallyDisabled = options.isExplicitlyDisabled(Option.XLINT, LintCategory.OPTIONS); verbose = options.isSet(VERBOSE); sourceOutput = options.isSet(PRINTSOURCE); // used to be -s @@ -454,7 +453,8 @@ public JavaCompiler(Context context) { context.get(DiagnosticListener.class) != null; devVerbose = options.isSet("dev"); processPcks = options.isSet("process.packages"); - werror = options.isSet(WERROR); + werrorAny = options.isSet(WERROR) || options.isSet(WERROR_CUSTOM, Option.LINT_CUSTOM_ALL); + werrorLint = options.getLintCategories(WERROR, null); verboseCompilePolicy = options.isSet("verboseCompilePolicy"); @@ -527,9 +527,13 @@ public boolean exists() { */ protected boolean processPcks; - /** Switch: treat warnings as errors + /** Switch: treat any kind of warning (including non-lint warnings) as an error. */ - protected boolean werror; + protected boolean werrorAny; + + /** Switch: treat lint warnings in the specified {@link LintCategory}s as errors. + */ + protected EnumSet werrorLint; /** Switch: is annotation processing requested explicitly via * CompilationTask.setProcessors? @@ -594,7 +598,8 @@ protected boolean shouldStop(CompileState cs) { /** The number of errors reported so far. */ public int errorCount() { - if (werror && log.nerrors == 0 && log.nwarnings > 0) { + if (log.nerrors == 0 && log.nwarnings > 0 && + (werrorAny || werrorLint.clone().removeAll(log.lintWarnings))) { log.error(Errors.WarningsAndWerror); } return log.nerrors; diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java index 44d90a7e59b73..a3d25bd67e0a4 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Option.java @@ -497,7 +497,7 @@ public void process(OptionHelper helper, String option) throws InvalidValueExcep log.printRawLines(WriterKind.STDOUT, log.localize(PrefixKind.JAVAC, "opt.help.lint.header")); log.printRawLines(WriterKind.STDOUT, String.format(LINT_KEY_FORMAT, - "all", + LINT_CUSTOM_ALL, log.localize(PrefixKind.JAVAC, "opt.Xlint.all"))); for (LintCategory lc : LintCategory.values()) { log.printRawLines(WriterKind.STDOUT, @@ -508,7 +508,7 @@ public void process(OptionHelper helper, String option) throws InvalidValueExcep } log.printRawLines(WriterKind.STDOUT, String.format(LINT_KEY_FORMAT, - "none", + LINT_CUSTOM_NONE, log.localize(PrefixKind.JAVAC, "opt.Xlint.none"))); super.process(helper, option); } @@ -539,6 +539,8 @@ public void process(OptionHelper helper, String option) throws InvalidValueExcep // treat warnings as errors WERROR("-Werror", "opt.Werror", STANDARD, BASIC), + WERROR_CUSTOM("-Werror:", "opt.arg.Werror", "opt.Werror.custom", STANDARD, BASIC, ANYOF, getXLintChoices()), + // prompt after each error // new Option("-prompt", "opt.prompt"), PROMPT("-prompt", null, HIDDEN, BASIC), @@ -841,6 +843,16 @@ private Option[] getSupportedRuntimeOptions() { } }; + /** + * Special lint category key meaning "all lint categories". + */ + public static final String LINT_CUSTOM_ALL = "all"; + + /** + * Special lint category key meaning "no lint categories". + */ + public static final String LINT_CUSTOM_NONE = "none"; + /** * This exception is thrown when an invalid value is given for an option. * The detail string gives a detailed, localized message, suitable for use @@ -1087,6 +1099,17 @@ public OptionKind getKind() { return kind; } + /** + * If this option is named {@code FOO}, obtain the option named {@code FOO_CUSTOM}. + * + * @param option regular option + * @return corresponding custom option + * @throws IllegalArgumentException if no such option exists + */ + public Option getCustom() { + return Option.valueOf(name() + "_CUSTOM"); + } + public boolean isInBasicOptionGroup() { return group == BASIC; } @@ -1370,12 +1393,12 @@ public static PkgInfo get(Options options) { private static Set getXLintChoices() { Set choices = new LinkedHashSet<>(); - choices.add("all"); + choices.add(LINT_CUSTOM_ALL); for (Lint.LintCategory c : Lint.LintCategory.values()) { choices.add(c.option); choices.add("-" + c.option); } - choices.add("none"); + choices.add(LINT_CUSTOM_NONE); return choices; } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java index 8147ac275285d..9c5dd264332bc 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java @@ -212,7 +212,7 @@ protected JavacProcessingEnvironment(Context context) { } fatalErrors = options.isSet("fatalEnterError"); showResolveErrors = options.isSet("showResolveErrors"); - werror = options.isSet(Option.WERROR); + werror = options.isSet(Option.WERROR, PROCESSING, false); fileManager = context.get(JavaFileManager.class); platformAnnotations = initPlatformAnnotations(); diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties index 28a89ee3072f5..9a49c981574aa 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/javac.properties @@ -1,5 +1,5 @@ # -# Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # This code is free software; you can redistribute it and/or modify it @@ -95,7 +95,13 @@ javac.opt.source=\ Provide source compatibility with the specified Java SE release.\n\ Supported releases: \n {0} javac.opt.Werror=\ - Terminate compilation if warnings occur + Terminate compilation if any warnings occur +javac.opt.arg.Werror=\ + (,)* +javac.opt.Werror.custom=\ + Specify warnings that should terminate compilation, separated by comma.\n\ + Precede a key by ''-'' to exclude the specified warning.\n\ + Use --help-lint to see the supported keys. javac.opt.A=\ Options to pass to annotation processors javac.opt.implicit=\ @@ -324,9 +330,9 @@ javac.opt.X=\ javac.opt.help=\ Print this help message javac.opt.help.lint=\ - Print the supported keys for -Xlint + Print the supported keys for -Xlint and -Werror javac.opt.help.lint.header=\ - The supported keys for -Xlint are: + The supported keys for -Xlint and -Werror are: javac.opt.print=\ Print out a textual representation of specified types javac.opt.printRounds=\ diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java index 56ee413ea0716..7e83abc63c2d9 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Log.java @@ -29,8 +29,10 @@ import java.util.Arrays; import java.util.Comparator; import java.util.EnumMap; +import java.util.EnumSet; import java.util.HashSet; import java.util.Map; +import java.util.Optional; import java.util.Queue; import java.util.Set; import java.util.function.Predicate; @@ -39,6 +41,7 @@ import javax.tools.JavaFileObject; import com.sun.tools.javac.api.DiagnosticFormatter; +import com.sun.tools.javac.code.Lint.LintCategory; import com.sun.tools.javac.main.Main; import com.sun.tools.javac.main.Option; import com.sun.tools.javac.tree.EndPosTable; @@ -410,10 +413,14 @@ protected int getDefaultMaxWarnings() { */ public int nerrors = 0; - /** The number of warnings encountered so far. + /** The total number of warnings encountered so far. */ public int nwarnings = 0; + /** Tracks whether any warnings have been encountered per {@link LintCategory}. + */ + public final EnumSet lintWarnings = LintCategory.newEmptySet(); + /** The number of errors encountered after MaxErrors was reached. */ public int nsuppressederrors = 0; @@ -677,7 +684,6 @@ protected void directError(String key, Object... args) { */ public void strictWarning(DiagnosticPosition pos, String key, Object ... args) { writeDiagnostic(diags.warning(null, source, pos, key, args)); - nwarnings++; } /** @@ -722,7 +728,6 @@ public void report(JCDiagnostic diagnostic) { if (emitWarnings || diagnostic.isMandatory()) { if (nwarnings < MaxWarnings) { writeDiagnostic(diagnostic); - nwarnings++; } else { nsuppressedwarns++; } @@ -734,7 +739,6 @@ public void report(JCDiagnostic diagnostic) { shouldReport(diagnostic)) { if (nerrors < MaxErrors) { writeDiagnostic(diagnostic); - nerrors++; } else { nsuppressederrors++; } @@ -748,9 +752,25 @@ public void report(JCDiagnostic diagnostic) { } /** - * Write out a diagnostic. + * Write out a diagnostic and bump the warning and error counters as needed. */ protected void writeDiagnostic(JCDiagnostic diag) { + + // Increment counter(s) + switch (diag.getType()) { + case WARNING: + nwarnings++; + Optional.of(diag) + .map(JCDiagnostic::getLintCategory) + .ifPresent(lintWarnings::add); + break; + case ERROR: + nerrors++; + break; + default: + break; + } + if (diagListener != null) { diagListener.report(diag); return; diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java index e7a07a57a8f7f..16a5941a40fa2 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java @@ -29,6 +29,7 @@ import java.util.function.Consumer; import java.util.function.Supplier; +import com.sun.tools.javac.code.Lint.LintCategory; import com.sun.tools.javac.main.Option; import static com.sun.tools.javac.main.Option.*; @@ -170,6 +171,99 @@ public boolean isUnset(Option option, String value) { return !isSet(option, value); } + /** + * Collect the set of {@link LintCategory}s specified by option flag(s) of the form + * {@code -Flag} and/or {@code -Flag:[-]key,[-]key,...}, starting with the provided defaults. + * + *

+ * The set of categories is calculated as folllows. First, an initial set is created: + *

    + *
  • If the {@code -Flag:all} flag appears, the initial set contains all categories; otherwise, + *
  • If the {@code -Flag:none} flag appears, the initial set is empty; otherwise, + *
  • If {@code defaults} is not null, it is invoked to populate an initial set; otherwise, + *
  • The initial set is empty. + *
+ * Next, for each lint category {@code key}: + *
    + *
  • If the {@code -Flag:key} flag appears, the category is added to the set; otherwise + *
  • If the {@code -Flag:-key} flag appears, the category is removed to the set + *
+ * Unrecognized {@code key}s are ignored. + * + * @param option the plain option + * @param defaults populates the default set, or null for an empty default set + * @return the specified set of categories + */ + public EnumSet getLintCategories(Option option, Consumer> defaults) { + + // Create the initial set + EnumSet categories; + Option customOption = option.getCustom(); + if (isSet(option) || isSet(customOption, Option.LINT_CUSTOM_ALL)) { + categories = EnumSet.allOf(LintCategory.class); + } else if (isSet(customOption, Option.LINT_CUSTOM_NONE)) { + categories = LintCategory.newEmptySet(); + } else { + categories = EnumSet.noneOf(LintCategory.class); + if (defaults != null) { + defaults.accept(categories); + } + } + + // Apply specific overrides + for (LintCategory category : LintCategory.values()) { + if (isSet(customOption, category.option)) { + categories.add(category); + } else if (isSet(customOption, "-" + category.option)) { + categories.remove(category); + } + } + + // Done + return categories; + } + + /** + * Apply the logic of {@link #getLintCategories} to a single {@link LintCategory}. + * + *

+ * This returns true if and only if the specified {@link LintCategory} would be included + * in the set returned by {@link #getLintCategories} given the provided given default value. + * + * @param option the plain option + * @param category the {@link LintCategory} in question + * @param defaultValue presumed default value + * @return true if {@code category} would be included + */ + public boolean isSet(Option option, LintCategory category, boolean defaultValue) { + Option customOption = option.getCustom(); + if (isSet(customOption, category.option)) { + return true; + } + if (isSet(customOption, "-" + category.option)) { + return false; + } + if (isSet(option) || isSet(customOption, Option.LINT_CUSTOM_ALL)) { + return true; + } + if (isSet(customOption, Option.LINT_CUSTOM_NONE)) { + return false; + } + return defaultValue; + } + + /** + * Determine if a specific {@link LintCategory} was explicitly disabled via an option flag + * of the form {@code -Flag:none} or {@code -Flag:-key}. + * + * @param category the {@link LintCategory} in question + * @param option the option + * @return true if {@code category} has been explicitly disabled + */ + public boolean isExplicitlyDisabled(Option option, LintCategory category) { + return !isSet(option, category, true); + } + public void put(String name, String value) { values.put(name, value); initialized = true; diff --git a/test/langtools/tools/javac/warnings/WerrorLint.e1.out b/test/langtools/tools/javac/warnings/WerrorLint.e1.out new file mode 100644 index 0000000000000..ae99cfa5056e2 --- /dev/null +++ b/test/langtools/tools/javac/warnings/WerrorLint.e1.out @@ -0,0 +1,4 @@ +WerrorLint.java:20:19: compiler.warn.strictfp +- compiler.err.warnings.and.werror +1 error +1 warning diff --git a/test/langtools/tools/javac/warnings/WerrorLint.e2.out b/test/langtools/tools/javac/warnings/WerrorLint.e2.out new file mode 100644 index 0000000000000..1c9bd4d54f8e1 --- /dev/null +++ b/test/langtools/tools/javac/warnings/WerrorLint.e2.out @@ -0,0 +1,5 @@ +WerrorLint.java:20:19: compiler.warn.strictfp +WerrorLint.java:21:30: compiler.warn.empty.if +- compiler.err.warnings.and.werror +1 error +2 warnings diff --git a/test/langtools/tools/javac/warnings/WerrorLint.java b/test/langtools/tools/javac/warnings/WerrorLint.java new file mode 100644 index 0000000000000..3331a664d5598 --- /dev/null +++ b/test/langtools/tools/javac/warnings/WerrorLint.java @@ -0,0 +1,23 @@ +/* + * @test /nodynamiccopyright/ + * @bug 8349847 + * + * @compile -XDrawDiagnostics -Xlint:none WerrorLint.java + * @compile -XDrawDiagnostics -Xlint:none -Werror WerrorLint.java + * @compile -XDrawDiagnostics -Xlint:none -Werror:empty WerrorLint.java + * @compile -XDrawDiagnostics -Xlint:none -Werror:strictfp WerrorLint.java + * @compile/ref=WerrorLint.w2.out -XDrawDiagnostics -Xlint:all WerrorLint.java + * @compile/fail/ref=WerrorLint.e2.out -XDrawDiagnostics -Xlint:all -Werror WerrorLint.java + * @compile/fail/ref=WerrorLint.e2.out -XDrawDiagnostics -Xlint:all -Werror:empty WerrorLint.java + * @compile/fail/ref=WerrorLint.e2.out -XDrawDiagnostics -Xlint:all -Werror:strictfp WerrorLint.java + * @compile/ref=WerrorLint.w1.out -XDrawDiagnostics WerrorLint.java + * @compile/fail/ref=WerrorLint.e1.out -XDrawDiagnostics -Werror WerrorLint.java + * @compile/ref=WerrorLint.w1.out -XDrawDiagnostics -Werror:empty WerrorLint.java + * @compile/fail/ref=WerrorLint.e1.out -XDrawDiagnostics -Werror:strictfp WerrorLint.java + */ + +class WerrorLint { + strictfp void m() { // [strictfp] - this category is enabled by default + if (hashCode() == 1) ; // [empty] - this category is disabled by default + } +} diff --git a/test/langtools/tools/javac/warnings/WerrorLint.w1.out b/test/langtools/tools/javac/warnings/WerrorLint.w1.out new file mode 100644 index 0000000000000..3e19de5103373 --- /dev/null +++ b/test/langtools/tools/javac/warnings/WerrorLint.w1.out @@ -0,0 +1,2 @@ +WerrorLint.java:20:19: compiler.warn.strictfp +1 warning diff --git a/test/langtools/tools/javac/warnings/WerrorLint.w2.out b/test/langtools/tools/javac/warnings/WerrorLint.w2.out new file mode 100644 index 0000000000000..bac258706a6f3 --- /dev/null +++ b/test/langtools/tools/javac/warnings/WerrorLint.w2.out @@ -0,0 +1,3 @@ +WerrorLint.java:20:19: compiler.warn.strictfp +WerrorLint.java:21:30: compiler.warn.empty.if +2 warnings From 9069f9021b119dbc97fbaba82708541b41e8f2be Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Mon, 12 May 2025 20:48:10 -0500 Subject: [PATCH 2/6] Update man page. --- src/jdk.compiler/share/man/javac.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/jdk.compiler/share/man/javac.md b/src/jdk.compiler/share/man/javac.md index f951ea0def3aa..27461bb98b97b 100644 --- a/src/jdk.compiler/share/man/javac.md +++ b/src/jdk.compiler/share/man/javac.md @@ -1,5 +1,5 @@ --- -# Copyright (c) 1994, 2024, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 1994, 2025, Oracle and/or its affiliates. All rights reserved. # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. # # This code is free software; you can redistribute it and/or modify it @@ -448,7 +448,12 @@ file system locations may be directories, JAR files or JMOD files. : Prints version information. `-Werror` -: Terminates compilation when warnings occur. +: Terminates compilation when any warnings occur; this includes warnings in all lint + categories, as well as non-lint warnings. + +`-Werror:`\[`-`\]*key*(`,`\[`-`\]*key*)\* +: Enables and/or disables specific categories of lint warnings that should terminate compilation. + See [`-Xlint`](#option-Xlint-custom) below for the list of lint category keys. ### Extra Options From 5069bf2d66625fd220d693e49a923e0b383ced3c Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Mon, 16 Jun 2025 20:52:10 -0500 Subject: [PATCH 3/6] Restore behavior when both -Xlint:options and -Xlint:-options are given. --- .../com/sun/tools/javac/util/Options.java | 48 ++++++------------- .../tools/javac/lint/LintOptions.java | 11 +++++ .../tools/javac/lint/LintOptions.out | 4 ++ 3 files changed, 29 insertions(+), 34 deletions(-) create mode 100644 test/langtools/tools/javac/lint/LintOptions.java create mode 100644 test/langtools/tools/javac/lint/LintOptions.out diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java index 63f5b0ca75abb..c882fb4cf8a20 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java @@ -172,55 +172,35 @@ public boolean isUnset(Option option, String value) { } /** - * Check whether the given lint category is explicitly enabled or disabled. + * Determine if a specific {@link LintCategory} is explicitly enabled via a custom + * option flag of the form {@code -Flag:key}. * *

- * If the category is neither enabled nor disabled, return the given default value. + * Note: It's possible the category was also explicitly disabled; this method does not check that. * - * @param option the plain (non-custom) option - * @param lc the {@link LintCategory} in question - * @param defaultValue presumed default value - * @return true if {@code lc} would be included - */ - public boolean isSet(Option option, LintCategory lc, boolean defaultValue) { - Option customOption = option.getCustom(); - if (lc.optionList.stream().anyMatch(alias -> isSet(customOption, alias))) { - return true; - } - if (lc.optionList.stream().anyMatch(alias -> isSet(customOption, "-" + alias))) { - return false; - } - if (isSet(option) || isSet(customOption, Option.LINT_CUSTOM_ALL)) { - return true; - } - if (isSet(customOption, Option.LINT_CUSTOM_NONE)) { - return false; - } - return defaultValue; - } - - /** - * Determine if a specific {@link LintCategory} was explicitly enabled via a custom option flag - * of the form {@code -Flag:all} or {@code -Flag:key}. - * - * @param option the option + * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) * @param lc the {@link LintCategory} in question * @return true if {@code lc} has been explicitly enabled */ public boolean isExplicitlyEnabled(Option option, LintCategory lc) { - return isSet(option, lc, false); + Option customOption = option.getCustom(); + return lc.optionList.stream().anyMatch(alias -> isSet(customOption, alias)); } /** - * Determine if a specific {@link LintCategory} was explicitly disabled via a custom option flag - * of the form {@code -Flag:none} or {@code -Flag:-key}. + * Determine if a specific {@link LintCategory} is explicitly disabled via a custom + * option flag of the form {@code -Flag:-key}. * - * @param option the option + *

+ * Note: It's possible the category was also explicitly enabled; this method does not check that. + * + * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) * @param lc the {@link LintCategory} in question * @return true if {@code lc} has been explicitly disabled */ public boolean isExplicitlyDisabled(Option option, LintCategory lc) { - return !isSet(option, lc, true); + Option customOption = option.getCustom(); + return lc.optionList.stream().anyMatch(alias -> isSet(customOption, "-" + alias)); } public void put(String name, String value) { diff --git a/test/langtools/tools/javac/lint/LintOptions.java b/test/langtools/tools/javac/lint/LintOptions.java new file mode 100644 index 0000000000000..81b0a0dc3250b --- /dev/null +++ b/test/langtools/tools/javac/lint/LintOptions.java @@ -0,0 +1,11 @@ +/* + * @test /nodynamiccopyright/ + * @bug 8359596 + * @summary Verify behavior when both "-Xlint:options" and "-Xlint:-options" are given + * @compile/fail/ref=LintOptions.out -Werror -XDrawDiagnostics -source 21 -target 21 LintOptions.java + * @compile/fail/ref=LintOptions.out -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options LintOptions.java + * @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:-options LintOptions.java + * @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options -Xlint:-options LintOptions.java + */ +class LintOptions { +} diff --git a/test/langtools/tools/javac/lint/LintOptions.out b/test/langtools/tools/javac/lint/LintOptions.out new file mode 100644 index 0000000000000..020c626ee5c20 --- /dev/null +++ b/test/langtools/tools/javac/lint/LintOptions.out @@ -0,0 +1,4 @@ +- compiler.warn.source.no.system.modules.path: 21, (compiler.misc.source.no.system.modules.path.with.target: 21, 21) +- compiler.err.warnings.and.werror +1 error +1 warning From 8dcea7004f9ad1f3c8ee5e6aeb934ed0518a2a37 Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Tue, 17 Jun 2025 08:59:59 -0500 Subject: [PATCH 4/6] No need for /nodynamiccopyright/ with this test. --- .../tools/javac/lint/LintOptions.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/test/langtools/tools/javac/lint/LintOptions.java b/test/langtools/tools/javac/lint/LintOptions.java index 81b0a0dc3250b..b5825fce72841 100644 --- a/test/langtools/tools/javac/lint/LintOptions.java +++ b/test/langtools/tools/javac/lint/LintOptions.java @@ -1,5 +1,30 @@ /* - * @test /nodynamiccopyright/ + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test * @bug 8359596 * @summary Verify behavior when both "-Xlint:options" and "-Xlint:-options" are given * @compile/fail/ref=LintOptions.out -Werror -XDrawDiagnostics -source 21 -target 21 LintOptions.java From 344501dae55cc5d9af7ef853ee691d533b2f9af5 Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Tue, 17 Jun 2025 12:25:25 -0500 Subject: [PATCH 5/6] Minor naming and Javadoc tweaks. --- .../share/classes/com/sun/tools/javac/code/Lint.java | 2 +- .../share/classes/com/sun/tools/javac/main/JavaCompiler.java | 4 ++-- .../share/classes/com/sun/tools/javac/util/Options.java | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java index e08b4e7b446fc..cd4ee63b591ce 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java @@ -148,7 +148,7 @@ protected Lint(Lint other) { // Process command line options on demand to allow use of root Lint early during startup private void initializeRootIfNeeded() { if (values == null) { - values = options.getLintCategories(Option.XLINT, this::getDefaults); + values = options.getLintCategoriesOf(Option.XLINT, this::getDefaults); suppressedValues = LintCategory.newEmptySet(); } } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java index 5e641d9b5d2fd..0ead16871d995 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java @@ -446,7 +446,7 @@ public JavaCompiler(Context context) { devVerbose = options.isSet("dev"); processPcks = options.isSet("process.packages"); werrorAny = options.isSet(WERROR) || options.isSet(WERROR_CUSTOM, Option.LINT_CUSTOM_ALL); - werrorLint = options.getLintCategories(WERROR, LintCategory::newEmptySet); + werrorLint = options.getLintCategoriesOf(WERROR, LintCategory::newEmptySet); verboseCompilePolicy = options.isSet("verboseCompilePolicy"); @@ -519,7 +519,7 @@ public boolean exists() { */ protected boolean processPcks; - /** Switch: treat any kind of warning (including non-lint warnings) as an error. + /** Switch: treat any kind of warning (lint or non-lint) as an error. */ protected boolean werrorAny; diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java index 072c6ea13cba1..3964157c09ac7 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java @@ -225,7 +225,7 @@ public boolean isExplicitlyDisabled(Option option, LintCategory lc) { * @param defaults populates the default set, or null for an empty default set * @return the specified set of categories */ - public EnumSet getLintCategories(Option option, Supplier> defaults) { + public EnumSet getLintCategoriesOf(Option option, Supplier> defaults) { // Create the initial set EnumSet categories; From 9e788319c5109a86e1bc1ec3417b3301aa1d0153 Mon Sep 17 00:00:00 2001 From: "Archie L. Cobbs" Date: Wed, 18 Jun 2025 16:06:32 -0500 Subject: [PATCH 6/6] Ensure that "-Xlint:none" still works for the affected warnings. The extra checks for "-Xlint:none" are needed now because of JDK-8352612, which changed the behavior of "-Xlint:none" to no longer imply "-nowarn", which allowed the affected warnings to get away with skipping that check. --- .../com/sun/tools/javac/comp/Modules.java | 2 +- .../com/sun/tools/javac/main/Arguments.java | 4 +- .../com/sun/tools/javac/util/Options.java | 41 ++++++++++++++++++- .../tools/javac/lint/LintOptions.java | 2 + 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java index a159793fe327f..0015b73631a99 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java @@ -205,7 +205,7 @@ protected Modules(Context context) { allowAccessIntoSystem = options.isUnset(Option.RELEASE); - lintOptions = !options.isExplicitlyDisabled(Option.XLINT, LintCategory.OPTIONS); + lintOptions = !options.isDisabled(Option.XLINT, LintCategory.OPTIONS); multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH); ClassWriter classWriter = ClassWriter.instance(context); diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java index 9e3a978c3bc9a..b1dfb0378bde5 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/main/Arguments.java @@ -503,7 +503,7 @@ public boolean validate() { } } else { // single-module or legacy mode - boolean lintPaths = !options.isExplicitlyDisabled(Option.XLINT, LintCategory.PATH); + boolean lintPaths = !options.isDisabled(Option.XLINT, LintCategory.PATH); if (lintPaths) { Path outDirParent = outDir.getParent(); if (outDirParent != null && Files.exists(outDirParent.resolve("module-info.class"))) { @@ -576,7 +576,7 @@ public boolean validate() { reportDiag(Errors.SourcepathModulesourcepathConflict); } - boolean lintOptions = !options.isExplicitlyDisabled(Option.XLINT, LintCategory.OPTIONS); + boolean lintOptions = !options.isDisabled(Option.XLINT, LintCategory.OPTIONS); if (lintOptions && source.compareTo(Source.DEFAULT) < 0 && !options.isSet(Option.RELEASE)) { if (fm instanceof BaseFileManager baseFileManager) { if (source.compareTo(Source.JDK8) <= 0) { diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java index c882fb4cf8a20..cbc69feb421ab 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Options.java @@ -171,12 +171,46 @@ public boolean isUnset(Option option, String value) { return !isSet(option, value); } + /** + * Determine if a specific {@link LintCategory} is enabled via a custom + * option flag of the form {@code -Flag}, {@code -Flag:all}, or {@code -Flag:key}. + * + *

+ * Note: It's possible the category was also disabled; this method does not check that. + * + * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) + * @param lc the {@link LintCategory} in question + * @return true if {@code lc} has been enabled + */ + public boolean isEnabled(Option option, LintCategory lc) { + Option custom = option.getCustom(); + return isExplicitlyEnabled(option, lc) || isSet(custom) || isSet(custom, Option.LINT_CUSTOM_ALL); + } + + /** + * Determine if a specific {@link LintCategory} is disabled via a custom + * option flag of the form {@code -Flag:none} or {@code -Flag:-key}. + * + *

+ * Note: It's possible the category was also enabled; this method does not check that. + * + * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) + * @param lc the {@link LintCategory} in question + * @return true if {@code lc} has been disabled + */ + public boolean isDisabled(Option option, LintCategory lc) { + return isExplicitlyDisabled(option, lc) || isSet(option.getCustom(), Option.LINT_CUSTOM_NONE); + } + /** * Determine if a specific {@link LintCategory} is explicitly enabled via a custom * option flag of the form {@code -Flag:key}. * *

- * Note: It's possible the category was also explicitly disabled; this method does not check that. + * Note: This does not check for option flags of the form {@code -Flag} or {@code -Flag:all}. + * + *

+ * Note: It's possible the category was also disabled; this method does not check that. * * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) * @param lc the {@link LintCategory} in question @@ -192,7 +226,10 @@ public boolean isExplicitlyEnabled(Option option, LintCategory lc) { * option flag of the form {@code -Flag:-key}. * *

- * Note: It's possible the category was also explicitly enabled; this method does not check that. + * Note: This does not check for an option flag of the form {@code -Flag:none}. + * + *

+ * Note: It's possible the category was also enabled; this method does not check that. * * @param option the plain (non-custom) version of the option (e.g., {@link Option#XLINT}) * @param lc the {@link LintCategory} in question diff --git a/test/langtools/tools/javac/lint/LintOptions.java b/test/langtools/tools/javac/lint/LintOptions.java index b5825fce72841..10def72a31a5e 100644 --- a/test/langtools/tools/javac/lint/LintOptions.java +++ b/test/langtools/tools/javac/lint/LintOptions.java @@ -31,6 +31,8 @@ * @compile/fail/ref=LintOptions.out -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options LintOptions.java * @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:-options LintOptions.java * @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options -Xlint:-options LintOptions.java + * @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:none LintOptions.java + * @compile -Werror -XDrawDiagnostics -source 21 -target 21 -Xlint:options -Xlint:none LintOptions.java */ class LintOptions { }