1 /*
2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @modules jdk.incubator.code
27 * @run junit TestLocation
28 */
29
30 import jdk.incubator.code.Reflect;
31 import jdk.incubator.code.CodeTransformer;
32 import jdk.incubator.code.Location;
33 import jdk.incubator.code.Op;
34 import jdk.incubator.code.dialect.core.CoreOp;
35 import jdk.incubator.code.dialect.java.JavaOp;
36 import jdk.incubator.code.extern.OpParser;
37 import jdk.incubator.code.extern.OpWriter;
38 import org.junit.jupiter.api.Assertions;
39 import org.junit.jupiter.api.Test;
40
41 import java.io.StringWriter;
42 import java.lang.reflect.Method;
43 import java.util.Optional;
44 import java.util.stream.Stream;
45
46 public class TestLocation {
47 @Test
48 public void testLocation() {
49 CoreOp.FuncOp f = getFuncOp(ClassWithReflectedMethod.class, "f");
50 f.elements().forEach(ce -> {
51 if (ce instanceof CoreOp.ConstantOp cop) {
52 Location loc = cop.location();
53 Assertions.assertNotNull(loc);
54
55 int actualLine = loc.line();
56 int expectedLine = Integer.parseInt((String) cop.value());
57 Assertions.assertEquals(expectedLine, actualLine);
58 }
59 });
60 }
61
62 @Reflect
63 static int f(int m, int n) {
64 int sum = 0;
65 for (int i = 0; i < m; i++) {
66 for (int j = 0; j < n; j++) {
67 sum++;
68 }
69 }
70 return sum;
71 }
72
73 @Test
74 public void dropLocationTransform() {
75 CoreOp.FuncOp f = getFuncOp(TestLocation.class, "f");
76
77 CoreOp.FuncOp tf = f.transform(CodeTransformer.DROP_LOCATION_TRANSFORMER);
78 tf.setLocation(Location.NO_LOCATION);
79 testNoLocations(tf);
80
81 CoreOp.FuncOp tlf = lower(f).transform(CodeTransformer.DROP_LOCATION_TRANSFORMER);
82 tlf.setLocation(Location.NO_LOCATION);
83 testNoLocations(tlf);
84 }
85
86 @Test
87 public void dropLocationWriter() {
88 CoreOp.FuncOp f = getFuncOp(TestLocation.class, "f");
89
90 StringWriter w = new StringWriter();
91 OpWriter.writeTo(w, f, OpWriter.LocationOption.DROP_LOCATION);
92 String tfText = w.toString();
93 CoreOp.FuncOp tf = (CoreOp.FuncOp) OpParser.fromString(JavaOp.JAVA_DIALECT_FACTORY, tfText).getFirst();
94 testNoLocations(tf);
95 }
96
97 static CoreOp.FuncOp lower(CoreOp.FuncOp f) {
98 return f.transform(CodeTransformer.LOWERING_TRANSFORMER);
99 }
100
101 static void testNoLocations(Op op) {
102 boolean noLocations = op.elements().filter(ce -> ce instanceof Op)
103 .allMatch(ce -> ((Op) ce).location() == Location.NO_LOCATION);
104 Assertions.assertTrue(noLocations);
105 }
106
107
108 static CoreOp.FuncOp getFuncOp(Class<?> c, String name) {
109 Optional<Method> om = Stream.of(c.getDeclaredMethods())
110 .filter(m -> m.getName().equals(name))
111 .findFirst();
112
113 Method m = om.get();
114 return Op.ofMethod(m).get();
115 }
116 }