comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/LineLocationToSourceSectionCollectionMap.java @ 16961:a1427e40deaf

Truffle/Instrumentation: some Javadoc revistions; minor code cleanups; remove one redundant operation; add tracing to the LineLocation maps.
author Michael Van De Vanter <michael.van.de.vanter@oracle.com>
date Tue, 26 Aug 2014 13:54:53 -0700
parents 7661cc464239
children c88ab4f1f04a
comparison
equal deleted inserted replaced
16955:7ef0a2355540 16961:a1427e40deaf
22 * or visit www.oracle.com if you need additional information or have any 22 * or visit www.oracle.com if you need additional information or have any
23 * questions. 23 * questions.
24 */ 24 */
25 package com.oracle.truffle.api.instrument.impl; 25 package com.oracle.truffle.api.instrument.impl;
26 26
27 import java.io.*;
27 import java.util.*; 28 import java.util.*;
28 29
29 import com.oracle.truffle.api.instrument.*; 30 import com.oracle.truffle.api.instrument.*;
30 import com.oracle.truffle.api.source.*; 31 import com.oracle.truffle.api.source.*;
31 32
35 * are instrumented as it uses the {@link ProbeListener} interface to determine the source sections 36 * are instrumented as it uses the {@link ProbeListener} interface to determine the source sections
36 * that exist in the file. 37 * that exist in the file.
37 */ 38 */
38 public class LineLocationToSourceSectionCollectionMap implements ProbeListener { 39 public class LineLocationToSourceSectionCollectionMap implements ProbeListener {
39 40
41 private static final boolean TRACE = false;
42 private static final PrintStream OUT = System.out;
43
40 /** 44 /**
41 * Map: Source line ==> source sections that exist on the line. 45 * Map: Source line ==> source sections that exist on the line.
42 */ 46 */
43 private final Map<LineLocation, Collection<SourceSection>> lineToSourceSectionsMap = new HashMap<>(); 47 private final Map<LineLocation, Collection<SourceSection>> lineToSourceSectionsMap = new HashMap<>();
44 48
45 public LineLocationToSourceSectionCollectionMap() { 49 public LineLocationToSourceSectionCollectionMap() {
46
47 } 50 }
48 51
49 public void newProbeInserted(SourceSection sourceSection, Probe probe) { 52 public void newProbeInserted(SourceSection sourceSection, Probe probe) {
50 if (sourceSection != null && !(sourceSection instanceof NullSourceSection)) { 53 if (sourceSection != null && !(sourceSection instanceof NullSourceSection)) {
51 this.addSourceSectionToLine(sourceSection.getLineLocation(), sourceSection); 54 final LineLocation lineLocation = sourceSection.getLineLocation();
55 if (TRACE) {
56 OUT.println("LineLocationToSourceSectionCollectionMap: adding " + lineLocation + " Probe=" + probe);
57 }
58 this.addSourceSectionToLine(lineLocation, sourceSection);
52 } 59 }
53 } 60 }
54 61
55 public void probeTaggedAs(Probe probe, SyntaxTag tag) { 62 public void probeTaggedAs(Probe probe, SyntaxTag tag) {
56 // This map ignores tags, but this subclasses can override this method to operate on tags. 63 // This map ignores tags, but this subclasses can override this method to operate on tags.
80 existingSourceSectionList.add(sourceSection); 87 existingSourceSectionList.add(sourceSection);
81 } 88 }
82 } 89 }
83 90
84 /** 91 /**
85 * Returns a collection of {@link SourceSection}s at the given {@link LineLocation}. If there 92 * Returns a collection of {@link SourceSection}s at the given {@link LineLocation}, an empty
86 * are no source sections at that line, a new empty list of size 1 is returned. 93 * list if none.
87 * 94 *
88 * @param line The line to check. 95 * @param line The line to check.
89 * @return A iterable collection of source sections at the given line. 96 * @return the source sections at the given line.
90 */ 97 */
91 public Collection<SourceSection> getSourceSectionsAtLine(LineLocation line) { 98 public Collection<SourceSection> getSourceSectionsAtLine(LineLocation line) {
92 Collection<SourceSection> sourceSectionList = lineToSourceSectionsMap.get(line); 99 Collection<SourceSection> sourceSectionList = lineToSourceSectionsMap.get(line);
93 100
94 if (sourceSectionList == null) 101 if (sourceSectionList == null) {
95 sourceSectionList = new ArrayList<>(1); 102 return Collections.emptyList();
96 103 }
97 return sourceSectionList; 104 return sourceSectionList;
98 } 105 }
99 106
100 /** 107 /**
101 * Convenience method to get source sections according to a int line number. Returns a 108 * Convenience method to get source sections according to a int line number. Returns a
102 * collection of {@link SourceSection}s at the given line number. If there are no source 109 * collection of {@link SourceSection}s at the given line number. If there are no source
103 * sections at that line, a new empty list is returned. 110 * sections at that line, an empty list is returned.
104 * 111 *
105 * @param lineNumber The line number to check. 112 * @param lineNumber The line number to check.
106 * @return A iterable collection of source sections at the given line. 113 * @return A collection of source sections at the given line.
107 */ 114 */
108 public Collection<SourceSection> getSourceSectionsAtLineNumber(int lineNumber) { 115 public Collection<SourceSection> getSourceSectionsAtLineNumber(int lineNumber) {
109 ArrayList<SourceSection> sourceSections = new ArrayList<>();
110 116
111 for (LineLocation line : lineToSourceSectionsMap.keySet()) { 117 final Set<LineLocation> keySet = lineToSourceSectionsMap.keySet();
118 if (keySet.size() == 0) {
119 return Collections.emptyList();
120 }
121
122 final ArrayList<SourceSection> sourceSections = new ArrayList<>();
123 for (LineLocation line : keySet) {
112 if (line.getLineNumber() == lineNumber) 124 if (line.getLineNumber() == lineNumber)
113 sourceSections.addAll(lineToSourceSectionsMap.get(line)); 125 sourceSections.addAll(lineToSourceSectionsMap.get(line));
114 } 126 }
115 127
116 return sourceSections; 128 return sourceSections;