Add a test for class inliner in presence of non-trivial constructor control flow
Bug: 140159143
Change-Id: I5d85159b09ba6a8be55b5e6917cecbf821fba6da
diff --git a/src/test/java/com/android/tools/r8/ir/optimize/classinliner/ConstructorWithNonTrivialControlFlowTest.java b/src/test/java/com/android/tools/r8/ir/optimize/classinliner/ConstructorWithNonTrivialControlFlowTest.java
new file mode 100644
index 0000000..779a1ca
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/ir/optimize/classinliner/ConstructorWithNonTrivialControlFlowTest.java
@@ -0,0 +1,95 @@
+// 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.ir.optimize.classinliner;
+
+import static com.android.tools.r8.utils.codeinspector.Matchers.isPresent;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import com.android.tools.r8.NeverInline;
+import com.android.tools.r8.NeverPropagateValue;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.utils.BooleanUtils;
+import com.android.tools.r8.utils.codeinspector.ClassSubject;
+import com.android.tools.r8.utils.codeinspector.CodeInspector;
+import java.util.List;
+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 ConstructorWithNonTrivialControlFlowTest extends TestBase {
+
+ private final boolean enableClassInlining;
+ private final TestParameters parameters;
+
+ @Parameters(name = "{1}, enable class inlining: {0}")
+ public static List<Object[]> data() {
+ return buildParameters(BooleanUtils.values(), getTestParameters().withAllRuntimes().build());
+ }
+
+ public ConstructorWithNonTrivialControlFlowTest(
+ boolean enableClassInlining, TestParameters parameters) {
+ this.enableClassInlining = enableClassInlining;
+ this.parameters = parameters;
+ }
+
+ @Test
+ public void test() throws Exception {
+ testForR8(parameters.getBackend())
+ .addInnerClasses(ConstructorWithNonTrivialControlFlowTest.class)
+ .addKeepMainRule(TestClass.class)
+ .addOptionsModification(options -> options.enableClassInlining = enableClassInlining)
+ .enableInliningAnnotations()
+ .enableMemberValuePropagationAnnotations()
+ .setMinApi(parameters.getRuntime())
+ .compile()
+ .inspect(this::verifyClassInliningRemovesCandidate)
+ .run(parameters.getRuntime(), TestClass.class)
+ .assertSuccess();
+ }
+
+ private void verifyClassInliningRemovesCandidate(CodeInspector inspector) {
+ ClassSubject candidateClassSubject = inspector.clazz(Candidate.class);
+ if (enableClassInlining) {
+ assertThat(candidateClassSubject, not(isPresent()));
+ } else {
+ assertThat(candidateClassSubject, isPresent());
+ }
+ }
+
+ static class TestClass {
+
+ public static void main(String[] args) {
+ new Candidate(args, fourtyTwo());
+ }
+
+ @NeverInline
+ @NeverPropagateValue
+ static int fourtyTwo() {
+ return 42;
+ }
+ }
+
+ static class Candidate {
+
+ Object x;
+ int y;
+
+ Candidate(Object x, int y) {
+ if (x == null || x.toString().isEmpty()) {
+ throw new RuntimeException(
+ "Argument `x` must be non-null and `x.toString()` must be non-empty");
+ }
+ if (y != 42) {
+ throw new RuntimeException("Argument `y` must be 42");
+ }
+ this.x = x;
+ this.y = y;
+ }
+ }
+}