From 71f44dce0f9cffdb01bfc23ba650383bea7000ca Mon Sep 17 00:00:00 2001 From: LepilkinaElena Date: Thu, 21 Jan 2021 19:53:08 +0300 Subject: [PATCH] Added StringTrimLowering to pipeline (#4647) --- .../backend/konan/KonanLoweringPhases.kt | 6 ++++ .../kotlin/backend/konan/ToplevelPhases.kt | 1 + .../backend.native/tests/build.gradle | 4 +++ .../tests/codegen/stringTrim/stringTrim.kt | 30 +++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 kotlin-native/backend.native/tests/codegen/stringTrim/stringTrim.kt diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLoweringPhases.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLoweringPhases.kt index d445332e8a5..8c4587e6225 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLoweringPhases.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLoweringPhases.kt @@ -379,4 +379,10 @@ internal val foldConstantLoweringPhase = makeKonanFileOpPhase( name = "FoldConstantLowering", description = "Constant Folding", prerequisite = setOf(flattenStringConcatenationPhase) +) + +internal val computeStringTrimPhase = makeKonanFileLoweringPhase( + ::StringTrimLowering, + name = "StringTrimLowering", + description = "Compute trimIndent and trimMargin operations on constant strings" ) \ No newline at end of file diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt index c4f94887680..4b71fd2dc53 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt @@ -221,6 +221,7 @@ internal val allLoweringsPhase = NamedCompilerPhase( forLoopsPhase, flattenStringConcatenationPhase, foldConstantLoweringPhase, + computeStringTrimPhase, stringConcatenationPhase, enumConstructorsPhase, initializersPhase, diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index d59f61e18d6..ee9a2b1b614 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -813,6 +813,10 @@ task hello0(type: KonanLocalTest) { source = "runtime/basic/hello0.kt" } +task stringTrim(type: KonanLocalTest) { + source = "codegen/stringTrim/stringTrim.kt" +} + standaloneTest("hello1") { goldValue = "Hello World" testData = "Hello World\n" diff --git a/kotlin-native/backend.native/tests/codegen/stringTrim/stringTrim.kt b/kotlin-native/backend.native/tests/codegen/stringTrim/stringTrim.kt new file mode 100644 index 00000000000..50deb484e89 --- /dev/null +++ b/kotlin-native/backend.native/tests/codegen/stringTrim/stringTrim.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +package codegen.stringTrim.stringTrim + +import kotlin.test.* + +// TODO: check IR +fun constantIndent(): String { + return """ + Hello, + World + """.trimIndent() +} + +fun constantMargin(): String { + return """ + |Hello, + |World + """.trimMargin() +} + +@Test +fun runTest() { + assertTrue(constantIndent() === constantIndent()) + assertTrue(constantMargin() === constantMargin()) +} +