mirror of
https://github.com/github/codeql.git
synced 2025-12-18 01:33:15 +01:00
Kotlin: Pull more out into KotlinUsesExtractor
This commit is contained in:
@@ -715,6 +715,21 @@ class X {
|
||||
}
|
||||
}
|
||||
|
||||
fun useValueDeclaration(d: IrValueDeclaration): Label<out DbVariable> {
|
||||
when(d) {
|
||||
is IrValueParameter -> {
|
||||
return useValueParameter(d)
|
||||
}
|
||||
is IrVariable -> {
|
||||
return useVariable(d)
|
||||
}
|
||||
else -> {
|
||||
logger.warn(Severity.ErrorSevere, "Unrecognised IrValueDeclaration: " + d.javaClass)
|
||||
return fakeLabel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun erase (t: IrType): IrType {
|
||||
if (t is IrSimpleType) {
|
||||
val classifier = t.classifier
|
||||
@@ -737,6 +752,52 @@ class X {
|
||||
return t
|
||||
}
|
||||
|
||||
fun getValueParameterLabel(vp: IrValueParameter) : String {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val parentId: Label<out DbMethod> = useDeclarationParent(vp.parent) as Label<out DbMethod>
|
||||
var idx = vp.index
|
||||
if (idx < 0) {
|
||||
// We're not extracting this and this@TYPE parameters of functions:
|
||||
logger.warn(Severity.ErrorSevere, "Unexpected negative index for parameter")
|
||||
}
|
||||
val label = "@\"params;{$parentId};$idx\""
|
||||
return label
|
||||
}
|
||||
|
||||
fun useValueParameter(vp: IrValueParameter): Label<out DbParam> {
|
||||
val label = getValueParameterLabel(vp)
|
||||
val id = tw.getLabelFor<DbParam>(label)
|
||||
return id
|
||||
}
|
||||
|
||||
fun getPropertyLabel(p: IrProperty) : String {
|
||||
val parentId = useDeclarationParent(p.parent)
|
||||
val label = "@\"field;{$parentId};${p.name.asString()}\""
|
||||
return label
|
||||
}
|
||||
|
||||
fun useProperty(p: IrProperty): Label<out DbField> {
|
||||
var label = getPropertyLabel(p)
|
||||
val id: Label<DbField> = tw.getLabelFor(label)
|
||||
return id
|
||||
}
|
||||
|
||||
private fun getEnumEntryLabel(ee: IrEnumEntry) : String {
|
||||
val parentId = useDeclarationParent(ee.parent)
|
||||
val label = "@\"field;{$parentId};${ee.name.asString()}\""
|
||||
return label
|
||||
}
|
||||
|
||||
fun useEnumEntry(ee: IrEnumEntry): Label<out DbField> {
|
||||
var label = getEnumEntryLabel(ee)
|
||||
val id: Label<DbField> = tw.getLabelFor(label)
|
||||
return id
|
||||
}
|
||||
|
||||
fun useVariable(v: IrVariable): Label<out DbLocalvar> {
|
||||
return tw.getVariableLabelFor<DbLocalvar>(v)
|
||||
}
|
||||
|
||||
fun withQuestionMark(t: IrType, hasQuestionMark: Boolean) = if(hasQuestionMark) t.makeNullable() else t.makeNotNull()
|
||||
|
||||
}
|
||||
@@ -815,18 +876,6 @@ open class KotlinFileExtractor(
|
||||
return id
|
||||
}
|
||||
|
||||
private fun getValueParameterLabel(vp: IrValueParameter) : String {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val parentId: Label<out DbMethod> = useDeclarationParent(vp.parent) as Label<out DbMethod>
|
||||
var idx = vp.index
|
||||
if (idx < 0) {
|
||||
// We're not extracting this and this@TYPE parameters of functions:
|
||||
logger.warnElement(Severity.ErrorSevere, "Unexpected negative index for parameter", vp)
|
||||
}
|
||||
val label = "@\"params;{$parentId};$idx\""
|
||||
return label
|
||||
}
|
||||
|
||||
private fun isQualifiedThis(vp: IrValueParameter): Boolean {
|
||||
return isQualifiedThisFunction(vp) ||
|
||||
isQualifiedThisClass(vp)
|
||||
@@ -847,12 +896,6 @@ open class KotlinFileExtractor(
|
||||
parent.thisReceiver == vp
|
||||
}
|
||||
|
||||
fun useValueParameter(vp: IrValueParameter): Label<out DbParam> {
|
||||
val label = getValueParameterLabel(vp)
|
||||
val id = tw.getLabelFor<DbParam>(label)
|
||||
return id
|
||||
}
|
||||
|
||||
fun extractValueParameter(vp: IrValueParameter, parent: Label<out DbCallable>, idx: Int) {
|
||||
val id = useValueParameter(vp)
|
||||
val typeId = useTypeOld(vp.type)
|
||||
@@ -964,18 +1007,6 @@ open class KotlinFileExtractor(
|
||||
currentFunction = null
|
||||
}
|
||||
|
||||
private fun getPropertyLabel(p: IrProperty) : String {
|
||||
val parentId = useDeclarationParent(p.parent)
|
||||
val label = "@\"field;{$parentId};${p.name.asString()}\""
|
||||
return label
|
||||
}
|
||||
|
||||
fun useProperty(p: IrProperty): Label<out DbField> {
|
||||
var label = getPropertyLabel(p)
|
||||
val id: Label<DbField> = tw.getLabelFor(label)
|
||||
return id
|
||||
}
|
||||
|
||||
fun extractProperty(p: IrProperty, parentId: Label<out DbReftype>) {
|
||||
val bf = p.backingField
|
||||
if(bf == null) {
|
||||
@@ -989,18 +1020,6 @@ open class KotlinFileExtractor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun getEnumEntryLabel(ee: IrEnumEntry) : String {
|
||||
val parentId = useDeclarationParent(ee.parent)
|
||||
val label = "@\"field;{$parentId};${ee.name.asString()}\""
|
||||
return label
|
||||
}
|
||||
|
||||
fun useEnumEntry(ee: IrEnumEntry): Label<out DbField> {
|
||||
var label = getEnumEntryLabel(ee)
|
||||
val id: Label<DbField> = tw.getLabelFor(label)
|
||||
return id
|
||||
}
|
||||
|
||||
fun extractEnumEntry(ee: IrEnumEntry, parentId: Label<out DbReftype>) {
|
||||
val id = useEnumEntry(ee)
|
||||
val locId = tw.getLocation(ee)
|
||||
@@ -1025,10 +1044,6 @@ open class KotlinFileExtractor(
|
||||
}
|
||||
}
|
||||
|
||||
fun useVariable(v: IrVariable): Label<out DbLocalvar> {
|
||||
return tw.getVariableLabelFor<DbLocalvar>(v)
|
||||
}
|
||||
|
||||
fun extractVariable(v: IrVariable, callable: Label<out DbCallable>, parent: Label<out DbStmtparent>, idx: Int) {
|
||||
val stmtId = tw.getFreshIdLabel<DbLocalvariabledeclstmt>()
|
||||
val locId = tw.getLocation(v)
|
||||
@@ -1066,21 +1081,6 @@ open class KotlinFileExtractor(
|
||||
}
|
||||
}
|
||||
|
||||
fun useValueDeclaration(d: IrValueDeclaration): Label<out DbVariable> {
|
||||
when(d) {
|
||||
is IrValueParameter -> {
|
||||
return useValueParameter(d)
|
||||
}
|
||||
is IrVariable -> {
|
||||
return useVariable(d)
|
||||
}
|
||||
else -> {
|
||||
logger.warnElement(Severity.ErrorSevere, "Unrecognised IrValueDeclaration: " + d.javaClass, d)
|
||||
return fakeLabel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun extractCall(c: IrCall, callable: Label<out DbCallable>, parent: Label<out DbExprparent>, idx: Int) {
|
||||
val exprId: Label<out DbExpr> = when {
|
||||
c.origin == PLUS -> {
|
||||
|
||||
@@ -38,6 +38,18 @@ open class TrapWriter (val lm: TrapLabelManager, val bw: BufferedWriter) {
|
||||
return maybeId
|
||||
}
|
||||
}
|
||||
val variableLabelMapping: MutableMap<IrVariable, Label<out DbLocalvar>> = mutableMapOf<IrVariable, Label<out DbLocalvar>>()
|
||||
fun <T> getVariableLabelFor(v: IrVariable): Label<out DbLocalvar> {
|
||||
val maybeId = variableLabelMapping.get(v)
|
||||
if(maybeId == null) {
|
||||
val id = lm.getFreshLabel<DbLocalvar>()
|
||||
variableLabelMapping.put(v, id)
|
||||
writeTrap("$id = *\n")
|
||||
return id
|
||||
} else {
|
||||
return maybeId
|
||||
}
|
||||
}
|
||||
|
||||
fun getLocation(fileId: Label<DbFile>, startLine: Int, startColumn: Int, endLine: Int, endColumn: Int): Label<DbLocation> {
|
||||
return getLabelFor("@\"loc,{$fileId},$startLine,$startColumn,$endLine,$endColumn\"") {
|
||||
@@ -141,18 +153,6 @@ open class FileTrapWriter (
|
||||
return "file://$filePath:$startLine:$startColumn:$endLine:$endColumn"
|
||||
}
|
||||
}
|
||||
val variableLabelMapping: MutableMap<IrVariable, Label<out DbLocalvar>> = mutableMapOf<IrVariable, Label<out DbLocalvar>>()
|
||||
fun <T> getVariableLabelFor(v: IrVariable): Label<out DbLocalvar> {
|
||||
val maybeId = variableLabelMapping.get(v)
|
||||
if(maybeId == null) {
|
||||
val id = lm.getFreshLabel<DbLocalvar>()
|
||||
variableLabelMapping.put(v, id)
|
||||
writeTrap("$id = *\n")
|
||||
return id
|
||||
} else {
|
||||
return maybeId
|
||||
}
|
||||
}
|
||||
fun <T> getFreshIdLabel(): Label<T> {
|
||||
val label = IntLabel<T>(lm.nextId++)
|
||||
writeTrap("$label = *\n")
|
||||
|
||||
Reference in New Issue
Block a user