Kotlin: Autoformat QL

This commit is contained in:
Ian Lynagh
2022-03-23 18:03:55 +00:00
parent c5e3aefe10
commit 77fec17a36
51 changed files with 316 additions and 243 deletions

View File

@@ -1,8 +1,14 @@
import java import java
from BinaryExpr be, string reason from BinaryExpr be, string reason
where not exists(be.getLeftOperand()) and reason = "No left operand" where
or not exists(be.getRightOperand()) and reason = "No right operand" not exists(be.getLeftOperand()) and reason = "No left operand"
or exists(Expr e, int i | e.isNthChildOf(be, i) and i != 0 and i != 1 and reason = "Unexpected operand " + i.toString()) or
or be.getOp() = " ?? " and reason = "No operator name" not exists(be.getRightOperand()) and reason = "No right operand"
or
exists(Expr e, int i |
e.isNthChildOf(be, i) and i != 0 and i != 1 and reason = "Unexpected operand " + i.toString()
)
or
be.getOp() = " ?? " and reason = "No operator name"
select be, reason select be, reason

View File

@@ -1,6 +1,8 @@
import java import java
from UnaryExpr ue from UnaryExpr ue
where not exists(ue.getExpr()) where
or exists(Expr e, int i | e.isNthChildOf(ue, i) and i != 0) not exists(ue.getExpr())
or
exists(Expr e, int i | e.isNthChildOf(ue, i) and i != 0)
select ue select ue

View File

@@ -3,4 +3,3 @@ import java
from BlockStmt b, Expr e from BlockStmt b, Expr e
where e.getParent() = b where e.getParent() = b
select b, e select b, e

View File

@@ -2,37 +2,63 @@ import java
import semmle.code.java.ControlFlowGraph import semmle.code.java.ControlFlowGraph
predicate shouldBeDeadEnd(ControlFlowNode n) { predicate shouldBeDeadEnd(ControlFlowNode n) {
n instanceof BreakStmt and n.getFile().isKotlinSourceFile() or // TODO n instanceof BreakStmt and n.getFile().isKotlinSourceFile() // TODO
n instanceof ReturnStmt and n.getFile().isKotlinSourceFile() or // TODO or
n instanceof Interface or // TODO n instanceof ReturnStmt and n.getFile().isKotlinSourceFile() // TODO
n instanceof Class or // TODO or
n instanceof Parameter or // TODO n instanceof Interface // TODO
n instanceof Field or // TODO or
n instanceof Annotation or // TODO n instanceof Class // TODO
n instanceof TypeAccess or // TODO or
n instanceof ArrayTypeAccess or // TODO n instanceof Parameter // TODO
n instanceof UnionTypeAccess or // TODO or
n instanceof IntersectionTypeAccess or // TODO n instanceof Field // TODO
n instanceof ArrayAccess or // TODO or
n instanceof AddExpr or // TODO n instanceof Annotation // TODO
n instanceof MinusExpr or // TODO or
n instanceof LocalVariableDecl or // TODO n instanceof TypeAccess // TODO
n instanceof FieldDeclaration or // TODO or
n instanceof ArrayInit or // TODO n instanceof ArrayTypeAccess // TODO
n instanceof VarAccess or // TODO or
n instanceof Literal or // TODO n instanceof UnionTypeAccess // TODO
n instanceof TypeLiteral or // TODO or
n instanceof TypeVariable or // TODO n instanceof IntersectionTypeAccess // TODO
n instanceof WildcardTypeAccess or // TODO or
n instanceof MethodAccess or // TODO n instanceof ArrayAccess // TODO
n instanceof Method or or
n instanceof Constructor or n instanceof AddExpr // TODO
not exists(n.getFile().getRelativePath()) or // TODO or
n instanceof MinusExpr // TODO
or
n instanceof LocalVariableDecl // TODO
or
n instanceof FieldDeclaration // TODO
or
n instanceof ArrayInit // TODO
or
n instanceof VarAccess // TODO
or
n instanceof Literal // TODO
or
n instanceof TypeLiteral // TODO
or
n instanceof TypeVariable // TODO
or
n instanceof WildcardTypeAccess // TODO
or
n instanceof MethodAccess // TODO
or
n instanceof Method
or
n instanceof Constructor
or
not exists(n.getFile().getRelativePath()) // TODO
or
n = any(ConstCase c).getValue(_) // TODO n = any(ConstCase c).getValue(_) // TODO
} }
from ControlFlowNode n, string s from ControlFlowNode n, string s
where // TODO: exists(n.getASuccessor()) and shouldBeDeadEnd(n) and s = "expected dead end" where
not exists(n.getASuccessor()) and not shouldBeDeadEnd(n) and s = "unexpected dead end" // TODO: exists(n.getASuccessor()) and shouldBeDeadEnd(n) and s = "expected dead end"
not exists(n.getASuccessor()) and not shouldBeDeadEnd(n) and s = "unexpected dead end"
select n, n.getPrimaryQlClasses(), s select n, n.getPrimaryQlClasses(), s

View File

