aoqi@0: /*
aefimov@3315: * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.tools.doclint;
aoqi@0:
aoqi@0: import java.io.IOException;
aoqi@0: import java.io.StringWriter;
aoqi@0: import java.net.URI;
aoqi@0: import java.net.URISyntaxException;
aoqi@0: import java.util.Deque;
aoqi@0: import java.util.EnumSet;
aoqi@0: import java.util.HashMap;
aoqi@0: import java.util.HashSet;
aoqi@0: import java.util.LinkedList;
aoqi@0: import java.util.List;
aoqi@0: import java.util.Map;
aoqi@0: import java.util.Set;
aoqi@0: import java.util.regex.Matcher;
aoqi@0: import java.util.regex.Pattern;
aoqi@0:
aoqi@0: import javax.lang.model.element.Element;
aoqi@0: import javax.lang.model.element.ElementKind;
aoqi@0: import javax.lang.model.element.ExecutableElement;
aoqi@0: import javax.lang.model.element.Name;
aoqi@0: import javax.lang.model.element.VariableElement;
aoqi@0: import javax.lang.model.type.TypeKind;
aoqi@0: import javax.lang.model.type.TypeMirror;
aoqi@0: import javax.tools.Diagnostic.Kind;
aoqi@0: import javax.tools.JavaFileObject;
aoqi@0:
aoqi@0: import com.sun.source.doctree.AttributeTree;
aoqi@0: import com.sun.source.doctree.AuthorTree;
aoqi@0: import com.sun.source.doctree.DocCommentTree;
aoqi@0: import com.sun.source.doctree.DocRootTree;
aoqi@0: import com.sun.source.doctree.DocTree;
aoqi@0: import com.sun.source.doctree.EndElementTree;
aoqi@0: import com.sun.source.doctree.EntityTree;
aoqi@0: import com.sun.source.doctree.ErroneousTree;
aoqi@0: import com.sun.source.doctree.IdentifierTree;
aoqi@0: import com.sun.source.doctree.InheritDocTree;
aoqi@0: import com.sun.source.doctree.LinkTree;
aoqi@0: import com.sun.source.doctree.LiteralTree;
aoqi@0: import com.sun.source.doctree.ParamTree;
aoqi@0: import com.sun.source.doctree.ReferenceTree;
aoqi@0: import com.sun.source.doctree.ReturnTree;
aoqi@0: import com.sun.source.doctree.SerialDataTree;
aoqi@0: import com.sun.source.doctree.SerialFieldTree;
aoqi@0: import com.sun.source.doctree.SinceTree;
aoqi@0: import com.sun.source.doctree.StartElementTree;
aoqi@0: import com.sun.source.doctree.TextTree;
aoqi@0: import com.sun.source.doctree.ThrowsTree;
aoqi@0: import com.sun.source.doctree.UnknownBlockTagTree;
aoqi@0: import com.sun.source.doctree.UnknownInlineTagTree;
aoqi@0: import com.sun.source.doctree.ValueTree;
aoqi@0: import com.sun.source.doctree.VersionTree;
aoqi@0: import com.sun.source.util.DocTreePath;
aoqi@0: import com.sun.source.util.DocTreePathScanner;
aoqi@0: import com.sun.source.util.TreePath;
aoqi@0: import com.sun.tools.doclint.HtmlTag.AttrKind;
aoqi@0: import com.sun.tools.javac.tree.DocPretty;
aoqi@0: import com.sun.tools.javac.util.StringUtils;
aoqi@0: import static com.sun.tools.doclint.Messages.Group.*;
aoqi@0:
aoqi@0:
aoqi@0: /**
aoqi@0: * Validate a doc comment.
aoqi@0: *
aoqi@0: *
This is NOT part of any supported API.
aoqi@0: * If you write code that depends on this, you do so at your own
aoqi@0: * risk. This code and its internal interfaces are subject to change
aoqi@0: * or deletion without notice.
aoqi@0: */
aoqi@0: public class Checker extends DocTreePathScanner {
aoqi@0: final Env env;
aoqi@0:
aoqi@0: Set foundParams = new HashSet<>();
aoqi@0: Set foundThrows = new HashSet<>();
aoqi@0: Map> foundAnchors = new HashMap<>();
aoqi@0: boolean foundInheritDoc = false;
aoqi@0: boolean foundReturn = false;
aoqi@0:
aoqi@0: public enum Flag {
aoqi@0: TABLE_HAS_CAPTION,
aoqi@0: HAS_ELEMENT,
aoqi@0: HAS_INLINE_TAG,
aoqi@0: HAS_TEXT,
aoqi@0: REPORTED_BAD_INLINE
aoqi@0: }
aoqi@0:
aoqi@0: static class TagStackItem {
aoqi@0: final DocTree tree; // typically, but not always, StartElementTree
aoqi@0: final HtmlTag tag;
aoqi@0: final Set attrs;
aoqi@0: final Set flags;
aoqi@0: TagStackItem(DocTree tree, HtmlTag tag) {
aoqi@0: this.tree = tree;
aoqi@0: this.tag = tag;
aoqi@0: attrs = EnumSet.noneOf(HtmlTag.Attr.class);
aoqi@0: flags = EnumSet.noneOf(Flag.class);
aoqi@0: }
aoqi@0: @Override
aoqi@0: public String toString() {
aoqi@0: return String.valueOf(tag);
aoqi@0: }
aoqi@0: }
aoqi@0:
aefimov@3315: private final Deque tagStack; // TODO: maybe want to record starting tree as well
aoqi@0: private HtmlTag currHeaderTag;
aoqi@0:
aoqi@0: private final int implicitHeaderLevel;
aoqi@0:
aoqi@0: //
aoqi@0:
aoqi@0: Checker(Env env) {
aoqi@0: env.getClass();
aoqi@0: this.env = env;
aoqi@0: tagStack = new LinkedList<>();
aoqi@0: implicitHeaderLevel = env.implicitHeaderLevel;
aoqi@0: }
aoqi@0:
aoqi@0: public Void scan(DocCommentTree tree, TreePath p) {
aoqi@0: env.setCurrent(p, tree);
aoqi@0:
aoqi@0: boolean isOverridingMethod = !env.currOverriddenMethods.isEmpty();
aoqi@0:
aoqi@0: if (p.getLeaf() == p.getCompilationUnit()) {
aoqi@0: // If p points to a compilation unit, the implied declaration is the
aoqi@0: // package declaration (if any) for the compilation unit.
aoqi@0: // Handle this case specially, because doc comments are only
aoqi@0: // expected in package-info files.
aoqi@0: JavaFileObject fo = p.getCompilationUnit().getSourceFile();
aoqi@0: boolean isPkgInfo = fo.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE);
aoqi@0: if (tree == null) {
aoqi@0: if (isPkgInfo)
aoqi@0: reportMissing("dc.missing.comment");
aoqi@0: return null;
aoqi@0: } else {
aoqi@0: if (!isPkgInfo)
aoqi@0: reportReference("dc.unexpected.comment");
aoqi@0: }
aoqi@0: } else {
aoqi@0: if (tree == null) {
aoqi@0: if (!isSynthetic() && !isOverridingMethod)
aoqi@0: reportMissing("dc.missing.comment");
aoqi@0: return null;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: tagStack.clear();
aoqi@0: currHeaderTag = null;
aoqi@0:
aoqi@0: foundParams.clear();
aoqi@0: foundThrows.clear();
aoqi@0: foundInheritDoc = false;
aoqi@0: foundReturn = false;
aoqi@0:
aoqi@0: scan(new DocTreePath(p, tree), null);
aoqi@0:
aoqi@0: if (!isOverridingMethod) {
aoqi@0: switch (env.currElement.getKind()) {
aoqi@0: case METHOD:
aoqi@0: case CONSTRUCTOR: {
aoqi@0: ExecutableElement ee = (ExecutableElement) env.currElement;
aoqi@0: checkParamsDocumented(ee.getTypeParameters());
aoqi@0: checkParamsDocumented(ee.getParameters());
aoqi@0: switch (ee.getReturnType().getKind()) {
aoqi@0: case VOID:
aoqi@0: case NONE:
aoqi@0: break;
aoqi@0: default:
aoqi@0: if (!foundReturn
aoqi@0: && !foundInheritDoc
aoqi@0: && !env.types.isSameType(ee.getReturnType(), env.java_lang_Void)) {
aoqi@0: reportMissing("dc.missing.return");
aoqi@0: }
aoqi@0: }
aoqi@0: checkThrowsDocumented(ee.getThrownTypes());
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: return null;
aoqi@0: }
aoqi@0:
aoqi@0: private void reportMissing(String code, Object... args) {
aoqi@0: env.messages.report(MISSING, Kind.WARNING, env.currPath.getLeaf(), code, args);
aoqi@0: }
aoqi@0:
aoqi@0: private void reportReference(String code, Object... args) {
aoqi@0: env.messages.report(REFERENCE, Kind.WARNING, env.currPath.getLeaf(), code, args);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public Void visitDocComment(DocCommentTree tree, Void ignore) {
aoqi@0: super.visitDocComment(tree, ignore);
aoqi@0: for (TagStackItem tsi: tagStack) {
aoqi@0: warnIfEmpty(tsi, null);
aoqi@0: if (tsi.tree.getKind() == DocTree.Kind.START_ELEMENT
aoqi@0: && tsi.tag.endKind == HtmlTag.EndKind.REQUIRED) {
aoqi@0: StartElementTree t = (StartElementTree) tsi.tree;
aoqi@0: env.messages.error(HTML, t, "dc.tag.not.closed", t.getName());
aoqi@0: }
aoqi@0: }
aoqi@0: return null;
aoqi@0: }
aoqi@0: //
aoqi@0:
aoqi@0: //
aoqi@0:
aoqi@0: @Override
aoqi@0: public Void visitText(TextTree tree, Void ignore) {
aoqi@0: if (hasNonWhitespace(tree)) {
aoqi@0: checkAllowsText(tree);
aoqi@0: markEnclosingTag(Flag.HAS_TEXT);
aoqi@0: }
aoqi@0: return null;
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public Void visitEntity(EntityTree tree, Void ignore) {
aoqi@0: checkAllowsText(tree);
aoqi@0: markEnclosingTag(Flag.HAS_TEXT);
aoqi@0: String name = tree.getName().toString();
aoqi@0: if (name.startsWith("#")) {
aoqi@0: int v = StringUtils.toLowerCase(name).startsWith("#x")
aoqi@0: ? Integer.parseInt(name.substring(2), 16)
aoqi@0: : Integer.parseInt(name.substring(1), 10);
aoqi@0: if (!Entity.isValid(v)) {
aoqi@0: env.messages.error(HTML, tree, "dc.entity.invalid", name);
aoqi@0: }
aoqi@0: } else if (!Entity.isValid(name)) {
aoqi@0: env.messages.error(HTML, tree, "dc.entity.invalid", name);
aoqi@0: }
aoqi@0: return null;
aoqi@0: }
aoqi@0:
aoqi@0: void checkAllowsText(DocTree tree) {
aoqi@0: TagStackItem top = tagStack.peek();
aoqi@0: if (top != null
aoqi@0: && top.tree.getKind() == DocTree.Kind.START_ELEMENT
aoqi@0: && !top.tag.acceptsText()) {
aoqi@0: if (top.flags.add(Flag.REPORTED_BAD_INLINE)) {
aoqi@0: env.messages.error(HTML, tree, "dc.text.not.allowed",
aoqi@0: ((StartElementTree) top.tree).getName());
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: //
aoqi@0:
aoqi@0: //
aoqi@0:
aoqi@0: @Override
aoqi@0: public Void visitStartElement(StartElementTree tree, Void ignore) {
aoqi@0: final Name treeName = tree.getName();
aoqi@0: final HtmlTag t = HtmlTag.get(treeName);
aoqi@0: if (t == null) {
aoqi@0: env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
aoqi@0: } else {
aoqi@0: boolean done = false;
aoqi@0: for (TagStackItem tsi: tagStack) {
aoqi@0: if (tsi.tag.accepts(t)) {
aoqi@0: while (tagStack.peek() != tsi) {
aoqi@0: warnIfEmpty(tagStack.peek(), null);
aoqi@0: tagStack.pop();
aoqi@0: }
aoqi@0: done = true;
aoqi@0: break;
aoqi@0: } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) {
aoqi@0: done = true;
aoqi@0: break;
aoqi@0: }
aoqi@0: }
aoqi@0: if (!done && HtmlTag.BODY.accepts(t)) {
aoqi@0: while (!tagStack.isEmpty()) {
aoqi@0: warnIfEmpty(tagStack.peek(), null);
aoqi@0: tagStack.pop();
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: markEnclosingTag(Flag.HAS_ELEMENT);
aoqi@0: checkStructure(tree, t);
aoqi@0:
aoqi@0: // tag specific checks
aoqi@0: switch (t) {
aoqi@0: // check for out of sequence headers, such as ...
...
aoqi@0: case H1: case H2: case H3: case H4: case H5: case H6:
aoqi@0: checkHeader(tree, t);
aoqi@0: break;
aoqi@0: }
aoqi@0:
aoqi@0: if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
aoqi@0: for (TagStackItem i: tagStack) {
aoqi@0: if (t == i.tag) {
aoqi@0: env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
aoqi@0: break;
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: // check for self closing tags, such as
aoqi@0: if (tree.isSelfClosing()) {
aoqi@0: env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
aoqi@0: }
aoqi@0:
aoqi@0: try {
aoqi@0: TagStackItem parent = tagStack.peek();
aoqi@0: TagStackItem top = new TagStackItem(tree, t);
aoqi@0: tagStack.push(top);
aoqi@0:
aoqi@0: super.visitStartElement(tree, ignore);
aoqi@0:
aoqi@0: // handle attributes that may or may not have been found in start element
aoqi@0: if (t != null) {
aoqi@0: switch (t) {
aoqi@0: case CAPTION:
aoqi@0: if (parent != null && parent.tag == HtmlTag.TABLE)
aoqi@0: parent.flags.add(Flag.TABLE_HAS_CAPTION);
aoqi@0: break;
aoqi@0:
aoqi@0: case IMG:
aoqi@0: if (!top.attrs.contains(HtmlTag.Attr.ALT))
aoqi@0: env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
aoqi@0: break;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: return null;
aoqi@0: } finally {
aoqi@0:
aoqi@0: if (t == null || t.endKind == HtmlTag.EndKind.NONE)
aoqi@0: tagStack.pop();
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: private void checkStructure(StartElementTree tree, HtmlTag t) {
aoqi@0: Name treeName = tree.getName();
aoqi@0: TagStackItem top = tagStack.peek();
aoqi@0: switch (t.blockType) {
aoqi@0: case BLOCK:
aoqi@0: if (top == null || top.tag.accepts(t))
aoqi@0: return;
aoqi@0:
aoqi@0: switch (top.tree.getKind()) {
aoqi@0: case START_ELEMENT: {
aoqi@0: if (top.tag.blockType == HtmlTag.BlockType.INLINE) {
aoqi@0: Name name = ((StartElementTree) top.tree).getName();
aoqi@0: env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.element",
aoqi@0: treeName, name);
aoqi@0: return;
aoqi@0: }
aoqi@0: }
aoqi@0: break;
aoqi@0:
aoqi@0: case LINK:
aoqi@0: case LINK_PLAIN: {
aoqi@0: String name = top.tree.getKind().tagName;
aoqi@0: env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.tag",
aoqi@0: treeName, name);
aoqi@0: return;
aoqi@0: }
aoqi@0: }
aoqi@0: break;
aoqi@0:
aoqi@0: case INLINE:
aoqi@0: if (top == null || top.tag.accepts(t))
aoqi@0: return;
aoqi@0: break;
aoqi@0:
aoqi@0: case LIST_ITEM:
aoqi@0: case TABLE_ITEM:
aoqi@0: if (top != null) {
aoqi@0: // reset this flag so subsequent bad inline content gets reported
aoqi@0: top.flags.remove(Flag.REPORTED_BAD_INLINE);
aoqi@0: if (top.tag.accepts(t))
aoqi@0: return;
aoqi@0: }
aoqi@0: break;
aoqi@0:
aoqi@0: case OTHER:
aefimov@3315: switch (t) {
aefimov@3315: case SCRIPT:
aefimov@3315: //