Skip to content

Commit 112b6d2

Browse files
Java: PrintAst: Handle multiple javadocs in one element correctly
1 parent e38b583 commit 112b6d2

File tree

1 file changed

+63
-12
lines changed

1 file changed

+63
-12
lines changed

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

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ private newtype TPrintAstNode =
102102
TBaseTypesNode(ClassOrInterface ty) { shouldPrint(ty, _) } or
103103
TGenericTypeNode(GenericType ty) { shouldPrint(ty, _) } or
104104
TGenericCallableNode(GenericCallable c) { shouldPrint(c, _) } or
105+
TDocumentableNode(Documentable d) { shouldPrint(d, _) and exists(d.getJavadoc()) } or
105106
TJavadocNode(Javadoc jd) { exists(Documentable d | d.getJavadoc() = jd | shouldPrint(d, _)) } or
106107
TJavadocElementNode(JavadocElement jd) { exists(Documentable d | d.getJavadoc() = jd.getParent*() | shouldPrint(d, _)) }
107108

@@ -259,7 +260,7 @@ final class CallableNode extends ElementNode {
259260

260261
override PrintAstNode getChild(int childIndex) {
261262
childIndex = 0 and
262-
result.(JavadocNode).getJavadoc().getCommentedElement() = callable
263+
result.(DocumentableNode).getDocumentable() = callable
263264
or
264265
childIndex = 1 and
265266
result.(AnnotationsNode).getAnnotated() = callable
@@ -321,7 +322,7 @@ final class ClassInterfaceNode extends ElementNode {
321322

322323
override PrintAstNode getChild(int childIndex) {
323324
childIndex = -4 and
324-
result.(JavadocNode).getJavadoc().getCommentedElement() = ty
325+
result.(DocumentableNode).getDocumentable() = ty
325326
or
326327
childIndex = -3 and
327328
result.(AnnotationsNode).getAnnotated() = ty
@@ -351,6 +352,9 @@ final class FieldDeclNode extends ElementNode {
351352
FieldDeclNode() { decl = element }
352353

353354
override PrintAstNode getChild(int childIndex) {
355+
childIndex = -3 and
356+
result.(DocumentableNode).getDocumentable() = decl.getAField()
357+
or
354358
childIndex = -2 and
355359
result.(AnnotationsNode).getAnnotated() = decl.getAField()
356360
or
@@ -416,7 +420,7 @@ final class AnnotationsNode extends PrintAstNode, TAnnotationsNode {
416420

417421
override string toString() { result = "(Annotations)" }
418422

419-
override Location getLocation() { result = ann.getLocation() }
423+
override Location getLocation() { none() }
420424

421425
override ElementNode getChild(int childIndex) {
422426
result.getElement() =
@@ -444,7 +448,7 @@ final class ParametersNode extends PrintAstNode, TParametersNode {
444448

445449
override string toString() { result = "(Parameters)" }
446450

447-
override Location getLocation() { result = c.getLocation() }
451+
override Location getLocation() { none() }
448452

449453
override ElementNode getChild(int childIndex) { result.getElement() = c.getParameter(childIndex) }
450454

@@ -465,10 +469,15 @@ final class BaseTypesNode extends PrintAstNode, TBaseTypesNode {
465469

466470
override string toString() { result = "(Base Types)" }
467471

472+
override Location getLocation() { none() }
473+
468474
override ElementNode getChild(int childIndex) {
469475
result.getElement().(TypeAccess).isNthChildOf(ty, -2 - childIndex)
470476
}
471477

478+
/**
479+
* Gets the underlying `Class` or `Interface`.
480+
*/
472481
ClassOrInterface getClassOrInterface() { result = ty }
473482
}
474483

@@ -483,10 +492,15 @@ final class GenericTypeNode extends PrintAstNode, TGenericTypeNode {
483492

484493
override string toString() { result = "(Generic Parameters)" }
485494

495+
override Location getLocation() { none() }
496+
486497
override ElementNode getChild(int childIndex) {
487498
result.getElement().(TypeVariable) = ty.getTypeParameter(childIndex)
488499
}
489500

501+
/**
502+
* Gets the underlying `GenericType`.
503+
*/
490504
GenericType getType() { result = ty }
491505
}
492506

@@ -505,9 +519,40 @@ final class GenericCallableNode extends PrintAstNode, TGenericCallableNode {
505519
result.getElement().(TypeVariable) = c.getTypeParameter(childIndex)
506520
}
507521

522+
/**
523+
* Gets the underlying `GenericCallable`.
524+
*/
508525
GenericCallable getCallable() { result = c }
509526
}
510527

528+
/**
529+
* A node representing the documentation of a `Documentable`.
530+
* Only rendered if there is at least one `Javadoc` attatched to it.
531+
*/
532+
final class DocumentableNode extends PrintAstNode, TDocumentableNode {
533+
Documentable d;
534+
535+
DocumentableNode() { this = TDocumentableNode(d) }
536+
537+
override string toString() { result = "(Javadoc)" }
538+
539+
override Location getLocation() { none() }
540+
541+
override JavadocNode getChild(int childIndex) {
542+
result.getJavadoc() =
543+
rank[childIndex](Javadoc jd, string file, int line, int column |
544+
jd.getCommentedElement() = d and jd.getLocation().hasLocationInfo(file, line, column, _, _)
545+
|
546+
jd order by file, line, column
547+
)
548+
}
549+
550+
/**
551+
* Gets the underlying `Documentable`.
552+
*/
553+
Documentable getDocumentable() { result = d }
554+
}
555+
511556
/**
512557
* A node representing a `Javadoc`.
513558
* Only rendered if it is the javadoc of some `Documentable`.
@@ -517,14 +562,17 @@ final class JavadocNode extends PrintAstNode, TJavadocNode {
517562

518563
JavadocNode() { this = TJavadocNode(jd) }
519564

520-
override JavadocElementNode getChild(int childIndex) {
521-
result.getJavadocElement() = jd.getChild(childIndex)
522-
}
523-
524565
override string toString() { result = getQlClass(jd) + jd.toString() }
525566

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

569+
override JavadocElementNode getChild(int childIndex) {
570+
result.getJavadocElement() = jd.getChild(childIndex)
571+
}
572+
573+
/**
574+
* Gets the `Javadoc` represented by this node.
575+
*/
528576
Javadoc getJavadoc() { result = jd }
529577
}
530578

@@ -536,14 +584,17 @@ final class JavadocElementNode extends PrintAstNode, TJavadocElementNode {
536584

537585
JavadocElementNode() { this = TJavadocElementNode(jd) }
538586

539-
override JavadocElementNode getChild(int childIndex) {
540-
result.getJavadocElement() = jd.(JavadocParent).getChild(childIndex)
541-
}
542-
543587
override string toString() { result = getQlClass(jd) + jd.toString() }
544588

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

591+
override JavadocElementNode getChild(int childIndex) {
592+
result.getJavadocElement() = jd.(JavadocParent).getChild(childIndex)
593+
}
594+
595+
/**
596+
* Gets the `JavadocElement` represented by this node.
597+
*/
547598
JavadocElement getJavadocElement() { result = jd }
548599
}
549600

0 commit comments

Comments
 (0)