Skip to content

Commit acb5323

Browse files
committed
[interop] add SWIFT_ prefix to the C++ interop annotations
1 parent 370bfa2 commit acb5323

File tree

9 files changed

+39
-40
lines changed

9 files changed

+39
-40
lines changed

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ NOTE(record_is_dependent, none, "record '%0' is dependent", (StringRef))
165165
NOTE(record_parent_unimportable, none, "record %0's parent is not importable", (StringRef))
166166
NOTE(reference_passed_by_value, none, "function uses foreign reference type "
167167
"'%0' as a value in %1 types which breaks "
168-
"'import_reference' contract (outlined in "
168+
"'swift_shared_reference' contract (outlined in "
169169
"C++ Interop User Manual).",
170170
(StringRef, StringRef))
171171
NOTE(record_not_automatically_importable, none, "record '%0' is not "
@@ -186,10 +186,10 @@ NOTE(projection_reference_not_imported, none, "C++ method '%0' that returns a re
186186
NOTE(projection_may_return_interior_ptr, none, "C++ method '%0' may return an "
187187
"interior pointer. ",
188188
(StringRef))
189-
NOTE(mark_self_contained, none, "Mark type '%0' as 'SELF_CONTAINED' in C++ to "
189+
NOTE(mark_self_contained, none, "Mark type '%0' as 'SWIFT_SELF_CONTAINED' in C++ to "
190190
"make methods that use it available in Swift. ",
191191
(StringRef))
192-
NOTE(mark_safe_to_import, none, "annotate method '%0' with 'RETURNS_INDEPENDENT_VALUE' in C++ to "
192+
NOTE(mark_safe_to_import, none, "annotate method '%0' with 'SWIFT_RETURNS_INDEPENDENT_VALUE' in C++ to "
193193
"make it available in Swift",
194194
(StringRef))
195195

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4409,7 +4409,7 @@ static void diagnoseForeignReferenceTypeFixit(ClangImporter::Implementation &Imp
44094409
auto importedLoc =
44104410
Impl.SwiftContext.getClangModuleLoader()->importSourceLocation(loc.clangLoc);
44114411
Impl.diagnose(loc, diag).fixItInsert(
4412-
importedLoc, "SHARED_REFERENCE(<#retain#>, <#release#>) ");
4412+
importedLoc, "SWIFT_SHARED_REFERENCE(<#retain#>, <#release#>) ");
44134413
}
44144414

44154415
bool ClangImporter::Implementation::emitDiagnosticsForTarget(

lib/ClangImporter/bridging

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
/// of the objects it references. Such type should not reference any objects whose
2222
/// lifetime is controlled externally. This annotation allows Swift to import methods
2323
/// that return a `class` or `struct` type that is annotated with this macro.
24-
#define SELF_CONTAINED __attribute__((swift_attr("import_owned")))
24+
#define SWIFT_SELF_CONTAINED __attribute__((swift_attr("import_owned")))
2525

2626
/// Specifies that a C++ method returns a value that is presumed to contain
2727
/// objects whose lifetime is not dependent on `this` or other parameters passed
2828
/// to the method.
29-
#define RETURNS_INDEPENDENT_VALUE __attribute__((swift_attr("import_unsafe")))
29+
#define SWIFT_RETURNS_INDEPENDENT_VALUE __attribute__((swift_attr("import_unsafe")))
3030

3131
#define _CXX_INTEROP_STRINGIFY(_x) #_x
3232

@@ -38,7 +38,7 @@
3838
/// This example shows how to use this macro to let Swift know that
3939
/// a non-copyable reference counted C++ class can be imported as a reference counted type in Swift:
4040
/// ```c++
41-
/// class SHARED_REFERENCE(retainSharedObject, releaseSharedObject)
41+
/// class SWIFT_SHARED_REFERENCE(retainSharedObject, releaseSharedObject)
4242
/// SharedObject : NonCopyable, IntrusiveReferenceCounted<SharedObject> {
4343
/// public:
4444
/// static SharedObject* create();
@@ -56,7 +56,7 @@
5656
/// object.doSomething()
5757
/// // The Swift compiler will release object here.
5858
/// ```
59-
#define SHARED_REFERENCE(_retain, _release) \
59+
#define SWIFT_SHARED_REFERENCE(_retain, _release) \
6060
__attribute__((swift_attr("import_reference"))) \
6161
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(retain:_retain)))) \
6262
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(release:_release))))
@@ -69,7 +69,7 @@
6969
/// This example shows how to use this macro to let Swift know that
7070
/// a non-copyable singleton C++ class can be imported as a reference type in Swift:
7171
/// ```c++
72-
/// class IMMORTAL_REFERENCE
72+
/// class SWIFT_IMMORTAL_REFERENCE
7373
/// LoggerSingleton : NonCopyable {
7474
/// public:
7575
/// static LoggerSingleton &getInstance();
@@ -83,15 +83,15 @@
8383
/// let logger = LoggerSingleton.getInstance()
8484
/// logger.log(123)
8585
/// ```
86-
#define IMMORTAL_REFERENCE \
86+
#define SWIFT_IMMORTAL_REFERENCE \
8787
__attribute__((swift_attr("import_reference"))) \
8888
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(retain:immortal)))) \
8989
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(release:immortal))))
9090

