[clang] a00944e - [clang] 'unused-but-set-variable' warning should not apply to __block objective-c pointers
Alex Lorenz via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 5 07:48:25 PDT 2021
Author: Alex Lorenz
Date: 2021-11-05T07:48:07-07:00
New Revision: a00944ebeab1b0adbce606cde4d2410fcbb3f440
URL: https://github.com/llvm/llvm-project/commit/a00944ebeab1b0adbce606cde4d2410fcbb3f440
DIFF: https://github.com/llvm/llvm-project/commit/a00944ebeab1b0adbce606cde4d2410fcbb3f440.diff
LOG: [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
Added:
clang/test/SemaObjC/block-capture-unused-variable.m
Modified:
clang/lib/Sema/SemaDecl.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index f4c0893cfcec2..d61570ee6a104 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1944,6 +1944,12 @@ void Sema::DiagnoseUnusedButSetDecl(const VarDecl *VD) {
}
}
+ // Don't warn about __block Objective-C pointer variables, as they might
+ // be assigned in the block but not used elsewhere for the purpose of lifetime
+ // extension.
+ if (VD->hasAttr<BlocksAttr>() && Ty->isObjCObjectPointerType())
+ return;
+
auto iter = RefsMinusAssignments.find(VD);
if (iter == RefsMinusAssignments.end())
return;
diff --git a/clang/test/SemaObjC/block-capture-unused-variable.m b/clang/test/SemaObjC/block-capture-unused-variable.m
new file mode 100644
index 0000000000000..1d40d9fb106b0
--- /dev/null
+++ b/clang/test/SemaObjC/block-capture-unused-variable.m
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -triple x86_64-apple-macos11 -fsyntax-only -fobjc-arc -fblocks -verify -Wunused-but-set-variable -Wno-objc-root-class %s
+
+typedef struct dispatch_queue_s *dispatch_queue_t;
+
+typedef void (^dispatch_block_t)(void);
+
+void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
+
+extern __attribute__((visibility("default"))) struct dispatch_queue_s _dispatch_main_q;
+
+id getFoo();
+
+ at protocol P
+
+ at end
+
+ at interface I
+
+ at end
+
+void test() {
+ // no diagnostics
+ __block id x = getFoo();
+ __block id<P> y = x;
+ __block I *z = (I *)x;
+ // diagnose non-block variables
+ id x2 = getFoo(); // expected-warning {{variable 'x2' set but not used}}
+ dispatch_async(&_dispatch_main_q, ^{
+ x = ((void *)0);
+ y = x;
+ z = ((void *)0);
+ });
+ x2 = getFoo();
+}
More information about the cfe-commits
mailing list