@@ -6,65 +6,69 @@ Element nthChildOf(Element e, int i) {
} }
predicate duplicateChildren(Element e, int i) { predicate duplicateChildren(Element e, int i) {
nthChildOf(e, i) != nthChildOf(e, i) nthChildOf(e, i) != nthChildOf(e, i) and
// Java #165 // Java #165
and not e instanceof Method not e instanceof Method and
// Java #165 // Java #165
and not e instanceof Constructor not e instanceof Constructor
} }
predicate gapInChildren(Element e, int i) { predicate gapInChildren(Element e, int i) {
exists(int left, int right | exists(int left, int right |
left = min(int l | exists(nthChildOf(e, l))) and left = min(int l | exists(nthChildOf(e, l))) and
right = max(int r | exists(nthChildOf(e, r))) and right = max(int r | exists(nthChildOf(e, r))) and
i in [left .. right] and i in [left .. right] and
not exists(nthChildOf(e, i))) not exists(nthChildOf(e, i))
) and
// Annotations are child 0 upwards, 'implements' are -2 downwards, // Annotations are child 0 upwards, 'implements' are -2 downwards,
// and there may or may not be an 'extends' for child -1. // and there may or may not be an 'extends' for child -1.
and not (e instanceof ClassOrInterface and i = -1) not (e instanceof ClassOrInterface and i = -1) and
// A class instance creation expression has the type as child -3, // A class instance creation expression has the type as child -3,
// may or may not have a qualifier as child -2, and will never have // may or may not have a qualifier as child -2, and will never have
// a child -1. // a child -1.
and not (e instanceof ClassInstanceExpr and i = [-2, -1]) not (e instanceof ClassInstanceExpr and i = [-2, -1]) and
// Type access have annotations from -2 down, and type // Type access have annotations from -2 down, and type
// arguments from 0 up, but may or may not have a qualifier // arguments from 0 up, but may or may not have a qualifier
// at -1. // at -1.
and not (e instanceof TypeAccess and i = -1) not (e instanceof TypeAccess and i = -1) and
// Try statements have their 'finally' clause as child 2, // Try statements have their 'finally' clause as child 2,
// and that may or may not exist. // and that may or may not exist.
and not (e instanceof TryStmt and i = -2) not (e instanceof TryStmt and i = -2) and
// For statements may or may not declare a new variable (child 0), or // For statements may or may not declare a new variable (child 0), or
// have a condition (child 1). // have a condition (child 1).
and not (e instanceof ForStmt and i = [0, 1]) not (e instanceof ForStmt and i = [0, 1]) and
// TODO: Clarify situation with Kotlin and MethodAccess. // TODO: Clarify situation with Kotlin and MethodAccess.
// -1 can be skipped (type arguments from -2 down, no qualifier at -1, // -1 can be skipped (type arguments from -2 down, no qualifier at -1,
// then arguments from 0). // then arguments from 0).
// Can we also skip arguments, e.g. due to defaults for parameters? // Can we also skip arguments, e.g. due to defaults for parameters?
and not (e instanceof MethodAccess and e.getFile().isKotlinSourceFile()) not (e instanceof MethodAccess and e.getFile().isKotlinSourceFile())
} }
predicate lateFirstChild(Element e, int i) { predicate lateFirstChild(Element e, int i) {
i > 0 i > 0 and
and exists(nthChildOf(e, i)) exists(nthChildOf(e, i)) and
and forex(int j | exists(nthChildOf(e, j)) | j >= i) forex(int j | exists(nthChildOf(e, j)) | j >= i) and
// A wildcard type access can be `?` with no children, // A wildcard type access can be `?` with no children,
// `? extends T` with only a child 0, or `? super T` // `? extends T` with only a child 0, or `? super T`
// with only a child 1. // with only a child 1.
and not (e instanceof WildcardTypeAccess and i = 1) not (e instanceof WildcardTypeAccess and i = 1) and
// For a normal local variable decl, child 0 is the type. // For a normal local variable decl, child 0 is the type.
// However, for a Java 10 `var x = ...` declaration, there is // However, for a Java 10 `var x = ...` declaration, there is
// no type, so the first child is the variable as child 1. // no type, so the first child is the variable as child 1.
// There can only be one variable declared in these declarations, // There can only be one variable declared in these declarations,
// so there will never be a child 2. // so there will never be a child 2.
and not (e instanceof LocalVariableDeclStmt and i = 1 and not exists(nthChildOf(e, 2))) not (e instanceof LocalVariableDeclStmt and i = 1 and not exists(nthChildOf(e, 2))) and
// For statements may or may not declare a new variable (child 0), or // For statements may or may not declare a new variable (child 0), or
// have a condition (child 1). // have a condition (child 1).
and not (e instanceof ForStmt and i = [1, 2]) not (e instanceof ForStmt and i = [1, 2])
} }
from Element e, int i, string problem from Element e, int i, string problem
where problem = "duplicate" and duplicateChildren(e, i) where
or problem = "gap" and gapInChildren(e, i) problem = "duplicate" and duplicateChildren(e, i)
or problem = "late" and lateFirstChild(e, i) or
problem = "gap" and gapInChildren(e, i)
or
problem = "late" and lateFirstChild(e, i)
select e, e.getPrimaryQlClasses(), i, problem, select e, e.getPrimaryQlClasses(), i, problem,
concat(int j | exists(nthChildOf(e, j)) | j.toString(), ", " order by j) concat(int j | exists(nthChildOf(e, j)) | j.toString(), ", " order by j)

View File

@@ -1,18 +1,17 @@
import java import java
predicate goodCompilation(Compilation c) { predicate goodCompilation(Compilation c) {
forex(int i | forex(int i | exists(c.getFileCompiled(i)) |
exists(c.getFileCompiled(i)) | (exists(c.getFileCompiled(i - 1)) or i = 0) and
(exists(c.getFileCompiled(i - 1)) or i = 0) c.fileCompiledSuccessful(i)
and c.fileCompiledSuccessful(i)) and ) and
forex(int i | forex(int i | exists(c.getArgument(i)) | exists(c.getArgument(i - 1)) or i = 0) and
exists(c.getArgument(i)) |
exists(c.getArgument(i - 1)) or i = 0) and
c.extractionStarted() and c.extractionStarted() and
c.extractionSuccessful() c.extractionSuccessful()
} }
from Compilation c from Compilation c
where c.isKotlin() where
and not goodCompilation(c) c.isKotlin() and
not goodCompilation(c)
select c select c

View File

@@ -1,9 +1,9 @@
import java import java
from Expr e, int n from Expr e, int n
where n = count(e.getType()) where
and n != 1 n = count(e.getType()) and
n != 1 and
// Java #144 // Java #144
and not e instanceof ReflectiveAccessAnnotation not e instanceof ReflectiveAccessAnnotation
select e, n select e, n

View File

