changeset 22093:0f0e34039769

Truffle/instrumentation: remove SourceTag machinery, not used so far. It will eventually be restored in the new API framework.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Mon, 17 Aug 2015 15:24:00 -0700
parents 1a1aa12ab310
children 0058a9461865
files truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceTagTest.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceListener.java truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceTag.java truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/CoverageTracker.java
diffstat 5 files changed, 27 insertions(+), 471 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceTagTest.java	Mon Aug 17 17:40:00 2015 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,162 +0,0 @@
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.
- *
- * This code 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 GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package com.oracle.truffle.api.test.source;
-
-import static org.junit.Assert.*;
-
-import org.junit.*;
-
-import com.oracle.truffle.api.source.*;
-
-public class SourceTagTest {
-
-    @Test
-    public void sourceTagTest() {
-
-        // Private tag
-        final SourceTag testTag = new SourceTag() {
-
-            public String name() {
-                return null;
-            }
-
-            public String getDescription() {
-                return null;
-            }
-        };
-
-        // No sources exist with the private tag
-        assertEquals(Source.findSourcesTaggedAs(testTag).size(), 0);
-
-        // Create a new source
-        final Source source = Source.fromText("test1 source", "test1 source");
-
-        // Initially has only the default tag
-        assertEquals(source.getSourceTags().size(), 1);
-
-        assertTrue(source.getSourceTags().contains(Source.Tags.FROM_LITERAL));
-        assertTrue(source.isTaggedAs(Source.Tags.FROM_LITERAL));
-        assertTrue(Source.findSourcesTaggedAs(Source.Tags.FROM_LITERAL).contains(source));
-
-        assertFalse(source.isTaggedAs(testTag));
-        assertEquals(Source.findSourcesTaggedAs(testTag).size(), 0);
-
-        // Add a private tag
-        source.tagAs(testTag);
-
-        // Now there are exactly two tags
-        assertEquals(source.getSourceTags().size(), 2);
-
-        assertTrue(source.getSourceTags().contains(Source.Tags.FROM_LITERAL));
-        assertTrue(source.isTaggedAs(Source.Tags.FROM_LITERAL));
-        assertTrue(Source.findSourcesTaggedAs(Source.Tags.FROM_LITERAL).contains(source));
-
-        assertTrue(source.getSourceTags().contains(testTag));
-        assertTrue(source.isTaggedAs(testTag));
-        assertEquals(Source.findSourcesTaggedAs(testTag).size(), 1);
-        assertTrue(Source.findSourcesTaggedAs(testTag).contains(source));
-
-        // Add the private tag again
-        source.tagAs(testTag);
-
-        // Nothing has changed
-        assertEquals(source.getSourceTags().size(), 2);
-
-        assertTrue(source.getSourceTags().contains(Source.Tags.FROM_LITERAL));
-        assertTrue(source.isTaggedAs(Source.Tags.FROM_LITERAL));
-        assertTrue(Source.findSourcesTaggedAs(Source.Tags.FROM_LITERAL).contains(source));
-
-        assertTrue(source.getSourceTags().contains(testTag));
-        assertTrue(source.isTaggedAs(testTag));
-        assertEquals(Source.findSourcesTaggedAs(testTag).size(), 1);
-        assertTrue(Source.findSourcesTaggedAs(testTag).contains(source));
-    }
-
-    @Test
-    public void sourceListenerTest() {
-
-        // Private tag
-        final SourceTag testTag = new SourceTag() {
-
-            public String name() {
-                return null;
-            }
-
-            public String getDescription() {
-                return null;
-            }
-        };
-
-        final int[] newSourceEvents = {0};
-        final Source[] newSource = {null};
-
-        final int[] newTagEvents = {0};
-        final Source[] taggedSource = {null};
-        final SourceTag[] newTag = {null};
-
-        Source.addSourceListener(new SourceListener() {
-
-            public void sourceCreated(Source source) {
-                newSourceEvents[0] = newSourceEvents[0] + 1;
-                newSource[0] = source;
-            }
-
-            public void sourceTaggedAs(Source source, SourceTag tag) {
-                newTagEvents[0] = newTagEvents[0] + 1;
-                taggedSource[0] = source;
-                newTag[0] = tag;
-            }
-        });
-
-        // New source has a default tag applied.
-        // Get one event for the new source, another one when it gets tagged
-        final Source source = Source.fromText("testSource", "testSource");
-        assertEquals(newSourceEvents[0], 1);
-        assertEquals(newSource[0], source);
-        assertEquals(newTagEvents[0], 1);
-        assertEquals(taggedSource[0], source);
-        assertEquals(newTag[0], Source.Tags.FROM_LITERAL);
-
-        // reset
-        newSource[0] = null;
-        taggedSource[0] = null;
-        newTag[0] = null;
-
-        // Add a tag; only get one event (the new tag)
-        source.tagAs(testTag);
-        assertEquals(newSourceEvents[0], 1);
-        assertEquals(newSource[0], null);
-        assertEquals(newTagEvents[0], 2);
-        assertEquals(taggedSource[0], source);
-        assertEquals(newTag[0], testTag);
-
-        // Add the same tag; no events, and nothing changes.
-        source.tagAs(testTag);
-        assertEquals(newSourceEvents[0], 1);
-        assertEquals(newSource[0], null);
-        assertEquals(newTagEvents[0], 2);
-        assertEquals(taggedSource[0], source);
-        assertEquals(newTag[0], testTag);
-
-    }
-}
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java	Mon Aug 17 17:40:00 2015 +0200
+++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/Source.java	Mon Aug 17 15:24:00 2015 -0700
@@ -27,15 +27,11 @@
 import java.io.*;
 import java.lang.ref.*;
 import java.net.*;
