================
@@ -9412,65 +9412,80 @@ void 
Sema::DeclareImplicitEqualityComparison(CXXRecordDecl *RD,
   popCodeSynthesisContext();
 }
 
-void Sema::DefineDefaultedComparison(SourceLocation UseLoc, FunctionDecl *FD,
-                                     DefaultedComparisonKind DCK) {
+/// Define a defaulted function whose body is synthesized by \p BuildBody: a
+/// defaulted comparison operator function or a defaulted postfix increment or
+/// decrement operator function.
+static void defineDefaultedFunctionWithSynthesizedBody(
+    Sema &S, SourceLocation UseLoc, FunctionDecl *FD,
+    llvm::function_ref<StmtResult(SourceLocation BodyLoc)> BuildBody) {
   assert(FD->isDefaulted() && !FD->isDeleted() &&
          !FD->doesThisDeclarationHaveABody());
   if (FD->willHaveBody() || FD->isInvalidDecl())
     return;
 
-  SynthesizedFunctionScope Scope(*this, FD);
+  Sema::SynthesizedFunctionScope Scope(S, FD);
 
   // Add a context note for diagnostics produced after this point.
   Scope.addContextNote(UseLoc);
 
-  DefaultedFunctionFPFeaturesRAII RestoreFP(*this, FD);
+  DefaultedFunctionFPFeaturesRAII RestoreFP(S, FD);
 
   {
     // Build and set up the function body.
-    // The first parameter has type maybe-ref-to maybe-const T, use that to get
-    // the type of the class being compared.
-    auto PT = FD->getParamDecl(0)->getType();
-    CXXRecordDecl *RD = PT.getNonReferenceType()->getAsCXXRecordDecl();
     SourceLocation BodyLoc =
         FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation();
-    StmtResult Body =
-        DefaultedComparisonSynthesizer(*this, RD, FD, DCK, BodyLoc).build();
+    StmtResult Body = BuildBody(BodyLoc);
     if (Body.isInvalid()) {
       FD->setInvalidDecl();
       return;
     }
     FD->setBody(Body.get());
-    FD->markUsed(Context);
+    FD->markUsed(S.Context);
   }
 
   // The exception specification is needed because we are defining the
   // function. Note that this will reuse the body we just built.
-  ResolveExceptionSpec(UseLoc, FD->getType()->castAs<FunctionProtoType>());
+  S.ResolveExceptionSpec(UseLoc, FD->getType()->castAs<FunctionProtoType>());
 
-  if (ASTMutationListener *L = getASTMutationListener())
+  if (ASTMutationListener *L = S.getASTMutationListener())
     L->CompletedImplicitDefinition(FD);
 }
 
+void Sema::DefineDefaultedComparison(SourceLocation UseLoc, FunctionDecl *FD,
+                                     DefaultedComparisonKind DCK) {
+  defineDefaultedFunctionWithSynthesizedBody(
+      *this, UseLoc, FD, [&](SourceLocation BodyLoc) {
+        // The first parameter has type maybe-ref-to maybe-const T, use that to
+        // get the type of the class being compared.
+        auto PT = FD->getParamDecl(0)->getType();
+        CXXRecordDecl *RD = PT.getNonReferenceType()->getAsCXXRecordDecl();
+        return DefaultedComparisonSynthesizer(*this, RD, FD, DCK, BodyLoc)
+            .build();
+      });
----------------
Sirraide wrote:

Can you assign the lambda to a local variable instead and then pass that to the 
function, a call expression that’s this many lines long is a bit hard to read.

https://github.com/llvm/llvm-project/pull/229560
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to