Java/Kotlin: Add File.is{,Java,Kotlin}SourceFile()

This commit is contained in:
Ian Lynagh
2021-12-08 15:40:29 +00:00
parent 70708d69bf
commit 7c03ed99dc
8 changed files with 21 additions and 12 deletions

View File

@@ -2,8 +2,8 @@ import java
import semmle.code.java.ControlFlowGraph
predicate shouldBeDeadEnd(ControlFlowNode n) {
n instanceof BreakStmt and n.getFile().getExtension() = "kt" or // TODO
n instanceof ReturnStmt and n.getFile().getExtension() = "kt" or // TODO
n instanceof BreakStmt and n.getFile().isKotlinSourceFile() or // TODO
n instanceof ReturnStmt and n.getFile().isKotlinSourceFile() or // TODO
n instanceof Interface or // TODO
n instanceof Class or // TODO
n instanceof Parameter or // TODO

View File

@@ -37,7 +37,7 @@ predicate gapInChildren(Element e, int i) {
// TODO: Tighten this up:
and not e instanceof ForStmt
// Kotlin bug?
and not (e instanceof MethodAccess and e.getFile().getExtension() = "kt")
and not (e instanceof MethodAccess and e.getFile().isKotlinSourceFile())
}
predicate lateFirstChild(Element e, int i) {

View File

@@ -35,9 +35,9 @@ predicate difference(Expr e) {
predicate notSpecified(Expr e) {
enclosingStmtOrOther(e) instanceof Stmt and not exists(e.getEnclosingStmt())
// bug in Java?
and not e instanceof Annotation and e.getFile().getExtension() = "java"
and not e instanceof Annotation and e.getFile().isJavaSourceFile()
// bug in Kotlin?
and not e instanceof TypeAccess and e.getFile().getExtension() = "kt"
and not e instanceof TypeAccess and e.getFile().isKotlinSourceFile()
}
predicate problem(Expr e, string s) {

View File

@@ -181,6 +181,15 @@ class File extends Container, @file {
override string getURL() { result = "file://" + this.getAbsolutePath() + ":0:0:0:0" }
override string getAPrimaryQlClass() { result = "File" }
/** Holds if this is a (Java or Kotlin) source file. */
predicate isSourceFile() { this.isJavaSourceFile() or this.isKotlinSourceFile() }
/** Holds if this is a Java source file. */
predicate isJavaSourceFile() { this.getExtension() = "java" }
/** Holds if this is a Kotlin source file. */
predicate isKotlinSourceFile() { this.getExtension() = "kt" }
}
/**

View File

@@ -196,7 +196,7 @@ class Location extends @location {
}
private predicate hasSourceLocation(Top l, Location loc, File f) {
hasLocation(l, loc) and f = loc.getFile() and f.getExtension() = ["java", "kt"]
hasLocation(l, loc) and f = loc.getFile() and f.isSourceFile()
}
cached

View File

@@ -34,7 +34,7 @@ class Element extends @element, Top {
* Elements pertaining to source files may include generated elements
* not visible in source code, such as implicit default constructors.
*/
predicate fromSource() { this.getCompilationUnit().getExtension() = ["java", "kt"] }
predicate fromSource() { this.getCompilationUnit().isSourceFile() }
/** Gets the compilation unit that this element belongs to. */
CompilationUnit getCompilationUnit() { result = this.getFile() }

View File

@@ -1,23 +1,23 @@
import java
query predicate genericType(GenericType t, TypeVariable tv, int i) {
t.getTypeParameter(i) = tv and t.getFile().getExtension() = "kt"
t.getTypeParameter(i) = tv and t.getFile().isKotlinSourceFile()
}
query predicate parameterizedType(ParameterizedType t, GenericType gt, int i, string ta) {
t.getGenericType() = gt and
t.getTypeArgument(i).toString() = ta and
t.getFile().getExtension() = "kt"
t.getFile().isKotlinSourceFile()
}
query predicate function(Callable c, string signature) {
signature = c.getSignature() and
c.getFile().getExtension() = "kt"
c.getFile().isKotlinSourceFile()
}
query predicate genericFunction(GenericCallable c, RefType declType, TypeVariable tv, int i) {
c.getTypeParameter(i) = tv and
c.getFile().getExtension() = "kt" and
c.getFile().isKotlinSourceFile() and
c.getDeclaringType() = declType
}

View File

@@ -1,7 +1,7 @@
import java
from File f
where f.getExtension() in ["java", "kt"]
where f.isSourceFile()
select f
// This test is mainly a consistency test; just checking that both the Java and Kotlin source were extracted here