Add kotlin example for retrace of inline functions

Bug: 141817471
Change-Id: I3c2c6cecf4423c3a0f08137abf98457de3bbbd1b
diff --git a/src/test/java/com/android/tools/r8/retrace/KotlinInlineFunctionRetraceTest.java b/src/test/java/com/android/tools/r8/retrace/KotlinInlineFunctionRetraceTest.java
new file mode 100644
index 0000000..cba0f8b
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/retrace/KotlinInlineFunctionRetraceTest.java
@@ -0,0 +1,137 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.retrace;
+
+import static com.android.tools.r8.KotlinCompilerTool.KOTLINC;
+import static com.android.tools.r8.ToolHelper.getFilesInTestFolderRelativeToClass;
+import static org.hamcrest.core.StringContains.containsString;
+
+import com.android.tools.r8.CompilationFailedException;
+import com.android.tools.r8.CompilationMode;
+import com.android.tools.r8.R8TestRunResult;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.TestRuntime;
+import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.ToolHelper.KotlinTargetVersion;
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class KotlinInlineFunctionRetraceTest extends TestBase {
+
+  private final TestParameters parameters;
+
+  @Parameters(name = "{0}")
+  public static TestParametersCollection data() {
+    // TODO(b/141817471): Extend with compilation modes.
+    return getTestParameters().withAllRuntimesAndApiLevels().build();
+  }
+
+  public KotlinInlineFunctionRetraceTest(TestParameters parameters) {
+    this.parameters = parameters;
+  }
+
+  @Test
+  public void testRuntime() throws ExecutionException, CompilationFailedException, IOException {
+    testForRuntime(parameters)
+        .addProgramFiles(
+            kotlinc(TestRuntime.getCheckedInJdk8(), KOTLINC, KotlinTargetVersion.JAVA_8)
+                .addSourceFiles(
+                    getFilesInTestFolderRelativeToClass(
+                        KotlinInlineFunctionRetraceTest.class, "kt", ".kt"))
+                .compile())
+        .addRunClasspathFiles(buildOnDexRuntime(parameters, ToolHelper.getKotlinStdlibJar()))
+        .run(parameters.getRuntime(), "retrace.MainKt")
+        .assertFailureWithErrorThatMatches(containsString("inlineExceptionStatic"))
+        .assertFailureWithErrorThatMatches(containsString("at retrace.MainKt.main(Main.kt:15)"));
+  }
+
+  @Test
+  public void testRetraceKotlinInlineStaticFunction()
+      throws ExecutionException, CompilationFailedException, IOException {
+    String main = "retrace.MainKt";
+    R8TestRunResult result =
+        testForR8(parameters.getBackend())
+            .addProgramFiles(
+                kotlinc(TestRuntime.getCheckedInJdk8(), KOTLINC, KotlinTargetVersion.JAVA_8)
+                    .addSourceFiles(
+                        getFilesInTestFolderRelativeToClass(
+                            KotlinInlineFunctionRetraceTest.class, "kt", ".kt"))
+                    .compile())
+            .addProgramFiles(ToolHelper.getKotlinStdlibJar())
+            .addKeepAttributes("SourceFile", "LineNumberTable")
+            .setMode(CompilationMode.RELEASE)
+            .addKeepMainRule(main)
+            .setMinApi(parameters.getApiLevel())
+            .run(parameters.getRuntime(), main)
+            .assertFailureWithErrorThatMatches(containsString("inlineExceptionStatic"))
+            .assertFailureWithErrorThatMatches(containsString("at retrace.MainKt.main(Main.kt:2)"));
+    List<String> retrace = result.retrace();
+    // TODO(b/141817471): Change the tracing information when solved.
+    // assertThat(retrace.get(1), containsString("at retrace.MainKt.main(Main.kt:15)"));
+  }
+
+  @Test
+  public void testRetraceKotlinInlineInstanceFunction()
+      throws ExecutionException, CompilationFailedException, IOException {
+    String main = "retrace.MainInstanceKt";
+    R8TestRunResult result =
+        testForR8(parameters.getBackend())
+            .addProgramFiles(
+                kotlinc(TestRuntime.getCheckedInJdk8(), KOTLINC, KotlinTargetVersion.JAVA_8)
+                    .addSourceFiles(
+                        getFilesInTestFolderRelativeToClass(
+                            KotlinInlineFunctionRetraceTest.class, "kt", ".kt"))
+                    .compile())
+            .addProgramFiles(ToolHelper.getKotlinStdlibJar())
+            .addKeepAttributes("SourceFile", "LineNumberTable")
+            .setMode(CompilationMode.RELEASE)
+            .addKeepMainRule(main)
+            .setMinApi(parameters.getApiLevel())
+            .run(parameters.getRuntime(), main)
+            .assertFailureWithErrorThatMatches(containsString("inlineExceptionInstance"))
+            .assertFailureWithErrorThatMatches(
+                containsString("at retrace.MainInstanceKt.main(MainInstance.kt:2)"));
+    List<String> retrace = result.retrace();
+    // TODO(b/141817471): Change the tracing information when solved.
+    // assertThat(retrace.get(1), containsString("at
+    // retrace.MainInstanceKt.main(MainInstance.kt:13)"));
+  }
+
+  @Test
+  public void testRetraceKotlinNestedInlineFunction()
+      throws ExecutionException, CompilationFailedException, IOException {
+    // TODO(b/141817471): Change the tracing information when solved.
+    int lineNumber = parameters.isCfRuntime() ? 4 : 3;
+    String main = "retrace.MainNestedKt";
+    R8TestRunResult result =
+        testForR8(parameters.getBackend())
+            .addProgramFiles(
+                kotlinc(TestRuntime.getCheckedInJdk8(), KOTLINC, KotlinTargetVersion.JAVA_8)
+                    .addSourceFiles(
+                        getFilesInTestFolderRelativeToClass(
+                            KotlinInlineFunctionRetraceTest.class, "kt", ".kt"))
+                    .compile())
+            .addProgramFiles(ToolHelper.getKotlinStdlibJar())
+            .addKeepAttributes("SourceFile", "LineNumberTable")
+            .setMode(CompilationMode.RELEASE)
+            .addKeepMainRule(main)
+            .setMinApi(parameters.getApiLevel())
+            .run(parameters.getRuntime(), main)
+            .assertFailureWithErrorThatMatches(containsString("inlineExceptionStatic"))
+            .assertFailureWithErrorThatMatches(
+                containsString("at retrace.MainNestedKt.main(MainNested.kt:" + lineNumber + ")"));
+    List<String> retrace = result.retrace();
+    // TODO(b/141817471): Change the tracing information when solved.
+    // assertThat(retrace.get(1), containsString("at retrace.MainNestedKt.main(MainNested.kt:19)"));
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/retrace/kt/InlineFunction.kt b/src/test/java/com/android/tools/r8/retrace/kt/InlineFunction.kt
new file mode 100644
index 0000000..ceb2656
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/retrace/kt/InlineFunction.kt
@@ -0,0 +1,18 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package retrace
+
+inline fun inlineExceptionStatic(f: () -> Unit) {
+  println("in inlineExceptionStatic")
+  throw java.lang.Exception("inlineExceptionStatic")
+  println("will not be printed")
+}
+
+class InlineFunction {
+  inline fun inlineExceptionInstance(f: () -> Unit) {
+    println("in inlineExceptionInstance")
+    throw java.lang.Exception("inlineExceptionInstance")
+    println("will not be printed")
+  }
+}
\ No newline at end of file
diff --git a/src/test/java/com/android/tools/r8/retrace/kt/Main.kt b/src/test/java/com/android/tools/r8/retrace/kt/Main.kt
new file mode 100644
index 0000000..1c97080
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/retrace/kt/Main.kt
@@ -0,0 +1,12 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package retrace
+
+// Adding a few spaces to better see where the debug information is positioned.
+
+fun main(args: Array<String>) {
+  inlineExceptionStatic {
+    throw Exception("Never get's here")
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/retrace/kt/MainInstance.kt b/src/test/java/com/android/tools/r8/retrace/kt/MainInstance.kt
new file mode 100644
index 0000000..38a781f
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/retrace/kt/MainInstance.kt
@@ -0,0 +1,10 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package retrace
+
+fun main(args: Array<String>) {
+  InlineFunction().inlineExceptionInstance {
+    throw Exception("Never get's here")
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/retrace/kt/MainNested.kt b/src/test/java/com/android/tools/r8/retrace/kt/MainNested.kt
new file mode 100644
index 0000000..f8329be
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/retrace/kt/MainNested.kt
@@ -0,0 +1,14 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package retrace
+
+// Some spaces to better see if retrace is working as expected.
+
+
+fun main(args: Array<String>) {
+  nestedInline {
+    throw Exception("Never get's here")
+  }
+}
+
diff --git a/src/test/java/com/android/tools/r8/retrace/kt/MainWithMultipleInlines.kt b/src/test/java/com/android/tools/r8/retrace/kt/MainWithMultipleInlines.kt
new file mode 100644
index 0000000..c968da4
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/retrace/kt/MainWithMultipleInlines.kt
@@ -0,0 +1,18 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package retrace
+
+fun main(args: Array<String>) {
+  println("Before")
+  inlineExceptionStatic {
+    throw Exception("Never get's here")
+  }
+  println("Middle")
+  inlineExceptionStatic {
+    throw Exception("Never get's here")
+  }
+  println("After")
+}
+
+
diff --git a/src/test/java/com/android/tools/r8/retrace/kt/NestedInlineFunction.kt b/src/test/java/com/android/tools/r8/retrace/kt/NestedInlineFunction.kt
new file mode 100644
index 0000000..d8e4736
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/retrace/kt/NestedInlineFunction.kt
@@ -0,0 +1,11 @@
+// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package retrace
+
+inline fun nestedInline(f: () -> Unit) {
+  println("in nestedInline")
+  inlineExceptionStatic(f)
+  println("will never be printed")
+}
+