@@ -616,29 +616,30 @@ Sema::InstantiatingTemplate::InstantiatingTemplate(
616616 Invalid = true ;
617617 return ;
618618 }
619- Invalid = CheckInstantiationDepth (PointOfInstantiation, InstantiationRange);
619+
620+ CodeSynthesisContext Inst;
621+ Inst.Kind = Kind;
622+ Inst.PointOfInstantiation = PointOfInstantiation;
623+ Inst.Entity = Entity;
624+ Inst.Template = Template;
625+ Inst.TemplateArgs = TemplateArgs.data ();
626+ Inst.NumTemplateArgs = TemplateArgs.size ();
627+ Inst.DeductionInfo = DeductionInfo;
628+ Inst.InstantiationRange = InstantiationRange;
629+ Inst.InConstraintSubstitution =
630+ Inst.Kind == CodeSynthesisContext::ConstraintSubstitution;
631+ if (!SemaRef.CodeSynthesisContexts .empty ())
632+ Inst.InConstraintSubstitution |=
633+ SemaRef.CodeSynthesisContexts .back ().InConstraintSubstitution ;
634+
635+ Invalid = SemaRef.pushCodeSynthesisContext (Inst);
620636 if (!Invalid) {
621- CodeSynthesisContext Inst;
622- Inst.Kind = Kind;
623- Inst.PointOfInstantiation = PointOfInstantiation;
624- Inst.Entity = Entity;
625- Inst.Template = Template;
626- Inst.TemplateArgs = TemplateArgs.data ();
627- Inst.NumTemplateArgs = TemplateArgs.size ();
628- Inst.DeductionInfo = DeductionInfo;
629- Inst.InstantiationRange = InstantiationRange;
630- Inst.InConstraintSubstitution =
631- Inst.Kind == CodeSynthesisContext::ConstraintSubstitution;
632- if (!SemaRef.CodeSynthesisContexts .empty ())
633- Inst.InConstraintSubstitution |=
634- SemaRef.CodeSynthesisContexts .back ().InConstraintSubstitution ;
635-
636- SemaRef.pushCodeSynthesisContext (Inst);
637-
638- AlreadyInstantiating = !Inst.Entity ? false :
639- !SemaRef.InstantiatingSpecializations
640- .insert ({Inst.Entity ->getCanonicalDecl (), Inst.Kind })
641- .second ;
637+ AlreadyInstantiating =
638+ !Inst.Entity
639+ ? false
640+ : !SemaRef.InstantiatingSpecializations
641+ .insert ({Inst.Entity ->getCanonicalDecl (), Inst.Kind })
642+ .second ;
642643 atTemplateBegin (SemaRef.TemplateInstCallbacks , SemaRef, Inst);
643644 }
644645}
@@ -834,18 +835,34 @@ Sema::InstantiatingTemplate::InstantiatingTemplate(
834835 : InstantiatingTemplate(SemaRef, CodeSynthesisContext::PartialOrderingTTP,
835836 ArgLoc, InstantiationRange, PArg) {}
836837
837- void Sema::pushCodeSynthesisContext (CodeSynthesisContext Ctx) {
838+ bool Sema::pushCodeSynthesisContext (CodeSynthesisContext Ctx) {
838839 Ctx.SavedInNonInstantiationSFINAEContext = InNonInstantiationSFINAEContext;
839840 InNonInstantiationSFINAEContext = false ;
840841
841- CodeSynthesisContexts.push_back (Ctx);
842-
843- if (!Ctx.isInstantiationRecord ())
842+ if (!Ctx.isInstantiationRecord ()) {
844843 ++NonInstantiationEntries;
844+ } else {
845+ assert (SemaRef.NonInstantiationEntries <=
846+ SemaRef.CodeSynthesisContexts .size ());
847+ if ((SemaRef.CodeSynthesisContexts .size () -
848+ SemaRef.NonInstantiationEntries ) >
849+ SemaRef.getLangOpts ().InstantiationDepth ) {
850+ SemaRef.Diag (Ctx.PointOfInstantiation ,
851+ diag::err_template_recursion_depth_exceeded)
852+ << SemaRef.getLangOpts ().InstantiationDepth << Ctx.InstantiationRange ;
853+ SemaRef.Diag (Ctx.PointOfInstantiation ,
854+ diag::note_template_recursion_depth)
855+ << SemaRef.getLangOpts ().InstantiationDepth ;
856+ return true ;
857+ }
858+ }
859+
860+ CodeSynthesisContexts.push_back (Ctx);
845861
846862 // Check to see if we're low on stack space. We can't do anything about this
847863 // from here, but we can at least warn the user.
848864 StackHandler.warnOnStackNearlyExhausted (Ctx.PointOfInstantiation );
865+ return false ;
849866}
850867
851868void Sema::popCodeSynthesisContext () {
@@ -907,25 +924,6 @@ static std::string convertCallArgsToString(Sema &S,
907924 return Result;
908925}
909926
910- bool Sema::InstantiatingTemplate::CheckInstantiationDepth (
911- SourceLocation PointOfInstantiation,
912- SourceRange InstantiationRange) {
913- assert (SemaRef.NonInstantiationEntries <=
914- SemaRef.CodeSynthesisContexts .size ());
915- if ((SemaRef.CodeSynthesisContexts .size () -
916- SemaRef.NonInstantiationEntries )
917- <= SemaRef.getLangOpts ().InstantiationDepth )
918- return false ;
919-
920- SemaRef.Diag (PointOfInstantiation,
921- diag::err_template_recursion_depth_exceeded)
922- << SemaRef.getLangOpts ().InstantiationDepth
923- << InstantiationRange;
924- SemaRef.Diag (PointOfInstantiation, diag::note_template_recursion_depth)
925- << SemaRef.getLangOpts ().InstantiationDepth ;
926- return true ;
927- }
928-
929927void Sema::PrintInstantiationStack (InstantiationContextDiagFuncRef DiagFunc) {
930928 // Determine which template instantiations to skip, if any.
931929 unsigned SkipStart = CodeSynthesisContexts.size (), SkipEnd = SkipStart;
0 commit comments