Merge pull request #9829 from smowton/smowton/fix/kotlin-underscore-parameter-names

Kotlin: Don't extract a name for a '_' parameter
This commit is contained in:
Chris Smowton
2022-08-10 12:28:26 +01:00
committed by GitHub
13 changed files with 146 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ import com.github.codeql.comments.CommentExtractor
import com.github.codeql.utils.* import com.github.codeql.utils.*
import com.github.codeql.utils.versions.functionN import com.github.codeql.utils.versions.functionN
import com.github.codeql.utils.versions.getIrStubFromDescriptor import com.github.codeql.utils.versions.getIrStubFromDescriptor
import com.github.codeql.utils.versions.isUnderscoreParameter
import com.semmle.extractor.java.OdasaOutput import com.semmle.extractor.java.OdasaOutput
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.backend.common.lower.parents import org.jetbrains.kotlin.backend.common.lower.parents
@@ -642,7 +643,7 @@ open class KotlinFileExtractor(
if (extractTypeAccess) { if (extractTypeAccess) {
extractTypeAccessRecursive(substitutedType, location, id, -1) extractTypeAccessRecursive(substitutedType, location, id, -1)
} }
val syntheticParameterNames = (vp.parent as? IrFunction)?.let { hasSynthesizedParameterNames(it) } ?: true val syntheticParameterNames = isUnderscoreParameter(vp) || ((vp.parent as? IrFunction)?.let { hasSynthesizedParameterNames(it) } ?: true)
return extractValueParameter(id, substitutedType, vp.name.asString(), location, parent, idx, useValueParameter(vp, parentSourceDeclaration), vp.isVararg, syntheticParameterNames) return extractValueParameter(id, substitutedType, vp.name.asString(), location, parent, idx, useValueParameter(vp, parentSourceDeclaration), vp.isVararg, syntheticParameterNames)
} }
} }

View File

@@ -0,0 +1,19 @@
package com.github.codeql.utils.versions
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun isUnderscoreParameter(vp: IrValueParameter) =
try {
DescriptorToSourceUtils.getSourceFromDescriptor(vp.descriptor)
?.safeAs<KtParameter>()?.isSingleUnderscore == true
} catch(e: NotImplementedError) {
// Some kinds of descriptor throw in `getSourceFromDescriptor` as that method is not normally expected to
// be applied to synthetic functions.
false
}

View File

@@ -0,0 +1,19 @@
package com.github.codeql.utils.versions
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun isUnderscoreParameter(vp: IrValueParameter) =
try {
DescriptorToSourceUtils.getSourceFromDescriptor(vp.descriptor)
?.safeAs<KtParameter>()?.isSingleUnderscore == true
} catch(e: NotImplementedError) {
// Some kinds of descriptor throw in `getSourceFromDescriptor` as that method is not normally expected to
// be applied to synthetic functions.
false
}

View File

@@ -0,0 +1,19 @@
package com.github.codeql.utils.versions
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun isUnderscoreParameter(vp: IrValueParameter) =
try {
DescriptorToSourceUtils.getSourceFromDescriptor(vp.descriptor)
?.safeAs<KtParameter>()?.isSingleUnderscore == true
} catch(e: NotImplementedError) {
// Some kinds of descriptor throw in `getSourceFromDescriptor` as that method is not normally expected to
// be applied to synthetic functions.
false
}

View File

@@ -0,0 +1,19 @@
package com.github.codeql.utils.versions
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun isUnderscoreParameter(vp: IrValueParameter) =
try {
DescriptorToSourceUtils.getSourceFromDescriptor(vp.descriptor)
?.safeAs<KtParameter>()?.isSingleUnderscore == true
} catch(e: NotImplementedError) {
// Some kinds of descriptor throw in `getSourceFromDescriptor` as that method is not normally expected to
// be applied to synthetic functions.
false
}

View File