@@ -3,6 +3,7 @@ import java
// File classes should only be extracted if we need somewhere to // File classes should only be extracted if we need somewhere to
// put top-level members. // put top-level members.
from Class c from Class c
where file_class(c) where
and not exists(c.getAMember()) file_class(c) and
not exists(c.getAMember())
select c select c

View File

@@ -1,10 +1,10 @@
import java import java
from Top t from Top t
where t.getAPrimaryQlClass() = "???" where
t.getAPrimaryQlClass() = "???" and
// TypeBound doesn't extend Top (but probably should); part of Kotlin #6 // TypeBound doesn't extend Top (but probably should); part of Kotlin #6
and not t instanceof TypeBound not t instanceof TypeBound and
// XMLLocatable doesn't extend Top (but probably should); part of Kotlin #6 // XMLLocatable doesn't extend Top (but probably should); part of Kotlin #6
and not t instanceof XMLLocatable not t instanceof XMLLocatable
select t, select t, concat(t.getAPrimaryQlClass(), ",")
concat(t.getAPrimaryQlClass(), ",")

View File

@@ -10,9 +10,9 @@ predicate badKotlinType(Element e, int i) {
} }
from Element e, int i from Element e, int i
where // TODO: Java extractor doesn't populate these yet where
e.getFile().isKotlinSourceFile() // TODO: Java extractor doesn't populate these yet
and badKotlinType(e, i) e.getFile().isKotlinSourceFile() and
and i != 1 badKotlinType(e, i) and
i != 1
select e, i select e, i

View File

@@ -18,26 +18,30 @@ Location backwardsLocation() {
// Normally a location should tag at least one locatable entity. // Normally a location should tag at least one locatable entity.
// Note this is not absolutely necessarily a bug (it does not always indicate a location is // Note this is not absolutely necessarily a bug (it does not always indicate a location is
// an orphan in database terms), because classes that are seen both as source code and as a // an orphan in database terms), because classes that are seen both as source code and as a
// .class file are extracted using both locations, with `Top.getLocation` filtering out the // .class file are extracted using both locations, with `Top.getLocation` filtering out the
// class-file location when both a source- and a class-file location are present in the // class-file location when both a source- and a class-file location are present in the
// database. However at present the Java extractor always uses the class-file location at // database. However at present the Java extractor always uses the class-file location at
// least to locate a `File`, so such a location does end up with a single use. // least to locate a `File`, so such a location does end up with a single use.
Location unusedLocation() { Location unusedLocation() {
not exists(Top t | t.getLocation() = result) and not exists(Top t | t.getLocation() = result) and
not exists(XMLLocatable x | x.getLocation() = result) and not exists(XMLLocatable x | x.getLocation() = result) and
not exists(ConfigLocatable c | c.getLocation() = result) and not exists(ConfigLocatable c | c.getLocation() = result) and
not exists(@diagnostic d | diagnostics(d, _, _, _, _, _, result)) and not exists(@diagnostic d | diagnostics(d, _, _, _, _, _, result)) and
not (result.getFile().getExtension() = "xml" and not (
result.getStartLine() = 0 and result.getFile().getExtension() = "xml" and
result.getStartColumn() = 0 and result.getStartLine() = 0 and
result.getEndLine() = 0 and result.getStartColumn() = 0 and
result.getEndColumn() = 0) result.getEndLine() = 0 and
result.getEndColumn() = 0
)
} }
from string reason, Location l from string reason, Location l
where reason = "Bad location" and l = badLocation() where
or reason = "Backwards location" and l = backwardsLocation() reason = "Bad location" and l = badLocation()
or reason = "Unused location" and l = unusedLocation() or
reason = "Backwards location" and l = backwardsLocation()
or
reason = "Unused location" and l = unusedLocation()
select reason, l select reason, l

View File

@@ -16,18 +16,27 @@ string topToString(Top t) {
t instanceof Javadoc and not exists(t.toString()) and result = "<Javadoc>" t instanceof Javadoc and not exists(t.toString()) and result = "<Javadoc>"
or or
// Java #144 // Java #144
t instanceof ReflectiveAccessAnnotation and not exists(t.toString()) and result = "<ReflectiveAccessAnnotation>" t instanceof ReflectiveAccessAnnotation and
not exists(t.toString()) and
result = "<ReflectiveAccessAnnotation>"
} }
string not1ToString() { string not1ToString() {
exists(Top t | count(topToString(t)) != 1 and result = "Top which doesn't have exactly 1 toString") exists(Top t |
count(topToString(t)) != 1 and result = "Top which doesn't have exactly 1 toString"
)
or or
exists(Location l | count(l.toString()) != 1 and result = "Location which doesn't have exactly 1 toString") exists(Location l |
count(l.toString()) != 1 and result = "Location which doesn't have exactly 1 toString"
)
or or
exists(Module m | count(m.toString()) != 1 and result = "Module which doesn't have exactly 1 toString") exists(Module m |
count(m.toString()) != 1 and result = "Module which doesn't have exactly 1 toString"
)
or or
exists(Directive d | count(d.toString()) != 1 and result = "Directive which doesn't have exactly 1 toString") exists(Directive d |
count(d.toString()) != 1 and result = "Directive which doesn't have exactly 1 toString"
)
} }
select not1ToString() select not1ToString()

View File

@@ -19,8 +19,8 @@ Type getATypeUsedInClass(RefType type) {
result = type.getAMethod().getParameterType(_) result = type.getAMethod().getParameterType(_)
or or
result = any(Expr e | e.getEnclosingCallable().getDeclaringType() = type).getType() result = any(Expr e | e.getEnclosingCallable().getDeclaringType() = type).getType()
// Structural recursion over types:
or or
// Structural recursion over types:
result = getAMentionedType(getATypeUsedInClass(type)) result = getAMentionedType(getATypeUsedInClass(type))
} }
@@ -35,5 +35,6 @@ TypeVariable getATypeVariableInScope(RefType type) {
} }
from ClassOrInterface typeUser, TypeVariable outOfScope from ClassOrInterface typeUser, TypeVariable outOfScope
where outOfScope = getAMentionedType(typeUser) and not outOfScope = getATypeVariableInScope(typeUser) where
select "Type " + typeUser + " uses out-of-scope type variable " + outOfScope outOfScope = getAMentionedType(typeUser) and not outOfScope = getATypeVariableInScope(typeUser)
select "Type " + typeUser + " uses out-of-scope type variable " + outOfScope

