Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions java/ql/lib/semmle/code/java/PrintAst.qll
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ private newtype TPrintAstNode =
TGenericTypeNode(GenericType ty) { shouldPrint(ty, _) } or
TGenericCallableNode(GenericCallable c) { shouldPrint(c, _) } or
TDocumentableNode(Documentable d) { shouldPrint(d, _) and exists(d.getJavadoc()) } or
TJavadocNode(Javadoc jd) { exists(Documentable d | d.getJavadoc() = jd | shouldPrint(d, _)) } or
TJavadocElementNode(JavadocElement jd) {
exists(Documentable d | d.getJavadoc() = jd.getParent*() | shouldPrint(d, _))
TJavadocNode(Javadoc jd, Documentable d) { d.getJavadoc() = jd and shouldPrint(d, _) } or
TJavadocElementNode(JavadocElement jd, Documentable d) {
d.getJavadoc() = jd.getParent*() and shouldPrint(d, _)
} or
TImportsNode(CompilationUnit cu) {
shouldPrint(cu, _) and exists(Import i | i.getCompilationUnit() = cu)
Expand Down Expand Up @@ -794,6 +794,7 @@ final class DocumentableNode extends PrintAstNode, TDocumentableNode {
override Location getLocation() { none() }

override JavadocNode getChild(int childIndex) {
result.getDocumentable() = d and
result.getJavadoc() =
rank[childIndex](Javadoc jd, string file, int line, int column |
jd.getCommentedElement() = d and jd.getLocation().hasLocationInfo(file, line, column, _, _)
Expand All @@ -814,21 +815,28 @@ final class DocumentableNode extends PrintAstNode, TDocumentableNode {
*/
final class JavadocNode extends PrintAstNode, TJavadocNode {
Javadoc jd;
Documentable d;

JavadocNode() { this = TJavadocNode(jd) }
JavadocNode() { this = TJavadocNode(jd, d) and not duplicateMetadata(d) }

override string toString() { result = getQlClass(jd) + jd.toString() }

override Location getLocation() { result = jd.getLocation() }

override JavadocElementNode getChild(int childIndex) {
result.getDocumentable() = d and
result.getJavadocElement() = jd.getChild(childIndex)
}

/**
* Gets the `Javadoc` represented by this node.
*/
Javadoc getJavadoc() { result = jd }

/**
* Gets the `Documentable` whose `Javadoc` is represented by this node.
*/
Documentable getDocumentable() { result = d }
}

/**
Expand All @@ -837,21 +845,28 @@ final class JavadocNode extends PrintAstNode, TJavadocNode {
*/
final class JavadocElementNode extends PrintAstNode, TJavadocElementNode {
JavadocElement jd;
Documentable d;

JavadocElementNode() { this = TJavadocElementNode(jd) }
JavadocElementNode() { this = TJavadocElementNode(jd, d) and not duplicateMetadata(d) }

override string toString() { result = getQlClass(jd) + jd.toString() }

override Location getLocation() { result = jd.getLocation() }

override JavadocElementNode getChild(int childIndex) {
result.getDocumentable() = d and
result.getJavadocElement() = jd.(JavadocParent).getChild(childIndex)
}

/**
* Gets the `JavadocElement` represented by this node.
*/
JavadocElement getJavadocElement() { result = jd }

/**
* Gets the `Documentable` whose `JavadocElement` is represented by this node.
*/
Documentable getDocumentable() { result = d }
}

/**
Expand Down
14 changes: 14 additions & 0 deletions java/ql/test/library-tests/printAst/A.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,18 @@ void varDecls(Object[] things) {
return;
}
}

enum E {
/**
* Javadoc for enum constant
*/
A,
B,
C;
}

/**
* Javadoc for fields
*/
int i, j, k;
}
27 changes: 27 additions & 0 deletions java/ql/test/library-tests/printAst/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,30 @@ A.java:
# 55| 1: [LocalVariableDeclExpr] rte
# 55| 1: [BlockStmt] { ... }
# 56| 0: [ReturnStmt] return ...
# 60| 10: [Class] E
# 64| 3: [FieldDeclaration] E A;
#-----| -3: (Javadoc)
# 61| 1: [Javadoc] /** Javadoc for enum constant */
# 62| 0: [JavadocText] Javadoc for enum constant
# 64| -1: [TypeAccess] E
# 64| 0: [ClassInstanceExpr] new E(...)
# 64| -3: [TypeAccess] E
# 65| 4: [FieldDeclaration] E B;
#-----| -3: (Javadoc)
# 61| 1: [Javadoc] /** Javadoc for enum constant */
# 62| 0: [JavadocText] Javadoc for enum constant
# 65| -1: [TypeAccess] E
# 65| 0: [ClassInstanceExpr] new E(...)
# 65| -3: [TypeAccess] E
# 66| 5: [FieldDeclaration] E C;
#-----| -3: (Javadoc)
# 61| 1: [Javadoc] /** Javadoc for enum constant */
# 62| 0: [JavadocText] Javadoc for enum constant
# 66| -1: [TypeAccess] E
# 66| 0: [ClassInstanceExpr] new E(...)
# 66| -3: [TypeAccess] E
# 72| 11: [FieldDeclaration] int i, ...;
#-----| -3: (Javadoc)
# 69| 1: [Javadoc] /** Javadoc for fields */
# 70| 0: [JavadocText] Javadoc for fields
# 72| -1: [TypeAccess] int