9191
/// Specifies that a C++ `class` or `struct` is a reference type whose lifetime
9292
/// is not managed automatically. The programmer must validate that any reference
9393
/// to such object is valid themselves. This annotation lets Swift import such a type as a reference type in Swift.
94-
#define UNSAFE_REFERENCE \
94+
#define SWIFT_UNSAFE_REFERENCE \
9595
__attribute__((swift_attr("import_reference"))) \
9696
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(retain:immortal)))) \
9797
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(release:immortal))))
@@ -105,10 +105,10 @@
105105
/// This example shows how to use this macro to conform a class template to a Swift protocol:
106106
/// ```
107107
/// template<class T>
108-
/// class CONFORMS_TO_PROTOCOL(SwiftModule.ProtocolName)
108+
/// class SWIFT_CONFORMS_TO_PROTOCOL(SwiftModule.ProtocolName)
109109
/// CustomClass {};
110110
/// ```
111-
#define CONFORMS_TO_PROTOCOL(_moduleName_protocolName) \
111+
#define SWIFT_CONFORMS_TO_PROTOCOL(_moduleName_protocolName) \
112112
__attribute__((swift_attr(_CXX_INTEROP_STRINGIFY(conforms_to:_moduleName_protocolName))))
113113

114114
#endif // SWIFT_CLANGIMPORTER_SWIFT_INTEROP_SUPPORT_H