@@ -0,0 +1,19 @@
package com.github.codeql.utils.versions
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun isUnderscoreParameter(vp: IrValueParameter) =
try {
DescriptorToSourceUtils.getSourceFromDescriptor(vp.descriptor)
?.safeAs<KtParameter>()?.isSingleUnderscore == true
} catch(e: NotImplementedError) {
// Some kinds of descriptor throw in `getSourceFromDescriptor` as that method is not normally expected to
// be applied to synthetic functions.
false
}

View File

@@ -0,0 +1,19 @@
package com.github.codeql.utils.versions
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun isUnderscoreParameter(vp: IrValueParameter) =
try {
DescriptorToSourceUtils.getSourceFromDescriptor(vp.descriptor)
?.safeAs<KtParameter>()?.isSingleUnderscore == true
} catch(e: NotImplementedError) {
// Some kinds of descriptor throw in `getSourceFromDescriptor` as that method is not normally expected to
// be applied to synthetic functions.
false
}

View File

@@ -0,0 +1,6 @@
package com.github.codeql.utils.versions
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
fun isUnderscoreParameter(vp: IrValueParameter) = vp.origin == IrDeclarationOrigin.UNDERSCORE_PARAMETER

View File

@@ -0,0 +1,6 @@
package com.github.codeql.utils.versions
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
fun isUnderscoreParameter(vp: IrValueParameter) = vp.origin == IrDeclarationOrigin.UNDERSCORE_PARAMETER

View File

@@ -3583,7 +3583,7 @@ funcExprs.kt:
# 27| 2: [Method] invoke # 27| 2: [Method] invoke
# 27| 3: [TypeAccess] int # 27| 3: [TypeAccess] int
#-----| 4: (Parameters) #-----| 4: (Parameters)
# 27| 0: [Parameter] <anonymous parameter 0> # 27| 0: [Parameter] p0
# 27| 0: [TypeAccess] int # 27| 0: [TypeAccess] int
# 27| 5: [BlockStmt] { ... } # 27| 5: [BlockStmt] { ... }
# 27| 0: [ReturnStmt] return ... # 27| 0: [ReturnStmt] return ...
@@ -3629,9 +3629,9 @@ funcExprs.kt:
# 30| 2: [Method] invoke # 30| 2: [Method] invoke
# 30| 3: [TypeAccess] int # 30| 3: [TypeAccess] int
#-----| 4: (Parameters) #-----| 4: (Parameters)
# 30| 0: [Parameter] <anonymous parameter 0> # 30| 0: [Parameter] p0
# 30| 0: [TypeAccess] int # 30| 0: [TypeAccess] int
# 30| 1: [Parameter] <anonymous parameter 1> # 30| 1: [Parameter] p1
# 30| 0: [TypeAccess] int # 30| 0: [TypeAccess] int
# 30| 5: [BlockStmt] { ... } # 30| 5: [BlockStmt] { ... }
# 30| 0: [ReturnStmt] return ... # 30| 0: [ReturnStmt] return ...
@@ -3652,9 +3652,9 @@ funcExprs.kt:
# 31| 2: [Method] invoke # 31| 2: [Method] invoke
# 31| 3: [TypeAccess] int # 31| 3: [TypeAccess] int
#-----| 4: (Parameters) #-----| 4: (Parameters)
# 31| 0: [Parameter] <anonymous parameter 0> # 31| 0: [Parameter] p0
# 31| 0: [TypeAccess] int # 31| 0: [TypeAccess] int
# 31| 1: [Parameter] <anonymous parameter 1> # 31| 1: [Parameter] p1
# 31| 0: [TypeAccess] int # 31| 0: [TypeAccess] int
# 31| 5: [BlockStmt] { ... } # 31| 5: [BlockStmt] { ... }
# 31| 0: [ReturnStmt] return ... # 31| 0: [ReturnStmt] return ...

View File

@@ -0,0 +1 @@
| test.kt:5:9:5:9 | p0 | p0 |

View File

@@ -0,0 +1,7 @@
class A {
var x: Int
get() = 1
set(_) { }
}

View File

@@ -0,0 +1,5 @@
import java
from Parameter p
where p.getCallable().fromSource()
select p, p.getName()