Skip to content

Commit d386e81

Browse files
committed
[cxx-interop] Introduce APINotes file for C++ stdlib
This adds an `std.apinotes` file that is installed into `lib/swift/apinotes` along with existing Darwin apinotes. This new file is installed on all platforms. It replaces a few special cases in the compiler for `cmath` and `cstring` functions. This does not require the upcoming APINotes support for namespaces, however, this does require swiftlang/llvm-project#7309. rdar://107572302
1 parent 15c4822 commit d386e81

File tree

3 files changed

+40
-22
lines changed

3 files changed

+40
-22
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,24 +3215,6 @@ namespace {
32153215

32163216
if (Impl.SwiftContext.LangOpts.EnableCXXInterop &&
32173217
!isa<clang::CXXMethodDecl>(decl)) {
3218-
// Do not import math functions from the C++ standard library, as
3219-
// they're also imported from the Darwin/Glibc module, and their
3220-
// presence in the C++ standard library will cause overloading
3221-
// ambiguities or other type checking errors in Swift.
3222-
auto isAlternativeCStdlibFunctionFromTextualHeader =
3223-
[this](const clang::FunctionDecl *d) -> bool {
3224-
// stdlib.h might be a textual header in libc++'s module map.
3225-
// in this case, check for known ambiguous functions by their name
3226-
// instead of checking if they come from the `std` module.
3227-
if (!d->getDeclName().isIdentifier())
3228-
return false;
3229-
if (d->getName() == "abs" || d->getName() == "div")
3230-
return true;
3231-
if (Impl.SwiftContext.LangOpts.Target.isOSDarwin())
3232-
return d->getName() == "strstr" || d->getName() == "sin" ||
3233-
d->getName() == "cos" || d->getName() == "exit";
3234-
return false;
3235-
};
32363218
auto topLevelModuleEq =
32373219
[](const clang::FunctionDecl *d, StringRef n) -> bool {
32383220
return d->getOwningModule() &&
@@ -3241,9 +3223,6 @@ namespace {
32413223
->getFullModuleName() == n;
32423224
};
32433225
if (topLevelModuleEq(decl, "std")) {
3244-
if (isAlternativeCStdlibFunctionFromTextualHeader(decl)) {
3245-
return nullptr;
3246-
}
32473226
auto filename =
32483227
Impl.getClangPreprocessor().getSourceManager().getFilename(
32493228
decl->getLocation());

stdlib/public/Cxx/std/CMakeLists.txt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
#
2+
# API Notes for the C++ Standard Library
3+
#
4+
set(output_dir "${SWIFTLIB_DIR}/apinotes")
5+
add_custom_target(CxxStdlib-apinotes
6+
COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${output_dir}"
7+
COMMAND ${CMAKE_COMMAND} "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/std.apinotes" "${output_dir}"
8+
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/std.apinotes
9+
COMMENT "Copying CxxStdlib API Notes to ${output_dir}")
10+
11+
swift_install_in_component(FILES std.apinotes
12+
DESTINATION "lib/swift/apinotes"
13+
COMPONENT compiler)
14+
15+
set_property(TARGET CxxStdlib-apinotes PROPERTY FOLDER "Miscellaneous")
16+
add_dependencies(sdk-overlay CxxStdlib-apinotes)
17+
add_dependencies(compiler CxxStdlib-apinotes)
18+
19+
120
# Swift compiler currently assumes that the Darwin overlay is a dependency of
221
# CxxStdlib, and fails to build CxxStdlib if Darwin.swiftmodule in build dir
322
# is built with a different (older) version of the compiler. To workaround this,
@@ -34,4 +53,4 @@ add_swift_target_library(swiftCxxStdlib STATIC NO_LINK_NAME IS_STDLIB IS_SWIFT_O
3453
TARGET_SDKS ALL_APPLE_PLATFORMS LINUX WINDOWS
3554
INSTALL_IN_COMPONENT compiler
3655
INSTALL_WITH_SHARED
37-
DEPENDS libstdcxx-modulemap libcxxshim_modulemap)
56+
DEPENDS libstdcxx-modulemap libcxxshim_modulemap CxxStdlib-apinotes)

stdlib/public/Cxx/std/std.apinotes

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Name: std
2+
Functions:
3+
- Name: abs
4+
Availability: nonswift
5+
AvailabilityMsg: Use the C standard library function
6+
- Name: cos
7+
Availability: nonswift
8+
AvailabilityMsg: Use the C standard library function
9+
- Name: div
10+
Availability: nonswift
11+
AvailabilityMsg: Use the C standard library function
12+
- Name: exit
13+
Availability: nonswift
14+
AvailabilityMsg: Use the C standard library function
15+
- Name: sin
16+
Availability: nonswift
17+
AvailabilityMsg: Use the C standard library function
18+
- Name: strstr
19+
Availability: nonswift
20+
AvailabilityMsg: Use the C standard library function

0 commit comments

Comments
 (0)