8301201: Allow \n@ inside inline tags using inlineContent

Reviewed-by: hannesw
This commit is contained in:
Jonathan Gibbons 2023-02-01 18:28:42 +00:00
parent 3361a26df4
commit 24ff3da054
4 changed files with 70 additions and 24 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 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
@ -623,11 +623,6 @@ public class DocCommentParser {
nextChar();
break;
case '@':
if (newline)
break loop;
// fallthrough
default:
if (textStart == -1)
textStart = bp;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 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
@ -895,8 +895,13 @@ public class DocCommentTester {
@Override
void check(TreePath path, Name name) throws Exception {
var annos = (path.getLeaf() instanceof MethodTree m)
? m.getModifiers().getAnnotations().toString()
: "";
boolean normalizeTags = !annos.equals("@NormalizeTags(false)");
String raw = trees.getDocComment(path);
String normRaw = normalize(raw);
String normRaw = normalize(raw, normalizeTags);
StringWriter out = new StringWriter();
DocPretty dp = new DocPretty(out);
@ -913,36 +918,44 @@ public class DocCommentTester {
}
/**
* Normalize white space in places where the tree does not preserve it.
* Maintain contents of at-code and at-literal inline tags.
* Normalize whitespace in places where the tree does not preserve it.
* Maintain contents of inline tags unless {@code normalizeTags} is
* {@code false}. This should normally be {@code true}, but should be
* set to {@code false} when there is syntactically invalid content
* that might resemble an inline tag, but which is not so.
*
* @param s the comment text to be normalized
* @param normalizeTags whether to normalize inline tags
* @return the normalized content
*/
String normalize(String s) {
String normalize(String s, boolean normalizeTags) {
String s2 = s.trim().replaceFirst("\\.\\s*\\n *@", ".\n@");
StringBuilder sb = new StringBuilder();
Pattern p = Pattern.compile("\\{@(code|literal)( )?");
Pattern p = Pattern.compile("(?i)\\{@([a-z][a-z0-9.:-]*)( )?");
Matcher m = p.matcher(s2);
int start = 0;
while (m.find(start)) {
sb.append(normalizeFragment(s2.substring(start, m.start())));
sb.append(m.group().trim());
start = copyLiteral(s2, m.end(), sb);
if (normalizeTags) {
while (m.find(start)) {
sb.append(normalizeFragment(s2.substring(start, m.start())));
sb.append(m.group().trim());
start = copyLiteral(s2, m.end(), sb);
}
}
sb.append(normalizeFragment(s2.substring(start)));
return sb.toString();
return sb.toString()
.replaceAll("(?i)\\{@([a-z][a-z0-9.:-]*)\\s+}", "{@$1}")
.replaceAll("(\\{@value\\s+[^}]+)\\s+(})", "$1$2");
}
String normalizeFragment(String s) {
return s.replaceAll("\\{@docRoot\\s+}", "{@docRoot}")
.replaceAll("\\{@inheritDoc\\s+}", "{@inheritDoc}")
.replaceAll("(\\{@value\\s+[^}]+)\\s+(})", "$1$2")
.replaceAll("\n[ \t]+@", "\n@");
return s.replaceAll("\n[ \t]+@", "\n@");
}
int copyLiteral(String s, int start, StringBuilder sb) {
int depth = 0;
for (int i = start; i < s.length(); i++) {
char ch = s.charAt(i);
if (i == start && !Character.isWhitespace(ch)) {
if (i == start && !Character.isWhitespace(ch) && ch != '}') {
sb.append(' ');
}
switch (ch) {

View File

@ -241,6 +241,7 @@ DocComment[DOC_COMMENT, pos:1
* abc {@index
* @return def} xyz
*/
@NormalizeTags(false) // see DocCommentTester.PrettyChecker
void bad_nl_at_in_term() {}
/*
DocComment[DOC_COMMENT, pos:1

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 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 7021614 8078320 8273244 8284908
* @bug 7021614 8078320 8273244 8284908 8301201
* @summary extend com.sun.source API to support parsing javadoc comments
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.file
@ -182,6 +182,43 @@ DocComment[DOC_COMMENT, pos:1
body: empty
block tags: empty
]
*/
/**
* {@code
* abc
* @def
* ghi
* }
*/
void inline_text_at() { }
/*
DocComment[DOC_COMMENT, pos:1
firstSentence: 1
Literal[CODE, pos:1, |_abc|_@def|_ghi|_]
body: empty
block tags: empty
]
*/
/**
* {@tag abc
* @def
* ghi
* }
*/
void inline_content_at() { }
/*
DocComment[DOC_COMMENT, pos:1
firstSentence: 1
UnknownInlineTag[UNKNOWN_INLINE_TAG, pos:1
tag:tag
content: 1
Text[TEXT, pos:7, abc|_@def|_ghi|_]
]
body: empty
block tags: empty
]
*/
}