Skip to content

Commit 212143f

Browse files
authored
Merge pull request #17881 from tamasvajk/ke2-safe-qualified-expr
KE2: Extract safe qualified expressions
2 parents 71931c3 + 84166e8 commit 212143f

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ private fun KotlinFileExtractor.extractExpression(
344344
extractExpression(e.baseExpression!!, callable, parent)
345345
}
346346

347-
is KtDotQualifiedExpression -> {
347+
is KtQualifiedExpression -> {
348348
// We're propagating the extraction to the child, and then getting the qualifier from the parent of the
349349
// child. The selector could be many expression kind, such as KtCallExpression, KtReferenceExpression,
350350
// and each of those would need to look for the qualifier

java/kotlin-extractor2/src/main/kotlin/entities/MethodCall.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import org.jetbrains.kotlin.analysis.api.resolution.symbol
66
import org.jetbrains.kotlin.analysis.api.symbols.KaFunctionSymbol
77
import org.jetbrains.kotlin.analysis.api.types.KaType
88
import org.jetbrains.kotlin.psi.KtCallExpression
9-
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
109
import org.jetbrains.kotlin.psi.KtExpression
10+
import org.jetbrains.kotlin.psi.KtQualifiedExpression
11+
import org.jetbrains.kotlin.psi.KtSafeQualifiedExpression
1112
import org.jetbrains.kotlin.utils.mapToIndex
1213

1314
context(KaSession)
@@ -43,14 +44,15 @@ fun KotlinFileExtractor.extractMethodCall(
4344
.sortedBy { p -> p.first }
4445
.map { p -> p.second }
4546

46-
// TODO: fix getting the qualifier, we should handle safe qualified expressions too
47-
val qualifier: KtExpression? = (call.parent as? KtDotQualifiedExpression)?.receiverExpression
47+
val callQualifiedParent = call.parent as? KtQualifiedExpression
48+
val qualifier =
49+
if (callQualifiedParent?.selectorExpression == call) callQualifiedParent.receiverExpression else null
4850
val extensionReceiver = if (target.isExtension) qualifier else null
4951
val dispatchReceiver = if (!target.isExtension) qualifier else null
5052

5153
val exprParent = stmtExprParent.expr(call, enclosingCallable)
5254

53-
extractRawMethodAccess(
55+
val callId = extractRawMethodAccess(
5456
target,
5557
tw.getLocation(call),
5658
call.expressionType!!,
@@ -62,6 +64,10 @@ fun KotlinFileExtractor.extractMethodCall(
6264
extensionReceiver,
6365
args
6466
)
67+
68+
if (call.parent is KtSafeQualifiedExpression) {
69+
tw.writeKtSafeAccess(callId)
70+
}
6571
}
6672

6773
context(KaSession)
@@ -138,7 +144,7 @@ private fun KotlinFileExtractor.extractRawMethodAccess(
138144
extractClassTypeArguments: Boolean = false,
139145
superQualifierSymbol: IrClassSymbol? = null
140146
*/
141-
) {
147+
): Label<DbMethodaccess> {
142148
/* OLD KE1:
143149
val callTarget = getCalleeRealOverrideTarget(syntacticCallTarget)
144150
val methodId = getCalleeMethodId(callTarget, drType, extractClassTypeArguments)
@@ -229,6 +235,8 @@ private fun KotlinFileExtractor.extractRawMethodAccess(
229235
}
230236
val idxOffset = if (extensionReceiver != null) 1 else 0
231237
extractCallValueArguments(id, valueArguments, enclosingStmt, enclosingCallable, idxOffset)
238+
239+
return id
232240
}
233241

234242
context(KaSession)

java/ql/lib/config/semmlecode.dbscheme

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,3 +1528,10 @@ ktFunctionOriginalNames(
15281528
ktDataClasses(
15291529
unique int id: @classorinterface ref
15301530
)
1531+
1532+
// not all variables are qualifiable, but adding fieldaccess would be more involved:
1533+
@qualifiableaccess = @varaccess | @methodaccess;
1534+
1535+
ktSafeAccess(
1536+
unique int id: @qualifiableaccess ref
1537+
)

java/ql/lib/semmle/code/java/Expr.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,6 +2360,9 @@ private module Qualifier {
23602360
result = this.(FieldAccess).getQualifier() or
23612361
result = this.(MethodCall).getQualifier()
23622362
}
2363+
2364+
/** Holds if this member access is a `?.` safe qualified expression in Kotlin. */
2365+
predicate safeAccess() { ktSafeAccess(this) }
23632366
}
23642367

23652368
/**

0 commit comments

Comments
 (0)