View File

@@ -1,7 +1,7 @@
import java import java
from Variable v, int n from Variable v, int n
where n = count(v.getType()) where
and n != 1 n = count(v.getType()) and
n != 1
select v, n select v, n

View File

@@ -40,10 +40,14 @@ class Compilation extends @compilation {
predicate fileCompiledSuccessful(int i) { compilation_compiling_files_completed(this, i, 0) } predicate fileCompiledSuccessful(int i) { compilation_compiling_files_completed(this, i, 0) }
/** Holds if the `i`th file during this invocation had recoverable extraction errors. */ /** Holds if the `i`th file during this invocation had recoverable extraction errors. */
predicate fileCompiledRecoverableErrors(int i) { compilation_compiling_files_completed(this, i, 1) } predicate fileCompiledRecoverableErrors(int i) {
compilation_compiling_files_completed(this, i, 1)
}
/** Holds if the `i`th file during this invocation had non-recoverable extraction errors. */ /** Holds if the `i`th file during this invocation had non-recoverable extraction errors. */
predicate fileCompiledNonRecoverableErrors(int i) { compilation_compiling_files_completed(this, i, 2) } predicate fileCompiledNonRecoverableErrors(int i) {
compilation_compiling_files_completed(this, i, 2)
}
/** /**
* Gets the amount of CPU time spent processing file number `i` in the * Gets the amount of CPU time spent processing file number `i` in the

View File

@@ -4,31 +4,32 @@
import java import java
class KotlinType extends Element, @kt_type { class KotlinType extends Element, @kt_type { }
}
class KotlinNullableType extends KotlinType, @kt_nullable_type { class KotlinNullableType extends KotlinType, @kt_nullable_type {
override string toString() { override string toString() {
exists(RefType javaType | exists(RefType javaType |
kt_nullable_types(this, javaType) and kt_nullable_types(this, javaType) and
result = "Kotlin nullable " + javaType.toString()) result = "Kotlin nullable " + javaType.toString()
)
} }
override string getAPrimaryQlClass() { result = "KotlinNullableType" } override string getAPrimaryQlClass() { result = "KotlinNullableType" }
} }
class KotlinNotnullType extends KotlinType, @kt_notnull_type { class KotlinNotnullType extends KotlinType, @kt_notnull_type {
override string toString() { override string toString() {
exists(RefType javaType | exists(RefType javaType |
kt_notnull_types(this, javaType) and kt_notnull_types(this, javaType) and
result = "Kotlin not-null " + javaType.toString()) result = "Kotlin not-null " + javaType.toString()
)
} }
override string getAPrimaryQlClass() { result = "KotlinNotnullType" } override string getAPrimaryQlClass() { result = "KotlinNotnullType" }
} }
class KotlinTypeAlias extends Element, @kt_type_alias { class KotlinTypeAlias extends Element, @kt_type_alias {
override string getAPrimaryQlClass() { result = "KotlinTypeAlias" } override string getAPrimaryQlClass() { result = "KotlinTypeAlias" }
KotlinType getKotlinType() { KotlinType getKotlinType() { kt_type_alias(this, _, result) }
kt_type_alias(this, _, result)
}
} }

View File

@@ -273,9 +273,7 @@ private class PpCastExpr extends PpAst, CastExpr {
} }
private class PpSafeCastExpr extends PpAst, SafeCastExpr { private class PpSafeCastExpr extends PpAst, SafeCastExpr {
override string getPart(int i) { override string getPart(int i) { i = 1 and result = "as?" }
i = 1 and result = "as?"
}
override PpAst getChild(int i) { override PpAst getChild(int i) {
i = 0 and result = this.getExpr() i = 0 and result = this.getExpr()
@@ -285,21 +283,15 @@ private class PpSafeCastExpr extends PpAst, SafeCastExpr {
} }
private class PpImplicitCastExpr extends PpAst, ImplicitCastExpr { private class PpImplicitCastExpr extends PpAst, ImplicitCastExpr {
override PpAst getChild(int i) { override PpAst getChild(int i) { i = 0 and result = this.getExpr() }
i = 0 and result = this.getExpr()
}
} }
private class PpImplicitNotNullExpr extends PpAst, ImplicitNotNullExpr { private class PpImplicitNotNullExpr extends PpAst, ImplicitNotNullExpr {
override PpAst getChild(int i) { override PpAst getChild(int i) { i = 0 and result = this.getExpr() }
i = 0 and result = this.getExpr()
}
} }
private class PpImplicitCoercionToUnitExpr extends PpAst, ImplicitCoercionToUnitExpr { private class PpImplicitCoercionToUnitExpr extends PpAst, ImplicitCoercionToUnitExpr {
override PpAst getChild(int i) { override PpAst getChild(int i) { i = 0 and result = this.getExpr() }
i = 0 and result = this.getExpr()
}
} }
private class PpCall extends PpAst, Call { private class PpCall extends PpAst, Call {

View File

@@ -700,14 +700,10 @@ class Class extends ClassOrInterface, @class {
/** A Kotlin `object`. */ /** A Kotlin `object`. */
class ClassObject extends Class { class ClassObject extends Class {
ClassObject() { ClassObject() { class_object(this, _) }
class_object(this, _)
}
/** Gets the instance variable that implements this `object`. */ /** Gets the instance variable that implements this `object`. */
Field getInstance() { Field getInstance() { class_object(this, result) }
class_object(this, result)
}
} }
/** A Kotlin `companion object`. */ /** A Kotlin `companion object`. */

View File

@@ -1,4 +1,5 @@
import java import java
from AssignOp ae from AssignOp ae
select ae, ae.getOp(), ae.getType().toString(), ae.getDest(), ae.getDest().getType().toString(), ae.getRhs(), ae.getRhs().getType().toString() select ae, ae.getOp(), ae.getType().toString(), ae.getDest(), ae.getDest().getType().toString(),
ae.getRhs(), ae.getRhs().getType().toString()

View File

@@ -1,16 +1,16 @@
import java import java
class InterestingParameter extends Parameter { class InterestingParameter extends Parameter {
InterestingParameter() { InterestingParameter() { this.getFile().getBaseName() = "primitiveArrays.kt" }
this.getFile().getBaseName() = "primitiveArrays.kt"
}
} }
from InterestingParameter p, Array a, KotlinType ktType from InterestingParameter p, Array a, KotlinType ktType
where p.getType() = a and ktType = p.getKotlinType() where p.getType() = a and ktType = p.getKotlinType()
select p, a, a.getComponentType().toString(), a.getElementType().toString(), ktType select p, a, a.getComponentType().toString(), a.getElementType().toString(), ktType
query predicate cloneMethods(Method m, string signature, Array declType, Type returnType, KotlinType ktReturnType) { query predicate cloneMethods(
Method m, string signature, Array declType, Type returnType, KotlinType ktReturnType
) {
any(InterestingParameter p).getType() = declType and any(InterestingParameter p).getType() = declType and
signature = m.getSignature() and signature = m.getSignature() and
declType = m.getDeclaringType() and declType = m.getDeclaringType() and

View File

@@ -3,4 +3,3 @@ import java
from Class c from Class c
where c.fromSource() where c.fromSource()
select c, c.getQualifiedName(), concat(string s | c.hasModifier(s) | s, ", ") select c, c.getQualifiedName(), concat(string s | c.hasModifier(s) | s, ", ")

View File

@@ -3,4 +3,3 @@ import java
from Interface i from Interface i
where i.fromSource() where i.fromSource()
select i select i

View File

@@ -3,15 +3,14 @@ import java
// Stop external filepaths from appearing in the results // Stop external filepaths from appearing in the results
class ClassOrInterfaceLocation extends ClassOrInterface { class ClassOrInterfaceLocation extends ClassOrInterface {
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) { override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
exists(string fullPath | exists(string fullPath | super.hasLocationInfo(fullPath, sl, sc, el, ec) |
super.hasLocationInfo(fullPath, sl, sc, el, ec) | if exists(this.getFile().getRelativePath())
if exists(this.getFile().getRelativePath()) then path = fullPath
then path = fullPath else path = fullPath.regexpReplaceAll(".*/", "<external>/")
else path = fullPath.regexpReplaceAll(".*/", "<external>/")) )
} }
} }
from ParameterizedClass c, int i from ParameterizedClass c, int i
where c.getSourceDeclaration().fromSource() where c.getSourceDeclaration().fromSource()
select c, i, c.getTypeArgument(i) select c, i, c.getTypeArgument(i)

View File

@@ -3,16 +3,16 @@ import java
// Stop external filepaths from appearing in the results // Stop external filepaths from appearing in the results
class ClassOrInterfaceLocation extends ClassOrInterface { class ClassOrInterfaceLocation extends ClassOrInterface {
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) { override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
exists(string fullPath | exists(string fullPath | super.hasLocationInfo(fullPath, sl, sc, el, ec) |
super.hasLocationInfo(fullPath, sl, sc, el, ec) | if exists(this.getFile().getRelativePath())
if exists(this.getFile().getRelativePath()) then path = fullPath
then path = fullPath else path = fullPath.regexpReplaceAll(".*/", "<external>/")
else path = fullPath.regexpReplaceAll(".*/", "<external>/")) )
} }
} }
from Class c, Type superType from Class c, Type superType
where c.getSourceDeclaration().fromSource() and where
superType = c.getASupertype() c.getSourceDeclaration().fromSource() and
superType = c.getASupertype()
select c, superType select c, superType

