Skip to content

Commit a00944e

Browse files
committed
[clang] 'unused-but-set-variable' warning should not apply to __block objective-c pointers
The __block Objective-C pointers can be set but not used due to a commonly used lifetime extension pattern in Objective-C. Differential Revision: https://reviews.llvm.org/D112850
1 parent 565cbc2 commit a00944e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

clang/lib/Sema/SemaDecl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,6 +1944,12 @@ void Sema::DiagnoseUnusedButSetDecl(const VarDecl *VD) {
19441944
}
19451945
}
19461946

1947+
// Don't warn about __block Objective-C pointer variables, as they might
1948+
// be assigned in the block but not used elsewhere for the purpose of lifetime
1949+
// extension.
1950+
if (VD->hasAttr<BlocksAttr>() && Ty->isObjCObjectPointerType())
1951+
return;
1952+
19471953
auto iter = RefsMinusAssignments.find(VD);
19481954
if (iter == RefsMinusAssignments.end())
19491955
return;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: %clang_cc1 -triple x86_64-apple-macos11 -fsyntax-only -fobjc-arc -fblocks -verify -Wunused-but-set-variable -Wno-objc-root-class %s
2+
3+
typedef struct dispatch_queue_s *dispatch_queue_t;
4+
5+
typedef void (^dispatch_block_t)(void);
6+
7+
void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
8+
9+
extern __attribute__((visibility("default"))) struct dispatch_queue_s _dispatch_main_q;
10+
11+
id getFoo();
12+
13+
@protocol P
14+
15+
@end
16+
17+
@interface I
18+
19+
@end
20+
21+
void test() {
22+
// no diagnostics
23+
__block id x = getFoo();
24+
__block id<P> y = x;
25+
__block I *z = (I *)x;
26+
// diagnose non-block variables
27+
id x2 = getFoo(); // expected-warning {{variable 'x2' set but not used}}
28+
dispatch_async(&_dispatch_main_q, ^{
29+
x = ((void *)0);
30+
y = x;
31+
z = ((void *)0);
32+
});
33+
x2 = getFoo();
34+
}

0 commit comments

Comments
 (0)