Skip to content

[PrintAsObjC] Add __attribute__((warn_unused_result)) for non-@discardableResult methods #6179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/PrintAsObjC/PrintAsObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,14 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
!isa<ProtocolDecl>(ctor->getDeclContext())) {
os << " OBJC_DESIGNATED_INITIALIZER";
}
} else if (isMistakableForInit(AFD->getObjCSelector())) {
os << " SWIFT_METHOD_FAMILY(none)";
} else {
if (isMistakableForInit(AFD->getObjCSelector())) {
os << " SWIFT_METHOD_FAMILY(none)";
}
if (!methodTy->getResult()->isVoid() &&
!AFD->getAttrs().hasAttribute<DiscardableResultAttr>()) {
os << " SWIFT_WARN_UNUSED_RESULT";
}
}

os << ";\n";
Expand Down Expand Up @@ -2112,6 +2118,11 @@ class ModuleWriter {
"#else\n"
"# define SWIFT_NOESCAPE\n"
"#endif\n"
"#if defined(__has_attribute) && __has_attribute(warn_unused_result)\n"
"# define SWIFT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n"
"#else\n"
"# define SWIFT_WARN_UNUSED_RESULT\n"
"#endif\n"
"#if !defined(SWIFT_CLASS_EXTRA)\n"
"# define SWIFT_CLASS_EXTRA\n"
"#endif\n"
Expand Down
6 changes: 3 additions & 3 deletions test/PrintAsObjC/Inputs/comments-expected-output.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SWIFT_CLASS("_TtC8comments21A010_AttachToEntities")
Aaa. init().
*/
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
- (NSInteger)objectAtIndexedSubscript:(NSInteger)i;
- (NSInteger)objectAtIndexedSubscript:(NSInteger)i SWIFT_WARN_UNUSED_RESULT;
- (void)setObject:(NSInteger)newValue atIndexedSubscript:(NSInteger)i;
/**
Aaa. v1.
Expand All @@ -23,7 +23,7 @@ SWIFT_CLASS("_TtC8comments21A010_AttachToEntities")
Aaa. v2.
*/
SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly) NSInteger v2;)
+ (NSInteger)v2;
+ (NSInteger)v2 SWIFT_WARN_UNUSED_RESULT;
@end


Expand Down Expand Up @@ -457,7 +457,7 @@ SWIFT_CLASS("_TtC8comments7Returns")
returns:
A number
*/
- (NSInteger)f0;
- (NSInteger)f0 SWIFT_WARN_UNUSED_RESULT;
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

