[clang-tools-extra] [clangd] Extract to function: Do not reject unconditionally in C files (PR #228465)

Aleksandr Platonov via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 6 01:09:50 PDT 2026


================
@@ -1035,10 +1137,39 @@ bool createParameters(NewFunction &ExtractedFunc,
         TypeInfo.addConst();
       }
     }
+
+    // C has no references. A parameter that would otherwise need one
+    // becomes a real pointer instead: the call site takes the address of
+    // the original variable explicitly, and every use of it in the
+    // copied-out body is rewritten into a dereference (see
+    // NewFunction::getFuncBody). Array types are the exception: they decay
+    // to a pointer on their own wherever they're used, so no rewriting or
+    // address-of is needed for them at all.
+    if (Kind == ParamPassKind::Reference && !LangOpts.CPlusPlus) {
+      if (TypeInfo->isArrayType()) {
+        Kind = ParamPassKind::Value;
----------------
ArcsinX wrote:

Array type is something like `int[10]` and `NewFunction::Parameter::render()` will produce uncompilable code like `int[10] A`.
So, I guess we need a special handling for arrays to replace `int[5]` with `int *`, maybe we can do this by changing `TypeInfo` here, thus `type + " " + name` still will be ok for arrays too.

Maybe we can also take into account a case when we extract to function code containing `sizeof(arr)`, because `sizeof` for pointer is not the same as `sizeof` for array

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


More information about the cfe-commits mailing list