Search in sources :

Example 6 with TextDocumentSurrogate

use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate in project graal by oracle.

the class CoverageRequestHandler method getCoverageWithEnteredContext.

public Coverage getCoverageWithEnteredContext(URI uri) {
    final TextDocumentSurrogate surrogate = surrogateMap.get(uri);
    if (surrogate != null && surrogate.getSourceWrapper() != null && surrogate.getSourceWrapper().isParsingSuccessful()) {
        final SourceSectionFilter filter = // 
        SourceSectionFilter.newBuilder().sourceIs(// 
        surrogate.getSourceWrapper().getSource()).tagIs(// 
        StatementTag.class).build();
        final Set<SourceSection> duplicateFilter = new HashSet<>();
        final List<Range> covered = new ArrayList<>();
        final List<Range> uncovered = new ArrayList<>();
        env.getInstrumenter().attachLoadSourceSectionListener(filter, new LoadSourceSectionListener() {

            @Override
            public void onLoad(LoadSourceSectionEvent event) {
                SourceSection section = event.getSourceSection();
                if (duplicateFilter.add(section)) {
                    if (surrogate.isLocationCovered(SourceSectionReference.from(section))) {
                        covered.add(SourceUtils.sourceSectionToRange(section));
                    } else {
                        uncovered.add(SourceUtils.sourceSectionToRange(section));
                    }
                }
            }
        }, true).dispose();
        return Coverage.create(covered, uncovered);
    }
    return null;
}
Also used : StatementTag(com.oracle.truffle.api.instrumentation.StandardTags.StatementTag) LoadSourceSectionEvent(com.oracle.truffle.api.instrumentation.LoadSourceSectionEvent) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) ArrayList(java.util.ArrayList) SourceSectionFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter) SourceSection(com.oracle.truffle.api.source.SourceSection) Range(org.graalvm.tools.lsp.server.types.Range) LoadSourceSectionListener(com.oracle.truffle.api.instrumentation.LoadSourceSectionListener) HashSet(java.util.HashSet)

Example 7 with TextDocumentSurrogate

use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate in project graal by oracle.

the class CoverageRequestHandler method runCoverageAnalysisWithEnteredContext.