View File

@@ -6,22 +6,31 @@ newtype TMaybeControlFlowNode =
class MaybeControlFlowNode extends TMaybeControlFlowNode { class MaybeControlFlowNode extends TMaybeControlFlowNode {
abstract string toString(); abstract string toString();
abstract Location getLocation(); abstract Location getLocation();
abstract string getPrimaryQlClasses(); abstract string getPrimaryQlClasses();
} }
class YesMaybeControlFlowNode extends MaybeControlFlowNode { class YesMaybeControlFlowNode extends MaybeControlFlowNode {
ControlFlowNode c; ControlFlowNode c;
YesMaybeControlFlowNode() { this = TControlFlowNode(c) } YesMaybeControlFlowNode() { this = TControlFlowNode(c) }
override string toString() { result = c.toString() } override string toString() { result = c.toString() }
override Location getLocation() { result = c.getLocation() } override Location getLocation() { result = c.getLocation() }
override string getPrimaryQlClasses() { result = c.getPrimaryQlClasses() } override string getPrimaryQlClasses() { result = c.getPrimaryQlClasses() }
} }
class NoMaybeControlFlowNode extends MaybeControlFlowNode { class NoMaybeControlFlowNode extends MaybeControlFlowNode {
NoMaybeControlFlowNode() { this = TNoControlFlowNode() } NoMaybeControlFlowNode() { this = TNoControlFlowNode() }
override string toString() { result = "<none>" } override string toString() { result = "<none>" }
override Location getLocation() { result.toString() = "file://:0:0:0:0" } override Location getLocation() { result.toString() = "file://:0:0:0:0" }
override string getPrimaryQlClasses() { result = "<none>" } override string getPrimaryQlClasses() { result = "<none>" }
} }
@@ -32,7 +41,7 @@ MaybeControlFlowNode maybeSuccessor(ControlFlowNode n) {
} }
from ControlFlowNode n, MaybeControlFlowNode m from ControlFlowNode n, MaybeControlFlowNode m
where m = maybeSuccessor(n) where
and n.getFile().(CompilationUnit).fromSource() m = maybeSuccessor(n) and
n.getFile().(CompilationUnit).fromSource()
select n, n.getPrimaryQlClasses(), m, m.getPrimaryQlClasses() select n, n.getPrimaryQlClasses(), m, m.getPrimaryQlClasses()

