-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8362885: A more formal way to mark javac's Flags that belong to a specific Symbol type only #26452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lahodaj
wants to merge
5
commits into
openjdk:master
Choose a base branch
from
lahodaj:JDK-8362885
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+473
−133
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9c7007f
8362885: A more formal way to mark javac's Flags that belong to a spe…
lahodaj 0ab2ba4
Fixing tests.
lahodaj 5bc1d6d
Reflecting review feedback:
lahodaj b0fbd91
Review feedback: marking ACC flags with the @Use annotation, forces s…
lahodaj e00209f
Fixing toString value for flags that have ACC_ overloaded flags.
lahodaj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
make/langtools/tools/flagsgenerator/FlagsGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package flagsgenerator; | ||
|
||
import com.sun.source.tree.CompilationUnitTree; | ||
import com.sun.source.util.JavacTask; | ||
import com.sun.source.util.TreePath; | ||
import com.sun.source.util.Trees; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Set; | ||
import java.util.TreeMap; | ||
import java.util.stream.Collectors; | ||
import javax.lang.model.element.AnnotationMirror; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.element.VariableElement; | ||
import javax.lang.model.util.ElementFilter; | ||
import javax.tools.ToolProvider; | ||
|
||
public class FlagsGenerator { | ||
public static void main(String... args) throws IOException { | ||
var compiler = ToolProvider.getSystemJavaCompiler(); | ||
|
||
try (var fm = compiler.getStandardFileManager(null, null, null)) { | ||
JavacTask task = (JavacTask) compiler.getTask(null, null, d -> {}, null, null, fm.getJavaFileObjects(args[0])); | ||
Trees trees = Trees.instance(task); | ||
CompilationUnitTree cut = task.parse().iterator().next(); | ||
|
||
task.analyze(); | ||
|
||
TypeElement clazz = (TypeElement) trees.getElement(new TreePath(new TreePath(cut), cut.getTypeDecls().get(0))); | ||
Map<Integer, List<String>> flag2Names = new TreeMap<>(); | ||
Map<FlagTarget, Map<Integer, List<String>>> target2FlagBit2Fields = new HashMap<>(); | ||
Map<String, String> customToString = new HashMap<>(); | ||
Set<String> noToString = new HashSet<>(); | ||
|
||
for (VariableElement field : ElementFilter.fieldsIn(clazz.getEnclosedElements())) { | ||
String flagName = field.getSimpleName().toString(); | ||
for (AnnotationMirror am : field.getAnnotationMirrors()) { | ||
switch (am.getAnnotationType().toString()) { | ||
case "com.sun.tools.javac.code.Flags.Use" -> { | ||
long flagValue = ((Number) field.getConstantValue()).longValue(); | ||
int flagBit = 63 - Long.numberOfLeadingZeros(flagValue); | ||
|
||
flag2Names.computeIfAbsent(flagBit, _ -> new ArrayList<>()) | ||
.add(flagName); | ||
|
||
List<?> originalTargets = (List<?>) valueOfValueAttribute(am); | ||
originalTargets.stream() | ||
.map(value -> FlagTarget.valueOf(value.toString())) | ||
.forEach(target -> target2FlagBit2Fields.computeIfAbsent(target, _ -> new HashMap<>()) | ||
.computeIfAbsent(flagBit, _ -> new ArrayList<>()) | ||
.add(flagName)); | ||
} | ||
case "com.sun.tools.javac.code.Flags.CustomToStringValue" -> { | ||
customToString.put(flagName, (String) valueOfValueAttribute(am)); | ||
} | ||
case "com.sun.tools.javac.code.Flags.NoToStringValue" -> { | ||
noToString.add(flagName); | ||
} | ||
} | ||
} | ||
} | ||
|
||
//verify there are no flag overlaps: | ||
for (Entry<FlagTarget, Map<Integer, List<String>>> targetAndFlag : target2FlagBit2Fields.entrySet()) { | ||
for (Entry<Integer, List<String>> flagAndFields : targetAndFlag.getValue().entrySet()) { | ||
if (flagAndFields.getValue().size() > 1) { | ||
throw new AssertionError("duplicate flag for target: " + targetAndFlag.getKey() + | ||
", flag: " + flagAndFields.getKey() + | ||
", flags fields: " + flagAndFields.getValue()); | ||
} | ||
} | ||
} | ||
|
||
String masks = Arrays.stream(FlagTarget.values()) | ||
.map(target -> " public static final long MASK_" + target.name() + "_FLAGS = " + getMask(target2FlagBit2Fields, target) + ";") | ||
.collect(Collectors.joining("\n")); | ||
|
||
try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(Paths.get(args[1])))) { | ||
out.println(""" | ||
package com.sun.tools.javac.code; | ||
import com.sun.tools.javac.util.Assert; | ||
public enum FlagsEnum { | ||
"""); | ||
for (Entry<Integer, List<String>> e : flag2Names.entrySet()) { | ||
String constantName = e.getValue().stream().collect(Collectors.joining("_OR_")); | ||
String toString = e.getValue() | ||
.stream() | ||
.filter(n -> !noToString.contains(n)) | ||
.map(n -> customToString.getOrDefault(n, n.toLowerCase(Locale.US))) | ||
.collect(Collectors.joining(" or ")); | ||
out.println(" " + constantName + "(1L<<" + e.getKey() + ", \"" + toString + "\"),"); | ||
} | ||
out.println(""" | ||
; | ||
${masks} | ||
private final long value; | ||
private final String toString; | ||
private FlagsEnum(long value, String toString) { | ||
this.value = value; | ||
this.toString = toString; | ||
} | ||
public long value() { | ||
return value; | ||
} | ||
public String toString() { | ||
return toString; | ||
} | ||
public static void assertNoUnexpectedFlags(long flags, long mask) { | ||
Assert.check((flags & ~mask) == 0, | ||
() -> "Unexpected flags: 0x" + Long.toHexString(flags & ~mask) + "L (" + | ||
Flags.asFlagSet(flags & ~mask) + ")"); | ||
} | ||
} | ||
""".replace("${masks}", masks)); | ||
} | ||
} | ||
} | ||
|
||
private static String getMask(Map<FlagTarget, Map<Integer, List<String>>> target2FlagBit2Fields, | ||
FlagTarget target) { | ||
long mask = target2FlagBit2Fields.get(target) | ||
.keySet() | ||
.stream() | ||
.mapToLong(bit -> 1L << bit) | ||
.reduce(0, (l, r) -> l | r); | ||
|
||
return "0x" + Long.toHexString(mask) + "L"; | ||
} | ||
|
||
private static Object valueOfValueAttribute(AnnotationMirror am) { | ||
return am.getElementValues() | ||
.values() | ||
.iterator() | ||
.next() | ||
.getValue(); | ||
} | ||
|
||
public enum FlagTarget { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just flyby comment: symbol kind is an enum, so in principle we could use constants such as MTH, VAR, TYP as targets, if we wanted to connect the decls more with the actual code. But I like your approach as well (as symbol kind has also a lot of other more dubious stuff/noise) |
||
BLOCK, | ||
CLASS, | ||
METHOD, | ||
MODULE, | ||
PACKAGE, | ||
TYPE_VAR, | ||
VARIABLE; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need a license header for generated sources?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CompilerProperties
and other files that are generated using similar means don't have license headers, so I assume we don't need that.