public Boolean runCoverageAnalysisWithEnteredContext(final URI uri) throws DiagnosticsNotification {
    final TextDocumentSurrogate surrogateOfOpenedFile = surrogateMap.get(uri);
    if (surrogateOfOpenedFile == null) {
        return Boolean.FALSE;
    }
    TextDocumentSurrogate surrogateOfTestFile = sourceCodeEvaluator.createSurrogateForTestFile(surrogateOfOpenedFile, null);
    final URI runScriptUri = surrogateOfTestFile.getUri();
    clearRelatedCoverageData(runScriptUri);
    try {
        final CallTarget callTarget = sourceCodeEvaluator.parse(surrogateOfTestFile);
        LanguageInfo languageInfo = surrogateOfTestFile.getLanguageInfo();
        SourcePredicate predicate = SourcePredicateBuilder.newBuilder().language(languageInfo).excludeInternal(env.getOptions()).build();
        SourceSectionFilter eventFilter = SourceSectionFilter.newBuilder().sourceIs(predicate).build();
        EventBinding<ExecutionEventNodeFactory> eventFactoryBinding = env.getInstrumenter().attachExecutionEventFactory(eventFilter, new ExecutionEventNodeFactory() {

            private final long creatorThreadId = Thread.currentThread().getId();

            @Override
            public ExecutionEventNode create(final EventContext eventContext) {
                final SourceSection section = eventContext.getInstrumentedSourceSection();
                if (section != null && section.isAvailable()) {
                    final Node instrumentedNode = eventContext.getInstrumentedNode();
                    Function<URI, TextDocumentSurrogate> func = (sourceUri) -> {
                        return surrogateMap.getOrCreateSurrogate(sourceUri, () -> instrumentedNode.getRootNode().getLanguageInfo());
                    };
                    return new CoverageEventNode(section, instrumentedNode, runScriptUri, func, creatorThreadId);
                } else {
                    return null;
                }
            }
        });
        try {
            callTarget.call();
        } finally {
            eventFactoryBinding.dispose();
        }
        surrogateOfOpenedFile.setCoverageAnalysisDone(true);
        return Boolean.TRUE;
    } catch (DiagnosticsNotification e) {
        throw e;
    } catch (Exception e) {
        InteropLibrary interopLib = InteropLibrary.getUncached();
        if (interopLib.isException(e)) {
            SourceSection sourceSection;
            try {
                sourceSection = interopLib.hasSourceLocation(e) ? interopLib.getSourceLocation(e) : null;
            } catch (UnsupportedMessageException um) {
                throw CompilerDirectives.shouldNotReachHere(um);
            }
            URI uriOfErronousSource = sourceSection != null ? sourceSection.getSource().getURI() : null;
            if (uriOfErronousSource == null) {
                uriOfErronousSource = uri;
            }
            throw DiagnosticsNotification.create(uriOfErronousSource, Diagnostic.create(SourceUtils.getRangeFrom(e, interopLib), e.getMessage(), DiagnosticSeverity.Error, null, "Coverage analysis", null));
        }
        throw e;
    }
}
Also used : ExecutionEventNodeFactory(com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory) CallTarget(com.oracle.truffle.api.CallTarget) Node(com.oracle.truffle.api.nodes.Node) ExecutionEventNode(com.oracle.truffle.api.instrumentation.ExecutionEventNode) CoverageEventNode(org.graalvm.tools.lsp.server.utils.CoverageEventNode) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) SourceSectionFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter) DiagnosticsNotification(org.graalvm.tools.lsp.exceptions.DiagnosticsNotification) URI(java.net.URI) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) EventContext(com.oracle.truffle.api.instrumentation.EventContext) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) Function(java.util.function.Function) SourcePredicate(com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) CoverageEventNode(org.graalvm.tools.lsp.server.utils.CoverageEventNode) SourceSection(com.oracle.truffle.api.source.SourceSection) ExecutionEventNode(com.oracle.truffle.api.instrumentation.ExecutionEventNode)

Example 8 with TextDocumentSurrogate

use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate in project graal by oracle.

the class HoverRequestHandler method hoverWithEnteredContext.

public Hover hoverWithEnteredContext(URI uri, int line, int column) {
    TextDocumentSurrogate surrogate = surrogateMap.get(uri);
    InstrumentableNode nodeAtCaret = findNodeAtCaret(surrogate, line, column);
    if (nodeAtCaret != null) {
        SourceSection hoverSection = ((Node) nodeAtCaret).getSourceSection();
        logger.log(Level.FINER, "Hover: SourceSection({0})", hoverSection.getCharacters());
        if (surrogate.hasCoverageData()) {
            List<CoverageData> coverages = surrogate.getCoverageData(hoverSection);
            if (coverages != null) {
                return evalHoverInfos(coverages, hoverSection, surrogate.getLanguageInfo());
            }
        } else if (developerMode) {
            String sourceText = hoverSection.getCharacters().toString();
            MarkupContent content = MarkupContent.create(MarkupKind.PlainText, "Language: " + surrogate.getLanguageId() + ", Section: " + sourceText + "\n" + "Node class: " + nodeAtCaret.getClass().getSimpleName() + "\n" + "Tags: " + getTags(nodeAtCaret));
            return Hover.create(content).setRange(SourceUtils.sourceSectionToRange(hoverSection));
        }
    }
    return Hover.create(Collections.emptyList());
}
Also used : InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) CoverageData(org.graalvm.tools.lsp.server.utils.CoverageData) ExecutableNode(com.oracle.truffle.api.nodes.ExecutableNode) Node(com.oracle.truffle.api.nodes.Node) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) CoverageEventNode(org.graalvm.tools.lsp.server.utils.CoverageEventNode) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) SourceSection(com.oracle.truffle.api.source.SourceSection) MarkupContent(org.graalvm.tools.lsp.server.types.MarkupContent)

