Skip to content

Commit d448d9c

Browse files
committed
Added tag to breakpoint builder API
Signed-off-by: Stefan Marr <git@stefan-marr.de>
1 parent c627b12 commit d448d9c

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

truffle/src/com.oracle.truffle.api.debug/src/com/oracle/truffle/api/debug/Breakpoint.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory;
5050
import com.oracle.truffle.api.instrumentation.SourceFilter;
5151
import com.oracle.truffle.api.instrumentation.SourceSectionFilter;
52+
import com.oracle.truffle.api.instrumentation.Tag;
5253
import com.oracle.truffle.api.instrumentation.TruffleInstrument;
5354
import com.oracle.truffle.api.nodes.ControlFlowException;
5455
import com.oracle.truffle.api.nodes.DirectCallNode;
@@ -795,6 +796,8 @@ public final class Builder {
795796
private SourceSection sourceSection;
796797
private SourceElement[] sourceElements;
797798

799+
private Class<? extends Tag> tag;
800+
798801
private Builder(Object key) {
799802
Objects.requireNonNull(key);
800803
this.key = key;
@@ -932,6 +935,19 @@ public Builder sourceElements(@SuppressWarnings("hiding") SourceElement... sourc
932935
return this;
933936
}
934937

938+
/**
939+
* Specify the tag a source section needs to provide.
940+
*
941+
* @since smarr/debugger
942+
*/
943+
public Builder tag(Class<? extends Tag> filterTag) {
944+
if (this.tag != null) {
945+
throw new IllegalStateException("Tag can only be set once per the builder.");
946+
}
947+
this.tag = filterTag;
948+
return this;
949+
}
950+
935951
/**
936952
* @return a new breakpoint instance of {@link Kind#SOURCE_LOCATION SOURCE_LOCATION} kind.
937953
*
@@ -943,9 +959,9 @@ public Breakpoint build() {
943959
}
944960
BreakpointLocation location;
945961
if (sourceSection != null) {
946-
location = BreakpointLocation.create(key, sourceElements, sourceSection);
962+
location = BreakpointLocation.create(key, sourceElements, sourceSection, tag);
947963
} else {
948-
location = BreakpointLocation.create(key, sourceElements, line, column);
964+
location = BreakpointLocation.create(key, sourceElements, line, column, tag);
949965
}
950966
Breakpoint breakpoint = new Breakpoint(location, anchor, oneShot, null, resolveListener);
951967
breakpoint.setIgnoreCount(ignoreCount);

truffle/src/com.oracle.truffle.api.debug/src/com/oracle/truffle/api/debug/BreakpointLocation.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import com.oracle.truffle.api.instrumentation.SourceFilter;
3131
import com.oracle.truffle.api.instrumentation.SourceSectionFilter;
32+
import com.oracle.truffle.api.instrumentation.Tag;
3233
import com.oracle.truffle.api.instrumentation.SourceSectionFilter.IndexRange;
3334
import com.oracle.truffle.api.instrumentation.TruffleInstrument;
3435
import com.oracle.truffle.api.source.Source;
@@ -51,12 +52,12 @@ abstract class BreakpointLocation {
5152
*/
5253
static final BreakpointLocation ANY = new BreakpointSourceLocation();
5354

54-
static BreakpointLocation create(Object key, SourceElement[] sourceElements, SourceSection sourceSection) {
55-
return new BreakpointSourceLocation(key, sourceElements, sourceSection);
55+
static BreakpointLocation create(Object key, SourceElement[] sourceElements, SourceSection sourceSection, Class<? extends Tag> tag) {
56+
return new BreakpointSourceLocation(key, sourceElements, sourceSection, tag);
5657
}
5758

58-
static BreakpointLocation create(Object key, SourceElement[] sourceElements, int line, int column) {
59-
return new BreakpointSourceLocation(key, sourceElements, line, column);
59+
static BreakpointLocation create(Object key, SourceElement[] sourceElements, int line, int column, Class<? extends Tag> tag) {
60+
return new BreakpointSourceLocation(key, sourceElements, line, column, tag);
6061
}
6162

6263
static BreakpointLocation create(SourceElement[] sourceElements, SuspensionFilter filter) {
@@ -69,11 +70,14 @@ static BreakpointLocation create(SourceElement[] sourceElements, SuspensionFilte
6970

7071
abstract SourceSectionFilter createLocationFilter(Source source, SuspendAnchor suspendAnchor);
7172

72-
private static void setTags(SourceSectionFilter.Builder f, SourceElement[] sourceElements) {
73-
Class<?>[] elementTags = new Class<?>[sourceElements.length];
74-
for (int i = 0; i < elementTags.length; i++) {
73+
private static void setTags(SourceSectionFilter.Builder f, SourceElement[] sourceElements, Class<? extends Tag> tag) {
74+
Class<?>[] elementTags = new Class<?>[sourceElements.length + ((tag == null) ? 0 : 1)];
75+
for (int i = 0; i < sourceElements.length; i++) {
7576
elementTags[i] = sourceElements[i].getTag();
7677
}
78+
if (tag != null) {
79+
elementTags[elementTags.length - 1] = tag;
80+
}
7781
f.tagIs(elementTags);
7882
}
7983

@@ -85,25 +89,28 @@ private static final class BreakpointSourceLocation extends BreakpointLocation {
8589
private int line;
8690
private int column;
8791

92+
private final Class<? extends Tag> tag;
93+
8894
/**
8995
* @param key non-null source identifier
9096
* @param line 1-based line number, -1 for unspecified
9197
*/
92-
BreakpointSourceLocation(Object key, SourceElement[] sourceElements, SourceSection sourceSection) {
98+
BreakpointSourceLocation(Object key, SourceElement[] sourceElements, SourceSection sourceSection, Class<? extends Tag> tag) {
9399
assert key instanceof Source || key instanceof URI;
94100
this.key = key;
95101
this.sourceElements = sourceElements;
96102
this.sourceSection = sourceSection;
97103
this.line = -1;
98104
this.column = -1;
105+
this.tag = tag;
99106
}
100107

101108
/**
102109
* @param key non-null source identifier
103110
* @param line 1-based line number
104111
* @param column 1-based column number, -1 for unspecified
105112
*/
106-
BreakpointSourceLocation(Object key, SourceElement[] sourceElements, int line, int column) {
113+
BreakpointSourceLocation(Object key, SourceElement[] sourceElements, int line, int column, Class<? extends Tag> tag) {
107114
assert key instanceof Source || key instanceof URI;
108115
assert line > 0;
109116
assert column > 0 || column == -1;
@@ -112,6 +119,7 @@ private static final class BreakpointSourceLocation extends BreakpointLocation {
112119
this.line = line;
113120
this.column = column;
114121
this.sourceSection = null;
122+
this.tag = tag;
115123
}
116124

117125
private BreakpointSourceLocation() {
@@ -120,6 +128,7 @@ private BreakpointSourceLocation() {
120128
this.line = -1;
121129
this.column = -1;
122130
this.sourceSection = null;
131+
this.tag = null;
123132
}
124133

125134
@Override
@@ -159,7 +168,7 @@ SourceSection adjustLocation(Source source, TruffleInstrument.Env env, SuspendAn
159168
return null;
160169
}
161170
boolean hasColumn = column > 0;
162-
SourceSection location = SuspendableLocationFinder.findNearest(source, sourceElements, line, column, suspendAnchor, env);
171+
SourceSection location = SuspendableLocationFinder.findNearest(source, sourceElements, line, column, tag, suspendAnchor, env);
163172
if (location != null) {
164173
switch (suspendAnchor) {
165174
case BEFORE:
@@ -213,7 +222,7 @@ SourceSectionFilter createLocationFilter(Source source, SuspendAnchor suspendAnc
213222
if (sourceSection != null) {
214223
f.sourceSectionEquals(sourceSection);
215224
}
216-
setTags(f, sourceElements);
225+
setTags(f, sourceElements, tag);
217226
return f.build();
218227
}
219228

@@ -267,7 +276,7 @@ SourceSectionFilter createLocationFilter(Source source, SuspendAnchor suspendAnc
267276
}
268277
SourceFilter sourceFilter = sourceFilterBuilder.build();
269278
f.sourceFilter(sourceFilter);
270-
setTags(f, sourceElements);
279+
setTags(f, sourceElements, null);
271280
return f.build();
272281
}
273282

truffle/src/com.oracle.truffle.api.debug/src/com/oracle/truffle/api/debug/SuspendableLocationFinder.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ final class SuspendableLocationFinder {
5959
private SuspendableLocationFinder() {
6060
}
6161

62-
static SourceSection findNearest(Source source, SourceElement[] sourceElements, int line, int column, SuspendAnchor anchor, TruffleInstrument.Env env) {
62+
static SourceSection findNearest(Source source, SourceElement[] sourceElements, int line, int column, Class<? extends Tag> tag, SuspendAnchor anchor, TruffleInstrument.Env env) {
6363
int boundLine = line;
6464
int boundColumn = column;
6565
int maxLine = source.getLineCount();
@@ -70,17 +70,20 @@ static SourceSection findNearest(Source source, SourceElement[] sourceElements,
7070
if (boundColumn > maxColumn) {
7171
boundColumn = maxColumn;
7272
}
73-
return findNearestBound(source, getElementTags(sourceElements), boundLine, boundColumn, anchor, env);
73+
return findNearestBound(source, getElementTags(sourceElements, tag), boundLine, boundColumn, anchor, env);
7474
}
7575

76-
private static Set<Class<? extends Tag>> getElementTags(SourceElement[] sourceElements) {
77-
if (sourceElements.length == 1) {
76+
private static Set<Class<? extends Tag>> getElementTags(SourceElement[] sourceElements, Class<? extends Tag> tag) {
77+
if (sourceElements.length == 1 && tag == null) {
7878
return Collections.singleton(sourceElements[0].getTag());
7979
}
8080
Set<Class<? extends Tag>> elementTags = new HashSet<>();
8181
for (int i = 0; i < sourceElements.length; i++) {
8282
elementTags.add(sourceElements[i].getTag());
8383
}
84+
if (tag != null) {
85+
elementTags.add(tag);
86+
}
8487
return elementTags;
8588
}
8689

0 commit comments

Comments
 (0)