Skip to content

Commit 45651cf

Browse files
Java: PrintAst: Add a synthetic node for the initialisers of for statements
1 parent d652b95 commit 45651cf

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ private predicate locationSortKeys(Element ast, string file, int line, int colum
113113
*/
114114
private newtype TPrintAstNode =
115115
TElementNode(Element el) { shouldPrint(el, _) } or
116+
TForInitNode(ForStmt fs) { shouldPrint(fs, _) and exists(fs.getAnInit()) } or
116117
TAnnotationsNode(Annotatable ann) {
117118
shouldPrint(ann, _) and ann.hasAnnotation() and not partOfAnnotation(ann)
118119
} or
@@ -248,7 +249,11 @@ final class ExprStmtNode extends ElementNode {
248249
override PrintAstNode getChild(int childIndex) {
249250
exists(Element el | result.(ElementNode).getElement() = el |
250251
el.(Expr).isNthChildOf(element, childIndex) and
251-
not partOfAnnotation(element)
252+
not partOfAnnotation(element) and
253+
not (
254+
element instanceof ForStmt and
255+
childIndex <= 0
256+
)
252257
or
253258
el.(Stmt).isNthChildOf(element, childIndex)
254259
or
@@ -271,6 +276,9 @@ final class ExprStmtNode extends ElementNode {
271276
childIndex = -2 and
272277
el = element.(LocalVariableDeclExpr).getVariable()
273278
)
279+
or
280+
childIndex = 0 and
281+
result.(ForInitNode).getForStmt() = element
274282
}
275283
}
276284

@@ -432,6 +440,27 @@ final class TypeVariableNode extends ElementNode {
432440
}
433441
}
434442

443+
/**
444+
* A node representing the initializers of a `ForStmt`.
445+
*/
446+
final class ForInitNode extends PrintAstNode, TForInitNode {
447+
ForStmt fs;
448+
449+
ForInitNode() { this = TForInitNode(fs) }
450+
451+
override string toString() { result = "(For Initializers) "}
452+
453+
override ElementNode getChild(int childIndex) {
454+
childIndex >= 0 and
455+
result.getElement().(Expr).isNthChildOf(fs, -childIndex)
456+
}
457+
458+
/**
459+
* Gets the underlying `ForStmt`.
460+
*/
461+
ForStmt getForStmt() {result = fs}
462+
}
463+
435464
/**
436465
* A node representing the annotations of an `Annotatable`.
437466
* Only rendered if there is at least one annotation.

0 commit comments

Comments
 (0)