Skip to content

Commit 3b33531

Browse files
committed
ASTGen: Accept @cdecl
1 parent 3a0ba5f commit 3b33531

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,11 +949,12 @@ BridgedBackDeployedAttr BridgedBackDeployedAttr_createParsed(
949949
BridgedSourceRange cRange, BridgedPlatformKind cPlatform,
950950
BridgedVersionTuple cVersion);
951951

952-
SWIFT_NAME("BridgedCDeclAttr.createParsed(_:atLoc:range:name:)")
952+
SWIFT_NAME("BridgedCDeclAttr.createParsed(_:atLoc:range:name:underscored:)")
953953
BridgedCDeclAttr BridgedCDeclAttr_createParsed(BridgedASTContext cContext,
954954
BridgedSourceLoc cAtLoc,
955955
BridgedSourceRange cRange,
956-
BridgedStringRef cName);
956+
BridgedStringRef cName,
957+
bool underscored);
957958

958959
SWIFT_NAME(
959960
"BridgedCustomAttr.createParsed(_:atLoc:type:initContext:argumentList:)")

lib/AST/Bridging/DeclAttributeBridging.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,11 @@ BridgedBackDeployedAttr BridgedBackDeployedAttr_createParsed(
247247
BridgedCDeclAttr BridgedCDeclAttr_createParsed(BridgedASTContext cContext,
248248
BridgedSourceLoc cAtLoc,
249249
BridgedSourceRange cRange,
250-
BridgedStringRef cName) {
250+
BridgedStringRef cName,
251+
bool underscored) {
251252
return new (cContext.unbridged())
252253
CDeclAttr(cName.unbridged(), cAtLoc.unbridged(), cRange.unbridged(),
253-
/*Implicit=*/false, /*Underscored*/true);
254+
/*Implicit=*/false, /*Underscored*/underscored);
254255
}
255256

256257
BridgedCustomAttr BridgedCustomAttr_createParsed(

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -531,20 +531,37 @@ extension ASTGenVisitor {
531531
/// E.g.:
532532
/// ```
533533
/// @_cdecl("c_function_name")
534+
/// @cdecl(c_function_name)
535+
/// @cdecl
534536
/// ```
535537
func generateCDeclAttr(attribute node: AttributeSyntax) -> BridgedCDeclAttr? {
536-
self.generateWithLabeledExprListArguments(attribute: node) { args in
537-
guard let name = self.generateConsumingSimpleStringLiteralAttrOption(args: &args) else {
538+
let attrName = node.attributeName.as(IdentifierTypeSyntax.self)?.name.text
539+
let underscored = attrName?.hasPrefix("_") ?? false
540+
541+
var name: BridgedStringRef? = nil
542+
if node.arguments != nil || underscored {
543+
name = self.generateWithLabeledExprListArguments(attribute: node) {
544+
args in
545+
if underscored {
546+
self.generateConsumingSimpleStringLiteralAttrOption(args: &args)
547+
} else {
548+
self.generateConsumingPlainIdentifierAttrOption(args: &args) {
549+
return $0.rawText.bridged
550+
}
551+
}
552+
}
553+
guard name != nil else {
538554
return nil
539555
}
540-
541-
return .createParsed(
542-
self.ctx,
543-
atLoc: self.generateSourceLoc(node.atSign),
544-
range: self.generateAttrSourceRange(node),
545-
name: name
546-
)
547556
}
557+
558+
return .createParsed(
559+
self.ctx,
560+
atLoc: self.generateSourceLoc(node.atSign),
561+
range: self.generateAttrSourceRange(node),
562+
name: name ?? "",
563+
underscored: underscored
564+
)
548565
}
549566

550567
struct GeneratedDerivativeOriginalDecl {

test/ASTGen/attrs.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// RUN: -enable-experimental-feature Lifetimes \
66
// RUN: -enable-experimental-feature RawLayout \
77
// RUN: -enable-experimental-feature SymbolLinkageMarkers \
8+
// RUN: -enable-experimental-feature CDecl \
89
// RUN: -enable-experimental-concurrency \
910
// RUN: -enable-experimental-move-only \
1011
// RUN: -enable-experimental-feature ParserASTGen \
@@ -15,6 +16,7 @@
1516
// RUN: -enable-experimental-feature Lifetimes \
1617
// RUN: -enable-experimental-feature RawLayout \
1718
// RUN: -enable-experimental-feature SymbolLinkageMarkers \
19+
// RUN: -enable-experimental-feature CDecl \
1820
// RUN: -enable-experimental-concurrency \
1921
// RUN: -enable-experimental-move-only \
2022
// RUN: | %sanitize-address > %t/cpp-parser.ast
@@ -28,6 +30,7 @@
2830
// RUN: -enable-experimental-feature Lifetimes \
2931
// RUN: -enable-experimental-feature RawLayout \
3032
// RUN: -enable-experimental-feature SymbolLinkageMarkers \
33+
// RUN: -enable-experimental-feature CDecl \
3134
// RUN: -enable-experimental-concurrency \
3235
// RUN: -enable-experimental-move-only
3336

@@ -39,6 +42,7 @@
3942
// REQUIRES: swift_feature_Lifetimes
4043
// REQUIRES: swift_feature_RawLayout
4144
// REQUIRES: swift_feature_SymbolLinkageMarkers
45+
// REQUIRES: swift_feature_CDecl
4246

4347
// rdar://116686158
4448
// UNSUPPORTED: asan
@@ -95,7 +99,9 @@ func fn(_: Int) {}
9599

96100
@_disallowFeatureSuppression(NoncopyableGenerics) public struct LoudlyNC<T: ~Copyable> {}
97101

98-
@_cdecl("c_function_name") func foo(x: Int) {}
102+
@_cdecl("c_function_name") func cdeclUnderscore(x: Int) {}
103+
@cdecl(c_function_name_official) func cdecl(x: Int) {}
104+
@cdecl func cdeclDefault() {}
99105

100106
struct StaticProperties {
101107
dynamic var property: Int { return 1 }

0 commit comments

Comments
 (0)