mirror of
https://github.com/github/codeql.git
synced 2026-04-29 10:45:15 +02:00
Allow specialised instances of anonymous classes
This commit is contained in:
@@ -583,12 +583,7 @@ open class KotlinFileExtractor(
|
||||
var parent: IrDeclarationParent? = declarationParent
|
||||
while (parent != null) {
|
||||
if (parent is IrClass) {
|
||||
val parentId =
|
||||
if (parent.isAnonymousObject) {
|
||||
useAnonymousClass(parent).javaResult.id.cast<DbClass>()
|
||||
} else {
|
||||
useClassInstance(parent, parentClassTypeArguments).typeResult.id
|
||||
}
|
||||
val parentId = useClassInstance(parent, parentClassTypeArguments).typeResult.id
|
||||
tw.writeEnclInReftype(innerId, parentId)
|
||||
if (innerClass != null && innerClass.isCompanion) {
|
||||
// If we are a companion then our parent has a
|
||||
@@ -866,7 +861,7 @@ open class KotlinFileExtractor(
|
||||
extractTypeAccess(useType(paramType), locId, paramId, -1)
|
||||
}
|
||||
}
|
||||
val paramsSignature = allParamTypeResults.joinToString(separator = ",", prefix = "(", postfix = ")") { it.javaResult.signature }
|
||||
val paramsSignature = allParamTypeResults.joinToString(separator = ",", prefix = "(", postfix = ")") { signatureOrWarn(it.javaResult, f) }
|
||||
val shortName = getDefaultsMethodName(f)
|
||||
|
||||
if (f.symbol is IrConstructorSymbol) {
|
||||
@@ -1076,6 +1071,9 @@ open class KotlinFileExtractor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun signatureOrWarn(t: TypeResult<*>, associatedElement: IrElement) =
|
||||
t.signature ?: "<signature unavailable>".also { logger.warnElement("Needed a signature for a type that doesn't have one", associatedElement) }
|
||||
|
||||
private fun forceExtractFunction(f: IrFunction, parentId: Label<out DbReftype>, extractBody: Boolean, extractMethodAndParameterTypeAccesses: Boolean, typeSubstitution: TypeSubstitution?, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?, extractOrigin: Boolean = true, overriddenAttributes: OverriddenFunctionAttributes? = null): Label<out DbCallable> {
|
||||
with("function", f) {
|
||||
DeclarationStackAdjuster(f, overriddenAttributes).use {
|
||||
@@ -1112,7 +1110,7 @@ open class KotlinFileExtractor(
|
||||
paramTypes
|
||||
}
|
||||
|
||||
val paramsSignature = allParamTypes.joinToString(separator = ",", prefix = "(", postfix = ")") { it.javaResult.signature }
|
||||
val paramsSignature = allParamTypes.joinToString(separator = ",", prefix = "(", postfix = ")") { signatureOrWarn(it.javaResult, f) }
|
||||
|
||||
val adjustedReturnType = addJavaLoweringWildcards(getAdjustedReturnType(f), false, (javaCallable as? JavaMethod)?.returnType)
|
||||
val substReturnType = typeSubstitution?.let { it(adjustedReturnType, TypeContext.RETURN, pluginContext) } ?: adjustedReturnType
|
||||
@@ -2933,20 +2931,8 @@ open class KotlinFileExtractor(
|
||||
logger.errorElement("Constructor call has non-simple type ${eType.javaClass}", e)
|
||||
return
|
||||
}
|
||||
val type = useType(eType)
|
||||
val isAnonymous = eType.isAnonymous
|
||||
val type: TypeResults = if (isAnonymous) {
|
||||
if (e.typeArgumentsCount > 0) {
|
||||
logger.warnElement("Unexpected type arguments (${e.typeArgumentsCount}) for anonymous class constructor call", e)
|
||||
}
|
||||
val c = eType.classifier.owner
|
||||
if (c !is IrClass) {
|
||||
logger.errorElement("Anonymous constructor call type not a class (${c.javaClass})", e)
|
||||
return
|
||||
}
|
||||
useAnonymousClass(c)
|
||||
} else {
|
||||
useType(eType)
|
||||
}
|
||||
val locId = tw.getLocation(e)
|
||||
val valueArgs = (0 until e.valueArgumentsCount).map { e.getValueArgument(it) }
|
||||
// For now, don't try to use default methods for enum constructor calls,
|
||||
@@ -2963,7 +2949,7 @@ open class KotlinFileExtractor(
|
||||
}
|
||||
|
||||
if (isAnonymous) {
|
||||
tw.writeIsAnonymClass(type.javaResult.id.cast<DbClass>(), id)
|
||||
tw.writeIsAnonymClass(type.javaResult.id.cast(), id)
|
||||
}
|
||||
|
||||
val dr = e.dispatchReceiver
|
||||
@@ -4614,7 +4600,7 @@ open class KotlinFileExtractor(
|
||||
Pair(paramId, paramType)
|
||||
}
|
||||
|
||||
val paramsSignature = parameters.joinToString(separator = ",", prefix = "(", postfix = ")") { it.second.javaResult.signature }
|
||||
val paramsSignature = parameters.joinToString(separator = ",", prefix = "(", postfix = ")") { signatureOrWarn(it.second.javaResult, declarationStack.peek().first) }
|
||||
|
||||
val rt = useType(returnType, TypeContext.RETURN)
|
||||
tw.writeMethods(methodId, name, "$name$paramsSignature", rt.javaResult.id, parentId, methodId)
|
||||
|
||||
@@ -210,10 +210,6 @@ open class KotlinUsesExtractor(
|
||||
// `typeArgs` can be null to describe a raw generic type.
|
||||
// For non-generic types it will be zero-length list.
|
||||
fun useClassInstance(c: IrClass, typeArgs: List<IrTypeArgument>?, inReceiverContext: Boolean = false): UseClassInstanceResult {
|
||||
if (c.isAnonymousObject) {
|
||||
logger.error("Unexpected access to anonymous class instance")
|
||||
}
|
||||
|
||||
val substituteClass = getJavaEquivalentClass(c)
|
||||
|
||||
val extractClass = substituteClass ?: c
|
||||
@@ -418,10 +414,11 @@ open class KotlinUsesExtractor(
|
||||
}
|
||||
|
||||
val fqName = replacedClass.fqNameWhenAvailable
|
||||
val signature = if (fqName == null) {
|
||||
val signature = if (replacedClass.isAnonymousObject) {
|
||||
null
|
||||
} else if (fqName == null) {
|
||||
logger.error("Unable to find signature/fqName for ${replacedClass.name}")
|
||||
// TODO: Should we return null here instead?
|
||||
"<no signature available>"
|
||||
null
|
||||
} else {
|
||||
fqName.asString()
|
||||
}
|
||||
@@ -497,16 +494,6 @@ open class KotlinUsesExtractor(
|
||||
// `args` can be null to describe a raw generic type.
|
||||
// For non-generic types it will be zero-length list.
|
||||
fun useSimpleTypeClass(c: IrClass, args: List<IrTypeArgument>?, hasQuestionMark: Boolean): TypeResults {
|
||||
if (c.isAnonymousObject) {
|
||||
args?.let {
|
||||
if (it.isNotEmpty() && !isUnspecialised(c, it, logger)) {
|
||||
logger.error("Unexpected specialised instance of generic anonymous class")
|
||||
}
|
||||
}
|
||||
|
||||
return useAnonymousClass(c)
|
||||
}
|
||||
|
||||
val classInstanceResult = useClassInstance(c, args)
|
||||
val javaClassId = classInstanceResult.typeResult.id
|
||||
val kotlinQualClassName = getUnquotedClassLabel(c, args).classLabel
|
||||
@@ -795,7 +782,7 @@ open class KotlinUsesExtractor(
|
||||
extractFileClass(dp)
|
||||
}
|
||||
is IrClass ->
|
||||
if (classTypeArguments != null && !dp.isAnonymousObject) {
|
||||
if (classTypeArguments != null) {
|
||||
useClassInstance(dp, classTypeArguments, inReceiverContext).typeResult.id
|
||||
} else {
|
||||
val replacedType = tryReplaceParcelizeRawType(dp)
|
||||
@@ -1411,20 +1398,24 @@ open class KotlinUsesExtractor(
|
||||
private fun getUnquotedClassLabel(c: IrClass, argsIncludingOuterClasses: List<IrTypeArgument>?): ClassLabelResults {
|
||||
val pkg = c.packageFqName?.asString() ?: ""
|
||||
val cls = c.name.asString()
|
||||
val label = when (val parent = c.parent) {
|
||||
is IrClass -> {
|
||||
"${getUnquotedClassLabel(parent, listOf()).classLabel}\$$cls"
|
||||
}
|
||||
is IrFunction -> {
|
||||
"{${useFunction<DbMethod>(parent)}}.$cls"
|
||||
}
|
||||
is IrField -> {
|
||||
"{${useField(parent)}}.$cls"
|
||||
}
|
||||
else -> {
|
||||
if (pkg.isEmpty()) cls else "$pkg.$cls"
|
||||
}
|
||||
}
|
||||
val label =
|
||||
if (c.isAnonymousObject)
|
||||
"{${useAnonymousClass(c).javaResult.id}}"
|
||||
else
|
||||
when (val parent = c.parent) {
|
||||
is IrClass -> {
|
||||
"${getUnquotedClassLabel(parent, listOf()).classLabel}\$$cls"
|
||||
}
|
||||
is IrFunction -> {
|
||||
"{${useFunction<DbMethod>(parent)}}.$cls"
|
||||
}
|
||||
is IrField -> {
|
||||
"{${useField(parent)}}.$cls"
|
||||
}
|
||||
else -> {
|
||||
if (pkg.isEmpty()) cls else "$pkg.$cls"
|
||||
}
|
||||
}
|
||||
|
||||
val reorderedArgs = orderTypeArgsLeftToRight(c, argsIncludingOuterClasses)
|
||||
val typeArgLabels = reorderedArgs?.map { getTypeArgumentLabel(it) }
|
||||
@@ -1435,20 +1426,17 @@ open class KotlinUsesExtractor(
|
||||
""
|
||||
else
|
||||
typeArgLabels.takeLast(c.typeParameters.size).joinToString(prefix = "<", postfix = ">", separator = ",") { it.shortName }
|
||||
val shortNamePrefix = if (c.isAnonymousObject) "" else cls
|
||||
|
||||
return ClassLabelResults(
|
||||
label + (typeArgLabels?.joinToString(separator = "") { ";{${it.id}}" } ?: "<>"),
|
||||
cls + typeArgsShortName
|
||||
shortNamePrefix + typeArgsShortName
|
||||
)
|
||||
}
|
||||
|
||||
// `args` can be null to describe a raw generic type.
|
||||
// For non-generic types it will be zero-length list.
|
||||
fun getClassLabel(c: IrClass, argsIncludingOuterClasses: List<IrTypeArgument>?): ClassLabelResults {
|
||||
if (c.isAnonymousObject) {
|
||||
logger.error("Label generation should not be requested for an anonymous class")
|
||||
}
|
||||
|
||||
val unquotedLabel = getUnquotedClassLabel(c, argsIncludingOuterClasses)
|
||||
return ClassLabelResults(
|
||||
"@\"class;${unquotedLabel.classLabel}\"",
|
||||
@@ -1456,10 +1444,6 @@ open class KotlinUsesExtractor(
|
||||
}
|
||||
|
||||
fun useClassSource(c: IrClass): Label<out DbClassorinterface> {
|
||||
if (c.isAnonymousObject) {
|
||||
return useAnonymousClass(c).javaResult.id.cast<DbClass>()
|
||||
}
|
||||
|
||||
// For source classes, the label doesn't include any type arguments
|
||||
val classTypeResult = addClassLabel(c, listOf())
|
||||
return classTypeResult.id
|
||||
|
||||
@@ -12,7 +12,7 @@ package com.github.codeql
|
||||
* `shortName` is a Java primitive name (e.g. "int"), a class short name with Java-style type arguments ("InnerClass<E>" or
|
||||
* "OuterClass<ConcreteArgument>" or "OtherClass<? extends Bound>") or an array ("componentShortName[]").
|
||||
*/
|
||||
data class TypeResultGeneric<SignatureType,out LabelType: AnyDbType>(val id: Label<out LabelType>, val signature: SignatureType, val shortName: String) {
|
||||
data class TypeResultGeneric<SignatureType,out LabelType: AnyDbType>(val id: Label<out LabelType>, val signature: SignatureType?, val shortName: String) {
|
||||
fun <U: AnyDbType> cast(): TypeResultGeneric<SignatureType,U> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return this as TypeResultGeneric<SignatureType,U>
|
||||
|
||||
@@ -686,7 +686,7 @@ class SrcRefType extends RefType {
|
||||
/** A class declaration. */
|
||||
class Class extends ClassOrInterface, @class {
|
||||
/** Holds if this class is an anonymous class. */
|
||||
predicate isAnonymous() { isAnonymClass(this, _) }
|
||||
predicate isAnonymous() { isAnonymClass(this.getSourceDeclaration(), _) }
|
||||
|
||||
override RefType getSourceDeclaration() { classes(this, _, _, result) }
|
||||
|
||||
@@ -800,10 +800,13 @@ class AnonymousClass extends NestedClass {
|
||||
}
|
||||
|
||||
/** Gets the class instance expression where this anonymous class occurs. */
|
||||
ClassInstanceExpr getClassInstanceExpr() { isAnonymClass(this, result) }
|
||||
ClassInstanceExpr getClassInstanceExpr() { isAnonymClass(this.getSourceDeclaration(), result) }
|
||||
|
||||
override string toString() {
|
||||
result = "new " + this.getClassInstanceExpr().getTypeName() + "(...) { ... }"
|
||||
// Include super.toString, i.e. the name given in the database, because for Kotlin anonymous
|
||||
// classes we can get specialisations of anonymous generic types, and this will supply the
|
||||
// trailing type arguments.
|
||||
result = "new " + this.getClassInstanceExpr().getTypeName() + "(...) { ... }" + super.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,15 +1,33 @@
|
||||
enclosingTypes
|
||||
| file:///!unknown-binary-location/A$<no name provided>.class:0:0:0:0 | new If<T>(...) { ... }<> | file:///!unknown-binary-location/A.class:0:0:0:0 | A<CharSequence> |
|
||||
| file:///!unknown-binary-location/A$<no name provided>.class:0:0:0:0 | new If<T>(...) { ... }<> | file:///!unknown-binary-location/A.class:0:0:0:0 | A<String> |
|
||||
| test.kt:9:18:11:3 | new If<T>(...) { ... } | test.kt:7:1:22:1 | A |
|
||||
| test.kt:13:33:15:3 | new If<T>(...) { ... } | test.kt:7:1:22:1 | A |
|
||||
| test.kt:13:33:15:3 | new If<T>(...) { ... }<> | test.kt:7:1:22:1 | A<> |
|
||||
#select
|
||||
| file:///!unknown-binary-location/A$<no name provided>.class:0:0:0:0 | new If<T>(...) { ... }<> | file:///!unknown-binary-location/A$<no name provided>.class:0:0:0:0 | |
|
||||
| file:///!unknown-binary-location/A$<no name provided>.class:0:0:0:0 | new If<T>(...) { ... }<> | file:///!unknown-binary-location/A$<no name provided>.class:0:0:0:0 | |
|
||||
| file:///!unknown-binary-location/A$<no name provided>.class:0:0:0:0 | new If<T>(...) { ... }<> | file:///!unknown-binary-location/A$<no name provided>.class:0:0:0:0 | getX |
|
||||
| file:///!unknown-binary-location/A$<no name provided>.class:0:0:0:0 | new If<T>(...) { ... }<> | file:///!unknown-binary-location/A$<no name provided>.class:0:0:0:0 | getX |
|
||||
| file:///!unknown-binary-location/A.class:0:0:0:0 | A<CharSequence> | file:///!unknown-binary-location/A.class:0:0:0:0 | A<CharSequence> |
|
||||
| file:///!unknown-binary-location/A.class:0:0:0:0 | A<CharSequence> | file:///!unknown-binary-location/A.class:0:0:0:0 | getAnonType |
|
||||
| file:///!unknown-binary-location/A.class:0:0:0:0 | A<CharSequence> | file:///!unknown-binary-location/A.class:0:0:0:0 | getPrivateAnonType$private |
|
||||
| file:///!unknown-binary-location/A.class:0:0:0:0 | A<CharSequence> | file:///!unknown-binary-location/A.class:0:0:0:0 | privateUser |
|
||||
| file:///!unknown-binary-location/A.class:0:0:0:0 | A<String> | file:///!unknown-binary-location/A.class:0:0:0:0 | A<String> |
|
||||
| file:///!unknown-binary-location/A.class:0:0:0:0 | A<String> | file:///!unknown-binary-location/A.class:0:0:0:0 | getAnonType |
|
||||
| file:///!unknown-binary-location/A.class:0:0:0:0 | A<String> | file:///!unknown-binary-location/A.class:0:0:0:0 | getPrivateAnonType$private |
|
||||
| file:///!unknown-binary-location/A.class:0:0:0:0 | A<String> | file:///!unknown-binary-location/A.class:0:0:0:0 | privateUser |
|
||||
| file:///!unknown-binary-location/If.class:0:0:0:0 | If<String> | file:///!unknown-binary-location/If.class:0:0:0:0 | getX |
|
||||
| file:///!unknown-binary-location/If.class:0:0:0:0 | If<T> | file:///!unknown-binary-location/If.class:0:0:0:0 | getX |
|
||||
| other.kt:1:1:1:34 | Ext | other.kt:1:1:1:34 | Ext |
|
||||
| test.kt:0:0:0:0 | TestKt | test.kt:19:1:19:38 | user |
|
||||
| test.kt:0:0:0:0 | TestKt | test.kt:24:1:24:38 | user |
|
||||
| test.kt:1:1:5:1 | If | test.kt:3:3:3:11 | getX |
|
||||
| test.kt:7:1:17:1 | A | test.kt:7:6:17:1 | A |
|
||||
| test.kt:7:1:17:1 | A | test.kt:9:3:11:3 | anonType |
|
||||
| test.kt:7:1:17:1 | A | test.kt:9:3:11:3 | getAnonType |
|
||||
| test.kt:7:1:17:1 | A | test.kt:13:3:15:3 | privateAnonType |
|
||||
| test.kt:7:1:17:1 | A | test.kt:13:11:15:3 | getPrivateAnonType$private |
|
||||
| test.kt:7:1:22:1 | A | test.kt:7:6:22:1 | A |
|
||||
| test.kt:7:1:22:1 | A | test.kt:9:3:11:3 | anonType |
|
||||
| test.kt:7:1:22:1 | A | test.kt:9:3:11:3 | getAnonType |
|
||||
| test.kt:7:1:22:1 | A | test.kt:13:3:15:3 | privateAnonType |
|
||||
| test.kt:7:1:22:1 | A | test.kt:13:11:15:3 | getPrivateAnonType$private |
|
||||
| test.kt:7:1:22:1 | A | test.kt:17:3:20:3 | privateUser |
|
||||
| test.kt:9:18:11:3 | new If<T>(...) { ... } | test.kt:9:18:11:3 | |
|
||||
| test.kt:9:18:11:3 | new If<T>(...) { ... } | test.kt:10:5:10:22 | x |
|
||||
| test.kt:9:18:11:3 | new If<T>(...) { ... } | test.kt:10:14:10:22 | getX |
|
||||
|
||||
@@ -14,6 +14,11 @@ open class A<T>(t: T) {
|
||||
override val x = t
|
||||
}
|
||||
|
||||
fun privateUser(x: A<String>, y: A<CharSequence>) {
|
||||
val a = x.privateAnonType.x
|
||||
val b = y.privateAnonType.x
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun user(x: A<String>) = x.anonType.x
|
||||
|
||||
@@ -3,3 +3,7 @@ import java
|
||||
from ClassOrInterface ci, Member m
|
||||
where m = ci.getAMember() and ci.getSourceDeclaration().fromSource()
|
||||
select ci, m
|
||||
|
||||
query predicate enclosingTypes(NestedType nt, Type encl) {
|
||||
nt.getSourceDeclaration().fromSource() and encl = nt.getEnclosingType()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user