Skip to content

Commit 227dad8

Browse files
committed
Merge main into redsun82/swift-extraction
2 parents e575bab + 7a1c380 commit 227dad8

Some content is hidden

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

71 files changed

+253
-34
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: "Swift: Run Integration Tests"
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- "swift/**"
7+
- .github/workflows/swift-integration-tests.yml
8+
- codeql-workspace.yml
9+
branches:
10+
- main
11+
defaults:
12+
run:
13+
working-directory: swift
14+
15+
jobs:
16+
integration-tests:
17+
runs-on: ${{ matrix.os }}
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
os:
22+
- ubuntu-20.04
23+
# - macos-latest TODO
24+
steps:
25+
- uses: actions/checkout@v3
26+
- uses: ./.github/actions/fetch-codeql
27+
- uses: bazelbuild/setup-bazelisk@v2
28+
- uses: actions/setup-python@v3
29+
- name: Build Swift extractor
30+
run: |
31+
bazel run //swift:create-extractor-pack
32+
- name: Run integration tests
33+
run: |
34+
python integration-tests/runner.py

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ WORKSPACE.bazel @github/codeql-ci-reviewers
4242
/.github/workflows/js-ml-tests.yml @github/codeql-ml-powered-queries-reviewers
4343
/.github/workflows/ql-for-ql-* @github/codeql-ql-for-ql-reviewers
4444
/.github/workflows/ruby-* @github/codeql-ruby
45+
/.github/workflows/swift-* @github/codeql-c

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

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.backend.common.lower.parents
1111
import org.jetbrains.kotlin.backend.common.lower.parentsWithSelf
1212
import org.jetbrains.kotlin.backend.jvm.ir.propertyIfAccessor
1313
import org.jetbrains.kotlin.builtins.StandardNames
14+
import org.jetbrains.kotlin.codegen.JvmCodegenUtil
1415
import org.jetbrains.kotlin.descriptors.*
1516
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
1617
import org.jetbrains.kotlin.ir.declarations.*
@@ -23,8 +24,10 @@ import org.jetbrains.kotlin.load.java.BuiltinMethodsWithSpecialGenericSignature
2324
import org.jetbrains.kotlin.load.java.JvmAbi
2425
import org.jetbrains.kotlin.load.java.sources.JavaSourceElement
2526
import org.jetbrains.kotlin.load.java.structure.*
27+
import org.jetbrains.kotlin.load.kotlin.getJvmModuleNameForDeserializedDescriptor
2628
import org.jetbrains.kotlin.name.FqName
2729
import org.jetbrains.kotlin.name.Name
30+
import org.jetbrains.kotlin.name.NameUtils
2831
import org.jetbrains.kotlin.name.SpecialNames
2932
import org.jetbrains.kotlin.types.Variance
3033
import org.jetbrains.kotlin.util.OperatorNameConventions
@@ -754,11 +757,25 @@ open class KotlinUsesExtractor(
754757

755758
data class FunctionNames(val nameInDB: String, val kotlinName: String)
756759

760+
@OptIn(ObsoleteDescriptorBasedAPI::class)
761+
private fun getJvmModuleName(f: IrFunction) =
762+
NameUtils.sanitizeAsJavaIdentifier(
763+
getJvmModuleNameForDeserializedDescriptor(f.descriptor) ?: JvmCodegenUtil.getModuleName(pluginContext.moduleDescriptor)
764+
)
765+
757766
fun getFunctionShortName(f: IrFunction) : FunctionNames {
758767
if (f.origin == IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA || f.isAnonymousFunction)
759768
return FunctionNames(
760769
OperatorNameConventions.INVOKE.asString(),
761770
OperatorNameConventions.INVOKE.asString())
771+
772+
fun getSuffixIfInternal() =
773+
if (f.visibility == DescriptorVisibilities.INTERNAL) {
774+
"\$" + getJvmModuleName(f)
775+
} else {
776+
""
777+
}
778+
762779
(f as? IrSimpleFunction)?.correspondingPropertySymbol?.let {
763780
val propName = it.owner.name.asString()
764781
val getter = it.owner.getter
@@ -774,35 +791,26 @@ open class KotlinUsesExtractor(
774791
}
775792
}
776793

777-
when (f) {
778-
getter -> {
779-
val defaultFunctionName = JvmAbi.getterName(propName)
780-
val defaultDbName = if (getter.visibility == DescriptorVisibilities.PRIVATE && getter.origin == IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR) {
781-
// In JVM these functions don't exist, instead the backing field is accessed directly
782-
defaultFunctionName + "\$private"
783-
} else {
784-
defaultFunctionName
785-
}
786-
return FunctionNames(getJvmName(getter) ?: defaultDbName, defaultFunctionName)
787-
}
788-
setter -> {
789-
val defaultFunctionName = JvmAbi.setterName(propName)
790-
val defaultDbName = if (setter.visibility == DescriptorVisibilities.PRIVATE && setter.origin == IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR) {
791-
// In JVM these functions don't exist, instead the backing field is accessed directly
792-
defaultFunctionName + "\$private"
793-
} else {
794-
defaultFunctionName
795-
}
796-
return FunctionNames(getJvmName(setter) ?: defaultDbName, defaultFunctionName)
797-
}
794+
val maybeFunctionName = when (f) {
795+
getter -> JvmAbi.getterName(propName)
796+
setter -> JvmAbi.setterName(propName)
798797
else -> {
799798
logger.error(
800799
"Function has a corresponding property, but is neither the getter nor the setter"
801800
)
801+
null
802+
}
803+
}
804+
maybeFunctionName?.let { defaultFunctionName ->
805+
val suffix = if (f.visibility == DescriptorVisibilities.PRIVATE && f.origin == IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR) {
806+
"\$private"
807+
} else {
808+
getSuffixIfInternal()
802809
}
810+
return FunctionNames(getJvmName(f) ?: "$defaultFunctionName$suffix", defaultFunctionName)
803811
}
804812
}
805-
return FunctionNames(getJvmName(f) ?: f.name.asString(), f.name.asString())
813+
return FunctionNames(getJvmName(f) ?: "${f.name.asString()}${getSuffixIfInternal()}", f.name.asString())
806814
}
807815

808816
// This excludes class type parameters that show up in (at least) constructors' typeParameters list.

java/ql/consistency-queries/visibility.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ where
1818
m.getFile().isKotlinSourceFile() and
1919
// TODO: This ought to have visibility information
2020
not m.getName() = "<clinit>" and
21-
count(visibility(m)) != 1
21+
count(visibility(m)) != 1 and
22+
not (count(visibility(m)) = 2 and visibility(m) = "public" and visibility(m) = "internal") // This is a reasonable result, since the JVM symbol is declared public, but Kotlin metadata flags it as internal
2223
select m, concat(visibility(m), ", ")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class User {
2+
3+
public static int test(Test1 test1, Test2 test2, Test3 test3) {
4+
5+
return test1.f$main() + test2.f$mymodule() + test3.f$reservedchars___();
6+
7+
}
8+
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| User.java:3:21:3:24 | test |
2+
| test1.kt:3:12:3:22 | f$main |
3+
| test2.kt:3:12:3:22 | f$mymodule |
4+
| test3.kt:3:12:3:22 | f$reservedchars___ |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from create_database_utils import *
2+
3+
run_codeql_database_create(["kotlinc test1.kt", "kotlinc test2.kt -module-name mymodule", "kotlinc test3.kt -module-name reservedchars\\\"${}/", "javac User.java -cp ." ], lang="java")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import java
2+
3+
from Method m
4+
where m.fromSource()
5+
select m
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Test1 {
2+
3+
internal fun f() = 1
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Test2 {
2+
3+
internal fun f() = 2
4+
5+
}

0 commit comments

Comments
 (0)