Expand Down
24 changes: 12 additions & 12 deletions test/PrintAsObjC/any_as_id.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ import Foundation
// CHECK-NEXT: @interface AnyAsIdTest : NSObject
class AnyAsIdTest : NSObject {

// CHECK-NEXT: - (NSArray * _Nonnull)arrayOfAny:(NSArray * _Nonnull)x;
// CHECK-NEXT: - (NSArray * _Nonnull)arrayOfAny:(NSArray * _Nonnull)x SWIFT_WARN_UNUSED_RESULT;
func arrayOfAny(_ x: [Any]) -> [Any] { return x }
// CHECK-NEXT: - (NSArray * _Nullable)arrayOfAnyPerhaps:(NSArray * _Nonnull)x;
// CHECK-NEXT: - (NSArray * _Nullable)arrayOfAnyPerhaps:(NSArray * _Nonnull)x SWIFT_WARN_UNUSED_RESULT;
func arrayOfAnyPerhaps(_ x: [Any]) -> [Any]? { return x }

// CHECK-NEXT: - (NSDictionary * _Nonnull)dictionaryOfAny:(NSDictionary * _Nonnull)x;
// CHECK-NEXT: - (NSDictionary * _Nonnull)dictionaryOfAny:(NSDictionary * _Nonnull)x SWIFT_WARN_UNUSED_RESULT;
func dictionaryOfAny(_ x: [AnyHashable: Any]) -> [AnyHashable: Any] { return x }
// CHECK-NEXT: - (void)dictionaryOfAnyKeys:(NSDictionary<id <NSCopying>, NSString *> * _Nonnull)x;
func dictionaryOfAnyKeys(_ x: [AnyHashable: String]) {}
// CHECK-NEXT: - (NSDictionary * _Nullable)dictionaryOfAnyMayhap:(NSDictionary * _Nonnull)x;
// CHECK-NEXT: - (NSDictionary * _Nullable)dictionaryOfAnyMayhap:(NSDictionary * _Nonnull)x SWIFT_WARN_UNUSED_RESULT;
func dictionaryOfAnyMayhap(_ x: [AnyHashable: Any]) -> [AnyHashable: Any]? { return x }
// CHECK-NEXT: - (void)dictionaryOfAnyValues:(NSDictionary<NSString *, id> * _Nonnull)x;
func dictionaryOfAnyValues(_ x: [String: Any]) {}

// CHECK-NEXT: - (id _Nonnull)getAny;
// CHECK-NEXT: - (id _Nonnull)getAny SWIFT_WARN_UNUSED_RESULT;
func getAny() -> Any { return 1 as Any }
// CHECK-NEXT: - (id _Nullable)getAnyConstructively;
// CHECK-NEXT: - (id _Nullable)getAnyConstructively SWIFT_WARN_UNUSED_RESULT;
func getAnyConstructively() -> Any? { return Optional<Any>(1 as Any) }
// CHECK-NEXT: - (id _Nullable)getAnyMaybe;
// CHECK-NEXT: - (id _Nullable)getAnyMaybe SWIFT_WARN_UNUSED_RESULT;
func getAnyMaybe() -> Any? { return nil }
// CHECK-NEXT: - (id _Nullable)getAnyProbably;
// CHECK-NEXT: - (id _Nullable)getAnyProbably SWIFT_WARN_UNUSED_RESULT;
func getAnyProbably() -> Any? { return 1 as Any }

// CHECK-NEXT: - (id _Nonnull)passThroughAny:(id _Nonnull)x;
// CHECK-NEXT: - (id _Nonnull)passThroughAny:(id _Nonnull)x SWIFT_WARN_UNUSED_RESULT;
func passThroughAny(_ x: Any) -> Any { return x }
// CHECK-NEXT: - (id _Nullable)passThroughAnyMaybe:(id _Nullable)x;
// CHECK-NEXT: - (id _Nullable)passThroughAnyMaybe:(id _Nullable)x SWIFT_WARN_UNUSED_RESULT;
func passThroughAnyMaybe(_ x: Any?) -> Any? { return x }

// CHECK-NEXT: - (void)setOfAny:(NSSet * _Nonnull)x;
Expand All @@ -58,10 +58,10 @@ class AnyAsIdTest : NSObject {
// CHECK-NEXT: - (void)takesId:(id _Nonnull)x;
func takesId(_ x: Any) {}

// CHECK-NEXT: - (id _Nonnull)unwrapAny:(id _Nullable)x;
// CHECK-NEXT: - (id _Nonnull)unwrapAny:(id _Nullable)x SWIFT_WARN_UNUSED_RESULT;
func unwrapAny(_ x : Any?) -> Any { return x! }

// CHECK-NEXT: - (id _Nullable)wrapAny:(id _Nonnull)x;
// CHECK-NEXT: - (id _Nullable)wrapAny:(id _Nonnull)x SWIFT_WARN_UNUSED_RESULT;
func wrapAny(_ x : Any) -> Any? { return x }

// CHECK-NEXT: - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
Expand Down
16 changes: 8 additions & 8 deletions test/PrintAsObjC/blocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typealias MyBlockWithNoescapeParam = (() -> ()) -> Int
// CHECK-LABEL: @interface Callbacks
@objc class Callbacks {

// CHECK-NEXT: - (void (^ _Nonnull)(void))voidBlocks:(void (^ _Nonnull)(void))input;
// CHECK-NEXT: - (void (^ _Nonnull)(void))voidBlocks:(void (^ _Nonnull)(void))input SWIFT_WARN_UNUSED_RESULT;
func voidBlocks(_ input: @escaping () -> ()) -> () -> () {
return input
}
Expand All @@ -47,17 +47,17 @@ typealias MyBlockWithNoescapeParam = (() -> ()) -> Int
(Int32) -> (UInt32)) ->
((Int8) -> (UInt8))) {}

// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull))returnsBlockWithInput;
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull))returnsBlockWithInput SWIFT_WARN_UNUSED_RESULT;
func returnsBlockWithInput() -> ((NSObject) -> ())? {
return nil
}

// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull))returnsBlockWithParenthesizedInput;
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull))returnsBlockWithParenthesizedInput SWIFT_WARN_UNUSED_RESULT;
func returnsBlockWithParenthesizedInput() -> ((NSObject) -> ())? {
return nil
}

// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull, NSObject * _Nonnull))returnsBlockWithTwoInputs;
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull, NSObject * _Nonnull))returnsBlockWithTwoInputs SWIFT_WARN_UNUSED_RESULT;
func returnsBlockWithTwoInputs() -> ((NSObject, NSObject) -> ())? {
return nil
}
Expand All @@ -74,7 +74,7 @@ typealias MyBlockWithNoescapeParam = (() -> ()) -> Int
// CHECK-NEXT: - (void)blockTakesNamedBlock:(void (^ _Nonnull)(SWIFT_NOESCAPE void (^ _Nonnull)(void)))input;
func blockTakesNamedBlock(_ input: @escaping (_ block: () -> ()) -> ()) {}

// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull))returnsBlockWithNamedInput;
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull))returnsBlockWithNamedInput SWIFT_WARN_UNUSED_RESULT;
func returnsBlockWithNamedInput() -> ((_ object: NSObject) -> ())? {
return nil
}
Expand All @@ -85,7 +85,7 @@ typealias MyBlockWithNoescapeParam = (() -> ()) -> Int
// CHECK-NEXT: - (void)blockWithKeyword:(SWIFT_NOESCAPE NSInteger (^ _Nonnull)(NSInteger))_Nullable_;
func blockWithKeyword(_ _Nullable: (_ `class`: Int) -> Int) {}

// CHECK-NEXT: - (NSInteger (* _Nonnull)(NSInteger))functionPointers:(NSInteger (* _Nonnull)(NSInteger))input;
// CHECK-NEXT: - (NSInteger (* _Nonnull)(NSInteger))functionPointers:(NSInteger (* _Nonnull)(NSInteger))input SWIFT_WARN_UNUSED_RESULT;
func functionPointers(_ input: @escaping @convention(c) (Int) -> Int)
-> @convention(c) (Int) -> Int {
return input
Expand All @@ -110,13 +110,13 @@ typealias MyBlockWithNoescapeParam = (() -> ()) -> Int
) {
}

// CHECK-NEXT: - (void (* _Nonnull)(SWIFT_NOESCAPE NSInteger (* _Nonnull)(NSInteger, NSInteger)))returnsFunctionPointerThatTakesFunctionPointer;
// CHECK-NEXT: - (void (* _Nonnull)(SWIFT_NOESCAPE NSInteger (* _Nonnull)(NSInteger, NSInteger)))returnsFunctionPointerThatTakesFunctionPointer SWIFT_WARN_UNUSED_RESULT;
func returnsFunctionPointerThatTakesFunctionPointer() ->
@convention(c) (_ comparator: @convention(c) (_ x: Int, _ y: Int) -> Int) -> Void {
fatalError()
}

// CHECK-NEXT: - (NSInteger (* _Nonnull)(NSInteger))functionPointersWithName:(NSInteger (* _Nonnull)(NSInteger))input;
// CHECK-NEXT: - (NSInteger (* _Nonnull)(NSInteger))functionPointersWithName:(NSInteger (* _Nonnull)(NSInteger))input SWIFT_WARN_UNUSED_RESULT;
func functionPointersWithName(_ input: @escaping @convention(c) (_ value: Int) -> Int)
-> @convention(c) (_ result: Int) -> Int {
return input
Expand Down
Loading