Search in sources :

Example 1 with SourcePredicate

use of com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate in project graal by oracle.

the class SourcePredicateBuilder method uriOrTruffleName.

/**
 * A special method to filter for either the URI of a {@link Source} or via the name. Some
 * languages create sources in a way that no URI is set, only the {@link Source}'s name which is
 * the path of the source.
 *
 * @param uri
 */
public SourcePredicateBuilder uriOrTruffleName(URI uri) {
    SourcePredicate predicate = src -> src.getURI().equals(uri) || ("truffle".equals(src.getURI().getScheme()) && src.getName() != null && src.getName().equals(uri.getPath()));
    this.predicates.add(predicate);
    return this;
}
Also used : OptionValues(org.graalvm.options.OptionValues) List(java.util.List) Source(com.oracle.truffle.api.source.Source) Predicate(java.util.function.Predicate) SourcePredicate(com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate) URI(java.net.URI) LSPInstrument(org.graalvm.tools.lsp.instrument.LSPInstrument) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) ArrayList(java.util.ArrayList) SourcePredicate(com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate)

Example 2 with SourcePredicate

use of com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate in project graal by oracle.

the class SourcePredicateBuilder method language.

public SourcePredicateBuilder language(LanguageInfo languageInfo) {
    SourcePredicate predicate = src -> languageInfo.getId().equals(src.getLanguage()) || (src.getMimeType() != null && languageInfo.getMimeTypes().contains(src.getMimeType()));
    this.predicates.add(predicate);
    return this;
}
Also used : OptionValues(org.graalvm.options.OptionValues) List(java.util.List) Source(com.oracle.truffle.api.source.Source) Predicate(java.util.function.Predicate) SourcePredicate(com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate) URI(java.net.URI) LSPInstrument(org.graalvm.tools.lsp.instrument.LSPInstrument) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) ArrayList(java.util.ArrayList) SourcePredicate(com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate)

Example 3 with SourcePredicate

use of com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate in project graal by oracle.

the class SourcePredicateBuilder method excludeInternal.

public SourcePredicateBuilder excludeInternal(OptionValues options) {
    boolean includeInternal = options.get(LSPInstrument.Internal);
    if (!includeInternal) {
        SourcePredicate predicate = src -> !src.isInternal();
        this.predicates.add(predicate);
    }
    return this;
}
Also used : OptionValues(org.graalvm.options.OptionValues) List(java.util.List) Source(com.oracle.truffle.api.source.Source) Predicate(java.util.function.Predicate) SourcePredicate(com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate) URI(java.net.URI) LSPInstrument(org.graalvm.tools.lsp.instrument.LSPInstrument) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) ArrayList(java.util.ArrayList) SourcePredicate(com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate)

Example 4 with SourcePredicate

use of com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate 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)

Aggregations

SourcePredicate (com.oracle.truffle.api.instrumentation.SourceSectionFilter.SourcePredicate)4 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)4 URI (java.net.URI)4 Source (com.oracle.truffle.api.source.Source)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Predicate (java.util.function.Predicate)3 OptionValues (org.graalvm.options.OptionValues)3 LSPInstrument (org.graalvm.tools.lsp.instrument.LSPInstrument)3 CallTarget (com.oracle.truffle.api.CallTarget)1 EventContext (com.oracle.truffle.api.instrumentation.EventContext)1 ExecutionEventNode (com.oracle.truffle.api.instrumentation.ExecutionEventNode)1 ExecutionEventNodeFactory (com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory)1 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)1 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)1 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)1 Node (com.oracle.truffle.api.nodes.Node)1 SourceSection (com.oracle.truffle.api.source.SourceSection)1 Function (java.util.function.Function)1 DiagnosticsNotification (org.graalvm.tools.lsp.exceptions.DiagnosticsNotification)1