Skip to content

Commit

Permalink
Fix secondary constructor handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
gchallen committed Oct 11, 2022
1 parent cf3b180 commit e838d4f
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ allprojects {
}
subprojects {
group = "com.github.cs125-illinois.jeed"
version = "2022.10.1"
version = "2022.10.2"
tasks.withType<Test> {
useJUnitPlatform()
enableAssertions = true
Expand Down
4 changes: 2 additions & 2 deletions containerrunner/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ plugins {
id("io.gitlab.arturbosch.detekt")
}
dependencies {
implementation("ch.qos.logback:logback-classic:1.4.3")
implementation("io.github.microutils:kotlin-logging:3.0.0")
implementation("ch.qos.logback:logback-classic:1.4.4")
implementation("io.github.microutils:kotlin-logging:3.0.2")
implementation("com.github.ajalt.clikt:clikt:3.5.0")
implementation("io.github.classgraph:classgraph:4.8.149")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2022.10.1
version=2022.10.2
6 changes: 3 additions & 3 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ dependencies {
implementation("org.ow2.asm:asm-tree:9.4")
implementation("org.ow2.asm:asm-util:9.4")
implementation("org.slf4j:slf4j-api:2.0.3")
implementation("ch.qos.logback:logback-classic:1.4.3")
implementation("io.github.microutils:kotlin-logging:3.0.0")
implementation("ch.qos.logback:logback-classic:1.4.4")
implementation("io.github.microutils:kotlin-logging:3.0.2")
implementation("io.github.classgraph:classgraph:4.8.149")
implementation("net.java.dev.jna:jna:5.12.1")
implementation("io.github.java-diff-utils:java-diff-utils:4.12")
Expand All @@ -43,7 +43,7 @@ dependencies {
api("org.jacoco:org.jacoco.core:0.8.8")
api("com.github.ben-manes.caffeine:caffeine:3.1.1")

testImplementation("io.kotest:kotest-runner-junit5:5.4.2")
testImplementation("io.kotest:kotest-runner-junit5:5.5.1")
}
tasks.test {
useJUnitPlatform()
Expand Down
1 change: 1 addition & 0 deletions core/src/main/kotlin/KotlinComplexity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class KotlinComplexityListener(val source: Source, entry: Map.Entry<String, Stri
override fun exitAnonymousInitializer(ctx: KotlinParser.AnonymousInitializerContext) {
exitMethodOrConstructor()
}

/*
// init
override fun enterClassMemberDeclaration(ctx: KotlinParser.ClassMemberDeclarationContext) {
Expand Down
25 changes: 25 additions & 0 deletions core/src/main/kotlin/KotlinFeatures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import edu.illinois.cs.cs125.jeed.core.antlr.KotlinParser.ClassBodyContext
import edu.illinois.cs.cs125.jeed.core.antlr.KotlinParser.ControlStructureBodyContext
import edu.illinois.cs.cs125.jeed.core.antlr.KotlinParser.FunctionBodyContext
import edu.illinois.cs.cs125.jeed.core.antlr.KotlinParser.FunctionDeclarationContext
import edu.illinois.cs.cs125.jeed.core.antlr.KotlinParser.SecondaryConstructorContext
import edu.illinois.cs.cs125.jeed.core.antlr.KotlinParser.StatementContext
import edu.illinois.cs.cs125.jeed.core.antlr.KotlinParserBaseListener
import org.antlr.v4.runtime.ParserRuleContext
Expand Down Expand Up @@ -232,6 +233,28 @@ class KotlinFeatureListener(val source: Source, entry: Map.Entry<String, String>
check(exitingLoopDepth == 0)
}

private var constructorCounter = 0
override fun enterSecondaryConstructor(ctx: SecondaryConstructorContext) {
ifDepths += 0
functionBlockDepths += 0
loopDepths += 0
enterMethodOrConstructor(
"constructor${constructorCounter++}",
Location(ctx.start.line, ctx.start.charPositionInLine),
Location(ctx.stop.line, ctx.stop.charPositionInLine)
)
}

override fun exitSecondaryConstructor(ctx: SecondaryConstructorContext) {
exitMethodOrConstructor()
val exitingBlockDepth = functionBlockDepths.pop()
check(exitingBlockDepth == 0)
val exitingIfDepth = ifDepths.pop()
check(exitingIfDepth == 0)
val exitingLoopDepth = loopDepths.pop()
check(exitingLoopDepth == 0)
}

override fun enterBlock(ctx: KotlinParser.BlockContext) {
if (functionBlockDepths.isNotEmpty()) {
functionBlockDepths[functionBlockDepths.size - 1]++
Expand All @@ -255,6 +278,7 @@ class KotlinFeatureListener(val source: Source, entry: Map.Entry<String, String>
is FunctionBodyContext -> return currentParent
is ClassBodyContext -> return currentParent
is AnonymousInitializerContext -> return currentParent
is SecondaryConstructorContext -> return currentParent
}
currentParent = currentParent.parent
}
Expand All @@ -275,6 +299,7 @@ class KotlinFeatureListener(val source: Source, entry: Map.Entry<String, String>
private fun ParserRuleContext.parentType() = when (parentContext()) {
is FunctionBodyContext -> ParentType.FUNCTION
is AnonymousInitializerContext -> ParentType.FUNCTION
is SecondaryConstructorContext -> ParentType.FUNCTION
is ClassBodyContext -> ParentType.CLASS
else -> ParentType.NONE
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2022.10.1
version=2022.10.2
17 changes: 17 additions & 0 deletions core/src/test/kotlin/TestKotlinComplexity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -742,4 +742,21 @@ class Main {
it.lookupFile("Main.kt") shouldBe 2
}
}
"should handle secondary constructors" {
Source.fromKotlin(
"""
class Main {
private val map = mutableMapOf<String, Int>()
constructor(list: List<String>) {
require(!list.isEmpty())
for (place in list) {
map[place] = 0
}
}
}
""".trim()
).complexity().also {
it.lookupFile("Main.kt") shouldBe 3
}
}
})
15 changes: 15 additions & 0 deletions core/src/test/kotlin/TestKotlinFeatures.kt
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,21 @@ class Test {
featureMap[FeatureName.LOCAL_VARIABLE_DECLARATIONS] shouldBe 1
}
}
"should handle secondary constructors" {
Source.fromKotlinSnippet(
"""
class Test {
private val map = mutableMapOf<String, Int>()
constructor(list: List<String>) {
require(!list.isEmpty())
for (place in list) {
map[place] = 0
}
}
}
""".trim()
).features()
}
})

fun FeaturesResults.check(path: String = ".", filename: String = "", block: Features.() -> Any): FeaturesResults {
Expand Down
4 changes: 2 additions & 2 deletions leaktest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ plugins {
dependencies {
implementation(project(":core"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("ch.qos.logback:logback-classic:1.4.3")
implementation("io.github.microutils:kotlin-logging:3.0.0")
implementation("ch.qos.logback:logback-classic:1.4.4")
implementation("io.github.microutils:kotlin-logging:3.0.2")
}
application {
@Suppress("DEPRECATION")
Expand Down
6 changes: 3 additions & 3 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("com.squareup.moshi:moshi-kotlin:1.14.0")
implementation("com.github.cs125-illinois:ktor-moshi:2022.9.0")
implementation("ch.qos.logback:logback-classic:1.4.3")
implementation("ch.qos.logback:logback-classic:1.4.4")
implementation("com.uchuhimo:konf-core:1.1.2")
implementation("com.uchuhimo:konf-yaml:1.1.2")
implementation("io.github.microutils:kotlin-logging:3.0.0")
implementation("io.github.microutils:kotlin-logging:3.0.2")
implementation("com.github.cs125-illinois:libcs1:2022.9.1")
implementation("com.beyondgrader.resource-agent:agent:2022.9.3")
implementation("com.beyondgrader.resource-agent:jeedplugin:2022.9.3")

testImplementation("io.kotest:kotest-runner-junit5:5.4.2")
testImplementation("io.kotest:kotest-runner-junit5:5.5.1")
testImplementation("io.kotest:kotest-assertions-ktor:4.4.3")
testImplementation("io.ktor:ktor-server-test-host:2.1.2")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2022.10.1
version=2022.10.2

0 comments on commit e838d4f

Please sign in to comment.