[libcxx-commits] [libcxx] [libc++] Add a few basic tests for C++ / Swift interoperation (PR #104863)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Aug 19 14:27:46 PDT 2024
https://github.com/ldionne created https://github.com/llvm/llvm-project/pull/104863
These tests are only enabled on Apple platforms. The purpose of these tests is to ensure that we don't break existing functionality powered by Clang modules as we refactor the modulemap, since Swift interop greatly exercises libc++'s support for Clang modules.
In particular, adding these tests to libc++ is not an official statement of support from libc++ towards Swift interop (which would require a community consensus that I am not pursuing at this time), but rather a way for us to find out when we unexpectedly break things so we can decide how / whether we want to address such breakage.
Once the refactoring of the modulemap started in #98214 is mostly done, it would be reasonable to remove these tests unless libc++ wants to pursue support for Swift interop more officially.
>From d2246d15b48e20ee8023f362b37b7020aed2baba Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 6 Aug 2024 10:18:25 -0400
Subject: [PATCH] [libc++] Add a few basic tests for C++ / Swift interoperation
These tests are only enabled on Apple platforms. The purpose of these
tests is to ensure that we don't break existing functionality powered
by Clang modules as we refactor the modulemap, since Swift interop
greatly exercises libc++'s support for Clang modules.
In particular, adding these tests to libc++ is not an official statement
of support from libc++ towards Swift interop (which would require a
community consensus that I am not pursuing at this time), but rather a
way for us to find out when we unexpectedly break things so we can decide
how / whether we want to address such breakage.
Once the refactoring of the modulemap started in #98214 is mostly done,
it would be reasonable to remove these tests unless libc++ wants to
pursue support for Swift interop more officially.
---
.../libcxx/vendor/apple/swift-interop.sh.cpp | 21 +++++++++++++++++++
.../apple/swift-interop/string/main.swift | 13 ++++++++++++
.../apple/swift-interop/vector/header.h | 13 ++++++++++++
.../apple/swift-interop/vector/main.swift | 13 ++++++++++++
.../swift-interop/vector/module.modulemap | 4 ++++
5 files changed, 64 insertions(+)
create mode 100755 libcxx/test/libcxx/vendor/apple/swift-interop.sh.cpp
create mode 100755 libcxx/test/libcxx/vendor/apple/swift-interop/string/main.swift
create mode 100644 libcxx/test/libcxx/vendor/apple/swift-interop/vector/header.h
create mode 100644 libcxx/test/libcxx/vendor/apple/swift-interop/vector/main.swift
create mode 100644 libcxx/test/libcxx/vendor/apple/swift-interop/vector/module.modulemap
diff --git a/libcxx/test/libcxx/vendor/apple/swift-interop.sh.cpp b/libcxx/test/libcxx/vendor/apple/swift-interop.sh.cpp
new file mode 100755
index 00000000000000..87c6c80f62a7eb
--- /dev/null
+++ b/libcxx/test/libcxx/vendor/apple/swift-interop.sh.cpp
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// This test requires access to the Swift compiler, which we assume is only available on Apple
+// platforms for now.
+// REQUIRES: buildhost=darwin
+
+// std::string basic test
+// RUN: swiftc -cxx-interoperability-mode=default %S/swift-interop/string/main.swift -o %t.string.exe \
+// RUN: -Xcc -nostdinc++ -Xcc -nostdlib++ -Xcc -isystem -Xcc "%{include-dir}"
+// RUN: %{exec} %t.string.exe
+
+// std::vector test with a modulemap
+// RUN: swiftc -cxx-interoperability-mode=default %S/swift-interop/vector/main.swift -o %t.vector.exe \
+// RUN: -Xcc -nostdinc++ -Xcc -nostdlib++ -Xcc -isystem -Xcc "%{include-dir}" -Xcc -I -Xcc %S/swift-interop/vector
+// RUN: %{exec} %t.vector.exe
diff --git a/libcxx/test/libcxx/vendor/apple/swift-interop/string/main.swift b/libcxx/test/libcxx/vendor/apple/swift-interop/string/main.swift
new file mode 100755
index 00000000000000..1b2e12f839ba7d
--- /dev/null
+++ b/libcxx/test/libcxx/vendor/apple/swift-interop/string/main.swift
@@ -0,0 +1,13 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+import CxxStdlib
+
+let s: std.string = "abc123"
+
+for char in s { print(char) }
diff --git a/libcxx/test/libcxx/vendor/apple/swift-interop/vector/header.h b/libcxx/test/libcxx/vendor/apple/swift-interop/vector/header.h
new file mode 100644
index 00000000000000..de41747de05155
--- /dev/null
+++ b/libcxx/test/libcxx/vendor/apple/swift-interop/vector/header.h
@@ -0,0 +1,13 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <vector>
+
+using VectorOfInt = std::vector<int>;
+
+inline VectorOfInt getStdVector() { return {1, 2, 3}; }
diff --git a/libcxx/test/libcxx/vendor/apple/swift-interop/vector/main.swift b/libcxx/test/libcxx/vendor/apple/swift-interop/vector/main.swift
new file mode 100644
index 00000000000000..6ec3640881424d
--- /dev/null
+++ b/libcxx/test/libcxx/vendor/apple/swift-interop/vector/main.swift
@@ -0,0 +1,13 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+import MyCxxDep // no explicit import of CxxStdlib
+
+let vec = getStdVector()
+
+for it in vec { print(it) }
diff --git a/libcxx/test/libcxx/vendor/apple/swift-interop/vector/module.modulemap b/libcxx/test/libcxx/vendor/apple/swift-interop/vector/module.modulemap
new file mode 100644
index 00000000000000..8dabb571262613
--- /dev/null
+++ b/libcxx/test/libcxx/vendor/apple/swift-interop/vector/module.modulemap
@@ -0,0 +1,4 @@
+module MyCxxDep {
+ header "header.h"
+ export *
+}
More information about the libcxx-commits
mailing list