[clang] [Clang] Fix the order of addInstantiatedParameters in LambdaScopeForCallOperatorInstantiationRAII (PR #97215)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Sat Jul 6 04:37:25 PDT 2024


================
@@ -2379,23 +2379,32 @@ Sema::LambdaScopeForCallOperatorInstantiationRAII::
 
   SemaRef.RebuildLambdaScopeInfo(cast<CXXMethodDecl>(FD));
 
-  FunctionDecl *Pattern = getPatternFunctionDecl(FD);
-  if (Pattern) {
-    SemaRef.addInstantiatedCapturesToScope(FD, Pattern, Scope, MLTAL);
+  FunctionDecl *FDPattern = getPatternFunctionDecl(FD);
+  if (!FDPattern)
+    return;
 
-    FunctionDecl *ParentFD = FD;
-    while (ShouldAddDeclsFromParentScope) {
+  SemaRef.addInstantiatedCapturesToScope(FD, FDPattern, Scope, MLTAL);
 
-      ParentFD =
-          dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(ParentFD));
-      Pattern =
-          dyn_cast<FunctionDecl>(getLambdaAwareParentOfDeclContext(Pattern));
+  if (!ShouldAddDeclsFromParentScope)
+    return;
 
-      if (!ParentFD || !Pattern)
-        break;
+  llvm::SmallVector<std::pair<FunctionDecl *, FunctionDecl *>, 4>
+      ParentInstantiations;
+  std::pair<FunctionDecl *, FunctionDecl *> Current = {FDPattern, FD};
+  while (true) {
+    Current.first = dyn_cast<FunctionDecl>(
+        getLambdaAwareParentOfDeclContext(Current.first));
+    Current.second = dyn_cast<FunctionDecl>(
+        getLambdaAwareParentOfDeclContext(Current.second));
 
-      SemaRef.addInstantiatedParametersToScope(ParentFD, Pattern, Scope, MLTAL);
-      SemaRef.addInstantiatedLocalVarsToScope(ParentFD, Pattern, Scope);
-    }
+    if (!Current.first || !Current.second)
+      break;
+
+    ParentInstantiations.push_back(Current);
+  }
+
+  for (const auto &[Pattern, Inst] : llvm::reverse(ParentInstantiations)) {
----------------
zyn0217 wrote:

I think it would be better to retain the name `PatternFD`.
In addition, can you also add some comments to explain why we're enumerating instantiations in the reversed way here? An example would be
```
// Add instantiated parameters to scopes in the order from the outer lambda to
// the inner lambda. We do so because the inner parameters could depend on
// any outer template parameters.
```

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


More information about the cfe-commits mailing list