View File

@@ -6,6 +6,7 @@ newtype TMaybeElement =
class MaybeElement extends TMaybeElement { class MaybeElement extends TMaybeElement {
abstract string toString(); abstract string toString();
abstract Location getLocation(); abstract Location getLocation();
} }
@@ -13,7 +14,9 @@ class YesMaybeElement extends MaybeElement {
Element e; Element e;
YesMaybeElement() { this = TElement(e) } YesMaybeElement() { this = TElement(e) }
override string toString() { result = e.toString() } override string toString() { result = e.toString() }
override Location getLocation() { result = e.getLocation() } override Location getLocation() { result = e.getLocation() }
} }
@@ -21,6 +24,7 @@ class NoMaybeElement extends MaybeElement {
NoMaybeElement() { this = TNoElement() } NoMaybeElement() { this = TNoElement() }
override string toString() { result = "<none>" } override string toString() { result = "<none>" }
override Location getLocation() { none() } override Location getLocation() { none() }
} }

View File

@@ -6,6 +6,7 @@ newtype TMaybeElement =
class MaybeElement extends TMaybeElement { class MaybeElement extends TMaybeElement {
abstract string toString(); abstract string toString();
abstract Location getLocation(); abstract Location getLocation();
} }
@@ -13,7 +14,9 @@ class YesMaybeElement extends MaybeElement {
Element e; Element e;
YesMaybeElement() { this = TElement(e) } YesMaybeElement() { this = TElement(e) }
override string toString() { result = e.toString() } override string toString() { result = e.toString() }
override Location getLocation() { result = e.getLocation() } override Location getLocation() { result = e.getLocation() }
} }
@@ -21,6 +24,7 @@ class NoMaybeElement extends MaybeElement {
NoMaybeElement() { this = TNoElement() } NoMaybeElement() { this = TNoElement() }
override string toString() { result = "<none>" } override string toString() { result = "<none>" }
override Location getLocation() { none() } override Location getLocation() { none() }
} }

View File

@@ -3,14 +3,14 @@ import java
// Stop external filepaths from appearing in the results // Stop external filepaths from appearing in the results
class ClassOrInterfaceLocation extends ClassOrInterface { class ClassOrInterfaceLocation extends ClassOrInterface {
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) { override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
exists(string fullPath | exists(string fullPath | super.hasLocationInfo(fullPath, sl, sc, el, ec) |
super.hasLocationInfo(fullPath, sl, sc, el, ec) | if exists(this.getFile().getRelativePath())
if exists(this.getFile().getRelativePath()) then path = fullPath
then path = fullPath else path = fullPath.regexpReplaceAll(".*/", "<external>/")
else path = fullPath.regexpReplaceAll(".*/", "<external>/")) )
} }
} }
from Method m from Method m
where m.fromSource() where m.fromSource()
select m, m.getReturnType() select m, m.getReturnType()

View File

@@ -1,4 +1,5 @@
import java import java
from MethodAccess c from MethodAccess c
select c, c.getCallee(), c.getCallee().getDeclaringType(), c.getCallee().getDeclaringType().(NestedType).getEnclosingType() select c, c.getCallee(), c.getCallee().getDeclaringType(),
c.getCallee().getDeclaringType().(NestedType).getEnclosingType()

View File

@@ -1,4 +1,5 @@
import java import java
from MethodAccess c from MethodAccess c
select c, c.getCallee(), c.getCallee().getDeclaringType(), c.getCallee().getDeclaringType().(NestedType).getEnclosingType() select c, c.getCallee(), c.getCallee().getDeclaringType(),
c.getCallee().getDeclaringType().(NestedType).getEnclosingType()

View File

@@ -1,4 +1,5 @@
import java import java
from MethodAccess c from MethodAccess c
select c, c.getCallee(), c.getCallee().getDeclaringType(), c.getCallee().getDeclaringType().(NestedType).getEnclosingType() select c, c.getCallee(), c.getCallee().getDeclaringType(),
c.getCallee().getDeclaringType().(NestedType).getEnclosingType()

View File

@@ -2,4 +2,5 @@ import java
from Parameter p from Parameter p
where p.fromSource() where p.fromSource()
select p, p.getType().toString(), p.getType().getFile().getBaseName(), p.getType().getLocation().getStartLine() select p, p.getType().toString(), p.getType().getFile().getBaseName(),
p.getType().getLocation().getStartLine()

View File