+import java.nio.*;
+import java.nio.charset.*;
 import java.util.*;
 
 import com.oracle.truffle.api.*;
-import com.oracle.truffle.api.instrument.*;
-import java.nio.ByteBuffer;
-import java.nio.CharBuffer;
-import java.nio.charset.CharacterCodingException;
-import java.nio.charset.Charset;
-import java.nio.charset.CharsetDecoder;
 
 /**
  * Representation of a guest language source code unit and its contents. Sources originate in
@@ -89,65 +85,12 @@
  * reload.</li>
  * </ol>
  * <p>
- *
- * @see SourceTag
- * @see SourceListener
  */
 public abstract class Source {
 
     // TODO (mlvdv) consider canonicalizing and reusing SourceSection instances
     // TOOD (mlvdv) connect SourceSections into a spatial tree for fast geometric lookup
 
-    public enum Tags implements SourceTag {
-
-        /**
-         * From bytes.
-         */
-        FROM_BYTES("bytes", "read from bytes"),
-
-        /**
-         * Read from a file.
-         */
-        FROM_FILE("file", "read from a file"),
-
-        /**
-         * From literal text.
-         */
-        FROM_LITERAL("literal", "from literal text"),
-
-        /**
-         * From a {@linkplain java.io.Reader Reader}.
-         */
-        FROM_READER("reader", "read from a Java Reader"),
-
-        /**
-         * Read from a URL.
-         */
-        FROM_URL("URL", "read from a URL");
-
-        private final String name;
-        private final String description;
-
-        private Tags(String name, String description) {
-            this.name = name;
-            this.description = description;
-        }
-
-        public String getName() {
-            return name;
-        }
-
-        public String getDescription() {
-            return description;
-        }
-
-    }
-
-    /**
-     * All Sources that have been created.
-     */
-    private static final List<WeakReference<Source>> allSources = Collections.synchronizedList(new ArrayList<WeakReference<Source>>());
-
     /**
      * Index of all named sources.
      */
@@ -155,8 +98,6 @@
 
     private static boolean fileCacheEnabled = true;
 
-    private static final List<SourceListener> sourceListeners = new ArrayList<>();
-
     /**
      * Locates an existing instance by the name under which it was indexed.
      */
@@ -194,7 +135,6 @@
         if (reset) {
             source.reset();
         }
-        notifyNewSource(source).tagAs(Tags.FROM_FILE);
         return source;
     }
 
