[clang] [Clang] Preserve coroutine parameter referenced state (PR #70973)
Yuxuan Chen via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 1 12:34:37 PDT 2023
https://github.com/yuxuanchen1997 updated https://github.com/llvm/llvm-project/pull/70973
>From 3be73f10d376739460f0c22d3292bc0de8f64b82 Mon Sep 17 00:00:00 2001
From: Yuxuan Chen <ych at meta.com>
Date: Wed, 1 Nov 2023 11:59:57 -0700
Subject: [PATCH] [Clang] Preserve coroutine parameter referenced state
---
clang/lib/Sema/SemaCoroutine.cpp | 6 ++++
.../warn-unused-parameters-coroutine.cpp | 34 +++++++++++++++++++
2 files changed, 40 insertions(+)
create mode 100644 clang/test/SemaCXX/warn-unused-parameters-coroutine.cpp
diff --git a/clang/lib/Sema/SemaCoroutine.cpp b/clang/lib/Sema/SemaCoroutine.cpp
index 38ac406b14adadf..bee80db8d166a68 100644
--- a/clang/lib/Sema/SemaCoroutine.cpp
+++ b/clang/lib/Sema/SemaCoroutine.cpp
@@ -1965,9 +1965,15 @@ bool Sema::buildCoroutineParameterMoves(SourceLocation Loc) {
if (PD->getType()->isDependentType())
continue;
+ // Preserve the referenced state for unused parameter diagnostics.
+ bool DeclReferenced = PD->isReferenced();
+
ExprResult PDRefExpr =
BuildDeclRefExpr(PD, PD->getType().getNonReferenceType(),
ExprValueKind::VK_LValue, Loc); // FIXME: scope?
+
+ PD->setReferenced(DeclReferenced);
+
if (PDRefExpr.isInvalid())
return false;
diff --git a/clang/test/SemaCXX/warn-unused-parameters-coroutine.cpp b/clang/test/SemaCXX/warn-unused-parameters-coroutine.cpp
new file mode 100644
index 000000000000000..b4c01550f9f7883
--- /dev/null
+++ b/clang/test/SemaCXX/warn-unused-parameters-coroutine.cpp
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -fsyntax-only -Wunused-parameter -verify -std=c++20 %s
+
+#include "Inputs/std-coroutine.h"
+
+struct awaitable {
+ bool await_ready() noexcept;
+ void await_resume() noexcept;
+ void await_suspend(std::coroutine_handle<>) noexcept;
+};
+
+struct task : awaitable {
+ struct promise_type {
+ task get_return_object() noexcept;
+ awaitable initial_suspend() noexcept;
+ awaitable final_suspend() noexcept;
+ void unhandled_exception() noexcept;
+ void return_void() noexcept;
+ };
+};
+
+task foo(int a) { // expected-warning{{unused parameter 'a'}}
+ co_return;
+}
+
+task bar(int a, int b) { // expected-warning{{unused parameter 'b'}}
+ a = a + 1;
+ co_return;
+}
+
+void create_closure() {
+ auto closure = [](int c) -> task { // expected-warning{{unused parameter 'c'}}
+ co_return;
+ };
+}
More information about the cfe-commits
mailing list