Skip to content

Commit

Permalink
8332858: References with escapes have broken positions after they are…
Browse files Browse the repository at this point in the history
… transformed

Reviewed-by: vromero, jjg
  • Loading branch information
lahodaj committed May 31, 2024
1 parent 1b7d59f commit 2ab8ab5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,9 @@ public void visit(Link link) {
// determine whether to use {@link ... } or {@linkplain ...}
// based on whether the "link text" is the same as the "link destination"
String ref = dest.substring(autorefScheme.length());
int refPos = sourcePosToTreePos(getRefPos(ref, link));
var newRefTree = m.at(refPos).newReferenceTree(ref).setEndPos(refPos + ref.length());
int[] span = getRefSpan(ref, link);
int refPos = sourcePosToTreePos(span[0]);
var newRefTree = m.at(refPos).newReferenceTree(ref).setEndPos(sourcePosToTreePos(span[1]));

Node child = link.getFirstChild();
DocTree.Kind linkKind = child.getNext() == null
Expand Down Expand Up @@ -835,7 +836,7 @@ public void visit(Link link) {
* @param ref the reference to find
* @param link the link containing the reference
*/
private int getRefPos(String ref, Link link) {
private int[] getRefSpan(String ref, Link link) {
var spans = link.getSourceSpans();
var revSpanIter = spans.listIterator(spans.size());
while (revSpanIter.hasPrevious()) {
Expand All @@ -845,11 +846,19 @@ private int getRefPos(String ref, Link link) {
var s = source.substring(start, end);
var index = s.lastIndexOf(ref);
if (index != -1) {
return start + index;
return new int[] {start + index, start + index + ref.length()};
} else {
String escapedRef = ref.replace("[]", "\\[\\]");
var escapedIndex = s.lastIndexOf(escapedRef);
if (escapedIndex != -1) {
return new int[] {start + escapedIndex,
start + escapedIndex + escapedRef.length()};
}
}
}
return NOPOS;
return NOSPAN;
}
private static final int[] NOSPAN = new int[] {NOPOS, NOPOS};

/**
* {@return the position in the original comment for a position in {@code source},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@

/*
* @test
* @bug 8332858
* @summary test case for Markdown positions
* @run main/othervm --limit-modules jdk.compiler MarkdownTransformerPositionTest
* @run main MarkdownTransformerPositionTest
* @run main MarkdownTransformerPositionTest links
*/

import com.sun.source.doctree.DocCommentTree;
import com.sun.source.doctree.LinkTree;
import com.sun.source.doctree.RawTextTree;
import com.sun.source.doctree.ReferenceTree;
import com.sun.source.tree.*;
import com.sun.source.util.*;

Expand All @@ -50,6 +53,10 @@ public static void main(String... args) throws Exception {

t.simpleTest();
t.testWithReplacements();

if (args.length > 0 && "links".equals(args[0])) {
t.linkWithEscapes();
}
}

private void simpleTest() throws Exception {
Expand All @@ -76,6 +83,16 @@ public class Test {
"testAuthor");
}

private void linkWithEscapes() throws Exception {
runConvertedLinksTest("""
/// Markdown comment.
/// [java.util.Arrays#asList(Object\\[\\])]
public class Test {
}
""",
"java.util.Arrays#asList(Object\\[\\])");
}

private void runTest(String source, String... expectedRawSpans) throws Exception {
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
JavacTask task = (JavacTask)comp.getTask(null, null, null, null, null, Arrays.asList(new JavaSource(source)));
Expand Down Expand Up @@ -107,6 +124,46 @@ public Void visitRawText(RawTextTree node, Void p) {
System.err.println("Test result: success, boot modules: " + ModuleLayer.boot().modules());
}

private void runConvertedLinksTest(String source, String... expectedRawSpans) throws Exception {
JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
JavacTask task = (JavacTask)comp.getTask(null, null, null, null, null, Arrays.asList(new JavaSource(source)));
CompilationUnitTree cu = task.parse().iterator().next();
task.analyze();
DocTrees trees = DocTrees.instance(task);
List<String> rawSpans = new ArrayList<>();
TreePath clazzTP = new TreePath(new TreePath(cu), cu.getTypeDecls().get(0));
Element clazz = trees.getElement(clazzTP);
DocCommentTree docComment = trees.getDocCommentTree(clazz);

new DocTreeScanner<Void, Void>() {
@Override
public Void visitLink(LinkTree node, Void p) {
int start = (int) trees.getSourcePositions().getStartPosition(cu, docComment, node);
if (start != (-1)) {
throw new AssertionError("UNexpected start position for synthetic link: " + start);
}
return super.visitLink(node, p);
}

@Override
public Void visitReference(ReferenceTree node, Void p) {
int start = (int) trees.getSourcePositions().getStartPosition(cu, docComment, node);
int end = (int) trees.getSourcePositions().getEndPosition(cu, docComment, node);
rawSpans.add(source.substring(start, end));
return super.visitReference(node, p);
}
}.scan(docComment, null);

List<String> expectedRawSpansList = List.of(expectedRawSpans);

if (!expectedRawSpansList.equals(rawSpans)) {
throw new AssertionError("Incorrect raw text spans, should be: " +
expectedRawSpansList + ", but is: " + rawSpans);
}

System.err.println("Test result: success, boot modules: " + ModuleLayer.boot().modules());
}

static class JavaSource extends SimpleJavaFileObject {

private final String source;
Expand Down

1 comment on commit 2ab8ab5

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.