Skip to content

Commit fe24cc1

Browse files
authored
Merge pull request github#13718 from igfoo/igfoo/file_classes
Kotlin: Improve file class support
2 parents 5f76caf + 75c835c commit fe24cc1

File tree

14 files changed

+50
-0
lines changed

14 files changed

+50
-0
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,10 @@ open class KotlinFileExtractor(
643643
logger.warnElement("Unrecognised class kind $kind", c)
644644
}
645645

646+
if (c.origin == IrDeclarationOrigin.FILE_CLASS) {
647+
tw.writeFile_class(id)
648+
}
649+
646650
if (c.isData) {
647651
tw.writeKtDataClasses(id)
648652
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fun a() {
2+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fun b() {
2+
a()
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class C {
2+
fun c() {}
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| AKt.class:0:0:0:0 | AKt | true |
2+
| B.kt:0:0:0:0 | BKt | true |
3+
| C.kt:1:1:3:1 | C | false |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import java
2+
3+
from Class c
4+
where exists(c.getLocation().getFile().getRelativePath())
5+
select c, any(boolean b | if c.isFileClass() then b = true else b = false)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from create_database_utils import *
2+
3+
runSuccessfully([get_cmd("kotlinc"), 'A.kt'])
4+
run_codeql_database_create(['kotlinc -cp . B.kt C.kt'], lang="java")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: feature
3+
---
4+
* A `Class.isFileClass()` predicate, to identify Kotlin file classes, has been added.

java/ql/lib/semmle/code/java/Type.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,12 @@ class Class extends ClassOrInterface {
709709
)
710710
}
711711

712+
/**
713+
* Holds if this class is a Kotlin "file class", e.g. the class FooKt
714+
* for top-level entities in Foo.kt.
715+
*/
716+
predicate isFileClass() { file_class(this) }
717+
712718
override string getAPrimaryQlClass() { result = "Class" }
713719
}
714720

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fun a() {
2+
}

0 commit comments

Comments
 (0)