mirror of
https://github.com/github/codeql.git
synced 2026-05-02 20:25:13 +02:00
Kotlin: extract type parameters
This commit is contained in:
@@ -302,6 +302,21 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
|
||||
return tw.getLabelFor(getTypeParameterLabel(param))
|
||||
}
|
||||
|
||||
private fun extractTypeParameter(tp: IrTypeParameter, optParentid: Optional<Label<out DbTypeorcallable>>) {
|
||||
val id = useTypeParameter(tp)
|
||||
|
||||
if (!optParentid.isPresent) {
|
||||
logger.warnElement(Severity.ErrorSevere, "Couldn't find expected parent of type parameter.", tp)
|
||||
return
|
||||
}
|
||||
|
||||
tw.writeTypeVars(id, tp.name.asString(), tp.index, 0, optParentid.get())
|
||||
val locId = tw.getLocation(tp)
|
||||
tw.writeHasLocation(id, locId)
|
||||
|
||||
// todo: add type bounds
|
||||
}
|
||||
|
||||
private fun getClassLabel(c: IrClass): String {
|
||||
val pkg = c.packageFqName?.asString() ?: ""
|
||||
val cls = c.name.asString()
|
||||
@@ -365,6 +380,9 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.typeParameters.map { extractTypeParameter(it, Optional.of(id)) }
|
||||
|
||||
c.declarations.map { extractDeclaration(it, Optional.of(id)) }
|
||||
|
||||
extractObjectInitializerFunction(c, id)
|
||||
@@ -552,6 +570,9 @@ class KotlinFileExtractor(val logger: FileLogger, val tw: FileTrapWriter, val fi
|
||||
val extendedType = useType(extReceiver.type)
|
||||
tw.writeKtExtensionFunctions(id, extendedType)
|
||||
}
|
||||
|
||||
f.typeParameters.map { extractTypeParameter(it, Optional.of(id)) }
|
||||
|
||||
currentFunction = null
|
||||
}
|
||||
|
||||
|
||||
10
java/ql/test/kotlin/library-tests/generics/generics.kt
Normal file
10
java/ql/test/kotlin/library-tests/generics/generics.kt
Normal file
@@ -0,0 +1,10 @@
|
||||
package foo.bar
|
||||
|
||||
fun <S> Int.f(s: S): S {
|
||||
return s
|
||||
}
|
||||
|
||||
class C1<T>(val t: T) {
|
||||
fun f1(t: T) {}
|
||||
fun <U> f2(u: U) {}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
genericType
|
||||
| generics.kt:7:10:7:10 | T | generics.kt:7:1:10:1 | C1 |
|
||||
genericFunction
|
||||
| generics.kt:3:6:3:6 | S | generics.kt:3:1:5:1 | f |
|
||||
| generics.kt:9:10:9:10 | U | generics.kt:9:5:9:23 | f2 |
|
||||
@@ -0,0 +1,5 @@
|
||||
import java
|
||||
|
||||
query predicate genericType(TypeVariable tv, RefType rt) { tv.getGenericType() = rt }
|
||||
|
||||
query predicate genericFunction(TypeVariable tv, GenericCallable c) { tv.getGenericCallable() = c }
|
||||
Reference in New Issue
Block a user