[clang] [clang-tools-extra] [llvm] [clang] WIP: Improved Context Declaration tracking (PR #107942)

Matheus Izvekov via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 10 08:23:33 PST 2024


================
@@ -17658,8 +17662,49 @@ HandleImmediateInvocations(Sema &SemaRef,
   }
 }
 
+static void setContextDecl(Sema &S, Decl *Base, Decl *ContextDecl) {
+  switch (Base->getKind()) {
+  case Decl::CXXRecord: {
+    auto *RD = cast<CXXRecordDecl>(Base);
+    RD->setLambdaContextDecl(ContextDecl);
+    S.handleLambdaNumbering(RD, RD->getLambdaCallOperator(),
+                            /*NumberingOverride=*/std::nullopt,
+                            /*InSignature=*/true);
+  } break;
+  case Decl::RequiresExprBody:
+    cast<RequiresExprBodyDecl>(Base)->setContextDecl(ContextDecl);
+    break;
+  default:
+    llvm_unreachable("Undexpected Decl Kind");
+  }
+}
+
+void Sema::UpdateCurrentContextDecl(Decl *ContextDecl) {
+  assert(ContextDecl);
+  ExpressionEvaluationContextRecord &Rec = ExprEvalContexts.back();
+  assert(!Rec.ContextDecl.hasValue());
+  assert(Rec.LazyContextDeclPos <= PendingLazyContextDecls.size());
+  Rec.ContextDecl = ContextDecl;
+  while (PendingLazyContextDecls.size() > Rec.LazyContextDeclPos)
+    setContextDecl(*this, PendingLazyContextDecls.pop_back_val(), ContextDecl);
+}
+
 void Sema::PopExpressionEvaluationContext() {
   ExpressionEvaluationContextRecord& Rec = ExprEvalContexts.back();
+  assert(Rec.LazyContextDeclPos <= PendingLazyContextDecls.size());
+  if (!Rec.HasReusedDeclContext) {
+    if (Rec.ContextDecl.hasValue()) {
+      assert(Rec.LazyContextDeclPos == PendingLazyContextDecls.size());
----------------
mizvekov wrote:

Yeah that's basically it. If we have already created the parent, we shouldn't be using the Pending list at all.
If this originally started as a lazy context, then we are in a state where we know the parent, so we should have already handled and emptied the lazy list, and shouldn't be adding new stuff to it anymore.

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


More information about the llvm-commits mailing list