Skip to content

Commit 1730ec2

Browse files
committed
Kotlin: Extract an ErrorType if we fail to correctly extract a type
1 parent 780f5ab commit 1730ec2

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,26 @@ open class KotlinUsesExtractor(
108108
}
109109
data class TypeResults(val javaResult: TypeResult<DbType>, val kotlinResult: TypeResult<DbKt_type>)
110110

111-
fun useType(t: IrType, context: TypeContext = TypeContext.OTHER) =
111+
fun useType(t: IrType, context: TypeContext = TypeContext.OTHER): TypeResults {
112112
when(t) {
113-
is IrSimpleType -> useSimpleType(t, context)
113+
is IrSimpleType -> return useSimpleType(t, context)
114114
else -> {
115115
logger.error("Unrecognised IrType: " + t.javaClass)
116-
TypeResults(TypeResult(fakeLabel(), "unknown", "unknown"), TypeResult(fakeLabel(), "unknown", "unknown"))
116+
return extractErrorType()
117117
}
118118
}
119+
}
120+
121+
private fun extractErrorType(): TypeResults {
122+
val typeId = tw.getLabelFor<DbErrortype>("@\"errorType\"") {
123+
tw.writeError_type(it)
124+
}
125+
val kotlinTypeId = tw.getLabelFor<DbKt_nullable_type>("@\"errorKotlinType\"") {
126+
tw.writeKt_nullable_types(it, typeId)
127+
}
128+
return TypeResults(TypeResult(typeId, null, "<CodeQL error type>"),
129+
TypeResult(kotlinTypeId, null, "<CodeQL error type>"))
130+
}
119131

120132
fun getJavaEquivalentClass(c: IrClass) =
121133
getJavaEquivalentClassId(c)?.let { pluginContext.referenceClass(it.asSingleFqName()) }?.owner

java/ql/lib/config/semmlecode.dbscheme

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,14 @@ modifiers(
332332
string nodeName: string ref
333333
);
334334

335+
/**
336+
* An errortype is used when the extractor is unable to extract a type
337+
* correctly for some reason.
338+
*/
339+
error_type(
340+
unique int id: @errortype
341+
);
342+
335343
classes(
336344
unique int id: @class,
337345
string nodeName: string ref,
@@ -1012,13 +1020,13 @@ javadocText(
10121020
@classorinterfaceorpackage = @classorinterface | @package;
10131021
@classorinterfaceorcallable = @classorinterface | @callable;
10141022
@boundedtype = @typevariable | @wildcard;
1015-
@reftype = @classorinterface | @array | @boundedtype;
1023+
@reftype = @classorinterface | @array | @boundedtype | @errortype;
10161024
@classorarray = @class | @array;
10171025
@type = @primitive | @reftype;
10181026
@callable = @method | @constructor;
10191027

10201028
/** A program element that has a name. */
1021-
@element = @package | @modifier | @annotation |
1029+
@element = @package | @modifier | @annotation | @errortype |
10221030
@locatableElement;
10231031

10241032
@locatableElement = @file | @primitive | @class | @interface | @method | @constructor | @param | @exception | @field |

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ predicate hasName(Element e, string name) {
4747
kt_type_alias(e, name, _)
4848
or
4949
ktProperties(e, name)
50+
or
51+
e instanceof ErrorType and name = "<CodeQL error type>"
5052
}
5153

5254
/**

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,14 @@ class RefType extends Type, Annotatable, Modifiable, @reftype {
666666
}
667667
}
668668

669+
/**
670+
* An `ErrorType` is generated when CodeQL is unable to correctly
671+
* extract a type.
672+
*/
673+
class ErrorType extends RefType, @errortype {
674+
override string getAPrimaryQlClass() { result = "ErrorType" }
675+
}
676+
669677
/** A type that is the same as its source declaration. */
670678
class SrcRefType extends RefType {
671679
SrcRefType() { this.isSourceDeclaration() }

0 commit comments

Comments
 (0)