1 /*
2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package com.sun.tools.doclint;
27 
28 import java.io.IOException;
29 import java.io.StringWriter;
30 import java.net.URI;
31 import java.net.URISyntaxException;
32 import java.util.Deque;
33 import java.util.EnumSet;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.LinkedList;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Set;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
42 
43 import javax.lang.model.element.Element;
44 import javax.lang.model.element.ElementKind;
45 import javax.lang.model.element.ExecutableElement;
46 import javax.lang.model.element.Name;
47 import javax.lang.model.element.VariableElement;
48 import javax.lang.model.type.TypeKind;
49 import javax.lang.model.type.TypeMirror;
50 import javax.tools.Diagnostic.Kind;
51 import javax.tools.JavaFileObject;
52 
53 import com.sun.source.doctree.AttributeTree;
54 import com.sun.source.doctree.AuthorTree;
55 import com.sun.source.doctree.DocCommentTree;
56 import com.sun.source.doctree.DocRootTree;
57 import com.sun.source.doctree.DocTree;
58 import com.sun.source.doctree.EndElementTree;
59 import com.sun.source.doctree.EntityTree;
60 import com.sun.source.doctree.ErroneousTree;
61 import com.sun.source.doctree.IdentifierTree;
62 import com.sun.source.doctree.IndexTree;
63 import com.sun.source.doctree.InheritDocTree;
64 import com.sun.source.doctree.LinkTree;
65 import com.sun.source.doctree.LiteralTree;
66 import com.sun.source.doctree.ParamTree;
67 import com.sun.source.doctree.ProvidesTree;
68 import com.sun.source.doctree.ReferenceTree;
69 import com.sun.source.doctree.ReturnTree;
70 import com.sun.source.doctree.SerialDataTree;
71 import com.sun.source.doctree.SerialFieldTree;
72 import com.sun.source.doctree.SinceTree;
73 import com.sun.source.doctree.StartElementTree;
74 import com.sun.source.doctree.SummaryTree;
75 import com.sun.source.doctree.SystemPropertyTree;
76 import com.sun.source.doctree.TextTree;
77 import com.sun.source.doctree.ThrowsTree;
78 import com.sun.source.doctree.UnknownBlockTagTree;
79 import com.sun.source.doctree.UnknownInlineTagTree;
80 import com.sun.source.doctree.UsesTree;
81 import com.sun.source.doctree.ValueTree;
82 import com.sun.source.doctree.VersionTree;
83 import com.sun.source.tree.Tree;
84 import com.sun.source.util.DocTreePath;
85 import com.sun.source.util.DocTreePathScanner;
86 import com.sun.source.util.TreePath;
87 import com.sun.tools.doclint.HtmlTag.AttrKind;
88 import com.sun.tools.javac.tree.DocPretty;
89 import com.sun.tools.javac.util.Assert;
90 import com.sun.tools.javac.util.DefinedBy;
91 import com.sun.tools.javac.util.DefinedBy.Api;
92 import com.sun.tools.javac.util.StringUtils;
93 
94 import static com.sun.tools.doclint.Messages.Group.*;
95 
96 
97 /**
98  * Validate a doc comment.
99  *
100  * <p><b>This is NOT part of any supported API.
101  * If you write code that depends on this, you do so at your own
102  * risk.  This code and its internal interfaces are subject to change
103  * or deletion without notice.</b></p>
104  */
105 public class Checker extends DocTreePathScanner<Void, Void> {
106     final Env env;
107 
108     Set<Element> foundParams = new HashSet<>();
109     Set<TypeMirror> foundThrows = new HashSet<>();
110     Map<Element, Set<String>> foundAnchors = new HashMap<>();
111     boolean foundInheritDoc = false;
112     boolean foundReturn = false;
113     boolean hasNonWhitespaceText = false;
114 
115     public enum Flag {
116         TABLE_HAS_CAPTION,
117         HAS_ELEMENT,
118         HAS_HEADING,
119         HAS_INLINE_TAG,
120         HAS_TEXT,
121         REPORTED_BAD_INLINE
122     }
123 
124     static class TagStackItem {
125         final DocTree tree; // typically, but not always, StartElementTree
126         final HtmlTag tag;
127         final Set<HtmlTag.Attr> attrs;
128         final Set<Flag> flags;
TagStackItem(DocTree tree, HtmlTag tag)129         TagStackItem(DocTree tree, HtmlTag tag) {
130             this.tree = tree;
131             this.tag = tag;
132             attrs = EnumSet.noneOf(HtmlTag.Attr.class);
133             flags = EnumSet.noneOf(Flag.class);
134         }
135         @Override
toString()136         public String toString() {
137             return String.valueOf(tag);
138         }
139     }
140 
141     private final Deque<TagStackItem> tagStack; // TODO: maybe want to record starting tree as well
142     private HtmlTag currHeadingTag;
143 
144     private int implicitHeadingRank;
145 
146     // <editor-fold defaultstate="collapsed" desc="Top level">
147 
Checker(Env env)148     Checker(Env env) {
149         this.env = Assert.checkNonNull(env);
150         tagStack = new LinkedList<>();
151     }
152 
scan(DocCommentTree tree, TreePath p)153     public Void scan(DocCommentTree tree, TreePath p) {
154         env.initTypes();
155         env.setCurrent(p, tree);
156 
157         boolean isOverridingMethod = !env.currOverriddenMethods.isEmpty();
158         JavaFileObject fo = p.getCompilationUnit().getSourceFile();
159 
160         if (p.getLeaf().getKind() == Tree.Kind.PACKAGE) {
161             // If p points to a package, the implied declaration is the
162             // package declaration (if any) for the compilation unit.
163             // Handle this case specially, because doc comments are only
164             // expected in package-info files.
165             boolean isPkgInfo = fo.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE);
166             if (tree == null) {
167                 if (isPkgInfo)
168                     reportMissing("dc.missing.comment");
169                 return null;
170             } else {
171                 if (!isPkgInfo)
172                     reportReference("dc.unexpected.comment");
173             }
174         } else if (tree != null && fo.isNameCompatible("package", JavaFileObject.Kind.HTML)) {
175             // a package.html file with a DocCommentTree
176             if (tree.getFullBody().isEmpty()) {
177                 reportMissing("dc.missing.comment");
178                 return null;
179             }
180         } else {
181             if (tree == null) {
182                 if (!isSynthetic() && !isOverridingMethod)
183                     reportMissing("dc.missing.comment");
184                 return null;
185             }
186         }
187 
188         tagStack.clear();
189         currHeadingTag = null;
190 
191         foundParams.clear();
192         foundThrows.clear();
193         foundInheritDoc = false;
194         foundReturn = false;
195         hasNonWhitespaceText = false;
196 
197         switch (p.getLeaf().getKind()) {
198             // the following are for declarations that have their own top-level page,
199             // and so the doc comment comes after the <h1> page title.
200             case MODULE:
201             case PACKAGE:
202             case CLASS:
203             case INTERFACE:
204             case ENUM:
205             case ANNOTATION_TYPE:
206             case RECORD:
207                 implicitHeadingRank = 1;
208                 break;
209 
210             // this is for html files
211             // ... if it is a legacy package.html, the doc comment comes after the <h1> page title
212             // ... otherwise, (e.g. overview file and doc-files/*.html files) no additional headings are inserted
213             case COMPILATION_UNIT:
214                 implicitHeadingRank = fo.isNameCompatible("package", JavaFileObject.Kind.HTML) ? 1 : 0;
215                 break;
216 
217             // the following are for member declarations, which appear in the page
218             // for the enclosing type, and so appear after the <h2> "Members"
219             // aggregate heading and the specific <h3> "Member signature" heading.
220             case METHOD:
221             case VARIABLE:
222                 implicitHeadingRank = 3;
223                 break;
224 
225             default:
226                 Assert.error("unexpected tree kind: " + p.getLeaf().getKind() + " " + fo);
227         }
228 
229         scan(new DocTreePath(p, tree), null);
230 
231         if (!isOverridingMethod) {
232             switch (env.currElement.getKind()) {
233                 case METHOD:
234                 case CONSTRUCTOR: {
235                     ExecutableElement ee = (ExecutableElement) env.currElement;
236                     checkParamsDocumented(ee.getTypeParameters());
237                     checkParamsDocumented(ee.getParameters());
238                     switch (ee.getReturnType().getKind()) {
239                         case VOID:
240                         case NONE:
241                             break;
242                         default:
243                             if (!foundReturn
244                                     && !foundInheritDoc
245                                     && !env.types.isSameType(ee.getReturnType(), env.java_lang_Void)) {
246                                 reportMissing("dc.missing.return");
247                             }
248                     }
249                     checkThrowsDocumented(ee.getThrownTypes());
250                 }
251             }
252         }
253 
254         return null;
255     }
256 
reportMissing(String code, Object... args)257     private void reportMissing(String code, Object... args) {
258         env.messages.report(MISSING, Kind.WARNING, env.currPath.getLeaf(), code, args);
259     }
260 
reportReference(String code, Object... args)261     private void reportReference(String code, Object... args) {
262         env.messages.report(REFERENCE, Kind.WARNING, env.currPath.getLeaf(), code, args);
263     }
264 
265     @Override @DefinedBy(Api.COMPILER_TREE)
visitDocComment(DocCommentTree tree, Void ignore)266     public Void visitDocComment(DocCommentTree tree, Void ignore) {
267         super.visitDocComment(tree, ignore);
268         for (TagStackItem tsi: tagStack) {
269             warnIfEmpty(tsi, null);
270             if (tsi.tree.getKind() == DocTree.Kind.START_ELEMENT
271                     && tsi.tag.endKind == HtmlTag.EndKind.REQUIRED) {
272                 StartElementTree t = (StartElementTree) tsi.tree;
273                 env.messages.error(HTML, t, "dc.tag.not.closed", t.getName());
274             }
275         }
276         return null;
277     }
278     // </editor-fold>
279 
280     // <editor-fold defaultstate="collapsed" desc="Text and entities.">
281 
282     @Override @DefinedBy(Api.COMPILER_TREE)
visitText(TextTree tree, Void ignore)283     public Void visitText(TextTree tree, Void ignore) {
284         hasNonWhitespaceText = hasNonWhitespace(tree);
285         if (hasNonWhitespaceText) {
286             checkAllowsText(tree);
287             markEnclosingTag(Flag.HAS_TEXT);
288         }
289         return null;
290     }
291 
292     @Override @DefinedBy(Api.COMPILER_TREE)
visitEntity(EntityTree tree, Void ignore)293     public Void visitEntity(EntityTree tree, Void ignore) {
294         checkAllowsText(tree);
295         markEnclosingTag(Flag.HAS_TEXT);
296         String name = tree.getName().toString();
297         if (name.startsWith("#")) {
298             int v = StringUtils.toLowerCase(name).startsWith("#x")
299                     ? Integer.parseInt(name.substring(2), 16)
300                     : Integer.parseInt(name.substring(1), 10);
301             if (!Entity.isValid(v)) {
302                 env.messages.error(HTML, tree, "dc.entity.invalid", name);
303             }
304         } else if (!Entity.isValid(name)) {
305             env.messages.error(HTML, tree, "dc.entity.invalid", name);
306         }
307         return null;
308     }
309 
checkAllowsText(DocTree tree)310     void checkAllowsText(DocTree tree) {
311         TagStackItem top = tagStack.peek();
312         if (top != null
313                 && top.tree.getKind() == DocTree.Kind.START_ELEMENT
314                 && !top.tag.acceptsText()) {
315             if (top.flags.add(Flag.REPORTED_BAD_INLINE)) {
316                 env.messages.error(HTML, tree, "dc.text.not.allowed",
317                         ((StartElementTree) top.tree).getName());
318             }
319         }
320     }
321 
322     // </editor-fold>
323 
324     // <editor-fold defaultstate="collapsed" desc="HTML elements">
325 
326     @Override @DefinedBy(Api.COMPILER_TREE)
visitStartElement(StartElementTree tree, Void ignore)327     public Void visitStartElement(StartElementTree tree, Void ignore) {
328         final Name treeName = tree.getName();
329         final HtmlTag t = HtmlTag.get(treeName);
330         if (t == null) {
331             env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
332         } else if (t.allowedVersion != HtmlVersion.ALL && t.allowedVersion != env.htmlVersion) {
333             env.messages.error(HTML, tree, "dc.tag.not.supported", treeName);
334         } else {
335             boolean done = false;
336             for (TagStackItem tsi: tagStack) {
337                 if (tsi.tag.accepts(t)) {
338                     while (tagStack.peek() != tsi) {
339                         warnIfEmpty(tagStack.peek(), null);
340                         tagStack.pop();
341                     }
342                     done = true;
343                     break;
344                 } else if (tsi.tag.endKind != HtmlTag.EndKind.OPTIONAL) {
345                     done = true;
346                     break;
347                 }
348             }
349             if (!done && HtmlTag.BODY.accepts(t)) {
350                 while (!tagStack.isEmpty()) {
351                     warnIfEmpty(tagStack.peek(), null);
352                     tagStack.pop();
353                 }
354             }
355 
356             markEnclosingTag(Flag.HAS_ELEMENT);
357             checkStructure(tree, t);
358 
359             // tag specific checks
360             switch (t) {
361                 // check for out of sequence headings, such as <h1>...</h1>  <h3>...</h3>
362                 case H1: case H2: case H3: case H4: case H5: case H6:
363                     checkHeading(tree, t);
364                     break;
365             }
366 
367             if (t.flags.contains(HtmlTag.Flag.NO_NEST)) {
368                 for (TagStackItem i: tagStack) {
369                     if (t == i.tag) {
370                         env.messages.warning(HTML, tree, "dc.tag.nested.not.allowed", treeName);
371                         break;
372                     }
373                 }
374             }
375         }
376 
377         // check for self closing tags, such as <a id="name"/>
378         if (tree.isSelfClosing()) {
379             env.messages.error(HTML, tree, "dc.tag.self.closing", treeName);
380         }
381 
382         try {
383             TagStackItem parent = tagStack.peek();
384             TagStackItem top = new TagStackItem(tree, t);
385             tagStack.push(top);
386 
387             super.visitStartElement(tree, ignore);
388 
389             // handle attributes that may or may not have been found in start element
390             if (t != null) {
391                 switch (t) {
392                     case CAPTION:
393                         if (parent != null && parent.tag == HtmlTag.TABLE)
394                             parent.flags.add(Flag.TABLE_HAS_CAPTION);
395                         break;
396 
397                     case H1: case H2: case H3: case H4: case H5: case H6:
398                         if (parent != null && (parent.tag == HtmlTag.SECTION || parent.tag == HtmlTag.ARTICLE)) {
399                             parent.flags.add(Flag.HAS_HEADING);
400                         }
401                         break;
402 
403                     case IMG:
404                         if (!top.attrs.contains(HtmlTag.Attr.ALT))
405                             env.messages.error(ACCESSIBILITY, tree, "dc.no.alt.attr.for.image");
406                         break;
407                 }
408             }
409 
410             return null;
411         } finally {
412 
413             if (t == null || t.endKind == HtmlTag.EndKind.NONE)
414                 tagStack.pop();
415         }
416     }
417 
checkStructure(StartElementTree tree, HtmlTag t)418     private void checkStructure(StartElementTree tree, HtmlTag t) {
419         Name treeName = tree.getName();
420         TagStackItem top = tagStack.peek();
421         switch (t.blockType) {
422             case BLOCK:
423                 if (top == null || top.tag.accepts(t))
424                     return;
425 
426                 switch (top.tree.getKind()) {
427                     case START_ELEMENT: {
428                         if (top.tag.blockType == HtmlTag.BlockType.INLINE) {
429                             Name name = ((StartElementTree) top.tree).getName();
430                             env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.element",
431                                     treeName, name);
432                             return;
433                         }
434                     }
435                     break;
436 
437                     case LINK:
438                     case LINK_PLAIN: {
439                         String name = top.tree.getKind().tagName;
440                         env.messages.error(HTML, tree, "dc.tag.not.allowed.inline.tag",
441                                 treeName, name);
442                         return;
443                     }
444                 }
445                 break;
446 
447             case INLINE:
448                 if (top == null || top.tag.accepts(t))
449                     return;
450                 break;
451 
452             case LIST_ITEM:
453             case TABLE_ITEM:
454                 if (top != null) {
455                     // reset this flag so subsequent bad inline content gets reported
456                     top.flags.remove(Flag.REPORTED_BAD_INLINE);
457                     if (top.tag.accepts(t))
458                         return;
459                 }
460                 break;
461 
462             case OTHER:
463                 switch (t) {
464                     case SCRIPT:
465                         // <script> may or may not be allowed, depending on --allow-script-in-comments
466                         // but we allow it here, and rely on a separate scanner to detect all uses
467                         // of JavaScript, including <script> tags, and use in attributes, etc.
468                         break;
469 
470                     default:
471                         env.messages.error(HTML, tree, "dc.tag.not.allowed", treeName);
472                 }
473                 return;
474         }
475 
476         env.messages.error(HTML, tree, "dc.tag.not.allowed.here", treeName);
477     }
478 
checkHeading(StartElementTree tree, HtmlTag tag)479     private void checkHeading(StartElementTree tree, HtmlTag tag) {
480         // verify the new tag
481         if (getHeadingRank(tag) > getHeadingRank(currHeadingTag) + 1) {
482             if (currHeadingTag == null) {
483                 env.messages.error(ACCESSIBILITY, tree, "dc.tag.heading.sequence.1",
484                         tag, implicitHeadingRank);
485             } else {
486                 env.messages.error(ACCESSIBILITY, tree, "dc.tag.heading.sequence.2",
487                     tag, currHeadingTag);
488             }
489         } else if (getHeadingRank(tag) <= implicitHeadingRank) {
490             env.messages.error(ACCESSIBILITY, tree, "dc.tag.heading.sequence.3",
491                     tag, implicitHeadingRank);
492         }
493 
494         currHeadingTag = tag;
495     }
496 
getHeadingRank(HtmlTag tag)497     private int getHeadingRank(HtmlTag tag) {
498         if (tag == null)
499             return implicitHeadingRank;
500         switch (tag) {
501             case H1: return 1;
502             case H2: return 2;
503             case H3: return 3;
504             case H4: return 4;
505             case H5: return 5;
506             case H6: return 6;
507             default: throw new IllegalArgumentException();
508         }
509     }
510 
511     @Override @DefinedBy(Api.COMPILER_TREE)
visitEndElement(EndElementTree tree, Void ignore)512     public Void visitEndElement(EndElementTree tree, Void ignore) {
513         final Name treeName = tree.getName();
514         final HtmlTag t = HtmlTag.get(treeName);
515         if (t == null) {
516             env.messages.error(HTML, tree, "dc.tag.unknown", treeName);
517         } else if (t.endKind == HtmlTag.EndKind.NONE) {
518             env.messages.error(HTML, tree, "dc.tag.end.not.permitted", treeName);
519         } else {
520             boolean done = false;
521             while (!tagStack.isEmpty()) {
522                 TagStackItem top = tagStack.peek();
523                 if (t == top.tag) {
524                     switch (t) {
525                         case TABLE:
526                             if (!top.attrs.contains(HtmlTag.Attr.SUMMARY)
527                                     && !top.flags.contains(Flag.TABLE_HAS_CAPTION)) {
528                                 env.messages.error(ACCESSIBILITY, tree,
529                                         "dc.no.summary.or.caption.for.table");
530                             }
531                             break;
532 
533                         case SECTION:
534                         case ARTICLE:
535                             if (env.htmlVersion == HtmlVersion.HTML5 && !top.flags.contains(Flag.HAS_HEADING)) {
536                                 env.messages.error(HTML, tree, "dc.tag.requires.heading", treeName);
537                             }
538                             break;
539                     }
540                     warnIfEmpty(top, tree);
541                     tagStack.pop();
542                     done = true;
543                     break;
544                 } else if (top.tag == null || top.tag.endKind != HtmlTag.EndKind.REQUIRED) {
545                     tagStack.pop();
546                 } else {
547                     boolean found = false;
548                     for (TagStackItem si: tagStack) {
549                         if (si.tag == t) {
550                             found = true;
551                             break;
552                         }
553                     }
554                     if (found && top.tree.getKind() == DocTree.Kind.START_ELEMENT) {
555                         env.messages.error(HTML, top.tree, "dc.tag.start.unmatched",
556                                 ((StartElementTree) top.tree).getName());
557                         tagStack.pop();
558                     } else {
559                         env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName);
560                         done = true;
561                         break;
562                     }
563                 }
564             }
565 
566             if (!done && tagStack.isEmpty()) {
567                 env.messages.error(HTML, tree, "dc.tag.end.unexpected", treeName);
568             }
569         }
570 
571         return super.visitEndElement(tree, ignore);
572     }
573 
warnIfEmpty(TagStackItem tsi, DocTree endTree)574     void warnIfEmpty(TagStackItem tsi, DocTree endTree) {
575         if (tsi.tag != null && tsi.tree instanceof StartElementTree) {
576             if (tsi.tag.flags.contains(HtmlTag.Flag.EXPECT_CONTENT)
577                     && !tsi.flags.contains(Flag.HAS_TEXT)
578                     && !tsi.flags.contains(Flag.HAS_ELEMENT)
579                     && !tsi.flags.contains(Flag.HAS_INLINE_TAG)) {
580                 DocTree tree = (endTree != null) ? endTree : tsi.tree;
581                 Name treeName = ((StartElementTree) tsi.tree).getName();
582                 env.messages.warning(HTML, tree, "dc.tag.empty", treeName);
583             }
584         }
585     }
586 
587     // </editor-fold>
588 
589     // <editor-fold defaultstate="collapsed" desc="HTML attributes">
590 
591     @Override @DefinedBy(Api.COMPILER_TREE) @SuppressWarnings("fallthrough")
visitAttribute(AttributeTree tree, Void ignore)592     public Void visitAttribute(AttributeTree tree, Void ignore) {
593         HtmlTag currTag = tagStack.peek().tag;
594         if (currTag != null) {
595             Name name = tree.getName();
596             HtmlTag.Attr attr = currTag.getAttr(name);
597             if (attr != null) {
598                 if (env.htmlVersion == HtmlVersion.HTML4 && attr.name().contains("-")) {
599                     env.messages.error(HTML, tree, "dc.attr.not.supported.html4", name);
600                 }
601                 boolean first = tagStack.peek().attrs.add(attr);
602                 if (!first)
603                     env.messages.error(HTML, tree, "dc.attr.repeated", name);
604             }
605             // for now, doclint allows all attribute names beginning with "on" as event handler names,
606             // without checking the validity or applicability of the name
607             if (!name.toString().startsWith("on")) {
608                 AttrKind k = currTag.getAttrKind(name);
609                 switch (env.htmlVersion) {
610                     case HTML4:
611                         validateHtml4Attrs(tree, name, k);
612                         break;
613 
614                     case HTML5:
615                         validateHtml5Attrs(tree, name, k);
616                         break;
617                 }
618             }
619 
620             if (attr != null) {
621                 switch (attr) {
622                     case NAME:
623                         if (currTag != HtmlTag.A) {
624                             break;
625                         }
626                         // fallthrough
627                     case ID:
628                         String value = getAttrValue(tree);
629                         if (value == null) {
630                             env.messages.error(HTML, tree, "dc.anchor.value.missing");
631                         } else {
632                             if (!validName.matcher(value).matches()) {
633                                 env.messages.error(HTML, tree, "dc.invalid.anchor", value);
634                             }
635                             if (!checkAnchor(value)) {
636                                 env.messages.error(HTML, tree, "dc.anchor.already.defined", value);
637                             }
638                         }
639                         break;
640 
641                     case HREF:
642                         if (currTag == HtmlTag.A) {
643                             String v = getAttrValue(tree);
644                             if (v == null || v.isEmpty()) {
645                                 env.messages.error(HTML, tree, "dc.attr.lacks.value");
646                             } else {
647                                 Matcher m = docRoot.matcher(v);
648                                 if (m.matches()) {
649                                     String rest = m.group(2);
650                                     if (!rest.isEmpty())
651                                         checkURI(tree, rest);
652                                 } else {
653                                     checkURI(tree, v);
654                                 }
655                             }
656                         }
657                         break;
658 
659                     case VALUE:
660                         if (currTag == HtmlTag.LI) {
661                             String v = getAttrValue(tree);
662                             if (v == null || v.isEmpty()) {
663                                 env.messages.error(HTML, tree, "dc.attr.lacks.value");
664                             } else if (!validNumber.matcher(v).matches()) {
665                                 env.messages.error(HTML, tree, "dc.attr.not.number");
666                             }
667                         }
668                         break;
669 
670                     case BORDER:
671                         if (currTag == HtmlTag.TABLE) {
672                             String v = getAttrValue(tree);
673                             try {
674                                 if (env.htmlVersion == HtmlVersion.HTML5
675                                         && (v == null || (!v.isEmpty() && Integer.parseInt(v) != 1))) {
676                                     env.messages.error(HTML, tree, "dc.attr.table.border.html5", attr);
677                                 }
678                             } catch (NumberFormatException ex) {
679                                 env.messages.error(HTML, tree, "dc.attr.table.border.html5", attr);
680                             }
681                         }
682                         break;
683                 }
684             }
685         }
686 
687         // TODO: basic check on value
688 
689         return null;
690     }
691 
validateHtml4Attrs(AttributeTree tree, Name name, AttrKind k)692     private void validateHtml4Attrs(AttributeTree tree, Name name, AttrKind k) {
693         switch (k) {
694             case ALL:
695             case HTML4:
696                 break;
697 
698             case INVALID:
699                 env.messages.error(HTML, tree, "dc.attr.unknown", name);
700                 break;
701 
702             case OBSOLETE:
703                 env.messages.warning(HTML, tree, "dc.attr.obsolete", name);
704                 break;
705 
706             case USE_CSS:
707                 env.messages.warning(HTML, tree, "dc.attr.obsolete.use.css", name);
708                 break;
709 
710             case HTML5:
711                 env.messages.error(HTML, tree, "dc.attr.not.supported.html4", name);
712                 break;
713         }
714     }
715 
validateHtml5Attrs(AttributeTree tree, Name name, AttrKind k)716     private void validateHtml5Attrs(AttributeTree tree, Name name, AttrKind k) {
717         switch (k) {
718             case ALL:
719             case HTML5:
720                 break;
721 
722             case INVALID:
723             case OBSOLETE:
724             case USE_CSS:
725             case HTML4:
726                 env.messages.error(HTML, tree, "dc.attr.not.supported.html5", name);
727                 break;
728         }
729     }
730 
checkAnchor(String name)731     private boolean checkAnchor(String name) {
732         Element e = getEnclosingPackageOrClass(env.currElement);
733         if (e == null)
734             return true;
735         Set<String> set = foundAnchors.get(e);
736         if (set == null)
737             foundAnchors.put(e, set = new HashSet<>());
738         return set.add(name);
739     }
740 
getEnclosingPackageOrClass(Element e)741     private Element getEnclosingPackageOrClass(Element e) {
742         while (e != null) {
743             switch (e.getKind()) {
744                 case CLASS:
745                 case ENUM:
746                 case INTERFACE:
747                 case PACKAGE:
748                     return e;
749                 default:
750                     e = e.getEnclosingElement();
751             }
752         }
753         return e;
754     }
755 
756     // http://www.w3.org/TR/html401/types.html#type-name
757     private static final Pattern validName = Pattern.compile("[A-Za-z][A-Za-z0-9-_:.]*");
758 
759     private static final Pattern validNumber = Pattern.compile("-?[0-9]+");
760 
761     // pattern to remove leading {@docRoot}/?
762     private static final Pattern docRoot = Pattern.compile("(?i)(\\{@docRoot *\\}/?)?(.*)");
763 
getAttrValue(AttributeTree tree)764     private String getAttrValue(AttributeTree tree) {
765         if (tree.getValue() == null)
766             return null;
767 
768         StringWriter sw = new StringWriter();
769         try {
770             new DocPretty(sw).print(tree.getValue());
771         } catch (IOException e) {
772             // cannot happen
773         }
774         // ignore potential use of entities for now
775         return sw.toString();
776     }
777 
checkURI(AttributeTree tree, String uri)778     private void checkURI(AttributeTree tree, String uri) {
779         // allow URIs beginning with javascript:, which would otherwise be rejected by the URI API.
780         if (uri.startsWith("javascript:"))
781             return;
782         try {
783             URI u = new URI(uri);
784         } catch (URISyntaxException e) {
785             env.messages.error(HTML, tree, "dc.invalid.uri", uri);
786         }
787     }
788     // </editor-fold>
789 
790     // <editor-fold defaultstate="collapsed" desc="javadoc tags">
791 
792     @Override @DefinedBy(Api.COMPILER_TREE)
visitAuthor(AuthorTree tree, Void ignore)793     public Void visitAuthor(AuthorTree tree, Void ignore) {
794         warnIfEmpty(tree, tree.getName());
795         return super.visitAuthor(tree, ignore);
796     }
797 
798     @Override @DefinedBy(Api.COMPILER_TREE)
visitDocRoot(DocRootTree tree, Void ignore)799     public Void visitDocRoot(DocRootTree tree, Void ignore) {
800         markEnclosingTag(Flag.HAS_INLINE_TAG);
801         return super.visitDocRoot(tree, ignore);
802     }
803 
804     @Override @DefinedBy(Api.COMPILER_TREE)
visitIndex(IndexTree tree, Void ignore)805     public Void visitIndex(IndexTree tree, Void ignore) {
806         for (TagStackItem tsi : tagStack) {
807             if (tsi.tag == HtmlTag.A) {
808                 env.messages.warning(HTML, tree, "dc.tag.a.within.a",
809                         "{@" + tree.getTagName() + "}");
810                 break;
811             }
812         }
813         return super.visitIndex(tree, ignore);
814     }
815 
816     @Override @DefinedBy(Api.COMPILER_TREE)
visitInheritDoc(InheritDocTree tree, Void ignore)817     public Void visitInheritDoc(InheritDocTree tree, Void ignore) {
818         markEnclosingTag(Flag.HAS_INLINE_TAG);
819         // TODO: verify on overridden method
820         foundInheritDoc = true;
821         return super.visitInheritDoc(tree, ignore);
822     }
823 
824     @Override @DefinedBy(Api.COMPILER_TREE)
visitLink(LinkTree tree, Void ignore)825     public Void visitLink(LinkTree tree, Void ignore) {
826         markEnclosingTag(Flag.HAS_INLINE_TAG);
827         // simulate inline context on tag stack
828         HtmlTag t = (tree.getKind() == DocTree.Kind.LINK)
829                 ? HtmlTag.CODE : HtmlTag.SPAN;
830         tagStack.push(new TagStackItem(tree, t));
831         try {
832             return super.visitLink(tree, ignore);
833         } finally {
834             tagStack.pop();
835         }
836     }
837 
838     @Override @DefinedBy(Api.COMPILER_TREE)
visitLiteral(LiteralTree tree, Void ignore)839     public Void visitLiteral(LiteralTree tree, Void ignore) {
840         markEnclosingTag(Flag.HAS_INLINE_TAG);
841         if (tree.getKind() == DocTree.Kind.CODE) {
842             for (TagStackItem tsi: tagStack) {
843                 if (tsi.tag == HtmlTag.CODE) {
844                     env.messages.warning(HTML, tree, "dc.tag.code.within.code");
845                     break;
846                 }
847             }
848         }
849         return super.visitLiteral(tree, ignore);
850     }
851 
852     @Override @DefinedBy(Api.COMPILER_TREE)
853     @SuppressWarnings("fallthrough")
visitParam(ParamTree tree, Void ignore)854     public Void visitParam(ParamTree tree, Void ignore) {
855         boolean typaram = tree.isTypeParameter();
856         IdentifierTree nameTree = tree.getName();
857         Element paramElement = nameTree != null ? env.trees.getElement(new DocTreePath(getCurrentPath(), nameTree)) : null;
858 
859         if (paramElement == null) {
860             switch (env.currElement.getKind()) {
861                 case CLASS: case INTERFACE: {
862                     if (!typaram) {
863                         env.messages.error(REFERENCE, tree, "dc.invalid.param");
864                         break;
865                     }
866                 }
867                 case METHOD: case CONSTRUCTOR: {
868                     env.messages.error(REFERENCE, nameTree, "dc.param.name.not.found");
869                     break;
870                 }
871 
872                 default:
873                     env.messages.error(REFERENCE, tree, "dc.invalid.param");
874                     break;
875             }
876         } else {
877             boolean unique = foundParams.add(paramElement);
878 
879             if (!unique) {
880                 env.messages.warning(REFERENCE, tree, "dc.exists.param", nameTree);
881             }
882         }
883 
884         warnIfEmpty(tree, tree.getDescription());
885         return super.visitParam(tree, ignore);
886     }
887 
checkParamsDocumented(List<? extends Element> list)888     private void checkParamsDocumented(List<? extends Element> list) {
889         if (foundInheritDoc)
890             return;
891 
892         for (Element e: list) {
893             if (!foundParams.contains(e)) {
894                 CharSequence paramName = (e.getKind() == ElementKind.TYPE_PARAMETER)
895                         ? "<" + e.getSimpleName() + ">"
896                         : e.getSimpleName();
897                 reportMissing("dc.missing.param", paramName);
898             }
899         }
900     }
901 
902     @Override @DefinedBy(Api.COMPILER_TREE)
visitProvides(ProvidesTree tree, Void ignore)903     public Void visitProvides(ProvidesTree tree, Void ignore) {
904         Element e = env.trees.getElement(env.currPath);
905         if (e.getKind() != ElementKind.MODULE) {
906             env.messages.error(REFERENCE, tree, "dc.invalid.provides");
907         }
908         ReferenceTree serviceType = tree.getServiceType();
909         Element se = env.trees.getElement(new DocTreePath(getCurrentPath(), serviceType));
910         if (se == null) {
911             env.messages.error(REFERENCE, tree, "dc.service.not.found");
912         }
913         return super.visitProvides(tree, ignore);
914     }
915 
916     @Override @DefinedBy(Api.COMPILER_TREE)
visitReference(ReferenceTree tree, Void ignore)917     public Void visitReference(ReferenceTree tree, Void ignore) {
918         String sig = tree.getSignature();
919         if (sig.contains("<") || sig.contains(">")) {
920             env.messages.error(REFERENCE, tree, "dc.type.arg.not.allowed");
921         } else {
922             Element e = env.trees.getElement(getCurrentPath());
923             if (e == null)
924                 env.messages.error(REFERENCE, tree, "dc.ref.not.found");
925         }
926         return super.visitReference(tree, ignore);
927     }
928 
929     @Override @DefinedBy(Api.COMPILER_TREE)
visitReturn(ReturnTree tree, Void ignore)930     public Void visitReturn(ReturnTree tree, Void ignore) {
931         if (foundReturn) {
932             env.messages.warning(REFERENCE, tree, "dc.exists.return");
933         }
934 
935         Element e = env.trees.getElement(env.currPath);
936         if (e.getKind() != ElementKind.METHOD
937                 || ((ExecutableElement) e).getReturnType().getKind() == TypeKind.VOID)
938             env.messages.error(REFERENCE, tree, "dc.invalid.return");
939         foundReturn = true;
940         warnIfEmpty(tree, tree.getDescription());
941         return super.visitReturn(tree, ignore);
942     }
943 
944     @Override @DefinedBy(Api.COMPILER_TREE)
visitSerialData(SerialDataTree tree, Void ignore)945     public Void visitSerialData(SerialDataTree tree, Void ignore) {
946         warnIfEmpty(tree, tree.getDescription());
947         return super.visitSerialData(tree, ignore);
948     }
949 
950     @Override @DefinedBy(Api.COMPILER_TREE)
visitSerialField(SerialFieldTree tree, Void ignore)951     public Void visitSerialField(SerialFieldTree tree, Void ignore) {
952         warnIfEmpty(tree, tree.getDescription());
953         return super.visitSerialField(tree, ignore);
954     }
955 
956     @Override @DefinedBy(Api.COMPILER_TREE)
visitSince(SinceTree tree, Void ignore)957     public Void visitSince(SinceTree tree, Void ignore) {
958         warnIfEmpty(tree, tree.getBody());
959         return super.visitSince(tree, ignore);
960     }
961 
962     @Override @DefinedBy(Api.COMPILER_TREE)
visitSummary(SummaryTree node, Void aVoid)963     public Void visitSummary(SummaryTree node, Void aVoid) {
964         int idx = env.currDocComment.getFullBody().indexOf(node);
965         // Warn if the node is preceded by non-whitespace characters,
966         // or other non-text nodes.
967         if ((idx == 1 && hasNonWhitespaceText) || idx > 1) {
968             env.messages.warning(SYNTAX, node, "dc.invalid.summary", node.getTagName());
969         }
970         return super.visitSummary(node, aVoid);
971     }
972 
973     @Override @DefinedBy(Api.COMPILER_TREE)
visitSystemProperty(SystemPropertyTree tree, Void ignore)974     public Void visitSystemProperty(SystemPropertyTree tree, Void ignore) {
975         for (TagStackItem tsi : tagStack) {
976             if (tsi.tag == HtmlTag.A) {
977                 env.messages.warning(HTML, tree, "dc.tag.a.within.a",
978                         "{@" + tree.getTagName() + "}");
979                 break;
980             }
981         }
982         return super.visitSystemProperty(tree, ignore);
983     }
984 
985     @Override @DefinedBy(Api.COMPILER_TREE)
visitThrows(ThrowsTree tree, Void ignore)986     public Void visitThrows(ThrowsTree tree, Void ignore) {
987         ReferenceTree exName = tree.getExceptionName();
988         Element ex = env.trees.getElement(new DocTreePath(getCurrentPath(), exName));
989         if (ex == null) {
990             env.messages.error(REFERENCE, tree, "dc.ref.not.found");
991         } else if (isThrowable(ex.asType())) {
992             switch (env.currElement.getKind()) {
993                 case CONSTRUCTOR:
994                 case METHOD:
995                     if (isCheckedException(ex.asType())) {
996                         ExecutableElement ee = (ExecutableElement) env.currElement;
997                         checkThrowsDeclared(exName, ex.asType(), ee.getThrownTypes());
998                     }
999                     break;
1000                 default:
1001                     env.messages.error(REFERENCE, tree, "dc.invalid.throws");
1002             }
1003         } else {
1004             env.messages.error(REFERENCE, tree, "dc.invalid.throws");
1005         }
1006         warnIfEmpty(tree, tree.getDescription());
1007         return scan(tree.getDescription(), ignore);
1008     }
1009 
isThrowable(TypeMirror tm)1010     private boolean isThrowable(TypeMirror tm) {
1011         switch (tm.getKind()) {
1012             case DECLARED:
1013             case TYPEVAR:
1014                 return env.types.isAssignable(tm, env.java_lang_Throwable);
1015         }
1016         return false;
1017     }
1018 
checkThrowsDeclared(ReferenceTree tree, TypeMirror t, List<? extends TypeMirror> list)1019     private void checkThrowsDeclared(ReferenceTree tree, TypeMirror t, List<? extends TypeMirror> list) {
1020         boolean found = false;
1021         for (TypeMirror tl : list) {
1022             if (env.types.isAssignable(t, tl)) {
1023                 foundThrows.add(tl);
1024                 found = true;
1025             }
1026         }
1027         if (!found)
1028             env.messages.error(REFERENCE, tree, "dc.exception.not.thrown", t);
1029     }
1030 
checkThrowsDocumented(List<? extends TypeMirror> list)1031     private void checkThrowsDocumented(List<? extends TypeMirror> list) {
1032         if (foundInheritDoc)
1033             return;
1034 
1035         for (TypeMirror tl: list) {
1036             if (isCheckedException(tl) && !foundThrows.contains(tl))
1037                 reportMissing("dc.missing.throws", tl);
1038         }
1039     }
1040 
1041     @Override @DefinedBy(Api.COMPILER_TREE)
visitUnknownBlockTag(UnknownBlockTagTree tree, Void ignore)1042     public Void visitUnknownBlockTag(UnknownBlockTagTree tree, Void ignore) {
1043         checkUnknownTag(tree, tree.getTagName());
1044         return super.visitUnknownBlockTag(tree, ignore);
1045     }
1046 
1047     @Override @DefinedBy(Api.COMPILER_TREE)
visitUnknownInlineTag(UnknownInlineTagTree tree, Void ignore)1048     public Void visitUnknownInlineTag(UnknownInlineTagTree tree, Void ignore) {
1049         checkUnknownTag(tree, tree.getTagName());
1050         return super.visitUnknownInlineTag(tree, ignore);
1051     }
1052 
checkUnknownTag(DocTree tree, String tagName)1053     private void checkUnknownTag(DocTree tree, String tagName) {
1054         if (env.customTags != null && !env.customTags.contains(tagName))
1055             env.messages.error(SYNTAX, tree, "dc.tag.unknown", tagName);
1056     }
1057 
1058     @Override @DefinedBy(Api.COMPILER_TREE)
visitUses(UsesTree tree, Void ignore)1059     public Void visitUses(UsesTree tree, Void ignore) {
1060         Element e = env.trees.getElement(env.currPath);
1061         if (e.getKind() != ElementKind.MODULE) {
1062             env.messages.error(REFERENCE, tree, "dc.invalid.uses");
1063         }
1064         ReferenceTree serviceType = tree.getServiceType();
1065         Element se = env.trees.getElement(new DocTreePath(getCurrentPath(), serviceType));
1066         if (se == null) {
1067             env.messages.error(REFERENCE, tree, "dc.service.not.found");
1068         }
1069         return super.visitUses(tree, ignore);
1070     }
1071 
1072     @Override @DefinedBy(Api.COMPILER_TREE)
visitValue(ValueTree tree, Void ignore)1073     public Void visitValue(ValueTree tree, Void ignore) {
1074         ReferenceTree ref = tree.getReference();
1075         if (ref == null || ref.getSignature().isEmpty()) {
1076             if (!isConstant(env.currElement))
1077                 env.messages.error(REFERENCE, tree, "dc.value.not.allowed.here");
1078         } else {
1079             Element e = env.trees.getElement(new DocTreePath(getCurrentPath(), ref));
1080             if (!isConstant(e))
1081                 env.messages.error(REFERENCE, tree, "dc.value.not.a.constant");
1082         }
1083 
1084         markEnclosingTag(Flag.HAS_INLINE_TAG);
1085         return super.visitValue(tree, ignore);
1086     }
1087 
isConstant(Element e)1088     private boolean isConstant(Element e) {
1089         if (e == null)
1090             return false;
1091 
1092         switch (e.getKind()) {
1093             case FIELD:
1094                 Object value = ((VariableElement) e).getConstantValue();
1095                 return (value != null); // can't distinguish "not a constant" from "constant is null"
1096             default:
1097                 return false;
1098         }
1099     }
1100 
1101     @Override @DefinedBy(Api.COMPILER_TREE)
visitVersion(VersionTree tree, Void ignore)1102     public Void visitVersion(VersionTree tree, Void ignore) {
1103         warnIfEmpty(tree, tree.getBody());
1104         return super.visitVersion(tree, ignore);
1105     }
1106 
1107     @Override @DefinedBy(Api.COMPILER_TREE)
visitErroneous(ErroneousTree tree, Void ignore)1108     public Void visitErroneous(ErroneousTree tree, Void ignore) {
1109         env.messages.error(SYNTAX, tree, null, tree.getDiagnostic().getMessage(null));
1110         return null;
1111     }
1112     // </editor-fold>
1113 
1114     // <editor-fold defaultstate="collapsed" desc="Utility methods">
1115 
isCheckedException(TypeMirror t)1116     private boolean isCheckedException(TypeMirror t) {
1117         return !(env.types.isAssignable(t, env.java_lang_Error)
1118                 || env.types.isAssignable(t, env.java_lang_RuntimeException));
1119     }
1120 
isSynthetic()1121     private boolean isSynthetic() {
1122         switch (env.currElement.getKind()) {
1123             case CONSTRUCTOR:
1124                 // A synthetic default constructor has the same pos as the
1125                 // enclosing class
1126                 TreePath p = env.currPath;
1127                 return env.getPos(p) == env.getPos(p.getParentPath());
1128         }
1129         return false;
1130     }
1131 
markEnclosingTag(Flag flag)1132     void markEnclosingTag(Flag flag) {
1133         TagStackItem top = tagStack.peek();
1134         if (top != null)
1135             top.flags.add(flag);
1136     }
1137 
toString(TreePath p)1138     String toString(TreePath p) {
1139         StringBuilder sb = new StringBuilder("TreePath[");
1140         toString(p, sb);
1141         sb.append("]");
1142         return sb.toString();
1143     }
1144 
toString(TreePath p, StringBuilder sb)1145     void toString(TreePath p, StringBuilder sb) {
1146         TreePath parent = p.getParentPath();
1147         if (parent != null) {
1148             toString(parent, sb);
1149             sb.append(",");
1150         }
1151        sb.append(p.getLeaf().getKind()).append(":").append(env.getPos(p)).append(":S").append(env.getStartPos(p));
1152     }
1153 
warnIfEmpty(DocTree tree, List<? extends DocTree> list)1154     void warnIfEmpty(DocTree tree, List<? extends DocTree> list) {
1155         for (DocTree d: list) {
1156             switch (d.getKind()) {
1157                 case TEXT:
1158                     if (hasNonWhitespace((TextTree) d))
1159                         return;
1160                     break;
1161                 default:
1162                     return;
1163             }
1164         }
1165         env.messages.warning(SYNTAX, tree, "dc.empty", tree.getKind().tagName);
1166     }
1167 
hasNonWhitespace(TextTree tree)1168     boolean hasNonWhitespace(TextTree tree) {
1169         String s = tree.getBody();
1170         for (int i = 0; i < s.length(); i++) {
1171             Character c = s.charAt(i);
1172             if (!Character.isWhitespace(s.charAt(i)))
1173                 return true;
1174         }
1175         return false;
1176     }
1177 
1178     // </editor-fold>
1179 
1180 }
1181