--- old/make/copy/Copy-jdk.jextract.gmk 2019-04-05 16:11:08.000000000 +0530 +++ new/make/copy/Copy-jdk.jextract.gmk 2019-04-05 16:11:07.000000000 +0530 @@ -27,11 +27,18 @@ ################################################################################ -$(eval $(call SetupCopyFiles, COPY_JEXTRACT_HEADERS, \ +$(eval $(call SetupCopyFiles, COPY_CLANG_HEADERS, \ DEST := $(CONF_DST_DIR)/jextract, \ FILES := $(wildcard $(CLANG_INCLUDE_AUX_PATH)/*.h), \ )) -TARGETS := $(COPY_JEXTRACT_HEADERS) +JEXTRACT_HEADERS_SRC := $(TOPDIR)/src/jdk.jextract/share/conf + +$(eval $(call SetupCopyFiles, COPY_JEXTRACT_HEADERS, \ + DEST := $(CONF_DST_DIR)/jextract, \ + FILES := $(wildcard $(JEXTRACT_HEADERS_SRC)/*.h), \ +)) + +TARGETS := $(COPY_CLANG_HEADERS) $(COPY_JEXTRACT_HEADERS) ################################################################################ --- old/src/jdk.jextract/share/classes/com/sun/tools/jextract/AsmCodeFactory.java 2019-04-05 16:11:11.000000000 +0530 +++ new/src/jdk.jextract/share/classes/com/sun/tools/jextract/AsmCodeFactory.java 2019-04-05 16:11:10.000000000 +0530 @@ -161,7 +161,7 @@ SourceLocation src = tree.location(); SourceLocation.Location loc = src.getFileLocation(); Path p = loc.path(); - av.visit("file", p == null ? "builtin" : p.toAbsolutePath().toString()); + av.visit("file", p == null ? "" : p.toAbsolutePath().toString()); av.visit("line", loc.line()); av.visit("column", loc.column()); av.visitEnd(); @@ -199,7 +199,7 @@ SourceLocation src = tree.location(); SourceLocation.Location loc = src.getFileLocation(); Path p = loc.path(); - av.visit("file", p == null ? "builtin" : p.toAbsolutePath().toString()); + av.visit("file", p == null ? "" : p.toAbsolutePath().toString()); av.visit("line", loc.line()); av.visit("column", loc.column()); av.visitEnd(); @@ -249,7 +249,7 @@ AnnotationVisitor av = mv.visitAnnotation(NATIVE_LOCATION, true); SourceLocation.Location loc = src.getFileLocation(); Path p = loc.path(); - av.visit("file", p == null ? "builtin" : p.toAbsolutePath().toString()); + av.visit("file", p == null ? "" : p.toAbsolutePath().toString()); av.visit("line", loc.line()); av.visit("column", loc.column()); av.visitEnd(); @@ -444,7 +444,7 @@ SourceLocation src = funcTree.location(); SourceLocation.Location loc = src.getFileLocation(); Path p = loc.path(); - av.visit("file", p == null ? "builtin" : p.toAbsolutePath().toString()); + av.visit("file", p == null ? "" : p.toAbsolutePath().toString()); av.visit("line", loc.line()); av.visit("column", loc.column()); av.visitEnd(); --- old/src/jdk.jextract/share/classes/com/sun/tools/jextract/Context.java 2019-04-05 16:11:17.000000000 +0530 +++ new/src/jdk.jextract/share/classes/com/sun/tools/jextract/Context.java 2019-04-05 16:11:15.000000000 +0530 @@ -55,4 +55,8 @@ public static Path getBuiltinHeadersDir() { return Paths.get(System.getProperty("java.home"), "conf", "jextract"); } + + public static Path getBuiltinHeaderFile() { + return getBuiltinHeadersDir().resolve("builtin$.h"); + } } --- old/src/jdk.jextract/share/classes/com/sun/tools/jextract/HeaderResolver.java 2019-04-05 16:11:21.000000000 +0530 +++ new/src/jdk.jextract/share/classes/com/sun/tools/jextract/HeaderResolver.java 2019-04-05 16:11:20.000000000 +0530 @@ -38,10 +38,12 @@ // The header file parsed private final Map headerMap = new LinkedHashMap<>(); private final Log log; + private final Path builtinHeader; public HeaderResolver(Context ctx) { this.log = ctx.log; usePackageForFolder(Context.getBuiltinHeadersDir(), "clang_support"); + this.builtinHeader = Context.getBuiltinHeaderFile(); ctx.sources.stream() .map(Path::getParent) .forEach(p -> usePackageForFolder(p, ctx.options.targetPackage)); @@ -144,6 +146,10 @@ } public HeaderFile headerFor(Path path) { + if (path == null) { + path = builtinHeader; + } + return headerMap.computeIfAbsent(path.normalize().toAbsolutePath(), this::getHeaderFile); } } --- old/src/jdk.jextract/share/classes/com/sun/tools/jextract/JavaSourceFactory.java 2019-04-05 16:11:24.000000000 +0530 +++ new/src/jdk.jextract/share/classes/com/sun/tools/jextract/JavaSourceFactory.java 2019-04-05 16:11:23.000000000 +0530 @@ -168,7 +168,7 @@ SourceLocation.Location loc = src.getFileLocation(); Path p = loc.path(); Map fields = new HashMap<>(); - fields.put("file", p == null ? "builtin" : p.toAbsolutePath().toString().replace("\\", "\\\\")); + fields.put("file", p == null ? "" : p.toAbsolutePath().toString().replace("\\", "\\\\")); fields.put("line", loc.line()); fields.put("column", loc.column()); jsb.addAnnotation(align, NATIVE_LOCATION, fields); --- old/src/jdk.jextract/share/classes/com/sun/tools/jextract/TypedefHandler.java 2019-04-05 16:11:27.000000000 +0530 +++ new/src/jdk.jextract/share/classes/com/sun/tools/jextract/TypedefHandler.java 2019-04-05 16:11:26.000000000 +0530 @@ -126,7 +126,7 @@ Cursor dc = defTree.cursor(); List trees = replacements.computeIfAbsent(dc, k -> new ArrayList<>()); trees.add(((StructTree)defTree).withName(tt.name())); - log.print(Level.FINE, () -> "Typedef " + defTree.type().spelling() + " as " + tt.name()); + log.print(Level.FINE, () -> "Typedef " + defTree.type().spelling() + " as " + tt.name()); return null; } else if (defTree.name().equals(tt.name())) { /* @@ -140,6 +140,16 @@ } } + /* + * There are typedefs on built-in types like struct __va_list_tag. These + * are not exposed as declaration cursor for any headers, but are available + * from Type objects. We've to walk and check null file path to detect these + * Cursors and create Trees. + */ + Utils.getBuiltinRecordTypes(tt.type()).forEach(c -> { + decls.add(treeMaker.createTree(c)); + }); + decls.add(tt); return null; } --- old/src/jdk.jextract/share/classes/com/sun/tools/jextract/Utils.java 2019-04-05 16:11:30.000000000 +0530 +++ new/src/jdk.jextract/share/classes/com/sun/tools/jextract/Utils.java 2019-04-05 16:11:29.000000000 +0530 @@ -27,6 +27,7 @@ import java.foreign.layout.Layout; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -36,6 +37,7 @@ import jdk.internal.clang.Cursor; import jdk.internal.clang.CursorKind; +import jdk.internal.clang.SourceLocation; import jdk.internal.clang.Type; import com.sun.tools.jextract.tree.LayoutUtils; import jdk.internal.clang.TypeKind; @@ -184,6 +186,56 @@ } } + // return builtin Record types accessible from the given Type + public static Stream getBuiltinRecordTypes(Type type) { + List recordTypes = new ArrayList<>(); + fillBuiltinRecordTypes(type, recordTypes); + return recordTypes.stream().distinct(); + } + + private static void fillBuiltinRecordTypes(Type type, List recordTypes) { + switch (type.kind()) { + case ConstantArray: + case IncompleteArray: + fillBuiltinRecordTypes(type.getElementType(), recordTypes); + break; + + case FunctionProto: + case FunctionNoProto: { + final int numArgs = type.numberOfArgs(); + for (int i = 0; i < numArgs; i++) { + fillBuiltinRecordTypes(type.argType(i), recordTypes); + } + fillBuiltinRecordTypes(type.resultType(), recordTypes); + } + break; + + case Record: { + Cursor c = type.getDeclarationCursor(); + if (c.isDefinition()) { + SourceLocation sloc = c.getSourceLocation(); + if (sloc != null && sloc.getFileLocation().path() == null) { + recordTypes.add(c); + } + } + } + break; + + case BlockPointer: + case Pointer: + fillBuiltinRecordTypes(type.getPointeeType().canonicalType(), recordTypes); + break; + + case Unexposed: + case Elaborated: + case Typedef: + fillBuiltinRecordTypes(type.canonicalType(), recordTypes); + break; + + default: // nothing to do + } + } + // return the absolute path of the library of given name by searching // in the given array of paths. public static Optional findLibraryPath(Path[] paths, String libName) { --- /dev/null 2019-04-05 16:11:33.000000000 +0530 +++ new/src/jdk.jextract/share/conf/builtin$.h 2019-04-05 16:11:32.000000000 +0530 @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +// Dummy header file for clang compiler builtins --- /dev/null 2019-04-05 16:11:36.000000000 +0530 +++ new/test/jdk/com/sun/tools/jextract/test8222025/ValistUseTest.java 2019-04-05 16:11:34.000000000 +0530 @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.nio.file.Path; +import java.io.IOException; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertTrue; + +/* + * @test + * @bug 8222025 + * @summary jextract generates reference to underfined type for va_list + * @library .. + * @run testng ValistUseTest + */ +public class ValistUseTest extends JextractToolRunner { + @Test + public void test() throws IOException { + Path vaListUseJar = getOutputFilePath("test8222025.jar"); + deleteFile(vaListUseJar); + Path va_list_use_H = getInputFilePath("va_list_use.h"); + run("-o", vaListUseJar.toString(), + va_list_use_H.toString()).checkSuccess(); + try { + Loader loader = classLoader(vaListUseJar); + Class vaListTag = loader.loadClass("clang_support.builtin$$__va_list_tag"); + assertTrue(vaListTag != null); + } finally { + deleteFile(vaListUseJar); + } + } +} --- /dev/null 2019-04-05 16:11:39.000000000 +0530 +++ new/test/jdk/com/sun/tools/jextract/test8222025/va_list_use.h 2019-04-05 16:11:37.000000000 +0530 @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#ifdef _WIN64 +#define EXPORT __declspec(dllexport) +#else +#define EXPORT +#endif + +#include + +EXPORT void func(const char* fmt, va_list list); + +#ifdef __cplusplus +} +#endif // __cplusplus