Skip to content

Commit d698e4f

Browse files
committed
Merge pull request #2103 from tanadeau/sr-1052-clang-importer
[WIP][Clang][SR-1052] Imported C decls w/o warn_unused_result get @discardableResult
2 parents c373b1c + 8a55e42 commit d698e4f

11 files changed

+138
-30
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5604,6 +5604,12 @@ void ClangImporter::Implementation::importAttributes(
56045604
MappedDecl->getAttrs().add(new (C) WarnUnusedResultAttr(SourceLoc(),
56055605
SourceLoc(),
56065606
false));
5607+
} else {
5608+
if (auto MD = dyn_cast<FuncDecl>(MappedDecl)) {
5609+
if (!MD->getResultType()->isVoid()) {
5610+
MD->getAttrs().add(new (C) DiscardableResultAttr(/*implicit*/false));
5611+
}
5612+
}
56075613
}
56085614
// Map __attribute__((const)).
56095615
if (ClangDecl->hasAttr<clang::ConstAttr>()) {

test/ClangModules/Inputs/SwiftPrivateAttr.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,26 @@ class Foo : NSObject, __PrivProto {
88
func __noArgs()
99
func __oneArg(_ arg: Int32)
1010
func __twoArgs(_ arg: Int32, other arg2: Int32)
11+
@discardableResult
1112
class func __foo() -> Self!
13+
@discardableResult
1214
class func __withNoArgs() -> Self!
1315
convenience init!(__oneArg arg: Int32)
1416
@available(*, unavailable, message: "use object construction 'Foo(__oneArg:)'")
17+
@discardableResult
1518
class func __withOneArg(_ arg: Int32) -> Self!
1619
convenience init!(__twoArgs arg: Int32, other arg2: Int32)
1720
@available(*, unavailable, message: "use object construction 'Foo(__twoArgs:other:)'")
21+
@discardableResult
1822
class func __withTwoArgs(_ arg: Int32, other arg2: Int32) -> Self!
1923
convenience init!(__ arg: Int32)
2024
@available(*, unavailable, message: "use object construction 'Foo(__:)'")
25+
@discardableResult
2126
class func __foo(_ arg: Int32) -> Self!
27+
@discardableResult
2228
func objectForKeyedSubscript(_ index: AnyObject!) -> AnyObject!
2329
func __setObject(_ object: AnyObject!, forKeyedSubscript index: AnyObject!)
30+
@discardableResult
2431
func __objectAtIndexedSubscript(_ index: Int32) -> AnyObject!
2532
func setObject(_ object: AnyObject!, atIndexedSubscript index: Int32)
2633
init()

test/IDE/Inputs/mock-sdk/Foo.printed.recursive.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ typealias FooTypedef1 = Int32
6969
var fooIntVar: Int32
7070

7171
/// Aaa. fooFunc1. Bbb.
72+
@discardableResult
7273
func fooFunc1(_ a: Int32) -> Int32
74+
@discardableResult
7375
func fooFunc1AnonymousParam(_: Int32) -> Int32
76+
@discardableResult
7477
func fooFunc3(_ a: Int32, _ b: Float, _ c: Double, _ d: UnsafeMutablePointer<Int32>!) -> Int32
7578
func fooFuncWithBlock(_ blk: ((Float) -> Int32)!)
7679
func fooFuncWithFunctionPointer(_ fptr: (@convention(c) (Float) -> Int32)!)
@@ -111,8 +114,10 @@ func fooFuncWithComment4()
111114
func fooFuncWithComment5()
112115

113116
/// Aaa. redeclaredInMultipleModulesFunc1. Bbb.
117+
@discardableResult
114118
func redeclaredInMultipleModulesFunc1(_ a: Int32) -> Int32
115119
@available(*, unavailable, message: "Variadic function is unavailable")
120+
@discardableResult
116121
func fooFuncUsingVararg(_ a: Int32, _ varargs: Any...) -> Int32
117122

118123
/// Aaa. FooProtocolBase. Bbb.
@@ -141,7 +146,9 @@ protocol FooProtocolDerived : FooProtocolBase {
141146
class FooClassBase {
142147
class func fooBaseInstanceFunc0()
143148
func fooBaseInstanceFunc0()
149+
@discardableResult
144150
class func fooBaseInstanceFunc1(_ anObject: AnyObject!) -> FooClassBase!
151+
@discardableResult
145152
func fooBaseInstanceFunc1(_ anObject: AnyObject!) -> FooClassBase!
146153
init!()
147154
convenience init!(float f: Float)
@@ -150,6 +157,7 @@ class FooClassBase {
150157
class func fooBaseClassFunc0()
151158
/*not inherited*/ init!(_ x: Int32)
152159
@available(*, unavailable, message: "use object construction 'FooClassBase(_:)'")
160+
@discardableResult
153161
class func fooClassBase(_ x: Int32) -> FooClassBase!
154162
}
155163

@@ -196,17 +204,25 @@ struct _InternalStruct {
196204
init(x: Int32)
197205
}
198206
extension FooClassBase {
207+
@discardableResult
199208
class func _internalMeth1() -> AnyObject!
209+
@discardableResult
200210
func _internalMeth1() -> AnyObject!
201211
}
202212
extension FooClassBase {
213+
@discardableResult
203214
class func _internalMeth2() -> AnyObject!
215+
@discardableResult
204216
func _internalMeth2() -> AnyObject!
217+
@discardableResult
205218
class func nonInternalMeth() -> AnyObject!
219+
@discardableResult
206220
func nonInternalMeth() -> AnyObject!
207221
}
208222
extension FooClassBase {
223+
@discardableResult
209224
class func _internalMeth3() -> AnyObject!
225+
@discardableResult
210226
func _internalMeth3() -> AnyObject!
211227
}
212228
protocol _InternalProt {
@@ -238,6 +254,7 @@ class FooClassWithClassProperties : FooClassBase {
238254
class FooUnavailableMembers : FooClassBase {
239255
convenience init!(int i: Int32)
240256
@available(*, unavailable, message: "use object construction 'FooUnavailableMembers(int:)'")
257+
@discardableResult
241258
class func withInt(_ i: Int32) -> Self!
242259
@available(*, unavailable, message: "x")
243260
func unavailable()
@@ -293,6 +310,7 @@ enum SCNFilterMode : Int {
293310
@available(*, unavailable)
294311
case SCNNoFiltering
295312
}
313+
@discardableResult
296314
func fooSubFunc1(_ a: Int32) -> Int32
297315
struct FooSubEnum1 : RawRepresentable, Equatable {
298316
init(_ rawValue: UInt32)

test/IDE/Inputs/mock-sdk/Foo.printed.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,12 @@ typealias FooTypedef1 = Int32
8585
var fooIntVar: Int32
8686

8787
/// Aaa. fooFunc1. Bbb.
88+
@discardableResult
8889
func fooFunc1(_ a: Int32) -> Int32
8990

91+
@discardableResult
9092
func fooFunc1AnonymousParam(_: Int32) -> Int32
93+
@discardableResult
9194
func fooFunc3(_ a: Int32, _ b: Float, _ c: Double, _ d: UnsafeMutablePointer<Int32>!) -> Int32
9295

9396
/*
@@ -135,9 +138,11 @@ func fooFuncWithComment4()
135138
func fooFuncWithComment5()
136139

137140
/// Aaa. redeclaredInMultipleModulesFunc1. Bbb.
141+
@discardableResult
138142
func redeclaredInMultipleModulesFunc1(_ a: Int32) -> Int32
139143

140144
@available(*, unavailable, message: "Variadic function is unavailable")
145+
@discardableResult
141146
func fooFuncUsingVararg(_ a: Int32, _ varargs: Any...) -> Int32 // This comment should not show without decl.
142147

143148
/// Aaa. FooProtocolBase. Bbb.
@@ -170,7 +175,9 @@ protocol FooProtocolDerived : FooProtocolBase {
170175
class FooClassBase {
171176
class func fooBaseInstanceFunc0()
172177
func fooBaseInstanceFunc0()
178+
@discardableResult
173179
class func fooBaseInstanceFunc1(_ anObject: AnyObject!) -> FooClassBase!
180+
@discardableResult
174181
func fooBaseInstanceFunc1(_ anObject: AnyObject!) -> FooClassBase!
175182
init!()
176183
convenience init!(float f: Float)
@@ -180,6 +187,7 @@ class FooClassBase {
180187
class func fooBaseClassFunc0()
181188
/*not inherited*/ init!(_ x: Int32)
182189
@available(*, unavailable, message: "use object construction 'FooClassBase(_:)'")
190+
@discardableResult
183191
class func fooClassBase(_ x: Int32) -> FooClassBase!
184192
}
185193

@@ -239,20 +247,28 @@ struct _InternalStruct {
239247
}
240248

241249
extension FooClassBase {
250+
@discardableResult
242251
class func _internalMeth1() -> AnyObject!
252+
@discardableResult
243253
func _internalMeth1() -> AnyObject!
244254
}
245255

246256
/* Extending FooClassBase with cool stuff */
247257
extension FooClassBase {
258+
@discardableResult
248259
class func _internalMeth2() -> AnyObject!
260+
@discardableResult
249261
func _internalMeth2() -> AnyObject!
262+
@discardableResult
250263
class func nonInternalMeth() -> AnyObject!
264+
@discardableResult
251265
func nonInternalMeth() -> AnyObject!
252266
}
253267

254268
extension FooClassBase {
269+
@discardableResult
255270
class func _internalMeth3() -> AnyObject!
271+
@discardableResult
256272
func _internalMeth3() -> AnyObject!
257273
}
258274

@@ -289,6 +305,7 @@ class FooClassWithClassProperties : FooClassBase {
289305
class FooUnavailableMembers : FooClassBase {
290306
convenience init!(int i: Int32)
291307
@available(*, unavailable, message: "use object construction 'FooUnavailableMembers(int:)'")
308+
@discardableResult
292309
class func withInt(_ i: Int32) -> Self!
293310

294311
@available(*, unavailable, message: "x")

test/IDE/import_as_member.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
// PRINT-NEXT: static var globalVar: Double
2020
// PRINT-NEXT: init(value value: Double)
2121
// PRINT-NEXT: init(specialLabel specialLabel: ())
22+
// PRINT-NEXT: @discardableResult
2223
// PRINT-NEXT: func inverted() -> Struct1
2324
// PRINT-NEXT: mutating func invert()
25+
// PRINT-NEXT: @discardableResult
2426
// PRINT-NEXT: func translate(radians radians: Double) -> Struct1
27+
// PRINT-NEXT: @discardableResult
2528
// PRINT-NEXT: func scale(_ radians: Double) -> Struct1
2629
// PRINT-NEXT: var radius: Double { get nonmutating set }
2730
// PRINT-NEXT: var altitude: Double{{$}}
2831
// PRINT-NEXT: var magnitude: Double { get }
32+
// PRINT-NEXT: @discardableResult
2933
// PRINT-NEXT: static func staticMethod() -> Int32
3034
// PRINT-NEXT: static var property: Int32
3135
// PRINT-NEXT: static var getOnlyProperty: Int32 { get }

test/IDE/infer_import_as_member.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import InferImportAsMember
2525
// PRINT-NEXT: init(specialLabel specialLabel: ())
2626
//
2727
// PRINT-LABEL: /// Methods
28+
// PRINT-NEXT: @discardableResult
2829
// PRINT-NEXT: func invert() -> IAMStruct1
2930
// PRINT-NEXT: mutating func invertInPlace()
31+
// PRINT-NEXT: @discardableResult
3032
// PRINT-NEXT: func rotate(radians radians: Double) -> IAMStruct1
3133
// PRINT-NEXT: func selfComesLast(x x: Double)
3234
// PRINT-NEXT: func selfComesThird(a a: Double, b b: Float, x x: Double)
@@ -38,33 +40,42 @@ import InferImportAsMember
3840
// PRINT-NEXT: var length: Double
3941
//
4042
// PRINT-LABEL: /// Various instance functions that can't quite be imported as properties.
43+
// PRINT-NEXT: @discardableResult
4144
// PRINT-NEXT: func getNonPropertyNumParams() -> Float
4245
// PRINT-NEXT: func setNonPropertyNumParams(a a: Float, b b: Float)
46+
// PRINT-NEXT: @discardableResult
4347
// PRINT-NEXT: func getNonPropertyType() -> Float
4448
// PRINT-NEXT: func setNonPropertyType(x x: Double)
49+
// PRINT-NEXT: @discardableResult
4550
// PRINT-NEXT: func getNonPropertyNoSelf() -> Float
4651
// PRINT-NEXT: static func setNonPropertyNoSelf(x x: Double, y y: Double)
4752
// PRINT-NEXT: func setNonPropertyNoGet(x x: Double)
4853
//
4954
// PRINT-LABEL: /// Various static functions that can't quite be imported as properties.
55+
// PRINT-NEXT: @discardableResult
5056
// PRINT-NEXT: static func staticGetNonPropertyNumParams() -> Float
5157
// PRINT-NEXT: static func staticSetNonPropertyNumParams(a a: Float, b b: Float)
5258
// PRINT-NEXT: static func staticGetNonPropertyNumParamsGetter(d d: Double)
59+
// PRINT-NEXT: @discardableResult
5360
// PRINT-NEXT: static func staticGetNonPropertyType() -> Float
5461
// PRINT-NEXT: static func staticSetNonPropertyType(x x: Double)
62+
// PRINT-NEXT: @discardableResult
5563
// PRINT-NEXT: static func staticGetNonPropertyNoSelf() -> Float
5664
// PRINT-NEXT: static func staticSetNonPropertyNoSelf(x x: Double, y y: Double)
5765
// PRINT-NEXT: static func staticSetNonPropertyNoGet(x x: Double)
5866
//
5967
// PRINT-LABEL: /// Static method
68+
// PRINT-NEXT: @discardableResult
6069
// PRINT-NEXT: static func staticMethod() -> Double
70+
// PRINT-NEXT: @discardableResult
6171
// PRINT-NEXT: static func tlaThreeLetterAcronym() -> Double
6272
//
6373
// PRINT-LABEL: /// Static computed properties
6474
// PRINT-NEXT: static var staticProperty: Double
6575
// PRINT-NEXT: static var staticOnlyProperty: Double { get }
6676
//
6777
// PRINT-LABEL: /// Omit needless words
78+
// PRINT-NEXT: @discardableResult
6879
// PRINT-NEXT: static func onwNeedlessTypeArgLabel(_ Double: Double) -> Double
6980
//
7081
// PRINT-LABEL: /// Fuzzy
@@ -73,6 +84,7 @@ import InferImportAsMember
7384
// PRINT-NEXT: init(fuzzyName fuzzyName: ())
7485
// PRINT-NEXT: }
7586
//
87+
// PRINT-NEXT: @discardableResult
7688
// PRINT-NEXT: func __IAMStruct1IgnoreMe(_ s: IAMStruct1) -> Double
7789
//
7890
// PRINT-LABEL: /// Mutable

test/IDE/print_clang_bool_bridging.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,29 @@
1010

1111
// stdbool.h uses #define, so this test does as well.
1212

13+
@discardableResult
1314
func testCBool(_: Bool) -> Bool
15+
@discardableResult
1416
func testObjCBool(_: Bool) -> Bool
17+
@discardableResult
1518
func testDarwinBoolean(_: Bool) -> Bool
1619

1720
typealias CBoolTypedef = Bool
1821
typealias ObjCBoolTypedef = ObjCBool
1922
typealias DarwinBooleanTypedef = DarwinBoolean
2023

24+
@discardableResult
2125
func testCBoolTypedef(_: CBoolTypedef) -> CBoolTypedef
26+
@discardableResult
2227
func testObjCBoolTypedef(_: Bool) -> Bool
28+
@discardableResult
2329
func testDarwinBooleanTypedef(_: Bool) -> Bool
2430

31+
@discardableResult
2532
func testCBoolPointer(_: UnsafeMutablePointer<Bool>) -> UnsafePointer<Bool>
33+
@discardableResult
2634
func testObjCBoolPointer(_: UnsafeMutablePointer<ObjCBool>) -> UnsafePointer<ObjCBool>
35+
@discardableResult
2736
func testDarwinBooleanPointer(_: UnsafeMutablePointer<DarwinBoolean>) -> UnsafePointer<DarwinBoolean>
2837

2938
typealias CBoolFn = @convention(c) (Bool) -> Bool
@@ -34,12 +43,18 @@ typealias CBoolBlock = (Bool) -> Bool
3443
typealias ObjCBoolBlock = (Bool) -> Bool
3544
typealias DarwinBooleanBlock = (Bool) -> Bool
3645

46+
@discardableResult
3747
func testCBoolFnToBlock(_: @convention(c) (Bool) -> Bool) -> (Bool) -> Bool
48+
@discardableResult
3849
func testObjCBoolFnToBlock(_: @convention(c) (ObjCBool) -> ObjCBool) -> (Bool) -> Bool
50+
@discardableResult
3951
func testDarwinBooleanFnToBlock(_: @convention(c) (DarwinBoolean) -> DarwinBoolean) -> (Bool) -> Bool
4052

53+
@discardableResult
4154
func testCBoolFnToBlockTypedef(_: CBoolFn) -> CBoolBlock
55+
@discardableResult
4256
func testObjCBoolFnToBlockTypedef(_: ObjCBoolFn) -> ObjCBoolBlock
57+
@discardableResult
4358
func testDarwinBooleanFnToBlockTypedef(_: DarwinBooleanFn) -> DarwinBooleanBlock
4459

4560
typealias CBoolFnToBlockType = (CBoolFn) -> CBoolBlock
@@ -59,16 +74,22 @@ class Test : NSObject {
5974
var propObjCBool: Bool
6075
var propDarwinBoolean: Bool
6176

77+
@discardableResult
6278
func testCBool(_ b: Bool) -> Bool
79+
@discardableResult
6380
func testObjCBool(_ b: Bool) -> Bool
81+
@discardableResult
6482
func testDarwinBoolean(_ b: Bool) -> Bool
6583

6684
var propCBoolBlock: (Bool) -> Bool
6785
var propObjCBoolBlock: (Bool) -> Bool
6886
var propDarwinBooleanBlock: (Bool) -> Bool
6987

88+
@discardableResult
7089
func testCBoolFn(toBlock fp: @convention(c) (Bool) -> Bool) -> (Bool) -> Bool
90+
@discardableResult
7191
func testObjCBoolFn(toBlock fp: @convention(c) (ObjCBool) -> ObjCBool) -> (Bool) -> Bool
92+
@discardableResult
7293
func testDarwinBooleanFn(toBlock fp: @convention(c) (DarwinBoolean) -> DarwinBoolean) -> (Bool) -> Bool
7394

7495
func produceCBoolBlockTypedef(_ outBlock: AutoreleasingUnsafeMutablePointer<(@convention(block) (Bool) -> Bool)?>)

test/IDE/print_clang_decls.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,14 @@
8080
// Skip through unavailable typedefs when importing types.
8181
// TAG_DECLS_AND_TYPEDEFS: @available(*, unavailable, message: "use double")
8282
// TAG_DECLS_AND_TYPEDEFS-NEXT: typealias real_t = Double
83+
// TAG_DECLS_AND_TYPEDEFS-NEXT: @discardableResult
8384
// TAG_DECLS_AND_TYPEDEFS-NEXT: func realSin(_ value: Double) -> Double
8485

8586
// NEGATIVE-NOT: typealias FooStructTypedef2
8687

8788
// FOUNDATION-LABEL: {{^}}/// Aaa. NSArray. Bbb.{{$}}
8889
// FOUNDATION-NEXT: {{^}}class NSArray : NSObject {{{$}}
89-
// FOUNDATION-NEXT func objectAtIndex(_ index: Int) -> AnyObject!
90+
// FOUNDATION-NEXT: subscript(idx: Int) -> AnyObject { get }
9091

9192
// FOUNDATION-LABEL: {{^}}/// Aaa. NSRuncingMode. Bbb.{{$}}
9293
// FOUNDATION-NEXT: {{^}}enum NSRuncingMode : UInt {{{$}}

test/IDE/print_clang_decls_AppKit.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
// APPKIT-LABEL: {{^}}class NSView : NSObject, NSCoding, NSAccessibility {{{$}}
2121
// APPKIT-NEXT: init?(coder aDecoder: NSCoder)
22+
// APPKIT-NEXT: @discardableResult
2223
// APPKIT-NEXT: func isDescendant(of aView: NSView) -> Bool
24+
// APPKIT-NEXT: @discardableResult
2325
// APPKIT-NEXT: func ancestorShared(with aView: NSView) -> NSView?
2426
// APPKIT-NEXT: func addSubview(_ aView: NSView)
2527
// APPKIT-NEXT: func addSubview(_ aView: NSView, positioned place: UInt32, relativeTo otherView: NSView?)

0 commit comments

Comments
 (0)