Skip to content

Commit

Permalink
KE2: Extract safe qualified expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasvajk committed Oct 31, 2024
1 parent f57fe71 commit 84166e8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions java/kotlin-extractor2/src/main/kotlin/entities/MethodCall.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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!!,
Expand All @@ -62,6 +64,10 @@ fun KotlinFileExtractor.extractMethodCall(
extensionReceiver,
args
)

if (call.parent is KtSafeQualifiedExpression) {
tw.writeKtSafeAccess(callId)
}
}

context(KaSession)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions java/ql/lib/config/semmlecode.dbscheme
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
3 changes: 3 additions & 0 deletions java/ql/lib/semmle/code/java/Expr.qll
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}

/**
Expand Down

0 comments on commit 84166e8

Please sign in to comment.