Sema: @cdecl functions C name defaults to the Swift name

This commit is contained in:
Alexis Laferrière
2025-06-10 13:25:25 -07:00
parent 2601ff44d4
commit 89f7ebfb18
5 changed files with 12 additions and 6 deletions

View File

@@ -4543,7 +4543,10 @@ StringRef ValueDecl::getCDeclName() const {
// Handle explicit cdecl attributes.
if (auto cdeclAttr = getAttrs().getAttribute<CDeclAttr>()) {
return cdeclAttr->Name;
if (!cdeclAttr->Name.empty())
return cdeclAttr->Name;
else
return getBaseIdentifier().str();
}
return "";

View File

@@ -1307,7 +1307,7 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
if (!clangMangling.empty())
return clangMangling;
}
return CDeclA->Name.str();
return getDecl()->getCDeclName().str();
}
if (SKind == ASTMangler::SymbolKind::DistributedThunk) {

View File

@@ -2387,8 +2387,8 @@ void AttributeChecker::visitCDeclAttr(CDeclAttr *attr) {
diagnose(attr->getLocation(), diag::cdecl_not_at_top_level,
attr);
// The name must not be empty.
if (attr->Name.empty())
// @_cdecl name must not be empty.
if (attr->Name.empty() && attr->Underscored)
diagnose(attr->getLocation(), diag::cdecl_empty_name,
attr);

View File

@@ -33,6 +33,10 @@ func a0_simple(x: Int, bar y: Int) -> Int { return x }
// CHECK-LABEL: // My documentation
// CHECK-LABEL: SWIFT_EXTERN ptrdiff_t simple(ptrdiff_t x, ptrdiff_t y) SWIFT_NOEXCEPT SWIFT_WARN_UNUSED_RESULT;
@cdecl
func a1_defaultName(x: Int) -> Int { return x }
// CHECK-LABEL: SWIFT_EXTERN ptrdiff_t a1_defaultName(ptrdiff_t x) SWIFT_NOEXCEPT SWIFT_WARN_UNUSED_RESULT;
@cdecl("primitiveTypes")
public func b_primitiveTypes(i: Int, ci: CInt, l: CLong, c: CChar, f: Float, d: Double, b: Bool) {}
// CHECK-LABEL: SWIFT_EXTERN void primitiveTypes(ptrdiff_t i, int ci, long l, char c, float f, double d, bool b) SWIFT_NOEXCEPT;

View File

@@ -24,8 +24,7 @@
// expected-error @-1 {{expected ')' in 'cdecl' attribute}}
// expected-error @-2 {{expected declaration}}
@cdecl("") // expected-error{{@cdecl symbol name cannot be empty}}
func emptyName(x: Int) -> Int { return x }
@cdecl func defaultName() {}
@cdecl("noBody")
func noBody(x: Int) -> Int // expected-error{{expected '{' in body of function}}