Skip to content

Commit 280c959

Browse files
authored
Merge branch 'main' into nickrolfe/pathname
2 parents c1515db + c72377c commit 280c959

File tree

239 files changed

+6372
-206
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

239 files changed

+6372
-206
lines changed

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

Lines changed: 22 additions & 7 deletions
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

@@ -732,12 +734,9 @@ open class KotlinFileExtractor(
732734

733735
val id =
734736
idOverride
735-
?: if (f.isLocalFunction())
736-
getLocallyVisibleFunctionLabels(f).function
737-
else
738-
// If this is a class that would ordinarily be replaced by a Java equivalent (e.g. kotlin.Map -> java.util.Map),
739-
// don't replace here, really extract the Kotlin version:
740-
useFunction<DbCallable>(f, parentId, classTypeArgsIncludingOuterClasses, noReplace = true)
737+
?: // If this is a class that would ordinarily be replaced by a Java equivalent (e.g. kotlin.Map -> java.util.Map),
738+
// don't replace here, really extract the Kotlin version:
739+
useFunction<DbCallable>(f, parentId, classTypeArgsIncludingOuterClasses, noReplace = true)
741740

742741
val sourceDeclaration =
743742
if (typeSubstitution != null && idOverride == null)
@@ -783,6 +782,14 @@ open class KotlinFileExtractor(
783782
val methodId = id.cast<DbMethod>()
784783
tw.writeMethods(methodId, shortName.nameInDB, "${shortName.nameInDB}$paramsSignature", returnType.javaResult.id, parentId, sourceDeclaration.cast<DbMethod>())
785784
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+
}
786793

787794
if (extractMethodAndParameterTypeAccesses) {
788795
extractTypeAccessRecursive(substReturnType, locId, id, -1)
@@ -1073,7 +1080,7 @@ open class KotlinFileExtractor(
10731080
tw.writeKtLocalFunction(ids.function)
10741081

10751082
if (s.origin == IrDeclarationOrigin.ADAPTER_FOR_CALLABLE_REFERENCE) {
1076-
tw.writeCompiler_generated(classId, 1)
1083+
tw.writeCompiler_generated(classId, CompilerGeneratedKinds.DECLARING_CLASSES_OF_ADAPTER_FUNCTIONS.kind)
10771084
}
10781085
} else {
10791086
logger.errorElement("Expected to find local function", s)
@@ -4370,4 +4377,12 @@ open class KotlinFileExtractor(
43704377
declarationStack.pop()
43714378
}
43724379
}
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+
}
43734388
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,15 +1201,14 @@ open class KotlinUsesExtractor(
12011201
} as IrFunction? ?: f
12021202

12031203
fun <T: DbCallable> useFunction(f: IrFunction, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>? = null, noReplace: Boolean = false): Label<out T> {
1204+
return useFunction(f, null, classTypeArgsIncludingOuterClasses, noReplace)
1205+
}
1206+
1207+
fun <T: DbCallable> useFunction(f: IrFunction, parentId: Label<out DbElement>?, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?, noReplace: Boolean = false): Label<out T> {
12041208
if (f.isLocalFunction()) {
12051209
val ids = getLocallyVisibleFunctionLabels(f)
12061210
return ids.function.cast<T>()
1207-
} else {
1208-
return useFunction(f, null, classTypeArgsIncludingOuterClasses, noReplace)
12091211
}
1210-
}
1211-
1212-
fun <T: DbCallable> useFunction(f: IrFunction, parentId: Label<out DbElement>?, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?, noReplace: Boolean = false): Label<out T> {
12131212
val javaFun = kotlinFunctionToJavaEquivalent(f, noReplace)
12141213
val label = getFunctionLabel(javaFun, parentId, classTypeArgsIncludingOuterClasses)
12151214
val id: Label<T> = tw.getLabelFor(label)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
a.kt:
2+
# 0| [CompilationUnit] a
3+
# 1| 1: [Class] A
4+
# 0| 1: [Method] <fn>
5+
# 0| 3: [TypeAccess] int
6+
# 0| 5: [BlockStmt] { ... }
7+
# 0| 0: [ReturnStmt] return ...
8+
# 0| 0: [IntegerLiteral] 42
9+
# 1| 2: [Constructor] A
10+
# 1| 5: [BlockStmt] { ... }
11+
# 1| 0: [SuperConstructorInvocationStmt] super(...)
12+
# 1| 1: [BlockStmt] { ... }
13+
# 2| 3: [Method] f1
14+
# 2| 3: [TypeAccess] int
15+
# 2| 5: [BlockStmt] { ... }
16+
# 2| 0: [ReturnStmt] return ...
17+
# 2| 0: [IntegerLiteral] 1
18+
b.kt:
19+
# 0| [CompilationUnit] b
20+
# 1| 1: [Class] B
21+
# 0| 1: [Method] <fn>
22+
# 0| 3: [TypeAccess] int
23+
# 0| 5: [BlockStmt] { ... }
24+
# 0| 0: [ReturnStmt] return ...
25+
# 0| 0: [UnsafeCoerceExpr] <unsafe coerce>
26+
# 0| 0: [TypeAccess] int
27+
# 0| 1: [IntegerLiteral] 1
28+
# 1| 2: [Constructor] B
29+
# 1| 5: [BlockStmt] { ... }
30+
# 1| 0: [SuperConstructorInvocationStmt] super(...)
31+
# 1| 1: [BlockStmt] { ... }
32+
c.kt:
33+
# 0| [CompilationUnit] c
34+
# 1| 1: [Class] C
35+
# 0| 1: [Method] <fn>
36+
# 0| 3: [TypeAccess] Unit
37+
#-----| 4: (Parameters)
38+
# 0| 0: [Parameter] param
39+
# 0| 0: [TypeAccess] ProcessBuilder
40+
# 0| 5: [BlockStmt] { ... }
41+
# 0| 0: [ReturnStmt] return ...
42+
# 0| 0: [MethodAccess] start(...)
43+
# 0| -1: [VarAccess] param
44+
# 1| 2: [Constructor] C
45+
# 1| 5: [BlockStmt] { ... }
46+
# 1| 0: [SuperConstructorInvocationStmt] super(...)
47+
# 1| 1: [BlockStmt] { ... }
48+
d.kt:
49+
# 0| [CompilationUnit] d
50+
# 1| 1: [Class] D
51+
# 0| 1: [FieldDeclaration] String bar;
52+
# 0| -1: [TypeAccess] String
53+
# 0| 0: [StringLiteral] Foobar
54+
# 1| 3: [Constructor] D
55+
# 1| 5: [BlockStmt] { ... }
56+
# 1| 0: [SuperConstructorInvocationStmt] super(...)
57+
# 1| 1: [BlockStmt] { ... }
58+
e.kt:
59+
# 0| [CompilationUnit] e
60+
# 1| 1: [Class] E
61+
# 0| 1: [Method] <fn_ArrayListAdd>
62+
# 0| 3: [TypeAccess] boolean
63+
# 0| 5: [BlockStmt] { ... }
64+
# 0| 0: [ReturnStmt] return ...
65+
# 0| 0: [MethodAccess] add(...)
66+
# 0| -1: [ClassInstanceExpr] new ArrayList<Object>(...)
67+
# 0| -3: [TypeAccess] ArrayList<Object>
68+
# 0| 0: [IntegerLiteral] 1
69+
# 0| 0: [NullLiteral] null
70+
# 0| 1: [Method] <fn_LinkedHashMap>
71+
# 0| 3: [TypeAccess] Object
72+
# 0| 5: [BlockStmt] { ... }
73+
# 0| 0: [ReturnStmt] return ...
74+
# 0| 0: [MethodAccess] put(...)
75+
# 0| -1: [ClassInstanceExpr] new LinkedHashMap<Object,Object>(...)
76+
# 0| -3: [TypeAccess] LinkedHashMap<Object,Object>
77+
# 0| 0: [IntegerLiteral] 1
78+
# 0| 0: [NullLiteral] null
79+
# 0| 1: [NullLiteral] null
80+
# 1| 3: [Constructor] E
81+
# 1| 5: [BlockStmt] { ... }
82+
# 1| 0: [SuperConstructorInvocationStmt] super(...)
83+
# 1| 1: [BlockStmt] { ... }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle/code/java/PrintAst.ql
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class A {
2+
fun f1() = 1
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class B {
2+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
3+
import subprocess
4+
import shutil
5+
import os
6+
import os.path
7+
import sys
8+
import shlex
9+
10+
11+
def run_process(cmd):
12+
try:
13+
print("Running command: " + shlex.join(cmd))
14+
return subprocess.run(cmd, check=True, capture_output=True)
15+
except subprocess.CalledProcessError as e:
16+
print("In: " + os.getcwd(), file=sys.stderr)
17+
print("Command failed: " + shlex.join(cmd), file=sys.stderr)
18+
print("stdout output:\n" + e.stdout.decode(encoding='UTF-8',
19+
errors='strict'), file=sys.stderr)
20+
print("stderr output:\n" + e.stderr.decode(encoding='UTF-8',
21+
errors='strict'), file=sys.stderr)
22+
raise e
23+
24+
root = '../../../../../../../../..'
25+
26+
sys.path.append(root + '/ql/java/kotlin-extractor')
27+
import kotlin_plugin_versions
28+
defaultKotlinDependencyVersion = kotlin_plugin_versions.get_single_version()
29+
30+
builddir = 'build'
31+
dependency_dir = root + '/resources/kotlin-dependencies/'
32+
dependencies = ['kotlin-stdlib-' + defaultKotlinDependencyVersion +
33+
'.jar', 'kotlin-compiler-' + defaultKotlinDependencyVersion + '.jar']
34+
classpath = ':'.join([dependency_dir + dep for dep in dependencies])
35+
srcs = ['plugin/Plugin.kt']
36+
output = 'plugin.jar'
37+
38+
if os.path.exists(builddir):
39+
shutil.rmtree(builddir)
40+
os.makedirs(builddir)
41+
42+
run_process(['kotlinc',
43+
'-J-Xmx2G',
44+
'-d', builddir,
45+
'-module-name', 'test',
46+
'-no-reflect', '-no-stdlib',
47+
'-jvm-target', '1.8',
48+
'-classpath', classpath] + srcs)
49+
50+
run_process(['jar', '-c', '-f', output,
51+
'-C', builddir, '.',
52+
'-C', 'plugin/resources', 'META-INF'])
53+
shutil.rmtree(builddir)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class C { }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class D { }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| CodeQL Kotlin extractor | 2 | | IrProperty without a getter | d.kt:0:0:0:0 | d.kt:0:0:0:0 |
2+
| CodeQL Kotlin extractor | 2 | | Not rewriting trap file for: Boolean -1.0-0- -1.0-0-null test-db/trap/java/classes/kotlin/Boolean.members.trap.gz | file://:0:0:0:0 | file://:0:0:0:0 |

0 commit comments

Comments
 (0)