Skip to content

Commit 21e401f

Browse files
authored
Merge pull request #6179 from timbodeit/printasobjc-warnunusedresult
[PrintAsObjC] Add `__attribute__((warn_unused_result))` for non-@discardableResult methods
2 parents d3b5dfa + 4eca52e commit 21e401f

13 files changed

+132
-111
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,14 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
541541
!isa<ProtocolDecl>(ctor->getDeclContext())) {
542542
os << " OBJC_DESIGNATED_INITIALIZER";
543543
}
544-
} else if (isMistakableForInit(AFD->getObjCSelector())) {
545-
os << " SWIFT_METHOD_FAMILY(none)";
544+
} else {
545+
if (isMistakableForInit(AFD->getObjCSelector())) {
546+
os << " SWIFT_METHOD_FAMILY(none)";
547+
}
548+
if (!methodTy->getResult()->isVoid() &&
549+
!AFD->getAttrs().hasAttribute<DiscardableResultAttr>()) {
550+
os << " SWIFT_WARN_UNUSED_RESULT";
551+
}
546552
}
547553

548554
os << ";\n";
@@ -2112,6 +2118,11 @@ class ModuleWriter {
21122118
"#else\n"
21132119
"# define SWIFT_NOESCAPE\n"
21142120
"#endif\n"
2121+
"#if defined(__has_attribute) && __has_attribute(warn_unused_result)\n"
2122+
"# define SWIFT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n"
2123+
"#else\n"
2124+
"# define SWIFT_WARN_UNUSED_RESULT\n"
2125+
"#endif\n"
21152126
"#if !defined(SWIFT_CLASS_EXTRA)\n"
21162127
"# define SWIFT_CLASS_EXTRA\n"
21172128
"#endif\n"

test/PrintAsObjC/Inputs/comments-expected-output.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ SWIFT_CLASS("_TtC8comments21A010_AttachToEntities")
1313
Aaa. init().
1414
*/
1515
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
16-
- (NSInteger)objectAtIndexedSubscript:(NSInteger)i;
16+
- (NSInteger)objectAtIndexedSubscript:(NSInteger)i SWIFT_WARN_UNUSED_RESULT;
1717
- (void)setObject:(NSInteger)newValue atIndexedSubscript:(NSInteger)i;
1818
/**
1919
Aaa. v1.
@@ -23,7 +23,7 @@ SWIFT_CLASS("_TtC8comments21A010_AttachToEntities")
2323
Aaa. v2.
2424
*/
2525
SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly) NSInteger v2;)
26-
+ (NSInteger)v2;
26+
+ (NSInteger)v2 SWIFT_WARN_UNUSED_RESULT;
2727
@end
2828

2929

@@ -457,7 +457,7 @@ SWIFT_CLASS("_TtC8comments7Returns")
457457
returns:
458458
A number
459459
*/
460-
- (NSInteger)f0;
460+
- (NSInteger)f0 SWIFT_WARN_UNUSED_RESULT;
461461
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
462462
@end
463463

