Skip to content

Commit 4d45a2c

Browse files
authored
Merge pull request #9775 from smowton/smowton/fix/accessors-respect-private-member-exclusion
Kotlin: don't extract private setters of external classes
2 parents e98bdbf + b499ba5 commit 4d45a2c

File tree

6 files changed

+46
-13
lines changed

6 files changed

+46
-13
lines changed

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ open class KotlinFileExtractor(
134134
is IrProperty -> {
135135
val parentId = useDeclarationParent(declaration.parent, false)?.cast<DbReftype>()
136136
if (parentId != null) {
137-
extractProperty(declaration, parentId, extractBackingField = true, extractFunctionBodies = extractFunctionBodies, null, listOf())
137+
extractProperty(declaration, parentId, extractBackingField = true, extractFunctionBodies = extractFunctionBodies, extractPrivateMembers = extractPrivateMembers, null, listOf())
138138
}
139139
Unit
140140
}
@@ -364,7 +364,7 @@ open class KotlinFileExtractor(
364364
if (shouldExtractDecl(it, false)) {
365365
when(it) {
366366
is IrFunction -> extractFunction(it, id, extractBody = false, extractMethodAndParameterTypeAccesses = false, typeParamSubstitution, argsIncludingOuterClasses)
367-
is IrProperty -> extractProperty(it, id, extractBackingField = false, extractFunctionBodies = false, typeParamSubstitution, argsIncludingOuterClasses)
367+
is IrProperty -> extractProperty(it, id, extractBackingField = false, extractFunctionBodies = false, extractPrivateMembers = false, typeParamSubstitution, argsIncludingOuterClasses)
368368
else -> {}
369369
}
370370
}
@@ -955,7 +955,7 @@ open class KotlinFileExtractor(
955955
return id
956956
}
957957

958-
private fun extractProperty(p: IrProperty, parentId: Label<out DbReftype>, extractBackingField: Boolean, extractFunctionBodies: Boolean, typeSubstitution: TypeSubstitution?, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?) {
958+
private fun extractProperty(p: IrProperty, parentId: Label<out DbReftype>, extractBackingField: Boolean, extractFunctionBodies: Boolean, extractPrivateMembers: Boolean, typeSubstitution: TypeSubstitution?, classTypeArgsIncludingOuterClasses: List<IrTypeArgument>?) {
959959
with("property", p) {
960960
if (isFake(p)) return
961961

@@ -970,21 +970,25 @@ open class KotlinFileExtractor(
970970
val getter = p.getter
971971
val setter = p.setter
972972

973-
if (getter != null) {
973+
if (getter == null) {
974+
if (p.modality != Modality.FINAL || !isExternalDeclaration(p)) {
975+
logger.warnElement("IrProperty without a getter", p)
976+
}
977+
} else if (shouldExtractDecl(getter, extractPrivateMembers)) {
974978
val getterId = extractFunction(getter, parentId, extractBody = extractFunctionBodies, extractMethodAndParameterTypeAccesses = extractFunctionBodies, typeSubstitution, classTypeArgsIncludingOuterClasses)?.cast<DbMethod>()
975979
if (getterId != null) {
976980
tw.writeKtPropertyGetters(id, getterId)
977981
if (getter.origin == IrDeclarationOrigin.DELEGATED_PROPERTY_ACCESSOR) {
978982
tw.writeCompiler_generated(getterId, CompilerGeneratedKinds.DELEGATED_PROPERTY_GETTER.kind)
979983
}
980984
}
981-
} else {
982-
if (p.modality != Modality.FINAL || !isExternalDeclaration(p)) {
983-
logger.warnElement("IrProperty without a getter", p)
984-
}
985985
}
986986

987-
if (setter != null) {
987+
if (setter == null) {
988+
if (p.isVar && !isExternalDeclaration(p)) {
989+
logger.warnElement("isVar property without a setter", p)
990+
}
991+
} else if (shouldExtractDecl(setter, extractPrivateMembers)) {
988992
if (!p.isVar) {
989993
logger.warnElement("!isVar property with a setter", p)
990994
}
@@ -995,10 +999,6 @@ open class KotlinFileExtractor(
995999
tw.writeCompiler_generated(setterId, CompilerGeneratedKinds.DELEGATED_PROPERTY_SETTER.kind)
9961000
}
9971001
}
998-
} else {
999-
if (p.isVar && !isExternalDeclaration(p)) {
1000-
logger.warnElement("isVar property without a setter", p)
1001-
}
10021002
}
10031003

10041004
if (bf != null && extractBackingField) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class HasProps {
2+
3+
var accessorsPublic = 1
4+
5+
var setterPrivate = 3
6+
private set
7+
8+
}
9+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| hasprops.kt:3:3:3:25 | getAccessorsPublic |
2+
| hasprops.kt:3:3:3:25 | setAccessorsPublic |
3+
| hasprops.kt:5:3:6:15 | getSetterPrivate |
4+
| hasprops.kt:6:13:6:15 | setSetterPrivate$private |
5+
| usesprops.kt:1:1:9:1 | user |
6+
| usesprops.kt:3:3:3:58 | useGetters |
7+
| usesprops.kt:5:3:7:3 | useSetter |
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 hasprops.kt", "kotlinc usesprops.kt -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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fun user(hp: HasProps) {
2+
3+
fun useGetters() = hp.accessorsPublic + hp.setterPrivate
4+
5+
fun useSetter(x: Int) {
6+
hp.accessorsPublic = x
7+
}
8+
9+
}

0 commit comments

Comments
 (0)