Skip to content

Commit aedaab2

Browse files
committed
[cxx-interop] Flip the switch: only import safe APIs (pt. 2). Start filtering out unsafe records.
1 parent 7c7a545 commit aedaab2

File tree

17 files changed

+88
-45
lines changed

17 files changed

+88
-45
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,6 +1974,11 @@ namespace {
19741974
// Recursively checks that there are no pointers, user-provided copy
19751975
// constructors, or destructors in any fields or base classes.
19761976
static bool isSufficientlyTrivial(const clang::CXXRecordDecl *decl) {
1977+
if (hasImportAsRefAttr(decl) ||
1978+
hasOwnedValueAttr(decl) ||
1979+
hasUnsafeAPIAttr(decl))
1980+
return true;
1981+
19771982
if (decl->hasUserDeclaredCopyConstructor() ||
19781983
decl->hasUserDeclaredCopyAssignment() ||
19791984
decl->hasUserDeclaredDestructor())
@@ -2026,8 +2031,8 @@ namespace {
20262031
}))
20272032
return false;
20282033

2029-
if (true || hasOwnedValueAttr(decl) || hasUnsafeAPIAttr(decl) ||
2030-
some build mode)
2034+
if (hasOwnedValueAttr(decl) || hasUnsafeAPIAttr(decl)) // ||
2035+
// some build mode)
20312036
return true;
20322037

