1 /*
  2  * Copyright (c) 2020, 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  * @bug 8239808
 27  * @build JextractApiTestBase
 28  * @run testng/othervm --enable-native-access=jdk.incubator.jextract TestAttributes
 29  */
 30 
 31 import java.lang.constant.Constable;
 32 import java.time.LocalDateTime;
 33 import java.time.format.DateTimeFormatter;
 34 import java.util.List;
 35 import java.util.stream.Collectors;
 36 import java.util.stream.Stream;
 37 import jdk.incubator.jextract.Declaration;
 38 import jdk.incubator.jextract.Type;
 39 import org.testng.annotations.Test;
 40 
 41 import static org.testng.Assert.assertEquals;
 42 import static org.testng.Assert.assertTrue;
 43 
 44 public class TestAttributes extends JextractApiTestBase {
 45     private final static Type C_INT = Type.primitive(Type.Primitive.Kind.Int);
 46     private final static String ASMLABEL = "AsmLabelAttr";
 47 
 48     private void validateAsmLabel(Declaration d, boolean isAdd) {
 49         var attrs = d.getAttribute(ASMLABEL).get();
 50         String value = isMacOSX ? "_" : "";
 51         value += d.name();
 52         value += isAdd ? "A" : "B";
 53         assertEquals(attrs.get(0), value);
 54     }
 55 
 56     private void validateHeader(Declaration.Scoped top, boolean isAdd) {
 57         if (isWindows) {
 58             // TODO: add Windows validation
 59             // Simply dump declaration for now
 60             System.out.println(top);
 61             return;
 62         }
 63         var list = top.members().stream()
 64                 .filter(byNameAndType("foo", Declaration.Variable.class))
 65                 .map(Declaration.Variable.class::cast)
 66                 .collect(Collectors.toList());
 67         assertEquals(list.size(), 3);
 68         int hasAttrs = 0;
 69         for (Declaration.Variable foo: list) {
 70             assertEquals(Declaration.Variable.Kind.GLOBAL, foo.kind());
 71             assertTypeEquals(C_INT, foo.type());
 72             if (foo.getAttribute(ASMLABEL).isPresent()) {
 73                 hasAttrs++;
 74                 validateAsmLabel(foo, isAdd);
 75             }
 76         }
 77         assertEquals(hasAttrs, 2);
 78         var listFunc = top.members().stream()
 79                 .filter(byNameAndType("func", Declaration.Function.class))
 80                 .map(Declaration.Function.class::cast)
 81                 .collect(Collectors.toList());
 82         assertEquals(listFunc.size(), 3);
 83         hasAttrs = 0;
 84         for (Declaration.Function func: listFunc) {
 85             checkFunction(func, C_INT, C_INT, C_INT);
 86             if (func.getAttribute(ASMLABEL).isPresent()) {
 87                 hasAttrs++;
 88                 validateAsmLabel(func, isAdd);
 89             }
 90         }
 91         assertEquals(hasAttrs, 2);
 92     }
 93 
 94     @Test
 95     public void testA() {
 96         Declaration.Scoped d = parse("libAsmSymbol.h", "-DADD");
 97         validateHeader(d, true);
 98     }
 99 
100     @Test
101     public void testB() {
102         Declaration.Scoped d = parse("libAsmSymbol.h");
103         validateHeader(d, false);
104     }
105 
106     private static  Constable getSingleValue(Declaration d, String name) {
107         List<Constable> values = d.getAttribute(name).get();
108         assertEquals(1, values.size());
109         return values.get(0);
110     }
111 
112     @Test
113     public void testAddAttribute() {
114         final String ts = "timestamp";
115         Declaration.Scoped d = parse("libAsmSymbol.h");
116         String timestamp = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME);
117         Declaration withAttrs = d.withAttribute("header", d.name())
118                 .withAttribute(ts, timestamp);
119 
120         assertEquals(getSingleValue(withAttrs, "header"), d.name());
121         assertEquals(getSingleValue(withAttrs, ts), timestamp);
122 
123         String timestamp2 = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
124         Declaration withNewAttrs = withAttrs.withAttribute(ts, timestamp2);
125         assertEquals(getSingleValue(withNewAttrs, ts), timestamp2);
126 
127         // Make sure original Declaration is not altered
128         assertEquals(getSingleValue(withAttrs, ts), timestamp);
129 
130         // Add more value to same attribute
131         withNewAttrs = withAttrs.withAttribute(ts, Stream.concat(
132                 withAttrs.getAttribute(ts).map(List::stream).orElse(Stream.empty()),
133                 Stream.of(timestamp2)
134             ).toArray(Constable[]::new));
135         assertEquals(withNewAttrs.getAttribute(ts).get(), List.of(timestamp, timestamp2));
136         assertEquals(getSingleValue(withNewAttrs,"header"), d.name());
137 
138         // Remove attribute
139         withAttrs = withNewAttrs.withAttribute(ts);
140         assertTrue(withAttrs.getAttribute(ts).isEmpty());
141 
142         // Strip attribute
143         withNewAttrs = withNewAttrs.stripAttributes();
144         assertTrue(withNewAttrs.attributeNames().isEmpty());
145     }
146 
147     @Test
148     public void replaceFunctionSymbol() {
149         Declaration.Scoped d = parse("libAsmSymbol.h", "-DADD");
150         validateHeader(d, true);
151 
152         var members = d.members().stream()
153             .map(m -> m.getAttribute(ASMLABEL)
154                     .map(attr -> m.withAttribute(ASMLABEL, attr.get(0).toString().replace('A', 'B')))
155                     .orElse(m))
156             .toArray(Declaration[]::new);
157         Declaration.Scoped patched = Declaration.toplevel(d.pos(), members);
158         validateHeader(patched, false);
159     }
160 }