From 983d1eeca1bd7547a496d4b92f09db27d034f03a Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 18 May 2016 11:03:40 -0700 Subject: [PATCH 1/5] Add a test that C functions with malformed import-as-member-property API notes don't crash. --- .../broken-modules/BrokenAPINotes.apinotes | 14 ++++++++++++++ .../Inputs/broken-modules/BrokenAPINotes.h | 11 +++++++++++ .../Inputs/broken-modules/module.modulemap | 3 +++ test/APINotes/broken-swift-name.swift | 16 ++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 test/APINotes/Inputs/broken-modules/BrokenAPINotes.apinotes create mode 100644 test/APINotes/Inputs/broken-modules/BrokenAPINotes.h create mode 100644 test/APINotes/Inputs/broken-modules/module.modulemap create mode 100644 test/APINotes/broken-swift-name.swift diff --git a/test/APINotes/Inputs/broken-modules/BrokenAPINotes.apinotes b/test/APINotes/Inputs/broken-modules/BrokenAPINotes.apinotes new file mode 100644 index 0000000000000..1f3b93e4fe986 --- /dev/null +++ b/test/APINotes/Inputs/broken-modules/BrokenAPINotes.apinotes @@ -0,0 +1,14 @@ +Name: BrokenAPINotes +Functions: + - Name: ZXSpectrumGetAccumulator + SwiftName: 'getter:ZXSpectrum.accumulator(self:)' + - Name: ZXSpectrumSetAccumulator + SwiftName: 'setter:ZXSpectrum.accumulator(self:newValue:)' + - Name: ZXSpectrumGetRegister + SwiftName: 'getter:ZXSpectrum.register(self:which:)' + - Name: ZXSpectrumSetRegister + SwiftName: 'setter:ZXSpectrum.register(self:which:newValue:)' + - Name: ZXSpectrumGetMisnamedRegister + SwiftName: 'getter:ZXSpectrum.misnamedRegister(self:)' + - Name: ZXSpectrumSetMisnamedRegister + SwiftName: 'setter:ZXSpectrum.misnamedRegister(self:newValue:)' diff --git a/test/APINotes/Inputs/broken-modules/BrokenAPINotes.h b/test/APINotes/Inputs/broken-modules/BrokenAPINotes.h new file mode 100644 index 0000000000000..a3b1ac9239614 --- /dev/null +++ b/test/APINotes/Inputs/broken-modules/BrokenAPINotes.h @@ -0,0 +1,11 @@ +typedef struct ZXSpectrum { unsigned char A, B, C, D, H, L; } ZXSpectrum; + +unsigned char ZXSpectrumGetAccumulator(const ZXSpectrum *self); +void ZXSpectrumSetAccumulator(ZXSpectrum *self, unsigned char newValue); + +unsigned char ZXSpectrumGetRegister(const ZXSpectrum *self, int which); +void ZXSpectrumSetRegister(ZXSpectrum *self, int which, unsigned char newValue); + +unsigned char ZXSpectrumGetMisnamedRegister(const ZXSpectrum *self, int which); +void ZXSpectrumSetMisnamedRegister(ZXSpectrum *self, int which, unsigned char newValue); + diff --git a/test/APINotes/Inputs/broken-modules/module.modulemap b/test/APINotes/Inputs/broken-modules/module.modulemap new file mode 100644 index 0000000000000..3bd0252444a1a --- /dev/null +++ b/test/APINotes/Inputs/broken-modules/module.modulemap @@ -0,0 +1,3 @@ +module BrokenAPINotes { + header "BrokenAPINotes.h" +} diff --git a/test/APINotes/broken-swift-name.swift b/test/APINotes/broken-swift-name.swift new file mode 100644 index 0000000000000..b8f61652d4e1c --- /dev/null +++ b/test/APINotes/broken-swift-name.swift @@ -0,0 +1,16 @@ +// RUN: %target-parse-verify-swift -I %S/Inputs/broken-modules +import BrokenAPINotes + +func testBrokenSwiftName(x: inout ZXSpectrum) { + _ = x.accumulator + x.accumulator = 0 + + // The functions that import as `register`'s accessors have incorrect + // signatures for a getter or setter. Ensure we drop the import instead of + // forming an invalid property. + _ = x.register // expected-error{{has no member}} + x.register = 0 // expected-error{{has no member}} + + _ = x.misnamedRegister // expected-error{{has no member}} + x.misnamedRegister = 0 // expected-error{{has no member}} +} From 7b3135d2a752c36ae87757ab6a675e16ddab9653 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 18 May 2016 15:16:09 -0700 Subject: [PATCH 2/5] Update ImportAsMember test not to use unprototyped function. We now reject this in Clang. --- test/IDE/Inputs/custom-modules/ImportAsMember.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/IDE/Inputs/custom-modules/ImportAsMember.h b/test/IDE/Inputs/custom-modules/ImportAsMember.h index 109893e7c91a5..e2211d933139e 100644 --- a/test/IDE/Inputs/custom-modules/ImportAsMember.h +++ b/test/IDE/Inputs/custom-modules/ImportAsMember.h @@ -11,7 +11,7 @@ extern double IAMStruct1GlobalVar extern struct IAMStruct1 IAMStruct1CreateSimple(double value) __attribute__((swift_name("Struct1.init(value:)"))); -extern struct IAMStruct1 IAMStruct1CreateSpecialLabel() +extern struct IAMStruct1 IAMStruct1CreateSpecialLabel(void) __attribute__((swift_name("Struct1.init(specialLabel:)"))); extern struct IAMStruct1 IAMStruct1Invert(struct IAMStruct1 s) From 6c13eb0db6d4f5d3e66fbb2d1cff97acf87bf99a Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Wed, 18 May 2016 16:05:41 -0700 Subject: [PATCH 3/5] Fix up some more non-prototyped declarations with swift_names. This is now diagnosed on the Clang side. --- test/IDE/Inputs/custom-modules/ImportAsMemberError.h | 4 +--- test/IDE/import_as_member_objc.swift | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/test/IDE/Inputs/custom-modules/ImportAsMemberError.h b/test/IDE/Inputs/custom-modules/ImportAsMemberError.h index 8d76c109dc35b..c3a59e7d2254b 100644 --- a/test/IDE/Inputs/custom-modules/ImportAsMemberError.h +++ b/test/IDE/Inputs/custom-modules/ImportAsMemberError.h @@ -19,7 +19,7 @@ typedef NSObject *ImportedProtocolBase_t; typedef NSObject *ErrorProto_t; // Instance and static member onto protocol -void mutateSomeStaticState() +void mutateSomeStaticState(void) __attribute__((swift_name("ErrorProto.mutateSomeStaticState()"))); // ok void mutateSomeInstanceState(ErrorProto_t self) __attribute__(( swift_name("ErrorProto.mutateSomeInstanceState(self:)"))); // error @@ -27,7 +27,5 @@ void mutateSomeInstanceState(ErrorProto_t self) __attribute__(( // Non-prototype declaration extern void IAMErrorStructHasPrototype(void) __attribute__((swift_name("ErrorStruct.hasPrototype()"))); // ok -extern void IAMErrorStructNonPrototype() - __attribute__((swift_name("ErrorStruct.nonPrototype()"))); // error #endif // IMPORT_AS_MEMBER_ERR_H diff --git a/test/IDE/import_as_member_objc.swift b/test/IDE/import_as_member_objc.swift index 43fbf6a26dc5b..b689ce50e4a16 100644 --- a/test/IDE/import_as_member_objc.swift +++ b/test/IDE/import_as_member_objc.swift @@ -43,8 +43,6 @@ import IAMError // Errors ErrorStruct.hasPrototype(); -ErrorStruct.nonPrototype(); - // expected-error@-1{{type 'ErrorStruct' has no member 'nonPrototype'}} // Protocols @objc class Foo : NSObject, IAMProto {} From a28719fa2bf840de27d913afec42f62702af11b8 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 19 May 2016 13:03:49 -0700 Subject: [PATCH 4/5] Fix broken API notes uncovered by strengthened Clang-side validation. --- apinotes/CoreGraphics.apinotes | 2 +- test/ClangModules/Inputs/custom-modules/SwiftName.apinotes | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apinotes/CoreGraphics.apinotes b/apinotes/CoreGraphics.apinotes index 64944387cf97f..f60397c1c049b 100644 --- a/apinotes/CoreGraphics.apinotes +++ b/apinotes/CoreGraphics.apinotes @@ -89,7 +89,7 @@ Functions: - Name: CGBitmapContextCreateWithData SwiftName: CGContextRef.init(data:width:height:bitsPerComponent:bytesPerRow:space:bitmapInfo:releaseCallback:releaseInfo:) - Name: CGBitmapContextCreate - SwiftName: CGContextRef.init(data:width:height:bitsPerComponent:bytesPerRow:space:bitmapInfo) + SwiftName: CGContextRef.init(data:width:height:bitsPerComponent:bytesPerRow:space:bitmapInfo:) - Name: CGBitmapContextGetData SwiftName: getter:CGContextRef.data(self:) - Name: CGBitmapContextGetWidth diff --git a/test/ClangModules/Inputs/custom-modules/SwiftName.apinotes b/test/ClangModules/Inputs/custom-modules/SwiftName.apinotes index d7cd242959409..332208e1d709c 100644 --- a/test/ClangModules/Inputs/custom-modules/SwiftName.apinotes +++ b/test/ClangModules/Inputs/custom-modules/SwiftName.apinotes @@ -5,6 +5,6 @@ Enumerators: SwiftName: challenging Functions: - Name: spuriousAPINotedSwiftName - SwiftName: spuriousAPINotedSwiftName + SwiftName: spuriousAPINotedSwiftName(_:) - Name: poorlyNamedFunction - SwiftName: nicelyRenamedFunction \ No newline at end of file + SwiftName: nicelyRenamedFunction(_:) From f982f688db82facba62e33dd1cc712a47b61de59 Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 19 May 2016 13:23:12 -0700 Subject: [PATCH 5/5] Add test that we still import Clang decls with rejected SwiftName API notes. Most cases fall out from swift_name validation on the Clang side dropping invalid API notes, though the validation on the Clang side is conservative and misses some cases. We still have Swift-side work to fall back to the original name here. --- test/APINotes/broken-swift-name.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/APINotes/broken-swift-name.swift b/test/APINotes/broken-swift-name.swift index b8f61652d4e1c..e9c11906532c0 100644 --- a/test/APINotes/broken-swift-name.swift +++ b/test/APINotes/broken-swift-name.swift @@ -13,4 +13,12 @@ func testBrokenSwiftName(x: inout ZXSpectrum) { _ = x.misnamedRegister // expected-error{{has no member}} x.misnamedRegister = 0 // expected-error{{has no member}} + + // Ensure that definitions with invalid API notes are still available + // under their original names. + ZXSpectrumGetRegister(&x, 0) + ZXSpectrumSetRegister(&x, 0, 1) + ZXSpectrumGetMisnamedRegister(&x, 0) + // TODO: Conservative validation in Clang doesn't reject the API name here + // ZXSpectrumSetMisnamedRegister(&x, 0, 1) }