Skip to content

Commit 861a368

Browse files
authored
Merge pull request #9703 from igfoo/igfoo/generated
Kotlin: Record more kinds of elements as compiler-generated
2 parents 0ddd5bb + 73a79e0 commit 861a368

File tree

14 files changed

+2674
-23
lines changed

14 files changed

+2674
-23
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,8 @@ open class KotlinFileExtractor(
595595
tw.writeMethods(clinitId, "<clinit>", "<clinit>()", returnType.javaResult.id, parentId, clinitId)
596596
tw.writeMethodsKotlinType(clinitId, returnType.kotlinResult.id)
597597

598+
tw.writeCompiler_generated(clinitId, CompilerGeneratedKinds.CLASS_INITIALISATION_METHOD.kind)
599+
598600
val locId = tw.getWholeFileLocation()
599601
tw.writeHasLocation(clinitId, locId)
600602

@@ -780,6 +782,14 @@ open class KotlinFileExtractor(
780782
val methodId = id.cast<DbMethod>()
781783
tw.writeMethods(methodId, shortName.nameInDB, "${shortName.nameInDB}$paramsSignature", returnType.javaResult.id, parentId, sourceDeclaration.cast<DbMethod>())
782784
tw.writeMethodsKotlinType(methodId, returnType.kotlinResult.id)
785+
when (f.origin) {
786+
IrDeclarationOrigin.GENERATED_DATA_CLASS_MEMBER ->
787+
tw.writeCompiler_generated(methodId, CompilerGeneratedKinds.GENERATED_DATA_CLASS_MEMBER.kind)
788+
IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR ->
789+
tw.writeCompiler_generated(methodId, CompilerGeneratedKinds.DEFAULT_PROPERTY_ACCESSOR.kind)
790+
IrDeclarationOrigin.ENUM_CLASS_SPECIAL_MEMBER ->
791+
tw.writeCompiler_generated(methodId, CompilerGeneratedKinds.ENUM_CLASS_SPECIAL_MEMBER.kind)
792+
}
783793

784794
if (extractMethodAndParameterTypeAccesses) {
785795
extractTypeAccessRecursive(substReturnType, locId, id, -1)
@@ -1070,7 +1080,7 @@ open class KotlinFileExtractor(
10701080
tw.writeKtLocalFunction(ids.function)
10711081

10721082
if (s.origin == IrDeclarationOrigin.ADAPTER_FOR_CALLABLE_REFERENCE) {
1073-
tw.writeCompiler_generated(classId, 1)
1083+
tw.writeCompiler_generated(classId, CompilerGeneratedKinds.DECLARING_CLASSES_OF_ADAPTER_FUNCTIONS.kind)
10741084
}
10751085
} else {
10761086
logger.errorElement("Expected to find local function", s)
@@ -4367,4 +4377,12 @@ open class KotlinFileExtractor(
43674377
declarationStack.pop()
43684378
}
43694379
}
4380+
4381+
private enum class CompilerGeneratedKinds(val kind: Int) {
4382+
DECLARING_CLASSES_OF_ADAPTER_FUNCTIONS(1),
4383+
GENERATED_DATA_CLASS_MEMBER(2),
4384+
DEFAULT_PROPERTY_ACCESSOR(3),
4385+
CLASS_INITIALISATION_METHOD(4),
4386+
ENUM_CLASS_SPECIAL_MEMBER(5)
4387+
}
43704388
}

java/ql/lib/config/semmlecode.dbscheme

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,10 +1211,15 @@ ktPropertyDelegates(
12111211
unique int variableId: @variable ref
12121212
)
12131213

1214+
/**
1215+
* If `id` is a compiler generated element, then the kind indicates the
1216+
* reason that the compiler generated it.
1217+
* See `Element.compilerGeneratedReason()` for an explanation of what
1218+
* each `kind` means.
1219+
*/
12141220
compiler_generated(
12151221
unique int id: @element ref,
12161222
int kind: int ref
1217-
// 1: Declaring classes of adapter functions in Kotlin
12181223
)
12191224

12201225
ktFunctionOriginalNames(

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ class Element extends @element, Top {
4444

4545
/** Holds if this is an auxiliary program element generated by the compiler. */
4646
predicate isCompilerGenerated() { compiler_generated(this, _) }
47+
48+
/** Gets the reason this element was generated by the compiler, if any. */
49+
string compilerGeneratedReason() {
50+
exists(int i | compiler_generated(this, i) |
51+
i = 1 and result = "Declaring classes of adapter functions in Kotlin"
52+
or
53+
i = 2 and result = "Generated data class member"
54+
or
55+
i = 3 and result = "Default property accessor"
56+
or
57+
i = 4 and result = "Class initialisation method <clinit>"
58+
or
59+
i = 5 and result = "Enum class special member"
60+
)
61+
}
4762
}
4863

4964
/**

0 commit comments

Comments
 (0)