Example 9 with TextDocumentSurrogate

use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate in project graal by oracle.

the class SourceCodeEvaluator method runToSectionAndEval.

public EvaluationResult runToSectionAndEval(final TextDocumentSurrogate surrogate, final SourceSection sourceSection, SourceSectionFilter eventFilter, SourceSectionFilter inputFilter) throws DiagnosticsNotification {
    Set<URI> coverageUris = surrogate.getCoverageUris(sourceSection);
    URI runScriptUriFallback = coverageUris == null ? null : coverageUris.stream().findFirst().orElseGet(() -> null);
    TextDocumentSurrogate surrogateOfTestFile = createSurrogateForTestFile(surrogate, runScriptUriFallback);
    final CallTarget callTarget = parse(surrogateOfTestFile);
    final boolean isInputFilterDefined = inputFilter != null;
    EventBinding<ExecutionEventNodeFactory> binding = env.getInstrumenter().attachExecutionEventFactory(eventFilter, inputFilter, new ExecutionEventNodeFactory() {

        StringBuilder indent = new StringBuilder("");

        @Override
        public ExecutionEventNode create(EventContext context) {
            return new ExecutionEventNode() {

                private String sourceSectionFormat(SourceSection section) {
                    return "SourceSection(" + section.getCharacters().toString().replaceAll("\n", Matcher.quoteReplacement("\\n")) + ")";
                }

                @Override
                public void onReturnValue(VirtualFrame frame, Object result) {
                    if (logger.isLoggable(Level.FINEST)) {
                        logOnReturnValue(result);
                    }
                    if (!isInputFilterDefined) {
                        CompilerDirectives.transferToInterpreter();
                        throw new EvaluationResultException(result);
                    }
                }

                @TruffleBoundary
                private void logOnReturnValue(Object result) {
                    if (indent.length() > 1) {
                        indent.setLength(indent.length() - 2);
                    }
                    logger.log(Level.FINEST, "{0}onReturnValue {1} {2} {3} {4}", new Object[] { indent, context.getInstrumentedNode().getClass().getSimpleName(), sourceSectionFormat(context.getInstrumentedSourceSection()), result });
                }

                @Override
                public void onReturnExceptional(VirtualFrame frame, Throwable exception) {
                    if (logger.isLoggable(Level.FINEST)) {
                        logOnReturnExceptional();
                    }
                }

                @TruffleBoundary
                private void logOnReturnExceptional() {
                    indent.setLength(indent.length() - 2);
                    logger.log(Level.FINEST, "{0}onReturnExceptional {1}", new Object[] { indent, sourceSectionFormat(context.getInstrumentedSourceSection()) });
                }

                @Override
                public void onEnter(VirtualFrame frame) {
                    if (logger.isLoggable(Level.FINEST)) {
                        logOnEnter();
                    }
                }

                @TruffleBoundary
                private void logOnEnter() {
                    logger.log(Level.FINEST, "{0}onEnter {1} {2}", new Object[] { indent, context.getInstrumentedNode().getClass().getSimpleName(), sourceSectionFormat(context.getInstrumentedSourceSection()) });
                    indent.append("  ");
                }

                @Override
                public void onInputValue(VirtualFrame frame, EventContext inputContext, int inputIndex, Object inputValue) {
                    if (logger.isLoggable(Level.FINEST)) {
                        logOnInputValue(inputContext, inputIndex, inputValue);
                    }
                    CompilerDirectives.transferToInterpreter();
                    throw new EvaluationResultException(inputValue);
                }

                @TruffleBoundary
                private void logOnInputValue(EventContext inputContext, int inputIndex, Object inputValue) {
                    indent.setLength(indent.length() - 2);
                    Object view = env.getLanguageView(inputContext.getInstrumentedNode().getRootNode().getLanguageInfo(), inputValue);
                    String metaObject;
                    try {
                        metaObject = INTEROP.hasMetaObject(view) ? INTEROP.asString(INTEROP.toDisplayString(INTEROP.getMetaObject(view))) : null;
                    } catch (UnsupportedMessageException e) {
                        CompilerDirectives.transferToInterpreter();
                        throw new AssertionError(e);
                    }
                    logger.log(Level.FINEST, "{0}onInputValue idx:{1} {2} {3} {4} {5} {6}", new Object[] { indent, inputIndex, inputContext.getInstrumentedNode().getClass().getSimpleName(), sourceSectionFormat(context.getInstrumentedSourceSection()), sourceSectionFormat(inputContext.getInstrumentedSourceSection()), inputValue, metaObject });
                    indent.append("  ");
                }
            };
        }
    });
    try {
        callTarget.call();
    } catch (EvaluationResultException e) {
        return e.isError() ? EvaluationResult.createError(e.getResult()) : EvaluationResult.createResult(e.getResult());
    } catch (RuntimeException e) {
        if (INTEROP.isException(e)) {
            try {
                if (INTEROP.getExceptionType(e) == ExceptionType.EXIT) {
                    return EvaluationResult.createEvaluationSectionNotReached();
                } else {
                    return EvaluationResult.createError(e);
                }
            } catch (UnsupportedMessageException ume) {
                throw CompilerDirectives.shouldNotReachHere(ume);
            }
        }
    } finally {
        binding.dispose();
    }
    return EvaluationResult.createEvaluationSectionNotReached();
}
Also used : ExecutionEventNodeFactory(com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory) CallTarget(com.oracle.truffle.api.CallTarget) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) URI(java.net.URI) EventContext(com.oracle.truffle.api.instrumentation.EventContext) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) EvaluationResultException(org.graalvm.tools.lsp.exceptions.EvaluationResultException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) SourceSection(com.oracle.truffle.api.source.SourceSection) ExecutionEventNode(com.oracle.truffle.api.instrumentation.ExecutionEventNode)