@@ -237,7 +177,6 @@
                 nameToSource.put(path, new WeakReference<>(source));
             }
         }
-        notifyNewSource(source).tagAs(Tags.FROM_FILE);
         return source;
     }
 
@@ -250,11 +189,8 @@
      */
     public static Source fromText(CharSequence chars, String description) {
         CompilerAsserts.neverPartOfCompilation();
-
         assert chars != null;
-        final LiteralSource source = new LiteralSource(description, chars.toString());
-        notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
-        return source;
+        return new LiteralSource(description, chars.toString());
     }
 
     /**
@@ -266,10 +202,7 @@
      */
     public static Source fromAppendableText(String description) {
         CompilerAsserts.neverPartOfCompilation();
-
-        final Source source = new AppendableLiteralSource(description);
-        notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
-        return source;
+        return new AppendableLiteralSource(description);
     }
 
     /**
@@ -283,10 +216,8 @@
      */
     public static Source fromNamedText(CharSequence chars, String name) {
         CompilerAsserts.neverPartOfCompilation();
-
         final Source source = new LiteralSource(name, chars.toString());
         nameToSource.put(name, new WeakReference<>(source));
-        notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
         return source;
     }
 
@@ -301,10 +232,8 @@
      */
     public static Source fromNamedAppendableText(String name) {
         CompilerAsserts.neverPartOfCompilation();
-
         final Source source = new AppendableLiteralSource(name);
         nameToSource.put(name, new WeakReference<>(source));
-        notifyNewSource(source).tagAs(Tags.FROM_LITERAL);
         return source;
     }
 
@@ -320,7 +249,6 @@
      */
     public static Source subSource(Source base, int baseCharIndex, int length) {
         CompilerAsserts.neverPartOfCompilation();
-
         final SubSource subSource = SubSource.create(base, baseCharIndex, length);
         return subSource;
     }
@@ -350,10 +278,7 @@
      */
     public static Source fromURL(URL url, String description) throws IOException {
         CompilerAsserts.neverPartOfCompilation();
-
-        final URLSource source = URLSource.get(url, description);
-        notifyNewSource(source).tagAs(Tags.FROM_URL);
-        return source;
+        return URLSource.get(url, description);
     }
 
     /**
@@ -366,10 +291,7 @@
      */
     public static Source fromReader(Reader reader, String description) throws IOException {
         CompilerAsserts.neverPartOfCompilation();
-
-        final LiteralSource source = new LiteralSource(description, read(reader));
-        notifyNewSource(source).tagAs(Tags.FROM_READER);
-        return source;
+        return new LiteralSource(description, read(reader));
     }
 
     /**
@@ -401,10 +323,7 @@
      */
     public static Source fromBytes(byte[] bytes, int byteIndex, int length, String description, Charset charset) {
         CompilerAsserts.neverPartOfCompilation();
-
-        final BytesSource source = new BytesSource(description, bytes, byteIndex, length, charset);
-        notifyNewSource(source).tagAs(Tags.FROM_BYTES);
-        return source;
+        return new BytesSource(description, bytes, byteIndex, length, charset);
     }
 
     // TODO (mlvdv) enable per-file choice whether to cache?
@@ -416,50 +335,6 @@
         fileCacheEnabled = enabled;
     }
 
