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 testng TestLocation 28 */ 29 30 import org.testng.Assert; 31 import org.testng.annotations.Test; 32 33 import java.io.StringWriter; 34 import java.lang.reflect.Method; 35 import jdk.incubator.code.Location; 36 import jdk.incubator.code.Op; 37 import jdk.incubator.code.OpTransformer; 38 import jdk.incubator.code.op.CoreOp; 39 import jdk.incubator.code.op.ExtendedOp; 40 import jdk.incubator.code.parser.OpParser; 41 import jdk.incubator.code.writer.OpWriter; 42 import jdk.incubator.code.CodeReflection; 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.traverse(null, (o, ce) -> { 51 if (ce instanceof CoreOp.ConstantOp cop) { 52 Location loc = cop.location(); 53 Assert.assertNotNull(loc); 54 55 int actualLine = loc.line(); 56 int expectedLine = Integer.parseInt((String) cop.value()); 57 Assert.assertEquals(actualLine, expectedLine); 58 } 59 return null; 60 }); 61 } 62 63 @CodeReflection 64 static int f(int m, int n) { 65 int sum = 0; 66 for (int i = 0; i < m; i++) { 67 for (int j = 0; j < n; j++) { 68 sum++; 69 } 70 } 71 return sum; 72 } 73 74 @Test 75 public void dropLocationTransform() { 76 CoreOp.FuncOp f = getFuncOp(TestLocation.class, "f"); 77 78 CoreOp.FuncOp tf = f.transform(OpTransformer.DROP_LOCATION_TRANSFORMER); 79 tf.setLocation(Location.NO_LOCATION); 80 testNoLocations(tf); 81 82 CoreOp.FuncOp tlf = lower(f).transform(OpTransformer.DROP_LOCATION_TRANSFORMER); 83 tlf.setLocation(Location.NO_LOCATION); 84 testNoLocations(tlf); 85 } 86 87 @Test 88 public void dropLocationWriter() { 89 CoreOp.FuncOp f = getFuncOp(TestLocation.class, "f"); 90 91 StringWriter w = new StringWriter(); 92 OpWriter.writeTo(w, f, OpWriter.LocationOption.DROP_LOCATION); 93 String tfText = w.toString(); 94 CoreOp.FuncOp tf = (CoreOp.FuncOp) OpParser.fromString(ExtendedOp.FACTORY, tfText).getFirst(); 95 testNoLocations(tf); 96 } 97 98 static CoreOp.FuncOp lower(CoreOp.FuncOp f) { 99 return f.transform(OpTransformer.LOWERING_TRANSFORMER); 100 } 101 102 static void testNoLocations(Op op) { 103 boolean noLocations = op.elements().filter(ce -> ce instanceof Op) 104 .allMatch(ce -> ((Op) ce).location() == Location.NO_LOCATION); 105 Assert.assertTrue(noLocations); 106 } 107 108 109 static CoreOp.FuncOp getFuncOp(Class<?> c, String name) { 110 Optional<Method> om = Stream.of(c.getDeclaredMethods()) 111 .filter(m -> m.getName().equals(name)) 112 .findFirst(); 113 114 Method m = om.get(); 115 return Op.ofMethod(m).get(); 116 } 117 }