mirror of
https://github.com/github/codeql.git
synced 2026-05-24 16:17:07 +02:00
Merge pull request #17881 from tamasvajk/ke2-safe-qualified-expr
KE2: Extract safe qualified expressions
This commit is contained in:
@@ -344,7 +344,7 @@ private fun KotlinFileExtractor.extractExpression(
|
||||
extractExpression(e.baseExpression!!, callable, parent)
|
||||
}
|
||||
|
||||
is KtDotQualifiedExpression -> {
|
||||
is KtQualifiedExpression -> {
|
||||
// We're propagating the extraction to the child, and then getting the qualifier from the parent of the
|
||||
// child. The selector could be many expression kind, such as KtCallExpression, KtReferenceExpression,
|
||||
// and each of those would need to look for the qualifier
|
||||
|
||||
@@ -6,8 +6,9 @@ import org.jetbrains.kotlin.analysis.api.resolution.symbol
|
||||
import org.jetbrains.kotlin.analysis.api.symbols.KaFunctionSymbol
|
||||
import org.jetbrains.kotlin.analysis.api.types.KaType
|
||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
||||
import org.jetbrains.kotlin.psi.KtExpression
|
||||
import org.jetbrains.kotlin.psi.KtQualifiedExpression
|
||||
import org.jetbrains.kotlin.psi.KtSafeQualifiedExpression
|
||||
import org.jetbrains.kotlin.utils.mapToIndex
|
||||
|
||||
context(KaSession)
|
||||
@@ -43,14 +44,15 @@ fun KotlinFileExtractor.extractMethodCall(
|
||||
.sortedBy { p -> p.first }
|
||||
.map { p -> p.second }
|
||||
|
||||
// TODO: fix getting the qualifier, we should handle safe qualified expressions too
|
||||
val qualifier: KtExpression? = (call.parent as? KtDotQualifiedExpression)?.receiverExpression
|
||||
val callQualifiedParent = call.parent as? KtQualifiedExpression
|
||||
val qualifier =
|
||||
if (callQualifiedParent?.selectorExpression == call) callQualifiedParent.receiverExpression else null
|
||||
val extensionReceiver = if (target.isExtension) qualifier else null
|
||||
val dispatchReceiver = if (!target.isExtension) qualifier else null
|
||||
|
||||
val exprParent = stmtExprParent.expr(call, enclosingCallable)
|
||||
|
||||
extractRawMethodAccess(
|
||||
val callId = extractRawMethodAccess(
|
||||
target,
|
||||
tw.getLocation(call),
|
||||
call.expressionType!!,
|
||||
@@ -62,6 +64,10 @@ fun KotlinFileExtractor.extractMethodCall(
|
||||
extensionReceiver,
|
||||
args
|
||||
)
|
||||
|
||||
if (call.parent is KtSafeQualifiedExpression) {
|
||||
tw.writeKtSafeAccess(callId)
|
||||
}
|
||||
}
|
||||
|
||||
context(KaSession)
|
||||
@@ -138,7 +144,7 @@ private fun KotlinFileExtractor.extractRawMethodAccess(
|
||||
extractClassTypeArguments: Boolean = false,
|
||||
superQualifierSymbol: IrClassSymbol? = null
|
||||
*/
|
||||
) {
|
||||
): Label<DbMethodaccess> {
|
||||
/* OLD KE1:
|
||||
val callTarget = getCalleeRealOverrideTarget(syntacticCallTarget)
|
||||
val methodId = getCalleeMethodId(callTarget, drType, extractClassTypeArguments)
|
||||
@@ -229,6 +235,8 @@ private fun KotlinFileExtractor.extractRawMethodAccess(
|
||||
}
|
||||
val idxOffset = if (extensionReceiver != null) 1 else 0
|
||||
extractCallValueArguments(id, valueArguments, enclosingStmt, enclosingCallable, idxOffset)
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
context(KaSession)
|
||||
|
||||
@@ -1528,3 +1528,10 @@ ktFunctionOriginalNames(
|
||||
ktDataClasses(
|
||||
unique int id: @classorinterface ref
|
||||
)
|
||||
|
||||
// not all variables are qualifiable, but adding fieldaccess would be more involved:
|
||||
@qualifiableaccess = @varaccess | @methodaccess;
|
||||
|
||||
ktSafeAccess(
|
||||
unique int id: @qualifiableaccess ref
|
||||
)
|
||||
|
||||
@@ -2360,6 +2360,9 @@ private module Qualifier {
|
||||
result = this.(FieldAccess).getQualifier() or
|
||||
result = this.(MethodCall).getQualifier()
|
||||
}
|
||||
|
||||
/** Holds if this member access is a `?.` safe qualified expression in Kotlin. */
|
||||
predicate safeAccess() { ktSafeAccess(this) }
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user