@@ -6,17 +6,20 @@ string paramTypeIfPresent(Callable m) {
else result = "No parameters" else result = "No parameters"
} }
query predicate calls(MethodAccess ma, Callable caller, RefType callerType, Callable called, RefType calledType) { query predicate calls(
MethodAccess ma, Callable caller, RefType callerType, Callable called, RefType calledType
) {
ma.getEnclosingCallable() = caller and ma.getEnclosingCallable() = caller and
ma.getCallee() = called and ma.getCallee() = called and
caller.fromSource() and caller.fromSource() and
callerType = caller.getDeclaringType() and callerType = caller.getDeclaringType() and
calledType = called.getDeclaringType() calledType = called.getDeclaringType()
} }
query predicate constructors(RefType t, Constructor c, string signature, string paramType, string returnType, RefType sourceDeclType, Constructor sourceDecl) { query predicate constructors(
RefType t, Constructor c, string signature, string paramType, string returnType,
RefType sourceDeclType, Constructor sourceDecl
) {
t.getSourceDeclaration().fromSource() and t.getSourceDeclaration().fromSource() and
c.getDeclaringType() = t and c.getDeclaringType() = t and
signature = c.getSignature() and signature = c.getSignature() and
@@ -31,13 +34,13 @@ query predicate constructorCalls(ConstructorCall cc, Constructor callee) {
callee.getSourceDeclaration().fromSource() callee.getSourceDeclaration().fromSource()
} }
query predicate refTypes(RefType rt) { query predicate refTypes(RefType rt) { rt.fromSource() }
rt.fromSource()
}
from RefType t, Method m, Method sourceDecl, RefType sourceDeclType from RefType t, Method m, Method sourceDecl, RefType sourceDeclType
where t.getSourceDeclaration().fromSource() where
and m.getDeclaringType() = t t.getSourceDeclaration().fromSource() and
and sourceDecl = m.getSourceDeclaration() m.getDeclaringType() = t and
and sourceDeclType = sourceDecl.getDeclaringType() sourceDecl = m.getSourceDeclaration() and
select t, m, m.getSignature(), paramTypeIfPresent(m), m.getReturnType().toString(), sourceDeclType, sourceDecl sourceDeclType = sourceDecl.getDeclaringType()
select t, m, m.getSignature(), paramTypeIfPresent(m), m.getReturnType().toString(), sourceDeclType,
sourceDecl

View File

@@ -1,4 +1,5 @@
import java import java
from MethodAccess ma from MethodAccess ma
select ma.getQualifier(), ma.getCallee(), ma.getCallee().getSignature(), ma.getCallee().getAParamType().toString(), ma.getCallee().getDeclaringType() select ma.getQualifier(), ma.getCallee(), ma.getCallee().getSignature(),
ma.getCallee().getAParamType().toString(), ma.getCallee().getDeclaringType()

View File

@@ -1,6 +1,7 @@
import java import java
from RefType rt, Member m from RefType rt, Member m
where rt.getSourceDeclaration().fromSource() where
and m.getDeclaringType() = rt rt.getSourceDeclaration().fromSource() and
m.getDeclaringType() = rt
select rt, m select rt, m

View File

@@ -1,9 +1,13 @@
import java import java
query predicate classTVs(TypeVariable tv, GenericType declType, string bound) { query predicate classTVs(TypeVariable tv, GenericType declType, string bound) {
tv.getGenericType() = declType and tv.getUpperBoundType().toString() = bound and declType.fromSource() tv.getGenericType() = declType and
tv.getUpperBoundType().toString() = bound and
declType.fromSource()
} }
query predicate functionTVs(TypeVariable tv, GenericCallable callable, string bound) { query predicate functionTVs(TypeVariable tv, GenericCallable callable, string bound) {
tv.getGenericCallable() = callable and tv.getUpperBoundType().toString() = bound and callable.fromSource() tv.getGenericCallable() = callable and
tv.getUpperBoundType().toString() = bound and
callable.fromSource()
} }

View File

@@ -7,10 +7,11 @@ RefType getADatabaseSubtype(RefType rt) {
} }
from Parameter p, RefType paramType, KotlinType paramKtType, string paramAncestorType from Parameter p, RefType paramType, KotlinType paramKtType, string paramAncestorType
where p.fromSource() where
and p.getCallable().getName() = "user" p.fromSource() and
and p.getType() = paramType p.getCallable().getName() = "user" and
and p.getKotlinType() = paramKtType p.getType() = paramType and
// Stringified to avoid printing the source-location (i.e. stdlib path) of `Any?` p.getKotlinType() = paramKtType and
and getADatabaseSubtype+(paramType).toString() = paramAncestorType // Stringified to avoid printing the source-location (i.e. stdlib path) of `Any?`
select p, paramType, paramKtType, paramAncestorType getADatabaseSubtype+(paramType).toString() = paramAncestorType
select p, paramType, paramKtType, paramAncestorType

View File

@@ -7,6 +7,8 @@ query predicate methods(RefType declType, Method m, string signature, string mod
modifiers = concat(string s | m.hasModifier(s) | s, ", ") modifiers = concat(string s | m.hasModifier(s) | s, ", ")
} }
query predicate constructors(RefType declType, Constructor c, string signature) { c.fromSource() and declType = c.getDeclaringType() and signature = c.getSignature() } query predicate constructors(RefType declType, Constructor c, string signature) {
c.fromSource() and declType = c.getDeclaringType() and signature = c.getSignature()
}
query predicate extensions(ExtensionMethod m, Type t) { m.getExtendedType() = t and m.fromSource() } query predicate extensions(ExtensionMethod m, Type t) { m.getExtendedType() = t and m.fromSource() }

View File

@@ -1,6 +1,7 @@
import java import java
from Method m, Parameter p, int i from Method m, Parameter p, int i
where m.getParameter(i) = p where
and m.fromSource() m.getParameter(i) = p and
m.fromSource()
select m, p, i select m, p, i

View File

@@ -3,5 +3,4 @@ import java
from File f from File f
where f.isSourceFile() where f.isSourceFile()
select f select f
// This test is mainly a consistency test; just checking that both the Java and Kotlin source were extracted here // This test is mainly a consistency test; just checking that both the Java and Kotlin source were extracted here

View File

@@ -3,4 +3,3 @@ import java
from Class c from Class c
where c.fromSource() where c.fromSource()
select c, c.getQualifiedName() select c, c.getQualifiedName()

View File

@@ -3,25 +3,26 @@ import java
// Stop external filepaths from appearing in the results // Stop external filepaths from appearing in the results
class ClassOrInterfaceLocation extends ClassOrInterface { class ClassOrInterfaceLocation extends ClassOrInterface {
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) { override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
exists(string fullPath | exists(string fullPath | super.hasLocationInfo(fullPath, sl, sc, el, ec) |
super.hasLocationInfo(fullPath, sl, sc, el, ec) | if exists(this.getFile().getRelativePath())
if exists(this.getFile().getRelativePath()) then path = fullPath
then path = fullPath else path = fullPath.regexpReplaceAll(".*/", "<external>/")
else path = fullPath.regexpReplaceAll(".*/", "<external>/")) )
} }
} }
class MethodLocation extends Method { class MethodLocation extends Method {
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) { override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
exists(string fullPath | exists(string fullPath | super.hasLocationInfo(fullPath, sl, sc, el, ec) |
super.hasLocationInfo(fullPath, sl, sc, el, ec) | if exists(this.getFile().getRelativePath())
if exists(this.getFile().getRelativePath()) then path = fullPath
then path = fullPath else path = fullPath.regexpReplaceAll(".*/", "<external>/")
else path = fullPath.regexpReplaceAll(".*/", "<external>/")) )
} }
} }
from MethodAccess ma, Method m from MethodAccess ma, Method m
where ma.getFile().(CompilationUnit).fromSource() where
and m = ma.getMethod() ma.getFile().(CompilationUnit).fromSource() and
m = ma.getMethod()
select ma, m, m.getQualifiedName(), m.getDeclaringType() select ma, m, m.getQualifiedName(), m.getDeclaringType()