20332038
return isSufficientlyTrivial(decl);

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,8 +1349,8 @@ bool TypeChecker::isDeclarationUnavailable(
13491349

13501350
static bool allowEagerUnsafeCxxAPIs(const Decl *decl,
13511351
const AvailableAttr *attr) {
1352-
return decl->hasClangNode() && decl->getASTContext().someBuildMode &&
1353-
attr->Message == "unsafe-api";
1352+
return true; // decl->hasClangNode() && decl->getASTContext().someBuildMode &&
1353+
// attr->Message == "unsafe-api";
13541354
}
13551355

13561356
Optional<UnavailabilityReason>

test/Interop/Cxx/class/Inputs/constructors.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ struct TemplatedConstructorWithExtraArg {
6363
TemplatedConstructorWithExtraArg(T value, U other) { }
6464
};
6565

66-
struct HasUserProvidedCopyConstructor {
66+
struct
67+
__attribute__((swift_attr("import_unsafe_api")))
68+
HasUserProvidedCopyConstructor {
6769
int numCopies;
6870
HasUserProvidedCopyConstructor(int numCopies = 0) : numCopies(numCopies) {}
6971
HasUserProvidedCopyConstructor(const HasUserProvidedCopyConstructor &other)

test/Interop/Cxx/class/Inputs/destructors.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33

44
struct DummyStruct {};
55

6-
struct HasUserProvidedDestructorAndDummy {
6+
struct
7+
__attribute__((swift_attr("import_unsafe_api")))
8+
HasUserProvidedDestructorAndDummy {
79
DummyStruct dummy;
810
~HasUserProvidedDestructorAndDummy() {}
911
};
1012

11-
struct HasUserProvidedDestructor {
13+
struct
14+
__attribute__((swift_attr("import_unsafe_api")))
15+
HasUserProvidedDestructor {
1216
int *value;
1317
~HasUserProvidedDestructor() { *value = 42; }
1418
};

test/Interop/Cxx/class/Inputs/protocol-conformance.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct DoesNotConformToProtocol {
1111

1212
struct DummyStruct {};
1313

14-
struct NonTrivial {
14+
struct __attribute__((swift_attr("import_unsafe_api"))) NonTrivial {
1515
~NonTrivial() {}
1616
NonTrivial(DummyStruct) {}
1717
NonTrivial() {}
@@ -29,11 +29,11 @@ struct Trivial {
2929
};
3030

3131
struct ReturnsNullableValue {
32-
const int *returnPointer() { return nullptr; }
32+
const int *returnPointer() __attribute__((swift_attr("import_unsafe_api"))) { return nullptr; }
3333
};
3434

3535
struct ReturnsNonNullValue {
36-
const int *returnPointer() __attribute__((returns_nonnull)) {
36+
const int *returnPointer() __attribute__((returns_nonnull)) __attribute__((swift_attr("import_unsafe_api"))) {
3737
return (int *)this;
3838
}
3939
};

test/Interop/Cxx/class/Inputs/type-classification.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ struct StructWithAdditionalConstructor {
1414
StructWithAdditionalConstructor(int parameter) {}
1515
};
1616

17-
struct StructWithCopyConstructor {
17+
struct
18+
__attribute__((swift_attr("import_unsafe_api")))
19+
StructWithCopyConstructor {
1820
StructWithCopyConstructor(const StructWithCopyConstructor &) {}
1921
};
2022

@@ -59,7 +61,9 @@ struct StructWithSubobjectMoveConstructor {
5961
StructWithMoveConstructor subobject;
6062
};
6163

62-
struct StructWithCopyAssignment {
64+
struct
65+
__attribute__((swift_attr("import_unsafe_api")))
66+
StructWithCopyAssignment {
6367
StructWithCopyAssignment &operator=(const StructWithCopyAssignment &);
6468
};
6569

@@ -79,7 +83,9 @@ struct StructWithSubobjectMoveAssignment {
7983
StructWithMoveAssignment subobject;
8084
};
8185

82-
struct StructWithDestructor {
86+
struct
87+
__attribute__((swift_attr("import_unsafe_api")))
88+
StructWithDestructor {
8389
~StructWithDestructor() {}
8490
};
8591

@@ -166,7 +172,9 @@ struct StructDeletedDestructor {
166172
~StructDeletedDestructor() = delete;
167173
};
168174

169-
struct StructWithCopyConstructorAndValue {
175+
struct
176+
__attribute__((swift_attr("import_owned_value")))
177+
StructWithCopyConstructorAndValue {
170178
int value;
171179
StructWithCopyConstructorAndValue() : value(0) {}
172180
StructWithCopyConstructorAndValue(int value) : value(value) {}
@@ -179,7 +187,9 @@ struct StructWithSubobjectCopyConstructorAndValue {
179187
StructWithCopyConstructorAndValue member;
180188
};
181189

182-
struct StructWithCopyConstructorAndSubobjectCopyConstructorAndValue {
190+
struct
191+
__attribute__((swift_attr("import_owned_value")))
192+
StructWithCopyConstructorAndSubobjectCopyConstructorAndValue {
183193
StructWithCopyConstructorAndValue member;
184194
StructWithCopyConstructorAndSubobjectCopyConstructorAndValue(
185195
StructWithCopyConstructorAndValue member)

test/Interop/Cxx/class/inheritance/Inputs/fields.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct DerivedFromAll : HasOneField, DerivedWithOneField {
1818

1919
// Non trivial types:
2020

21-
struct NonTrivial {
21+
struct __attribute__((swift_attr("import_unsafe_api"))) NonTrivial {
2222
NonTrivial() {}
2323
~NonTrivial() {}
2424
};
@@ -33,14 +33,16 @@ struct NonTrivialDerivedWithOneField : NonTrivialHasThreeFields {
3333
int d = 4;
3434
};
3535

36-
struct NonTrivialHasOneField {
36+
struct __attribute__((swift_attr("import_unsafe_api"))) NonTrivialHasOneField {
3737
NonTrivialHasOneField() {}
3838
~NonTrivialHasOneField() {}
3939

4040
int e = 5;
4141
};
4242

43-
struct NonTrivialDerivedFromAll : NonTrivialHasOneField, NonTrivialDerivedWithOneField {
43+
struct
44+
__attribute__((swift_attr("import_unsafe_api")))
45+
NonTrivialDerivedFromAll : NonTrivialHasOneField, NonTrivialDerivedWithOneField {
4446
int f = 6;
4547
};
4648

test/Interop/Cxx/class/inheritance/Inputs/functions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
struct NonTrivial {
1+
struct __attribute__((swift_attr("import_unsafe_api"))) NonTrivial {
22
NonTrivial() {}
33
~NonTrivial() {}
44

@@ -74,7 +74,7 @@ struct DerivedFromDerived : Derived {
7474
}
7575
};
7676

77-
struct DerivedFromNonTrivial : NonTrivial {};
77+
struct __attribute__((swift_attr("import_unsafe_api"))) DerivedFromNonTrivial : NonTrivial {};
7878

7979
struct EmptyBaseClass {
8080
const char *inBase() const __attribute__((swift_attr("import_unsafe_api"))) {

test/Interop/Cxx/class/method/Inputs/methods.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef TEST_INTEROP_CXX_CLASS_METHOD_METHODS_H
22
#define TEST_INTEROP_CXX_CLASS_METHOD_METHODS_H
33

4-
struct NonTrivialInWrapper {
4+
struct __attribute__((swift_attr("import_unsafe_api"))) NonTrivialInWrapper {
55
int value;
66

77
~NonTrivialInWrapper() { }

test/Interop/Cxx/ergonomics/Inputs/implicit-computed-properties.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ struct MultipleArgsSetter {
136136
void setX(int a, int b);
137137
};
138138

139-
struct NonTrivial {
139+
struct __attribute__((swift_attr("import_unsafe_api"))) NonTrivial {
140140
int value = 42;
141141
~NonTrivial() {}
142142
};

test/Interop/Cxx/operators/Inputs/member-inline.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct LoadableBoolWrapper {
4040
}
4141
};
4242

43-
struct AddressOnlyIntWrapper {
43+
struct __attribute__((swift_attr("import_owned_value"))) AddressOnlyIntWrapper {
4444
int value;
4545

4646
AddressOnlyIntWrapper(int value) : value(value) {}
@@ -112,7 +112,9 @@ struct ReadWriteIntArray {
112112
};
113113
};
114114

115-
struct ReadOnlyIntArray {
115+
struct
116+
__attribute__((swift_attr("import_owned_value")))
117+
ReadOnlyIntArray {
116118
private:
117119
int values[5] = { 1, 2, 3, 4, 5 };
118120

@@ -174,7 +176,7 @@ struct TemplatedArray {
174176
};
175177
typedef TemplatedArray<double> TemplatedDoubleArray;
176178

177-
struct TemplatedSubscriptArray {
179+
struct __attribute__((swift_attr("import_unsafe_api"))) TemplatedSubscriptArray {
178180
int *ptr;
179181

180182
template<class T>
@@ -195,7 +197,9 @@ struct IntArrayByVal {
195197
int values[3] = { 1, 2, 3 };
196198
};
197199

198-
struct NonTrivialIntArrayByVal {
200+
struct
201+
__attribute__((swift_attr("import_owned_value")))
202+
NonTrivialIntArrayByVal {
199203
NonTrivialIntArrayByVal(int first) { values[0] = first; }
200204
NonTrivialIntArrayByVal(const NonTrivialIntArrayByVal &other) {
201205
for (int i = 0; i < 5; i++)
@@ -237,7 +241,7 @@ struct TemplatedByVal {
237241
}
238242
};
239243

240-
struct TemplatedOperatorArrayByVal {
244+
struct __attribute__((swift_attr("import_unsafe_api"))) TemplatedOperatorArrayByVal {
241245
int *ptr;
242246
template<class T> T operator[](T i) { return ptr[i]; }
243247
template <class T>
@@ -246,7 +250,7 @@ struct TemplatedOperatorArrayByVal {
246250
}
247251
};
248252

249-
struct NonTrivial {
253+
struct __attribute__((swift_attr("import_owned_value"))) NonTrivial {
250254
char *Str;
251255
long long a;
252256
short b;
@@ -278,15 +282,15 @@ struct PtrByVal {
278282
int a = 64;
279283
};
280284

281-
struct RefToPtr {
285+
struct __attribute__((swift_attr("import_unsafe_api"))) RefToPtr {
282286
RefToPtr() { b = &a; }
283287
int *&operator[](int x) { return b; }
284288
private:
285289
int a = 64;
286290
int *b = nullptr;
287291
};
288292

289-
struct PtrToPtr {
293+
struct __attribute__((swift_attr("import_unsafe_api"))) PtrToPtr {
290294
PtrToPtr() { b = &a; }
291295
int **operator[](int x) { return &b; }
292296
private:

test/Interop/Cxx/operators/Inputs/member-out-of-line.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct LoadableIntWrapper {
99
int operator()(int x, int y) const;
1010
};
1111

12-
struct AddressOnlyIntWrapper {
12+
struct __attribute__((swift_attr("import_owned_value"))) AddressOnlyIntWrapper {
1313
int value;
1414

1515
AddressOnlyIntWrapper(int value) : value(value) {}
@@ -29,7 +29,7 @@ struct ReadWriteIntArray {
2929
int &operator[](int x);
3030
};
3131

32-
struct NonTrivialIntArrayByVal {
32+
struct __attribute__((swift_attr("import_owned_value"))) NonTrivialIntArrayByVal {
3333
NonTrivialIntArrayByVal(int first) { values[0] = first; }
3434
NonTrivialIntArrayByVal(const NonTrivialIntArrayByVal &other) {
3535
for (int i = 0; i < 5; i++)

test/Interop/Cxx/templates/Inputs/define-referenced-inline.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ inline void inlineFn2(T) { }
1010
inline void inlineFn3() { }
1111

1212
template<class T>
13-
struct HasInlineDtor {
13+
struct __attribute__((swift_attr("import_unsafe_api"))) HasInlineDtor {
1414
inline ~HasInlineDtor() { inlineFn1(T()); }
1515
};
1616

test/Interop/Cxx/union/Inputs/anonymous-union-partly-invalid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{}
44
@end
55

6-
struct S {
6+
struct __attribute__((swift_attr("import_unsafe_api"))) S {
77
union {
88
C *t;
99
char c;

test/Interop/Cxx/value-witness-table/Inputs/copy-constructors.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef TEST_INTEROP_CXX_VALUE_WITNESS_TABLE_INPUTS_COPY_CONSTRUCTORS_H
22
#define TEST_INTEROP_CXX_VALUE_WITNESS_TABLE_INPUTS_COPY_CONSTRUCTORS_H
33

4-
struct HasUserProvidedCopyConstructor {
4+
struct
5+
__attribute__((swift_attr("import_unsafe_api")))
6+
HasUserProvidedCopyConstructor {
57
int numCopies;
68
HasUserProvidedCopyConstructor(int numCopies = 0) : numCopies(numCopies) {}
79
HasUserProvidedCopyConstructor(const HasUserProvidedCopyConstructor &other)

0 commit comments

Comments
 (0)