mirror of
https://github.com/github/codeql.git
synced 2026-04-30 11:15:13 +02:00
Kotlin: Extract method visibility
This commit is contained in:
@@ -8,6 +8,7 @@ import com.semmle.extractor.java.OdasaOutput
|
||||
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
|
||||
import org.jetbrains.kotlin.builtins.functions.BuiltInFunctionArity
|
||||
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
|
||||
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
|
||||
import org.jetbrains.kotlin.descriptors.Modality
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
@@ -93,6 +94,16 @@ open class KotlinFileExtractor(
|
||||
return id
|
||||
}
|
||||
|
||||
fun extractVisibility(elementForLocation: IrElement, id: Label<out DbModifiable>, v: DescriptorVisibility) {
|
||||
when (v) {
|
||||
DescriptorVisibilities.PRIVATE -> addModifiers(id, "private")
|
||||
DescriptorVisibilities.PROTECTED -> addModifiers(id, "protected")
|
||||
DescriptorVisibilities.PUBLIC -> addModifiers(id, "public")
|
||||
DescriptorVisibilities.INTERNAL -> addModifiers(id, "internal")
|
||||
else -> logger.warnElement(Severity.ErrorSevere, "Unexpected visibility: $v", elementForLocation)
|
||||
}
|
||||
}
|
||||
|
||||
fun extractClassModifiers(c: IrClass, id: Label<out DbClassorinterface>) {
|
||||
when (c.modality) {
|
||||
Modality.FINAL -> addModifiers(id, "final")
|
||||
@@ -101,13 +112,7 @@ open class KotlinFileExtractor(
|
||||
Modality.ABSTRACT -> addModifiers(id, "abstract")
|
||||
else -> logger.warnElement(Severity.ErrorSevere, "Unexpected class modality: ${c.modality}", c)
|
||||
}
|
||||
when (c.visibility) {
|
||||
DescriptorVisibilities.PRIVATE -> addModifiers(id, "private")
|
||||
DescriptorVisibilities.PROTECTED -> addModifiers(id, "protected")
|
||||
DescriptorVisibilities.PUBLIC -> addModifiers(id, "public")
|
||||
DescriptorVisibilities.INTERNAL -> addModifiers(id, "internal")
|
||||
else -> logger.warnElement(Severity.ErrorSevere, "Unexpected class visibility: ${c.visibility}", c)
|
||||
}
|
||||
extractVisibility(c, id, c.visibility)
|
||||
}
|
||||
|
||||
fun extractClassInstance(c: IrClass, typeArgs: List<IrTypeArgument>): Label<out DbClassorinterface> {
|
||||
@@ -452,6 +457,8 @@ open class KotlinFileExtractor(
|
||||
extractBody(body, id)
|
||||
}
|
||||
|
||||
extractVisibility(f, id, f.visibility)
|
||||
|
||||
currentFunction = null
|
||||
return id
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
| methods5.kt:10:13:10:18 | new (...) | ClassInstanceExpr |
|
||||
| methods5.kt:10:16:10:17 | 42 | IntegerLiteral |
|
||||
| methods5.kt:13:1:13:14 | <obinit>(...) | MethodAccess |
|
||||
| methods.kt:5:1:13:1 | <obinit>(...) | MethodAccess |
|
||||
| methods.kt:5:1:19:1 | <obinit>(...) | MethodAccess |
|
||||
| methods.kt:10:9:10:25 | classMethod(...) | MethodAccess |
|
||||
| methods.kt:10:9:10:25 | this | ThisAccess |
|
||||
| methods.kt:10:21:10:21 | a | VarAccess |
|
||||
|
||||
@@ -1,21 +1,26 @@
|
||||
methods
|
||||
| methods2.kt:0:0:0:0 | Methods2Kt | methods2.kt:4:1:5:1 | fooBarTopLevelMethod | fooBarTopLevelMethod(int,int) |
|
||||
| methods2.kt:7:1:10:1 | Class2 | methods2.kt:7:1:10:1 | <obinit> | <obinit>() |
|
||||
| methods2.kt:7:1:10:1 | Class2 | methods2.kt:8:5:9:5 | fooBarClassMethod | fooBarClassMethod(int,int) |
|
||||
| methods3.kt:0:0:0:0 | Methods3Kt | methods3.kt:3:1:3:42 | fooBarTopLevelMethodExt | fooBarTopLevelMethodExt(int,int) |
|
||||
| methods3.kt:5:1:7:1 | Class3 | methods3.kt:5:1:7:1 | <obinit> | <obinit>() |
|
||||
| methods3.kt:5:1:7:1 | Class3 | methods3.kt:6:5:6:46 | fooBarTopLevelMethodExt | fooBarTopLevelMethodExt(int,int) |
|
||||
| methods4.kt:3:1:11:1 | NestedTest | methods4.kt:3:1:11:1 | <obinit> | <obinit>() |
|
||||
| methods4.kt:5:3:9:3 | InsideNestedTest | methods4.kt:5:3:9:3 | <obinit> | <obinit>() |
|
||||
| methods4.kt:5:3:9:3 | InsideNestedTest | methods4.kt:7:5:7:34 | m | m(foo.bar.NestedTest.InsideNestedTest) |
|
||||
| methods5.kt:0:0:0:0 | Methods5Kt | methods5.kt:3:1:11:1 | x | x() |
|
||||
| methods5.kt:5:3:5:27 | new Object(...) { ... } | methods5.kt:5:3:5:27 | a | a(int) |
|
||||
| methods5.kt:9:3:9:32 | new Object(...) { ... } | methods5.kt:9:3:9:32 | f1 | f1(foo.bar.C1,int) |
|
||||
| methods5.kt:13:1:13:14 | C1 | methods5.kt:13:1:13:14 | <obinit> | <obinit>() |
|
||||
| methods.kt:0:0:0:0 | MethodsKt | methods.kt:2:1:3:1 | topLevelMethod | topLevelMethod(int,int) |
|
||||
| methods.kt:5:1:13:1 | Class | methods.kt:5:1:13:1 | <obinit> | <obinit>() |
|
||||
| methods.kt:5:1:13:1 | Class | methods.kt:6:5:7:5 | classMethod | classMethod(int,int) |
|
||||
| methods.kt:5:1:13:1 | Class | methods.kt:9:5:12:5 | anotherClassMethod | anotherClassMethod(int,int) |
|
||||
| methods2.kt:0:0:0:0 | Methods2Kt | methods2.kt:4:1:5:1 | fooBarTopLevelMethod | fooBarTopLevelMethod(int,int) | public |
|
||||
| methods2.kt:7:1:10:1 | Class2 | methods2.kt:7:1:10:1 | <obinit> | <obinit>() | |
|
||||
| methods2.kt:7:1:10:1 | Class2 | methods2.kt:8:5:9:5 | fooBarClassMethod | fooBarClassMethod(int,int) | public |
|
||||
| methods3.kt:0:0:0:0 | Methods3Kt | methods3.kt:3:1:3:42 | fooBarTopLevelMethodExt | fooBarTopLevelMethodExt(int,int) | public |
|
||||
| methods3.kt:5:1:7:1 | Class3 | methods3.kt:5:1:7:1 | <obinit> | <obinit>() | |
|
||||
| methods3.kt:5:1:7:1 | Class3 | methods3.kt:6:5:6:46 | fooBarTopLevelMethodExt | fooBarTopLevelMethodExt(int,int) | public |
|
||||
| methods4.kt:3:1:11:1 | NestedTest | methods4.kt:3:1:11:1 | <obinit> | <obinit>() | |
|
||||
| methods4.kt:5:3:9:3 | InsideNestedTest | methods4.kt:5:3:9:3 | <obinit> | <obinit>() | |
|
||||
| methods4.kt:5:3:9:3 | InsideNestedTest | methods4.kt:7:5:7:34 | m | m(foo.bar.NestedTest.InsideNestedTest) | public |
|
||||
| methods5.kt:0:0:0:0 | Methods5Kt | methods5.kt:3:1:11:1 | x | x() | public |
|
||||
| methods5.kt:5:3:5:27 | new Object(...) { ... } | methods5.kt:5:3:5:27 | a | a(int) | |
|
||||
| methods5.kt:9:3:9:32 | new Object(...) { ... } | methods5.kt:9:3:9:32 | f1 | f1(foo.bar.C1,int) | |
|
||||
| methods5.kt:13:1:13:14 | C1 | methods5.kt:13:1:13:14 | <obinit> | <obinit>() | |
|
||||
| methods.kt:0:0:0:0 | MethodsKt | methods.kt:2:1:3:1 | topLevelMethod | topLevelMethod(int,int) | public |
|
||||
| methods.kt:5:1:19:1 | Class | methods.kt:5:1:19:1 | <obinit> | <obinit>() | |
|
||||
| methods.kt:5:1:19:1 | Class | methods.kt:6:5:7:5 | classMethod | classMethod(int,int) | public |
|
||||
| methods.kt:5:1:19:1 | Class | methods.kt:9:5:12:5 | anotherClassMethod | anotherClassMethod(int,int) | public |
|
||||
| methods.kt:5:1:19:1 | Class | methods.kt:14:12:14:29 | publicFun | publicFun() | public |
|
||||
| methods.kt:5:1:19:1 | Class | methods.kt:15:15:15:35 | protectedFun | protectedFun() | protected |
|
||||
| methods.kt:5:1:19:1 | Class | methods.kt:16:13:16:31 | privateFun | privateFun() | private |
|
||||
| methods.kt:5:1:19:1 | Class | methods.kt:17:14:17:33 | internalFun | internalFun() | internal |
|
||||
| methods.kt:5:1:19:1 | Class | methods.kt:18:5:18:36 | noExplicitVisibilityFun | noExplicitVisibilityFun() | public |
|
||||
constructors
|
||||
| methods2.kt:7:1:10:1 | Class2 | methods2.kt:7:1:10:1 | Class2 | Class2() |
|
||||
| methods3.kt:5:1:7:1 | Class3 | methods3.kt:5:1:7:1 | Class3 | Class3() |
|
||||
@@ -25,7 +30,7 @@ constructors
|
||||
| methods5.kt:9:3:9:32 | new Object(...) { ... } | methods5.kt:9:3:9:32 | | |
|
||||
| methods5.kt:13:1:13:14 | C1 | methods5.kt:13:1:13:14 | C1 | C1() |
|
||||
| methods5.kt:13:1:13:14 | C1<Integer> | methods5.kt:13:1:13:14 | C1<Integer> | C1<Integer>() |
|
||||
| methods.kt:5:1:13:1 | Class | methods.kt:5:1:13:1 | Class | Class() |
|
||||
| methods.kt:5:1:19:1 | Class | methods.kt:5:1:19:1 | Class | Class() |
|
||||
extensions
|
||||
| methods3.kt:3:1:3:42 | fooBarTopLevelMethodExt | file://:0:0:0:0 | int |
|
||||
| methods3.kt:6:5:6:46 | fooBarTopLevelMethodExt | file://:0:0:0:0 | int |
|
||||
|
||||
@@ -10,5 +10,11 @@ class Class {
|
||||
classMethod(a, 3)
|
||||
topLevelMethod(b, 4)
|
||||
}
|
||||
|
||||
public fun publicFun() {}
|
||||
protected fun protectedFun() {}
|
||||
private fun privateFun() {}
|
||||
internal fun internalFun() {}
|
||||
fun noExplicitVisibilityFun() {}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import java
|
||||
|
||||
query predicate methods(RefType declType, Method m, string signature) { m.fromSource() and declType = m.getDeclaringType() and signature = m.getSignature() }
|
||||
query predicate methods(RefType declType, Method m, string signature, string modifiers) {
|
||||
m.fromSource() and
|
||||
declType = m.getDeclaringType() and
|
||||
signature = m.getSignature() and
|
||||
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() }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user