Skip to content

Commit 89f7ebf

Browse files
committed
Sema: @cdecl functions C name defaults to the Swift name
1 parent 2601ff4 commit 89f7ebf

File tree

5 files changed

+12
-6
lines changed

5 files changed

+12
-6
lines changed

lib/AST/Decl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4543,7 +4543,10 @@ StringRef ValueDecl::getCDeclName() const {
45434543

45444544
// Handle explicit cdecl attributes.
45454545
if (auto cdeclAttr = getAttrs().getAttribute<CDeclAttr>()) {
4546-
return cdeclAttr->Name;
4546+
if (!cdeclAttr->Name.empty())
4547+
return cdeclAttr->Name;
4548+
else
4549+
return getBaseIdentifier().str();
45474550
}
45484551

45494552
return "";

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ std::string SILDeclRef::mangle(ManglingKind MKind) const {
13071307
if (!clangMangling.empty())
13081308
return clangMangling;
13091309
}
1310-
return CDeclA->Name.str();
1310+
return getDecl()->getCDeclName().str();
13111311
}
13121312

13131313
if (SKind == ASTMangler::SymbolKind::DistributedThunk) {

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,8 +2387,8 @@ void AttributeChecker::visitCDeclAttr(CDeclAttr *attr) {
23872387
diagnose(attr->getLocation(), diag::cdecl_not_at_top_level,
23882388
attr);
23892389

2390-
// The name must not be empty.
2391-
if (attr->Name.empty())
2390+
// @_cdecl name must not be empty.
2391+
if (attr->Name.empty() && attr->Underscored)
23922392
diagnose(attr->getLocation(), diag::cdecl_empty_name,
23932393
attr);
23942394

test/PrintAsObjC/cdecl-official.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func a0_simple(x: Int, bar y: Int) -> Int { return x }
3333
// CHECK-LABEL: // My documentation
3434
// CHECK-LABEL: SWIFT_EXTERN ptrdiff_t simple(ptrdiff_t x, ptrdiff_t y) SWIFT_NOEXCEPT SWIFT_WARN_UNUSED_RESULT;
3535

36+
@cdecl
37+
func a1_defaultName(x: Int) -> Int { return x }
38+
// CHECK-LABEL: SWIFT_EXTERN ptrdiff_t a1_defaultName(ptrdiff_t x) SWIFT_NOEXCEPT SWIFT_WARN_UNUSED_RESULT;
39+
3640
@cdecl("primitiveTypes")
3741
public func b_primitiveTypes(i: Int, ci: CInt, l: CLong, c: CChar, f: Float, d: Double, b: Bool) {}
3842
// CHECK-LABEL: SWIFT_EXTERN void primitiveTypes(ptrdiff_t i, int ci, long l, char c, float f, double d, bool b) SWIFT_NOEXCEPT;

test/attr/attr_cdecl_official.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
// expected-error @-1 {{expected ')' in 'cdecl' attribute}}
2525
// expected-error @-2 {{expected declaration}}
2626

27-
@cdecl("") // expected-error{{@cdecl symbol name cannot be empty}}
28-
func emptyName(x: Int) -> Int { return x }
27+
@cdecl func defaultName() {}
2928

3029
@cdecl("noBody")
3130
func noBody(x: Int) -> Int // expected-error{{expected '{' in body of function}}

0 commit comments

Comments
 (0)