Example 10 with TextDocumentSurrogate

use of org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate in project graal by oracle.

the class SourceCodeEvaluator method createSurrogateForTestFile.

public TextDocumentSurrogate createSurrogateForTestFile(TextDocumentSurrogate surrogateOfOpenedFile, URI runScriptUriFallback) {
    URI runScriptUri = runScriptUriFallback != null ? runScriptUriFallback : surrogateOfOpenedFile.getUri();
    final String langIdOfTestFile = findLanguageOfTestFile(runScriptUri, surrogateOfOpenedFile.getLanguageId());
    LanguageInfo languageInfo = env.getLanguages().get(langIdOfTestFile);
    assert languageInfo != null;
    TextDocumentSurrogate surrogateOfTestFile = surrogateMap.getOrCreateSurrogate(runScriptUri, languageInfo);
    return surrogateOfTestFile;
}
Also used : LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) TextDocumentSurrogate(org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate) URI(java.net.URI)

Aggregations

TextDocumentSurrogate (org.graalvm.tools.lsp.server.utils.TextDocumentSurrogate)16 SourceSection (com.oracle.truffle.api.source.SourceSection)6 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)5 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)5 URI (java.net.URI)5 CallTarget (com.oracle.truffle.api.CallTarget)4 Node (com.oracle.truffle.api.nodes.Node)4 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)3 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)3 InteropException (com.oracle.truffle.api.interop.InteropException)3 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 ArrayList (java.util.ArrayList)3 EventContext (com.oracle.truffle.api.instrumentation.EventContext)2 ExecutionEventNode (com.oracle.truffle.api.instrumentation.ExecutionEventNode)2 ExecutionEventNodeFactory (com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory)2 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)2 Source (com.oracle.truffle.api.source.Source)2 HashSet (java.util.HashSet)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2