Debian Codegraph @

test/2262-checker-return-sinking/src/Main.java

raw
1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 public class Main {
18 public static void main(String[] args) {
19 // Switch case.
20 assertEquals("First case", $noinline$testSinkReturnSwitch(1));
21 assertEquals("Second case", $noinline$testSinkReturnSwitch(2));
22 assertEquals("default", $noinline$testSinkReturnSwitch(3));
23 $noinline$testSinkReturnVoidSwitch(1);
24
25 // If/else if/else.
26 assertEquals("First case", $noinline$testSinkReturnIfElse(1));
27 assertEquals("Second case", $noinline$testSinkReturnIfElse(2));
28 assertEquals("default", $noinline$testSinkReturnIfElse(3));
29 $noinline$testSinkReturnVoidIfElse(1);
30
31 // Non-trivial if cases.
32 assertEquals("First case", $noinline$testSinkReturnSeparatedReturns(1));
33 assertEquals("Second case", $noinline$testSinkReturnSeparatedReturns(2));
34 assertEquals("default", $noinline$testSinkReturnSeparatedReturns(3));
35 $noinline$testSinkReturnVoidSeparatedReturns(1);
36 }
37
38 /// CHECK-START: java.lang.String Main.$noinline$testSinkReturnSwitch(int) code_sinking (before)
39 /// CHECK: Return
40 /// CHECK: Return
41 /// CHECK: Return
42 /// CHECK-NOT: Return
43
44 /// CHECK-START: java.lang.String Main.$noinline$testSinkReturnSwitch(int) code_sinking (after)
45 /// CHECK: Return
46 /// CHECK-NOT: Return
47 private static String $noinline$testSinkReturnSwitch(int switch_id) {
48 switch (switch_id) {
49 case 1:
50 return "First case";
51 case 2:
52 return "Second case";
53 default:
54 return "default";
55 }
56 }
57
58 /// CHECK-START: void Main.$noinline$testSinkReturnVoidSwitch(int) code_sinking (before)
59 /// CHECK: ReturnVoid
60 /// CHECK: ReturnVoid
61 /// CHECK: ReturnVoid
62 /// CHECK-NOT: ReturnVoid
63
64 /// CHECK-START: void Main.$noinline$testSinkReturnVoidSwitch(int) code_sinking (after)
65 /// CHECK: ReturnVoid
66 /// CHECK-NOT: ReturnVoid
67 private static void $noinline$testSinkReturnVoidSwitch(int switch_id) {
68 switch (switch_id) {
69 case 1:
70 $noinline$emptyMethod();
71 return;
72 case 2:
73 $noinline$emptyMethod2();
74 return;
75 default:
76 $noinline$emptyMethod3();
77 return;
78 }
79 }
80
81 /// CHECK-START: java.lang.String Main.$noinline$testSinkReturnIfElse(int) code_sinking (before)
82 /// CHECK: Return
83 /// CHECK: Return
84 /// CHECK: Return
85 /// CHECK-NOT: Return
86
87 /// CHECK-START: java.lang.String Main.$noinline$testSinkReturnIfElse(int) code_sinking (after)
88 /// CHECK: Return
89 /// CHECK-NOT: Return
90 private static String $noinline$testSinkReturnIfElse(int id) {
91 if (id == 1) {
92 return "First case";
93 } else if (id == 2) {
94 return "Second case";
95 } else {
96 return "default";
97 }
98 }
99
100 /// CHECK-START: void Main.$noinline$testSinkReturnVoidIfElse(int) code_sinking (before)
101 /// CHECK: ReturnVoid
102 /// CHECK: ReturnVoid
103 /// CHECK: ReturnVoid
104 /// CHECK-NOT: ReturnVoid
105
106 /// CHECK-START: void Main.$noinline$testSinkReturnVoidIfElse(int) code_sinking (after)
107 /// CHECK: ReturnVoid
108 /// CHECK-NOT: ReturnVoid
109 private static void $noinline$testSinkReturnVoidIfElse(int id) {
110 if (id == 1) {
111 $noinline$emptyMethod();
112 return;
113 } else if (id == 2) {
114 $noinline$emptyMethod2();
115 return;
116 } else {
117 $noinline$emptyMethod3();
118 return;
119 }
120 }
121
122 /// CHECK-START: java.lang.String Main.$noinline$testSinkReturnSeparatedReturns(int) code_sinking (before)
123 /// CHECK: Return
124 /// CHECK: Return
125 /// CHECK: Return
126 /// CHECK-NOT: Return
127
128 /// CHECK-START: java.lang.String Main.$noinline$testSinkReturnSeparatedReturns(int) code_sinking (after)
129 /// CHECK: Return
130 /// CHECK-NOT: Return
131 private static String $noinline$testSinkReturnSeparatedReturns(int id) {
132 if (id == 1) {
133 return "First case";
134 }
135 $noinline$emptyMethod();
136
137 if (id == 2) {
138 return "Second case";
139 }
140
141 $noinline$emptyMethod2();
142 return "default";
143 }
144
145 /// CHECK-START: void Main.$noinline$testSinkReturnVoidSeparatedReturns(int) code_sinking (before)
146 /// CHECK: ReturnVoid
147 /// CHECK: ReturnVoid
148 /// CHECK: ReturnVoid
149 /// CHECK-NOT: ReturnVoid
150
151 /// CHECK-START: void Main.$noinline$testSinkReturnVoidSeparatedReturns(int) code_sinking (after)
152 /// CHECK: ReturnVoid
153 /// CHECK-NOT: ReturnVoid
154 private static void $noinline$testSinkReturnVoidSeparatedReturns(int id) {
155 if (id == 1) {
156 return;
157 }
158 $noinline$emptyMethod();
159
160 if (id == 2) {
161 return;
162 }
163
164 $noinline$emptyMethod2();
165 return;
166 }
167
168 private static void $noinline$emptyMethod() {}
169 private static void $noinline$emptyMethod2() {}
170 private static void $noinline$emptyMethod3() {}
171
172 private static void assertEquals(String expected, String result) {
173 if (expected != result) {
174 throw new Error("Expected: " + expected + ", found: " + result);
175 }
176 }
177 }