Skip to content

Commit 3e960c1

Browse files
Java: PrintAst: Refactor exceptions to the usual AST of expressions and statements using dispatch
1 parent 1f99607 commit 3e960c1

File tree

1 file changed

+89
-41
lines changed

1 file changed

+89
-41
lines changed

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

Lines changed: 89 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -222,60 +222,108 @@ abstract class ElementNode extends PrintAstNode, TElementNode {
222222
final Element getElement() { result = element }
223223
}
224224

225+
/**
226+
* A node representing an `Expr` or a `Stmt`.
227+
*/
228+
class ExprStmtNode extends ElementNode {
229+
ExprStmtNode() { element instanceof ExprOrStmt }
230+
231+
override PrintAstNode getChild(int childIndex) {
232+
exists(Element el | result.(ElementNode).getElement() = el |
233+
el.(Expr).isNthChildOf(element, childIndex)
234+
or
235+
el.(Stmt).isNthChildOf(element, childIndex)
236+
)
237+
}
238+
}
239+
240+
/**
241+
* Holds if the given expression is part of an annotation.
242+
*/
225243
private predicate partOfAnnotation(Expr e) {
226244
e instanceof Annotation
227245
or
228246
e instanceof ArrayInit and
229247
partOfAnnotation(e.getParent())
230248
}
231249

232-
private Expr getAnAnnotationChild(Expr e) {
233-
partOfAnnotation(e) and
234-
(
235-
result = e.(Annotation).getValue(_)
236-
or
237-
result = e.(ArrayInit).getAnInit()
238-
or
239-
result = e.(ArrayInit).(Annotatable).getAnAnnotation()
240-
)
241-
}
242-
243250
/**
244-
* An node representing an `Expr` or a `Stmt`.
251+
* A node representing an `Expr` that is part of an annotation.
245252
*/
246-
final class ExprStmtNode extends ElementNode {
247-
ExprStmtNode() { element instanceof ExprOrStmt }
253+
final class AnnotationPartNode extends ExprStmtNode {
254+
AnnotationPartNode() { partOfAnnotation(element) }
248255

249-
override PrintAstNode getChild(int childIndex) {
250-
exists(Element el | result.(ElementNode).getElement() = el |
251-
el.(Expr).isNthChildOf(element, childIndex) and
252-
not partOfAnnotation(element) and
253-
not (
254-
element instanceof ForStmt and
255-
childIndex <= 0
256+
override ElementNode getChild(int childIndex) {
257+
result.getElement() =
258+
rank[childIndex](Element ch, string file, int line, int column |
259+
ch = getAnAnnotationChild() and locationSortKeys(ch, file, line, column)
260+
|
261+
ch order by file, line, column
256262
)
263+
}
264+
265+
private Expr getAnAnnotationChild() {
266+
(
267+
result = element.(Annotation).getValue(_)
257268
or
258-
el.(Stmt).isNthChildOf(element, childIndex)
259-
or
260-
childIndex = -4 and
261-
el = element.(ClassInstanceExpr).getAnonymousClass()
262-
or
263-
childIndex = 0 and
264-
el = element.(LocalClassDeclStmt).getLocalClass()
269+
result = element.(ArrayInit).getAnInit()
265270
or
266-
partOfAnnotation(element) and
267-
el =
268-
rank[childIndex](Element ch, string file, int line, int column |
269-
ch = getAnAnnotationChild(element) and locationSortKeys(ch, file, line, column)
270-
|
271-
ch order by file, line, column
272-
)
271+
result = element.(ArrayInit).(Annotatable).getAnAnnotation()
273272
)
273+
}
274+
}
275+
276+
/**
277+
* A node representing a `LocalVariableDeclExpr`.
278+
*/
279+
final class LocalVarDeclExprNode extends ExprStmtNode {
280+
LocalVarDeclExprNode() { element instanceof LocalVariableDeclExpr }
281+
282+
override PrintAstNode getChild(int childIndex) {
283+
result = super.getChild(childIndex)
274284
or
275-
exists(Element el | result.(AnnotationsNode).getAnnotated() = el |
276-
childIndex = -2 and
277-
el = element.(LocalVariableDeclExpr).getVariable()
278-
)
285+
childIndex = -2 and
286+
result.(AnnotationsNode).getAnnotated() = element.(LocalVariableDeclExpr).getVariable()
287+
}
288+
}
289+
290+
/**
291+
* A node representing a `ClassInstanceExpr`.
292+
*/
293+
final class ClassInstanceExprNode extends ExprStmtNode {
294+
ClassInstanceExprNode() { element instanceof ClassInstanceExpr }
295+
296+
override ElementNode getChild(int childIndex) {
297+
result = super.getChild(childIndex)
298+
or
299+
childIndex = -4 and
300+
result.getElement() = element.(ClassInstanceExpr).getAnonymousClass()
301+
}
302+
}
303+
304+
/**
305+
* A node representing a `LocalClassDeclStmt`.
306+
*/
307+
final class LocalClassDeclStmtNode extends ExprStmtNode {
308+
LocalClassDeclStmtNode() { element instanceof LocalClassDeclStmt }
309+
310+
override ElementNode getChild(int childIndex) {
311+
result = super.getChild(childIndex)
312+
or
313+
childIndex = 0 and
314+
result.getElement() = element.(LocalClassDeclStmt).getLocalClass()
315+
}
316+
}
317+
318+
/**
319+
* A node representing a `ForStmt`.
320+
*/
321+
final class ForStmtNode extends ExprStmtNode {
322+
ForStmtNode() { element instanceof ForStmt }
323+
324+
override PrintAstNode getChild(int childIndex) {
325+
childIndex >= 1 and
326+
result = super.getChild(childIndex)
279327
or
280328
childIndex = 0 and
281329
result.(ForInitNode).getForStmt() = element
@@ -448,7 +496,7 @@ final class ForInitNode extends PrintAstNode, TForInitNode {
448496

449497
ForInitNode() { this = TForInitNode(fs) }
450498

451-
override string toString() { result = "(For Initializers) "}
499+
override string toString() { result = "(For Initializers) " }
452500

453501
override ElementNode getChild(int childIndex) {
454502
childIndex >= 0 and
@@ -458,7 +506,7 @@ final class ForInitNode extends PrintAstNode, TForInitNode {
458506
/**
459507
* Gets the underlying `ForStmt`.
460508
*/
461-
ForStmt getForStmt() {result = fs}
509+
ForStmt getForStmt() { result = fs }
462510
}
463511

464512
/**

0 commit comments

Comments
 (0)