Merge pull request #13718 from igfoo/igfoo/file_classes

Kotlin: Improve file class support
This commit is contained in:
Ian Lynagh
2023-07-12 15:42:16 +01:00
committed by GitHub
14 changed files with 50 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
fun a() {
}

View File

@@ -0,0 +1,3 @@
fun b() {
a()
}

View File

@@ -0,0 +1,3 @@
class C {
fun c() {}
}

View File

@@ -0,0 +1,3 @@
| AKt.class:0:0:0:0 | AKt | true |
| B.kt:0:0:0:0 | BKt | true |
| C.kt:1:1:3:1 | C | false |

View File

@@ -0,0 +1,5 @@
import java
from Class c
where exists(c.getLocation().getFile().getRelativePath())
select c, any(boolean b | if c.isFileClass() then b = true else b = false)

View File

@@ -0,0 +1,4 @@
from create_database_utils import *
runSuccessfully([get_cmd("kotlinc"), 'A.kt'])
run_codeql_database_create(['kotlinc -cp . B.kt C.kt'], lang="java")

View File

@@ -0,0 +1,4 @@
---
category: feature
---
* A `Class.isFileClass()` predicate, to identify Kotlin file classes, has been added.

View File

@@ -709,6 +709,12 @@ class Class extends ClassOrInterface {
)
}
/**
* Holds if this class is a Kotlin "file class", e.g. the class FooKt
* for top-level entities in Foo.kt.
*/
predicate isFileClass() { file_class(this) }
override string getAPrimaryQlClass() { result = "Class" }
}

View File

@@ -0,0 +1,2 @@
fun a() {
}

View File

@@ -0,0 +1,3 @@
fun b() {
a()
}

View File

@@ -0,0 +1,3 @@
class C {
fun c() {}
}

View File

@@ -0,0 +1,3 @@
| A.kt:0:0:0:0 | AKt | true |
| B.kt:0:0:0:0 | BKt | true |
| C.kt:1:1:3:1 | C | false |

View File

@@ -0,0 +1,5 @@
import java
from Class c
where c.fromSource()
select c, any(boolean b | if c.isFileClass() then b = true else b = false)