Skip to content

Commit dac6944

Browse files
Minor code cleanups.
1 parent c0ab369 commit dac6944

15 files changed

+49
-67
lines changed

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGenerator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,6 @@ protected NativeLibraries setupNativeLibraries(HostedProviders providers, CEnumC
14171417
}
14181418
}
14191419

1420-
@SuppressWarnings("deprecation")
14211420
protected void registerEntryPoints(Map<Method, CEntryPointData> entryPoints) {
14221421
for (Method m : loader.findAnnotatedMethods(CEntryPoint.class)) {
14231422
if (!Modifier.isStatic(m.getModifiers())) {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/CAnnotationProcessor.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public CAnnotationProcessor(NativeLibraries nativeLibs, NativeCodeContext codeCt
8787
public NativeCodeInfo process(CAnnotationProcessorCache cache) {
8888
InfoTreeBuilder constructor = new InfoTreeBuilder(nativeLibs, codeCtx);
8989
codeInfo = constructor.construct();
90-
if (nativeLibs.getErrors().size() > 0) {
90+
if (!nativeLibs.getErrors().isEmpty()) {
9191
return codeInfo;
9292
}
9393

@@ -99,7 +99,7 @@ public NativeCodeInfo process(CAnnotationProcessorCache cache) {
9999
*/
100100
writer = new QueryCodeWriter(queryCodeDirectory);
101101
Path queryFile = writer.write(codeInfo);
102-
if (nativeLibs.getErrors().size() > 0) {
102+
if (!nativeLibs.getErrors().isEmpty()) {
103103
return codeInfo;
104104
}
105105
assert Files.exists(queryFile);
@@ -110,12 +110,12 @@ public NativeCodeInfo process(CAnnotationProcessorCache cache) {
110110
}
111111
Path binary = compileQueryCode(queryFile);
112112
DeadlockWatchdog.singleton().recordActivity();
113-
if (nativeLibs.getErrors().size() > 0) {
113+
if (!nativeLibs.getErrors().isEmpty()) {
114114
return codeInfo;
115115
}
116116

117117
makeQuery(cache, binary.toString());
118-
if (nativeLibs.getErrors().size() > 0) {
118+
if (!nativeLibs.getErrors().isEmpty()) {
119119
return codeInfo;
120120
}
121121
}
@@ -159,8 +159,7 @@ private Path compileQueryCode(Path queryFile) {
159159
}
160160
String fileName = fileNamePath.toString();
161161
Path binary = tempDirectory.resolve(compilerInvoker.asExecutableName(fileName.substring(0, fileName.lastIndexOf("."))));
162-
ArrayList<String> options = new ArrayList<>();
163-
options.addAll(codeCtx.getDirectives().getOptions());
162+
ArrayList<String> options = new ArrayList<>(codeCtx.getDirectives().getOptions());
164163
if (Platform.includedIn(Platform.LINUX.class)) {
165164
options.addAll(HostedLibCBase.singleton().getAdditionalQueryCodeCompilerOptions());
166165
}
@@ -187,7 +186,7 @@ protected void reportCompilerError(ProcessBuilder current, Path queryFile, Strin
187186
elements.add(writer.getElementForLineNumber(lineNumber));
188187
elements.add("C file contents around line " + lineNumber + ":");
189188
for (int i = Math.max(lineNumber - 1, 1); i <= lineNumber + 1; i++) {
190-
elements.add(queryFile.toString() + ":" + i + ": " + writer.getLine(i));
189+
elements.add(queryFile + ":" + i + ": " + writer.getLine(i));
191190
}
192191
} catch (NumberFormatException ex) {
193192
/* Ignore if not a valid number. */

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/CAnnotationProcessorCache.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,13 @@ public static class Options {
7777
@Override
7878
public Boolean getValueOrDefault(UnmodifiableEconomicMap<OptionKey<?>, Object> values) {
7979
if (!values.containsKey(this)) {
80-
// If user hasn't specified this option, we should determine optimal default
81-
// value.
82-
if (!ExitAfterQueryCodeGeneration.getValue() && !ImageSingletons.lookup(Platform.class).getArchitecture().equals(SubstrateUtil.getArchitectureName())) {
83-
// If query code generation isn't explicitly requested, and we are running
84-
// cross-arch build, CAP cache should be required (since we cannot run query
85-
// code).
86-
return true;
87-
} else {
88-
return false;
89-
}
80+
/*
81+
* If the user hasn't specified this option, we should determine an optimal
82+
* value automatically. If query code generation isn't explicitly requested, and
83+
* we are running cross-arch build, CAP cache is required (since we cannot run
84+
* any query code).
85+
*/
86+
return !ExitAfterQueryCodeGeneration.getValue() && !ImageSingletons.lookup(Platform.class).getArchitecture().equals(SubstrateUtil.getArchitectureName());
9087
}
9188
return (Boolean) values.get(this);
9289
}
@@ -118,7 +115,6 @@ public Boolean getValue(OptionValues values) {
118115
}
119116

120117
private File cache;
121-
private File query;
122118

123119
public CAnnotationProcessorCache() {
124120
if ((Options.UseCAPCache.getValue() || Options.NewCAPCache.getValue())) {
@@ -145,7 +141,7 @@ public CAnnotationProcessorCache() {
145141

146142
if (Options.QueryCodeDir.hasBeenSet()) {
147143
Path queryPath = FileSystems.getDefault().getPath(Options.QueryCodeDir.getValue()).toAbsolutePath();
148-
query = queryPath.toFile();
144+
File query = queryPath.toFile();
149145
if (!query.exists()) {
150146
try {
151147
query = Files.createDirectories(queryPath).toFile();
@@ -188,16 +184,15 @@ public void put(NativeCodeInfo nativeCodeInfo, List<String> lines) {
188184
writer.write(line);
189185
writer.write(Character.LINE_SEPARATOR);
190186
}
191-
return;
192187
} catch (IOException e) {
193-
throw shouldNotReachHere("Invalid CAnnotation Processor Cache item:" + cachedItem.toString());
188+
throw shouldNotReachHere("Invalid CAnnotation Processor Cache item:" + cachedItem);
194189
}
195190
}
196191

197192
private void clearCache() {
198193
try {
199194
final Path cachePath = cache.toPath();
200-
Files.walkFileTree(cachePath, new SimpleFileVisitor<Path>() {
195+
Files.walkFileTree(cachePath, new SimpleFileVisitor<>() {
201196
@Override
202197
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
203198
assert file.toString().endsWith(FILE_EXTENSION);

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/CGlobalDataFeature.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
198198
AbstractMergeNode merge = b.append(new MergeNode());
199199
merge.addForwardEnd(thenEnd);
200200
merge.addForwardEnd(elseEnd);
201-
ValuePhiNode phiNode = new ValuePhiNode(StampFactory.pointer(), merge, new ValueNode[]{address, readValue});
201+
ValuePhiNode phiNode = new ValuePhiNode(StampFactory.pointer(), merge, address, readValue);
202202
phiNode.inferStamp();
203203
b.push(targetMethod.getSignature().getReturnKind(), b.getGraph().addOrUnique(phiNode));
204204
b.setStateAfter(merge);
@@ -296,7 +296,8 @@ private void layout() {
296296
info.assignOffset(currentOffset);
297297

298298
int nextOffset = currentOffset + info.getSize();
299-
return (nextOffset + (wordSize - 1)) & ~(wordSize - 1); // align
299+
int alignmentMask = -wordSize;
300+
return (nextOffset + (wordSize - 1)) & alignmentMask;
300301
}, Integer::sum);
301302
assert isLaidOut();
302303
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/CIsolateDataFeature.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@
2828
import java.util.Comparator;
2929
import java.util.Map;
3030

31-
import com.oracle.svm.core.c.CIsolateDataStorage;
31+
import org.graalvm.word.UnsignedWord;
32+
3233
import com.oracle.svm.core.c.CIsolateData;
34+
import com.oracle.svm.core.c.CIsolateDataStorage;
3335
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
3436
import com.oracle.svm.core.feature.InternalFeature;
3537
import com.oracle.svm.core.util.ConcurrentIdentityHashMap;
3638
import com.oracle.svm.core.util.UnsignedUtils;
3739
import com.oracle.svm.core.util.VMError;
3840

3941
import jdk.graal.compiler.word.Word;
40-
import org.graalvm.word.UnsignedWord;
4142

4243
@AutomaticallyRegisteredFeature
4344
public class CIsolateDataFeature implements InternalFeature {
@@ -50,8 +51,7 @@ public void duringSetup(DuringSetupAccess access) {
5051
}
5152

5253
private Object replaceObject(Object obj) {
53-
if (obj instanceof CIsolateData<?>) {
54-
CIsolateData<?> entry = (CIsolateData<?>) obj;
54+
if (obj instanceof CIsolateData<?> entry) {
5555
usedEntries.compute(entry.getName(), (key, old) -> {
5656
VMError.guarantee(old == null || old == entry, "The isolate data section already contains an entry for %s", key);
5757
return entry;

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/NativeLibraries.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ public List<CInterfaceError> getErrors() {
397397
}
398398

399399
public void reportErrors() {
400-
if (errors.size() > 0) {
400+
if (!errors.isEmpty()) {
401401
throw UserError.abort(errors.stream().map(CInterfaceError::getMessage).collect(Collectors.toList()));
402402
}
403403
}
@@ -576,7 +576,7 @@ private Class<? extends CContext.Directives> getDirectives(ResolvedJavaType type
576576
}
577577

578578
public void finish() {
579-
libraryPaths.addAll(SubstrateOptions.CLibraryPath.getValue().values().stream().map(Path::toString).collect(Collectors.toList()));
579+
libraryPaths.addAll(SubstrateOptions.CLibraryPath.getValue().values().stream().map(Path::toString).toList());
580580
for (NativeCodeContext context : compilationUnitToContext.values()) {
581581
if (context.isInConfiguration()) {
582582
libraries.addAll(context.getDirectives().getLibraries());
@@ -642,9 +642,9 @@ public ConstantReflectionProvider getConstantReflection() {
642642
return constantReflection;
643643
}
644644

645-
public boolean processAnnotated() {
645+
public void processAnnotated() {
646646
if (annotated.isEmpty()) {
647-
return false;
647+
return;
648648
}
649649
for (CLibrary lib : annotated) {
650650
if (lib.requireStatic()) {
@@ -654,7 +654,6 @@ public boolean processAnnotated() {
654654
}
655655
}
656656
annotated.clear();
657-
return true;
658657
}
659658

660659
public List<String> getJniStaticLibraries() {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/OffsetOfSupportImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public int offsetOf(Class<? extends PointerBase> clazz, String fieldName) {
4949
VMError.guarantee(typeInfo instanceof StructInfo, "Class parameter %s of call to %s is not an annotated C struct", type, SizeOf.class.getSimpleName());
5050
StructInfo structInfo = (StructInfo) typeInfo;
5151
for (ElementInfo element : structInfo.getChildren()) {
52-
if (element instanceof StructFieldInfo) {
53-
StructFieldInfo field = (StructFieldInfo) element;
52+
if (element instanceof StructFieldInfo field) {
5453
if (field.getName().equals(fieldName)) {
5554
return field.getOffsetInfo().getProperty();
5655
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/codegen/CSourceCodeWriter.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,15 @@
6363
import jdk.vm.ci.meta.ResolvedJavaType;
6464

6565
public class CSourceCodeWriter {
66-
6766
private static final String INDENT4 = " ";
68-
public static final String C_SOURCE_FILE_EXTENSION = ".c";
69-
70-
private final List<String> lines;
71-
private final StringBuilder currentLine;
7267

68+
private final List<String> lines = new ArrayList<>();
69+
private final StringBuilder currentLine = new StringBuilder(100);
70+
private final Path tempDirectory;
7371
private int indentLevel = 0;
74-
protected final Path tempDirectory;
7572

7673
public CSourceCodeWriter(Path tempDirectory) {
7774
this.tempDirectory = tempDirectory;
78-
this.lines = new ArrayList<>();
79-
this.currentLine = new StringBuilder(100);
8075
}
8176

8277
public void writeCStandardHeaders() {
@@ -262,8 +257,7 @@ private static boolean isUnsigned(AnnotatedType type) {
262257

263258
private static boolean isFunctionPointer(MetaAccessProvider metaAccess, ResolvedJavaType type) {
264259
boolean functionPointer = metaAccess.lookupJavaType(CFunctionPointer.class).isAssignableFrom(type);
265-
return functionPointer &&
266-
Arrays.stream(type.getDeclaredMethods(false)).anyMatch(v -> v.getDeclaredAnnotation(InvokeCFunctionPointer.class) != null);
260+
return functionPointer && Arrays.stream(type.getDeclaredMethods(false)).anyMatch(v -> v.getDeclaredAnnotation(InvokeCFunctionPointer.class) != null);
267261
}
268262

269263
/**

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/codegen/QueryCodeWriter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import com.oracle.svm.hosted.c.query.QueryResultFormat;
5555

5656
public class QueryCodeWriter extends InfoTreeVisitor {
57-
57+
private static final String C_SOURCE_FILE_EXTENSION = ".c";
5858
private static final String formatFloat = "%.15e";
5959
private static final String formatString = QueryResultFormat.STRING_MARKER + "%s" + QueryResultFormat.STRING_MARKER;
6060

@@ -91,8 +91,7 @@ private static String int64(boolean isWindows, boolean unsigned) {
9191
public Path write(NativeCodeInfo nativeCodeInfo) {
9292
nativeCodeInfo.accept(this);
9393

94-
String srcFileExtension = CSourceCodeWriter.C_SOURCE_FILE_EXTENSION;
95-
String sourceFileName = nativeCodeInfo.getName().replaceAll("\\W", "_") + srcFileExtension;
94+
String sourceFileName = nativeCodeInfo.getName().replaceAll("\\W", "_") + C_SOURCE_FILE_EXTENSION;
9695
return writer.writeFile(sourceFileName);
9796
}
9897

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/function/CEntryPointSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
7777
@Override
7878
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode isolate, ValueNode ensureJavaThreadNode) {
7979
if (!ensureJavaThreadNode.isConstant()) {
80-
b.bailout("Parameter ensureJavaThread of enterAttachThread must be a compile time constant");
80+
throw b.bailout("Parameter ensureJavaThread of enterAttachThread must be a compile time constant");
8181
}
8282
b.addPush(JavaKind.Int, CEntryPointEnterNode.attachThread(isolate, false, ensureJavaThreadNode.asJavaConstant().asInt() != 0));
8383
return true;
@@ -87,7 +87,7 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
8787
@Override
8888
public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode isolate, ValueNode startedByIsolate, ValueNode ensureJavaThread) {
8989
if (!startedByIsolate.isConstant() || !ensureJavaThread.isConstant()) {
90-
b.bailout("Parameters ensureJavaThread and startedByIsolate of enterAttachThread must be a compile time constant");
90+
throw b.bailout("Parameters ensureJavaThread and startedByIsolate of enterAttachThread must be a compile time constant");
9191
}
9292
b.addPush(JavaKind.Int, CEntryPointEnterNode.attachThread(isolate, startedByIsolate.asJavaConstant().asInt() != 0, ensureJavaThread.asJavaConstant().asInt() != 0));
9393
return true;

0 commit comments

Comments
 (0)