View File

@@ -3,4 +3,3 @@ import java
from Method m from Method m
where m.fromSource() where m.fromSource()
select m, m.getQualifiedName(), m.getDeclaringType() select m, m.getQualifiedName(), m.getDeclaringType()

View File

@@ -1,6 +1,7 @@
import java import java
from ClassObject co, Field f from ClassObject co, Field f
where co.fromSource() where
and f = co.getInstance() co.fromSource() and
f = co.getInstance()
select co, f, concat(f.getAModifier().toString(), ",") select co, f, concat(f.getAModifier().toString(), ",")

View File

@@ -1,12 +1,10 @@
import java import java
from KtComment c, string s, int len from KtComment c, string s, int len
where s = c.getText() where
and len = s.length() s = c.getText() and
len = s.length() and
// Ignore the comments that are describing what's going on, and only // Ignore the comments that are describing what's going on, and only
// select the large ones // select the large ones
and len > 1000 len > 1000
select c.getLocation(), select c.getLocation(), len, s.prefix(5) + "..." + s.suffix(len - 5), c.getPrimaryQlClasses()
len,
s.prefix(5) + "..." + s.suffix(len - 5),
c.getPrimaryQlClasses()

View File

@@ -2,4 +2,6 @@ import semmle.code.java.Diagnostics
from Diagnostic d from Diagnostic d
select d, d.getGeneratedBy(), d.getSeverity(), d.getTag(), d.getMessage(), select d, d.getGeneratedBy(), d.getSeverity(), d.getTag(), d.getMessage(),
d.getFullMessage().regexpReplaceAll("^\\[[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} K\\] ", "[DATE TIME K] ") d.getFullMessage()
.regexpReplaceAll("^\\[[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} K\\] ",
"[DATE TIME K] ")

View File

@@ -2,7 +2,5 @@ import java
from Literal l, int len from Literal l, int len
where len = l.getValue().length() where len = l.getValue().length()
select l.getLocation(), select l.getLocation(), len, l.getValue().prefix(5) + "..." + l.getValue().suffix(len - 5),
len, l.getPrimaryQlClasses()
l.getValue().prefix(5) + "..." + l.getValue().suffix(len - 5),
l.getPrimaryQlClasses()

View File

@@ -3,16 +3,16 @@ import java
// Stop external filepaths from appearing in the results // Stop external filepaths from appearing in the results
class ClassOrInterfaceLocation extends ClassOrInterface { class ClassOrInterfaceLocation extends ClassOrInterface {
override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) { override predicate hasLocationInfo(string path, int sl, int sc, int el, int ec) {
exists(string fullPath | exists(string fullPath | super.hasLocationInfo(fullPath, sl, sc, el, ec) |
super.hasLocationInfo(fullPath, sl, sc, el, ec) | if exists(this.getFile().getRelativePath())
if exists(this.getFile().getRelativePath()) then path = fullPath
then path = fullPath else path = fullPath.regexpReplaceAll(".*/", "<external>/")
else path = fullPath.regexpReplaceAll(".*/", "<external>/")) )
} }
} }
from Method m, int i, Parameter p from Method m, int i, Parameter p
where m.getName().matches("foo%") where
and p = m.getParameter(i) m.getName().matches("foo%") and
p = m.getParameter(i)
select m, m.getReturnType(), i, p, p.getType() select m, m.getReturnType(), i, p, p.getType()

View File

@@ -2,4 +2,3 @@ import java
from Type t from Type t
select t.toString(), concat(t.getAPrimaryQlClass(), ", ") select t.toString(), concat(t.getAPrimaryQlClass(), ", ")

View File

@@ -15,4 +15,4 @@ class Config extends DataFlow::Configuration {
from DataFlow::Node source, DataFlow::Node sink, Config c from DataFlow::Node source, DataFlow::Node sink, Config c
where c.hasFlow(source, sink) where c.hasFlow(source, sink)
select source, sink select source, sink

View File

@@ -6,6 +6,7 @@ newtype TMaybeExpr =
class MaybeExpr extends TMaybeExpr { class MaybeExpr extends TMaybeExpr {
abstract string toString(); abstract string toString();
abstract Location getLocation(); abstract Location getLocation();
} }
@@ -13,7 +14,9 @@ class YesMaybeExpr extends MaybeExpr {
Expr c; Expr c;
YesMaybeExpr() { this = TExpr(c) } YesMaybeExpr() { this = TExpr(c) }
override string toString() { result = c.toString() } override string toString() { result = c.toString() }
override Location getLocation() { result = c.getLocation() } override Location getLocation() { result = c.getLocation() }
} }
@@ -21,16 +24,14 @@ class NoMaybeExpr extends MaybeExpr {
NoMaybeExpr() { this = TNoExpr() } NoMaybeExpr() { this = TNoExpr() }
override string toString() { result = "<none>" } override string toString() { result = "<none>" }
override Location getLocation() { none() } override Location getLocation() { none() }
} }
MaybeExpr initializer(Variable v) { MaybeExpr initializer(Variable v) {
if exists(v.getInitializer()) if exists(v.getInitializer()) then result = TExpr(v.getInitializer()) else result = TNoExpr()
then result = TExpr(v.getInitializer())
else result = TNoExpr()
} }
from Variable v from Variable v
where v.fromSource() where v.fromSource()
select v, v.getType().toString(), initializer(v) select v, v.getType().toString(), initializer(v)