[clang-tools-extra] [clangd] Extract to Function: mark unmodified captured parameters const (PR #225666)

Aleksandr Platonov via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 29 05:01:29 PDT 2026


================
@@ -697,13 +742,189 @@ CapturedZoneInfo captureZoneInfo(const ExtractionZone &ExtZone) {
       if (!DeclInfo)
         DeclInfo = Info.createDeclInfo(D, ZoneRelative::OutsideFunc);
       DeclInfo->markOccurence(CurrentLocation);
-      // FIXME: check if reference mutates the Decl being referred.
+      return true;
+    }
+
+    // Conservatively marks D as possibly mutated: used both for actual
+    // direct mutations (assignment, increment/decrement, ...) and for
+    // constructs that alias D in a way we don't want to trace further (a
+    // reference bound to D, D's address taken, D captured by reference in a
+    // lambda, ...). We never try to determine whether such an alias is
+    // itself later mutated -- that would require searching beyond this one
+    // occurrence, which is exactly the cost this design avoids (and what
+    // makes ExprMutationAnalyzer prohibitively slow). The price is
+    // that we sometimes keep a parameter non-const where a full alias
+    // analysis could prove it safe to const; we never get this wrong in the
+    // other, unsafe direction.
+    void markPossiblyMutated(const Decl *D) {
+      if (!D || CurrentLocation != ZoneRelative::Inside)
+        return;
+      if (auto *DeclInfo = Info.getDeclInfoFor(D))
+        DeclInfo->IsPossiblyMutated = true;
+    }
+    void markPossiblyMutated(const Expr *E) {
+      markPossiblyMutated(underlyingDecl(E));
----------------
ArcsinX wrote:

There is still a problem, that `underlyingDecl()` returns `nullptr` for `AbstractConditionalOperator`, as the result we process `(B ? C : D).A = 1;` incorrectly, marking everything as `const`.

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


More information about the cfe-commits mailing list