Skip to content

[6.2][cxx-interop] Support _LIBCPP_PREFERRED_OVERLOAD #82106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release/6.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/AST/ModuleNameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "swift/AST/NameLookup.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/Basic/Assertions.h"
#include "clang/AST/Attr.h"
#include "clang/AST/ExprCXX.h"
#include "llvm/Support/raw_ostream.h"

using namespace swift;
Expand Down Expand Up @@ -278,6 +280,43 @@ void ModuleNameLookup<LookupStrategy>::lookupInModule(
if (decls.size() - initialCount <= 1)
return;

// Some functions like `memchr` are defined both in libc and in libc++.
// Importing both would result in ambiguities, but there are some attributes
// that mark the preferred overloads. See _LIBCPP_PREFERRED_OVERLOAD.
llvm::SmallPtrSet<ValueDecl *, 4> declsToRemove;
bool hasPreferredOverload = false;
for (auto decl : decls)
if (const auto *clangDecl = decl->getClangDecl()) {
if (clangDecl->hasAttr<clang::EnableIfAttr>()) {
// FIXME: at some point we might want to call into Clang to implement
// the full enable_if semantics including the constant evaluation of the
// conditions. For now, just look for the first enable_if(true, "...")
// and assume all the rest of the enable_ifs evaluate to true.
bool thisDeclHasPreferredOverload = false;
for (auto clangAttr :
clangDecl->specific_attrs<clang::EnableIfAttr>()) {
if (auto litExpr =
dyn_cast<clang::CXXBoolLiteralExpr>(clangAttr->getCond())) {
if (litExpr->getValue()) {
thisDeclHasPreferredOverload = hasPreferredOverload = true;
break;
}
}
}
if (!thisDeclHasPreferredOverload)
declsToRemove.insert(decl);
} else
declsToRemove.insert(decl);
}

if (hasPreferredOverload) {
decls.erase(std::remove_if(decls.begin() + initialCount, decls.end(),
[&](ValueDecl *d) -> bool {
return declsToRemove.contains(d);
}),
decls.end());
}

// Remove duplicated declarations, which can happen when the same module is
// imported with multiple access paths.
llvm::SmallPtrSet<ValueDecl *, 4> knownDecls;
Expand Down
3 changes: 3 additions & 0 deletions test/Interop/Cxx/modules/Inputs/ambiguous_a.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void f(int a);
3 changes: 3 additions & 0 deletions test/Interop/Cxx/modules/Inputs/ambiguous_b.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void f(int a) __attribute__((enable_if(a > 0, ""))) __attribute__((enable_if(true, "")));
10 changes: 10 additions & 0 deletions test/Interop/Cxx/modules/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ module TopLevelModule {
}
export *
}

module AmbiguousA {
header "ambiguous_a.h"
requires cplusplus
}

module AmbiguousB {
header "ambiguous_a.h"
requires cplusplus
}
8 changes: 8 additions & 0 deletions test/Interop/Cxx/modules/preferred-overload.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-typecheck-verify-swift -I %S/Inputs -cxx-interoperability-mode=default

import AmbiguousA
import AmbiguousB

func g(_ x: CInt) {
f(x)
}
13 changes: 13 additions & 0 deletions test/Interop/Cxx/objc-correctness/memchr.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %target-swift-frontend -cxx-interoperability-mode=default -typecheck -verify -I %S/Inputs %s

// REQUIRES: objc_interop
// REQUIRES: VENDOR=apple

import Darwin

func test_memchr() {
var src = "hello"
var _ = src.withUTF8 { srcBuf in
Darwin.memchr(srcBuf.baseAddress!, 137, src.count)
}
}