8319339: Internal error on spurious markup in a hybrid snippet

Reviewed-by: jjg
This commit is contained in:
Pavel Rappo 2023-11-10 16:50:53 +00:00
parent ea1ffa3419
commit c9077b8b81
3 changed files with 142 additions and 9 deletions

View File

@ -376,9 +376,10 @@ public class SnippetTaglet extends BaseTaglet {
StyledText externalSnippet = null;
try {
Diags d = (text, pos) -> {
Diags d = (key, pos) -> {
var path = utils.getCommentHelper(holder)
.getDocTreePath(snippetTag.getBody());
var text = resources.getText(key);
config.getReporter().print(Diagnostic.Kind.WARNING,
path, pos, pos, pos, text);
};
@ -397,7 +398,7 @@ public class SnippetTaglet extends BaseTaglet {
try {
var finalFileObject = fileObject;
Diags d = (text, pos) -> messages.warning(finalFileObject, pos, pos, pos, text);
Diags d = (key, pos) -> messages.warning(finalFileObject, pos, pos, pos, key);
if (externalContent != null) {
externalSnippet = parse(resources, d, language, externalContent);
}
@ -484,7 +485,7 @@ public class SnippetTaglet extends BaseTaglet {
}
public interface Diags {
void warn(String text, int pos);
void warn(String key, int pos);
}
private static String stringValueOf(AttributeTree at) throws BadSnippetException {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, 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
@ -161,7 +161,7 @@ public final class Parser {
}
}
if (parsedTags.isEmpty()) { // (2)
diags.warn(resources.getText("doclet.snippet.markup.spurious"), next.offset() + markedUpLine.start("markup"));
diags.warn("doclet.snippet.markup.spurious", next.offset() + markedUpLine.start("markup"));
line = rawLine + (addLineTerminator ? "\n" : "");
} else { // (3)
hasMarkup = true;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023, 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
@ -23,7 +23,7 @@
/*
* @test
* @bug 8266666 8281969
* @bug 8266666 8281969 8319339
* @summary Implementation for snippets
* @library /tools/lib ../../lib
* @modules jdk.compiler/com.sun.tools.javac.api
@ -259,8 +259,12 @@ public class TestSnippetMarkup extends SnippetTester {
Path srcDir = base.resolve("src");
Path outDir = base.resolve("out");
var goodFile = "good.txt";
// use two files that differ in name but not content, to work around
// error deduplication, whereby an error related to coordinates
// (file, pos) reported before is suppressed; see:
// com.sun.tools.javac.util.Log.shouldReport(JavaFileObject, int)
var badFile = "bad.txt";
var badFile2 = "bad2.txt"; // to workaround error deduplication
var badFile2 = "bad2.txt";
new ClassBuilder(tb, "pkg.A")
.setModifiers("public", "class")
.addMembers(
@ -625,7 +629,7 @@ First line // @highlight :
}
@Test
public void testPositiveInlineTagMarkup_FalseMarkup(Path base) throws Exception {
public void testPositiveInlineTagMarkup_SpuriousMarkup(Path base) throws Exception {
var testCases = List.of(
new TestCase(
"""
@ -661,6 +665,134 @@ First line // @highlight :
""")
);
testPositive(base, testCases);
checkOutput(Output.OUT, true, """
A.java:6: warning: spurious markup
// @formatter:off
^""","""
A.java:9: warning: spurious markup
// @formatter:on
^""","""
A.java:17: warning: spurious markup
// @formatter:off
^""","""
A.java:22: warning: spurious markup
// @formatter:on
^""");
}
/*
* If spurious markup appears in an external snippet or either side of a
* hybrid snippet, then all of the below is true:
*
* - no error is raised
* - relevant warnings are emitted
* - spurious markup is output literally
*/
@Test
public void testPositiveExternalHybridTagMarkup_SpuriousMarkup(Path base) throws Exception {
Path srcDir = base.resolve("src");
Path outDir = base.resolve("out");
var plain = "plain.txt";
var withRegion = "withRegion.txt";
new ClassBuilder(tb, "pkg.A")
.setModifiers("public", "class")
.addMembers(
ClassBuilder.MethodBuilder
.parse("public void external() { }")
.setComments("""
{@snippet file="%s"}
""".formatted(plain)))
.addMembers(
ClassBuilder.MethodBuilder
.parse("public void hybrid1() { }")
.setComments("""
{@snippet file="%s":
First line
// @formatter:off
Second Line
Third line
// @formatter:on
Fourth line
}
""".formatted(plain)))
.addMembers(
ClassBuilder.MethodBuilder
.parse("public void hybrid2() { }")
.setComments("""
{@snippet file="%s" region="showThis" :
Second Line
Third line
}
""".formatted(withRegion)))
.addMembers(
ClassBuilder.MethodBuilder
.parse("public void hybrid3() { }")
.setComments("""
{@snippet file="%s" region="showThis" :
First line
// @formatter:off
Second Line // @start region=showThis
Third line
// @end
// @formatter:on
Fourth line
}
""".formatted(withRegion)))
.write(srcDir);
addSnippetFile(srcDir, "pkg", plain, """
First line
// @formatter:off
Second Line
Third line
// @formatter:on
Fourth line
""");
addSnippetFile(srcDir, "pkg", withRegion, """
First line
// @formatter:off
Second Line // @start region=showThis
Third line
// @end
// @formatter:on
Fourth line
""");
javadoc("-d", outDir.toString(),
"-sourcepath", srcDir.toString(),
"pkg");
checkExit(Exit.OK);
checkNoCrashes();
checkOutput(Output.OUT, true, """
%s:2: warning: spurious markup
// @formatter:off
^""".formatted(plain), """
%s:5: warning: spurious markup
// @formatter:on
^""".formatted(plain), """
A.java:11: warning: spurious markup
// @formatter:off
^""", """
A.java:14: warning: spurious markup
// @formatter:on
^""", """
%s:2: warning: spurious markup
// @formatter:off
^""".formatted(plain), """
%s:5: warning: spurious markup
// @formatter:on
^""".formatted(plain), """
%s:2: warning: spurious markup
// @formatter:off
^""".formatted(withRegion), """
%s:6: warning: spurious markup
// @formatter:on
^""".formatted(withRegion), """
A.java:31: warning: spurious markup
// @formatter:off
^""", """
A.java:35: warning: spurious markup
// @formatter:on
^""");
}
@Test