Merge pull request #11064 from smowton/smowton/fix/kotlin-inherited-defaults

Kotlin: handle default parameter values inherited from an overridden function
This commit is contained in:
Chris Smowton
2022-11-02 18:13:14 +00:00
committed by GitHub
4 changed files with 38 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ package com.github.codeql
import com.github.codeql.comments.CommentExtractor
import com.github.codeql.utils.*
import com.github.codeql.utils.versions.allOverriddenIncludingSelf
import com.github.codeql.utils.versions.functionN
import com.github.codeql.utils.versions.isUnderscoreParameter
import com.semmle.extractor.java.OdasaOutput
@@ -1847,16 +1848,27 @@ open class KotlinFileExtractor(
// Ensure the real target gets extracted, as we might not every directly touch it thanks to this call being redirected to a $default method.
useFunction<DbCallable>(callTarget)
}
val defaultMethodLabel = getDefaultsMethodLabel(callTarget)
val id = extractMethodAccessWithoutArgs(resultType, locId, enclosingCallable, callsiteParent, childIdx, enclosingStmt, defaultMethodLabel)
if (callTarget.isLocalFunction()) {
extractTypeAccess(getLocallyVisibleFunctionLabels(callTarget).type, locId, id, -1, enclosingCallable, enclosingStmt)
} else {
extractStaticTypeAccessQualifierUnchecked(callTarget.parent, id, locId, enclosingCallable, enclosingStmt)
// Default parameter values are inherited by overrides; in this case the call should dispatch against the $default method belonging to the class
// that specified the default values, which will in turn dynamically dispatch back to the relevant override.
val overriddenCallTarget = (callTarget as? IrSimpleFunction)?.allOverriddenIncludingSelf()?.firstOrNull {
it.overriddenSymbols.isEmpty() && it.valueParameters.any { p -> p.defaultValue != null }
} ?: callTarget
if (isExternalDeclaration(overriddenCallTarget)) {
// Likewise, ensure the overridden target gets extracted.
useFunction<DbCallable>(overriddenCallTarget)
}
extractDefaultsCallArguments(id, callTarget, enclosingCallable, enclosingStmt, valueArguments, dispatchReceiver, extensionReceiver)
val defaultMethodLabel = getDefaultsMethodLabel(overriddenCallTarget)
val id = extractMethodAccessWithoutArgs(resultType, locId, enclosingCallable, callsiteParent, childIdx, enclosingStmt, defaultMethodLabel)
if (overriddenCallTarget.isLocalFunction()) {
extractTypeAccess(getLocallyVisibleFunctionLabels(overriddenCallTarget).type, locId, id, -1, enclosingCallable, enclosingStmt)
} else {
extractStaticTypeAccessQualifierUnchecked(overriddenCallTarget.parent, id, locId, enclosingCallable, enclosingStmt)
}
extractDefaultsCallArguments(id, overriddenCallTarget, enclosingCallable, enclosingStmt, valueArguments, dispatchReceiver, extensionReceiver)
}
private fun extractDefaultsCallArguments(