Skip to content

Commit b73e7d8

Browse files
Java: PrintAST: Support Javadoc
1 parent c3320ee commit b73e7d8

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

java/ql/src/semmle/code/java/Javadoc.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class Javadoc extends JavadocParent, @javadoc {
5353

5454
/** Gets the Java code element that is commented by this piece of Javadoc. */
5555
Documentable getCommentedElement() { result.getJavadoc() = this }
56+
57+
override string getAPrimaryQlClass() { result = "Javadoc" }
5658
}
5759

5860
/** A documentable element that can have an attached Javadoc comment. */
@@ -89,6 +91,8 @@ class JavadocTag extends JavadocElement, JavadocParent, @javadocTag {
8991

9092
/** Gets the text associated with this Javadoc tag. */
9193
override string getText() { result = this.getChild(0).toString() }
94+
95+
override string getAPrimaryQlClass() { result = "Javadoc" }
9296
}
9397

9498
/** A Javadoc `@param` tag. */
@@ -139,4 +143,6 @@ class JavadocText extends JavadocElement, @javadocText {
139143

140144
/** Gets a printable representation of this Javadoc element. */
141145
override string toString() { result = this.getText() }
146+
147+
override string getAPrimaryQlClass() { result = "JavadocText" }
142148
}

java/ql/src/semmle/code/java/PrintAst.qll

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private predicate isNotNeeded(Element el) {
7272
/**
7373
* Retrieves the canonical QL class(es) for entity `el`
7474
*/
75-
private string getQlClass(Element el) {
75+
private string getQlClass(Top el) {
7676
result = "[" + concat(el.getAPrimaryQlClass(), ",") + "] "
7777
// Alternative implementation -- do not delete. It is useful for QL class discovery.
7878
// result = "[" + concat(el.getAQlClass(), ",") + "] "
@@ -101,7 +101,9 @@ private newtype TPrintAstNode =
101101
TParametersNode(Callable c) { shouldPrint(c, _) and not c.hasNoParameters() } or
102102
TBaseTypesNode(ClassOrInterface ty) { shouldPrint(ty, _) } or
103103
TGenericTypeNode(GenericType ty) { shouldPrint(ty, _) } or
104-
TGenericCallableNode(GenericCallable c) { shouldPrint(c, _) }
104+
TGenericCallableNode(GenericCallable c) { shouldPrint(c, _) } or
105+
TJavadocNode(Javadoc jd) { exists(Documentable d | d.getJavadoc() = jd | shouldPrint(d, _)) } or
106+
TJavadocElementNode(JavadocElement jd) { exists(Documentable d | d.getJavadoc() = jd.getParent*() | shouldPrint(d, _)) }
105107

106108
/**
107109
* A node in the output tree.
@@ -256,20 +258,22 @@ final class CallableNode extends ElementNode {
256258
CallableNode() { callable = element }
257259

258260
override PrintAstNode getChild(int childIndex) {
259-
// TODO: javadoc
260261
childIndex = 0 and
261-
result.(AnnotationsNode).getAnnotated() = callable
262+
result.(JavadocNode).getJavadoc().getCommentedElement() = callable
262263
or
263264
childIndex = 1 and
264-
result.(GenericCallableNode).getCallable() = callable
265+
result.(AnnotationsNode).getAnnotated() = callable
265266
or
266267
childIndex = 2 and
267-
result.(ElementNode).getElement().(Expr).isNthChildOf(callable, -1) // return type
268+
result.(GenericCallableNode).getCallable() = callable
268269
or
269270
childIndex = 3 and
270-
result.(ParametersNode).getCallable() = callable
271+
result.(ElementNode).getElement().(Expr).isNthChildOf(callable, -1) // return type
271272
or
272273
childIndex = 4 and
274+
result.(ParametersNode).getCallable() = callable
275+
or
276+
childIndex = 5 and
273277
result.(ElementNode).getElement() = callable.getBody()
274278
}
275279
}
@@ -316,7 +320,9 @@ final class ClassInterfaceNode extends ElementNode {
316320
}
317321

318322
override PrintAstNode getChild(int childIndex) {
319-
// TODO: javadoc
323+
childIndex = -4 and
324+
result.(JavadocNode).getJavadoc().getCommentedElement() = ty
325+
or
320326
childIndex = -3 and
321327
result.(AnnotationsNode).getAnnotated() = ty
322328
or
@@ -371,6 +377,7 @@ final class CompilationUnitNode extends ElementNode {
371377
}
372378

373379
override PrintAstNode getChild(int childIndex) {
380+
childIndex >= 0 and
374381
result.(ElementNode).getElement() =
375382
rank[childIndex](Element e, string file, int line, int column |
376383
e = getADeclaration() and locationSortKeys(e, file, line, column)
@@ -501,6 +508,45 @@ final class GenericCallableNode extends PrintAstNode, TGenericCallableNode {
501508
GenericCallable getCallable() { result = c }
502509
}
503510

511+
/**
512+
* A node representing a `Javadoc`.
513+
* Only rendered if it is the javadoc of some `Documentable`.
514+
*/
515+
final class JavadocNode extends PrintAstNode, TJavadocNode {
516+
Javadoc jd;
517+
518+
JavadocNode() { this = TJavadocNode(jd) }
519+
520+
override JavadocElementNode getChild(int childIndex) {
521+
result.getJavadocElement() = jd.getChild(childIndex)
522+
}
523+
524+
override string toString() { result = getQlClass(jd) + jd.toString() }
525+
526+
override Location getLocation() { result = jd.getLocation() }
527+
528+
Javadoc getJavadoc() { result = jd }
529+
}
530+
531+
/** A node representing a `JavadocElement`.
532+
* Only rendered if it is part of the javadoc of some `Documentable`.
533+
*/
534+
final class JavadocElementNode extends PrintAstNode, TJavadocElementNode {
535+
JavadocElement jd;
536+
537+
JavadocElementNode() { this = TJavadocElementNode(jd) }
538+
539+
override JavadocElementNode getChild(int childIndex) {
540+
result.getJavadocElement() = jd.(JavadocParent).getChild(childIndex)
541+
}
542+
543+
override string toString() { result = getQlClass(jd) + jd.toString() }
544+
545+
override Location getLocation() { result = jd.getLocation() }
546+
547+
JavadocElement getJavadocElement() { result = jd }
548+
}
549+
504550
/** Holds if `node` belongs to the output tree, and its property `key` has the given `value`. */
505551
query predicate nodes(PrintAstNode node, string key, string value) { value = node.getProperty(key) }
506552

0 commit comments

Comments
 (0)