lib/Sema/CSDiagnostics.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4035,7 +4035,7 @@ void MissingMemberFailure::diagnoseUnsafeCxxMethod(SourceLoc loc,
40354035
ctx.Diags
40364036
.diagnose(methodSwiftLoc, diag::mark_safe_to_import,
40374037
name.getBaseIdentifier().str())
4038-
.fixItInsert(methodSwiftLoc, " RETURNS_INDEPENDENT_VALUE ");
4038+
.fixItInsert(methodSwiftLoc, " SWIFT_RETURNS_INDEPENDENT_VALUE ");
40394039
} else if (cxxMethod->getReturnType()->isReferenceType()) {
40404040
// Rewrite a call to .at(42) as a subscript.
40414041
if (name.getBaseIdentifier().is("at") &&
@@ -4065,7 +4065,7 @@ void MissingMemberFailure::diagnoseUnsafeCxxMethod(SourceLoc loc,
40654065
ctx.Diags
40664066
.diagnose(methodSwiftLoc, diag::mark_safe_to_import,
40674067
name.getBaseIdentifier().str())
4068-
.fixItInsert(methodSwiftLoc, " RETURNS_INDEPENDENT_VALUE ");
4068+
.fixItInsert(methodSwiftLoc, " SWIFT_RETURNS_INDEPENDENT_VALUE ");
40694069
}
40704070
} else if (cxxMethod->getReturnType()->isRecordType()) {
40714071
if (auto cxxRecord = dyn_cast<clang::CXXRecordDecl>(
@@ -4090,11 +4090,10 @@ void MissingMemberFailure::diagnoseUnsafeCxxMethod(SourceLoc loc,
40904090
ctx.Diags
40914091
.diagnose(methodSwiftLoc, diag::mark_safe_to_import,
40924092
name.getBaseIdentifier().str())
4093-
.fixItInsert(methodSwiftLoc, " RETURNS_INDEPENDENT_VALUE ");
4093+
.fixItInsert(methodSwiftLoc, " SWIFT_RETURNS_INDEPENDENT_VALUE ");
40944094
ctx.Diags
40954095
.diagnose(baseSwiftLoc, diag::mark_self_contained, returnTypeStr)
4096-
.fixItInsert(baseSwiftLoc,
4097-
"SELF_CONTAINED ");
4096+
.fixItInsert(baseSwiftLoc, "SWIFT_SELF_CONTAINED ");
40984097
}
40994098
}
41004099
}

test/Interop/Cxx/class/fixit-add-safe-to-import-self-contained.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ struct X {
2121
import Test
2222

2323
public func test(x: X) {
24-
// CHECK: note: annotate method 'test' with 'RETURNS_INDEPENDENT_VALUE' in C++ to make it available in Swift
24+
// CHECK: note: annotate method 'test' with 'SWIFT_RETURNS_INDEPENDENT_VALUE' in C++ to make it available in Swift
2525
// CHECK: int *test() { }
2626
// CHECK: ^
27-
// CHECK: RETURNS_INDEPENDENT_VALUE
27+
// CHECK: SWIFT_RETURNS_INDEPENDENT_VALUE
2828

2929
x.test()
3030

31-
// CHECK: note: annotate method 'other' with 'RETURNS_INDEPENDENT_VALUE' in C++ to make it available in Swift
31+
// CHECK: note: annotate method 'other' with 'SWIFT_RETURNS_INDEPENDENT_VALUE' in C++ to make it available in Swift
3232
// CHECK: Ptr other() { }
3333
// CHECK: ^
34-
// CHECK: RETURNS_INDEPENDENT_VALUE
34+
// CHECK: SWIFT_RETURNS_INDEPENDENT_VALUE
3535

36-
// CHECK: note: Mark type 'Ptr' as 'SELF_CONTAINED' in C++ to make methods that use it available in Swift.
36+
// CHECK: note: Mark type 'Ptr' as 'SWIFT_SELF_CONTAINED' in C++ to make methods that use it available in Swift.
3737
// CHECK: struct Ptr {
3838
// CHECK: ^
39-
// CHECK: SELF_CONTAINED
39+
// CHECK: SWIFT_SELF_CONTAINED
4040
x.other()
4141
}

test/Interop/Cxx/class/invalid-class-errors.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ import Test
3030
// CHECK: note: record 'A' is not automatically available: does not have a copy constructor or destructor. Does this type have reference semantics?
3131
// CHECK: struct A {
3232
// CHECK: ^
33-
// CHECK: SHARED_REFERENCE(<#retain#>, <#release#>)
33+
// CHECK: SWIFT_SHARED_REFERENCE(<#retain#>, <#release#>)
3434
public func test(x: A) { }
3535
// CHECK: note: record 'B' is not automatically available: does not have a copy constructor or destructor. Does this type have reference semantics?
3636
// CHECK: struct {{.*}}B {
3737
// CHECK: ^
38-
// CHECK: SHARED_REFERENCE(<#retain#>, <#release#>)
38+
// CHECK: SWIFT_SHARED_REFERENCE(<#retain#>, <#release#>)
3939
public func test(x: B) { }
4040
// CHECK: note: record 'Nested' is not automatically available: does not have a copy constructor or destructor. Does this type have reference semantics?
4141
// CHECK: struct Nested {
4242
// CHECK: ^
43-
// CHECK: SHARED_REFERENCE(<#retain#>, <#release#>)
43+
// CHECK: SWIFT_SHARED_REFERENCE(<#retain#>, <#release#>)
4444
public func test(x: Namespace.Nested) { }

test/Interop/Cxx/class/invalid-unsafe-projection-errors.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ import Test
2929
public func test(x: M) {
3030
// CHECK: note: C++ method 'test1' that returns a pointer of type 'UnsafeMutablePointer' is unavailable.
3131
// CHECK: note: C++ method 'test1' may return an interior pointer.
32-
// CHECK: note: annotate method 'test1' with 'RETURNS_INDEPENDENT_VALUE' in C++ to make it available in Swift
32+
// CHECK: note: annotate method 'test1' with 'SWIFT_RETURNS_INDEPENDENT_VALUE' in C++ to make it available in Swift
3333
x.test1()
3434
// CHECK: note: C++ method 'test2' that returns a reference of type 'UnsafeMutablePointer' is unavailable.
3535
// CHECK: note: C++ method 'test2' may return an interior pointer.
36-
// CHECK: note: annotate method 'test2' with 'RETURNS_INDEPENDENT_VALUE' in C++ to make it available in Swift
36+
// CHECK: note: annotate method 'test2' with 'SWIFT_RETURNS_INDEPENDENT_VALUE' in C++ to make it available in Swift
3737
x.test2()
3838
// CHECK: note: C++ method 'test3' that returns a value of type 'Ptr' is unavailable.
3939
// CHECK: note: C++ method 'test3' may return an interior pointer.
40-
// CHECK: note: annotate method 'test3' with 'RETURNS_INDEPENDENT_VALUE' in C++ to make it available in Swift
41-
// CHECK: note: Mark type 'Ptr' as 'SELF_CONTAINED' in C++ to make methods that use it available in Swift.
40+
// CHECK: note: annotate method 'test3' with 'SWIFT_RETURNS_INDEPENDENT_VALUE' in C++ to make it available in Swift
41+
// CHECK: note: Mark type 'Ptr' as 'SWIFT_SELF_CONTAINED' in C++ to make methods that use it available in Swift.
4242
x.test3()
4343
// CHECK: note: C++ method 'begin' that returns an iterator is unavailable
4444
// CHECK: note: C++ methods that return iterators are potentially unsafe. Try re-writing to use Swift iterator APIs.

test/Interop/Cxx/ergonomics/swift-bridging-annotations.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ module CxxModule {
2929
#pragma clang module import SwiftBridging
3030
#endif
3131

32-
class SELF_CONTAINED SelfContained {
32+
class SWIFT_SELF_CONTAINED SelfContained {
3333
public:
3434
int *pointer;
3535

3636
SelfContained();
3737

38-
const int *returnsIndependent() const RETURNS_INDEPENDENT_VALUE;
38+
const int *returnsIndependent() const SWIFT_RETURNS_INDEPENDENT_VALUE;
3939
};
4040

41-
class SHARED_REFERENCE(retainSharedObject, releaseSharedObject)
41+
class SWIFT_SHARED_REFERENCE(retainSharedObject, releaseSharedObject)
4242
SharedObject {
4343
public:
4444
static SharedObject *create();
@@ -47,21 +47,21 @@ public:
4747
void retainSharedObject(SharedObject *);
4848
void releaseSharedObject(SharedObject *);
4949

50-
class IMMORTAL_REFERENCE LoggerSingleton {
50+
class SWIFT_IMMORTAL_REFERENCE LoggerSingleton {
5151
public:
5252
LoggerSingleton(const LoggerSingleton &) = delete;
5353
static LoggerSingleton *getInstance();
5454
};
5555

56-
class UNSAFE_REFERENCE UnsafeNonCopyable {
56+
class SWIFT_UNSAFE_REFERENCE UnsafeNonCopyable {
5757
public:
5858
UnsafeNonCopyable(UnsafeNonCopyable &) = delete;
5959
};
6060

6161
UnsafeNonCopyable *returnsPointerToUnsafeReference();
6262
void takesPointerToUnsafeNonCopyable(UnsafeNonCopyable *);
6363

64-
class CONFORMS_TO_PROTOCOL(SwiftMod.Proto) ConformsTo {
64+
class SWIFT_CONFORMS_TO_PROTOCOL(SwiftMod.Proto) ConformsTo {
6565
public:
6666
};
6767

test/Interop/Cxx/foreign-reference/value-type-errors.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ void takesRef(Ref r);
1919
import Test
2020

2121
public func test(x: Ref) {
22-
// CHECK: note: function uses foreign reference type 'Ref' as a value in the return types which breaks 'import_reference' contract (outlined in C++ Interop User Manual).
22+
// CHECK: note: function uses foreign reference type 'Ref' as a value in the return types which breaks 'swift_shared_reference' contract (outlined in C++ Interop User Manual).
2323
returnsRef()
24-
// CHECK: note: function uses foreign reference type 'Ref' as a value in a parameter types which breaks 'import_reference' contract (outlined in C++ Interop User Manual).
24+
// CHECK: note: function uses foreign reference type 'Ref' as a value in a parameter types which breaks 'swift_shared_reference' contract (outlined in C++ Interop User Manual).
2525
takesRef(x)
26-
}
26+
}

0 commit comments

Comments
 (0)