Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KE2: Extract safe qualified expressions #17881

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading