diff --git a/java-frontend/pom.xml b/java-frontend/pom.xml index 82c006418f7..cb7f29b6ea8 100644 --- a/java-frontend/pom.xml +++ b/java-frontend/pom.xml @@ -158,13 +158,6 @@ src/main/resources - - ${maven.multiModuleProjectDirectory}/semsitter-bridge/prebuilt - - bridge-*.xz - - bridge - diff --git a/java-frontend/src/main/java/org/sonar/java/JavaFrontend.java b/java-frontend/src/main/java/org/sonar/java/JavaFrontend.java index 4e001d09122..637041b2d3d 100644 --- a/java-frontend/src/main/java/org/sonar/java/JavaFrontend.java +++ b/java-frontend/src/main/java/org/sonar/java/JavaFrontend.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -42,6 +43,7 @@ import org.sonar.java.filters.SonarJavaIssueFilter; import org.sonar.java.model.JParserConfig; import org.sonar.java.model.VisitorsBridge; +import org.sonar.java.semsitter.SemsitterBridge; import org.sonar.java.telemetry.Telemetry; import org.sonar.java.telemetry.TelemetryKey; import org.sonar.plugins.java.api.JavaCheck; @@ -237,6 +239,18 @@ private void scanInBatches(BatchModeContext context, List allInputFil private void scanBatch(BatchModeContext context, List batchFiles, AnalysisProgress analysisProgress) { analysisProgress.startBatch(batchFiles.size()); Set environmentsCleaners = new HashSet<>(); + if (sonarComponents.context() != null) { + SemsitterBridge.moduleRoot(sonarComponents.context().fileSystem().baseDir().toPath()); + var config = sonarComponents.context().config(); + Map> settings = new HashMap<>(); + settings.put("jvm.libraries", List.of(config.getStringArray("sonar.java.libraries"))); + settings.put("jvm.binaries", List.of(config.getStringArray("sonar.java.binaries"))); + settings.put("jvm.test_libraries", List.of(config.getStringArray("sonar.java.test.libraries"))); + settings.put("jvm.test_binaries", List.of(config.getStringArray("sonar.java.test.binaries"))); + config.get("sonar.java.jdkHome").ifPresent(home -> settings.put("jvm.jdk_home", List.of(home))); + settings.values().removeIf(List::isEmpty); + SemsitterBridge.moduleSettings(settings); + } JParserConfig.Mode.BATCH .create(javaVersion, context.getClasspath(), sonarComponents.shouldIgnoreUnnamedModuleForSplitPackage()) .parse(batchFiles, sonarComponents::analysisCancelled, analysisProgress, (input, result) -> scanAsBatchCallback(input, result, context, environmentsCleaners)); diff --git a/java-frontend/src/main/java/org/sonar/java/model/JParser.java b/java-frontend/src/main/java/org/sonar/java/model/JParser.java index 21c7ccb338c..30f6f28811d 100644 --- a/java-frontend/src/main/java/org/sonar/java/model/JParser.java +++ b/java-frontend/src/main/java/org/sonar/java/model/JParser.java @@ -31,8 +31,7 @@ import java.util.Objects; import java.util.stream.Stream; import javax.annotation.Nullable; -import org.sonar.java.semsitter.BridgeExtractor; -import org.sonar.java.semsitter.PlatformDetection; +import org.sonar.java.semsitter.EmbeddedUdgGen; import org.sonar.java.semsitter.SemsitterBridge; public final class JParser { @@ -132,19 +131,19 @@ private static Path privateTempDirectory() throws IOException { return Files.createTempDirectory("sonar-java-unit"); } + /** The {@code udg-gen} executable: {@code -Dsonar.java.semsitter.udg}, else {@code SEMSITTER_UDG_GEN}, else the packaged one. */ static Path bridgePath() { - String configured = System.getProperty("sonar.java.semsitter.bridge"); + String configured = System.getProperty("sonar.java.semsitter.udg"); if (configured == null || configured.isBlank()) { - configured = System.getenv("SEMSITTER_BRIDGE"); + configured = System.getenv("SEMSITTER_UDG_GEN"); } if (configured != null && !configured.isBlank()) { return Path.of(configured); } try { - Path target = Path.of(System.getProperty("java.io.tmpdir"), "sonar-java", "semsitter-bridge"); - return BridgeExtractor.extract(target, new PlatformDetection().getPlatform()); - } catch (Exception failure) { - throw new RecognitionException(1, "SemSitter bridge is unavailable: " + failure.getMessage(), failure); + return EmbeddedUdgGen.extract(); + } catch (IOException failure) { + throw new RecognitionException(1, "SemSitter server is unavailable: " + failure.getMessage(), failure); } } diff --git a/java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java b/java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java new file mode 100644 index 00000000000..940c950d8a5 --- /dev/null +++ b/java-frontend/src/main/java/org/sonar/java/semsitter/AnalyzerSession.java @@ -0,0 +1,416 @@ +/* + * SonarQube Java + * Copyright (C) SonarSource Sàrl + * mailto:info AT sonarsource DOT com + * + * You can redistribute and/or modify this program under the terms of + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. + * + * This program 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 Sonar Source-Available License for more details. + * + * You should have received a copy of the Sonar Source-Available License + * along with this program; if not, see https://sonarsource.com/license/ssal/ + */ +package org.sonar.java.semsitter; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.stream.JsonReader; +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.StringReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.StandardOpenOption; +import java.nio.file.Path; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicLong; + +/** + * Client of {@code udg-gen --server} analyzer queries: one process for the whole JVM, one scan per module and classpath, + * {@code get-file} and {@code get-types} per request, and a type cache so each declaration crosses once. + */ +final class AnalyzerSession { + + static final AtomicLong BYTES_IN = new AtomicLong(); + static final AtomicLong BYTES_OUT = new AtomicLong(); + static final AtomicLong REQUESTS = new AtomicLong(); + static final AtomicLong TYPE_REQUESTS = new AtomicLong(); + + private static AnalyzerSession shared; + + private final Path executable; + private Process process; + private OutputStream toServer; + private DataInputStream fromServer; + private String startedRoot; + private String scannedKey; + private static volatile Path moduleRoot; + private static volatile Map> moduleSettings = Map.of(); + private final Map stableTypes = new HashMap<>(); + private final Map batchTypes = new HashMap<>(); + private final Set absent = new HashSet<>(); + private int nextId = 1; + + AnalyzerSession(Path executable) { + this.executable = executable; + } + + /** The module base directory the server scans; without it, the common ancestor of the files of a request. */ + static void moduleRoot(Path root) { + moduleRoot = root; + } + + /** Server settings of the module (the {@code jvm.*} classpath keys), as the scanner properties give them. */ + static void moduleSettings(Map> settings) { + moduleSettings = settings; + } + + static synchronized AnalyzerSession shared(Path executable) { + if (shared == null) { + shared = new AnalyzerSession(executable); + Runtime.getRuntime().addShutdownHook(new Thread(shared::quit)); + } + return shared; + } + + /** + * A module is scanned once per classpath: later batches with the same root and classpath only iterate its files. The + * server takes its root on the command line, so a new root restarts it. + */ + synchronized JsonObject analyze(JsonObject request) throws IOException { + String root = projectRoot(request); + if (process != null && !root.equals(startedRoot)) { + stop(); + } + if (process == null) { + start(root); + } + JsonObject settings = settings(request); + String key = root + "\n" + settings; + if (!key.equals(scannedKey)) { + scan(settings); + scannedKey = key; + } + JsonObject result = new JsonObject(); + result.addProperty("ok", true); + result.addProperty("root", root); + result.add("files", request.getAsJsonArray("files")); + return result; + } + + /** + * The module root when set, else the request's root when every file is under it, else the closest directory above all + * of them. + */ + private static String projectRoot(JsonObject request) { + Path module = moduleRoot; + if (module != null) { + return module.toAbsolutePath().toString(); + } + Path root = Path.of(request.get("root").getAsString()); + List files = new ArrayList<>(); + request.getAsJsonArray("files").forEach(f -> files.add(Path.of(f.getAsString()))); + if (files.isEmpty() || files.stream().allMatch(f -> f.startsWith(root))) { + return root.toString(); + } + Path common = files.get(0).getParent(); + for (Path file : files) { + while (common != null && !file.startsWith(common)) { + common = common.getParent(); + } + } + return (common == null ? root : common).toString(); + } + + private void start(String root) throws IOException { + List command = new ArrayList<>(); + command.add(executable.toAbsolutePath().toString()); + command.addAll(List.of("--server", "--lang", "java", "--modifiers", "--structural", "--decl-location", "--field-items", + "--field-reference-edges", "--override-edges", "--src", root, "--project-root", root)); + String extra = System.getProperty("sonar.java.semsitter.udg.args"); + if (extra != null && !extra.isBlank()) { + command.addAll(List.of(extra.split(" "))); + } + ProcessBuilder builder = new ProcessBuilder(command); + builder.redirectError(ProcessBuilder.Redirect.INHERIT); + process = builder.start(); + startedRoot = root; + toServer = new BufferedOutputStream(process.getOutputStream()); + fromServer = new DataInputStream(process.getInputStream()); + try { + JsonObject hello = new JsonObject(); + hello.addProperty("cmd", "hello"); + String protocol = send(hello, false).header.get("protocol").getAsString(); + if (!protocol.startsWith("1.")) { + throw new IOException("unsupported analyzer protocol " + protocol); + } + } catch (IOException | RuntimeException e) { + stop(); + throw e; + } + } + + /** Ends the process and forgets everything tied to it, so the next request starts a fresh one. */ + private void stop() { + if (process != null) { + process.destroy(); + } + process = null; + toServer = null; + fromServer = null; + startedRoot = null; + scannedKey = null; + stableTypes.clear(); + batchTypes.clear(); + absent.clear(); + } + + /** + * The module {@code jvm.*} settings win when they name libraries or binaries; otherwise the request classpath is used, + * minus the JDK runtime image, which the server reads from {@code jvm.jdk_home}. + */ + private static JsonObject settings(JsonObject request) { + JsonObject settings = new JsonObject(); + Map> configured = moduleSettings; + for (Map.Entry> entry : configured.entrySet()) { + JsonArray values = new JsonArray(); + entry.getValue().forEach(values::add); + settings.add(entry.getKey(), values); + } + if (!settings.has("jvm.libraries") && !settings.has("jvm.binaries")) { + JsonArray libraries = new JsonArray(); + for (JsonElement entry : request.getAsJsonArray("classpath")) { + if (!isJdkImage(entry.getAsString())) { + libraries.add(entry.getAsString()); + } + } + settings.add("jvm.libraries", libraries); + } + if (!settings.has("jvm.jdk_home")) { + JsonArray home = new JsonArray(); + home.add(System.getProperty("java.home")); + settings.add("jvm.jdk_home", home); + } + return settings; + } + + /** The JDK runtime image ({@code lib/modules}), whichever separator the entry uses. */ + static boolean isJdkImage(String entry) { + return entry.replace('\\', '/').endsWith("/lib/modules"); + } + + private void scan(JsonObject settings) throws IOException { + batchTypes.clear(); + absent.clear(); + if (System.getenv("SEMSITTER_COUNTERS_FILE") != null) { + System.err.println("SEMSITTER-SETTINGS " + settings); + } + JsonObject set = new JsonObject(); + set.addProperty("cmd", "set-settings"); + set.add("settings", settings); + send(set, false); + JsonObject full = new JsonObject(); + full.addProperty("cmd", "full-gen"); + send(full, false); + } + + synchronized JsonObject cst(JsonObject request) throws IOException { + JsonObject get = new JsonObject(); + get.addProperty("cmd", "get-file"); + get.addProperty("path", request.get("path").getAsString()); + String ahead = System.getProperty("sonar.java.semsitter.udg.prefetch"); + if (ahead != null) { + get.addProperty("ahead", Integer.parseInt(ahead)); + } + Answer answer = send(get, true); + // The bridge's response message around the document: ok = true, document = field 5. + ByteArrayOutputStream wrapped = new ByteArrayOutputStream(answer.payload.length + 16); + wrapped.write(0x10); + wrapped.write(0x01); + wrapped.write(0x2a); + varint(wrapped, answer.payload.length); + wrapped.write(answer.payload); + return BridgeFrames.response(wrapped.toByteArray()); + } + + /** The declarations of {@code names} and their supertypes, asking the server only for names never seen. */ + synchronized JsonObject types(Set names) throws IOException { + Set missing = new HashSet<>(); + for (String name : names) { + String erased = erase(name); + if (!stableTypes.containsKey(erased) && !batchTypes.containsKey(erased) && !absent.contains(erased)) { + missing.add(erased); + } + } + if (!missing.isEmpty()) { + JsonObject request = new JsonObject(); + request.addProperty("cmd", "get-types"); + JsonArray array = new JsonArray(); + missing.forEach(array::add); + request.add("names", array); + TYPE_REQUESTS.incrementAndGet(); + Answer answer = send(request, true); + Set project = new HashSet<>(); + if (answer.header.has("project")) { + answer.header.getAsJsonArray("project").forEach(e -> project.add(e.getAsString())); + } + JsonReader reader = new JsonReader(new StringReader(new String(answer.payload, StandardCharsets.UTF_8))); + reader.setNestingLimit(Integer.MAX_VALUE); + for (Map.Entry entry : JsonParser.parseReader(reader).getAsJsonObject().entrySet()) { + (project.contains(entry.getKey()) ? batchTypes : stableTypes).put(entry.getKey(), entry.getValue().getAsJsonObject()); + } + for (String name : missing) { + if (!stableTypes.containsKey(name) && !batchTypes.containsKey(name)) { + absent.add(name); + } + } + } + JsonObject found = new JsonObject(); + Deque queue = new ArrayDeque<>(); + names.forEach(name -> queue.add(erase(name))); + Set seen = new HashSet<>(); + while (!queue.isEmpty()) { + String name = queue.poll(); + if (!seen.add(name)) { + continue; + } + JsonObject info = batchTypes.containsKey(name) ? batchTypes.get(name) : stableTypes.get(name); + if (info == null) { + continue; + } + found.add(name, info); + for (String key : new String[] {"super", "interfaces"}) { + JsonElement value = info.get(key); + if (value == null || value.isJsonNull()) { + continue; + } + if (value.isJsonArray()) { + value.getAsJsonArray().forEach(e -> { + if (e.isJsonPrimitive()) { + queue.add(erase(e.getAsString())); + } + }); + } else if (value.isJsonPrimitive()) { + queue.add(erase(value.getAsString())); + } + } + } + JsonObject response = new JsonObject(); + response.addProperty("ok", true); + response.add("types", found); + return response; + } + + private record Answer(JsonObject header, byte[] payload) {} + + private Answer send(JsonObject request, boolean payload) throws IOException { + try { + return exchange(request); + } catch (IOException | RuntimeException e) { + System.err.println("SERVER-ERROR " + request.get("cmd") + " " + e); + if (process != null && !process.isAlive()) { + stop(); + } + throw e; + } + } + + private Answer exchange(JsonObject request) throws IOException { + request.addProperty("id", nextId++); + byte[] line = (request + "\n").getBytes(StandardCharsets.UTF_8); + toServer.write(line); + toServer.flush(); + BYTES_OUT.addAndGet(line.length); + String text = readLine(fromServer); + BYTES_IN.addAndGet(text.length() + 1L); + REQUESTS.incrementAndGet(); + JsonReader reader = new JsonReader(new StringReader(text)); + reader.setNestingLimit(Integer.MAX_VALUE); + JsonObject header = JsonParser.parseReader(reader).getAsJsonObject(); + if (!header.has("ok") || !header.get("ok").getAsBoolean()) { + throw new IOException("semsitter server: " + header.get("error")); + } + byte[] bytes = new byte[0]; + if (header.has("payload_bytes")) { + bytes = fromServer.readNBytes(header.get("payload_bytes").getAsInt()); + BYTES_IN.addAndGet(bytes.length); + } + return new Answer(header, bytes); + } + + private static String readLine(InputStream in) throws IOException { + ByteArrayOutputStream line = new ByteArrayOutputStream(256); + for (int b = in.read(); b != '\n'; b = in.read()) { + if (b < 0) { + throw new IOException("semsitter server closed its output"); + } + line.write(b); + } + return line.toString(StandardCharsets.UTF_8); + } + + private static void varint(ByteArrayOutputStream out, long value) { + while ((value & ~0x7FL) != 0) { + out.write((int) ((value & 0x7F) | 0x80)); + value >>>= 7; + } + out.write((int) value); + } + + private synchronized void quit() { + if (process == null) { + return; + } + try { + String counters = System.getenv("SEMSITTER_COUNTERS_FILE"); + if (counters != null && !counters.isBlank()) { + JsonObject release = new JsonObject(); + release.addProperty("cmd", "release"); + String line = "SERVER-COUNTERS " + send(release, false).header.get("counters") + "\n" + + String.format("SERVER-CLIENT requests=%d type_requests=%d bytes_in=%d bytes_out=%d%n", REQUESTS.get(), TYPE_REQUESTS.get(), + BYTES_IN.get(), BYTES_OUT.get()); + Files.writeString(Path.of(counters), line, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); + } + JsonObject quit = new JsonObject(); + quit.addProperty("cmd", "quit"); + send(quit, false); + toServer.close(); + process.waitFor(); + } catch (Exception ignored) { + process.destroy(); + } + } + + /** Strips the generic arity suffix of a SemSitter id segment. */ + static String erase(String id) { + StringBuilder out = new StringBuilder(id.length()); + for (int i = 0; i < id.length(); i++) { + char c = id.charAt(i); + if (c == '`') { + while (i + 1 < id.length() && Character.isDigit(id.charAt(i + 1))) { + i++; + } + } else { + out.append(c); + } + } + return out.toString(); + } +} diff --git a/java-frontend/src/main/java/org/sonar/java/semsitter/BridgeExtractor.java b/java-frontend/src/main/java/org/sonar/java/semsitter/BridgeExtractor.java deleted file mode 100644 index a31a1462ac5..00000000000 --- a/java-frontend/src/main/java/org/sonar/java/semsitter/BridgeExtractor.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ -package org.sonar.java.semsitter; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import java.nio.file.attribute.PosixFileAttributeView; -import java.util.Set; -import org.apache.commons.compress.archivers.tar.TarArchiveEntry; -import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; -import org.apache.commons.compress.compressors.xz.XZCompressorInputStream; -import org.sonar.java.semsitter.PlatformDetection.Platform; - -import static java.nio.file.attribute.PosixFilePermission.OWNER_EXECUTE; -import static java.nio.file.attribute.PosixFilePermission.OWNER_READ; -import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE; - -public final class BridgeExtractor { - - private BridgeExtractor() { - } - - public static Path extract(Path targetDir, Platform platform) throws IOException { - if (platform == Platform.UNSUPPORTED) { - throw new IllegalStateException("Cannot extract bridge for unsupported platform"); - } - String bridgeName = bridgeName(platform); - Path bridgePath = targetDir.resolve(bridgeName); - String archiveName = archiveName(platform); - Path prebuilt = findPrebuilt(archiveName); - if (Files.exists(bridgePath) && !prebuiltIsNewer(prebuilt, bridgePath)) { - return bridgePath; - } - String resourcePath = "/bridge/" + archiveName; - InputStream resource = prebuilt != null ? Files.newInputStream(prebuilt) : BridgeExtractor.class.getResourceAsStream(resourcePath); - if (resource == null) { - throw new IllegalStateException("Bridge resource not found"); - } - try (InputStream stream = resource) { - extractArchive(targetDir, bridgePath, stream, resourcePath, bridgeName); - } - return bridgePath; - } - - private static boolean prebuiltIsNewer(Path prebuilt, Path bridgePath) throws IOException { - return prebuilt != null && Files.getLastModifiedTime(prebuilt).compareTo(Files.getLastModifiedTime(bridgePath)) > 0; - } - - private static Path findPrebuilt(String archiveName) { - Path found = walk(Path.of("").toAbsolutePath(), archiveName); - if (found != null) { - return found; - } - Path origin = codeOrigin(); - return origin == null ? null : walk(origin, archiveName); - } - - private static Path codeOrigin() { - try { - var source = BridgeExtractor.class.getProtectionDomain().getCodeSource(); - if (source == null || source.getLocation() == null) { - return null; - } - return Path.of(source.getLocation().toURI()); - } catch (Exception failure) { - return null; - } - } - - private static Path walk(Path dir, String archiveName) { - for (int i = 0; i < 8 && dir != null; i++) { - Path archive = dir.resolve("semsitter-bridge").resolve("prebuilt").resolve(archiveName); - if (Files.isRegularFile(archive)) { - return archive; - } - dir = dir.getParent(); - } - return null; - } - - private static String archiveName(Platform platform) { - String suffix = switch (platform) { - case ALPINE_AARCH64 -> Platform.LINUX_AARCH64.toString(); - case ALPINE_X64 -> Platform.LINUX_X64.toString(); - default -> platform.toString(); - }; - return "bridge-" + suffix + ".xz"; - } - - private static String bridgeName(Platform platform) { - return platform == Platform.WINDOWS_X64 ? "bridge.exe" : "bridge"; - } - - private static void extractArchive(Path target, Path bridgePath, InputStream archive, String resourcePath, String bridgeName) throws IOException { - Path stagingPath = target.resolve(bridgeName + ".ext"); - try { - try (var xzStream = new XZCompressorInputStream(archive); - var tarStream = new TarArchiveInputStream(xzStream)) { - TarArchiveEntry entry = tarStream.getNextEntry(); - while (entry != null && (!bridgeName.equals(entry.getName()) || entry.isDirectory())) { - entry = tarStream.getNextEntry(); - } - if (entry == null) { - throw new IllegalStateException("Bridge binary not found in archive: " + resourcePath); - } - Files.createDirectories(target); - Files.copy(tarStream, stagingPath, StandardCopyOption.REPLACE_EXISTING); - } - Files.deleteIfExists(bridgePath); - Files.move(stagingPath, bridgePath, StandardCopyOption.ATOMIC_MOVE); - PosixFileAttributeView view = Files.getFileAttributeView(bridgePath, PosixFileAttributeView.class); - if (view != null) { - view.setPermissions(Set.of(OWNER_EXECUTE, OWNER_READ, OWNER_WRITE)); - } - } catch (Exception failure) { - Files.deleteIfExists(stagingPath); - if (failure instanceof IOException ioException) { - throw ioException; - } - if (failure instanceof RuntimeException runtimeException) { - throw runtimeException; - } - throw new IllegalStateException(failure); - } - } -} diff --git a/java-frontend/src/main/java/org/sonar/java/semsitter/EmbeddedUdgGen.java b/java-frontend/src/main/java/org/sonar/java/semsitter/EmbeddedUdgGen.java new file mode 100644 index 00000000000..b462264586e --- /dev/null +++ b/java-frontend/src/main/java/org/sonar/java/semsitter/EmbeddedUdgGen.java @@ -0,0 +1,83 @@ +/* + * SonarQube Java + * Copyright (C) SonarSource Sàrl + * mailto:info AT sonarsource DOT com + * + * You can redistribute and/or modify this program under the terms of + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. + * + * This program 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 Sonar Source-Available License for more details. + * + * You should have received a copy of the Sonar Source-Available License + * along with this program; if not, see https://sonarsource.com/license/ssal/ + */ +package org.sonar.java.semsitter; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Locale; +import javax.annotation.Nullable; +import org.apache.commons.compress.compressors.xz.XZCompressorInputStream; + +/** The {@code udg-gen} executable packaged in the plugin, extracted once per JVM to a temporary file. */ +public final class EmbeddedUdgGen { + + private static Path extracted; + + private EmbeddedUdgGen() { + } + + /** The resource name for a platform, or null when no executable is packaged for it. */ + @Nullable + static String resourceName(@Nullable String osName, @Nullable String osArch) { + String os = osName == null ? "" : osName.toLowerCase(Locale.ROOT); + String arch = osArch == null ? "" : osArch.toLowerCase(Locale.ROOT); + boolean x64 = arch.equals("amd64") || arch.equals("x86_64"); + boolean arm64 = arch.equals("aarch64") || arch.equals("arm64"); + String platform = null; + if (os.contains("linux")) { + // Statically linked, so they also run on musl distributions. + platform = x64 ? "linux-x64" : arm64 ? "linux-aarch64" : null; + } else if (os.contains("mac")) { + platform = x64 ? "macos-x64" : arm64 ? "macos-aarch64" : null; + } else if (os.contains("win") && x64) { + platform = "windows-x64"; + } + return platform == null ? null : "/udg-gen/udg-gen-" + platform + ".xz"; + } + + public static synchronized Path extract() throws IOException { + if (extracted != null && Files.isExecutable(extracted)) { + return extracted; + } + String osName = System.getProperty("os.name"); + String osArch = System.getProperty("os.arch"); + String resource = resourceName(osName, osArch); + if (resource == null) { + throw new IOException("no packaged udg-gen for " + osName + " " + osArch + + ": set -Dsonar.java.semsitter.udg or SEMSITTER_UDG_GEN"); + } + Path dir = Files.createTempDirectory("sonar-java-udg-gen"); + Path target = dir.resolve(resource.contains("windows") ? "udg-gen.exe" : "udg-gen"); + try (InputStream packaged = EmbeddedUdgGen.class.getResourceAsStream(resource)) { + if (packaged == null) { + throw new IOException("packaged udg-gen " + resource + " is missing from the plugin"); + } + try (InputStream xz = new XZCompressorInputStream(packaged)) { + Files.copy(xz, target); + } + } + if (!target.toFile().setExecutable(true, true)) { + throw new IOException("cannot make " + target + " executable"); + } + dir.toFile().deleteOnExit(); + target.toFile().deleteOnExit(); + extracted = target; + return target; + } +} diff --git a/java-frontend/src/main/java/org/sonar/java/semsitter/PlatformDetection.java b/java-frontend/src/main/java/org/sonar/java/semsitter/PlatformDetection.java deleted file mode 100644 index be7db6214e3..00000000000 --- a/java-frontend/src/main/java/org/sonar/java/semsitter/PlatformDetection.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ -package org.sonar.java.semsitter; - -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Locale; -import java.util.Optional; - -public class PlatformDetection { - - private final String rawOsName; - private final String rawOsArch; - - public enum Platform { - LINUX_X64("linux-x64"), - LINUX_AARCH64("linux-aarch64"), - ALPINE_X64("alpine-x64"), - ALPINE_AARCH64("alpine-aarch64"), - MACOS_X64("macos-x64"), - MACOS_AARCH64("macos-aarch64"), - WINDOWS_X64("windows-x64"), - UNSUPPORTED("unsupported"); - - private final String name; - - Platform(String name) { - this.name = name; - } - - @Override - public String toString() { - return name; - } - } - - public PlatformDetection() { - this(System.getProperty("os.name"), System.getProperty("os.arch")); - } - - PlatformDetection(String rawOsName, String rawOsArch) { - this.rawOsName = rawOsName; - this.rawOsArch = rawOsArch; - } - - public Platform getPlatform() { - String platformString = osName() + "-" + osArch(); - for (Platform platform : Platform.values()) { - if (platform.toString().equalsIgnoreCase(platformString)) { - return platform; - } - } - return Platform.UNSUPPORTED; - } - - private String osName() { - String osName = Optional.ofNullable(rawOsName).orElse("generic").toLowerCase(Locale.ROOT); - if (osName.contains("win")) { - return "windows"; - } else if (osName.contains("mac")) { - return "macos"; - } else if (osName.contains("nux")) { - if (isAlpineLinux()) { - return "alpine"; - } - return "Linux"; - } - return osName.replaceAll("\\s+", ""); - } - - private String osArch() { - String osArch = Optional.ofNullable(rawOsArch).orElse("unknown").toLowerCase(Locale.ROOT); - return switch (osArch) { - case "amd64", "x86_64" -> "x64"; - case "aarch64", "arm64" -> "aarch64"; - case "x86", "i386", "i686" -> "x86"; - default -> osArch; - }; - } - - private static boolean isAlpineLinux() { - return Files.exists(Path.of("/etc/alpine-release")); - } -} diff --git a/java-frontend/src/main/java/org/sonar/java/semsitter/SemsitterBridge.java b/java-frontend/src/main/java/org/sonar/java/semsitter/SemsitterBridge.java index 756a1c53361..70d4b216161 100644 --- a/java-frontend/src/main/java/org/sonar/java/semsitter/SemsitterBridge.java +++ b/java-frontend/src/main/java/org/sonar/java/semsitter/SemsitterBridge.java @@ -17,291 +17,81 @@ package org.sonar.java.semsitter; import com.google.gson.JsonArray; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.stream.JsonReader; -import java.io.BufferedReader; -import java.io.BufferedWriter; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.StringReader; -import java.nio.charset.StandardCharsets; import java.nio.file.Path; -import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.List; -import java.util.concurrent.TimeUnit; +import java.util.Map; /** - * Client for the {@code semsitter-bridge} executable. + * Client view of the shared {@code udg-gen --server} process. * - *

A current bridge is spoken to over shared memory: protobuf payloads, one ping byte on the - * pipe. {@code -Dsonar.java.semsitter.dump=

} also asks it to write {@code udg.json}, the - * semantic-dump JSON and the annotated CST JSON under that directory. - * - *

{@link #analyze} scans a batch of files with the classpath, then {@link #cst} returns the - * CST of one of them with its SemSitter semantics: {@code tree} (nodes with kind {@code k}, - * field {@code f}, position {@code p} as 1-based lines and 0-based UTF-16 columns, text {@code t}, - * children {@code c}, declared symbol ids {@code decls}, resolved occurrences {@code refs}), - * {@code symbols} (the semantic dump symbols) and {@code facts} (graph facts by id). + *

{@link #analyze} scans the module once, {@link #cst} returns the CST of one file with its SemSitter semantics: + * {@code tree} (nodes with kind {@code k}, field {@code f}, position {@code p} as 1-based lines and 0-based UTF-16 + * columns, text {@code t}, children {@code c}, declared symbol ids {@code decls}, resolved occurrences {@code refs}), + * {@code symbols} and {@code facts} (graph facts by id). Closing a batch keeps the process, which lives for the whole + * scanner run. */ public final class SemsitterBridge implements AutoCloseable { - private static final int STDERR_LIMIT = 8192; - - private final Session session; - private int nextId = 1; - private boolean closed; + private final AnalyzerSession server; - private SemsitterBridge(Session session) { - this.session = session; + private SemsitterBridge(AnalyzerSession server) { + this.server = server; } - public static SemsitterBridge start(Path executable) throws IOException { - boolean shm = speaksShm(executable); - List command = new ArrayList<>(); - command.add(executable.toAbsolutePath().toString()); - command.add("serve"); - if (shm) { - command.add("--shm"); - } - String dump = System.getProperty("sonar.java.semsitter.dump"); - if (shm && dump != null && !dump.isBlank()) { - command.add("--dump-json"); - command.add(dump); - } - Process process = new ProcessBuilder(command).start(); - return new SemsitterBridge(shm ? new ShmSession(process) : new JsonlSession(process)); + public static SemsitterBridge start(Path executable) { + return new SemsitterBridge(AnalyzerSession.shared(executable)); } - /** - * Scans {@code files} as one project rooted at {@code root}. - * @return the response, whose {@code files} are the project-relative paths accepted by {@link #cst} - */ - public synchronized JsonObject analyze(Path root, Collection files, Collection classpath) throws IOException { - return session.exchange(BridgeFrames.analyze(nextId++, root.toAbsolutePath().toString(), absolute(files), classpath), jsonAnalyze(root, files, classpath)); + /** Sets the {@code jvm.*} classpath settings the server scans with. */ + public static void moduleSettings(Map> settings) { + AnalyzerSession.moduleSettings(settings); } - public synchronized JsonObject cst(String path) throws IOException { - JsonObject request = new JsonObject(); - request.addProperty("op", "cst"); - request.addProperty("path", path); - return session.exchange(BridgeFrames.cst(nextId++, path), request).getAsJsonObject("cst"); + /** Sets the module base directory the server scans. */ + public static void moduleRoot(Path root) { + AnalyzerSession.moduleRoot(root); } /** - * @return declarations (kind, supertypes, members) of the types named by erased binary names, and of their supertypes + * Makes the module ready for the files of this batch. + * @return the response, whose {@code files} are the paths accepted by {@link #cst} */ - public synchronized JsonObject types(Collection names) throws IOException { - return session.exchange(BridgeFrames.types(nextId++, names), jsonTypes(names)).getAsJsonObject("types"); - } - - public synchronized JsonObject parse(String source) throws IOException { + public JsonObject analyze(Path root, Collection files, Collection classpath) throws IOException { JsonObject request = new JsonObject(); - request.addProperty("op", "parse"); - request.addProperty("source", source); - return session.exchange(BridgeFrames.parse(nextId++, source), request).getAsJsonObject("cst"); - } - - @Override - public synchronized void close() { - if (closed) { - return; - } - closed = true; - session.close(BridgeFrames.quit(nextId++)); - } - - private static boolean speaksShm(Path executable) throws IOException { - Process help = new ProcessBuilder(executable.toAbsolutePath().toString(), "-h").start(); - String text = new String(help.getInputStream().readAllBytes(), StandardCharsets.UTF_8); - try { - help.waitFor(2, TimeUnit.SECONDS); - } catch (InterruptedException interrupted) { - Thread.currentThread().interrupt(); - } - help.destroyForcibly(); - return text.contains("--shm"); - } - - private static JsonObject jsonAnalyze(Path root, Collection files, Collection classpath) { - JsonObject request = new JsonObject(); - request.addProperty("op", "analyze"); request.addProperty("root", root.toAbsolutePath().toString()); JsonArray fileArray = new JsonArray(); - absolute(files).forEach(fileArray::add); + files.forEach(file -> fileArray.add(file.toAbsolutePath().toString())); request.add("files", fileArray); JsonArray classpathArray = new JsonArray(); classpath.forEach(classpathArray::add); request.add("classpath", classpathArray); - return request; + return server.analyze(request); } - private static JsonObject jsonTypes(Collection names) { + public JsonObject cst(String path) throws IOException { JsonObject request = new JsonObject(); - request.addProperty("op", "types"); - JsonArray nameArray = new JsonArray(); - names.forEach(nameArray::add); - request.add("names", nameArray); - return request; - } - - private static List absolute(Collection files) { - List paths = new ArrayList<>(); - files.forEach(file -> paths.add(file.toAbsolutePath().toString())); - return paths; - } - - private interface Session { - JsonObject exchange(byte[] protobuf, JsonObject jsonl) throws IOException; - - void close(byte[] quit); - } - - private static final class ShmSession implements Session { - private final Process process; - private final OutputStream toBridge; - private final InputStream fromBridge; - private final SharedSlots slots; - private final StringBuilder stderr = new StringBuilder(); - - private ShmSession(Process process) throws IOException { - this.process = process; - this.toBridge = process.getOutputStream(); - this.fromBridge = process.getInputStream(); - Thread drain = new Thread(() -> drain(process.getErrorStream(), stderr), "semsitter-bridge-stderr"); - drain.setDaemon(true); - drain.start(); - int ready = fromBridge.read(); - if (ready != SharedSlots.READY) { - throw new IOException("semsitter bridge did not open shared memory" + suffix(stderr)); - } - this.slots = SharedSlots.open(process.pid()); - toBridge.write(SharedSlots.ATTACHED); - toBridge.flush(); - } - - @Override - public JsonObject exchange(byte[] protobuf, JsonObject jsonl) throws IOException { - slots.writeRequest(protobuf, toBridge, fromBridge); - JsonObject response = BridgeFrames.response(slots.readResponse(fromBridge, toBridge)); - if (!response.has("ok") || !response.get("ok").getAsBoolean()) { - String error = response.has("error") ? response.get("error").getAsString() : "bridge request failed"; - throw new IOException("semsitter bridge: " + error + suffix(stderr)); - } - return response; - } - - @Override - public void close(byte[] quit) { - try { - exchange(quit, new JsonObject()); - slots.close(); - toBridge.close(); - } catch (IOException ignored) { - process.destroy(); - } - waitFor(process); - } - } - - private static final class JsonlSession implements Session { - private final Process process; - private final BufferedWriter toBridge; - private final BufferedReader fromBridge; - private final StringBuilder stderr = new StringBuilder(); - private int nextId = 1; - - private JsonlSession(Process process) { - this.process = process; - this.toBridge = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8)); - this.fromBridge = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)); - Thread drain = new Thread(() -> drain(process.getErrorStream(), stderr), "semsitter-bridge-stderr"); - drain.setDaemon(true); - drain.start(); - } - - @Override - public JsonObject exchange(byte[] protobuf, JsonObject jsonl) throws IOException { - int id = nextId++; - jsonl.addProperty("id", id); - toBridge.write(jsonl.toString()); - toBridge.write('\n'); - toBridge.flush(); - String responseLine = fromBridge.readLine(); - if (responseLine == null) { - throw new IOException("semsitter bridge closed stdout" + suffix(stderr)); - } - JsonElement parsed; - try { - JsonReader reader = new JsonReader(new StringReader(responseLine)); - reader.setNestingLimit(Integer.MAX_VALUE); - parsed = JsonParser.parseReader(reader); - } catch (RuntimeException invalid) { - throw new IOException("semsitter bridge returned invalid JSON" + suffix(stderr), invalid); - } - if (!parsed.isJsonObject()) { - throw new IOException("semsitter bridge returned a non-object" + suffix(stderr)); - } - JsonObject response = parsed.getAsJsonObject(); - if (!response.has("id") || response.get("id").getAsInt() != id) { - throw new IOException("semsitter bridge response id mismatch, expected " + id); - } - if (!response.has("ok") || !response.get("ok").getAsBoolean()) { - String error = response.has("error") ? response.get("error").getAsString() : responseLine; - throw new IOException("semsitter bridge: " + error + suffix(stderr)); - } - return response; - } - - @Override - public void close(byte[] quit) { - try { - JsonObject request = new JsonObject(); - request.addProperty("op", "quit"); - exchange(quit, request); - toBridge.close(); - } catch (IOException ignored) { - process.destroy(); - } - waitFor(process); - } + request.addProperty("path", path); + return server.cst(request).getAsJsonObject("cst"); } - private static void waitFor(Process process) { - try { - if (!process.waitFor(2, TimeUnit.SECONDS)) { - process.destroyForcibly(); - } - } catch (InterruptedException interrupted) { - Thread.currentThread().interrupt(); - process.destroyForcibly(); - } + /** + * @return declarations (kind, supertypes, members) of the types named by erased binary names, and of their supertypes + */ + public JsonObject types(Collection names) throws IOException { + return server.types(new HashSet<>(names)).getAsJsonObject("types"); } - private static String suffix(StringBuilder stderr) { - synchronized (stderr) { - return stderr.isEmpty() ? "" : (" stderr: " + stderr); - } + /** Single-source parses have no module to scan, so the server does not serve them. */ + public JsonObject parse(String source) throws IOException { + throw new IOException("single-source parse is not supported by the analyzer server"); } - private static void drain(InputStream stream, StringBuilder buffer) { - try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) { - String line; - while ((line = reader.readLine()) != null) { - synchronized (buffer) { - buffer.append(line).append('\n'); - if (buffer.length() > STDERR_LIMIT) { - buffer.delete(0, buffer.length() - STDERR_LIMIT); - } - } - } - } catch (IOException ignored) { - // stderr closed with the process - } + @Override + public void close() { + // the process serves the next batch and ends with the scanner } } diff --git a/java-frontend/src/main/java/org/sonar/java/semsitter/SharedSlots.java b/java-frontend/src/main/java/org/sonar/java/semsitter/SharedSlots.java deleted file mode 100644 index dd89e42e921..00000000000 --- a/java-frontend/src/main/java/org/sonar/java/semsitter/SharedSlots.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ -package org.sonar.java.semsitter; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.RandomAccessFile; -import java.nio.ByteOrder; -import java.nio.MappedByteBuffer; -import java.nio.channels.FileChannel; -import java.nio.file.Files; -import java.nio.file.Path; - -/** - * Pair of shared mappings the bridge creates for one process. Payloads stay in the mappings. - * The pipe only carries a one-byte ping: {@code 1} data is ready, {@code 2} the mapping grew. - */ -final class SharedSlots implements AutoCloseable { - - static final byte READY = 'R'; - static final byte ATTACHED = 'A'; - static final byte DATA = 1; - static final byte GROW = 2; - - private static final int HEADER = 24; - private static final int MAGIC = 0x31424A53; - private static final int MAX = 512 * 1024 * 1024; - - private final RandomAccessFile requestFile; - private final RandomAccessFile responseFile; - private MappedByteBuffer request; - private MappedByteBuffer response; - - private SharedSlots(RandomAccessFile requestFile, MappedByteBuffer request, RandomAccessFile responseFile, MappedByteBuffer response) { - this.requestFile = requestFile; - this.request = request; - this.responseFile = responseFile; - this.response = response; - } - - static SharedSlots open(long pid) throws IOException { - Path dir = Files.isDirectory(Path.of("/dev/shm")) ? Path.of("/dev/shm") : Path.of(System.getProperty("java.io.tmpdir")); - RandomAccessFile requestFile = new RandomAccessFile(dir.resolve("sjb-" + pid + "-q").toFile(), "rw"); - RandomAccessFile responseFile = new RandomAccessFile(dir.resolve("sjb-" + pid + "-a").toFile(), "rw"); - return new SharedSlots(requestFile, map(requestFile), responseFile, map(responseFile)); - } - - void writeRequest(byte[] bytes, OutputStream toBridge, InputStream fromBridge) throws IOException { - request = write(requestFile, request, bytes, toBridge, fromBridge); - } - - byte[] readResponse(InputStream fromBridge, OutputStream toBridge) throws IOException { - Read read = read(responseFile, response, fromBridge, toBridge); - response = read.buffer; - return read.payload; - } - - @Override - public void close() throws IOException { - requestFile.close(); - responseFile.close(); - } - - private static MappedByteBuffer write(RandomAccessFile file, MappedByteBuffer buffer, byte[] bytes, OutputStream toPeer, InputStream fromPeer) throws IOException { - int need = HEADER + bytes.length; - if (need > buffer.capacity()) { - int cap = buffer.capacity(); - while (cap < need) { - if (cap >= MAX) { - throw new IOException("protobuf message of " + bytes.length + " bytes exceeds the shared mapping"); - } - cap = Math.min(cap * 2, MAX); - } - file.setLength(cap); - header(buffer, GROW, cap, cap); - ping(toPeer, GROW); - if (readPing(fromPeer) != GROW) { - throw new IOException("shared mapping grow was not acknowledged"); - } - buffer = map(file); - } - buffer.position(HEADER); - buffer.put(bytes); - header(buffer, (byte) 0, bytes.length, buffer.capacity()); - ping(toPeer, DATA); - return buffer; - } - - private static Read read(RandomAccessFile file, MappedByteBuffer buffer, InputStream fromPeer, OutputStream toPeer) throws IOException { - while (true) { - switch (readPing(fromPeer)) { - case GROW -> { - int cap = (int) len(buffer); - file.setLength(cap); - buffer = map(file); - ping(toPeer, GROW); - } - case DATA -> { - int size = (int) len(buffer); - if (HEADER + size > buffer.capacity()) { - throw new IOException("payload of " + size + " bytes does not fit the shared mapping"); - } - byte[] payload = new byte[size]; - buffer.position(HEADER); - buffer.get(payload); - return new Read(buffer, payload); - } - default -> throw new IOException("unexpected shared-memory ping"); - } - } - } - - private static void header(MappedByteBuffer buffer, int kind, long len, long cap) { - buffer.order(ByteOrder.LITTLE_ENDIAN); - buffer.putInt(0, MAGIC); - buffer.putInt(4, kind); - buffer.putLong(8, len); - buffer.putLong(16, cap); - } - - private static long len(MappedByteBuffer buffer) throws IOException { - buffer.order(ByteOrder.LITTLE_ENDIAN); - if (buffer.getInt(0) != MAGIC) { - throw new IOException("shared mapping has no bridge header"); - } - return buffer.getLong(8); - } - - private static MappedByteBuffer map(RandomAccessFile file) throws IOException { - MappedByteBuffer buffer = file.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, file.length()); - buffer.order(ByteOrder.LITTLE_ENDIAN); - return buffer; - } - - private static void ping(OutputStream toPeer, byte value) throws IOException { - toPeer.write(value); - toPeer.flush(); - } - - private static byte readPing(InputStream fromPeer) throws IOException { - int value = fromPeer.read(); - if (value < 0) { - throw new IOException("semsitter bridge closed the ping pipe"); - } - return (byte) value; - } - - private record Read(MappedByteBuffer buffer, byte[] payload) { - } -} diff --git a/java-frontend/src/main/resources/udg-gen/udg-gen-linux-aarch64.xz b/java-frontend/src/main/resources/udg-gen/udg-gen-linux-aarch64.xz new file mode 100644 index 00000000000..f17d750a250 Binary files /dev/null and b/java-frontend/src/main/resources/udg-gen/udg-gen-linux-aarch64.xz differ diff --git a/java-frontend/src/main/resources/udg-gen/udg-gen-linux-x64.xz b/java-frontend/src/main/resources/udg-gen/udg-gen-linux-x64.xz new file mode 100644 index 00000000000..a95f58e5cd8 Binary files /dev/null and b/java-frontend/src/main/resources/udg-gen/udg-gen-linux-x64.xz differ diff --git a/java-frontend/src/main/resources/udg-gen/udg-gen-macos-aarch64.xz b/java-frontend/src/main/resources/udg-gen/udg-gen-macos-aarch64.xz new file mode 100644 index 00000000000..896e328f6d0 Binary files /dev/null and b/java-frontend/src/main/resources/udg-gen/udg-gen-macos-aarch64.xz differ diff --git a/java-frontend/src/main/resources/udg-gen/udg-gen-macos-x64.xz b/java-frontend/src/main/resources/udg-gen/udg-gen-macos-x64.xz new file mode 100644 index 00000000000..863bd9ec995 Binary files /dev/null and b/java-frontend/src/main/resources/udg-gen/udg-gen-macos-x64.xz differ diff --git a/java-frontend/src/test/java/org/sonar/java/semsitter/AnalyzerSessionTest.java b/java-frontend/src/test/java/org/sonar/java/semsitter/AnalyzerSessionTest.java new file mode 100644 index 00000000000..a304164780d --- /dev/null +++ b/java-frontend/src/test/java/org/sonar/java/semsitter/AnalyzerSessionTest.java @@ -0,0 +1,126 @@ +/* + * SonarQube Java + * Copyright (C) SonarSource Sàrl + * mailto:info AT sonarsource DOT com + * + * You can redistribute and/or modify this program under the terms of + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. + * + * This program 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 Sonar Source-Available License for more details. + * + * You should have received a copy of the Sonar Source-Available License + * along with this program; if not, see https://sonarsource.com/license/ssal/ + */ +package org.sonar.java.semsitter; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.api.io.TempDir; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@DisabledOnOs(OS.WINDOWS) +class AnalyzerSessionTest { + + @TempDir + Path temp; + + @AfterEach + void reset() { + AnalyzerSession.moduleRoot(null); + AnalyzerSession.moduleSettings(Map.of()); + } + + @Test + void jdk_image_is_recognized_with_either_separator() { + assertThat(AnalyzerSession.isJdkImage("/jdk/lib/modules")).isTrue(); + assertThat(AnalyzerSession.isJdkImage("C:\\jdk\\lib\\modules")).isTrue(); + assertThat(AnalyzerSession.isJdkImage("/libs/guava.jar")).isFalse(); + } + + @Test + void failed_handshake_ends_the_process_and_the_next_request_starts_a_new_one() throws IOException { + AnalyzerSession session = new AnalyzerSession(fakeServer("2.0.0")); + + assertThatThrownBy(() -> session.analyze(request("a", "x.jar"))) + .hasMessage("unsupported analyzer protocol 2.0.0"); + assertThatThrownBy(() -> session.analyze(request("a", "x.jar"))) + .hasMessage("unsupported analyzer protocol 2.0.0"); + + assertThat(starts()).isEqualTo(2); + } + + @Test + void new_root_restarts_the_server_and_same_root_does_not() throws IOException { + AnalyzerSession session = new AnalyzerSession(fakeServer("1.2.0")); + + session.analyze(request("a", "x.jar")); + session.analyze(request("a", "x.jar")); + assertThat(starts()).isEqualTo(1); + + session.analyze(request("b", "x.jar")); + assertThat(starts()).isEqualTo(2); + } + + @Test + void classpath_change_rescans_without_restart_and_module_libraries_take_priority() throws IOException { + AnalyzerSession session = new AnalyzerSession(fakeServer("1.2.0")); + + session.analyze(request("a", "x.jar")); + session.analyze(request("a", "x.jar", "test.jar")); + assertThat(starts()).isEqualTo(1); + assertThat(commands("set-settings")).isEqualTo(2); + + AnalyzerSession.moduleSettings(Map.of("jvm.libraries", List.of("module.jar"))); + session.analyze(request("a", "x.jar", "test.jar")); + session.analyze(request("a", "other.jar")); + assertThat(commands("set-settings")).isEqualTo(3); + } + + private Path fakeServer(String protocol) throws IOException { + Path script = temp.resolve("fake-server.sh"); + Files.writeString(script, String.join("\n", + "#!/bin/sh", + "echo start >> '" + temp.resolve("log") + "'", + "while read line; do", + " echo \"$line\" >> '" + temp.resolve("log") + "'", + " echo '{\"ok\":true,\"protocol\":\"" + protocol + "\"}'", + "done", + "")); + assertThat(script.toFile().setExecutable(true)).isTrue(); + return script; + } + + private JsonObject request(String module, String... classpath) { + Path root = temp.resolve(module); + AnalyzerSession.moduleRoot(root); + JsonObject request = new JsonObject(); + request.addProperty("root", root.toString()); + request.add("files", new JsonArray()); + JsonArray entries = new JsonArray(); + List.of(classpath).forEach(entries::add); + request.add("classpath", entries); + return request; + } + + private long starts() throws IOException { + return Files.readAllLines(temp.resolve("log")).stream().filter("start"::equals).count(); + } + + private long commands(String name) throws IOException { + return Files.readAllLines(temp.resolve("log")).stream().filter(line -> line.contains("\"cmd\":\"" + name + "\"")).count(); + } +} diff --git a/java-frontend/src/test/java/org/sonar/java/semsitter/EmbeddedUdgGenTest.java b/java-frontend/src/test/java/org/sonar/java/semsitter/EmbeddedUdgGenTest.java new file mode 100644 index 00000000000..ee6b83815bb --- /dev/null +++ b/java-frontend/src/test/java/org/sonar/java/semsitter/EmbeddedUdgGenTest.java @@ -0,0 +1,53 @@ +/* + * SonarQube Java + * Copyright (C) SonarSource Sàrl + * mailto:info AT sonarsource DOT com + * + * You can redistribute and/or modify this program under the terms of + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. + * + * This program 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 Sonar Source-Available License for more details. + * + * You should have received a copy of the Sonar Source-Available License + * along with this program; if not, see https://sonarsource.com/license/ssal/ + */ +package org.sonar.java.semsitter; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledOnOs; +import org.junit.jupiter.api.condition.OS; + +import static org.assertj.core.api.Assertions.assertThat; + +class EmbeddedUdgGenTest { + + @Test + void packaged_platforms_follow_the_detected_os_and_architecture() { + assertThat(EmbeddedUdgGen.resourceName("Linux", "amd64")).isEqualTo("/udg-gen/udg-gen-linux-x64.xz"); + assertThat(EmbeddedUdgGen.resourceName("Linux", "x86_64")).isEqualTo("/udg-gen/udg-gen-linux-x64.xz"); + assertThat(EmbeddedUdgGen.resourceName("Linux", "aarch64")).isEqualTo("/udg-gen/udg-gen-linux-aarch64.xz"); + assertThat(EmbeddedUdgGen.resourceName("Mac OS X", "aarch64")).isEqualTo("/udg-gen/udg-gen-macos-aarch64.xz"); + assertThat(EmbeddedUdgGen.resourceName("Mac OS X", "x86_64")).isEqualTo("/udg-gen/udg-gen-macos-x64.xz"); + assertThat(EmbeddedUdgGen.resourceName("Windows 11", "amd64")).isEqualTo("/udg-gen/udg-gen-windows-x64.xz"); + assertThat(EmbeddedUdgGen.resourceName("Windows 11", "aarch64")).isNull(); + assertThat(EmbeddedUdgGen.resourceName("FreeBSD", "amd64")).isNull(); + assertThat(EmbeddedUdgGen.resourceName(null, null)).isNull(); + } + + @Test + @EnabledOnOs({OS.LINUX, OS.MAC, OS.WINDOWS}) + void extracts_an_executable_for_the_running_platform() throws IOException { + if (EmbeddedUdgGen.resourceName(System.getProperty("os.name"), System.getProperty("os.arch")) == null) { + return; + } + Path executable = EmbeddedUdgGen.extract(); + assertThat(executable).isExecutable(); + assertThat(Files.size(executable)).isGreaterThan(1_000_000L); + } +} diff --git a/java-frontend/src/test/java/org/sonar/java/semsitter/SemsitterBridgeTest.java b/java-frontend/src/test/java/org/sonar/java/semsitter/SemsitterBridgeTest.java deleted file mode 100644 index cb681c218b1..00000000000 --- a/java-frontend/src/test/java/org/sonar/java/semsitter/SemsitterBridgeTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ -package org.sonar.java.semsitter; - -import com.google.gson.JsonObject; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assumptions.assumeTrue; - -class SemsitterBridgeTest { - - private Path executable; - - @BeforeEach - void locateBridge() { - String configured = System.getenv("SEMSITTER_BRIDGE"); - assumeTrue(configured != null && Files.isExecutable(Path.of(configured))); - executable = Path.of(configured); - } - - @Test - void analyze_then_cst(@TempDir Path root) throws IOException { - Path file = Files.writeString(root.resolve("A.java"), "class A { int f; int g() { return f; } }"); - try (SemsitterBridge bridge = SemsitterBridge.start(executable)) { - JsonObject analysis = bridge.analyze(root, List.of(file), List.of()); - assertThat(analysis.getAsJsonArray("files").get(0).getAsString()).isEqualTo("A.java"); - JsonObject cst = bridge.cst(file.toString()); - assertThat(cst.getAsJsonObject("tree").get("k").getAsString()).isEqualTo("program"); - assertThat(cst.getAsJsonArray("symbols")).isNotEmpty(); - assertThat(cst.getAsJsonObject("facts").has("A#g()")).isTrue(); - } - } - - @Test - void errors_are_reported_and_the_bridge_survives() throws IOException { - try (SemsitterBridge bridge = SemsitterBridge.start(executable)) { - assertThatThrownBy(() -> bridge.cst("A.java")).isInstanceOf(IOException.class).hasMessageContaining("no analyze request yet"); - assertThat(bridge.parse("class B {}").get("hasError").getAsBoolean()).isFalse(); - } - } -} diff --git a/semsitter-bridge/.gitignore b/semsitter-bridge/.gitignore deleted file mode 100644 index b83d22266ac..00000000000 --- a/semsitter-bridge/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/semsitter-bridge/Cargo.lock b/semsitter-bridge/Cargo.lock deleted file mode 100644 index 84b6c264565..00000000000 --- a/semsitter-bridge/Cargo.lock +++ /dev/null @@ -1,1756 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "adler2" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" - -[[package]] -name = "aho-corasick" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" -dependencies = [ - "memchr", -] - -[[package]] -name = "anstream" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" -dependencies = [ - "anstyle", - "anstyle-parse", - "anstyle-query", - "anstyle-wincon", - "colorchoice", - "is_terminal_polyfill", - "utf8parse", -] - -[[package]] -name = "anstyle" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" - -[[package]] -name = "anstyle-parse" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" -dependencies = [ - "utf8parse", -] - -[[package]] -name = "anstyle-query" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" -dependencies = [ - "windows-sys 0.61.2", -] - -[[package]] -name = "anstyle-wincon" -version = "3.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" -dependencies = [ - "anstyle", - "once_cell_polyfill", - "windows-sys 0.61.2", -] - -[[package]] -name = "anyhow" -version = "1.0.104" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" - -[[package]] -name = "bitflags" -version = "2.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" - -[[package]] -name = "block-buffer" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" -dependencies = [ - "generic-array", -] - -[[package]] -name = "bstr" -version = "1.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bb31b46c14244e20ee9984b11bf5c992b91fb6939fea616e3512c8baecdbe5f" -dependencies = [ - "memchr", - "serde_core", -] - -[[package]] -name = "bumpalo" -version = "3.20.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" - -[[package]] -name = "bytes" -version = "1.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" - -[[package]] -name = "cc" -version = "1.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86e055fa6e3e40ad6187ba25b6855eeb635d5271f8e0f0e1e9a5826b8d75bea0" -dependencies = [ - "find-msvc-tools", - "shlex", -] - -[[package]] -name = "cfg-if" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" - -[[package]] -name = "cfg_aliases" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" - -[[package]] -name = "clap" -version = "4.6.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946" -dependencies = [ - "clap_builder", - "clap_derive", -] - -[[package]] -name = "clap_builder" -version = "4.6.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d" -dependencies = [ - "anstream", - "anstyle", - "clap_lex", - "strsim", -] - -[[package]] -name = "clap_derive" -version = "4.6.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0" -dependencies = [ - "heck", - "proc-macro2", - "quote", - "syn 3.0.9", -] - -[[package]] -name = "clap_lex" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486" - -[[package]] -name = "colorchoice" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" - -[[package]] -name = "core_detect" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" - -[[package]] -name = "cpufeatures" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" -dependencies = [ - "libc", -] - -[[package]] -name = "crc32fast" -version = "1.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ba106e844045ddc02c20768ac4b0b7cb45f2cc2ecbd833ac9f5dacb183c2055" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "crossbeam-deque" -version = "0.8.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a" -dependencies = [ - "crossbeam-epoch", - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-epoch" -version = "0.9.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" -dependencies = [ - "crossbeam-utils", -] - -[[package]] -name = "crossbeam-utils" -version = "0.8.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" - -[[package]] -name = "crypto-common" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" -dependencies = [ - "generic-array", - "typenum", -] - -[[package]] -name = "digest" -version = "0.10.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" -dependencies = [ - "block-buffer", - "crypto-common", -] - -[[package]] -name = "dyn-clone" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" - -[[package]] -name = "either" -version = "1.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" - -[[package]] -name = "encoding_rs" -version = "0.8.42" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e985e0451871ad22fb8d2b6b076e2028a502a0d3950998c2c5c0a4f9b5d9679" -dependencies = [ - "cfg-if", - "core_detect", - "multiversion_no_op", - "rustversion", - "scopeguard", - "simdutf8", -] - -[[package]] -name = "equivalent" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" - -[[package]] -name = "errno" -version = "0.3.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" -dependencies = [ - "libc", - "windows-sys 0.61.2", -] - -[[package]] -name = "fastrand" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" - -[[package]] -name = "file-id" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1fc6a637b6dc58414714eddd9170ff187ecb0933d4c7024d1abbd23a3cc26e9" -dependencies = [ - "windows-sys 0.60.2", -] - -[[package]] -name = "find-msvc-tools" -version = "0.1.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" - -[[package]] -name = "flate2" -version = "1.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb" -dependencies = [ - "crc32fast", - "miniz_oxide", - "zlib-rs", -] - -[[package]] -name = "foldhash" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" - -[[package]] -name = "fswatch" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "ignore", - "notify-debouncer-full", - "tracing", -] - -[[package]] -name = "generic-array" -version = "0.14.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" -dependencies = [ - "typenum", - "version_check", -] - -[[package]] -name = "getrandom" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" -dependencies = [ - "cfg-if", - "js-sys", - "libc", - "r-efi", - "wasm-bindgen", -] - -[[package]] -name = "globset" -version = "0.4.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07c34a9410465b45bd9787443bc7370f37735bad04b0f0cd57ff1a3186c98988" -dependencies = [ - "aho-corasick", - "bstr", - "log", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "hashbrown" -version = "0.17.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" - -[[package]] -name = "heck" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" - -[[package]] -name = "ignore" -version = "0.4.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b69833ed729dc5aa7d19541d96d6cf8e9137194207a04916d658e43168402f" -dependencies = [ - "crossbeam-deque", - "globset", - "log", - "memchr", - "regex-automata", - "same-file", - "walkdir", - "winapi-util", -] - -[[package]] -name = "indexmap" -version = "2.14.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" -dependencies = [ - "equivalent", - "hashbrown", -] - -[[package]] -name = "inotify" -version = "0.11.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cc00ea907cab49550b7da656f80ebb97be1b997d931fbcd28d39734e17ce592" -dependencies = [ - "bitflags", - "inotify-sys", - "libc", -] - -[[package]] -name = "inotify-sys" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c033f80b2c113cdf91ab7a33faa9cbc014726dcad99880c8609af2a370edf37d" -dependencies = [ - "libc", -] - -[[package]] -name = "is_terminal_polyfill" -version = "1.70.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" - -[[package]] -name = "itertools" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" -dependencies = [ - "either", -] - -[[package]] -name = "itoa" -version = "1.0.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" - -[[package]] -name = "js-sys" -version = "0.3.106" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7883d941dae510fb2d978fc3fe018c71c9e2892fd38854de3e8b92c2e5ad9cc5" -dependencies = [ - "cfg-if", - "wasm-bindgen", -] - -[[package]] -name = "kqueue" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d763e5b24120b4ddf50de6c92308156765aabfbbccebf401da7cff2d70a41ea" -dependencies = [ - "kqueue-sys", - "libc", -] - -[[package]] -name = "kqueue-sys" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07293a4e297ac234359b510362495713f75ea345d5307140414f20c69ffeb087" -dependencies = [ - "bitflags", - "libc", -] - -[[package]] -name = "lazy_static" -version = "1.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" - -[[package]] -name = "libc" -version = "0.2.191" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" - -[[package]] -name = "libmimalloc-sys" -version = "0.1.49" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a45a52f43e1c16f667ccfe4dd8c85b7f7c204fd5e3bf46c5b0db9a5c3c0b8e9" -dependencies = [ - "cc", -] - -[[package]] -name = "linux-raw-sys" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" - -[[package]] -name = "log" -version = "0.4.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6" - -[[package]] -name = "matchers" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" -dependencies = [ - "regex-automata", -] - -[[package]] -name = "memchr" -version = "2.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" - -[[package]] -name = "memmap2" -version = "0.9.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b0ca1155a9ecc25aa12e8313a119f32edf0255fea2f44839c4ac0a12b2ac157" -dependencies = [ - "libc", -] - -[[package]] -name = "mimalloc" -version = "0.1.52" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d4139bb28d14ad1facf21d5eb8825051b326e172d216b39f6d31df53cc97862" -dependencies = [ - "libmimalloc-sys", -] - -[[package]] -name = "miniz_oxide" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" -dependencies = [ - "adler2", - "simd-adler32", -] - -[[package]] -name = "mio" -version = "1.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee" -dependencies = [ - "libc", - "log", - "wasi", - "windows-sys 0.61.2", -] - -[[package]] -name = "multiversion_no_op" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d" - -[[package]] -name = "nix" -version = "0.31.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf20d2fde8ff38632c426f1165ed7436270b44f199fc55284c38276f9db47c3d" -dependencies = [ - "bitflags", - "cfg-if", - "cfg_aliases", - "libc", -] - -[[package]] -name = "notify" -version = "8.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82f812702fbc75d35416d3f884e04925be7e9498f4eceea2e24ba34559284674" -dependencies = [ - "bitflags", - "inotify", - "kqueue", - "libc", - "log", - "mio", - "notify-types", - "objc2-core-foundation", - "objc2-core-services", - "walkdir", - "windows-sys 0.61.2", -] - -[[package]] -name = "notify-debouncer-full" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c02b49179cfebc9932238d04d6079912d26de0379328872846118a0fa0dbb302" -dependencies = [ - "file-id", - "log", - "notify", - "notify-types", - "walkdir", -] - -[[package]] -name = "notify-types" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42b8cfee0e339a0337359f3c88165702ac6e600dc01c0cc9579a92d62b08477a" -dependencies = [ - "bitflags", -] - -[[package]] -name = "nu-ansi-term" -version = "0.50.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" -dependencies = [ - "windows-sys 0.61.2", -] - -[[package]] -name = "objc2-core-foundation" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536" -dependencies = [ - "bitflags", -] - -[[package]] -name = "objc2-core-services" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583300ad934cba24ff5292aee751ecc070f7ca6b39a574cc21b7b5e588e06a0b" -dependencies = [ - "libc", - "objc2-core-foundation", -] - -[[package]] -name = "once_cell" -version = "1.21.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" - -[[package]] -name = "once_cell_polyfill" -version = "1.70.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" - -[[package]] -name = "pin-project-lite" -version = "0.2.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" - -[[package]] -name = "proc-macro2" -version = "1.0.107" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "prost" -version = "0.14.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "528ac67416ff8646872a3c02cad9cc4ee5dc9f9540c9b10771855c95cb2e5ae1" -dependencies = [ - "bytes", - "prost-derive", -] - -[[package]] -name = "prost-derive" -version = "0.14.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b570b25f7617e43d59005d0990ccb79e950a423952cea19671b7a876da390adf" -dependencies = [ - "anyhow", - "itertools", - "proc-macro2", - "quote", - "syn 2.0.119", -] - -[[package]] -name = "pulldown-cmark" -version = "0.13.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "278e9268c93b9e498a2da46c34d90ba93a3255eb99009646398f34bc73ab36eb" -dependencies = [ - "bitflags", - "memchr", - "unicase", -] - -[[package]] -name = "quick-xml" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41b1177fdf999d2321d3fb46ff47159d9c1fb9ad66a4879f8c50a0b504615e9b" -dependencies = [ - "memchr", -] - -[[package]] -name = "quote" -version = "1.0.47" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "r-efi" -version = "6.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" - -[[package]] -name = "rayon" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" -dependencies = [ - "either", - "rayon-core", -] - -[[package]] -name = "rayon-core" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" -dependencies = [ - "crossbeam-deque", - "crossbeam-utils", -] - -[[package]] -name = "ref-cast" -version = "1.0.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e440fb4e4b4147295338efb76001ab9e4efc0e5839df2c47fc5ac2381d365c3" -dependencies = [ - "ref-cast-impl", -] - -[[package]] -name = "ref-cast-impl" -version = "1.0.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92ecd8964f8453721699a1ed72037b0db49ce2f5a5138486ee89bed6f67cdf3a" -dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.9", -] - -[[package]] -name = "regex" -version = "1.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata", - "regex-syntax", -] - -[[package]] -name = "regex-automata" -version = "0.4.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.8.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" - -[[package]] -name = "rustc-hash" -version = "2.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" - -[[package]] -name = "rustix" -version = "1.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys 0.61.2", -] - -[[package]] -name = "rustversion" -version = "1.0.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" - -[[package]] -name = "same-file" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" -dependencies = [ - "winapi-util", -] - -[[package]] -name = "schemars" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab508826f74a77ca9d5aba6ff19b522583ee3eaf28a19384ff3d0e5835fadf6e" -dependencies = [ - "dyn-clone", - "ref-cast", - "schemars_derive", - "serde", - "serde_json", -] - -[[package]] -name = "schemars_derive" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1c3a92094fa7d61aa124645844facb6b554dfc797136d0f5fd1f890e2bffc69" -dependencies = [ - "proc-macro2", - "quote", - "serde_derive_internals", - "syn 3.0.9", -] - -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - -[[package]] -name = "semsitter-bridge" -version = "0.1.0" -dependencies = [ - "anyhow", - "memmap2", - "serde_json", - "tempfile", - "tree-sitter", - "tree-sitter-java", - "udg-family-jvm", - "udg-gen", - "udg-lang-sdk", - "udg-semantic", -] - -[[package]] -name = "serde" -version = "1.0.229" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" -dependencies = [ - "serde_core", - "serde_derive", -] - -[[package]] -name = "serde_core" -version = "1.0.229" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.229" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" -dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.9", -] - -[[package]] -name = "serde_derive_internals" -version = "0.30.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f852137cce035d6a4df67ccce505ff6b3e9fd3a10e3e52b24dc71e650bb1a9bd" -dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.9", -] - -[[package]] -name = "serde_json" -version = "1.0.154" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" -dependencies = [ - "foldhash", - "indexmap", - "itoa", - "memchr", - "serde", - "serde_core", - "zmij", -] - -[[package]] -name = "serde_spanned" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e7523beb55eece201a2356bee0bbca0d1ab466c14c07703b2e0ee6d42cb0c2c" -dependencies = [ - "serde_core", -] - -[[package]] -name = "sha2" -version = "0.10.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest", -] - -[[package]] -name = "sharded-slab" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" -dependencies = [ - "lazy_static", -] - -[[package]] -name = "shlex" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" - -[[package]] -name = "simd-adler32" -version = "0.3.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" - -[[package]] -name = "simdutf8" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" - -[[package]] -name = "smallvec" -version = "1.16.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0d5d4b1981b8bbbb7aed204cae5726d683d5d6ac7e74ec54122ea28f315e7c8" - -[[package]] -name = "streaming-iterator" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b2231b7c3057d5e4ad0156fb3dc807d900806020c5ffa3ee6ff2c8c76fb8520" - -[[package]] -name = "strsim" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" - -[[package]] -name = "syn" -version = "2.0.119" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "syn" -version = "3.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "tempfile" -version = "3.27.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" -dependencies = [ - "fastrand", - "getrandom", - "once_cell", - "rustix", - "windows-sys 0.61.2", -] - -[[package]] -name = "thread_local" -version = "1.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "toml" -version = "1.1.8+spec-1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20489e00e4d8741d6be680764cc12e270655e375a20d1011e844a9c3379e678d" -dependencies = [ - "indexmap", - "serde_core", - "serde_spanned", - "toml_datetime", - "toml_parser", - "toml_writer", - "winnow", -] - -[[package]] -name = "toml_datetime" -version = "1.1.2+spec-1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b86d767906c6c42421dcba507eb9d203e779497710a47782a224bb871653053" -dependencies = [ - "serde_core", -] - -[[package]] -name = "toml_parser" -version = "1.1.5+spec-1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baa693a8032d7e1cada7d0041e96126df243179ff061456783ac7f12bda4744c" -dependencies = [ - "winnow", -] - -[[package]] -name = "toml_writer" -version = "1.1.3+spec-1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06bdbd8cfc056b8d2e2e85f29b56a3bdbecb527cef81eb39e3e7b98af4652770" - -[[package]] -name = "tracing" -version = "0.1.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" -dependencies = [ - "pin-project-lite", - "tracing-attributes", - "tracing-core", -] - -[[package]] -name = "tracing-attributes" -version = "0.1.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.119", -] - -[[package]] -name = "tracing-core" -version = "0.1.36" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" -dependencies = [ - "once_cell", - "valuable", -] - -[[package]] -name = "tracing-log" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" -dependencies = [ - "log", - "once_cell", - "tracing-core", -] - -[[package]] -name = "tracing-subscriber" -version = "0.3.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" -dependencies = [ - "matchers", - "nu-ansi-term", - "once_cell", - "regex-automata", - "sharded-slab", - "smallvec", - "thread_local", - "tracing", - "tracing-core", - "tracing-log", -] - -[[package]] -name = "tree-sitter" -version = "0.27.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5129de2ccc97808c4583d48f1fb44576d015cf241e432800572bda11ef30c3b" -dependencies = [ - "cc", - "regex", - "serde_json", - "streaming-iterator", - "tree-sitter-language", -] - -[[package]] -name = "tree-sitter-java" -version = "0.23.5" -source = "git+https://github.com/rombirli/tree-sitter-java?rev=324af3f22ee0a4579a1d5c1a8bc588631ec4d17c#324af3f22ee0a4579a1d5c1a8bc588631ec4d17c" -dependencies = [ - "cc", - "tree-sitter-language", -] - -[[package]] -name = "tree-sitter-language" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0af592be68c579aa78a16846bd19422978c3c52e438523d45ff5d1bff1f9d4a" - -[[package]] -name = "typed-path" -version = "0.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ce898823d8cd0f028662d2f349694b1af0a5cc9be83f1d213a00d3147445e1e" - -[[package]] -name = "typenum" -version = "1.20.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" - -[[package]] -name = "udg-boundary" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "flate2", - "serde", - "serde_json", - "sha2", - "tracing", - "udg-engine", - "udg-model", -] - -[[package]] -name = "udg-buffer" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "rustc-hash", - "tracing", - "udg-lang-sdk", - "udg-model", -] - -[[package]] -name = "udg-embed" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "clap", - "rustc-hash", - "serde", - "serde_json", - "tracing", - "udg-engine", - "udg-lang-sdk", - "udg-model", - "udg-sdk-tree-sitter", - "udg-store", -] - -[[package]] -name = "udg-engine" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "ignore", - "pulldown-cmark", - "rayon", - "regex", - "rustc-hash", - "serde", - "serde_json", - "tracing", - "tree-sitter", - "udg-buffer", - "udg-lang-sdk", - "udg-model", - "udg-sdk-tree-sitter", -] - -[[package]] -name = "udg-engine-plugins" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "rayon", - "tracing", - "tree-sitter", - "udg-engine", - "udg-family-jvm", - "udg-lang-java", - "udg-lang-sdk", - "udg-model", - "udg-sdk-tree-sitter", -] - -[[package]] -name = "udg-family-jvm" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "encoding_rs", - "globset", - "quick-xml", - "rustc-hash", - "serde", - "serde_json", - "toml", - "tracing", - "udg-lang-sdk", - "udg-model", - "udg-packed-table", - "zip", -] - -[[package]] -name = "udg-gen" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "clap", - "mimalloc", - "prost", - "rustc-hash", - "serde", - "serde_json", - "toml", - "tracing", - "tracing-subscriber", - "tree-sitter", - "tree-sitter-java", - "udg-boundary", - "udg-embed", - "udg-engine", - "udg-engine-plugins", - "udg-family-jvm", - "udg-lang-java", - "udg-lang-sdk", - "udg-live", - "udg-live-serve", - "udg-model", - "udg-semantic", - "udg-semantic-session", - "udg-store", - "udg-ucfg", -] - -[[package]] -name = "udg-lang-java" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "rustc-hash", - "serde", - "serde_json", - "tracing", - "tree-sitter", - "tree-sitter-java", - "udg-family-jvm", - "udg-lang-sdk", - "udg-model", - "udg-packed-table", - "udg-sdk-tree-sitter", - "udg-semantic-core", -] - -[[package]] -name = "udg-lang-sdk" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "encoding_rs", - "rustc-hash", - "serde", - "udg-model", -] - -[[package]] -name = "udg-live" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "clap", - "serde", - "serde_json", - "tracing", - "udg-embed", - "udg-engine", - "udg-lang-sdk", - "udg-model", - "udg-store", -] - -[[package]] -name = "udg-live-serve" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "fswatch", - "ignore", - "prost", - "schemars", - "serde", - "serde_json", - "tracing", - "udg-embed", - "udg-engine", - "udg-lang-sdk", - "udg-live", - "udg-model", - "udg-store", -] - -[[package]] -name = "udg-model" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "clap", - "rustc-hash", - "schemars", - "serde", - "serde_json", - "tracing", - "tracing-subscriber", -] - -[[package]] -name = "udg-packed-table" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "flate2", -] - -[[package]] -name = "udg-sdk-tree-sitter" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "rustc-hash", - "serde_json", - "tree-sitter", - "udg-lang-sdk", - "udg-model", -] - -[[package]] -name = "udg-semantic" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "udg-lang-sdk", - "udg-model", - "udg-semantic-core", - "udg-store", -] - -[[package]] -name = "udg-semantic-core" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "serde_json", - "udg-lang-sdk", - "udg-model", -] - -[[package]] -name = "udg-semantic-session" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "rayon", - "tracing", - "tree-sitter", - "udg-engine", - "udg-lang-java", - "udg-lang-sdk", - "udg-model", - "udg-semantic", -] - -[[package]] -name = "udg-store" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "clap", - "ignore", - "memmap2", - "prost", - "rayon", - "regex", - "rustc-hash", - "schemars", - "serde", - "serde_json", - "tracing", - "udg-model", -] - -[[package]] -name = "udg-ucfg" -version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" -dependencies = [ - "anyhow", - "clap", - "prost", - "regex", - "serde", - "serde_json", - "sha2", - "tracing", - "tree-sitter", - "tree-sitter-java", - "udg-engine", - "udg-engine-plugins", - "udg-family-jvm", - "udg-lang-java", - "udg-lang-sdk", - "udg-model", - "udg-sdk-tree-sitter", - "udg-semantic", - "zip", -] - -[[package]] -name = "unicase" -version = "2.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "357cc3acc6a036009fd6c973ed009037c732d60d0b4f6c673e9041497482a28f" - -[[package]] -name = "unicode-ident" -version = "1.0.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" - -[[package]] -name = "utf8parse" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" - -[[package]] -name = "valuable" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" - -[[package]] -name = "version_check" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" - -[[package]] -name = "walkdir" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" -dependencies = [ - "same-file", - "winapi-util", -] - -[[package]] -name = "wasi" -version = "0.11.1+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" - -[[package]] -name = "wasm-bindgen" -version = "0.2.129" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bb54f33acc68fd454578d9820b0bde1a1a3d17aa17bb7b6595806d02886d409" -dependencies = [ - "cfg-if", - "once_cell", - "rustversion", - "wasm-bindgen-macro", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.129" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e29d0c35b16e224a7eeb5cd2d25e3e1968fbd65604117b44d3b789d00ee8535" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.129" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f501a8bc3719dba86ef8ae4728879c08001bea749eb1333ac5b91e040e2a6b7" -dependencies = [ - "bumpalo", - "proc-macro2", - "quote", - "syn 3.0.9", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.129" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23f0c9c52aa7cd7d77769a4cfe2a9adb1b331f489a41d912ce14513d5ab995c6" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "winapi-util" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" -dependencies = [ - "windows-sys 0.61.2", -] - -[[package]] -name = "windows-link" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" - -[[package]] -name = "windows-sys" -version = "0.60.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-sys" -version = "0.61.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" -dependencies = [ - "windows-link", -] - -[[package]] -name = "windows-targets" -version = "0.53.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" -dependencies = [ - "windows-link", - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" - -[[package]] -name = "windows_i686_gnu" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" - -[[package]] -name = "windows_i686_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" - -[[package]] -name = "winnow" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23b97319f7b8343df12cc98938e5c3eb436064524c8d2b4e30a1d3a36eecdf81" - -[[package]] -name = "zip" -version = "9.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec2e3161de27091f18a831d9494e9c53378a34833af5c1dda35ad85849da18d6" -dependencies = [ - "crc32fast", - "flate2", - "getrandom", - "indexmap", - "memchr", - "nix", - "typed-path", - "windows-sys 0.61.2", - "zopfli", -] - -[[package]] -name = "zlib-rs" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" - -[[package]] -name = "zmij" -version = "1.0.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" - -[[package]] -name = "zopfli" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aaf7fc5d30c28483d93805c4a5e12b05bbb52407fa67c5f8bd552374cd01fb11" -dependencies = [ - "bumpalo", - "crc32fast", - "log", - "simd-adler32", -] diff --git a/semsitter-bridge/Cargo.toml b/semsitter-bridge/Cargo.toml deleted file mode 100644 index 9a47e4ed7dd..00000000000 --- a/semsitter-bridge/Cargo.toml +++ /dev/null @@ -1,32 +0,0 @@ -[package] -name = "semsitter-bridge" -version = "0.1.0" -edition = "2024" -publish = false -description = "Java CST with SemSitter semantics, served over JSONL to the SonarJava frontend." - -[[bin]] -name = "semsitter-bridge" -path = "src/main.rs" - -[dependencies] -anyhow = "1" -memmap2 = "0.9" -serde_json = "1" -tree-sitter = "0.27" -tree-sitter-java = "0.23.5" -udg-gen = { git = "ssh://git@github.com/SonarSource/semsitter.git", rev = "22d846d83268f460e68912d1fd3ebb8843bafce9", default-features = false, features = ["lang-java"] } -udg-family-jvm = { git = "ssh://git@github.com/SonarSource/semsitter.git", rev = "22d846d83268f460e68912d1fd3ebb8843bafce9" } -udg-lang-sdk = { git = "ssh://git@github.com/SonarSource/semsitter.git", rev = "22d846d83268f460e68912d1fd3ebb8843bafce9", features = ["unstable-sdk"] } -udg-semantic = { git = "ssh://git@github.com/SonarSource/semsitter.git", rev = "22d846d83268f460e68912d1fd3ebb8843bafce9" } - -[patch.crates-io] -tree-sitter-java = { git = "https://github.com/rombirli/tree-sitter-java", rev = "324af3f22ee0a4579a1d5c1a8bc588631ec4d17c" } - -[dev-dependencies] -tempfile = "3" - -[profile.release] -lto = "thin" -codegen-units = 4 -strip = true diff --git a/semsitter-bridge/archive-bridge.sh b/semsitter-bridge/archive-bridge.sh deleted file mode 100644 index 95b33bce1c9..00000000000 --- a/semsitter-bridge/archive-bridge.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env bash -# Regenerates prebuilt/bridge--.xz, packaged in the plugin JAR so CI needs neither cargo nor -# access to the private semsitter repository. Run on an Apple silicon Mac with Docker and SSH access to GitHub. -set -euo pipefail - -cd "$(dirname "$0")" -work=$(mktemp -d) -trap 'rm -rf "$work"' EXIT - -export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=url.ssh://git@github.com/.insteadOf GIT_CONFIG_VALUE_0=https://github.com/ -export CARGO_NET_GIT_FETCH_WITH_CLI=true -target_dir="${CARGO_TARGET_DIR:-$PWD/target}" - -cargo vendor --locked "$work/vendor" | sed "s#$work/vendor#/vendor#" > "$work/config.toml" -mkdir -p "$work/out" - -docker_build() { - local platform=$1 image=$2 setup=$3 target=$4 out=$5 - docker run --rm --platform "$platform" \ - -v "$PWD:/src:ro" -v "$work/vendor:/vendor:ro" -v "$work/config.toml:/usr/local/cargo/config.toml:ro" -v "$work/out:/out" \ - -e CARGO_TARGET_DIR=/tmp/target "$image" \ - sh -c "$setup && cd /src && cargo build --release --locked --offline $target && cp \$(ls /tmp/target/${target:+${target#--target }/}release/semsitter-bridge /tmp/target/${target:+${target#--target }/}release/semsitter-bridge.exe 2>/dev/null) /out/$out" -} - -docker_build linux/amd64 rust:1-alpine "apk add --no-cache musl-dev" "" linux-x64 -docker_build linux/arm64 rust:1-alpine "apk add --no-cache musl-dev" "" linux-aarch64 -docker_build linux/arm64 rust:1 "apt-get update -qq && apt-get install -y -qq gcc-mingw-w64-x86-64 && rustup target add x86_64-pc-windows-gnu" \ - "--target x86_64-pc-windows-gnu" windows-x64 - -rustup target add x86_64-apple-darwin -cargo build --release --locked -cargo build --release --locked --target x86_64-apple-darwin -cp "$target_dir/release/semsitter-bridge" "$work/out/macos-aarch64" -cp "$target_dir/x86_64-apple-darwin/release/semsitter-bridge" "$work/out/macos-x64" - -mkdir -p prebuilt -for os in linux-x64 linux-aarch64 macos-x64 macos-aarch64 windows-x64; do - bin=bridge - [[ $os == windows-x64 ]] && bin=bridge.exe - mkdir -p "$work/$os" - install -m 755 "$work/out/$os" "$work/$os/$bin" - tar -cJf "prebuilt/bridge-$os.xz" -C "$work/$os" "$bin" -done diff --git a/semsitter-bridge/prebuilt/bridge-linux-aarch64.xz b/semsitter-bridge/prebuilt/bridge-linux-aarch64.xz deleted file mode 100644 index 2c07943f766..00000000000 Binary files a/semsitter-bridge/prebuilt/bridge-linux-aarch64.xz and /dev/null differ diff --git a/semsitter-bridge/prebuilt/bridge-linux-x64.xz b/semsitter-bridge/prebuilt/bridge-linux-x64.xz deleted file mode 100644 index 18a59653df8..00000000000 Binary files a/semsitter-bridge/prebuilt/bridge-linux-x64.xz and /dev/null differ diff --git a/semsitter-bridge/prebuilt/bridge-macos-aarch64.xz b/semsitter-bridge/prebuilt/bridge-macos-aarch64.xz deleted file mode 100644 index 4a6e2eaccd3..00000000000 Binary files a/semsitter-bridge/prebuilt/bridge-macos-aarch64.xz and /dev/null differ diff --git a/semsitter-bridge/prebuilt/bridge-macos-x64.xz b/semsitter-bridge/prebuilt/bridge-macos-x64.xz deleted file mode 100644 index bb3a1f40d74..00000000000 Binary files a/semsitter-bridge/prebuilt/bridge-macos-x64.xz and /dev/null differ diff --git a/semsitter-bridge/prebuilt/bridge-windows-x64.xz b/semsitter-bridge/prebuilt/bridge-windows-x64.xz deleted file mode 100644 index 47e3788d914..00000000000 Binary files a/semsitter-bridge/prebuilt/bridge-windows-x64.xz and /dev/null differ diff --git a/semsitter-bridge/proto/bridge.proto b/semsitter-bridge/proto/bridge.proto deleted file mode 100644 index d1831481b06..00000000000 --- a/semsitter-bridge/proto/bridge.proto +++ /dev/null @@ -1,110 +0,0 @@ -syntax = "proto3"; - -// Contract between the Rust bridge and the Java frontend. Field numbers are the wire format -// implemented by hand in src/wire.rs and BridgeFrames.java, so this file can move into -// SemSitter with the CST builder without a protocol change. -// -// The CST is protobuf. Semantic-dump symbols and the open `facts` object on an occurrence stay -// JSON bytes: that schema is SemSitter's semantic-dump/1. Graph facts projected for SonarJava -// are protobuf. Type declarations are one JSON object, same reason. - -message Request { - uint32 id = 1; - AnalyzeRequest analyze = 2; - CstRequest cst = 3; - TypesRequest types = 4; - ParseRequest parse = 5; - bool quit = 6; -} - -message AnalyzeRequest { - string root = 1; - repeated string files = 2; - repeated string classpath = 3; -} - -message CstRequest { - string path = 1; -} - -message TypesRequest { - repeated string names = 1; -} - -message ParseRequest { - string source = 1; -} - -message Response { - uint32 id = 1; - bool ok = 2; - string error = 3; - AnalyzeResult analyze = 4; - Document document = 5; - string types_json = 6; -} - -message AnalyzeResult { - string root = 1; - repeated string files = 2; -} - -message Document { - bool has_error = 1; - Node tree = 2; - string path = 3; - bool semantic = 4; - string symbols_json = 5; - repeated FactEntry facts = 6; -} - -message FactEntry { - string key = 1; - Fact value = 2; -} - -message Fact { - string kind = 1; - string tier = 2; - string parent = 3; - repeated Prop props = 4; - repeated string extends = 5; - repeated string implements = 6; - repeated string permits = 7; - repeated string returns = 8; - repeated string type = 9; - repeated string params = 10; - repeated string overrides = 11; - repeated string annotations = 12; -} - -message Prop { - string name = 1; - repeated string values = 2; -} - -message Node { - string kind = 1; - bool named = 2; - string field = 3; - bool missing = 4; - uint32 start_line = 5; - uint32 start_col = 6; - uint32 end_line = 7; - uint32 end_col = 8; - uint32 start_byte = 9; - uint32 end_byte = 10; - string text = 11; - repeated Node children = 12; - repeated string decls = 13; - repeated Ref refs = 14; -} - -message Ref { - string access = 1; - string outcome = 2; - string target = 3; - string reason = 4; - bool bounded = 5; - string facts_json = 6; -} diff --git a/semsitter-bridge/src/lib.rs b/semsitter-bridge/src/lib.rs deleted file mode 100644 index 5508c19d55f..00000000000 --- a/semsitter-bridge/src/lib.rs +++ /dev/null @@ -1,641 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ - -//! Java CST annotated with SemSitter semantics. -//! -//! [`analyze`] runs one SemSitter scan over a batch of Java files (with the project classpath) -//! and keeps each file's `semantic-dump/1` in memory, plus the graph facts SonarJava needs on -//! top of it (return types, supertypes, overrides, item kinds and modifiers). -//! [`Analysis::cst`] then parses a file with tree-sitter-java and attaches, by exact byte span, -//! the symbols declared at a name (`decls`) and the resolved occurrences (`refs`). -//! -//! The scan's graph is the UDG. A dump symbol's id is that graph's FQN, and the CST only -//! points at it by id. Transport of those documents lives in [`wire`] and [`shm`] so it can -//! move into SemSitter later without the Java frontend knowing where the bytes were built. - -pub mod server; -pub mod shm; -pub mod types; -pub mod wire; - -use std::collections::{BTreeMap, BTreeSet, HashMap}; -use std::io; -use std::path::{Path, PathBuf}; -use std::sync::Arc; - -use anyhow::{Context, Result, anyhow}; -use serde_json::{Map, Value, json}; -use tree_sitter::{Node, Parser, Tree}; -use udg_gen::overlay_passes::overlay_registry; -use udg_gen::scanner::{ScanOptions, scan_timed_with_registry}; -use udg_gen::semantic_dump::languages; -use udg_gen::udg_model::graph::{DependencyTarget, NodeTier, UdgDependencyKind, UdgGraph}; -use udg_gen::udg_model::source_provider::SourceProvider; -use udg_family_jvm::external::classpath::LazyClasspath; -use udg_lang_sdk::settings::ResolvedSettings; -use udg_semantic::{DumpLanguage, DumpProject, GraphIds, language_of}; - -const BOM: &[u8] = b"\xEF\xBB\xBF"; -pub const STACK_SIZE: usize = 512 * 1024 * 1024; -const GRAPH_EDGES: &[(&str, &str)] = &[ - ("extends", "extends"), - ("implements", "implements"), - ("permits", "permits"), - ("returns", "returns"), - ("is type", "type"), - ("has param", "params"), - ("overrides", "overrides"), - ("annotated by", "annotations"), -]; - -/// Result of one SemSitter scan over a batch of files. -pub struct Analysis { - root: PathBuf, - files: Vec, - dumps: HashMap, - facts: HashMap, - calls: HashMap>, - project_types: types::ProjectTypes, - classpath: Arc, -} - -#[derive(Debug)] -struct ListedSources { - files: Vec, -} - -impl SourceProvider for ListedSources { - fn read(&self, path: &Path) -> io::Result { - Ok(String::from_utf8_lossy(&std::fs::read(path)?).into_owned()) - } - - fn read_bytes(&self, path: &Path) -> io::Result> { - std::fs::read(path) - } - - fn list_files(&self, root: &Path, _project_root: &Path, extensions: &[&str]) -> Vec { - let mut found: Vec = self - .files - .iter() - .filter(|f| f.starts_with(root)) - .filter(|f| { - f.extension() - .and_then(|e| e.to_str()) - .is_some_and(|e| extensions.iter().any(|x| x.trim_start_matches('.').eq_ignore_ascii_case(e))) - }) - .cloned() - .collect(); - found.sort(); - found - } - - fn file_exists(&self, path: &Path) -> bool { - path.is_file() - } -} - -/// Scans `files` (Java sources) as one project rooted at `root`, resolving library types -/// against `classpath` (jars, zips and class directories, declarations only). -pub fn analyze(root: &Path, files: &[PathBuf], classpath: &[String]) -> Result { - analyze_dumping(root, files, classpath, None) -} - -/// [`analyze`], and when `dump` is set writes `udg.json`, each `semantic-dump/1` document and -/// the annotated CST as JSON under that directory. The scan itself stays in memory. -pub fn analyze_dumping(root: &Path, files: &[PathBuf], classpath: &[String], dump: Option<&Path>) -> Result { - let files: Vec = files.iter().filter_map(|f| std::fs::canonicalize(f).ok()).collect(); - let root = project_root(root, &files); - let mut opts = ScanOptions { - src_dirs: vec![root.clone()], - project_root: Some(root.clone()), - source_provider: Some(Arc::new(ListedSources { files: files.clone() })), - ..ScanOptions::default() - }; - opts.extras.modifiers = true; - opts.extras.structural = true; - opts.extras.type_reference_edges = true; - opts.extras.override_edges = true; - opts.extras.annotation_edges = true; - opts.extras.field_reference_edges = true; - opts.extras.decl_location = true; - opts.extras.field_items = true; - - let rel: Vec = files.iter().filter_map(|f| f.strip_prefix(&root).ok()).map(to_slash).collect(); - let mut facts = HashMap::new(); - let mut calls = HashMap::new(); - let mut raw_dumps: HashMap = HashMap::new(); - if !files.is_empty() { - let registry = overlay_registry(); - let (graph, _timings, _failures, _artefacts) = scan_timed_with_registry(&opts, ®istry)?; - facts = graph_facts(&graph); - calls = call_sites(&graph); - let settings = registry - .settings_report() - .unwrap_or_else(|| Arc::clone(ResolvedSettings::empty())); - raw_dumps = memory_dumps(&root, &rel, &graph, settings.as_ref())?; - if let Some(dir) = dump { - write_debug_bundle(dir, &graph, &raw_dumps)?; - } - } - - let dumps = raw_dumps - .iter() - .filter_map(|(path, text)| Some((path.clone(), serde_json::from_str(text).ok()?))) - .collect(); - let project_types = types::ProjectTypes::new(&facts); - let analysis = Analysis { - root, - files: rel, - dumps, - facts, - calls, - project_types, - classpath: shared_classpath(classpath), - }; - if let Some(dir) = dump { - write_cst_json(&analysis, dir)?; - } - Ok(analysis) -} - -/// The classpath of the previous analysis when it lists the same entries, so a client that analyzes -/// one file at a time lists and parses each jar once. -fn shared_classpath(entries: &[String]) -> Arc { - static LAST: std::sync::Mutex, Arc)>> = std::sync::Mutex::new(None); - let mut last = LAST.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - if let Some((listed, classpath)) = last.as_ref() - && listed.as_slice() == entries - { - return Arc::clone(classpath); - } - let classpath = Arc::new(LazyClasspath::new(entries.to_vec())); - *last = Some((entries.to_vec(), Arc::clone(&classpath))); - classpath -} - -fn memory_dumps(root: &Path, files: &[String], graph: &UdgGraph, settings: &ResolvedSettings) -> Result> { - let ids = GraphIds::of(graph); - let mut listed: Vec<&str> = files.iter().map(String::as_str).filter(|r| is_project_relative(r)).collect(); - listed.sort_unstable(); - listed.dedup(); - let plain = languages(); - let project = DumpProject { - graph, - root, - files: &listed, - settings, - expressions: false, - }; - let bound: Vec>> = plain.iter().map(|language| language.for_project(&project)).collect(); - let bound_langs: Vec<&dyn DumpLanguage> = plain - .iter() - .zip(&bound) - .map(|(language, project_language)| project_language.as_deref().unwrap_or(*language)) - .collect(); - let mut dumps = HashMap::new(); - for rel in listed { - let Some(language) = language_of(&bound_langs, rel) else { - continue; - }; - let Ok(stored) = std::fs::read(root.join(rel)) else { - continue; - }; - if let Some(document) = language.dump_stored(rel, stored, ids.file(rel)) { - dumps.insert(rel.to_string(), document); - } - } - Ok(dumps) -} - -fn is_project_relative(rel: &str) -> bool { - let path = Path::new(rel); - !path.is_absolute() - && path.components().all(|c| matches!(c, std::path::Component::Normal(_) | std::path::Component::CurDir)) -} - -fn write_debug_bundle(dir: &Path, graph: &UdgGraph, dumps: &HashMap) -> Result<()> { - std::fs::create_dir_all(dir)?; - let file = std::fs::File::create(dir.join("udg.json")).context("creating udg.json")?; - udg_gen::udg_graph_to_proto_json_writer(graph, udg_gen::EdgeFidelity::Full, file).context("writing udg.json")?; - for (rel, document) in dumps { - let target = dir.join("semdump").join(format!("{rel}.semdump.json")); - if let Some(parent) = target.parent() { - std::fs::create_dir_all(parent)?; - } - std::fs::write(target, document).with_context(|| format!("writing semdump {rel}"))?; - } - Ok(()) -} - -fn write_cst_json(analysis: &Analysis, dir: &Path) -> Result<()> { - use std::io::Write; - for file in analysis.files() { - let target = dir.join("cst").join(format!("{file}.json")); - if let Some(parent) = target.parent() { - std::fs::create_dir_all(parent)?; - } - let mut out = std::fs::File::create(&target).with_context(|| format!("creating {}", target.display()))?; - serde_json::to_writer_pretty(&mut out, &analysis.cst(file)?)?; - writeln!(out)?; - } - Ok(()) -} - -impl Analysis { - pub fn root(&self) -> &Path { - &self.root - } - - /// Project-relative paths (forward slashes) of the analyzed files. - pub fn files(&self) -> &[String] { - &self.files - } - - /// Declarations of the types named `names` (erased binary names) and of their supertypes. - pub fn types(&self, names: &[String]) -> Value { - Value::Object(types::lookup(names, &self.project_types, &self.facts, self.classpath.index())) - } - - /// Resolves an absolute or project-relative path to the key used by [`Self::cst`]. - pub fn relative(&self, path: &str) -> Option { - if self.dumps.contains_key(path) || self.files.iter().any(|f| f == path) { - return Some(path.to_string()); - } - let canonical = std::fs::canonicalize(path).ok()?; - canonical.strip_prefix(&self.root).ok().map(to_slash) - } - - /// CST of one analyzed file with its semantics: tree, declared symbols and the graph facts - /// of every symbol id the file mentions. - pub fn cst(&self, path: &str) -> Result { - let rel = self.relative(path).ok_or_else(|| anyhow!("not analyzed: {path}"))?; - let source = std::fs::read(self.root.join(&rel)).with_context(|| format!("reading {rel}"))?; - let dump = self.dumps.get(&rel); - let calls = self.calls.get(&rel).map(Vec::as_slice).unwrap_or_default(); - let index = SemanticIndex::new(dump, calls, &source); - let mut document = document(&source, &index)?; - let symbols = dump.and_then(|d| d.get("symbols")).cloned().unwrap_or_else(|| json!([])); - let facts: Map = index - .ids - .iter() - .filter_map(|id| self.facts.get(id).map(|f| (id.clone(), f.clone()))) - .collect(); - let object = document.as_object_mut().expect("document is an object"); - object.insert("path".into(), Value::String(rel.clone())); - object.insert("semantic".into(), Value::Bool(dump.is_some())); - object.insert("symbols".into(), symbols); - object.insert("facts".into(), Value::Object(facts)); - Ok(document) - } -} - -/// CST of `source` without semantics. -pub fn parse_only(source: &[u8]) -> Result { - document(source, &SemanticIndex::default()) -} - -fn document(source: &[u8], index: &SemanticIndex) -> Result { - let source = source.to_vec(); - let index = index.clone(); - std::thread::Builder::new() - .stack_size(STACK_SIZE) - .spawn(move || { - let tree = parse(&source)?; - let lines = Lines::new(&source); - let root = tree.root_node(); - Ok(json!({ - "hasError": root.has_error(), - "tree": node_json(root, None, None, &source, &lines, &index), - })) - })? - .join() - .map_err(|_| anyhow!("CST serialization panicked"))? -} - -fn parse(source: &[u8]) -> Result { - let mut parser = Parser::new(); - parser.set_language(&tree_sitter_java::LANGUAGE.into())?; - let tree = if source.starts_with(BOM) { - let mut blanked = source.to_vec(); - blanked[..BOM.len()].fill(b' '); - parser.parse(&blanked, None) - } else { - parser.parse(source, None) - }; - tree.ok_or_else(|| anyhow!("tree-sitter returned no tree")) -} - -/// One CST node. `called` is the graph call target of the enclosing `method_invocation`, used -/// for its `name` when the semantic dump has no occurrence there. -fn node_json( - node: Node, - field: Option<&str>, - called: Option<&str>, - source: &[u8], - lines: &Lines, - index: &SemanticIndex, -) -> Value { - let mut object = Map::new(); - let (start, end) = (node.start_byte(), node.end_byte()); - object.insert("k".into(), Value::String(node.kind().to_string())); - if node.is_named() { - object.insert("n".into(), Value::Bool(true)); - } - if let Some(field) = field { - object.insert("f".into(), Value::String(field.to_string())); - } - if node.is_missing() { - object.insert("m".into(), Value::Bool(true)); - } - let (start_line, start_col) = lines.position(source, start); - let (end_line, end_col) = lines.position(source, end); - object.insert("p".into(), json!([start_line, start_col, end_line, end_col])); - object.insert("b".into(), json!([start, end])); - let call = if node.kind() == "method_invocation" { - index.calls.get(&(start, end)).map(String::as_str) - } else { - None - }; - let mut cursor = node.walk(); - let children: Vec = if cursor.goto_first_child() { - let mut children = Vec::new(); - loop { - let child_field = cursor.field_name(); - let child_call = call.filter(|_| child_field == Some("name")); - children.push(node_json(cursor.node(), child_field, child_call, source, lines, index)); - if !cursor.goto_next_sibling() { - break; - } - } - children - } else { - Vec::new() - }; - if children.is_empty() || is_literal(node.kind()) { - object.insert( - "t".into(), - Value::String(String::from_utf8_lossy(&source[start..end]).into_owned()), - ); - } - if !children.is_empty() { - object.insert("c".into(), Value::Array(children)); - } - if children_free_or_name(node) { - if let Some(decls) = index.decls.get(&(start, end)) { - object.insert("decls".into(), json!(decls)); - } - if let Some(refs) = index.refs.get(&(start, end)) { - object.insert("refs".into(), Value::Array(refs.clone())); - } else if let Some(target) = called { - object.insert( - "refs".into(), - json!([{"access": "r", "outcome": "bound", "target": target, "reason": "graph-call"}]), - ); - } - } - Value::Object(object) -} - -fn children_free_or_name(node: Node) -> bool { - node.child_count() == 0 || matches!(node.kind(), "this" | "super") -} - -fn is_literal(kind: &str) -> bool { - matches!( - kind, - "string_literal" | "character_literal" | "text_block" | "decimal_integer_literal" - | "hex_integer_literal" | "octal_integer_literal" | "binary_integer_literal" - | "decimal_floating_point_literal" | "hex_floating_point_literal" - ) -} - -/// Byte offset to (1-based line, 0-based UTF-16 column), the convention of SonarJava tokens. -struct Lines { - starts: Vec, -} - -impl Lines { - fn new(source: &[u8]) -> Self { - let mut starts = vec![0]; - let mut i = 0; - while i < source.len() { - match source[i] { - b'\n' => starts.push(i + 1), - b'\r' if source.get(i + 1) != Some(&b'\n') => starts.push(i + 1), - _ => {} - } - i += 1; - } - Lines { starts } - } - - fn position(&self, source: &[u8], offset: usize) -> (usize, usize) { - let line = self.starts.partition_point(|&s| s <= offset) - 1; - let prefix = String::from_utf8_lossy(&source[self.starts[line]..offset]); - (line + 1, prefix.trim_start_matches('\u{feff}').encode_utf16().count()) - } - - fn offset(&self, (line, column): (usize, usize)) -> Option { - self.starts.get(line.checked_sub(1)?).map(|start| start + column) - } -} - -/// Semantic dump indexed by byte span. -#[derive(Default, Clone)] -struct SemanticIndex { - decls: HashMap<(usize, usize), Vec>, - refs: HashMap<(usize, usize), Vec>, - calls: HashMap<(usize, usize), String>, - ids: BTreeSet, -} - -impl SemanticIndex { - fn new(dump: Option<&Value>, calls: &[CallSite], source: &[u8]) -> Self { - let mut index = SemanticIndex::default(); - let lines = Lines::new(source); - for call in calls { - if let (Some(start), Some(end)) = (lines.offset(call.start), lines.offset(call.end)) { - index.ids.insert(call.target.clone()); - index.calls.insert((start, end), call.target.clone()); - } - } - let Some(dump) = dump else { - return index; - }; - for symbol in array(dump, "symbols") { - let Some(id) = symbol.get("id").and_then(Value::as_str) else { - continue; - }; - index.ids.insert(id.to_string()); - collect_type_ids(symbol.pointer("/facts/type"), &mut index.ids); - if let Some(span) = span(symbol) { - index.decls.entry(span).or_default().push(id.to_string()); - } - } - for occurrence in array(dump, "occurrences") { - let Some(span) = span(occurrence) else { - continue; - }; - let mut reference = occurrence.clone(); - if let Some(object) = reference.as_object_mut() { - for key in ["start", "end", "name", "line", "col"] { - object.remove(key); - } - } - if let Some(target) = occurrence.get("target").and_then(Value::as_str) { - index.ids.insert(target.to_string()); - } - for candidate in occurrence - .pointer("/facts/candidates") - .and_then(Value::as_array) - .into_iter() - .flatten() - { - if let Some(id) = candidate.as_str().or_else(|| candidate.get("id").and_then(Value::as_str)) { - index.ids.insert(id.to_string()); - } - } - index.refs.entry(span).or_default().push(reference); - } - index - } -} - -fn collect_type_ids(ty: Option<&Value>, ids: &mut BTreeSet) { - let Some(ty) = ty else { - return; - }; - if let Some(id) = ty.get("id").and_then(Value::as_str) { - ids.insert(id.to_string()); - } - collect_type_ids(ty.get("elem"), ids); - for arg in ty.get("args").and_then(Value::as_array).into_iter().flatten() { - collect_type_ids(Some(arg), ids); - } -} - -fn array<'a>(value: &'a Value, key: &str) -> impl Iterator { - value.get(key).and_then(Value::as_array).into_iter().flatten() -} - -fn span(value: &Value) -> Option<(usize, usize)> { - Some(( - value.get("start")?.as_u64()? as usize, - value.get("end")?.as_u64()? as usize, - )) -} - -/// A resolved call edge of the graph, located at its call site (1-based lines, 0-based columns). -#[derive(Clone)] -struct CallSite { - start: (usize, usize), - end: (usize, usize), - target: String, -} - -fn call_sites(graph: &UdgGraph) -> HashMap> { - let mut sites: HashMap> = HashMap::new(); - for fqn in graph.items.keys() { - for dependency in graph.get_downstream_dependencies(fqn.as_str()) { - if dependency.kind != UdgDependencyKind::Calls { - continue; - } - let DependencyTarget::Item(target) = &dependency.to_node else { - continue; - }; - if target.tier == NodeTier::ExternalUnknown { - continue; - } - let Some(location) = &dependency.location else { - continue; - }; - let (Some(end_line), Some(end_offset)) = (location.end_line, location.end_line_offset) else { - continue; - }; - sites.entry(location.file.path.to_string()).or_default().push(CallSite { - start: (location.start_line as usize, location.start_line_offset as usize), - end: (end_line as usize, end_offset as usize), - target: target.fqn.as_str().to_string(), - }); - } - } - sites -} - -fn graph_facts(graph: &UdgGraph) -> HashMap { - graph - .items - .iter() - .map(|(fqn, item)| { - let mut edges: BTreeMap> = BTreeMap::new(); - for dependency in graph.get_downstream_dependencies(fqn.as_str()) { - let kind = serde_json::to_value(dependency.kind) - .ok() - .and_then(|k| k.as_str().map(str::to_string)) - .unwrap_or_default(); - if let Some((_, key)) = GRAPH_EDGES.iter().find(|(name, _)| *name == kind) { - let targets = edges.entry((*key).to_string()).or_default(); - let target = dependency.to_node.key().to_string(); - if !targets.contains(&target) { - targets.push(target); - } - } - } - let properties: Map = item - .properties - .iter() - .map(|p| { - let values: Vec = p.values.iter().map(|v| Value::String(v.value.to_string())).collect(); - (p.name.to_string(), Value::Array(values)) - }) - .collect(); - let mut object = Map::new(); - object.insert("kind".into(), serde_json::to_value(item.kind).unwrap_or(Value::Null)); - object.insert("tier".into(), serde_json::to_value(item.tier).unwrap_or(Value::Null)); - if let Some(parent) = &item.parent_item_fqn { - object.insert("parent".into(), Value::String(parent.as_str().to_string())); - } - if !properties.is_empty() { - object.insert("props".into(), Value::Object(properties)); - } - for (kind, targets) in edges { - object.insert(kind, json!(targets)); - } - (fqn.as_str().to_string(), Value::Object(object)) - }) - .collect() -} - -fn project_root(root: &Path, files: &[PathBuf]) -> PathBuf { - let root = std::fs::canonicalize(root).unwrap_or_else(|_| root.to_path_buf()); - if files.iter().all(|f| f.starts_with(&root)) { - return root; - } - let mut common = files.first().and_then(|f| f.parent()).map(Path::to_path_buf).unwrap_or(root); - for file in files { - while !file.starts_with(&common) { - if !common.pop() { - break; - } - } - } - common -} - -fn to_slash(path: &Path) -> String { - path.to_string_lossy().replace('\\', "/") -} diff --git a/semsitter-bridge/src/main.rs b/semsitter-bridge/src/main.rs deleted file mode 100644 index 51fb3c84150..00000000000 --- a/semsitter-bridge/src/main.rs +++ /dev/null @@ -1,126 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ - -use std::io::Write; -use std::path::PathBuf; - -use anyhow::{Result, anyhow, bail}; -use semsitter_bridge::server::{print_csts, serve_jsonl, serve_shm}; - -const USAGE: &str = "\ -usage: - semsitter-bridge serve [--shm | --jsonl] [--dump-json DIR] - semsitter-bridge cst --root DIR [--classpath CP] [--dump-json DIR] FILE... - semsitter-bridge parse [--dump-json DIR] FILE -CP uses the platform path separator. ---shm serves protobuf over shared memory, with one ping byte on stdin/stdout. ---jsonl is the line-delimited JSON server. ---dump-json writes udg.json, semantic-dump JSON and annotated CST JSON under DIR."; - -/// Runs the command, and the scan workers, on a stack deep enough to analyze, encode and drop the -/// tree of a deeply nested expression, such as a concatenation of thousands of strings. -fn main() -> Result<()> { - udg_gen::scanner::init_global_pool(None, Some(semsitter_bridge::STACK_SIZE)); - std::thread::Builder::new() - .stack_size(semsitter_bridge::STACK_SIZE) - .spawn(run)? - .join() - .map_err(|_| anyhow!("the bridge panicked"))? -} - -fn run() -> Result<()> { - let args: Vec = std::env::args().skip(1).collect(); - match args.first().map(String::as_str) { - None | Some("serve") => serve_command(&args[1..]), - Some("cst") => cst(&args[1..]), - Some("parse") => parse(&args[1..]), - Some("-h" | "--help") => { - println!("{USAGE}"); - Ok(()) - } - Some(other) => bail!("unknown command: {other}\n{USAGE}"), - } -} - -fn serve_command(args: &[String]) -> Result<()> { - let mut shm = false; - let mut dump = None; - let mut args = args.iter(); - while let Some(arg) = args.next() { - match arg.as_str() { - "--shm" => shm = true, - "--jsonl" => shm = false, - "--dump-json" => dump = Some(PathBuf::from(args.next().ok_or_else(|| anyhow!(USAGE))?)), - "-h" | "--help" => { - println!("{USAGE}"); - return Ok(()); - } - other => bail!("unknown argument: {other}\n{USAGE}"), - } - } - let stdin = std::io::stdin().lock(); - let stdout = std::io::stdout().lock(); - if shm { - serve_shm(stdin, stdout, dump) - } else { - serve_jsonl(stdin, stdout, dump) - } -} - -fn parse(args: &[String]) -> Result<()> { - let mut dump = None; - let mut file = None; - let mut args = args.iter(); - while let Some(arg) = args.next() { - match arg.as_str() { - "--dump-json" => dump = Some(PathBuf::from(args.next().ok_or_else(|| anyhow!(USAGE))?)), - other if file.is_none() => file = Some(other), - other => bail!("unknown argument: {other}\n{USAGE}"), - } - } - let file = file.ok_or_else(|| anyhow!(USAGE))?; - let cst = semsitter_bridge::parse_only(&std::fs::read(file)?)?; - if let Some(dir) = &dump { - std::fs::create_dir_all(dir)?; - std::fs::write(dir.join("parse.json"), serde_json::to_vec_pretty(&cst)?)?; - } - let mut stdout = std::io::stdout().lock(); - serde_json::to_writer(&mut stdout, &cst)?; - writeln!(stdout)?; - Ok(()) -} - -fn cst(args: &[String]) -> Result<()> { - let mut root = None; - let mut classpath = Vec::new(); - let mut dump = None; - let mut files = Vec::new(); - let mut args = args.iter(); - while let Some(arg) = args.next() { - match arg.as_str() { - "--root" => root = args.next().map(PathBuf::from), - "--classpath" => { - let value = args.next().ok_or_else(|| anyhow!(USAGE))?; - classpath.extend(std::env::split_paths(value).map(|p| p.to_string_lossy().into_owned())); - } - "--dump-json" => dump = Some(PathBuf::from(args.next().ok_or_else(|| anyhow!(USAGE))?)), - file => files.push(PathBuf::from(file)), - } - } - let root = root.ok_or_else(|| anyhow!(USAGE))?; - print_csts(&root, &files, &classpath, dump.as_deref(), std::io::stdout().lock()) -} diff --git a/semsitter-bridge/src/server.rs b/semsitter-bridge/src/server.rs deleted file mode 100644 index 60c4348d102..00000000000 --- a/semsitter-bridge/src/server.rs +++ /dev/null @@ -1,231 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ - -//! JSONL protocol: one request object per stdin line, one response object per stdout line. -//! -//! - `{"id":1,"op":"analyze","root":"/p","files":["/p/A.java"],"classpath":["/lib.jar"]}` -//! scans the batch and answers `{"id":1,"ok":true,"root":"/p","files":["A.java"]}`. -//! - `{"id":2,"op":"cst","path":"/p/A.java"}` answers `{"id":2,"ok":true,"cst":{...}}` with the -//! CST of a file of the last `analyze`. -//! - `{"id":3,"op":"types","names":["java.util.List"]}` answers `{"id":3,"ok":true,"types":{...}}` -//! with the declarations (kind, supertypes, members) of the named types and their supertypes. -//! - `{"id":4,"op":"parse","source":"class A {}"}` answers the CST without semantics. -//! - `{"id":5,"op":"quit"}` answers `{"id":5,"ok":true}` and stops the server. -//! -//! Failures answer `{"id":n,"ok":false,"error":"..."}` and keep the server running. - -use std::io::{BufRead, Read, Write}; -use std::path::{Path, PathBuf}; - -use anyhow::{Result, anyhow}; -use serde_json::{Map, Value, json}; - -use crate::shm::{ATTACHED, Link, READY}; -use crate::wire::{self, Request}; -use crate::{Analysis, analyze_dumping, parse_only}; - -pub struct Server { - analysis: Option, - dump: Option, - types_written: u32, -} - -impl Default for Server { - fn default() -> Self { - Self { analysis: None, dump: None, types_written: 0 } - } -} - -impl Server { - /// Answers one request line, and whether the server must stop. - pub fn handle(&mut self, line: &str) -> (Value, bool) { - let request: Value = match serde_json::from_str(line) { - Ok(request) => request, - Err(e) => return (json!({"ok": false, "error": format!("invalid request: {e}")}), false), - }; - let id = request.get("id").cloned().unwrap_or(Value::Null); - let op = request.get("op").and_then(Value::as_str).unwrap_or_default(); - let quit = op == "quit"; - let mut response = match self.dispatch(op, &request) { - Ok(body) => body, - Err(e) => json!({"ok": false, "error": format!("{e:#}")}), - }; - response.as_object_mut().expect("response is an object").insert("id".into(), id); - (response, quit) - } - - fn dispatch(&mut self, op: &str, request: &Value) -> Result { - match op { - "analyze" => { - let root = PathBuf::from(string(request, "root")?); - let files: Vec = strings(request, "files").into_iter().map(PathBuf::from).collect(); - let classpath = strings(request, "classpath"); - let analysis = analyze_dumping(&root, &files, &classpath, self.dump.as_deref())?; - let body = json!({ - "ok": true, - "root": analysis.root().to_string_lossy(), - "files": analysis.files(), - }); - self.analysis = Some(analysis); - Ok(body) - } - "cst" => { - let analysis = self.analysis.as_ref().ok_or_else(|| anyhow!("no analyze request yet"))?; - Ok(ok_with("cst", analysis.cst(string(request, "path")?)?)) - } - "types" => { - let analysis = self.analysis.as_ref().ok_or_else(|| anyhow!("no analyze request yet"))?; - let names = strings(request, "names"); - let types = analysis.types(&names); - self.write_types(&types)?; - Ok(ok_with("types", types)) - } - "parse" => { - let cst = parse_only(string(request, "source")?.as_bytes())?; - if let Some(dir) = &self.dump { - std::fs::create_dir_all(dir)?; - std::fs::write(dir.join("parse.json"), serde_json::to_vec_pretty(&cst)?)?; - } - Ok(ok_with("cst", cst)) - } - "quit" => Ok(json!({"ok": true})), - other => Err(anyhow!("unknown op: {other}")), - } - } -} - -impl Server { - fn write_types(&mut self, types: &Value) -> Result<()> { - let Some(dir) = &self.dump else { - return Ok(()); - }; - self.types_written += 1; - let target = dir.join("types").join(format!("{}.json", self.types_written)); - if let Some(parent) = target.parent() { - std::fs::create_dir_all(parent)?; - } - std::fs::write(target, serde_json::to_vec_pretty(types)?)?; - Ok(()) - } - - fn handle_bytes(&mut self, bytes: &[u8]) -> (Vec, bool) { - let incoming = match wire::decode_request(bytes) { - Ok(incoming) => incoming, - Err(e) => return (wire::encode_response(0, &json!({"ok": false, "error": format!("{e:#}")})), false), - }; - let request = match &incoming.request { - Request::Analyze { root, files, classpath } => json!({ - "op": "analyze", - "root": root, - "files": files, - "classpath": classpath, - }), - Request::Cst { path } => json!({"op": "cst", "path": path}), - Request::Types { names } => json!({"op": "types", "names": names}), - Request::Parse { source } => json!({"op": "parse", "source": source}), - Request::Quit => json!({"op": "quit"}), - }; - let op = request.get("op").and_then(Value::as_str).unwrap_or_default(); - let quit = matches!(incoming.request, Request::Quit); - let mut response = match self.dispatch(op, &request) { - Ok(body) => body, - Err(e) => json!({"ok": false, "error": format!("{e:#}")}), - }; - response.as_object_mut().expect("response is an object").insert("id".into(), json!(incoming.id)); - (wire::encode_response(incoming.id, &response), quit) - } -} - -/// JSONL server. One request object per stdin line, one response object per stdout line. -pub fn serve(input: impl BufRead, output: impl Write) -> Result<()> { - serve_jsonl(input, output, None) -} - -pub fn serve_jsonl(input: impl BufRead, mut output: impl Write, dump: Option) -> Result<()> { - let mut server = Server { dump, ..Server::default() }; - for line in input.lines() { - let line = line?; - if line.trim().is_empty() { - continue; - } - let (response, quit) = server.handle(&line); - serde_json::to_writer(&mut output, &response)?; - output.write_all(b"\n")?; - output.flush()?; - if quit { - break; - } - } - Ok(()) -} - -/// Shared-memory server. Writes [`READY`] once the mappings exist, reads [`ATTACHED`], -/// then exchanges protobuf messages. Each message is announced by a single byte on the pipe. -pub fn serve_shm(mut input: impl Read, mut output: impl Write, dump: Option) -> Result<()> { - let id = std::process::id().to_string(); - let mut link = Link::create(&id)?; - output.write_all(&[READY])?; - output.flush()?; - let mut attached = [0u8; 1]; - input.read_exact(&mut attached)?; - if attached[0] != ATTACHED { - return Err(anyhow!("expected the peer to attach the shared mappings")); - } - link.unlink(); - let mut server = Server { dump, ..Server::default() }; - loop { - let request = link.read_request(&mut input, &mut output)?; - let (response, quit) = server.handle_bytes(&request); - link.write_response(&response, &mut output, &mut input)?; - if quit { - break; - } - } - Ok(()) -} - -/// CSTs of `files` (with semantics), one JSON object per line. -pub fn print_csts(root: &Path, files: &[PathBuf], classpath: &[String], dump: Option<&Path>, mut output: impl Write) -> Result<()> { - let analysis = analyze_dumping(root, files, classpath, dump)?; - for file in analysis.files() { - serde_json::to_writer(&mut output, &analysis.cst(file)?)?; - output.write_all(b"\n")?; - } - output.flush()?; - Ok(()) -} - -fn ok_with(key: &str, value: Value) -> Value { - let mut body = Map::new(); - body.insert("ok".into(), Value::Bool(true)); - body.insert(key.into(), value); - Value::Object(body) -} - -fn string<'a>(request: &'a Value, key: &str) -> Result<&'a str> { - request.get(key).and_then(Value::as_str).ok_or_else(|| anyhow!("missing string field: {key}")) -} - -fn strings(request: &Value, key: &str) -> Vec { - request - .get(key) - .and_then(Value::as_array) - .into_iter() - .flatten() - .filter_map(|v| v.as_str().map(str::to_string)) - .collect() -} diff --git a/semsitter-bridge/src/shm.rs b/semsitter-bridge/src/shm.rs deleted file mode 100644 index d059a855c0c..00000000000 --- a/semsitter-bridge/src/shm.rs +++ /dev/null @@ -1,234 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ - -//! Two shared mappings and a one-byte ping on the pipe. -//! -//! The payload never travels on the pipe. `1` means the mapping holds a protobuf message, -//! `2` means the peer grew it and this side must remap. The directory entry is removed once -//! both sides have mapped, so nothing is left to read as a file. On Linux the files live in -//! `/dev/shm`. - -use std::fs::OpenOptions; -use std::io::{Read, Write}; -use std::path::{Path, PathBuf}; -use std::sync::atomic::{Ordering, fence}; - -use anyhow::{Result, bail}; -use memmap2::MmapMut; - -pub const READY: u8 = b'R'; -pub const ATTACHED: u8 = b'A'; -const DATA: u8 = 1; -const GROW: u8 = 2; -const INITIAL: usize = 32 * 1024 * 1024; -const MAX: usize = 512 * 1024 * 1024; -const HEADER: usize = 24; -const MAGIC: u32 = 0x3142_4A53; - -pub struct Link { - id: String, - request: Slot, - response: Slot, - created: bool, -} - -struct Slot { - path: PathBuf, - file: std::fs::File, - map: MmapMut, -} - -impl Link { - pub fn create(id: &str) -> Result { - Ok(Link { - request: Slot::create(&slot_path(id, "q"), INITIAL)?, - response: Slot::create(&slot_path(id, "a"), INITIAL)?, - id: id.to_string(), - created: true, - }) - } - - pub fn open(id: &str) -> Result { - Ok(Link { - request: Slot::open(&slot_path(id, "q"))?, - response: Slot::open(&slot_path(id, "a"))?, - id: id.to_string(), - created: false, - }) - } - - pub fn id(&self) -> &str { - &self.id - } - - /// Drops the directory entries. Mappings stay valid. - pub fn unlink(&self) { - let _ = std::fs::remove_file(slot_path(&self.id, "q")); - let _ = std::fs::remove_file(slot_path(&self.id, "a")); - } - - pub fn write_request(&mut self, bytes: &[u8], to_peer: &mut impl Write, from_peer: &mut impl Read) -> Result<()> { - self.request.write(bytes, to_peer, from_peer) - } - - pub fn read_request(&mut self, from_peer: &mut impl Read, to_peer: &mut impl Write) -> Result> { - self.request.read(from_peer, to_peer) - } - - pub fn write_response(&mut self, bytes: &[u8], to_peer: &mut impl Write, from_peer: &mut impl Read) -> Result<()> { - self.response.write(bytes, to_peer, from_peer) - } - - pub fn read_response(&mut self, from_peer: &mut impl Read, to_peer: &mut impl Write) -> Result> { - self.response.read(from_peer, to_peer) - } -} - -impl Drop for Link { - fn drop(&mut self) { - if self.created { - self.unlink(); - } - } -} - -impl Slot { - fn create(path: &Path, len: usize) -> Result { - if let Some(parent) = path.parent() { - std::fs::create_dir_all(parent)?; - } - let file = OpenOptions::new().read(true).write(true).create(true).truncate(true).open(path)?; - file.set_len(len as u64)?; - let mut slot = Slot { - path: path.to_path_buf(), - map: unsafe { MmapMut::map_mut(&file)? }, - file, - }; - slot.put_header(0, 0, len as u64); - Ok(slot) - } - - fn open(path: &Path) -> Result { - let file = OpenOptions::new().read(true).write(true).open(path)?; - let len = file.metadata()?.len() as usize; - Ok(Slot { - path: path.to_path_buf(), - map: unsafe { MmapMut::map_mut(&file)? }, - file, - }) - .and_then(|slot| { - if slot.map.len() != len { - bail!("mapping length mismatch for {}", path.display()); - } - Ok(slot) - }) - } - - fn write(&mut self, bytes: &[u8], to_peer: &mut impl Write, from_peer: &mut impl Read) -> Result<()> { - let need = HEADER + bytes.len(); - if need > self.map.len() { - let mut cap = self.map.len().max(INITIAL); - while cap < need { - if cap >= MAX { - bail!("protobuf message of {} bytes exceeds the shared mapping", bytes.len()); - } - cap = cap.saturating_mul(2).min(MAX); - } - self.file.set_len(cap as u64)?; - self.put_header(GROW as u32, cap as u64, cap as u64); - fence(Ordering::Release); - ping(to_peer, GROW)?; - let ack = read_ping(from_peer)?; - if ack != GROW { - bail!("shared mapping grow was not acknowledged"); - } - self.remap(cap)?; - } - self.map[HEADER..HEADER + bytes.len()].copy_from_slice(bytes); - self.put_header(0, bytes.len() as u64, self.map.len() as u64); - fence(Ordering::Release); - ping(to_peer, DATA)?; - Ok(()) - } - - fn read(&mut self, from_peer: &mut impl Read, to_peer: &mut impl Write) -> Result> { - loop { - match read_ping(from_peer)? { - GROW => { - let cap = self.header_len()? as usize; - self.file.set_len(cap as u64)?; - self.remap(cap)?; - fence(Ordering::Release); - ping(to_peer, GROW)?; - } - DATA => { - fence(Ordering::Acquire); - let len = self.header_len()? as usize; - if HEADER + len > self.map.len() { - bail!("payload of {len} bytes does not fit the shared mapping"); - } - return Ok(self.map[HEADER..HEADER + len].to_vec()); - } - other => bail!("unexpected shared-memory ping {other}"), - } - } - } - - fn remap(&mut self, len: usize) -> Result<()> { - self.map = unsafe { MmapMut::map_mut(&self.file)? }; - if self.map.len() < len { - bail!("remapped {} is shorter than {len}", self.path.display()); - } - Ok(()) - } - - fn put_header(&mut self, kind: u32, len: u64, cap: u64) { - self.map[0..4].copy_from_slice(&MAGIC.to_le_bytes()); - self.map[4..8].copy_from_slice(&kind.to_le_bytes()); - self.map[8..16].copy_from_slice(&len.to_le_bytes()); - self.map[16..24].copy_from_slice(&cap.to_le_bytes()); - } - - fn header_len(&self) -> Result { - let magic = u32::from_le_bytes(self.map[0..4].try_into().unwrap()); - if magic != MAGIC { - bail!("shared mapping has no bridge header"); - } - Ok(u64::from_le_bytes(self.map[8..16].try_into().unwrap())) - } -} - -fn ping(to_peer: &mut impl Write, byte: u8) -> Result<()> { - to_peer.write_all(&[byte])?; - to_peer.flush()?; - Ok(()) -} - -fn read_ping(from_peer: &mut impl Read) -> Result { - let mut byte = [0u8; 1]; - from_peer.read_exact(&mut byte)?; - Ok(byte[0]) -} - -pub fn slot_dir() -> PathBuf { - let shm = PathBuf::from("/dev/shm"); - if shm.is_dir() { shm } else { std::env::temp_dir() } -} - -fn slot_path(id: &str, which: &str) -> PathBuf { - slot_dir().join(format!("sjb-{id}-{which}")) -} diff --git a/semsitter-bridge/src/types.rs b/semsitter-bridge/src/types.rs deleted file mode 100644 index abff01b97c0..00000000000 --- a/semsitter-bridge/src/types.rs +++ /dev/null @@ -1,621 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ - -//! Type declarations by erased binary name (`java.util.Map$Entry`), from the first source that -//! knows the type: the scanned project graph, the classpath index (jars and the JDK runtime -//! image), then the JDK table SemSitter embeds. - -use std::collections::{BTreeSet, HashMap, VecDeque}; - -use serde_json::{Map, Value, json}; -use udg_family_jvm::external::classpath::{ - Annotation, ClasspathDeclaration, ClasspathIndex, ClasspathType, ConstantValue, ElementValue, -}; -use udg_family_jvm::external::jdk_table::{JdkMemberKind, JdkType, jdk_type, parameter_type_variables}; - -const ACC_STATIC: u16 = 0x0008; -const ACC_FINAL: u16 = 0x0010; -const ACC_PUBLIC: u16 = 0x0001; -const ACC_PRIVATE: u16 = 0x0002; -const ACC_PROTECTED: u16 = 0x0004; -const ACC_ENUM: u16 = 0x4000; -const ACC_ABSTRACT: u16 = 0x0400; - -/// Strips the generic arity suffix of a graph id segment: ``java.util.List`1`` is `java.util.List`. -pub fn erase(id: &str) -> String { - let mut out = String::with_capacity(id.len()); - let mut chars = id.chars().peekable(); - while let Some(c) = chars.next() { - if c == '`' { - while chars.peek().is_some_and(char::is_ascii_digit) { - chars.next(); - } - } else { - out.push(c); - } - } - out -} - -/// Project types of the graph: erased name to graph id, and graph id to member ids. -#[derive(Default)] -pub struct ProjectTypes { - pub keys: HashMap, - pub members: HashMap>, -} - -impl ProjectTypes { - pub fn new(facts: &HashMap) -> Self { - let mut types = ProjectTypes::default(); - for (id, fact) in facts { - if fact.get("tier").and_then(Value::as_str) != Some("IN_PROJECT") { - continue; - } - if let Some(parent) = fact.get("parent").and_then(Value::as_str) { - types.members.entry(parent.to_string()).or_default().push(id.clone()); - } - if !id.contains('#') && is_type_kind(fact.get("kind").and_then(Value::as_str).unwrap_or_default()) { - types.keys.insert(erase(id), id.clone()); - } - } - for members in types.members.values_mut() { - members.sort(); - } - types - } -} - -fn is_type_kind(kind: &str) -> bool { - matches!(kind, "class" | "interface" | "enum" | "record" | "annotation" | "exception") -} - -/// Declarations of `names` and of all their supertypes. -pub fn lookup( - names: &[String], - project: &ProjectTypes, - facts: &HashMap, - classpath: &ClasspathIndex, -) -> Map { - let mut found = Map::new(); - let mut queue: VecDeque = names.iter().map(|n| erase(n)).collect(); - let mut seen: BTreeSet = BTreeSet::new(); - while let Some(name) = queue.pop_front() { - if !seen.insert(name.clone()) { - continue; - } - let info = project_type(&name, project, facts) - .or_else(|| classpath.get(&name).map(|ty| classpath_type(&name, ty, classpath.declaration(&name)))) - .or_else(|| jdk_type(&name).map(jdk_type_info)); - let Some(info) = info else { - continue; - }; - for key in ["super", "interfaces"] { - for supertype in strings(&info, key) { - queue.push_back(erase(&supertype)); - } - } - found.insert(name, info); - } - found -} - -fn strings(value: &Value, key: &str) -> Vec { - match value.get(key) { - Some(Value::String(s)) => vec![s.clone()], - Some(Value::Array(items)) => items.iter().filter_map(|i| i.as_str().map(str::to_string)).collect(), - _ => Vec::new(), - } -} - -fn project_type(name: &str, project: &ProjectTypes, facts: &HashMap) -> Option { - let id = project.keys.get(name)?; - let fact = facts.get(id)?; - let kind = fact.get("kind").and_then(Value::as_str).unwrap_or("class"); - let extends = strings(fact, "extends"); - let implements = strings(fact, "implements"); - let (superclass, interfaces) = if kind == "interface" || kind == "annotation" { - (None, extends) - } else { - (extends.into_iter().next(), implements) - }; - let members: Vec = project - .members - .get(id) - .into_iter() - .flatten() - .filter_map(|member| facts.get(member).map(|f| json!({"id": member, "facts": f}))) - .collect(); - Some(json!({ - "origin": "project", - "id": id, - "kind": kind, - "super": superclass, - "interfaces": interfaces, - "modifiers": fact.pointer("/props/modifiers").cloned().unwrap_or_else(|| json!([])), - "members": members, - })) -} - -fn classpath_type(name: &str, ty: &ClasspathType, declaration: Option<&ClasspathDeclaration>) -> Value { - let parameters = ty.signature.as_deref().map(type_parameters).unwrap_or_default(); - let no_annotations: &[Annotation] = &[]; - let no_throws: &[String] = &[]; - let mut members: Vec = ty - .methods - .iter() - .map(|m| { - let returned = m.signature.as_deref().and_then(|s| class_return_parameter(s, ¶meters)); - let declared = declaration.and_then(|facts| facts.methods.get(&(m.name.clone(), m.descriptor.clone()))); - json!({ - "k": "m", - "name": m.name, - "desc": m.descriptor, - "static": m.is_static, - "abstract": m.is_abstract, - "varargs": m.is_varargs, - "vis": "public", - "retParam": returned, - "retSelf": returned.is_some_and(|index| parameters[index].1 == name), - "retType": m.signature.as_deref().and_then(return_type), - "paramVars": m.signature.as_deref().map(|s| parameter_variables(s, ¶meters)), - "paramTypes": m.signature.as_deref().map(parameter_types), - "typeVars": m.signature.as_deref().map(|s| type_parameters(s).into_iter().map(|(name, _)| name).collect::>()), - "annotations": annotations_json(declared.map(|facts| facts.annotations.as_slice()).unwrap_or(no_annotations)), - "throws": declared.map(|facts| facts.exceptions.as_slice()).unwrap_or(no_throws), - }) - }) - .collect(); - members.extend(ty.fields.iter().filter_map(|f| { - let desc = field_descriptor(f.declared_type.as_ref()?); - let declared = declaration.and_then(|facts| facts.fields.get(&f.name)); - Some(json!({ - "k": "f", - "name": f.name, - "desc": desc, - "static": f.access_flags & ACC_STATIC != 0, - "final": f.access_flags & ACC_FINAL != 0, - "enum": f.access_flags & ACC_ENUM != 0, - "vis": access_visibility(f.access_flags), - "constant": f.constant.as_ref().and_then(ConstantValue::as_string), - "annotations": annotations_json(declared.map(|facts| facts.annotations.as_slice()).unwrap_or(no_annotations)), - })) - })); - let flags = declaration.map(|facts| facts.access_flags).unwrap_or(0); - let mut info = json!({ - "origin": "classpath", - "kind": classpath_kind(ty), - "super": ty.super_class, - "interfaces": ty.interfaces, - "modifiers": classpath_modifiers(flags), - "annotations": annotations_json(declaration.map(|facts| facts.annotations.as_slice()).unwrap_or(no_annotations)), - "typeParameters": parameters.len(), - "typeVars": parameters.iter().map(|(name, _)| name).collect::>(), - "members": members, - }); - if let (Some(signature), Some(object)) = (ty.signature.as_deref(), info.as_object_mut()) { - let (superclass, interfaces) = signature_supertypes(signature); - object.insert("superType".into(), json!(superclass)); - object.insert("interfaceTypes".into(), json!(interfaces)); - } - info -} - -/// The JVM descriptor of a classpath field type, which the classpath reader spells as a source -/// type: a primitive or a binary name, followed by one `[]` per array dimension. -fn field_descriptor(source: &str) -> String { - let element = source.trim_end_matches("[]"); - let dimensions = (source.len() - element.len()) / 2; - let base = match element { - "boolean" => "Z".to_string(), - "byte" => "B".to_string(), - "char" => "C".to_string(), - "short" => "S".to_string(), - "int" => "I".to_string(), - "long" => "J".to_string(), - "float" => "F".to_string(), - "double" => "D".to_string(), - name => format!("L{};", name.replace('.', "/")), - }; - format!("{}{base}", "[".repeat(dimensions)) -} - -fn annotations_json(annotations: &[Annotation]) -> Value { - Value::Array(annotations.iter().map(annotation_json).collect()) -} - -fn annotation_json(annotation: &Annotation) -> Value { - json!({ - "name": annotation.name, - "values": annotation.elements.iter().map(|(name, value)| element_json(name, value)).collect::>(), - }) -} - -fn element_json(name: &str, value: &ElementValue) -> Value { - match value { - ElementValue::Constant(ConstantValue::Boolean(value)) => json!({"name": name, "kind": "boolean", "value": value}), - ElementValue::Constant(ConstantValue::Byte(value)) => json!({"name": name, "kind": "byte", "value": value}), - ElementValue::Constant(ConstantValue::Char(value)) => json!({"name": name, "kind": "char", "value": value}), - ElementValue::Constant(ConstantValue::Short(value)) => json!({"name": name, "kind": "short", "value": value}), - ElementValue::Constant(ConstantValue::Int(value)) => json!({"name": name, "kind": "int", "value": value}), - ElementValue::Constant(ConstantValue::Long(value)) => json!({"name": name, "kind": "long", "value": value}), - ElementValue::Constant(ConstantValue::Float(value)) => json!({"name": name, "kind": "float", "value": value}), - ElementValue::Constant(ConstantValue::Double(value)) => json!({"name": name, "kind": "double", "value": value}), - ElementValue::Constant(ConstantValue::String(value)) => json!({"name": name, "kind": "string", "value": value}), - ElementValue::Class(value) => json!({"name": name, "kind": "class", "value": value}), - ElementValue::Enum { type_name, constant } => json!({"name": name, "kind": "enum", "type": type_name, "const": constant}), - ElementValue::Annotation(annotation) => json!({"name": name, "kind": "annotation", "value": annotation_json(annotation)}), - ElementValue::Array(items) => json!({ - "name": name, - "kind": "array", - "value": items.iter().map(|item| element_json("value", item)).collect::>(), - }), - } -} - -/// Class-file access flags as the modifier names `JSema` already applies to JDK types. -fn classpath_modifiers(flags: u16) -> Vec<&'static str> { - let mut modifiers = Vec::new(); - if flags & ACC_PUBLIC != 0 { - modifiers.push("public"); - } else if flags & ACC_PROTECTED != 0 { - modifiers.push("protected"); - } else if flags & ACC_PRIVATE != 0 { - modifiers.push("private"); - } - if flags & ACC_STATIC != 0 { - modifiers.push("static"); - } - if flags & ACC_FINAL != 0 { - modifiers.push("final"); - } - if flags & ACC_ABSTRACT != 0 { - modifiers.push("abstract"); - } - modifiers -} - -/// The classfile reader only distinguishes classes from interfaces. A record is a class whose superclass is `java.lang.Record`. -fn classpath_kind(ty: &ClasspathType) -> Value { - if ty.super_class.as_deref() == Some("java.lang.Record") { - return json!("record"); - } - serde_json::to_value(ty.kind).unwrap_or(Value::Null) -} - -/// Type parameters of a generic signature, `...`, with the erased dotted -/// class of their first bound. -fn type_parameters(signature: &str) -> Vec<(String, String)> { - let bytes = signature.as_bytes(); - let mut out = Vec::new(); - if bytes.first() != Some(&b'<') { - return out; - } - let mut i = 1; - while i < bytes.len() && bytes[i] != b'>' { - let start = i; - while i < bytes.len() && bytes[i] != b':' { - i += 1; - } - let name = signature[start..i].to_string(); - let mut bound = String::new(); - while i < bytes.len() && bytes[i] == b':' { - i += 1; - if i < bytes.len() && bytes[i] != b':' && bytes[i] != b'>' { - let begin = i; - i = skip_type(bytes, i); - if bound.is_empty() { - bound = erased_class(&signature[begin..i]); - } - } - } - out.push((name, bound)); - } - out -} - -fn skip_type(bytes: &[u8], mut i: usize) -> usize { - match bytes.get(i) { - Some(b'[') => skip_type(bytes, i + 1), - Some(b'L') => { - let mut depth = 0; - while i < bytes.len() { - match bytes[i] { - b'<' => depth += 1, - b'>' => depth -= 1, - b';' if depth == 0 => return i + 1, - _ => {} - } - i += 1; - } - i - } - Some(b'T') => bytes[i..].iter().position(|&b| b == b';').map_or(bytes.len(), |end| i + end + 1), - _ => i + 1, - } -} - -/// The generic superclass and interfaces of a class `Signature`, as type facts (`id`, `args`, -/// `kind` `typevar`/`array`/`wildcard`). -fn signature_supertypes(signature: &str) -> (Option, Vec) { - let bytes = signature.as_bytes(); - let mut i = 0; - if bytes.first() == Some(&b'<') { - let mut depth = 0; - while i < bytes.len() { - match bytes[i] { - b'<' => depth += 1, - b'>' => depth -= 1, - _ => {} - } - i += 1; - if depth == 0 { - break; - } - } - } - let superclass = (i < bytes.len()).then(|| signature_type(signature, &mut i)); - let mut interfaces = Vec::new(); - while i < bytes.len() { - interfaces.push(signature_type(signature, &mut i)); - } - (superclass, interfaces) -} - -/// One JVM type signature starting at `i`, which is moved past it. -fn signature_type(signature: &str, i: &mut usize) -> Value { - let bytes = signature.as_bytes(); - let Some(&head) = bytes.get(*i) else { - return json!({"kind": "unknown"}); - }; - *i += 1; - match head { - b'[' => json!({"kind": "array", "elem": signature_type(signature, i)}), - b'T' => { - let end = signature[*i..].find(';').map_or(bytes.len(), |e| *i + e); - let name = &signature[*i..end]; - *i = end + 1; - json!({"kind": "typevar", "id": name}) - } - b'*' => json!({"kind": "wildcard"}), - b'+' => json!({"kind": "wildcard", "extends": signature_type(signature, i)}), - b'-' => json!({"kind": "wildcard", "super": signature_type(signature, i)}), - b'L' => { - let mut name = Vec::new(); - let mut args = Vec::new(); - while *i < bytes.len() { - match bytes[*i] { - b';' => { - *i += 1; - break; - } - b'<' => { - *i += 1; - args.clear(); - while *i < bytes.len() && bytes[*i] != b'>' { - args.push(signature_type(signature, i)); - } - *i += 1; - } - b'.' => { - name.push(b'$'); - *i += 1; - } - b'/' => { - name.push(b'.'); - *i += 1; - } - c => { - name.push(c); - *i += 1; - } - } - } - json!({"id": String::from_utf8_lossy(&name), "args": args}) - } - primitive => json!({"id": primitive_name(primitive)}), - } -} - -fn primitive_name(code: u8) -> &'static str { - match code { - b'B' => "byte", - b'C' => "char", - b'D' => "double", - b'F' => "float", - b'I' => "int", - b'J' => "long", - b'S' => "short", - b'Z' => "boolean", - _ => "void", - } -} - -fn erased_class(signature: &str) -> String { - signature - .strip_prefix('L') - .map(|rest| rest.split(['<', ';']).next().unwrap_or_default().replace('/', ".")) - .unwrap_or_default() -} - -/// The JDK table's class and method parameter type variables, in the shape of [`parameter_variables`]. -fn jdk_parameter_variables(class: Option<&str>, method: Option<&str>) -> Option> { - if class.is_none() && method.is_none() { - return None; - } - let class = class.map(parameter_type_variables).unwrap_or_default(); - let method = method.map(parameter_type_variables).unwrap_or_default(); - let slot = |prefix: char, slots: &[Option<(usize, &str)>], i: usize| { - slots.get(i).copied().flatten().map(|(index, array)| format!("{prefix}{index}{array}")) - }; - Some( - (0..class.len().max(method.len())) - .map(|i| slot('c', &class, i).or_else(|| slot('m', &method, i)).unwrap_or_default()) - .collect(), - ) -} - -/// Per parameter of a method signature, the type variable it is: `c1` for the owner's second type -/// parameter, `m0[]` for an array of the method's first one, or empty. -fn parameter_variables(signature: &str, parameters: &[(String, String)]) -> Vec { - let own = type_parameters(signature); - let bytes = signature.as_bytes(); - let Some(open) = signature.find('(') else { - return Vec::new(); - }; - let mut out = Vec::new(); - let mut i = open + 1; - while i < bytes.len() && bytes[i] != b')' { - let end = skip_type(bytes, i); - let parameter = &signature[i..end]; - let element = parameter.trim_start_matches('['); - let array = "[]".repeat(parameter.len() - element.len()); - let variable = element.strip_prefix('T').and_then(|v| v.strip_suffix(';')); - out.push(match variable { - Some(name) => match own.iter().position(|(p, _)| p == name) { - Some(index) => format!("m{index}{array}"), - None => parameters.iter().position(|(p, _)| p == name).map_or_else(String::new, |index| format!("c{index}{array}")), - }, - None => String::new(), - }); - i = end; - } - out -} - -/// The return type of a method `Signature`, keeping the type variables it is written in terms of. -fn return_type(signature: &str) -> Option { - let close = signature.rfind(')')?; - let mut i = close + 1; - if i >= signature.len() { - return None; - } - Some(signature_type(signature, &mut i)) -} - -/// The declared parameter types of a method `Signature`, as type facts keeping the type variables -/// they are written in terms of, so that the caller can substitute them for the receiver's arguments. -fn parameter_types(signature: &str) -> Vec { - let bytes = signature.as_bytes(); - let Some(open) = signature.find('(') else { - return Vec::new(); - }; - let mut i = open + 1; - let mut out = Vec::new(); - while i < bytes.len() && bytes[i] != b')' { - out.push(signature_type(signature, &mut i)); - } - out -} - -/// Index of the class type parameter a method signature returns, when it returns one directly. -fn class_return_parameter(signature: &str, parameters: &[(String, String)]) -> Option { - let returned = signature[signature.rfind(')')? + 1..].strip_prefix('T')?; - let name = &returned[..returned.find(';')?]; - if type_parameters(signature).iter().any(|(own, _)| own == name) { - return None; - } - parameters.iter().position(|(parameter, _)| parameter == name) -} - -fn access_visibility(flags: u16) -> &'static str { - if flags & ACC_PUBLIC != 0 { - "public" - } else if flags & ACC_PROTECTED != 0 { - "protected" - } else if flags & ACC_PRIVATE != 0 { - "private" - } else { - "package" - } -} - -fn jdk_type_info(ty: &JdkType) -> Value { - let is_enum = serde_json::to_value(ty.kind).ok().and_then(|k| k.as_str().map(|k| k == "enum")).unwrap_or(false); - let own_descriptor = format!("L{};", ty.fqn.replace('.', "/")); - let members: Vec = ty - .members - .iter() - .map(|m| { - let constant = is_enum && m.kind == JdkMemberKind::Field && m.is_static && m.is_final && m.descriptor == own_descriptor; - json!({ - "enum": constant, - "k": if m.kind == JdkMemberKind::Method { "m" } else { "f" }, - "name": m.name, - "desc": m.descriptor, - "static": m.is_static, - "final": m.is_final, - "abstract": m.is_abstract, - "vis": format!("{:?}", m.visibility).to_lowercase(), - "retParam": m.return_type_class_param_index, - "paramVars": jdk_parameter_variables(m.parameter_type_variables, m.parameter_method_type_variables), - }) - }) - .collect(); - let mut modifiers = Vec::new(); - if ty.is_public { - modifiers.push("public"); - } - if ty.is_abstract { - modifiers.push("abstract"); - } - if ty.is_sealed { - modifiers.push("sealed"); - } - if is_enum { - modifiers.push("final"); - } - json!({ - "origin": "jdk", - "kind": serde_json::to_value(ty.kind).unwrap_or(Value::Null), - "super": ty.super_type, - "interfaces": ty.interfaces, - "modifiers": modifiers, - "typeParameters": ty.type_parameters, - "members": members, - }) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn field_descriptors_round_trip_source_spellings() { - assert_eq!(field_descriptor("java.io.PrintStream"), "Ljava/io/PrintStream;"); - assert_eq!(field_descriptor("int"), "I"); - assert_eq!(field_descriptor("java.util.Map$Entry[][]"), "[[Ljava/util/Map$Entry;"); - assert_eq!(field_descriptor("long[]"), "[J"); - } - - #[test] - fn supertypes_keep_type_arguments_and_type_variables() { - let (superclass, interfaces) = signature_supertypes( - "Ljava/util/AbstractList;Ljava/util/List<[TE;>;Ljava/util/Map$Entry<*+Ljava/lang/Number;>;", - ); - assert_eq!(superclass, Some(json!({"id": "java.util.AbstractList", "args": [{"kind": "typevar", "id": "E"}]}))); - assert_eq!( - interfaces, - vec![ - json!({"id": "java.util.List", "args": [{"kind": "array", "elem": {"kind": "typevar", "id": "E"}}]}), - json!({"id": "java.util.Map$Entry", "args": [{"kind": "wildcard"}, {"kind": "wildcard", "extends": {"id": "java.lang.Number", "args": []}}]}), - ] - ); - // A member of a generic outer class keeps only its own arguments. - let (superclass, _) = signature_supertypes("Lp/Outer.Inner;"); - assert_eq!(superclass, Some(json!({"id": "p.Outer$Inner", "args": [{"id": "java.lang.String", "args": []}]}))); - } -} diff --git a/semsitter-bridge/src/wire.rs b/semsitter-bridge/src/wire.rs deleted file mode 100644 index a067a915ab3..00000000000 --- a/semsitter-bridge/src/wire.rs +++ /dev/null @@ -1,593 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ - -//! Protobuf codec for [`super::proto`](../../proto/bridge.proto). The messages are the only -//! bytes that cross the process boundary; JSON stays a debug dump. - -use anyhow::{Result, anyhow}; -use serde_json::{Map, Value, json}; - -pub enum Request { - Analyze { root: String, files: Vec, classpath: Vec }, - Cst { path: String }, - Types { names: Vec }, - Parse { source: String }, - Quit, -} - -pub struct Incoming { - pub id: u32, - pub request: Request, -} - -pub fn decode_request(bytes: &[u8]) -> Result { - let mut id = 0u32; - let mut analyze = None; - let mut cst = None; - let mut types = None; - let mut parse = None; - let mut quit = false; - for (field, value) in fields(bytes)? { - match (field, value) { - (1, Wire::Varint(v)) => id = v as u32, - (2, Wire::Bytes(b)) => analyze = Some(decode_analyze(&b)?), - (3, Wire::Bytes(b)) => cst = Some(decode_path(&b)?), - (4, Wire::Bytes(b)) => types = Some(decode_names(&b)?), - (5, Wire::Bytes(b)) => parse = Some(decode_source(&b)?), - (6, Wire::Varint(v)) => quit = v != 0, - _ => {} - } - } - let request = if let Some((root, files, classpath)) = analyze { - Request::Analyze { root, files, classpath } - } else if let Some(path) = cst { - Request::Cst { path } - } else if let Some(names) = types { - Request::Types { names } - } else if let Some(source) = parse { - Request::Parse { source } - } else if quit { - Request::Quit - } else { - return Err(anyhow!("request has no op")); - }; - Ok(Incoming { id, request }) -} - -pub fn encode_response(id: u32, body: &Value) -> Vec { - let mut out = Vec::new(); - varint_field(&mut out, 1, id as u64); - let ok = body.get("ok").and_then(Value::as_bool).unwrap_or(false); - if ok { - varint_field(&mut out, 2, 1); - } - if let Some(error) = body.get("error").and_then(Value::as_str) { - string_field(&mut out, 3, error); - } - if let Some(root) = body.get("root").and_then(Value::as_str) { - let mut msg = Vec::new(); - string_field(&mut msg, 1, root); - for file in strings(body, "files") { - string_field(&mut msg, 2, &file); - } - message_field(&mut out, 4, &msg); - } - if let Some(document) = body.get("cst") { - message_field(&mut out, 5, &encode_document(document)); - } - if let Some(types) = body.get("types") { - if let Ok(json) = serde_json::to_string(types) { - string_field(&mut out, 6, &json); - } - } - out -} - -pub fn decode_response(bytes: &[u8]) -> Result { - let mut id = Value::Null; - let mut ok = false; - let mut error = None; - let mut analyze = None; - let mut document = None; - let mut types = None; - for (field, value) in fields(bytes)? { - match (field, value) { - (1, Wire::Varint(v)) => id = json!(v), - (2, Wire::Varint(v)) => ok = v != 0, - (3, Wire::Bytes(b)) => error = Some(String::from_utf8_lossy(&b).into_owned()), - (4, Wire::Bytes(b)) => analyze = Some(decode_analyze_result(&b)?), - (5, Wire::Bytes(b)) => document = Some(decode_document(&b)?), - (6, Wire::Bytes(b)) => types = Some(serde_json::from_slice(&b).unwrap_or(Value::Null)), - _ => {} - } - } - let mut object = Map::new(); - object.insert("id".into(), id); - object.insert("ok".into(), Value::Bool(ok)); - if let Some(error) = error { - object.insert("error".into(), Value::String(error)); - } - if let Some((root, files)) = analyze { - object.insert("root".into(), Value::String(root)); - object.insert("files".into(), json!(files)); - } - if let Some(document) = document { - object.insert("cst".into(), document); - } - if let Some(types) = types { - object.insert("types".into(), types); - } - Ok(Value::Object(object)) -} - -fn encode_document(document: &Value) -> Vec { - let mut out = Vec::new(); - if document.get("hasError").and_then(Value::as_bool) == Some(true) { - varint_field(&mut out, 1, 1); - } - if let Some(tree) = document.get("tree") { - message_field(&mut out, 2, &encode_node(tree)); - } - if let Some(path) = document.get("path").and_then(Value::as_str) { - string_field(&mut out, 3, path); - } - if document.get("semantic").and_then(Value::as_bool) == Some(true) { - varint_field(&mut out, 4, 1); - } - if let Some(symbols) = document.get("symbols") { - if let Ok(json) = serde_json::to_string(symbols) { - string_field(&mut out, 5, &json); - } - } - if let Some(facts) = document.get("facts").and_then(Value::as_object) { - for (key, fact) in facts { - let mut entry = Vec::new(); - string_field(&mut entry, 1, key); - message_field(&mut entry, 2, &encode_fact(fact)); - message_field(&mut out, 6, &entry); - } - } - out -} - -fn encode_fact(fact: &Value) -> Vec { - let mut out = Vec::new(); - if let Some(kind) = fact.get("kind").and_then(Value::as_str) { - string_field(&mut out, 1, kind); - } - if let Some(tier) = fact.get("tier").and_then(Value::as_str) { - string_field(&mut out, 2, tier); - } - if let Some(parent) = fact.get("parent").and_then(Value::as_str) { - string_field(&mut out, 3, parent); - } - if let Some(props) = fact.get("props").and_then(Value::as_object) { - for (name, values) in props { - let mut prop = Vec::new(); - string_field(&mut prop, 1, name); - for value in values.as_array().into_iter().flatten().filter_map(Value::as_str) { - string_field(&mut prop, 2, value); - } - message_field(&mut out, 4, &prop); - } - } - for (field, key) in [(5, "extends"), (6, "implements"), (7, "permits"), (8, "returns"), (9, "type"), (10, "params"), (11, "overrides"), (12, "annotations")] { - for value in strings(fact, key) { - string_field(&mut out, field, &value); - } - } - out -} - -fn encode_node(node: &Value) -> Vec { - let mut out = Vec::new(); - if let Some(kind) = node.get("k").and_then(Value::as_str) { - string_field(&mut out, 1, kind); - } - if node.get("n").and_then(Value::as_bool) == Some(true) { - varint_field(&mut out, 2, 1); - } - if let Some(field) = node.get("f").and_then(Value::as_str) { - string_field(&mut out, 3, field); - } - if node.get("m").and_then(Value::as_bool) == Some(true) { - varint_field(&mut out, 4, 1); - } - if let Some(position) = node.get("p").and_then(Value::as_array) { - for (field, value) in position.iter().enumerate().take(4) { - varint_field(&mut out, 5 + field as u32, value.as_u64().unwrap_or(0)); - } - } - if let Some(span) = node.get("b").and_then(Value::as_array) { - for (field, value) in span.iter().enumerate().take(2) { - varint_field(&mut out, 9 + field as u32, value.as_u64().unwrap_or(0)); - } - } - if let Some(text) = node.get("t").and_then(Value::as_str) { - string_field(&mut out, 11, text); - } - for child in node.get("c").and_then(Value::as_array).into_iter().flatten() { - message_field(&mut out, 12, &encode_node(child)); - } - for decl in strings(node, "decls") { - string_field(&mut out, 13, &decl); - } - for reference in node.get("refs").and_then(Value::as_array).into_iter().flatten() { - message_field(&mut out, 14, &encode_ref(reference)); - } - out -} - -fn encode_ref(reference: &Value) -> Vec { - let mut out = Vec::new(); - for (field, key) in [(1, "access"), (2, "outcome"), (3, "target"), (4, "reason")] { - if let Some(value) = reference.get(key).and_then(Value::as_str) { - string_field(&mut out, field, value); - } - } - if reference.get("bounded").and_then(Value::as_bool) == Some(true) { - varint_field(&mut out, 5, 1); - } - if let Some(facts) = reference.get("facts") { - if !facts.is_null() { - if let Ok(json) = serde_json::to_string(facts) { - string_field(&mut out, 6, &json); - } - } - } - out -} - -fn decode_document(bytes: &[u8]) -> Result { - let mut object = Map::new(); - object.insert("hasError".into(), Value::Bool(false)); - object.insert("semantic".into(), Value::Bool(false)); - object.insert("symbols".into(), json!([])); - object.insert("facts".into(), json!({})); - for (field, value) in fields(bytes)? { - match (field, value) { - (1, Wire::Varint(v)) => object.insert("hasError".into(), Value::Bool(v != 0)), - (2, Wire::Bytes(b)) => object.insert("tree".into(), decode_node(&b)?), - (3, Wire::Bytes(b)) => object.insert("path".into(), Value::String(text(&b))), - (4, Wire::Varint(v)) => object.insert("semantic".into(), Value::Bool(v != 0)), - (5, Wire::Bytes(b)) => object.insert("symbols".into(), serde_json::from_slice(&b).unwrap_or_else(|_| json!([]))), - (6, Wire::Bytes(b)) => { - let (key, fact) = decode_fact_entry(&b)?; - object - .entry("facts") - .or_insert_with(|| json!({})) - .as_object_mut() - .expect("facts is an object") - .insert(key, fact); - None - } - _ => None, - }; - } - Ok(Value::Object(object)) -} - -fn decode_fact_entry(bytes: &[u8]) -> Result<(String, Value)> { - let mut key = String::new(); - let mut fact = json!({}); - for (field, value) in fields(bytes)? { - match (field, value) { - (1, Wire::Bytes(b)) => key = text(&b), - (2, Wire::Bytes(b)) => fact = decode_fact(&b)?, - _ => {} - } - } - Ok((key, fact)) -} - -fn decode_fact(bytes: &[u8]) -> Result { - let mut object = Map::new(); - for (field, value) in fields(bytes)? { - match (field, value) { - (1, Wire::Bytes(b)) => object.insert("kind".into(), Value::String(text(&b))), - (2, Wire::Bytes(b)) => object.insert("tier".into(), Value::String(text(&b))), - (3, Wire::Bytes(b)) => object.insert("parent".into(), Value::String(text(&b))), - (4, Wire::Bytes(b)) => { - let (name, values) = decode_prop(&b)?; - let props = object.entry("props").or_insert_with(|| json!({})); - props.as_object_mut().expect("props").insert(name, json!(values)); - None - } - (5, Wire::Bytes(b)) => push_string(&mut object, "extends", text(&b)), - (6, Wire::Bytes(b)) => push_string(&mut object, "implements", text(&b)), - (7, Wire::Bytes(b)) => push_string(&mut object, "permits", text(&b)), - (8, Wire::Bytes(b)) => push_string(&mut object, "returns", text(&b)), - (9, Wire::Bytes(b)) => push_string(&mut object, "type", text(&b)), - (10, Wire::Bytes(b)) => push_string(&mut object, "params", text(&b)), - (11, Wire::Bytes(b)) => push_string(&mut object, "overrides", text(&b)), - (12, Wire::Bytes(b)) => push_string(&mut object, "annotations", text(&b)), - _ => None, - }; - } - Ok(Value::Object(object)) -} - -fn decode_prop(bytes: &[u8]) -> Result<(String, Vec)> { - let mut name = String::new(); - let mut values = Vec::new(); - for (field, value) in fields(bytes)? { - match (field, value) { - (1, Wire::Bytes(b)) => name = text(&b), - (2, Wire::Bytes(b)) => values.push(text(&b)), - _ => {} - } - } - Ok((name, values)) -} - -fn decode_node(bytes: &[u8]) -> Result { - let mut object = Map::new(); - let mut position = [0u64; 4]; - let mut span = [0u64; 2]; - let mut saw_position = false; - let mut saw_span = false; - for (field, value) in fields(bytes)? { - match (field, value) { - (1, Wire::Bytes(b)) => object.insert("k".into(), Value::String(text(&b))), - (2, Wire::Varint(v)) if v != 0 => object.insert("n".into(), Value::Bool(true)), - (3, Wire::Bytes(b)) => object.insert("f".into(), Value::String(text(&b))), - (4, Wire::Varint(v)) if v != 0 => object.insert("m".into(), Value::Bool(true)), - (5..=8, Wire::Varint(v)) => { - position[(field - 5) as usize] = v; - saw_position = true; - None - } - (9 | 10, Wire::Varint(v)) => { - span[(field - 9) as usize] = v; - saw_span = true; - None - } - (11, Wire::Bytes(b)) => object.insert("t".into(), Value::String(text(&b))), - (12, Wire::Bytes(b)) => { - let children = object.entry("c").or_insert_with(|| json!([])); - children.as_array_mut().expect("children").push(decode_node(&b)?); - None - } - (13, Wire::Bytes(b)) => push_string(&mut object, "decls", text(&b)), - (14, Wire::Bytes(b)) => { - let refs = object.entry("refs").or_insert_with(|| json!([])); - refs.as_array_mut().expect("refs").push(decode_ref(&b)?); - None - } - _ => None, - }; - } - if saw_position { - object.insert("p".into(), json!(position)); - } - if saw_span { - object.insert("b".into(), json!(span)); - } - Ok(Value::Object(object)) -} - -fn decode_ref(bytes: &[u8]) -> Result { - let mut object = Map::new(); - for (field, value) in fields(bytes)? { - match (field, value) { - (1, Wire::Bytes(b)) => object.insert("access".into(), Value::String(text(&b))), - (2, Wire::Bytes(b)) => object.insert("outcome".into(), Value::String(text(&b))), - (3, Wire::Bytes(b)) => object.insert("target".into(), Value::String(text(&b))), - (4, Wire::Bytes(b)) => object.insert("reason".into(), Value::String(text(&b))), - (5, Wire::Varint(v)) if v != 0 => object.insert("bounded".into(), Value::Bool(true)), - (6, Wire::Bytes(b)) => object.insert("facts".into(), serde_json::from_slice(&b).unwrap_or(Value::Null)), - _ => None, - }; - } - Ok(Value::Object(object)) -} - -fn decode_analyze(bytes: &[u8]) -> Result<(String, Vec, Vec)> { - let mut root = String::new(); - let mut files = Vec::new(); - let mut classpath = Vec::new(); - for (field, value) in fields(bytes)? { - match (field, value) { - (1, Wire::Bytes(b)) => root = text(&b), - (2, Wire::Bytes(b)) => files.push(text(&b)), - (3, Wire::Bytes(b)) => classpath.push(text(&b)), - _ => {} - } - } - Ok((root, files, classpath)) -} - -fn decode_analyze_result(bytes: &[u8]) -> Result<(String, Vec)> { - let mut root = String::new(); - let mut files = Vec::new(); - for (field, value) in fields(bytes)? { - match (field, value) { - (1, Wire::Bytes(b)) => root = text(&b), - (2, Wire::Bytes(b)) => files.push(text(&b)), - _ => {} - } - } - Ok((root, files)) -} - -fn decode_path(bytes: &[u8]) -> Result { - for (field, value) in fields(bytes)? { - if field == 1 { - if let Wire::Bytes(b) = value { - return Ok(text(&b)); - } - } - } - Ok(String::new()) -} - -fn decode_names(bytes: &[u8]) -> Result> { - let mut names = Vec::new(); - for (field, value) in fields(bytes)? { - if field == 1 { - if let Wire::Bytes(b) = value { - names.push(text(&b)); - } - } - } - Ok(names) -} - -fn decode_source(bytes: &[u8]) -> Result { - decode_path(bytes) -} - -fn push_string(object: &mut Map, key: &str, value: String) -> Option { - let array = object.entry(key).or_insert_with(|| json!([])); - array.as_array_mut().expect(key).push(Value::String(value)); - None -} - -fn strings(value: &Value, key: &str) -> Vec { - value - .get(key) - .and_then(Value::as_array) - .into_iter() - .flatten() - .filter_map(|v| v.as_str().map(str::to_string)) - .collect() -} - -fn text(bytes: &[u8]) -> String { - String::from_utf8_lossy(bytes).into_owned() -} - -enum Wire { - Varint(u64), - Bytes(Vec), -} - -fn fields(bytes: &[u8]) -> Result> { - let mut out = Vec::new(); - let mut i = 0; - while i < bytes.len() { - let (key, next) = varint_at(bytes, i)?; - i = next; - let field = (key >> 3) as u32; - match key & 7 { - 0 => { - let (value, next) = varint_at(bytes, i)?; - i = next; - out.push((field, Wire::Varint(value))); - } - 2 => { - let (len, next) = varint_at(bytes, i)?; - i = next; - let len = len as usize; - if i + len > bytes.len() { - return Err(anyhow!("truncated protobuf field {field}")); - } - out.push((field, Wire::Bytes(bytes[i..i + len].to_vec()))); - i += len; - } - 1 => i += 8, - 5 => i += 4, - other => return Err(anyhow!("unsupported protobuf wire type {other}")), - } - } - Ok(out) -} - -fn varint_at(bytes: &[u8], mut i: usize) -> Result<(u64, usize)> { - let mut value = 0u64; - let mut shift = 0; - while i < bytes.len() && shift < 64 { - let byte = bytes[i]; - i += 1; - value |= ((byte & 0x7f) as u64) << shift; - if byte & 0x80 == 0 { - return Ok((value, i)); - } - shift += 7; - } - Err(anyhow!("truncated varint")) -} - -fn varint_field(out: &mut Vec, field: u32, value: u64) { - varint(out, (field as u64) << 3); - varint(out, value); -} - -fn string_field(out: &mut Vec, field: u32, value: &str) { - bytes_field(out, field, value.as_bytes()); -} - -fn message_field(out: &mut Vec, field: u32, value: &[u8]) { - bytes_field(out, field, value); -} - -fn bytes_field(out: &mut Vec, field: u32, value: &[u8]) { - varint(out, ((field as u64) << 3) | 2); - varint(out, value.len() as u64); - out.extend_from_slice(value); -} - -fn varint(out: &mut Vec, mut value: u64) { - loop { - let mut byte = (value & 0x7f) as u8; - value >>= 7; - if value != 0 { - byte |= 0x80; - } - out.push(byte); - if value == 0 { - break; - } - } -} - -pub fn encode_request(id: u32, request: &Request) -> Vec { - let mut out = Vec::new(); - varint_field(&mut out, 1, id as u64); - match request { - Request::Analyze { root, files, classpath } => { - let mut msg = Vec::new(); - string_field(&mut msg, 1, root); - for file in files { - string_field(&mut msg, 2, file); - } - for entry in classpath { - string_field(&mut msg, 3, entry); - } - message_field(&mut out, 2, &msg); - } - Request::Cst { path } => { - let mut msg = Vec::new(); - string_field(&mut msg, 1, path); - message_field(&mut out, 3, &msg); - } - Request::Types { names } => { - let mut msg = Vec::new(); - for name in names { - string_field(&mut msg, 1, name); - } - message_field(&mut out, 4, &msg); - } - Request::Parse { source } => { - let mut msg = Vec::new(); - string_field(&mut msg, 1, source); - message_field(&mut out, 5, &msg); - } - Request::Quit => varint_field(&mut out, 6, 1), - } - out -} diff --git a/semsitter-bridge/tests/cst.rs b/semsitter-bridge/tests/cst.rs deleted file mode 100644 index 42b81862bbe..00000000000 --- a/semsitter-bridge/tests/cst.rs +++ /dev/null @@ -1,128 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ - -use std::path::{Path, PathBuf}; - -use serde_json::Value; - -fn write(dir: &Path, rel: &str, text: &str) -> PathBuf { - let path = dir.join(rel); - std::fs::create_dir_all(path.parent().unwrap()).unwrap(); - std::fs::write(&path, text).unwrap(); - path -} - -fn find<'a>(node: &'a Value, text: &str, out: &mut Vec<&'a Value>) { - if node.get("t").and_then(Value::as_str) == Some(text) { - out.push(node); - } - for child in node.get("c").and_then(Value::as_array).into_iter().flatten() { - find(child, text, out); - } -} - -fn nodes<'a>(cst: &'a Value, text: &str) -> Vec<&'a Value> { - let mut out = Vec::new(); - find(&cst["tree"], text, &mut out); - out -} - -#[test] -fn cst_carries_declarations_references_and_facts() { - let dir = tempfile::tempdir().unwrap(); - let a = write( - dir.path(), - "src/p/A.java", - "package p;\nimport java.util.List;\npublic class A {\n int f;\n String name(List l) { return l.get(f); }\n}\n", - ); - let b = write( - dir.path(), - "src/p/B.java", - "package p;\nclass B extends A {\n void run() { A a = new A(); String s = a.name(null); }\n}\n", - ); - - let analysis = semsitter_bridge::analyze(dir.path(), &[a.clone(), b], &[]).unwrap(); - assert_eq!(analysis.files(), ["src/p/A.java", "src/p/B.java"]); - - let cst_a = analysis.cst(a.to_str().unwrap()).unwrap(); - assert_eq!(cst_a["path"], "src/p/A.java"); - assert_eq!(cst_a["semantic"], true); - assert_eq!(cst_a["tree"]["k"], "program"); - assert_eq!(cst_a["hasError"], false); - - let class_name = nodes(&cst_a, "A")[0]; - assert_eq!(class_name["decls"][0], "p.A"); - assert_eq!(class_name["p"], serde_json::json!([3, 13, 3, 14])); - - let get = nodes(&cst_a, "get")[0]; - let target = get["refs"][0]["target"].as_str().unwrap(); - assert!(target.starts_with("java.util.List"), "{target}"); - - let field_use = nodes(&cst_a, "f")[1]; - assert_eq!(field_use["refs"][0]["target"], "p.A#f"); - - let cst_b = analysis.cst("src/p/B.java").unwrap(); - assert_eq!(cst_b["facts"]["p.B"]["extends"][0], "p.A"); - let name_call = nodes(&cst_b, "name")[0]; - assert_eq!(name_call["refs"][0]["target"], "p.A#name(java.util.List`1)"); - assert_eq!(cst_b["facts"]["p.A#name(java.util.List`1)"]["returns"][0], "java.lang.String"); -} - -#[test] -fn types_come_from_the_project_graph_and_the_jdk_table_with_their_supertypes() { - let dir = tempfile::tempdir().unwrap(); - let a = write(dir.path(), "p/A.java", "package p;\npublic class A extends java.util.ArrayList { int f; void m() {} }\n"); - let analysis = semsitter_bridge::analyze(dir.path(), &[a], &[]).unwrap(); - let types = analysis.types(&["p.A".to_string()]); - assert_eq!(types["p.A"]["origin"], "project"); - assert_eq!(types["p.A"]["super"], "java.util.ArrayList`1"); - let member_ids: Vec<&str> = types["p.A"]["members"].as_array().unwrap().iter().map(|m| m["id"].as_str().unwrap()).collect(); - assert!(member_ids.contains(&"p.A#m()"), "{member_ids:?}"); - assert_eq!(types["java.util.ArrayList"]["origin"], "jdk"); - assert_eq!(types["java.util.AbstractList"]["super"], "java.util.AbstractCollection"); - let list_get = types["java.util.List"]["members"].as_array().unwrap().iter().find(|m| m["name"] == "get").unwrap(); - assert_eq!(list_get["desc"], "(I)Ljava/lang/Object;"); -} - -#[test] -fn dump_json_writes_the_udg_the_semantic_dump_and_the_cst() { - let dir = tempfile::tempdir().unwrap(); - let dump = tempfile::tempdir().unwrap(); - let file = write(dir.path(), "A.java", "class A { int f; }"); - semsitter_bridge::analyze_dumping(dir.path(), &[file], &[], Some(dump.path())).unwrap(); - assert!(dump.path().join("udg.json").is_file()); - let semdump = std::fs::read_to_string(dump.path().join("semdump/A.java.semdump.json")).unwrap(); - assert!(semdump.contains("semantic-dump/1"), "{semdump}"); - let cst = std::fs::read_to_string(dump.path().join("cst/A.java.json")).unwrap(); - assert!(cst.contains("\"k\": \"program\""), "{cst}"); -} - -#[test] -fn a_bom_is_neither_a_syntax_error_nor_a_column() { - let cst = semsitter_bridge::parse_only(b"\xEF\xBB\xBFclass A {}").unwrap(); - assert_eq!(cst["hasError"], false); - assert_eq!(nodes(&cst, "class")[0]["p"], serde_json::json!([1, 0, 1, 5])); -} - -#[test] -fn parse_only_keeps_every_token_and_utf16_columns() { - let cst = semsitter_bridge::parse_only("class É { String s = \"é\"; }".as_bytes()).unwrap(); - let literal = &nodes(&cst, "\"é\"")[0]; - assert_eq!(literal["k"], "string_literal"); - assert_eq!(literal["p"], serde_json::json!([1, 21, 1, 24])); - assert_eq!(nodes(&cst, "{").len(), 1); -} diff --git a/semsitter-bridge/tests/jsonl.rs b/semsitter-bridge/tests/jsonl.rs deleted file mode 100644 index 7c9f99d04f2..00000000000 --- a/semsitter-bridge/tests/jsonl.rs +++ /dev/null @@ -1,58 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ - -use serde_json::{Value, json}; -use semsitter_bridge::server::serve; - -fn exchange(requests: &[Value]) -> Vec { - let input: String = requests.iter().map(|r| format!("{r}\n")).collect(); - let mut output = Vec::new(); - serve(input.as_bytes(), &mut output).unwrap(); - String::from_utf8(output).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect() -} - -#[test] -fn analyze_then_cst_then_quit() { - let dir = tempfile::tempdir().unwrap(); - let file = dir.path().join("A.java"); - std::fs::write(&file, "class A { int f; int g() { return f; } }").unwrap(); - let responses = exchange(&[ - json!({"id": 1, "op": "analyze", "root": dir.path(), "files": [file], "classpath": []}), - json!({"id": 2, "op": "cst", "path": file}), - json!({"id": 3, "op": "cst", "path": "Missing.java"}), - json!({"id": 4, "op": "quit"}), - json!({"id": 5, "op": "parse", "source": "class B {}"}), - ]); - assert_eq!(responses.len(), 4); - assert_eq!(responses[0]["files"], json!(["A.java"])); - assert_eq!(responses[1]["id"], 2); - assert_eq!(responses[1]["cst"]["symbols"].as_array().unwrap().iter().filter(|s| s["name"] == "f").count(), 1); - assert_eq!(responses[2]["ok"], false); - assert_eq!(responses[3], json!({"id": 4, "ok": true})); -} - -#[test] -fn parse_needs_no_analysis_and_errors_keep_the_server_alive() { - let responses = exchange(&[ - json!({"id": 1, "op": "cst", "path": "A.java"}), - json!({"id": 2, "op": "nope"}), - json!({"id": 3, "op": "parse", "source": "class B {}"}), - ]); - assert_eq!(responses[0]["ok"], false); - assert_eq!(responses[1]["error"], "unknown op: nope"); - assert_eq!(responses[2]["cst"]["tree"]["k"], "program"); -} diff --git a/semsitter-bridge/tests/shm.rs b/semsitter-bridge/tests/shm.rs deleted file mode 100644 index 645baab9662..00000000000 --- a/semsitter-bridge/tests/shm.rs +++ /dev/null @@ -1,79 +0,0 @@ -/* - * SonarQube Java - * Copyright (C) SonarSource Sàrl - * mailto:info AT sonarsource DOT com - * - * You can redistribute and/or modify this program under the terms of - * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. - * - * This program 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 Sonar Source-Available License for more details. - * - * You should have received a copy of the Sonar Source-Available License - * along with this program; if not, see https://sonarsource.com/license/ssal/ - */ - -use std::io::{Read, Write}; -use std::thread; - -use semsitter_bridge::server::serve_shm; -use semsitter_bridge::shm::{Link, ATTACHED, READY}; -use semsitter_bridge::wire::{self, Request}; - -#[test] -fn protobuf_round_trips_a_tree() { - let document = serde_json::json!({ - "ok": true, - "cst": { - "hasError": false, - "semantic": true, - "path": "A.java", - "symbols": [{"id": "A", "kind": "class"}], - "facts": {"A": {"kind": "class", "tier": "IN_PROJECT", "extends": ["java.lang.Object"]}}, - "tree": {"k": "program", "n": true, "p": [1, 0, 1, 10], "b": [0, 10], "t": "class A {}", "decls": ["A"], - "refs": [{"access": "r", "outcome": "bound", "target": "A"}]} - } - }); - let decoded = wire::decode_response(&wire::encode_response(7, &document)).unwrap(); - assert_eq!(decoded["id"], 7); - assert_eq!(decoded["cst"]["tree"]["k"], "program"); - assert_eq!(decoded["cst"]["tree"]["decls"], serde_json::json!(["A"])); - assert_eq!(decoded["cst"]["tree"]["refs"][0]["target"], "A"); - assert_eq!(decoded["cst"]["symbols"][0]["id"], "A"); - assert_eq!(decoded["cst"]["facts"]["A"]["extends"], serde_json::json!(["java.lang.Object"])); -} - -#[cfg(unix)] -#[test] -fn shared_memory_serves_a_parse() { - use std::os::unix::net::UnixStream; - - let (server_side, mut client_side) = UnixStream::pair().unwrap(); - let server = thread::spawn(move || { - let input = server_side.try_clone().unwrap(); - serve_shm(input, server_side, None).unwrap(); - }); - - let mut ready = [0u8; 1]; - client_side.read_exact(&mut ready).unwrap(); - assert_eq!(ready[0], READY); - let mut link = Link::open(&std::process::id().to_string()).unwrap(); - client_side.write_all(&[ATTACHED]).unwrap(); - client_side.flush().unwrap(); - - let mut to_server = client_side.try_clone().unwrap(); - let mut from_server = client_side; - let request = wire::encode_request(3, &Request::Parse { source: "class B {}".into() }); - link.write_request(&request, &mut to_server, &mut from_server).unwrap(); - let response = link.read_response(&mut from_server, &mut to_server).unwrap(); - let decoded = wire::decode_response(&response).unwrap(); - assert_eq!(decoded["ok"], true); - assert_eq!(decoded["cst"]["tree"]["k"], "program"); - - let quit = wire::encode_request(4, &Request::Quit); - link.write_request(&quit, &mut to_server, &mut from_server).unwrap(); - let _ = link.read_response(&mut from_server, &mut to_server).unwrap(); - server.join().unwrap(); -} diff --git a/sonar-java-plugin/pom.xml b/sonar-java-plugin/pom.xml index bcadd0df715..3a8bc75bd94 100644 --- a/sonar-java-plugin/pom.xml +++ b/sonar-java-plugin/pom.xml @@ -152,13 +152,6 @@ ${project.build.directory}/generated-resources/profiles - - ${maven.multiModuleProjectDirectory}/semsitter-bridge/prebuilt - - bridge-*.xz - - bridge - @@ -191,7 +184,7 @@ - 45000000 + 100000000 10000000 ${project.build.directory}/${project.build.finalName}.jar