test/PrintAsObjC/any_as_id.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,32 @@ import Foundation
2424
// CHECK-NEXT: @interface AnyAsIdTest : NSObject
2525
class AnyAsIdTest : NSObject {
2626

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

32-
// CHECK-NEXT: - (NSDictionary * _Nonnull)dictionaryOfAny:(NSDictionary * _Nonnull)x;
32+
// CHECK-NEXT: - (NSDictionary * _Nonnull)dictionaryOfAny:(NSDictionary * _Nonnull)x SWIFT_WARN_UNUSED_RESULT;
3333
func dictionaryOfAny(_ x: [AnyHashable: Any]) -> [AnyHashable: Any] { return x }
3434
// CHECK-NEXT: - (void)dictionaryOfAnyKeys:(NSDictionary<id <NSCopying>, NSString *> * _Nonnull)x;
3535
func dictionaryOfAnyKeys(_ x: [AnyHashable: String]) {}
36-
// CHECK-NEXT: - (NSDictionary * _Nullable)dictionaryOfAnyMayhap:(NSDictionary * _Nonnull)x;
36+
// CHECK-NEXT: - (NSDictionary * _Nullable)dictionaryOfAnyMayhap:(NSDictionary * _Nonnull)x SWIFT_WARN_UNUSED_RESULT;
3737
func dictionaryOfAnyMayhap(_ x: [AnyHashable: Any]) -> [AnyHashable: Any]? { return x }
3838
// CHECK-NEXT: - (void)dictionaryOfAnyValues:(NSDictionary<NSString *, id> * _Nonnull)x;
3939
func dictionaryOfAnyValues(_ x: [String: Any]) {}
4040

41-
// CHECK-NEXT: - (id _Nonnull)getAny;
41+
// CHECK-NEXT: - (id _Nonnull)getAny SWIFT_WARN_UNUSED_RESULT;
4242
func getAny() -> Any { return 1 as Any }
43-
// CHECK-NEXT: - (id _Nullable)getAnyConstructively;
43+
// CHECK-NEXT: - (id _Nullable)getAnyConstructively SWIFT_WARN_UNUSED_RESULT;
4444
func getAnyConstructively() -> Any? { return Optional<Any>(1 as Any) }
45-
// CHECK-NEXT: - (id _Nullable)getAnyMaybe;
45+
// CHECK-NEXT: - (id _Nullable)getAnyMaybe SWIFT_WARN_UNUSED_RESULT;
4646
func getAnyMaybe() -> Any? { return nil }
47-
// CHECK-NEXT: - (id _Nullable)getAnyProbably;
47+
// CHECK-NEXT: - (id _Nullable)getAnyProbably SWIFT_WARN_UNUSED_RESULT;
4848
func getAnyProbably() -> Any? { return 1 as Any }
4949

50-
// CHECK-NEXT: - (id _Nonnull)passThroughAny:(id _Nonnull)x;
50+
// CHECK-NEXT: - (id _Nonnull)passThroughAny:(id _Nonnull)x SWIFT_WARN_UNUSED_RESULT;
5151
func passThroughAny(_ x: Any) -> Any { return x }
52-
// CHECK-NEXT: - (id _Nullable)passThroughAnyMaybe:(id _Nullable)x;
52+
// CHECK-NEXT: - (id _Nullable)passThroughAnyMaybe:(id _Nullable)x SWIFT_WARN_UNUSED_RESULT;
5353
func passThroughAnyMaybe(_ x: Any?) -> Any? { return x }
5454

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

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

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

6767
// CHECK-NEXT: - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;

test/PrintAsObjC/blocks.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ typealias MyBlockWithNoescapeParam = (() -> ()) -> Int
2222
// CHECK-LABEL: @interface Callbacks
2323
@objc class Callbacks {
2424

25-
// CHECK-NEXT: - (void (^ _Nonnull)(void))voidBlocks:(void (^ _Nonnull)(void))input;
25+
// CHECK-NEXT: - (void (^ _Nonnull)(void))voidBlocks:(void (^ _Nonnull)(void))input SWIFT_WARN_UNUSED_RESULT;
2626
func voidBlocks(_ input: @escaping () -> ()) -> () -> () {
2727
return input
2828
}
@@ -47,17 +47,17 @@ typealias MyBlockWithNoescapeParam = (() -> ()) -> Int
4747
(Int32) -> (UInt32)) ->
4848
((Int8) -> (UInt8))) {}
4949

50-
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull))returnsBlockWithInput;
50+
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull))returnsBlockWithInput SWIFT_WARN_UNUSED_RESULT;
5151
func returnsBlockWithInput() -> ((NSObject) -> ())? {
5252
return nil
5353
}
5454

55-
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull))returnsBlockWithParenthesizedInput;
55+
// CHECK-NEXT: - (void (^ _Nullable)(NSObject * _Nonnull))returnsBlockWithParenthesizedInput SWIFT_WARN_UNUSED_RESULT;
5656
func returnsBlockWithParenthesizedInput() -> ((NSObject) -> ())? {
5757
return nil
5858
}
5959

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

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

88-
// CHECK-NEXT: - (NSInteger (* _Nonnull)(NSInteger))functionPointers:(NSInteger (* _Nonnull)(NSInteger))input;
88+
// CHECK-NEXT: - (NSInteger (* _Nonnull)(NSInteger))functionPointers:(NSInteger (* _Nonnull)(NSInteger))input SWIFT_WARN_UNUSED_RESULT;
8989
func functionPointers(_ input: @escaping @convention(c) (Int) -> Int)
9090
-> @convention(c) (Int) -> Int {
9191
return input
@@ -110,13 +110,13 @@ typealias MyBlockWithNoescapeParam = (() -> ()) -> Int
110110
) {
111111
}
112112

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

119-
// CHECK-NEXT: - (NSInteger (* _Nonnull)(NSInteger))functionPointersWithName:(NSInteger (* _Nonnull)(NSInteger))input;
119+
// CHECK-NEXT: - (NSInteger (* _Nonnull)(NSInteger))functionPointersWithName:(NSInteger (* _Nonnull)(NSInteger))input SWIFT_WARN_UNUSED_RESULT;
120120
func functionPointersWithName(_ input: @escaping @convention(c) (_ value: Int) -> Int)
121121
-> @convention(c) (_ result: Int) -> Int {
122122
return input

0 commit comments

Comments
 (0)