Skip to content

Commit 25ed36f

Browse files
committed
8359493: Refactor how aggregated mandatory warnings are handled in the compiler
8350514: Refactor MandatoryWarningHandler to support dynamic verbosity Reviewed-by: mcimadamore
1 parent a2315dd commit 25ed36f

File tree

16 files changed

+271
-248
lines changed

16 files changed

+271
-248
lines changed

make/langtools/tools/propertiesparser/gen/ClassGenerator.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -47,6 +47,7 @@
4747
import java.util.Arrays;
4848
import java.util.Collections;
4949
import java.util.List;
50+
import java.util.Locale;
5051
import java.util.Map;
5152
import java.util.Map.Entry;
5253
import java.util.Properties;
@@ -93,7 +94,9 @@ enum StubKind {
9394
FACTORY_FIELD_LINT("factory.decl.field.lint"),
9495
WILDCARDS_EXTENDS("wildcards.extends"),
9596
SUPPRESS_WARNINGS("suppress.warnings"),
96-
LINT_CATEGORY("lint.category");
97+
LINT_CATEGORY("lint.category"),
98+
DIAGNOSTIC_FLAGS_EMPTY("diagnostic.flags.empty"),
99+
DIAGNOSTIC_FLAGS_NON_EMPTY("diagnostic.flags.non-empty");
97100

98101
/** stub key (as it appears in the property file) */
99102
String key;
@@ -259,17 +262,30 @@ List<String> generateFactoryMethodsAndFields(FactoryKind k, String key, Message
259262
.map(MessageLine::lintCategory)
260263
.findFirst().orElse(null);
261264
//System.out.println("category for " + key + " = " + lintCategory);
265+
String diagnosticFlags = lines.stream()
266+
.filter(MessageLine::isDiagnosticFlags)
267+
.map(MessageLine::diagnosticFlags)
268+
.flatMap(Stream::of)
269+
.map(s -> s.replace('-', '_'))
270+
.map(s -> s.toUpperCase(Locale.ROOT))
271+
.collect(Collectors.joining(", "));
262272
String factoryName = factoryName(key);
263273
if (msgInfo.getTypes().isEmpty()) {
264274
//generate field
265275
String factoryField;
266276
if (lintCategory == null) {
267277
factoryField = StubKind.FACTORY_FIELD.format(k.keyClazz, factoryName,
278+
diagnosticFlags.isEmpty() ?
279+
StubKind.DIAGNOSTIC_FLAGS_EMPTY.format() :
280+
StubKind.DIAGNOSTIC_FLAGS_NON_EMPTY.format(diagnosticFlags),
268281
"\"" + keyParts[0] + "\"",
269282
"\"" + Stream.of(keyParts).skip(2).collect(Collectors.joining(".")) + "\"",
270283
javadoc);
271284
} else {
272285
factoryField = StubKind.FACTORY_FIELD_LINT.format(k.keyClazz, factoryName,
286+
diagnosticFlags.isEmpty() ?
287+
StubKind.DIAGNOSTIC_FLAGS_EMPTY.format() :
288+
StubKind.DIAGNOSTIC_FLAGS_NON_EMPTY.format(diagnosticFlags),
273289
StubKind.LINT_CATEGORY.format("\"" + lintCategory + "\""),
274290
"\"" + keyParts[0] + "\"",
275291
"\"" + Stream.of(keyParts).skip(2).collect(Collectors.joining(".")) + "\"",
@@ -287,11 +303,17 @@ List<String> generateFactoryMethodsAndFields(FactoryKind k, String key, Message
287303
String methodBody;
288304
if (lintCategory == null) {
289305
methodBody = StubKind.FACTORY_METHOD_BODY.format(k.keyClazz,
306+
diagnosticFlags.isEmpty() ?
307+
StubKind.DIAGNOSTIC_FLAGS_EMPTY.format() :
308+
StubKind.DIAGNOSTIC_FLAGS_NON_EMPTY.format(diagnosticFlags),
290309
"\"" + keyParts[0] + "\"",
291310
"\"" + Stream.of(keyParts).skip(2).collect(Collectors.joining(".")) + "\"",
292311
argNames.stream().collect(Collectors.joining(", ")));
293312
} else {
294313
methodBody = StubKind.FACTORY_METHOD_BODY_LINT.format(k.keyClazz,
314+
diagnosticFlags.isEmpty() ?
315+
StubKind.DIAGNOSTIC_FLAGS_EMPTY.format() :
316+
StubKind.DIAGNOSTIC_FLAGS_NON_EMPTY.format(diagnosticFlags),
295317
StubKind.LINT_CATEGORY.format("\"" + lintCategory + "\""),
296318
"\"" + keyParts[0] + "\"",
297319
"\"" + Stream.of(keyParts).skip(2).collect(Collectors.joining(".")) + "\"",

make/langtools/tools/propertiesparser/parser/Message.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
* A message within the message file.
3333
* A message is a series of lines containing a "name=value" property,
3434
* optionally preceded by a comment describing the use of placeholders
35-
* such as {0}, {1}, etc within the property value.
35+
* such as {0}, {1}, etc within the property value, a lint category,
36+
* and/or a list of diagnostic flags.
3637
*/
3738
public final class Message {
3839
final MessageLine firstLine;
@@ -49,7 +50,7 @@ public final class Message {
4950
public MessageInfo getMessageInfo() {
5051
if (messageInfo == null) {
5152
MessageLine l = firstLine.prev;
52-
if (l != null && l.isLint()) {
53+
while (l != null && (l.isLint() || l.isDiagnosticFlags())) {
5354
l = l.prev;
5455
}
5556
if (l != null && l.isInfo())
@@ -74,7 +75,7 @@ public List<MessageLine> getLines(boolean includeAllPrecedingComments) {
7475
while (l.text.isEmpty())
7576
l = l.next;
7677
} else {
77-
if (l.prev != null && (l.prev.isInfo() || l.prev.isLint()))
78+
while (l.prev != null && (l.prev.isInfo() || l.prev.isLint() || l.prev.isDiagnosticFlags()))
7879
l = l.prev;
7980
}
8081

make/langtools/tools/propertiesparser/parser/MessageLine.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
2525

2626
package propertiesparser.parser;
2727

28+
import java.util.List;
2829
import java.util.regex.Matcher;
2930
import java.util.regex.Pattern;
3031

@@ -39,6 +40,7 @@ public class MessageLine {
3940
static final Pattern infoPattern = Pattern.compile(String.format("# ([0-9]+: %s, )*[0-9]+: %s",
4041
typePattern.pattern(), typePattern.pattern()));
4142
static final Pattern lintPattern = Pattern.compile("# lint: ([a-z\\-]+)");
43+
static final Pattern diagnosticFlagsPattern = Pattern.compile("# flags: ([a-z\\-]+(, ([a-z\\-]+))*)");
4244

4345
public String text;
4446
MessageLine prev;
@@ -69,6 +71,19 @@ public String lintCategory() {
6971
}
7072
}
7173

74+
public boolean isDiagnosticFlags() {
75+
return diagnosticFlagsPattern.matcher(text).matches();
76+
}
77+
78+
public String[] diagnosticFlags() {
79+
Matcher matcher = diagnosticFlagsPattern.matcher(text);
80+
if (matcher.matches()) {
81+
return matcher.group(1).split(", ", -1);
82+
} else {
83+
return null;
84+
}
85+
}
86+
7287
boolean hasContinuation() {
7388
return (next != null) && text.endsWith("\\");
7489
}

make/langtools/tools/propertiesparser/resources/templates.properties

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
2+
# Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
#
55
# This code is free software; you can redistribute it and/or modify it
@@ -27,13 +27,18 @@ toplevel.decl=\
2727
package {0};\n\
2828
\n\
2929
{1}\n\
30+
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;\n\
3031
import com.sun.tools.javac.util.JCDiagnostic.Error;\n\
3132
import com.sun.tools.javac.util.JCDiagnostic.Warning;\n\
3233
import com.sun.tools.javac.util.JCDiagnostic.LintWarning;\n\
3334
import com.sun.tools.javac.util.JCDiagnostic.Note;\n\
3435
import com.sun.tools.javac.util.JCDiagnostic.Fragment;\n\
3536
import com.sun.tools.javac.code.Lint.LintCategory;\n\
3637
\n\
38+
import java.util.EnumSet;\n\
39+
\n\
40+
import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;\n\
41+
\n\
3742
public class {2} '{'\n\
3843
{3}\n\
3944
'}'\n
@@ -58,22 +63,22 @@ factory.decl.method.arg=\
5863
arg{0}
5964

6065
factory.decl.method.body=\
61-
return new {0}({1}, {2}, {3});
66+
return new {0}({1}, {2}, {3}, {4});
6267

6368
factory.decl.method.body.lint=\
64-
return new {0}({1}, {2}, {3}, {4});
69+
return new {0}({1}, {2}, {3}, {4}, {5});
6570

6671
factory.decl.field=\
6772
/**\n\
6873
' '* {4}\n\
6974
' '*/\n\
70-
public static final {0} {1} = new {0}({2}, {3});
75+
public static final {0} {1} = new {0}({2}, {3}, {4});
7176

7277
factory.decl.field.lint=\
7378
/**\n\
7479
' '* {5}\n\
7580
' '*/\n\
76-
public static final {0} {1} = new {0}({2}, {3}, {4});
81+
public static final {0} {1} = new {0}({2}, {3}, {4}, {5});
7782

7883
wildcards.extends=\
7984
{0}<? extends {1}>
@@ -84,3 +89,9 @@ suppress.warnings=\
8489
lint.category=\
8590
LintCategory.get({0}).get()
8691

92+
diagnostic.flags.empty=\
93+
EnumSet.noneOf(DiagnosticFlag.class)
94+
95+
diagnostic.flags.non-empty=\
96+
EnumSet.of({0})
97+

src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,6 @@ void clear() {
272272
((ReusableJavaCompiler)ReusableJavaCompiler.instance(this)).clear();
273273
Types.instance(this).newRound();
274274
Check.instance(this).newRound();
275-
Check.instance(this).clear(); //clear mandatory warning handlers
276-
Preview.instance(this).clear(); //clear mandatory warning handlers
277275
Modules.instance(this).newRound();
278276
Annotate.instance(this).newRound();
279277
CompileStates.instance(this).clear();

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
4141
import com.sun.tools.javac.util.JCDiagnostic.Warning;
4242
import com.sun.tools.javac.util.Log;
43-
import com.sun.tools.javac.util.MandatoryWarningHandler;
4443
import com.sun.tools.javac.util.Names;
4544
import com.sun.tools.javac.util.Options;
4645

@@ -71,9 +70,6 @@ public class Preview {
7170
/** flag: is the "preview" lint category enabled? */
7271
private final boolean verbose;
7372

74-
/** the diag handler to manage preview feature usage diagnostics */
75-
private final MandatoryWarningHandler previewHandler;
76-
7773
/** test flag: should all features be considered as preview features? */
7874
private final boolean forcePreview;
7975

@@ -105,7 +101,6 @@ protected Preview(Context context) {
105101
log = Log.instance(context);
106102
source = Source.instance(context);
107103
verbose = Lint.instance(context).isEnabled(LintCategory.PREVIEW);
108-
previewHandler = new MandatoryWarningHandler(log, source, verbose, true, LintCategory.PREVIEW);
109104
forcePreview = options.isSet("forcePreview");
110105
majorVersionToSource = initMajorVersionToSourceMap();
111106
}
@@ -176,7 +171,8 @@ public void warnPreview(DiagnosticPosition pos, Feature feature) {
176171
Assert.check(isEnabled());
177172
Assert.check(isPreview(feature));
178173
markUsesPreview(pos);
179-
previewHandler.report(pos, feature.isPlural() ?
174+
log.mandatoryWarning(pos,
175+
feature.isPlural() ?
180176
LintWarnings.PreviewFeatureUsePlural(feature.nameFragment()) :
181177
LintWarnings.PreviewFeatureUse(feature.nameFragment()));
182178
}
@@ -203,10 +199,6 @@ public void markUsesPreview(DiagnosticPosition pos) {
203199
sourcesWithPreviewFeatures.add(log.currentSourceFile());
204200
}
205201

206-
public void reportPreviewWarning(DiagnosticPosition pos, LintWarning warnKey) {
207-
previewHandler.report(pos, warnKey);
208-
}
209-
210202
public boolean usesPreview(JavaFileObject file) {
211203
return sourcesWithPreviewFeatures.contains(file);
212204
}
@@ -269,25 +261,13 @@ public boolean declaredUsingPreviewFeature(Symbol sym) {
269261
return false;
270262
}
271263

272-
/**
273-
* Report any deferred diagnostics.
274-
*/
275-
public void reportDeferredDiagnostics() {
276-
previewHandler.reportDeferredDiagnostic();
277-
}
278-
279-
public void clear() {
280-
previewHandler.clear();
281-
}
282-
283264
public void checkSourceLevel(DiagnosticPosition pos, Feature feature) {
284265
if (isPreview(feature) && !isEnabled()) {
285266
//preview feature without --preview flag, error
286-
log.error(JCDiagnostic.DiagnosticFlag.SOURCE_LEVEL, pos, disabledError(feature));
267+
log.error(pos, disabledError(feature));
287268
} else {
288269
if (!feature.allowedInSource(source)) {
289-
log.error(JCDiagnostic.DiagnosticFlag.SOURCE_LEVEL, pos,
290-
feature.error(source.name));
270+
log.error(pos, feature.error(source.name));
291271
}
292272
if (isEnabled() && isPreview(feature)) {
293273
warnPreview(pos, feature);

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
import static com.sun.tools.javac.code.TypeTag.*;
8080
import static com.sun.tools.javac.code.TypeTag.WILDCARD;
8181
import static com.sun.tools.javac.tree.JCTree.Tag.*;
82-
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
8382

8483
/** This is the main context-dependent analysis phase in GJC. It
8584
* encompasses name resolution, type checking and constant folding as
@@ -4146,8 +4145,7 @@ public void visitTypeTest(JCInstanceOf tree) {
41464145
!exprtype.isErroneous() && !clazztype.isErroneous() &&
41474146
tree.pattern.getTag() != RECORDPATTERN) {
41484147
if (!allowUnconditionalPatternsInstanceOf) {
4149-
log.error(DiagnosticFlag.SOURCE_LEVEL, tree.pos(),
4150-
Feature.UNCONDITIONAL_PATTERN_IN_INSTANCEOF.error(this.sourceName));
4148+
log.error(tree.pos(), Feature.UNCONDITIONAL_PATTERN_IN_INSTANCEOF.error(this.sourceName));
41514149
}
41524150
}
41534151
typeTree = TreeInfo.primaryPatternTypeTree((JCPattern) tree.pattern);
@@ -4167,8 +4165,7 @@ public void visitTypeTest(JCInstanceOf tree) {
41674165
if (allowReifiableTypesInInstanceof) {
41684166
valid = checkCastablePattern(tree.expr.pos(), exprtype, clazztype);
41694167
} else {
4170-
log.error(DiagnosticFlag.SOURCE_LEVEL, tree.pos(),
4171-
Feature.REIFIABLE_TYPES_INSTANCEOF.error(this.sourceName));
4168+
log.error(tree.pos(), Feature.REIFIABLE_TYPES_INSTANCEOF.error(this.sourceName));
41724169
allowReifiableTypesInInstanceof = true;
41734170
}
41744171
if (!valid) {

0 commit comments

Comments
 (0)