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 8239128
27  * @build JextractApiTestBase
28  * @run testng/othervm --enable-native-access=jdk.incubator.jextract TestMacros
29  */
30 
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import jdk.incubator.jextract.Declaration;
34 import jdk.incubator.jextract.Type;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.Test;
37 
38 public class TestMacros extends JextractApiTestBase {
39     Declaration.Scoped badMacro;
40     Declaration.Scoped foo;
41     Declaration.Scoped bar;
42     private final static Type C_INT = Type.primitive(Type.Primitive.Kind.Int);
43 
44     @BeforeClass
45     public void parse() {
46         // We need stdint.h for pointer macro, otherwise evaluation failed and Constant declaration is not created
47         Path builtinInc = Paths.get(System.getProperty("java.home"), "conf", "jextract");
48         badMacro = parse("badMacros.h", "-I", builtinInc.toString());
49 
50         foo = checkStruct(badMacro, "foo", "ptrFoo", "ptrBar");
51         bar = checkStruct(badMacro, "bar", "ptrFoo", "arFooPtr");
52     }
53 
54     @Test
55     public void testBadMacros() {
56         checkConstant(badMacro, "INVALID_INT_CONSUMER",
57             Type.pointer(Type.function(false, Type.void_(), C_INT)),
58             0L);
59         // Record type in macro definition are erased to void
60         checkConstant(badMacro, "NO_FOO", Type.pointer(Type.declared(foo)), 0L);
61         checkConstant(badMacro, "INVALID_INT_ARRAY_PTR", Type.pointer(Type.pointer(C_INT)), 0L);
62     }
63 
64     @Test
65     public void verifyFunctions() {
66         checkFunction(badMacro, "func", Type.void_(),
67             Type.pointer(Type.declared(bar)), Type.pointer(Type.declared(foo)));
68         checkFunction(badMacro, "withArray", Type.void_(),
69             Type.pointer(Type.typedef("foo_t", Type.pointer(Type.declared(foo)))));
70     }
71 
72     @Test
73     public void verifyGlobals() {
74         checkGlobal(badMacro, "op", Type.pointer(
75                 Type.function(false, Type.void_(), C_INT, Type.pointer(C_INT))));
76     }
77 
78     @Test
79     public void verifyFields() {
80         Type foo_t = Type.typedef("foo_t", Type.pointer(Type.declared(foo)));
81         checkField(foo, "ptrFoo", foo_t);
82         checkField(foo, "ptrBar", Type.pointer(Type.declared(bar)));
83         checkField(bar, "ptrFoo", foo_t);
84         checkField(bar, "arFooPtr", Type.pointer(foo_t));
85     }
86 }