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..8eb2710497b 100644 --- a/java-frontend/src/main/java/org/sonar/java/JavaFrontend.java +++ b/java-frontend/src/main/java/org/sonar/java/JavaFrontend.java @@ -239,6 +239,7 @@ private void scanBatch(BatchModeContext context, List b Set environmentsCleaners = new HashSet<>(); JParserConfig.Mode.BATCH .create(javaVersion, context.getClasspath(), sonarComponents.shouldIgnoreUnnamedModuleForSplitPackage()) + .withSourcepath(sonarComponents.getSourcepath()) .parse(batchFiles, sonarComponents::analysisCancelled, analysisProgress, (input, result) -> scanAsBatchCallback(input, result, context, environmentsCleaners)); // Due to a bug in ECJ, JAR files remain locked after the analysis on Windows, we unlock them manually, at the end of each batches. See SONARJAVA-3609. environmentsCleaners.forEach(Runnable::run); diff --git a/java-frontend/src/main/java/org/sonar/java/SonarComponents.java b/java-frontend/src/main/java/org/sonar/java/SonarComponents.java index 992dd4f39cb..3bee387b8c0 100644 --- a/java-frontend/src/main/java/org/sonar/java/SonarComponents.java +++ b/java-frontend/src/main/java/org/sonar/java/SonarComponents.java @@ -112,6 +112,12 @@ public class SonarComponents extends CheckRegistrar.RegistrarContext { */ public static final String SONAR_FAIL_ON_STACKOVERFLOW = "sonar.java.internal.failOnStackOverflow"; + /** + * Comma-separated directories whose Java files are read to resolve types, without being analyzed. + * Relative paths are resolved against the module base directory. Only the SemSitter bridge reads them. + */ + public static final String SONAR_SOURCEPATH_KEY = "sonar.java.internal.sourcepath"; + private static final Version SONARLINT_6_3 = Version.parse("6.3"); private static final Version SONARQUBE_9_2 = Version.parse("9.2"); @VisibleForTesting @@ -506,6 +512,17 @@ public boolean shouldFailOnStackOverflow() { return context.config().getBoolean(SONAR_FAIL_ON_STACKOVERFLOW).orElse(true); } + public List getSourcepath() { + if (context == null || context.config() == null) { + return List.of(); + } + return Stream.of(context.config().getStringArray(SONAR_SOURCEPATH_KEY)) + .map(String::trim) + .filter(path -> !path.isEmpty()) + .map(context.fileSystem()::resolvePath) + .toList(); + } + private static long computeIdealBatchSize() { // We take a fraction of the total memory available though -Xmx. // If we assume that the average size of a file is 5KB and the average CI should have 1GB of memory, diff --git a/java-frontend/src/main/java/org/sonar/java/ast/JavaAstScanner.java b/java-frontend/src/main/java/org/sonar/java/ast/JavaAstScanner.java index 250505e02af..f0b7ee93a67 100644 --- a/java-frontend/src/main/java/org/sonar/java/ast/JavaAstScanner.java +++ b/java-frontend/src/main/java/org/sonar/java/ast/JavaAstScanner.java @@ -110,6 +110,7 @@ private void scan(Iterable inputFiles, Consumer classpath; final boolean shouldIgnoreUnnamedModuleForSplitPackage; private SemsitterBridge bridge; + private List sourcepath = List.of(); private JParserConfig(JavaVersion javaVersion, List classpath, boolean shouldIgnoreUnnamedModuleForSplitPackage) { this.javaVersion = javaVersion; @@ -57,6 +62,14 @@ private JParserConfig(JavaVersion javaVersion, List classpath, boolean sho public abstract void parse(Iterable inputFiles, BooleanSupplier isCanceled, AnalysisProgress analysisProgress, BiConsumer action); + /** + * Directories whose Java files are read to resolve types, without being analyzed. + */ + public JParserConfig withSourcepath(List sourcepath) { + this.sourcepath = List.copyOf(sourcepath); + return this; + } + public enum Mode { BATCH(Batch::new), FILE_BY_FILE(FileByFile::new); @@ -211,7 +224,7 @@ void openBridge(List files) { Path root = files.get(0).path().toAbsolutePath().getParent(); bridge.analyze( root == null ? Path.of(".") : root, - files.stream().map(file -> file.path().toAbsolutePath()).toList(), + filesToScan(files.stream().map(file -> file.path().toAbsolutePath()).toList(), sourcepath), jdkClasspath()); } } catch (Exception failure) { @@ -220,6 +233,47 @@ void openBridge(List files) { } } + /** + * The files the bridge scans: the analyzed files, then the Java files under the sourcepath directories that are not + * analyzed. The bridge resolves types across all of them, but only the analyzed files are parsed and reach the rules. + */ + @VisibleForTesting + static List filesToScan(List analyzedFiles, List sourcepath) { + if (sourcepath.isEmpty()) { + return analyzedFiles; + } + Set seen = new HashSet<>(); + analyzedFiles.forEach(file -> seen.add(realPath(file))); + List result = new ArrayList<>(analyzedFiles); + for (File directory : sourcepath) { + if (!directory.isDirectory()) { + LOG.debug("Skipping sourcepath entry that is not a directory: {}", directory); + continue; + } + try (Stream walk = Files.walk(directory.toPath())) { + walk.filter(file -> file.toString().endsWith(".java") && Files.isRegularFile(file)) + .map(Path::toAbsolutePath) + .forEach(file -> { + if (seen.add(realPath(file))) { + result.add(file); + } + }); + } catch (IOException | UncheckedIOException e) { + LOG.warn("Could not read sourcepath directory {} ({})", directory, e.toString()); + } + } + LOG.debug("SemSitter reads {} sourcepath files next to {} analyzed files", result.size() - analyzedFiles.size(), analyzedFiles.size()); + return result; + } + + private static Path realPath(Path file) { + try { + return file.toRealPath(); + } catch (IOException e) { + return file.normalize(); + } + } + static List jdkClasspath() { return List.of(Path.of(System.getProperty("java.home"), "lib", "modules").toString()); } diff --git a/java-frontend/src/test/java/org/sonar/java/SonarComponentsTest.java b/java-frontend/src/test/java/org/sonar/java/SonarComponentsTest.java index ea268b97501..b09fd5ac1d2 100644 --- a/java-frontend/src/test/java/org/sonar/java/SonarComponentsTest.java +++ b/java-frontend/src/test/java/org/sonar/java/SonarComponentsTest.java @@ -44,6 +44,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.CsvSource; @@ -1140,6 +1141,24 @@ void shouldIgnoreUnnamedModuleForSplitPackage_returns_true_when_analysis_paramet assertThat(sonarComponents.shouldIgnoreUnnamedModuleForSplitPackage()).isTrue(); } + @Test + void getSourcepath_is_empty_by_default() { + SonarComponents sonarComponents = new SonarComponents(null, null, null, null, null, null); + sonarComponents.setSensorContext(SensorContextTester.create(new File("")).setSettings(new MapSettings())); + assertThat(sonarComponents.getSourcepath()).isEmpty(); + } + + @Test + void getSourcepath_resolves_entries_against_the_base_directory(@TempDir File baseDir) throws IOException { + File absolute = new File(baseDir, "other/src/main/java").getAbsoluteFile(); + MapSettings settings = new MapSettings().setProperty("sonar.java.internal.sourcepath", " src/main/java , ," + absolute.getPath()); + SonarComponents sonarComponents = new SonarComponents(null, null, null, null, null, null); + sonarComponents.setSensorContext(SensorContextTester.create(baseDir).setSettings(settings)); + assertThat(sonarComponents.getSourcepath()) + .extracting(File::getCanonicalFile) + .containsExactly(new File(baseDir, "src/main/java").getCanonicalFile(), absolute.getCanonicalFile()); + } + @Test void shouldFailOnStackOverflow_returns_true_by_default() { MapSettings settings = new MapSettings(); diff --git a/java-frontend/src/test/java/org/sonar/java/model/JParserConfigTest.java b/java-frontend/src/test/java/org/sonar/java/model/JParserConfigTest.java index 78d488f74b7..b06b25271b4 100644 --- a/java-frontend/src/test/java/org/sonar/java/model/JParserConfigTest.java +++ b/java-frontend/src/test/java/org/sonar/java/model/JParserConfigTest.java @@ -16,11 +16,24 @@ */ package org.sonar.java.model; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; import org.slf4j.event.Level; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.java.AnalysisProgress; +import org.sonar.java.TestUtils; import org.sonar.java.testing.ThreadLocalLogTester; +import org.sonar.plugins.java.api.semantic.Symbol; +import org.sonar.plugins.java.api.tree.BaseTreeVisitor; +import org.sonar.plugins.java.api.tree.MethodInvocationTree; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.java.model.JParserConfig.shouldEnablePreviewFlag; @@ -55,4 +68,67 @@ void a_debug_message_is_logged_when_shouldIgnoreUnnamedModuleForSplitPackage_is_ JParserConfig.Mode.BATCH.create(new JavaVersionImpl(17), Collections.emptyList(), true); assertThat(logTester.logs()).containsExactly("The Java analyzer will ignore the unnamed module for split packages."); } + + @Test + void files_to_scan_are_the_analyzed_files_when_there_is_no_sourcepath(@TempDir Path root) throws IOException { + Path analyzed = Files.writeString(root.resolve("A.java"), "class A {}"); + assertThat(JParserConfig.filesToScan(List.of(analyzed), List.of())).containsExactly(analyzed); + } + + @Test + void files_to_scan_add_the_java_files_of_the_sourcepath_once(@TempDir Path root) throws IOException { + Path sources = Files.createDirectories(root.resolve("src/main/java/p")); + Path analyzed = Files.writeString(sources.resolve("A.java"), "package p; class A {}"); + Path other = Files.writeString(sources.resolve("B.java"), "package p; class B {}"); + Path nested = Files.writeString(Files.createDirectories(sources.resolve("q")).resolve("C.java"), "package p.q; class C {}"); + Files.writeString(sources.resolve("notes.txt"), "not java"); + File sourceRoot = root.resolve("src/main/java").toFile(); + + List files = JParserConfig.filesToScan(List.of(analyzed), List.of(sourceRoot, sourceRoot, root.resolve("missing").toFile())); + + assertThat(files.get(0)).isEqualTo(analyzed); + assertThat(files).containsExactlyInAnyOrder(analyzed, other.toAbsolutePath(), nested.toAbsolutePath()); + } + + @Test + void sourcepath_files_resolve_types_but_are_not_analyzed(@TempDir Path root) throws IOException { + Path sources = root.resolve("src/main/java"); + Files.writeString(Files.createDirectories(sources.resolve("com/a")).resolve("Util.java"), """ + package com.a; + public class Util { + public static String make() { return ""; } + } + """); + Path example = Files.writeString(Files.createDirectories(sources.resolve("com/b")).resolve("Example.java"), """ + package com.b; + import com.a.Util; + class Example { + int m() { return Util.make().length(); } + } + """); + InputFile inputFile = TestUtils.inputFile(example.toFile()); + List parsed = new ArrayList<>(); + List invoked = new ArrayList<>(); + + JParserConfig.Mode.BATCH.create(new JavaVersionImpl(17), Collections.emptyList()) + .withSourcepath(List.of(sources.toFile())) + .parse(List.of(inputFile), () -> false, new AnalysisProgress(1), (file, result) -> { + parsed.add(file); + try { + result.get().accept(new BaseTreeVisitor() { + @Override + public void visitMethodInvocation(MethodInvocationTree tree) { + invoked.add(tree.methodSymbol()); + super.visitMethodInvocation(tree); + } + }); + } catch (Exception e) { + throw new IllegalStateException(e); + } + }); + + assertThat(parsed).containsExactly(inputFile); + assertThat(invoked).extracting(symbol -> symbol.owner().type().fullyQualifiedName() + "#" + symbol.name()) + .containsExactlyInAnyOrder("java.lang.String#length", "com.a.Util#make"); + } } diff --git a/semsitter-bridge/Cargo.lock b/semsitter-bridge/Cargo.lock index 84b6c264565..b929067b773 100644 --- a/semsitter-bridge/Cargo.lock +++ b/semsitter-bridge/Cargo.lock @@ -330,7 +330,7 @@ checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" [[package]] name = "fswatch" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "ignore", @@ -889,6 +889,7 @@ dependencies = [ "tree-sitter-java", "udg-family-jvm", "udg-gen", + "udg-lang-java", "udg-lang-sdk", "udg-semantic", ] @@ -1201,7 +1202,7 @@ checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" [[package]] name = "udg-boundary" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "flate2", @@ -1216,7 +1217,7 @@ dependencies = [ [[package]] name = "udg-buffer" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "rustc-hash", "tracing", @@ -1227,7 +1228,7 @@ dependencies = [ [[package]] name = "udg-embed" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "clap", @@ -1245,7 +1246,7 @@ dependencies = [ [[package]] name = "udg-engine" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "ignore", @@ -1266,7 +1267,7 @@ dependencies = [ [[package]] name = "udg-engine-plugins" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "rayon", "tracing", @@ -1282,7 +1283,7 @@ dependencies = [ [[package]] name = "udg-family-jvm" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "encoding_rs", @@ -1302,7 +1303,7 @@ dependencies = [ [[package]] name = "udg-gen" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "clap", @@ -1335,7 +1336,7 @@ dependencies = [ [[package]] name = "udg-lang-java" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "rustc-hash", @@ -1355,7 +1356,7 @@ dependencies = [ [[package]] name = "udg-lang-sdk" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "encoding_rs", "rustc-hash", @@ -1366,7 +1367,7 @@ dependencies = [ [[package]] name = "udg-live" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "clap", @@ -1383,7 +1384,7 @@ dependencies = [ [[package]] name = "udg-live-serve" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "fswatch", @@ -1404,7 +1405,7 @@ dependencies = [ [[package]] name = "udg-model" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "clap", "rustc-hash", @@ -1418,7 +1419,7 @@ dependencies = [ [[package]] name = "udg-packed-table" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "flate2", ] @@ -1426,7 +1427,7 @@ dependencies = [ [[package]] name = "udg-sdk-tree-sitter" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "rustc-hash", @@ -1439,7 +1440,7 @@ dependencies = [ [[package]] name = "udg-semantic" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "udg-lang-sdk", "udg-model", @@ -1450,7 +1451,7 @@ dependencies = [ [[package]] name = "udg-semantic-core" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "serde_json", "udg-lang-sdk", @@ -1460,7 +1461,7 @@ dependencies = [ [[package]] name = "udg-semantic-session" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "rayon", @@ -1476,7 +1477,7 @@ dependencies = [ [[package]] name = "udg-store" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "clap", @@ -1496,7 +1497,7 @@ dependencies = [ [[package]] name = "udg-ucfg" version = "0.1.0" -source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=22d846d83268f460e68912d1fd3ebb8843bafce9#22d846d83268f460e68912d1fd3ebb8843bafce9" +source = "git+ssh://git@github.com/SonarSource/semsitter.git?rev=71bc323e0e39a2566d757f605e1cb022d45fa737#71bc323e0e39a2566d757f605e1cb022d45fa737" dependencies = [ "anyhow", "clap", diff --git a/semsitter-bridge/Cargo.toml b/semsitter-bridge/Cargo.toml index 9a47e4ed7dd..fbdf07d2fd5 100644 --- a/semsitter-bridge/Cargo.toml +++ b/semsitter-bridge/Cargo.toml @@ -15,10 +15,11 @@ 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" } +udg-gen = { git = "ssh://git@github.com/SonarSource/semsitter.git", rev = "71bc323e0e39a2566d757f605e1cb022d45fa737", default-features = false, features = ["lang-java"] } +udg-family-jvm = { git = "ssh://git@github.com/SonarSource/semsitter.git", rev = "71bc323e0e39a2566d757f605e1cb022d45fa737" } +udg-lang-java = { git = "ssh://git@github.com/SonarSource/semsitter.git", rev = "71bc323e0e39a2566d757f605e1cb022d45fa737" } +udg-lang-sdk = { git = "ssh://git@github.com/SonarSource/semsitter.git", rev = "71bc323e0e39a2566d757f605e1cb022d45fa737", features = ["unstable-sdk"] } +udg-semantic = { git = "ssh://git@github.com/SonarSource/semsitter.git", rev = "71bc323e0e39a2566d757f605e1cb022d45fa737" } [patch.crates-io] tree-sitter-java = { git = "https://github.com/rombirli/tree-sitter-java", rev = "324af3f22ee0a4579a1d5c1a8bc588631ec4d17c" } diff --git a/semsitter-bridge/prebuilt/bridge-linux-aarch64.xz b/semsitter-bridge/prebuilt/bridge-linux-aarch64.xz index 2c07943f766..32eb397fc96 100644 Binary files a/semsitter-bridge/prebuilt/bridge-linux-aarch64.xz and b/semsitter-bridge/prebuilt/bridge-linux-aarch64.xz differ diff --git a/semsitter-bridge/prebuilt/bridge-linux-x64.xz b/semsitter-bridge/prebuilt/bridge-linux-x64.xz index 18a59653df8..e781d5c8382 100644 Binary files a/semsitter-bridge/prebuilt/bridge-linux-x64.xz and b/semsitter-bridge/prebuilt/bridge-linux-x64.xz differ diff --git a/semsitter-bridge/prebuilt/bridge-macos-aarch64.xz b/semsitter-bridge/prebuilt/bridge-macos-aarch64.xz index 4a6e2eaccd3..27374365550 100644 Binary files a/semsitter-bridge/prebuilt/bridge-macos-aarch64.xz and b/semsitter-bridge/prebuilt/bridge-macos-aarch64.xz differ diff --git a/semsitter-bridge/prebuilt/bridge-macos-x64.xz b/semsitter-bridge/prebuilt/bridge-macos-x64.xz index bb3a1f40d74..6c85162f479 100644 Binary files a/semsitter-bridge/prebuilt/bridge-macos-x64.xz and b/semsitter-bridge/prebuilt/bridge-macos-x64.xz differ diff --git a/semsitter-bridge/prebuilt/bridge-windows-x64.xz b/semsitter-bridge/prebuilt/bridge-windows-x64.xz index 47e3788d914..7cd8bbe88bc 100644 Binary files a/semsitter-bridge/prebuilt/bridge-windows-x64.xz and b/semsitter-bridge/prebuilt/bridge-windows-x64.xz differ diff --git a/semsitter-bridge/src/types.rs b/semsitter-bridge/src/types.rs index abff01b97c0..7374b60d5af 100644 --- a/semsitter-bridge/src/types.rs +++ b/semsitter-bridge/src/types.rs @@ -17,7 +17,7 @@ //! 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. +//! image), the JDK table SemSitter embeds, then SemSitter's framework API database of third-party libraries. use std::collections::{BTreeSet, HashMap, VecDeque}; @@ -26,6 +26,7 @@ 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}; +use udg_lang_java::java_db::{JAVA_DB, erase as erase_spelled}; const ACC_STATIC: u16 = 0x0008; const ACC_FINAL: u16 = 0x0010; @@ -99,7 +100,8 @@ pub fn lookup( } 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)); + .or_else(|| jdk_type(&name).map(jdk_type_info)) + .or_else(|| framework_type(&name)); let Some(info) = info else { continue; }; @@ -113,6 +115,85 @@ pub fn lookup( found } +/// A library type no jar and no JDK table declares, from SemSitter's framework API database (`java.txt` and the generated +/// taint rows). The graph already binds library calls with it; without this the sonar-java frontend gets no declaration +/// for the owner of such a call and cannot type it. Its rows state erased, source-spelled parameter and return types, +/// which become the JVM descriptors the other sources report. +fn framework_type(name: &str) -> Option { + let entry = JAVA_DB.type_entry(name); + let owner = JAVA_DB.methods.get(name); + if entry.is_none() && owner.is_none() { + return None; + } + let kind = entry + .and_then(|e| e.kind) + .map(|k| format!("{k:?}").to_lowercase()) + .unwrap_or_else(|| "class".to_string()); + let interface_like = kind == "interface" || kind == "annotation"; + let superclass = if interface_like || name == "java.lang.Object" { + None + } else { + Some( + entry + .and_then(|e| e.super_class) + .map(|s| erase_spelled(s).to_string()) + .unwrap_or_else(|| "java.lang.Object".to_string()), + ) + }; + let interfaces: Vec = entry + .map(|e| e.interfaces.iter().map(|i| erase_spelled(i).to_string()).collect()) + .unwrap_or_default(); + let mut members: Vec = Vec::new(); + if let Some(owner) = owner { + let mut names: Vec<&str> = owner.keys().copied().collect(); + names.sort_unstable(); + for method in names { + for row in owner.get(method).into_iter().flatten() { + let Some(parameters) = row.param_types.as_ref() else { + continue; + }; + let descriptor = format!( + "({}){}", + parameters.iter().map(|p| spelled_descriptor(p)).collect::(), + spelled_descriptor(row.return_type.unwrap_or("void")) + ); + let flags: Vec<&str> = row.modifiers.split(',').map(str::trim).collect(); + members.push(json!({ + "k": "m", + "name": method, + "desc": descriptor, + "static": flags.contains(&"static"), + "abstract": flags.contains(&"abstract"), + "varargs": flags.contains(&"varargs"), + "vis": row.effective_visibility.unwrap_or("public"), + "annotations": row.annotations.iter().map(|a| json!({"name": a, "values": []})).collect::>(), + })); + } + } + } + Some(json!({ + "origin": "framework", + "kind": kind, + "super": superclass, + "interfaces": interfaces, + "modifiers": ["public"], + "annotations": [], + "members": members, + })) +} + +/// The JVM descriptor of a type as the framework database spells it: `void`, a primitive, a binary name, each followed +/// by one `[]` per array dimension. A `$T` marker (a type variable) is its erasure, `Object`. +fn spelled_descriptor(spelled: &str) -> String { + if spelled == "void" { + return "V".to_string(); + } + if spelled.is_empty() || spelled.starts_with('$') { + return "Ljava/lang/Object;".to_string(); + } + field_descriptor(spelled) +} + fn strings(value: &Value, key: &str) -> Vec { match value.get(key) { Some(Value::String(s)) => vec![s.clone()],