Skip to content

Commit 32e8b13

Browse files
authored
Merge pull request github#12501 from tamasvajk/java/javadoc_printast
Java: Fix printAST to handle javadoc belonging to multiple elements
2 parents d953ad6 + c57fcfb commit 32e8b13

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ private newtype TPrintAstNode =
132132
TGenericTypeNode(GenericType ty) { shouldPrint(ty, _) } or
133133
TGenericCallableNode(GenericCallable c) { shouldPrint(c, _) } or
134134
TDocumentableNode(Documentable d) { shouldPrint(d, _) and exists(d.getJavadoc()) } or
135-
TJavadocNode(Javadoc jd) { exists(Documentable d | d.getJavadoc() = jd | shouldPrint(d, _)) } or
136-
TJavadocElementNode(JavadocElement jd) {
137-
exists(Documentable d | d.getJavadoc() = jd.getParent*() | shouldPrint(d, _))
135+
TJavadocNode(Javadoc jd, Documentable d) { d.getJavadoc() = jd and shouldPrint(d, _) } or
136+
TJavadocElementNode(JavadocElement jd, Documentable d) {
137+
d.getJavadoc() = jd.getParent*() and shouldPrint(d, _)
138138
} or
139139
TImportsNode(CompilationUnit cu) {
140140
shouldPrint(cu, _) and exists(Import i | i.getCompilationUnit() = cu)
@@ -794,6 +794,7 @@ final class DocumentableNode extends PrintAstNode, TDocumentableNode {
794794
override Location getLocation() { none() }
795795

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

818-
JavadocNode() { this = TJavadocNode(jd) }
820+
JavadocNode() { this = TJavadocNode(jd, d) and not duplicateMetadata(d) }
819821

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

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

824826
override JavadocElementNode getChild(int childIndex) {
827+
result.getDocumentable() = d and
825828
result.getJavadocElement() = jd.getChild(childIndex)
826829
}
827830

828831
/**
829832
* Gets the `Javadoc` represented by this node.
830833
*/
831834
Javadoc getJavadoc() { result = jd }
835+
836+
/**
837+
* Gets the `Documentable` whose `Javadoc` is represented by this node.
838+
*/
839+
Documentable getDocumentable() { result = d }
832840
}
833841

834842
/**
@@ -837,21 +845,28 @@ final class JavadocNode extends PrintAstNode, TJavadocNode {
837845
*/
838846
final class JavadocElementNode extends PrintAstNode, TJavadocElementNode {
839847
JavadocElement jd;
848+
Documentable d;
840849

841-
JavadocElementNode() { this = TJavadocElementNode(jd) }
850+
JavadocElementNode() { this = TJavadocElementNode(jd, d) and not duplicateMetadata(d) }
842851

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

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

847856
override JavadocElementNode getChild(int childIndex) {
857+
result.getDocumentable() = d and
848858
result.getJavadocElement() = jd.(JavadocParent).getChild(childIndex)
849859
}
850860

851861
/**
852862
* Gets the `JavadocElement` represented by this node.
853863
*/
854864
JavadocElement getJavadocElement() { result = jd }
865+
866+
/**
867+
* Gets the `Documentable` whose `JavadocElement` is represented by this node.
868+
*/
869+
Documentable getDocumentable() { result = d }
855870
}
856871

857872
/**

java/ql/test/library-tests/printAst/A.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,18 @@ void varDecls(Object[] things) {
5656
return;
5757
}
5858
}
59+
60+
enum E {
61+
/**
62+
* Javadoc for enum constant
63+
*/
64+
A,
65+
B,
66+
C;
67+
}
68+
69+
/**
70+
* Javadoc for fields
71+
*/
72+
int i, j, k;
5973
}

java/ql/test/library-tests/printAst/PrintAst.expected

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,30 @@ A.java:
125125
# 55| 1: [LocalVariableDeclExpr] rte
126126
# 55| 1: [BlockStmt] { ... }
127127
# 56| 0: [ReturnStmt] return ...
128+
# 60| 10: [Class] E
129+
# 64| 3: [FieldDeclaration] E A;
130+
#-----| -3: (Javadoc)
131+
# 61| 1: [Javadoc] /** Javadoc for enum constant */
132+
# 62| 0: [JavadocText] Javadoc for enum constant
133+
# 64| -1: [TypeAccess] E
134+
# 64| 0: [ClassInstanceExpr] new E(...)
135+
# 64| -3: [TypeAccess] E
136+
# 65| 4: [FieldDeclaration] E B;
137+
#-----| -3: (Javadoc)
138+
# 61| 1: [Javadoc] /** Javadoc for enum constant */
139+
# 62| 0: [JavadocText] Javadoc for enum constant
140+
# 65| -1: [TypeAccess] E
141+
# 65| 0: [ClassInstanceExpr] new E(...)
142+
# 65| -3: [TypeAccess] E
143+
# 66| 5: [FieldDeclaration] E C;
144+
#-----| -3: (Javadoc)
145+
# 61| 1: [Javadoc] /** Javadoc for enum constant */
146+
# 62| 0: [JavadocText] Javadoc for enum constant
147+
# 66| -1: [TypeAccess] E
148+
# 66| 0: [ClassInstanceExpr] new E(...)
149+
# 66| -3: [TypeAccess] E
150+
# 72| 11: [FieldDeclaration] int i, ...;
151+
#-----| -3: (Javadoc)
152+
# 69| 1: [Javadoc] /** Javadoc for fields */
153+
# 70| 0: [JavadocText] Javadoc for fields
154+
# 72| -1: [TypeAccess] int

0 commit comments

Comments
 (0)