-    /**
-     * Returns all {@link Source}s holding a particular {@link SyntaxTag}, or the whole collection
-     * of Sources if the specified tag is {@code null}.
-     *
-     * @return A collection of Sources containing the given tag.
-     */
-    public static Collection<Source> findSourcesTaggedAs(SourceTag tag) {
-        final List<Source> taggedSources = new ArrayList<>();
-        synchronized (allSources) {
-            for (WeakReference<Source> ref : allSources) {
-                Source source = ref.get();
-                if (source != null) {
-                    if (tag == null || source.isTaggedAs(tag)) {
-                        taggedSources.add(ref.get());
-                    }
-                }
-            }
-        }
-        return taggedSources;
-    }
-
-    /**
-     * Adds a {@link SourceListener} to receive events.
-     */
-    public static void addSourceListener(SourceListener listener) {
-        assert listener != null;
-        sourceListeners.add(listener);
-    }
-
-    /**
-     * Removes a {@link SourceListener}. Ignored if listener not found.
-     */
-    public static void removeSourceListener(SourceListener listener) {
-        sourceListeners.remove(listener);
-    }
-
-    private static Source notifyNewSource(Source source) {
-        allSources.add(new WeakReference<>(source));
-        for (SourceListener listener : sourceListeners) {
-            listener.sourceCreated(source);
-        }
-        return source;
-    }
-
     private static String read(Reader reader) throws IOException {
         final BufferedReader bufferedReader = new BufferedReader(reader);
         final StringBuilder builder = new StringBuilder();
@@ -479,8 +354,6 @@
         return builder.toString();
     }
 
-    private final ArrayList<SourceTag> tags = new ArrayList<>();
-
     private Source() {
     }
 
@@ -488,32 +361,6 @@
 
     abstract void reset();
 
