basic for analyse

This commit is contained in:
2026-05-06 03:40:55 +01:00
parent 41f2c804a0
commit 85a224fc7b
11 changed files with 466 additions and 46 deletions
@@ -57,7 +57,7 @@ public class BuilderPipelineService {
public AnalysisSnapshot analyze(
final BuilderPipelineContext ctx,
final LogAggregator logs) {
final var diagnostics = run(ctx, logs, analyses, new ArrayList<>());
final var diagnostics = run(ctx, logs, analyses, new ArrayList<>(), false);
return new AnalysisSnapshot(
ctx.resolvedWorkspace.frontendSpec(),
diagnostics,
@@ -70,7 +70,10 @@ public class BuilderPipelineService {
final BuilderPipelineContext ctx,
final LogAggregator logs) {
final var analysisSnapshot = this.analyze(ctx, logs);
final var diagnostics = run(ctx, logs, compile, new ArrayList<>(analysisSnapshot.diagnostics()));
if (hasErrors(analysisSnapshot.diagnostics())) {
throw new BuildException("issues found on pipeline stage: analyze");
}
final var diagnostics = run(ctx, logs, compile, new ArrayList<>(analysisSnapshot.diagnostics()), true);
return new CompileResult(
analysisSnapshot,
diagnostics,
@@ -84,7 +87,7 @@ public class BuilderPipelineService {
final BuilderPipelineContext ctx,
final LogAggregator logs) {
final var compileResult = this.compile(ctx, logs);
final var diagnostics = run(ctx, logs, build, new ArrayList<>(compileResult.diagnostics()));
final var diagnostics = run(ctx, logs, build, new ArrayList<>(compileResult.diagnostics()), true);
return new BuildResult(
compileResult,
diagnostics,
@@ -95,19 +98,32 @@ public class BuilderPipelineService {
final BuilderPipelineContext ctx,
final LogAggregator logs,
final List<PipelineStage> stages,
final List<BuildingIssue> diagnostics) {
final List<BuildingIssue> diagnostics,
final boolean failFast) {
for (final var builderPipelineStage : stages) {
final var issues = builderPipelineStage.run(ctx, logs);
diagnostics.addAll(issues.asCollection());
printIssues(issues, logs);
if (issues.hasErrors()) {
throw new BuildException("issues found on pipeline stage: " + builderPipelineStage.getClass().getSimpleName());
if (failFast) {
throw new BuildException("issues found on pipeline stage: " + builderPipelineStage.getClass().getSimpleName());
}
break;
}
}
logs.using(log).info("builder pipeline completed successfully");
return List.copyOf(diagnostics);
}
private boolean hasErrors(final List<BuildingIssue> diagnostics) {
for (final var diagnostic : diagnostics) {
if (diagnostic.isError()) {
return true;
}
}
return false;
}
private void printIssues(
final ReadOnlyCollection<BuildingIssue> issues,
final LogAggregator logs) {
@@ -48,6 +48,11 @@ public class FrontendPhasePipelineStage implements PipelineStage {
final var span = diagnostic.getSpan();
issues.report(builder -> builder
.error(diagnostic.getSeverity().isError())
.phase(diagnostic.getPhase().id())
.code(diagnostic.getCode())
.fileId(span.getFileId().isNone() ? null : span.getFileId().getId())
.start(safeToInt(span.getStart()))
.end(safeToInt(span.getEnd()))
.message("[BUILD] %s :: %s @ %s:[%d,%d)".formatted(
diagnostic.getCode(),
diagnostic.getMessage(),
@@ -56,4 +61,8 @@ public class FrontendPhasePipelineStage implements PipelineStage {
span.getEnd())));
}
}
private int safeToInt(final long value) {
return value > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) Math.max(0L, value);
}
}
@@ -3,12 +3,15 @@ package p.studio.compiler.integration;
import org.junit.jupiter.api.Test;
import p.studio.compiler.messages.BuilderPipelineConfig;
import p.studio.compiler.models.BuilderPipelineContext;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.utilities.SourceProviderFactory;
import p.studio.compiler.workspaces.BuilderPipelineService;
import p.studio.utilities.logs.LogAggregator;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
@@ -32,6 +35,36 @@ class MainProjectPipelineIntegrationTest {
assertFalse(Files.exists(outputPath), "analyze must not write output: " + outputPath);
}
@Test
void analyzeShouldReturnStructuredDiagnosticsForOverlayDocumentWithoutThrowing() throws IOException {
final var projectRoot = projectRoot();
final var documentPath = projectRoot.resolve("src").resolve("main.pbs").toAbsolutePath().normalize();
final var logs = bufferedLogs();
final var invalidOverlay = """
fn frame() -> void {
let broken: int = ;
}
""";
final var context = BuilderPipelineContext.fromConfig(new BuilderPipelineConfig(
false,
projectRoot.toString(),
"core-v1",
SourceProviderFactory.overlayUtf8(Map.of(documentPath, invalidOverlay))));
final var snapshot = assertDoesNotThrow(
() -> BuilderPipelineService.INSTANCE.analyze(context, logs),
() -> "analyze unexpectedly failed for overlay " + documentPath);
final var documentDiagnostics = snapshot.diagnostics().stream()
.filter(issue -> issue.getFileId() != null)
.filter(issue -> issue.getFileId() >= 0 && issue.getFileId() < snapshot.fileTable().size())
.filter(issue -> snapshot.fileTable().get(new FileId(issue.getFileId())).getCanonPath().toAbsolutePath().normalize().equals(documentPath))
.toList();
assertFalse(documentDiagnostics.isEmpty(), "overlay analysis must surface diagnostics for " + documentPath);
assertTrue(documentDiagnostics.stream().anyMatch(issue -> issue.isError() && issue.getCode() != null && issue.getStart() != null && issue.getEnd() != null));
}
@Test
void compileShouldProduceInMemoryBytecodeWithoutWritingProgramBytecode() throws IOException {
final var projectRoot = projectRoot();
@@ -1,8 +1,11 @@
package p.studio.compiler.utilities;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@FunctionalInterface
@@ -24,5 +27,38 @@ public interface SourceProviderFactory {
};
}
// more providers can be added here, e.g., in-memory, etc.
static SourceProviderFactory overlayUtf8(final Map<Path, String> overrides) {
final Map<Path, byte[]> bytesByPath = new HashMap<>();
for (final var entry : Objects.requireNonNull(overrides, "overrides").entrySet()) {
bytesByPath.put(
normalize(entry.getKey()),
Objects.requireNonNull(entry.getValue(), "override text").getBytes(StandardCharsets.UTF_8));
}
return overlay(bytesByPath, filesystem());
}
static SourceProviderFactory overlay(
final Map<Path, byte[]> overrides,
final SourceProviderFactory fallback) {
final Map<Path, byte[]> normalizedOverrides = new HashMap<>();
for (final var entry : Objects.requireNonNull(overrides, "overrides").entrySet()) {
normalizedOverrides.put(
normalize(entry.getKey()),
Objects.requireNonNull(entry.getValue(), "override bytes").clone());
}
final SourceProviderFactory safeFallback = Objects.requireNonNull(fallback, "fallback");
return path -> {
final Path normalizedPath = normalize(path);
final byte[] override = normalizedOverrides.get(normalizedPath);
if (override != null) {
final byte[] immutableBytes = override.clone();
return () -> immutableBytes;
}
return safeFallback.create(normalizedPath);
};
}
private static Path normalize(final Path path) {
return Objects.requireNonNull(path, "path").toAbsolutePath().normalize();
}
}