[clang] [Clang] Warn against unused parameters in C++ coroutines with `-Wunused-parameters` (PR #70567)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 31 23:24:30 PDT 2023


================
@@ -3165,7 +3165,16 @@ class Sema final {
 
   /// Diagnose any unused parameters in the given sequence of
   /// ParmVarDecl pointers.
-  void DiagnoseUnusedParameters(ArrayRef<ParmVarDecl *> Parameters);
+  ///
+  /// Normally, we check if the parameter decls have the Referenced bit set.
+  /// C++ Coroutines, however, are a special case due to the existences of
+  /// parameter moves (See Sema::buildCoroutineParameterMoves), the parameters
+  /// are always referenced in coroutines. Therefore, in the case of coroutines,
+  /// CoroutineBodyRefs must be passed to correctly diagnose parameter usages
+  /// as written by the user.
+  void DiagnoseUnusedParameters(
+      ArrayRef<ParmVarDecl *> Parameters,
+      llvm::SmallSet<ParmVarDecl *, 4> *CoroutineBodyRefs = nullptr);
----------------
ChuanqiXu9 wrote:

> If so, what about adding an assertion in the original version of DiagnoseUnusedParameters? Or should we keep the interface as is (allowing coroutines to be diagnosed in the same function, but handle the special case in DiagnoseUnusedParameters without changing the interface?)

Sorry, what do you mean by adding an assertion to DiagnoseUnusedParameters? Do you say doing so after adopting my suggest? Then, it sounds good but how can we do that? And it sounds not meaningful.

> CoroutineParameterMoves does not give me what I want. It points me to Stmts that moved the parameters while I need the opposite -- All other references to the parameters.

Sorry for misleading. But let's try to get a solution light-weight enough.  I don't want to pay a lot for this diagnostic .  An instinct idea now is to mark all the parameters as non referenced/used after building CoroutineParameterMoves. But it needs to be verified.


https://github.com/llvm/llvm-project/pull/70567


More information about the cfe-commits mailing list