-    public final boolean isTaggedAs(SourceTag tag) {
-        assert tag != null;
-        return tags.contains(tag);
-    }
-
-    public final Collection<SourceTag> getSourceTags() {
-        return Collections.unmodifiableCollection(tags);
-    }
-
-    /**
-     * Adds a {@linkplain SourceTag tag} to the set of tags associated with this {@link Source};
-     * {@code no-op} if already in the set.
-     *
-     * @return this
-     */
-    public final Source tagAs(SourceTag tag) {
-        assert tag != null;
-        if (!tags.contains(tag)) {
-            tags.add(tag);
-            for (SourceListener listener : sourceListeners) {
-                listener.sourceTaggedAs(this, tag);
-            }
-        }
-        return this;
-    }
-
     /**
      * Returns the name of this resource holding a guest language program. An example would be the
      * name of a guest language source code file.
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceListener.java	Mon Aug 17 17:40:00 2015 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code 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 GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package com.oracle.truffle.api.source;
-
-/**
- * An observer of events related to {@link Source}s: creating and tagging.
- */
-public interface SourceListener {
-
-    /**
-     * Notifies that a new {@link Source} has just been created.
-     */
-    void sourceCreated(Source source);
-
-    /**
-     * Notifies that a {@link SourceTag} has been newly added to the set of tags associated with a
-     * {@link Source} via {@link Source#tagAs(SourceTag)}.
-     * <p>
-     * The {@linkplain SourceTag tags} at a {@link Source} are a <em>set</em>; this notification
-     * will only be delivered the first time a particular {@linkplain SourceTag tag} is added at a
-     * {@link Source}.
-     *
-     * @param source where a tag has been added
-     * @param tag the tag that has been newly added (subsequent additions of the tag are
-     *            unreported).
-     */
-    void sourceTaggedAs(Source source, SourceTag tag);
-
-}
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/source/SourceTag.java	Mon Aug 17 17:40:00 2015 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,51 +0,0 @@
-/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code 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 GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-package com.oracle.truffle.api.source;
-
-/**
- * Categorical information (best implemented as enums} about particular sources of Guest Language
- * code that can be useful to configure behavior of both the language runtime and external tools.
- * These might include {@linkplain Source.Tags standard tags} noting, for example, whether the
- * source was read from a file and whether it should be considered library code.
- * <p>
- * The need for additional tags is likely to arise, in some cases because of issue specific to a
- * Guest Language, but also for help configuring the behavior of particular tools.
- *
- * @see Source
- * @see Source.Tags
- */
-public interface SourceTag {
-
-    /**
-     * Human-friendly name of a category of code sources, e.g. "file", or "library".
-     *
-     */
-    String name();
-
-    /**
-     * Criteria and example uses for the tag.
-     */
-    String getDescription();
-}
--- a/truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/CoverageTracker.java	Mon Aug 17 17:40:00 2015 +0200
+++ b/truffle/com.oracle.truffle.tools/src/com/oracle/truffle/tools/CoverageTracker.java	Mon Aug 17 15:24:00 2015 -0700
@@ -40,9 +40,6 @@
  * is specified, {@linkplain StandardSyntaxTag#STATEMENT STATEMENT} is used, corresponding to
  * conventional behavior for code coverage tools.
  * <p>
- * No counts will be kept for execution in sources that hold the {@link SourceTag}
- * {@link Tags#NO_COVERAGE}.
- * <p>
  * <b>Tool Life Cycle</b>
  * <p>
  * See {@link InstrumentationTool} for the life cycle common to all such tools.
@@ -73,30 +70,6 @@
  */
 public final class CoverageTracker extends InstrumentationTool {
 
-    public enum Tags implements SourceTag {
-
-        /**
-         * Report no counts for sources holding this tag.
-         */
-        NO_COVERAGE("No Coverage", "Coverage Tracker will igore");
-
-        private final String name;
-        private final String description;
-
-        private Tags(String name, String description) {
-            this.name = name;
-            this.description = description;
-        }
-
-        public String getName() {
-            return name;
-        }
-
-        public String getDescription() {
-            return description;
-        }
-    }
-
     /** Counting data. */
     private final Map<LineLocation, CoverageRecord> coverageMap = new HashMap<>();
 
@@ -291,28 +264,28 @@
                 final SourceSection srcSection = probe.getProbedSourceSection();
                 if (srcSection == null) {
                     // TODO (mlvdv) report this?
-                } else if (!srcSection.getSource().isTaggedAs(Tags.NO_COVERAGE)) {
-                    // Get the source line where the
-                    final LineLocation lineLocation = srcSection.getLineLocation();
-                    CoverageRecord record = coverageMap.get(lineLocation);
-                    if (record != null) {
-                        // Another node starts on same line; count only the first (textually)
-                        if (srcSection.getCharIndex() > record.srcSection.getCharIndex()) {
-                            // Existing record, corresponds to code earlier on line
-                            return;
-                        } else {
-                            // Existing record, corresponds to code at a later position; replace it
-                            record.instrument.dispose();
-                        }
+                    return;
+                }
+                // Get the source line where the
+                final LineLocation lineLocation = srcSection.getLineLocation();
+                CoverageRecord record = coverageMap.get(lineLocation);
+                if (record != null) {
+                    // Another node starts on same line; count only the first (textually)
+                    if (srcSection.getCharIndex() > record.srcSection.getCharIndex()) {
+                        // Existing record, corresponds to code earlier on line
+                        return;
+                    } else {
+                        // Existing record, corresponds to code at a later position; replace it
+                        record.instrument.dispose();
                     }
+                }
 
-                    final CoverageRecord coverage = new CoverageRecord(srcSection);
-                    final Instrument instrument = Instrument.create(coverage, CoverageTracker.class.getSimpleName());
-                    coverage.instrument = instrument;
-                    instruments.add(instrument);
-                    probe.attach(instrument);
-                    coverageMap.put(lineLocation, coverage);
-                }
+                final CoverageRecord coverage = new CoverageRecord(srcSection);
+                final Instrument instrument = Instrument.create(coverage, CoverageTracker.class.getSimpleName());
+                coverage.instrument = instrument;
+                instruments.add(instrument);
+                probe.attach(instrument);
+                coverageMap.put(lineLocation, coverage);
             }
         }
     }