Java/Kotlin: Add 'children' consistency query

This commit is contained in:
Ian Lynagh
2021-11-16 16:57:53 +00:00
parent 8f1f137fa1
commit 755bf4d8b3

View File

@@ -0,0 +1,51 @@
import java
Element nthChildOf(Element e, int i) {
result.(Expr).isNthChildOf(e, i) or
result.(Stmt).isNthChildOf(e, i)
}
predicate duplicateChildren(Element e, int i) {
nthChildOf(e, i) != nthChildOf(e, i)
// Bug?:
and not e instanceof Method
// Bug?:
and not e instanceof Constructor
}
predicate gapInChildren(Element e, int i) {
exists(nthChildOf(e, i))
and not exists(nthChildOf(e, i - 1))
and exists(int j | j < i | exists(nthChildOf(e, j)))
// TODO: Tighten this up:
and not e instanceof Class
// TODO: Tighten this up:
and not e instanceof Interface
// TODO: Tighten this up:
and not e instanceof ClassInstanceExpr
// TODO: Tighten this up:
and not e instanceof TypeAccess
// TODO: Tighten this up:
and not e instanceof TryStmt
// TODO: Tighten this up:
and not e instanceof ForStmt
}
predicate lateFirstChild(Element e, int i) {
i > 0
and exists(nthChildOf(e, i))
and forex(int j | exists(nthChildOf(e, j)) | j >= i)
// TODO: Tighten this up:
and not e instanceof WildcardTypeAccess
// TODO: Tighten this up:
and not e instanceof LocalVariableDeclStmt
// TODO: Tighten this up:
and not e instanceof ForStmt
}
from Element e, int i, string problem
where problem = "duplicate" and duplicateChildren(e, i)
or problem = "gap" and gapInChildren(e, i)
or problem = "late" and lateFirstChild(e, i)
select e, e.getPrimaryQlClasses(), i, problem, nthChildOf(e, i),
concat(int j | exists(nthChildOf(e, j)) | j.toString(), ", ")