Skip to content

Commit d9566c6

Browse files
committed
ASTGen: Accept @cdecl
1 parent 89f7ebf commit d9566c6

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
@@ -970,11 +970,12 @@ BridgedBackDeployedAttr BridgedBackDeployedAttr_createParsed(
970970
BridgedSourceRange cRange, BridgedPlatformKind cPlatform,
971971
BridgedVersionTuple cVersion);
972972

973-
SWIFT_NAME("BridgedCDeclAttr.createParsed(_:atLoc:range:name:)")
973+
SWIFT_NAME("BridgedCDeclAttr.createParsed(_:atLoc:range:name:underscored:)")
974974
BridgedCDeclAttr BridgedCDeclAttr_createParsed(BridgedASTContext cContext,
975975
BridgedSourceLoc cAtLoc,
976976
BridgedSourceRange cRange,
977-
BridgedStringRef cName);
977+
BridgedStringRef cName,
978+
bool underscored);
978979

979980
SWIFT_NAME(
980981
"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
@@ -535,20 +535,37 @@ extension ASTGenVisitor {
535535
/// E.g.:
536536
/// ```
537537
/// @_cdecl("c_function_name")
538+
/// @cdecl(c_function_name)
539+
/// @cdecl
538540
/// ```
539541
func generateCDeclAttr(attribute node: AttributeSyntax) -> BridgedCDeclAttr? {
540-
self.generateWithLabeledExprListArguments(attribute: node) { args in
541-
guard let name = self.generateConsumingSimpleStringLiteralAttrOption(args: &args) else {
542+
let attrName = node.attributeName.as(IdentifierTypeSyntax.self)?.name.text
543+
let underscored = attrName?.hasPrefix("_") ?? false
544+
545+
var name: BridgedStringRef? = nil
546+
if node.arguments != nil || underscored {
547+
name = self.generateWithLabeledExprListArguments(attribute: node) {
548+
args in
549+
if underscored {
550+
self.generateConsumingSimpleStringLiteralAttrOption(args: &args)
551+
} else {
552+
self.generateConsumingPlainIdentifierAttrOption(args: &args) {
553+
return $0.rawText.bridged
554+
}
555+
}
556+
}
557+
guard name != nil else {
542558
return nil
543559
}
544-
545-
return .createParsed(
546-
self.ctx,
547-
atLoc: self.generateSourceLoc(node.atSign),
548-
range: self.generateAttrSourceRange(node),
549-
name: name
550-
)
551560
}
561+
562+
return .createParsed(
563+
self.ctx,
564+
atLoc: self.generateSourceLoc(node.atSign),
565+
range: self.generateAttrSourceRange(node),
566+
name: name ?? "",
567+
underscored: underscored
568+
)
552569
}
553570

554571
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 LifetimeDependence \
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 LifetimeDependence \
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 LifetimeDependence \
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_LifetimeDependence
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)